blob: 8a47f8c2397376e5c762f31c4890418cc1e43f59 [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());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000372 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000373 // 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.org381abbb2009-02-25 13:23:22 +0000400MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(Slot* slot,
401 Register tmp,
402 Register tmp2,
403 Label* slow) {
404 ASSERT(slot->type() == Slot::CONTEXT);
405 int index = slot->index();
406 Register context = cp;
407 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
408 if (s->num_heap_slots() > 0) {
409 if (s->calls_eval()) {
410 // Check that extension is NULL.
411 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
412 __ tst(tmp2, tmp2);
413 __ b(ne, slow);
414 }
415 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
416 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
417 context = tmp;
418 }
419 }
420 // Check that last extension is NULL.
421 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
422 __ tst(tmp2, tmp2);
423 __ b(ne, slow);
424 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
425 return ContextOperand(tmp, index);
426}
427
428
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000429// Loads a value on TOS. If it is a boolean value, the result may have been
430// (partially) translated into branches, or it may have set the condition
431// code register. If force_cc is set, the value is forced to set the
432// condition code register and no value is pushed. If the condition code
433// register was set, has_cc() is true and cc_reg_ contains the condition to
434// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000435void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000436 TypeofState typeof_state,
437 Label* true_target,
438 Label* false_target,
439 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000440 ASSERT(!has_cc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
ager@chromium.org7c537e22008-10-16 08:43:32 +0000442 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000443 Visit(x);
444 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000446 // Convert the TOS value to a boolean in the condition code register.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000447 // Visiting an expression may possibly choose neither (a) to leave a
448 // value in the condition code register nor (b) to leave a value in TOS
449 // (eg, by compiling to only jumps to the targets). In that case the
450 // code generated by ToBoolean is wrong because it assumes the value of
451 // the expression in TOS. So long as there is always a value in TOS or
452 // the condition code register when control falls through to here (there
453 // is), the code generated by ToBoolean is dead and therefore safe.
mads.s.ager31e71382008-08-13 09:32:07 +0000454 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 }
456 ASSERT(has_cc() || !force_cc);
457}
458
459
ager@chromium.org7c537e22008-10-16 08:43:32 +0000460void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 Label true_target;
462 Label false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000463 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464
465 if (has_cc()) {
466 // convert cc_reg_ into a bool
467 Label loaded, materialize_true;
468 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000469 __ mov(r0, Operand(Factory::false_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000470 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 __ b(&loaded);
472 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000473 __ mov(r0, Operand(Factory::true_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000474 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 __ bind(&loaded);
476 cc_reg_ = al;
477 }
478
479 if (true_target.is_linked() || false_target.is_linked()) {
480 // we have at least one condition value
481 // that has been "translated" into a branch,
482 // thus it needs to be loaded explicitly again
483 Label loaded;
484 __ b(&loaded); // don't lose current TOS
485 bool both = true_target.is_linked() && false_target.is_linked();
486 // reincarnate "true", if necessary
487 if (true_target.is_linked()) {
488 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000489 __ mov(r0, Operand(Factory::true_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000490 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 }
492 // if both "true" and "false" need to be reincarnated,
493 // jump across code for "false"
494 if (both)
495 __ b(&loaded);
496 // reincarnate "false", if necessary
497 if (false_target.is_linked()) {
498 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000499 __ mov(r0, Operand(Factory::false_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000500 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 }
502 // everything is loaded at this point
503 __ bind(&loaded);
504 }
505 ASSERT(!has_cc());
506}
507
508
ager@chromium.org7c537e22008-10-16 08:43:32 +0000509void CodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000510 __ ldr(r0, GlobalObject());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000511 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512}
513
514
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000515void CodeGenerator::LoadGlobalReceiver(Register scratch) {
516 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
517 __ ldr(scratch,
518 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
519 frame_->Push(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000520}
521
522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000524// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
525// variables w/o reference errors elsewhere.
526void CodeGenerator::LoadTypeofExpression(Expression* x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 Variable* variable = x->AsVariableProxy()->AsVariable();
528 if (variable != NULL && !variable->is_this() && variable->is_global()) {
529 // NOTE: This is somewhat nasty. We force the compiler to load
530 // the variable as if through '<global>.<variable>' to make sure we
531 // do not get reference errors.
532 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
533 Literal key(variable->name());
534 // TODO(1241834): Fetch the position from the variable instead of using
535 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000536 Property property(&global, &key, RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 Load(&property);
538 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000539 Load(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 }
541}
542
543
ager@chromium.org7c537e22008-10-16 08:43:32 +0000544Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
546 cgen->LoadReference(this);
547}
548
549
550Reference::~Reference() {
551 cgen_->UnloadReference(this);
552}
553
554
ager@chromium.org7c537e22008-10-16 08:43:32 +0000555void CodeGenerator::LoadReference(Reference* ref) {
556 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 Expression* e = ref->expression();
558 Property* property = e->AsProperty();
559 Variable* var = e->AsVariableProxy()->AsVariable();
560
561 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000562 // The expression is either a property or a variable proxy that rewrites
563 // to a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 Load(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000565 // We use a named reference if the key is a literal symbol, unless it is
566 // a string that can be legally parsed as an integer. This is because
567 // otherwise we will not get into the slow case code that handles [] on
568 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 Literal* literal = property->key()->AsLiteral();
570 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000571 if (literal != NULL &&
572 literal->handle()->IsSymbol() &&
573 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 ref->set_type(Reference::NAMED);
575 } else {
576 Load(property->key());
577 ref->set_type(Reference::KEYED);
578 }
579 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000580 // The expression is a variable proxy that does not rewrite to a
581 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 LoadGlobal();
584 ref->set_type(Reference::NAMED);
585 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000586 ASSERT(var->slot() != NULL);
587 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 }
589 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000590 // Anything else is a runtime error.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 Load(e);
592 __ CallRuntime(Runtime::kThrowReferenceError, 1);
593 }
594}
595
596
ager@chromium.org7c537e22008-10-16 08:43:32 +0000597void CodeGenerator::UnloadReference(Reference* ref) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000598 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000599 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000601 if (size > 0) {
602 frame_->Pop(r0);
603 frame_->Drop(size);
604 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 }
606}
607
608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
610// register to a boolean in the condition code register. The code
611// may jump to 'false_target' in case the register converts to 'false'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000612void CodeGenerator::ToBoolean(Label* true_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000613 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000614 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 // Only the condition code should be set.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000616 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617
618 // Fast case checks
619
mads.s.ager31e71382008-08-13 09:32:07 +0000620 // Check if the value is 'false'.
621 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 __ b(eq, false_target);
623
mads.s.ager31e71382008-08-13 09:32:07 +0000624 // Check if the value is 'true'.
625 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 __ b(eq, true_target);
627
mads.s.ager31e71382008-08-13 09:32:07 +0000628 // Check if the value is 'undefined'.
629 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 __ b(eq, false_target);
631
mads.s.ager31e71382008-08-13 09:32:07 +0000632 // Check if the value is a smi.
633 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000635 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 __ b(eq, true_target);
637
638 // Slow case: call the runtime.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000639 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000640 __ CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000641 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643
644 cc_reg_ = ne;
645}
646
647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648class GetPropertyStub : public CodeStub {
649 public:
650 GetPropertyStub() { }
651
652 private:
653 Major MajorKey() { return GetProperty; }
654 int MinorKey() { return 0; }
655 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656};
657
658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659class SetPropertyStub : public CodeStub {
660 public:
661 SetPropertyStub() { }
662
663 private:
664 Major MajorKey() { return SetProperty; }
665 int MinorKey() { return 0; }
666 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667};
668
669
kasper.lund7276f142008-07-30 08:49:36 +0000670class GenericBinaryOpStub : public CodeStub {
671 public:
672 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
673
674 private:
675 Token::Value op_;
676
677 Major MajorKey() { return GenericBinaryOp; }
678 int MinorKey() { return static_cast<int>(op_); }
679 void Generate(MacroAssembler* masm);
680
681 const char* GetName() {
682 switch (op_) {
683 case Token::ADD: return "GenericBinaryOpStub_ADD";
684 case Token::SUB: return "GenericBinaryOpStub_SUB";
685 case Token::MUL: return "GenericBinaryOpStub_MUL";
686 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000687 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
688 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
689 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
690 case Token::SAR: return "GenericBinaryOpStub_SAR";
691 case Token::SHL: return "GenericBinaryOpStub_SHL";
692 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000693 default: return "GenericBinaryOpStub";
694 }
695 }
696
697#ifdef DEBUG
698 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
699#endif
700};
701
702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703class InvokeBuiltinStub : public CodeStub {
704 public:
705 enum Kind { Inc, Dec, ToNumber };
706 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
707
708 private:
709 Kind kind_;
710 int argc_;
711
712 Major MajorKey() { return InvokeBuiltin; }
713 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
714 void Generate(MacroAssembler* masm);
715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716#ifdef DEBUG
717 void Print() {
718 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
719 static_cast<int>(kind_),
720 argc_);
721 }
722#endif
723};
724
725
ager@chromium.org7c537e22008-10-16 08:43:32 +0000726void CodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +0000727 // sp[0] : y
728 // sp[1] : x
729 // result : r0
730
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 // Stub is entered with a call: 'return address' is in lr.
732 switch (op) {
733 case Token::ADD: // fall through.
734 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000735 case Token::MUL:
736 case Token::BIT_OR:
737 case Token::BIT_AND:
738 case Token::BIT_XOR:
739 case Token::SHL:
740 case Token::SHR:
741 case Token::SAR: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000742 frame_->Pop(r0); // r0 : y
743 frame_->Pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +0000744 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 __ CallStub(&stub);
746 break;
747 }
748
749 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +0000750 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000751 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 break;
753 }
754
755 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +0000756 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000757 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 break;
759 }
760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 case Token::COMMA:
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000762 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 // simply discard left value
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000764 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 break;
766
767 default:
768 // Other cases should have been handled before this point.
769 UNREACHABLE();
770 break;
771 }
772}
773
774
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000775class DeferredInlinedSmiOperation: public DeferredCode {
776 public:
777 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
778 int value, bool reversed) :
779 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
780 set_comment("[ DeferredInlinedSmiOperation");
781 }
782
783 virtual void Generate() {
784 switch (op_) {
785 case Token::ADD: {
786 if (reversed_) {
787 // revert optimistic add
788 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
789 __ mov(r1, Operand(Smi::FromInt(value_))); // x
790 } else {
791 // revert optimistic add
792 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
793 __ mov(r0, Operand(Smi::FromInt(value_)));
794 }
795 break;
796 }
797
798 case Token::SUB: {
799 if (reversed_) {
800 // revert optimistic sub
801 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
802 __ mov(r1, Operand(Smi::FromInt(value_)));
803 } else {
804 __ add(r1, r0, Operand(Smi::FromInt(value_)));
805 __ mov(r0, Operand(Smi::FromInt(value_)));
806 }
807 break;
808 }
809
810 case Token::BIT_OR:
811 case Token::BIT_XOR:
812 case Token::BIT_AND: {
813 if (reversed_) {
814 __ mov(r1, Operand(Smi::FromInt(value_)));
815 } else {
816 __ mov(r1, Operand(r0));
817 __ mov(r0, Operand(Smi::FromInt(value_)));
818 }
819 break;
820 }
821
822 case Token::SHL:
823 case Token::SHR:
824 case Token::SAR: {
825 if (!reversed_) {
826 __ mov(r1, Operand(r0));
827 __ mov(r0, Operand(Smi::FromInt(value_)));
828 } else {
829 UNREACHABLE(); // should have been handled in SmiOperation
830 }
831 break;
832 }
833
834 default:
835 // other cases should have been handled before this point.
836 UNREACHABLE();
837 break;
838 }
839
840 GenericBinaryOpStub igostub(op_);
841 __ CallStub(&igostub);
842 }
843
844 private:
845 Token::Value op_;
846 int value_;
847 bool reversed_;
848};
849
850
ager@chromium.org7c537e22008-10-16 08:43:32 +0000851void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000852 Handle<Object> value,
853 bool reversed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 // NOTE: This is an attempt to inline (a bit) more of the code for
855 // some possible smi operations (like + and -) when (at least) one
856 // of the operands is a literal smi. With this optimization, the
857 // performance of the system is increased by ~15%, and the generated
858 // code size is increased by ~1% (measured on a combination of
859 // different benchmarks).
860
mads.s.ager31e71382008-08-13 09:32:07 +0000861 // sp[0] : operand
862
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000863 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
865 Label exit;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000866 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 switch (op) {
869 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000870 DeferredCode* deferred =
871 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000873 __ add(r0, r0, Operand(value), SetCC);
874 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000876 __ b(ne, deferred->enter());
877 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 break;
879 }
880
881 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000882 DeferredCode* deferred =
883 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000886 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000888 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000890 __ b(vs, deferred->enter());
891 __ tst(r0, Operand(kSmiTagMask));
892 __ b(ne, deferred->enter());
893 __ bind(deferred->exit());
894 break;
895 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000897 case Token::BIT_OR:
898 case Token::BIT_XOR:
899 case Token::BIT_AND: {
900 DeferredCode* deferred =
901 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
902 __ tst(r0, Operand(kSmiTagMask));
903 __ b(ne, deferred->enter());
904 switch (op) {
905 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
906 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
907 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
908 default: UNREACHABLE();
909 }
910 __ bind(deferred->exit());
911 break;
912 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000914 case Token::SHL:
915 case Token::SHR:
916 case Token::SAR: {
917 if (reversed) {
918 __ mov(ip, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000919 frame_->Push(ip);
920 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000921 GenericBinaryOperation(op);
922
923 } else {
924 int shift_value = int_value & 0x1f; // least significant 5 bits
925 DeferredCode* deferred =
926 new DeferredInlinedSmiOperation(this, op, shift_value, false);
927 __ tst(r0, Operand(kSmiTagMask));
928 __ b(ne, deferred->enter());
929 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
930 switch (op) {
931 case Token::SHL: {
932 __ mov(r2, Operand(r2, LSL, shift_value));
933 // check that the *unsigned* result fits in a smi
934 __ add(r3, r2, Operand(0x40000000), SetCC);
935 __ b(mi, deferred->enter());
936 break;
937 }
938 case Token::SHR: {
939 // LSR by immediate 0 means shifting 32 bits.
940 if (shift_value != 0) {
941 __ mov(r2, Operand(r2, LSR, shift_value));
942 }
943 // check that the *unsigned* result fits in a smi
944 // neither of the two high-order bits can be set:
945 // - 0x80000000: high bit would be lost when smi tagging
946 // - 0x40000000: this number would convert to negative when
947 // smi tagging these two cases can only happen with shifts
948 // by 0 or 1 when handed a valid smi
949 __ and_(r3, r2, Operand(0xc0000000), SetCC);
950 __ b(ne, deferred->enter());
951 break;
952 }
953 case Token::SAR: {
954 if (shift_value != 0) {
955 // ASR by immediate 0 means shifting 32 bits.
956 __ mov(r2, Operand(r2, ASR, shift_value));
957 }
958 break;
959 }
960 default: UNREACHABLE();
961 }
962 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
963 __ bind(deferred->exit());
964 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 break;
966 }
967
968 default:
969 if (!reversed) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000970 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000971 __ mov(r0, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000972 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 } else {
974 __ mov(ip, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000975 frame_->Push(ip);
976 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 }
kasper.lund7276f142008-07-30 08:49:36 +0000978 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 break;
980 }
981
982 __ bind(&exit);
983}
984
985
ager@chromium.org7c537e22008-10-16 08:43:32 +0000986void CodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +0000987 // sp[0] : y
988 // sp[1] : x
989 // result : cc register
990
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 // Strict only makes sense for equality comparisons.
992 ASSERT(!strict || cc == eq);
993
994 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000995 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
996 if (cc == gt || cc == le) {
997 cc = ReverseCondition(cc);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000998 frame_->Pop(r1);
999 frame_->Pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001000 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001001 frame_->Pop(r0);
1002 frame_->Pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001003 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 __ orr(r2, r0, Operand(r1));
1005 __ tst(r2, Operand(kSmiTagMask));
1006 __ b(eq, &smi);
1007
1008 // Perform non-smi comparison by runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001009 frame_->Push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010
1011 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001012 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 int argc;
1014 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001015 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 argc = 1;
1017 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001018 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 int ncr; // NaN compare result
1020 if (cc == lt || cc == le) {
1021 ncr = GREATER;
1022 } else {
1023 ASSERT(cc == gt || cc == ge); // remaining cases
1024 ncr = LESS;
1025 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001026 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001027 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 argc = 2;
1029 }
1030
1031 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1032 // tagged as a small integer.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001033 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001034 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001035 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 __ cmp(r0, Operand(0));
1037 __ b(&exit);
1038
1039 // test smi equality by pointer comparison.
1040 __ bind(&smi);
1041 __ cmp(r1, Operand(r0));
1042
1043 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 cc_reg_ = cc;
1045}
1046
1047
kasper.lund7276f142008-07-30 08:49:36 +00001048class CallFunctionStub: public CodeStub {
1049 public:
1050 explicit CallFunctionStub(int argc) : argc_(argc) {}
1051
1052 void Generate(MacroAssembler* masm);
1053
1054 private:
1055 int argc_;
1056
kasper.lund7276f142008-07-30 08:49:36 +00001057#if defined(DEBUG)
1058 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1059#endif // defined(DEBUG)
1060
1061 Major MajorKey() { return CallFunction; }
1062 int MinorKey() { return argc_; }
1063};
1064
1065
mads.s.ager31e71382008-08-13 09:32:07 +00001066// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001067void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001070 for (int i = 0; i < args->length(); i++) {
1071 Load(args->at(i));
1072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073
kasper.lund7276f142008-07-30 08:49:36 +00001074 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001075 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076
kasper.lund7276f142008-07-30 08:49:36 +00001077 // Use the shared code stub to call the function.
1078 CallFunctionStub call_function(args->length());
1079 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080
1081 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001082 __ ldr(cp, frame_->Context());
1083 frame_->Pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084}
1085
1086
ager@chromium.org7c537e22008-10-16 08:43:32 +00001087void CodeGenerator::Branch(bool if_true, Label* L) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 ASSERT(has_cc());
1089 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
1090 __ b(cc, L);
1091 cc_reg_ = al;
1092}
1093
1094
ager@chromium.org7c537e22008-10-16 08:43:32 +00001095void CodeGenerator::CheckStack() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 if (FLAG_check_stack) {
1097 Comment cmnt(masm_, "[ check stack");
1098 StackCheckStub stub;
1099 __ CallStub(&stub);
1100 }
1101}
1102
1103
ager@chromium.org7c537e22008-10-16 08:43:32 +00001104void CodeGenerator::VisitBlock(Block* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 Comment cmnt(masm_, "[ Block");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001106 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 node->set_break_stack_height(break_stack_height_);
1108 VisitStatements(node->statements());
1109 __ bind(node->break_target());
1110}
1111
1112
ager@chromium.org7c537e22008-10-16 08:43:32 +00001113void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00001114 __ mov(r0, Operand(pairs));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001115 frame_->Push(r0);
1116 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001117 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001118 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001120 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121}
1122
1123
ager@chromium.org7c537e22008-10-16 08:43:32 +00001124void CodeGenerator::VisitDeclaration(Declaration* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 Comment cmnt(masm_, "[ Declaration");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001126 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 Variable* var = node->proxy()->var();
1128 ASSERT(var != NULL); // must have been resolved
1129 Slot* slot = var->slot();
1130
1131 // If it was not possible to allocate the variable at compile time,
1132 // we need to "declare" it at runtime to make sure it actually
1133 // exists in the local context.
1134 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1135 // Variables with a "LOOKUP" slot were introduced as non-locals
1136 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001137 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001139 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001140 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001141 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 // Declaration nodes are always declared in only two modes.
1143 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1144 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001145 __ mov(r0, Operand(Smi::FromInt(attr)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001146 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 // Push initial value, if any.
1148 // Note: For variables we must not push an initial value (such as
1149 // 'undefined') because we may have a (legal) redeclaration and we
1150 // must not destroy the current value.
1151 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001152 __ mov(r0, Operand(Factory::the_hole_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001153 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 } else if (node->fun() != NULL) {
1155 Load(node->fun());
1156 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001157 __ mov(r0, Operand(0)); // no initial value!
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001158 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001160 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
1161 // Ignore the return value (declarations are statements).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 return;
1163 }
1164
1165 ASSERT(!var->is_global());
1166
1167 // If we have a function or a constant, we need to initialize the variable.
1168 Expression* val = NULL;
1169 if (node->mode() == Variable::CONST) {
1170 val = new Literal(Factory::the_hole_value());
1171 } else {
1172 val = node->fun(); // NULL if we don't have a function
1173 }
1174
1175 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001176 {
1177 // Set initial value.
1178 Reference target(this, node->proxy());
1179 Load(val);
1180 target.SetValue(NOT_CONST_INIT);
1181 // The reference is removed from the stack (preserving TOS) when
1182 // it goes out of scope.
1183 }
1184 // Get rid of the assigned value (declarations are statements).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001185 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 }
1187}
1188
1189
ager@chromium.org7c537e22008-10-16 08:43:32 +00001190void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 Comment cmnt(masm_, "[ ExpressionStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001192 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193 Expression* expression = node->expression();
1194 expression->MarkAsStatement();
1195 Load(expression);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001196 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197}
1198
1199
ager@chromium.org7c537e22008-10-16 08:43:32 +00001200void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 Comment cmnt(masm_, "// EmptyStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001202 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 // nothing to do
1204}
1205
1206
ager@chromium.org7c537e22008-10-16 08:43:32 +00001207void CodeGenerator::VisitIfStatement(IfStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 Comment cmnt(masm_, "[ IfStatement");
1209 // Generate different code depending on which
1210 // parts of the if statement are present or not.
1211 bool has_then_stm = node->HasThenStatement();
1212 bool has_else_stm = node->HasElseStatement();
1213
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001214 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215
1216 Label exit;
1217 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001218 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 Label then;
1220 Label else_;
1221 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001222 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 Branch(false, &else_);
1224 // then
1225 __ bind(&then);
1226 Visit(node->then_statement());
1227 __ b(&exit);
1228 // else
1229 __ bind(&else_);
1230 Visit(node->else_statement());
1231
1232 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001233 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 ASSERT(!has_else_stm);
1235 Label then;
1236 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001237 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &exit, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 Branch(false, &exit);
1239 // then
1240 __ bind(&then);
1241 Visit(node->then_statement());
1242
1243 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001244 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 ASSERT(!has_then_stm);
1246 Label else_;
1247 // if (!cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001248 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 Branch(true, &exit);
1250 // else
1251 __ bind(&else_);
1252 Visit(node->else_statement());
1253
1254 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001255 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 ASSERT(!has_then_stm && !has_else_stm);
1257 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001258 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &exit, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 if (has_cc()) {
1260 cc_reg_ = al;
1261 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001262 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 }
1264 }
1265
1266 // end
1267 __ bind(&exit);
1268}
1269
1270
ager@chromium.org7c537e22008-10-16 08:43:32 +00001271void CodeGenerator::CleanStack(int num_bytes) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001272 ASSERT(num_bytes % kPointerSize == 0);
1273 frame_->Drop(num_bytes / kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274}
1275
1276
ager@chromium.org7c537e22008-10-16 08:43:32 +00001277void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Comment cmnt(masm_, "[ ContinueStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001279 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1281 __ b(node->target()->continue_target());
1282}
1283
1284
ager@chromium.org7c537e22008-10-16 08:43:32 +00001285void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 Comment cmnt(masm_, "[ BreakStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001287 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1289 __ b(node->target()->break_target());
1290}
1291
1292
ager@chromium.org7c537e22008-10-16 08:43:32 +00001293void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 Comment cmnt(masm_, "[ ReturnStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001295 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00001297 // Move the function result into r0.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001298 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 __ b(&function_return_);
1301}
1302
1303
ager@chromium.org7c537e22008-10-16 08:43:32 +00001304void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 Comment cmnt(masm_, "[ WithEnterStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001306 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001308 if (node->is_catch_block()) {
1309 __ CallRuntime(Runtime::kPushCatchContext, 1);
1310 } else {
1311 __ CallRuntime(Runtime::kPushContext, 1);
1312 }
kasper.lund7276f142008-07-30 08:49:36 +00001313 if (kDebug) {
1314 Label verified_true;
1315 __ cmp(r0, Operand(cp));
1316 __ b(eq, &verified_true);
1317 __ stop("PushContext: r0 is expected to be the same as cp");
1318 __ bind(&verified_true);
1319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001321 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322}
1323
1324
ager@chromium.org7c537e22008-10-16 08:43:32 +00001325void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 Comment cmnt(masm_, "[ WithExitStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001327 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 // Pop context.
1329 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1330 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001331 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332}
1333
1334
ager@chromium.org7c537e22008-10-16 08:43:32 +00001335int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
1336 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001337}
1338
ager@chromium.org7c537e22008-10-16 08:43:32 +00001339int CodeGenerator::FastCaseSwitchMinCaseCount() {
1340 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001341}
1342
1343
ager@chromium.org7c537e22008-10-16 08:43:32 +00001344void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001345 SwitchStatement* node,
1346 int min_index,
1347 int range,
1348 Label* fail_label,
1349 Vector<Label*> case_targets,
1350 Vector<Label> case_labels) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001351
1352 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
1353
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001354 frame_->Pop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001355
1356 // Test for a Smi value in a HeapNumber.
1357 Label is_smi;
1358 __ tst(r0, Operand(kSmiTagMask));
1359 __ b(eq, &is_smi);
1360 __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag));
1361 __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag));
1362 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
1363 __ b(ne, fail_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001364 frame_->Push(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001365 __ CallRuntime(Runtime::kNumberToSmi, 1);
1366 __ bind(&is_smi);
1367
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001368 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001369 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001370 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001371 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1372 if (Smi::IsValid(-min_index)) {
1373 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1374 } else {
1375 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1376 __ add(r0, r0, Operand(Smi::FromInt(1)));
1377 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001378 } else {
1379 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1380 }
1381 }
1382 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
1383 __ b(ne, fail_label);
1384 __ cmp(r0, Operand(Smi::FromInt(range)));
1385 __ b(ge, fail_label);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001386 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001387
1388 GenerateFastCaseSwitchCases(node, case_labels);
1389}
1390
1391
ager@chromium.org7c537e22008-10-16 08:43:32 +00001392void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 Comment cmnt(masm_, "[ SwitchStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001394 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 node->set_break_stack_height(break_stack_height_);
1396
1397 Load(node->tag());
1398
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001399 if (TryGenerateFastCaseSwitchStatement(node)) {
1400 return;
1401 }
1402
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 Label next, fall_through, default_case;
1404 ZoneList<CaseClause*>* cases = node->cases();
1405 int length = cases->length();
1406
1407 for (int i = 0; i < length; i++) {
1408 CaseClause* clause = cases->at(i);
1409
1410 Comment cmnt(masm_, "[ case clause");
1411
1412 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001413 // Continue matching cases. The program will execute the default case's
1414 // statements if it does not match any of the cases.
1415 __ b(&next);
1416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 // Bind the default case label, so we can branch to it when we
1418 // have compared against all other cases.
1419 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001420 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 } else {
1422 __ bind(&next);
1423 next.Unuse();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001424 __ ldr(r0, frame_->Top());
1425 frame_->Push(r0); // duplicate TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 Load(clause->label());
1427 Comparison(eq, true);
1428 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 }
1430
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001431 // Entering the case statement for the first time. Remove the switch value
1432 // from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001433 frame_->Pop();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001434
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001436 // This is also the target for the fall through from the previous case's
1437 // statements which has to skip over the matching code and the popping of
1438 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 __ bind(&fall_through);
1440 fall_through.Unuse();
1441 VisitStatements(clause->statements());
1442 __ b(&fall_through);
1443 }
1444
1445 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001446 // Reached the end of the case statements without matching any of the cases.
1447 if (default_case.is_bound()) {
1448 // A default case exists -> execute its statements.
1449 __ b(&default_case);
1450 } else {
1451 // Remove the switch value from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001452 frame_->Pop();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001453 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454
1455 __ bind(&fall_through);
1456 __ bind(node->break_target());
1457}
1458
1459
ager@chromium.org7c537e22008-10-16 08:43:32 +00001460void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 Comment cmnt(masm_, "[ LoopStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001462 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 node->set_break_stack_height(break_stack_height_);
1464
1465 // simple condition analysis
1466 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1467 if (node->cond() == NULL) {
1468 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1469 info = ALWAYS_TRUE;
1470 } else {
1471 Literal* lit = node->cond()->AsLiteral();
1472 if (lit != NULL) {
1473 if (lit->IsTrue()) {
1474 info = ALWAYS_TRUE;
1475 } else if (lit->IsFalse()) {
1476 info = ALWAYS_FALSE;
1477 }
1478 }
1479 }
1480
1481 Label loop, entry;
1482
1483 // init
1484 if (node->init() != NULL) {
1485 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1486 Visit(node->init());
1487 }
1488 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
1489 __ b(&entry);
1490 }
1491
1492 // body
1493 __ bind(&loop);
1494 Visit(node->body());
1495
1496 // next
1497 __ bind(node->continue_target());
1498 if (node->next() != NULL) {
1499 // Record source position of the statement as this code which is after the
1500 // code for the body actually belongs to the loop statement and not the
1501 // body.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001502 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1504 Visit(node->next());
1505 }
1506
1507 // cond
1508 __ bind(&entry);
1509 switch (info) {
1510 case ALWAYS_TRUE:
1511 CheckStack(); // TODO(1222600): ignore if body contains calls.
1512 __ b(&loop);
1513 break;
1514 case ALWAYS_FALSE:
1515 break;
1516 case DONT_KNOW:
1517 CheckStack(); // TODO(1222600): ignore if body contains calls.
1518 LoadCondition(node->cond(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00001519 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 &loop,
1521 node->break_target(),
1522 true);
1523 Branch(true, &loop);
1524 break;
1525 }
1526
1527 // exit
1528 __ bind(node->break_target());
1529}
1530
1531
ager@chromium.org7c537e22008-10-16 08:43:32 +00001532void CodeGenerator::VisitForInStatement(ForInStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 Comment cmnt(masm_, "[ ForInStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001534 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535
1536 // We keep stuff on the stack while the body is executing.
1537 // Record it, so that a break/continue crossing this statement
1538 // can restore the stack.
1539 const int kForInStackSize = 5 * kPointerSize;
1540 break_stack_height_ += kForInStackSize;
1541 node->set_break_stack_height(break_stack_height_);
1542
1543 Label loop, next, entry, cleanup, exit, primitive, jsobject;
1544 Label filter_key, end_del_check, fixed_array, non_string;
1545
1546 // Get the object to enumerate over (converted to JSObject).
1547 Load(node->enumerable());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001548 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549
1550 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1551 // to the specification. 12.6.4 mandates a call to ToObject.
1552 __ cmp(r0, Operand(Factory::undefined_value()));
1553 __ b(eq, &exit);
1554 __ cmp(r0, Operand(Factory::null_value()));
1555 __ b(eq, &exit);
1556
1557 // Stack layout in body:
1558 // [iteration counter (Smi)]
1559 // [length of array]
1560 // [FixedArray]
1561 // [Map or 0]
1562 // [Object]
1563
1564 // Check if enumerable is already a JSObject
1565 __ tst(r0, Operand(kSmiTagMask));
1566 __ b(eq, &primitive);
1567 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1568 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001569 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 __ b(hs, &jsobject);
1571
1572 __ bind(&primitive);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001573 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001574 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001575 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
1577
1578 __ bind(&jsobject);
1579
1580 // Get the set of properties (as a FixedArray or Map).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001581 frame_->Push(r0); // duplicate the object being enumerated
1582 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1584
1585 // If we got a Map, we can do a fast modification check.
1586 // Otherwise, we got a FixedArray, and we have to do a slow check.
1587 __ mov(r2, Operand(r0));
1588 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1589 __ cmp(r1, Operand(Factory::meta_map()));
1590 __ b(ne, &fixed_array);
1591
1592 // Get enum cache
1593 __ mov(r1, Operand(r0));
1594 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1595 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1596 __ ldr(r2,
1597 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1598
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001599 frame_->Push(r0); // map
1600 frame_->Push(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001601 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001603 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001604 __ mov(r0, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001605 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 __ b(&entry);
1607
1608
1609 __ bind(&fixed_array);
1610
1611 __ mov(r1, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001612 frame_->Push(r1); // insert 0 in place of Map
1613 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614
1615 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001616 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001618 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001619 __ mov(r0, Operand(Smi::FromInt(0))); // init index
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001620 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 __ b(&entry);
1623
1624 // Body.
1625 __ bind(&loop);
1626 Visit(node->body());
1627
1628 // Next.
1629 __ bind(node->continue_target());
1630 __ bind(&next);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001631 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632 __ add(r0, r0, Operand(Smi::FromInt(1)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001633 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634
1635 // Condition.
1636 __ bind(&entry);
1637
mads.s.ager31e71382008-08-13 09:32:07 +00001638 // sp[0] : index
1639 // sp[1] : array/enum cache length
1640 // sp[2] : array or enum cache
1641 // sp[3] : 0 or map
1642 // sp[4] : enumerable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001643 __ ldr(r0, frame_->Element(0)); // load the current count
1644 __ ldr(r1, frame_->Element(1)); // load the length
mads.s.ager31e71382008-08-13 09:32:07 +00001645 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 __ b(hs, &cleanup);
1647
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001648 __ ldr(r0, frame_->Element(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001649
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 // Get the i'th entry of the array.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001651 __ ldr(r2, frame_->Element(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1653 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1654
1655 // Get Map or 0.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001656 __ ldr(r2, frame_->Element(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 // Check if this (still) matches the map of the enumerable.
1658 // If not, we have to filter the key.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001659 __ ldr(r1, frame_->Element(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1661 __ cmp(r1, Operand(r2));
1662 __ b(eq, &end_del_check);
1663
1664 // Convert the entry to a string (or null if it isn't a property anymore).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001665 __ ldr(r0, frame_->Element(4)); // push enumerable
1666 frame_->Push(r0);
1667 frame_->Push(r3); // push entry
mads.s.ager31e71382008-08-13 09:32:07 +00001668 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001669 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671
1672 // If the property has been removed while iterating, we just skip it.
1673 __ cmp(r3, Operand(Factory::null_value()));
1674 __ b(eq, &next);
1675
1676
1677 __ bind(&end_del_check);
1678
1679 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00001680 // r3: i'th entry of the enum cache (or string there of)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001681 frame_->Push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 { Reference each(this, node->each());
1683 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001684 if (each.size() > 0) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001685 __ ldr(r0, frame_->Element(each.size()));
1686 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001687 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001688 // If the reference was to a slot we rely on the convenient property
1689 // that it doesn't matter whether a value (eg, r3 pushed above) is
1690 // right on top of or right underneath a zero-sized reference.
1691 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001692 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001693 // It's safe to pop the value lying on top of the reference before
1694 // unloading the reference itself (which preserves the top of stack,
1695 // ie, now the topmost value of the non-zero sized reference), since
1696 // we will discard the top of stack after unloading the reference
1697 // anyway.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001698 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001699 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 }
1701 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001702 // Discard the i'th entry pushed above or else the remainder of the
1703 // reference, whichever is currently on top of the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001704 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 CheckStack(); // TODO(1222600): ignore if body contains calls.
1706 __ jmp(&loop);
1707
1708 // Cleanup.
1709 __ bind(&cleanup);
1710 __ bind(node->break_target());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001711 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712
1713 // Exit.
1714 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715
1716 break_stack_height_ -= kForInStackSize;
1717}
1718
1719
ager@chromium.org7c537e22008-10-16 08:43:32 +00001720void CodeGenerator::VisitTryCatch(TryCatch* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 Comment cmnt(masm_, "[ TryCatch");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001722 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723
1724 Label try_block, exit;
1725
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726 __ bl(&try_block);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 // --- Catch block ---
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001728 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729
1730 // Store the caught exception in the catch variable.
1731 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001732 ASSERT(ref.is_slot());
1733 // Here we make use of the convenient property that it doesn't matter
1734 // whether a value is immediately on top of or underneath a zero-sized
1735 // reference.
1736 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 }
1738
1739 // Remove the exception from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001740 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741
1742 VisitStatements(node->catch_block()->statements());
1743 __ b(&exit);
1744
1745
1746 // --- Try block ---
1747 __ bind(&try_block);
1748
1749 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1750
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001751 // Shadow the labels for all escapes from the try block, including
1752 // returns. During shadowing, the original label is hidden as the
1753 // LabelShadow and operations on the original actually affect the
1754 // shadowing label.
1755 //
1756 // We should probably try to unify the escaping labels and the return
1757 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 int nof_escapes = node->escaping_labels()->length();
1759 List<LabelShadow*> shadows(1 + nof_escapes);
1760 shadows.Add(new LabelShadow(&function_return_));
1761 for (int i = 0; i < nof_escapes; i++) {
1762 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1763 }
1764
1765 // Generate code for the statements in the try block.
1766 VisitStatements(node->try_block()->statements());
ager@chromium.org381abbb2009-02-25 13:23:22 +00001767 // Discard the code slot from the handler.
1768 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769
1770 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001771 // After shadowing stops, the original labels are unshadowed and the
1772 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 int nof_unlinks = 0;
1774 for (int i = 0; i <= nof_escapes; i++) {
1775 shadows[i]->StopShadowing();
1776 if (shadows[i]->is_linked()) nof_unlinks++;
1777 }
1778
1779 // Unlink from try chain.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001780 // The code slot has already been discarded, so the next index is
1781 // adjusted by 1.
1782 const int kNextIndex =
1783 (StackHandlerConstants::kNextOffset / kPointerSize) - 1;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001784 __ ldr(r1, frame_->Element(kNextIndex)); // read next_sp
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1786 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00001787 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001788 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 if (nof_unlinks > 0) __ b(&exit);
1790
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001791 // Generate unlink code for the (formerly) shadowing labels that have been
1792 // jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 for (int i = 0; i <= nof_escapes; i++) {
1794 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001795 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 __ bind(shadows[i]);
1797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 // Reload sp from the top handler, because some statements that we
1799 // break from (eg, for...in) may have left stuff on the stack.
1800 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1801 __ ldr(sp, MemOperand(r3));
1802
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001803 __ ldr(r1, frame_->Element(kNextIndex));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00001805 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001806 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001808 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 }
1810 }
1811
1812 __ bind(&exit);
1813}
1814
1815
ager@chromium.org7c537e22008-10-16 08:43:32 +00001816void CodeGenerator::VisitTryFinally(TryFinally* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 Comment cmnt(masm_, "[ TryFinally");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001818 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819
1820 // State: Used to keep track of reason for entering the finally
1821 // block. Should probably be extended to hold information for
1822 // break/continue from within the try block.
1823 enum { FALLING, THROWING, JUMPING };
1824
1825 Label exit, unlink, try_block, finally_block;
1826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 __ bl(&try_block);
1828
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001829 frame_->Push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 // In case of thrown exceptions, this is where we continue.
1831 __ mov(r2, Operand(Smi::FromInt(THROWING)));
1832 __ b(&finally_block);
1833
1834
1835 // --- Try block ---
1836 __ bind(&try_block);
1837
1838 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1839
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001840 // Shadow the labels for all escapes from the try block, including
1841 // returns. Shadowing hides the original label as the LabelShadow and
1842 // operations on the original actually affect the shadowing label.
1843 //
1844 // We should probably try to unify the escaping labels and the return
1845 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 int nof_escapes = node->escaping_labels()->length();
1847 List<LabelShadow*> shadows(1 + nof_escapes);
1848 shadows.Add(new LabelShadow(&function_return_));
1849 for (int i = 0; i < nof_escapes; i++) {
1850 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1851 }
1852
1853 // Generate code for the statements in the try block.
1854 VisitStatements(node->try_block()->statements());
1855
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001856 // Stop the introduced shadowing and count the number of required unlinks.
1857 // After shadowing stops, the original labels are unshadowed and the
1858 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 int nof_unlinks = 0;
1860 for (int i = 0; i <= nof_escapes; i++) {
1861 shadows[i]->StopShadowing();
1862 if (shadows[i]->is_linked()) nof_unlinks++;
1863 }
1864
1865 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00001866 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001867 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 __ mov(r2, Operand(Smi::FromInt(FALLING)));
1869 if (nof_unlinks > 0) __ b(&unlink);
1870
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001871 // Generate code to set the state for the (formerly) shadowing labels that
1872 // have been jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 for (int i = 0; i <= nof_escapes; i++) {
1874 if (shadows[i]->is_linked()) {
1875 __ bind(shadows[i]);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001876 if (shadows[i]->original_label() == &function_return_) {
1877 // If this label shadowed the function return, materialize the
1878 // return value on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001879 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001880 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001881 // Fake TOS for labels that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00001882 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001883 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 }
1885 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
1886 __ b(&unlink);
1887 }
1888 }
1889
mads.s.ager31e71382008-08-13 09:32:07 +00001890 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891 __ bind(&unlink);
1892
ager@chromium.org381abbb2009-02-25 13:23:22 +00001893 frame_->Pop(r0); // Preserve TOS result in r0 across stack manipulation.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894 // Reload sp from the top handler, because some statements that we
1895 // break from (eg, for...in) may have left stuff on the stack.
1896 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1897 __ ldr(sp, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001898 const int kNextIndex = (StackHandlerConstants::kNextOffset
1899 + StackHandlerConstants::kAddressDisplacement)
1900 / kPointerSize;
1901 __ ldr(r1, frame_->Element(kNextIndex));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 __ str(r1, MemOperand(r3));
1903 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org381abbb2009-02-25 13:23:22 +00001904 // The stack pointer was restored to just below the code slot (the
1905 // topmost slot) of the handler, so all but the code slot need to be
1906 // dropped.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001907 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001908 // Restore result to TOS.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001909 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910
1911 // --- Finally block ---
1912 __ bind(&finally_block);
1913
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001914 // Push the state on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001915 frame_->Push(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001916
1917 // We keep two elements on the stack - the (possibly faked) result
1918 // and the state - while evaluating the finally block. Record it, so
1919 // that a break/continue crossing this statement can restore the
1920 // stack.
1921 const int kFinallyStackSize = 2 * kPointerSize;
1922 break_stack_height_ += kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923
1924 // Generate code for the statements in the finally block.
1925 VisitStatements(node->finally_block()->statements());
1926
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001927 // Restore state and return value or faked TOS.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001928 frame_->Pop(r2);
1929 frame_->Pop(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001930 break_stack_height_ -= kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001932 // Generate code to jump to the right destination for all used (formerly)
1933 // shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 for (int i = 0; i <= nof_escapes; i++) {
1935 if (shadows[i]->is_bound()) {
1936 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
ager@chromium.org381abbb2009-02-25 13:23:22 +00001937 __ b(eq, shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 }
1939 }
1940
1941 // Check if we need to rethrow the exception.
1942 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
1943 __ b(ne, &exit);
1944
1945 // Rethrow exception.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001946 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 __ CallRuntime(Runtime::kReThrow, 1);
1948
1949 // Done.
1950 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951}
1952
1953
ager@chromium.org7c537e22008-10-16 08:43:32 +00001954void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 Comment cmnt(masm_, "[ DebuggerStatament");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001956 CodeForStatement(node);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001957 __ CallRuntime(Runtime::kDebugBreak, 0);
1958 // Ignore the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959}
1960
1961
ager@chromium.org7c537e22008-10-16 08:43:32 +00001962void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 ASSERT(boilerplate->IsBoilerplate());
1964
1965 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001966 __ mov(r0, Operand(boilerplate));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001967 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968
1969 // Create a new closure.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001970 frame_->Push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 __ CallRuntime(Runtime::kNewClosure, 2);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001972 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973}
1974
1975
ager@chromium.org7c537e22008-10-16 08:43:32 +00001976void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 Comment cmnt(masm_, "[ FunctionLiteral");
1978
1979 // Build the function boilerplate and instantiate it.
1980 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00001981 // Check for stack-overflow exception.
1982 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 InstantiateBoilerplate(boilerplate);
1984}
1985
1986
ager@chromium.org7c537e22008-10-16 08:43:32 +00001987void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 FunctionBoilerplateLiteral* node) {
1989 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
1990 InstantiateBoilerplate(node->boilerplate());
1991}
1992
1993
ager@chromium.org7c537e22008-10-16 08:43:32 +00001994void CodeGenerator::VisitConditional(Conditional* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 Comment cmnt(masm_, "[ Conditional");
1996 Label then, else_, exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001997 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 Branch(false, &else_);
1999 __ bind(&then);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002000 Load(node->then_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 __ b(&exit);
2002 __ bind(&else_);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002003 Load(node->else_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 __ bind(&exit);
2005}
2006
2007
ager@chromium.org7c537e22008-10-16 08:43:32 +00002008void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2009 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002010 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011
ager@chromium.org381abbb2009-02-25 13:23:22 +00002012 Label slow, done;
2013
2014 // Generate fast-case code for variables that might be shadowed by
2015 // eval-introduced variables. Eval is used a lot without
2016 // introducing variables. In those cases, we do not want to
2017 // perform a runtime call for all variables in the scope
2018 // containing the eval.
2019 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2020 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
2021 __ b(&done);
2022
2023 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2024 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2025 // Only generate the fast case for locals that rewrite to slots.
2026 // This rules out argument loads.
2027 if (potential_slot != NULL) {
2028 __ ldr(r0,
2029 ContextSlotOperandCheckExtensions(potential_slot,
2030 r1,
2031 r2,
2032 &slow));
2033 __ b(&done);
2034 }
2035 }
2036
2037 __ bind(&slow);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002038 frame_->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002039 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002040 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041
ager@chromium.org7c537e22008-10-16 08:43:32 +00002042 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002043 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002044 } else {
2045 __ CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002047
2048 __ bind(&done);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002049 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050
2051 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002052 // Note: We would like to keep the assert below, but it fires because of
2053 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002054 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002056 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002057 __ ldr(r0, SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002058 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002059 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002060 // Const slots may contain 'the hole' value (the constant hasn't been
2061 // initialized yet) which needs to be converted into the 'undefined'
2062 // value.
2063 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002064 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002065 __ cmp(r0, Operand(Factory::the_hole_value()));
2066 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002067 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 }
2069 }
2070}
2071
2072
ager@chromium.org381abbb2009-02-25 13:23:22 +00002073void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2074 TypeofState typeof_state,
2075 Register tmp,
2076 Register tmp2,
2077 Label* slow) {
2078 // Check that no extension objects have been created by calls to
2079 // eval from the current scope to the global scope.
2080 Register context = cp;
2081 Scope* s = scope();
2082 while (s != NULL) {
2083 if (s->num_heap_slots() > 0) {
2084 if (s->calls_eval()) {
2085 // Check that extension is NULL.
2086 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2087 __ tst(tmp2, tmp2);
2088 __ b(ne, slow);
2089 }
2090 // Load next context in chain.
2091 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2092 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2093 context = tmp;
2094 }
2095 // If no outer scope calls eval, we do not need to check more
2096 // context extensions.
2097 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2098 s = s->outer_scope();
2099 }
2100
2101 if (s->is_eval_scope()) {
2102 Label next, fast;
2103 if (!context.is(tmp)) __ mov(tmp, Operand(context));
2104 __ bind(&next);
2105 // Terminate at global context.
2106 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2107 __ cmp(tmp2, Operand(Factory::global_context_map()));
2108 __ b(eq, &fast);
2109 // Check that extension is NULL.
2110 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2111 __ tst(tmp2, tmp2);
2112 __ b(ne, slow);
2113 // Load next context in chain.
2114 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2115 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2116 __ b(&next);
2117 __ bind(&fast);
2118 }
2119
2120 // All extension objects were empty and it is safe to use a global
2121 // load IC call.
2122 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2123 // Load the global object.
2124 LoadGlobal();
2125 // Setup the name register.
2126 __ mov(r2, Operand(slot->var()->name()));
2127 // Call IC stub.
2128 if (typeof_state == INSIDE_TYPEOF) {
2129 __ Call(ic, RelocInfo::CODE_TARGET);
2130 } else {
2131 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
2132 }
2133
2134 // Pop the global object. The result is in r0.
2135 frame_->Pop();
2136}
2137
2138
ager@chromium.org7c537e22008-10-16 08:43:32 +00002139void CodeGenerator::VisitSlot(Slot* node) {
2140 Comment cmnt(masm_, "[ Slot");
2141 LoadFromSlot(node, typeof_state());
2142}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143
ager@chromium.org7c537e22008-10-16 08:43:32 +00002144
2145void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
2146 Comment cmnt(masm_, "[ VariableProxy");
2147
2148 Variable* var = node->var();
2149 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002150 if (expr != NULL) {
2151 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002153 ASSERT(var->is_global());
2154 Reference ref(this, node);
2155 ref.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 }
2157}
2158
2159
ager@chromium.org7c537e22008-10-16 08:43:32 +00002160void CodeGenerator::VisitLiteral(Literal* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002162 __ mov(r0, Operand(node->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002163 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164}
2165
2166
ager@chromium.org7c537e22008-10-16 08:43:32 +00002167void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 Comment cmnt(masm_, "[ RexExp Literal");
2169
2170 // Retrieve the literal array and check the allocated entry.
2171
2172 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002173 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174
2175 // Load the literals array of the function.
2176 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2177
2178 // Load the literal at the ast saved index.
2179 int literal_offset =
2180 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2181 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2182
2183 Label done;
2184 __ cmp(r2, Operand(Factory::undefined_value()));
2185 __ b(ne, &done);
2186
2187 // If the entry is undefined we call the runtime system to computed
2188 // the literal.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002189 frame_->Push(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002190 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002191 frame_->Push(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002192 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002193 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002194 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002195 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002197 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198
mads.s.ager31e71382008-08-13 09:32:07 +00002199 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 // Push the literal.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002201 frame_->Push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202}
2203
2204
2205// This deferred code stub will be used for creating the boilerplate
2206// by calling Runtime_CreateObjectLiteral.
2207// Each created boilerplate is stored in the JSFunction and they are
2208// therefore context dependent.
2209class ObjectLiteralDeferred: public DeferredCode {
2210 public:
2211 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
2212 : DeferredCode(generator), node_(node) {
2213 set_comment("[ ObjectLiteralDeferred");
2214 }
2215 virtual void Generate();
2216 private:
2217 ObjectLiteral* node_;
2218};
2219
2220
2221void ObjectLiteralDeferred::Generate() {
2222 // If the entry is undefined we call the runtime system to computed
2223 // the literal.
2224
2225 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00002226 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002228 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2229 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002231 __ mov(r0, Operand(node_->constant_properties()));
2232 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002234 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235}
2236
2237
ager@chromium.org7c537e22008-10-16 08:43:32 +00002238void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 Comment cmnt(masm_, "[ ObjectLiteral");
2240
2241 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
2242
2243 // Retrieve the literal array and check the allocated entry.
2244
2245 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002246 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247
2248 // Load the literals array of the function.
2249 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2250
2251 // Load the literal at the ast saved index.
2252 int literal_offset =
2253 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2254 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2255
2256 // Check whether we need to materialize the object literal boilerplate.
2257 // If so, jump to the deferred code.
2258 __ cmp(r2, Operand(Factory::undefined_value()));
2259 __ b(eq, deferred->enter());
2260 __ bind(deferred->exit());
2261
2262 // Push the object literal boilerplate.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002263 frame_->Push(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 // Clone the boilerplate object.
2266 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002267 frame_->Push(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002268 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269
2270 for (int i = 0; i < node->properties()->length(); i++) {
2271 ObjectLiteral::Property* property = node->properties()->at(i);
2272 Literal* key = property->key();
2273 Expression* value = property->value();
2274 switch (property->kind()) {
2275 case ObjectLiteral::Property::CONSTANT: break;
2276 case ObjectLiteral::Property::COMPUTED: // fall through
2277 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002278 frame_->Push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 Load(key);
2280 Load(value);
2281 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002282 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002283 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 break;
2285 }
2286 case ObjectLiteral::Property::SETTER: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002287 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002289 __ mov(r0, Operand(Smi::FromInt(1)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002290 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 Load(value);
2292 __ CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002293 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 break;
2295 }
2296 case ObjectLiteral::Property::GETTER: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002297 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002299 __ mov(r0, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002300 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 Load(value);
2302 __ CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002303 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 break;
2305 }
2306 }
2307 }
2308}
2309
2310
ager@chromium.org7c537e22008-10-16 08:43:32 +00002311void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002313
2314 // Call runtime to create the array literal.
2315 __ mov(r0, Operand(node->literals()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002316 frame_->Push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002317 // Load the function of this frame.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002318 __ ldr(r0, frame_->Function());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002319 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002320 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002321 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
2322
2323 // Push the resulting array literal on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002324 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002325
2326 // Generate code to set the elements in the array that are not
2327 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 for (int i = 0; i < node->values()->length(); i++) {
2329 Expression* value = node->values()->at(i);
2330
2331 // If value is literal the property value is already
2332 // set in the boilerplate object.
2333 if (value->AsLiteral() == NULL) {
2334 // The property must be set by generated code.
2335 Load(value);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002336 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337
2338 // Fetch the object literal
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002339 __ ldr(r1, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 // Get the elements array.
2341 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
2342
2343 // Write to the indexed properties array.
2344 int offset = i * kPointerSize + Array::kHeaderSize;
2345 __ str(r0, FieldMemOperand(r1, offset));
2346
2347 // Update the write barrier for the array address.
2348 __ mov(r3, Operand(offset));
2349 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 }
2351 }
2352}
2353
2354
ager@chromium.org32912102009-01-16 10:38:43 +00002355void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
2356 // Call runtime routine to allocate the catch extension object and
2357 // assign the exception value to the catch variable.
2358 Comment cmnt(masm_, "[CatchExtensionObject ");
2359 Load(node->key());
2360 Load(node->value());
2361 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2362 frame_->Push(r0);
2363}
2364
2365
ager@chromium.org7c537e22008-10-16 08:43:32 +00002366void CodeGenerator::VisitAssignment(Assignment* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002368 CodeForStatement(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 Reference target(this, node->target());
2371 if (target.is_illegal()) return;
2372
2373 if (node->op() == Token::ASSIGN ||
2374 node->op() == Token::INIT_VAR ||
2375 node->op() == Token::INIT_CONST) {
2376 Load(node->value());
2377
2378 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002379 target.GetValue(NOT_INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 Literal* literal = node->value()->AsLiteral();
2381 if (literal != NULL && literal->handle()->IsSmi()) {
2382 SmiOperation(node->binary_op(), literal->handle(), false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002383 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002384
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385 } else {
2386 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00002387 GenericBinaryOperation(node->binary_op());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002388 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 }
2390 }
2391
2392 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2393 if (var != NULL &&
2394 (var->mode() == Variable::CONST) &&
2395 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2396 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002397
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398 } else {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002399 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 if (node->op() == Token::INIT_CONST) {
2401 // Dynamic constant initializations must use the function context
2402 // and initialize the actual constant declared. Dynamic variable
2403 // initializations are simply assignments and use SetValue.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002404 target.SetValue(CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002406 target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407 }
2408 }
2409}
2410
2411
ager@chromium.org7c537e22008-10-16 08:43:32 +00002412void CodeGenerator::VisitThrow(Throw* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 Comment cmnt(masm_, "[ Throw");
2414
2415 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002416 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 __ CallRuntime(Runtime::kThrow, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002418 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419}
2420
2421
ager@chromium.org7c537e22008-10-16 08:43:32 +00002422void CodeGenerator::VisitProperty(Property* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002423 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002424
ager@chromium.org7c537e22008-10-16 08:43:32 +00002425 Reference property(this, node);
2426 property.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427}
2428
2429
ager@chromium.org7c537e22008-10-16 08:43:32 +00002430void CodeGenerator::VisitCall(Call* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 Comment cmnt(masm_, "[ Call");
2432
2433 ZoneList<Expression*>* args = node->arguments();
2434
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002435 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 // Standard function call.
2437
2438 // Check if the function is a variable or a property.
2439 Expression* function = node->expression();
2440 Variable* var = function->AsVariableProxy()->AsVariable();
2441 Property* property = function->AsProperty();
2442
2443 // ------------------------------------------------------------------------
2444 // Fast-case: Use inline caching.
2445 // ---
2446 // According to ECMA-262, section 11.2.3, page 44, the function to call
2447 // must be resolved after the arguments have been evaluated. The IC code
2448 // automatically handles this by loading the arguments before the function
2449 // is resolved in cache misses (this also holds for megamorphic calls).
2450 // ------------------------------------------------------------------------
2451
2452 if (var != NULL && !var->is_this() && var->is_global()) {
2453 // ----------------------------------
2454 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2455 // ----------------------------------
2456
2457 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002458 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002459 frame_->Push(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002460
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002461 // Pass the global object as the receiver and let the IC stub
2462 // patch the stack to use the global proxy as 'this' in the
2463 // invoked function.
2464 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465
2466 // Load the arguments.
2467 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
2469 // Setup the receiver register and call the IC initialization code.
2470 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002471 CodeForSourcePosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002472 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002473 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002475 frame_->Pop();
2476 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477
2478 } else if (var != NULL && var->slot() != NULL &&
2479 var->slot()->type() == Slot::LOOKUP) {
2480 // ----------------------------------
2481 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2482 // ----------------------------------
2483
2484 // Load the function
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002485 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002486 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002487 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2489 // r0: slot value; r1: receiver
2490
2491 // Load the receiver.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002492 frame_->Push(r0); // function
2493 frame_->Push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494
2495 // Call the function.
2496 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002497 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498
2499 } else if (property != NULL) {
2500 // Check if the key is a literal string.
2501 Literal* literal = property->key()->AsLiteral();
2502
2503 if (literal != NULL && literal->handle()->IsSymbol()) {
2504 // ------------------------------------------------------------------
2505 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2506 // ------------------------------------------------------------------
2507
2508 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002509 __ mov(r0, Operand(literal->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002510 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511 Load(property->obj());
2512
2513 // Load the arguments.
2514 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
2516 // Set the receiver register and call the IC initialization code.
2517 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002518 CodeForSourcePosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002519 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002520 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521
2522 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002523 frame_->Pop();
mads.s.ager31e71382008-08-13 09:32:07 +00002524
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002525 frame_->Push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526
2527 } else {
2528 // -------------------------------------------
2529 // JavaScript example: 'array[index](1, 2, 3)'
2530 // -------------------------------------------
2531
2532 // Load the function to call from the property through a reference.
2533 Reference ref(this, property);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002534 ref.GetValue(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535
2536 // Pass receiver to called function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002537 __ ldr(r0, frame_->Element(ref.size()));
2538 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539 // Call the function.
2540 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002541 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002542 }
2543
2544 } else {
2545 // ----------------------------------
2546 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
2547 // ----------------------------------
2548
2549 // Load the function.
2550 Load(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002551
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002552 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002553 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555 // Call the function.
2556 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002557 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 }
2559}
2560
2561
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002562void CodeGenerator::VisitCallEval(CallEval* node) {
2563 Comment cmnt(masm_, "[ CallEval");
2564
2565 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
2566 // the function we need to call and the receiver of the call.
2567 // Then we call the resolved function using the given arguments.
2568
2569 ZoneList<Expression*>* args = node->arguments();
2570 Expression* function = node->expression();
2571
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002572 CodeForStatement(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002573
2574 // Prepare stack for call to resolved function.
2575 Load(function);
2576 __ mov(r2, Operand(Factory::undefined_value()));
2577 __ push(r2); // Slot for receiver
2578 for (int i = 0; i < args->length(); i++) {
2579 Load(args->at(i));
2580 }
2581
2582 // Prepare stack for call to ResolvePossiblyDirectEval.
2583 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize + kPointerSize));
2584 __ push(r1);
2585 if (args->length() > 0) {
2586 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize));
2587 __ push(r1);
2588 } else {
2589 __ push(r2);
2590 }
2591
2592 // Resolve the call.
2593 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
2594
2595 // Touch up stack with the right values for the function and the receiver.
2596 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
2597 __ str(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
2598 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
2599 __ str(r1, MemOperand(sp, args->length() * kPointerSize));
2600
2601 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002602 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002603
2604 CallFunctionStub call_function(args->length());
2605 __ CallStub(&call_function);
2606
2607 __ ldr(cp, frame_->Context());
2608 // Remove the function from the stack.
2609 frame_->Pop();
2610 frame_->Push(r0);
2611}
2612
2613
ager@chromium.org7c537e22008-10-16 08:43:32 +00002614void CodeGenerator::VisitCallNew(CallNew* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 Comment cmnt(masm_, "[ CallNew");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002616 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617
2618 // According to ECMA-262, section 11.2.2, page 44, the function
2619 // expression in new calls must be evaluated before the
2620 // arguments. This is different from ordinary calls, where the
2621 // actual function to call is resolved after the arguments have been
2622 // evaluated.
2623
2624 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002625 // receiver. There is no need to use the global proxy here because
2626 // it will always be replaced with a newly allocated object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002628 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629
2630 // Push the arguments ("left-to-right") on the stack.
2631 ZoneList<Expression*>* args = node->arguments();
2632 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2633
mads.s.ager31e71382008-08-13 09:32:07 +00002634 // r0: the number of arguments.
2635 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002637 // Load the function into r1 as per calling convention.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002638 __ ldr(r1, frame_->Element(args->length() + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 // Call the construct call builtin that handles allocation and
2641 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002642 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00002644 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00002645
2646 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002647 __ str(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648}
2649
2650
ager@chromium.org7c537e22008-10-16 08:43:32 +00002651void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 ASSERT(args->length() == 1);
2653 Label leave;
2654 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002655 frame_->Pop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00002656 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 __ tst(r0, Operand(kSmiTagMask));
2658 __ b(eq, &leave);
2659 // It is a heap object - get map.
2660 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2661 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002662 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 __ cmp(r1, Operand(JS_VALUE_TYPE));
2664 __ b(ne, &leave);
2665 // Load the value.
2666 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2667 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002668 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669}
2670
2671
ager@chromium.org7c537e22008-10-16 08:43:32 +00002672void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 ASSERT(args->length() == 2);
2674 Label leave;
2675 Load(args->at(0)); // Load the object.
2676 Load(args->at(1)); // Load the value.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002677 frame_->Pop(r0); // r0 contains value
2678 frame_->Pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 // if (object->IsSmi()) return object.
2680 __ tst(r1, Operand(kSmiTagMask));
2681 __ b(eq, &leave);
2682 // It is a heap object - get map.
2683 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2684 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2685 // if (!object->IsJSValue()) return object.
2686 __ cmp(r2, Operand(JS_VALUE_TYPE));
2687 __ b(ne, &leave);
2688 // Store the value.
2689 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2690 // Update the write barrier.
2691 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2692 __ RecordWrite(r1, r2, r3);
2693 // Leave.
2694 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002695 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696}
2697
2698
ager@chromium.org7c537e22008-10-16 08:43:32 +00002699void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 ASSERT(args->length() == 1);
2701 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002702 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002703 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 cc_reg_ = eq;
2705}
2706
2707
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002708void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
2709 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
2710 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002711#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002712 if (ShouldGenerateLog(args->at(0))) {
2713 Load(args->at(1));
2714 Load(args->at(2));
2715 __ CallRuntime(Runtime::kLog, 2);
2716 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002717#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002718 __ mov(r0, Operand(Factory::undefined_value()));
2719 frame_->Push(r0);
2720}
2721
2722
ager@chromium.org7c537e22008-10-16 08:43:32 +00002723void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002724 ASSERT(args->length() == 1);
2725 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002726 frame_->Pop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002727 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2728 cc_reg_ = eq;
2729}
2730
2731
kasper.lund7276f142008-07-30 08:49:36 +00002732// This should generate code that performs a charCodeAt() call or returns
2733// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2734// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002735void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00002736 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002737 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002738 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002739}
2740
2741
ager@chromium.org7c537e22008-10-16 08:43:32 +00002742void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002743 ASSERT(args->length() == 1);
2744 Load(args->at(0));
2745 Label answer;
2746 // We need the CC bits to come out as not_equal in the case where the
2747 // object is a smi. This can't be done with the usual test opcode so
2748 // we use XOR to get the right CC bits.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002749 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002750 __ and_(r1, r0, Operand(kSmiTagMask));
2751 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2752 __ b(ne, &answer);
2753 // It is a heap object - get the map.
2754 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2755 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2756 // Check if the object is a JS array or not.
2757 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2758 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759 cc_reg_ = eq;
2760}
2761
2762
ager@chromium.org7c537e22008-10-16 08:43:32 +00002763void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764 ASSERT(args->length() == 0);
2765
mads.s.ager31e71382008-08-13 09:32:07 +00002766 // Seed the result with the formal parameters count, which will be used
2767 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2769
2770 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002771 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002773 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774}
2775
2776
ager@chromium.org7c537e22008-10-16 08:43:32 +00002777void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 ASSERT(args->length() == 1);
2779
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002780 // Satisfy contract with ArgumentsAccessStub:
2781 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002783 frame_->Pop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002784 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
2786 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002787 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002789 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790}
2791
2792
ager@chromium.org7c537e22008-10-16 08:43:32 +00002793void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002794 ASSERT(args->length() == 2);
2795
2796 // Load the two objects into registers and perform the comparison.
2797 Load(args->at(0));
2798 Load(args->at(1));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002799 frame_->Pop(r0);
2800 frame_->Pop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002801 __ cmp(r0, Operand(r1));
2802 cc_reg_ = eq;
2803}
2804
2805
ager@chromium.org7c537e22008-10-16 08:43:32 +00002806void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002807 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002808
2809 ZoneList<Expression*>* args = node->arguments();
2810 Comment cmnt(masm_, "[ CallRuntime");
2811 Runtime::Function* function = node->function();
2812
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002814 // Push the arguments ("left-to-right").
2815 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2816
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817 // Call the C runtime function.
2818 __ CallRuntime(function, args->length());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002819 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002822 // Prepare stack for calling JS runtime function.
2823 __ mov(r0, Operand(node->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002824 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002825 // Push the builtins object found in the current global object.
2826 __ ldr(r1, GlobalObject());
2827 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002828 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002829
2830 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2831
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002834 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002835 __ ldr(cp, frame_->Context());
2836 frame_->Pop();
2837 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 }
2839}
2840
2841
ager@chromium.org7c537e22008-10-16 08:43:32 +00002842void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 Comment cmnt(masm_, "[ UnaryOperation");
2844
2845 Token::Value op = node->op();
2846
2847 if (op == Token::NOT) {
2848 LoadCondition(node->expression(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002849 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002850 false_target(),
2851 true_target(),
2852 true);
2853 cc_reg_ = NegateCondition(cc_reg_);
2854
2855 } else if (op == Token::DELETE) {
2856 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002857 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858 if (property != NULL) {
2859 Load(property->obj());
2860 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002861 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002862 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002863
mads.s.ager31e71382008-08-13 09:32:07 +00002864 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002865 Slot* slot = variable->slot();
2866 if (variable->is_global()) {
2867 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00002868 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002869 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002870 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002871 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872
2873 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2874 // lookup the context holding the named variable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002875 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002876 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002877 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878 __ CallRuntime(Runtime::kLookupContext, 2);
2879 // r0: context
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002880 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002881 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002882 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002883 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002884 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885
mads.s.ager31e71382008-08-13 09:32:07 +00002886 } else {
2887 // Default: Result of deleting non-global, not dynamically
2888 // introduced variables is false.
2889 __ mov(r0, Operand(Factory::false_value()));
2890 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891
2892 } else {
2893 // Default: Result of deleting expressions is true.
2894 Load(node->expression()); // may have side-effects
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002895 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 __ mov(r0, Operand(Factory::true_value()));
2897 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002898 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002899
2900 } else if (op == Token::TYPEOF) {
2901 // Special case for loading the typeof expression; see comment on
2902 // LoadTypeofExpression().
2903 LoadTypeofExpression(node->expression());
2904 __ CallRuntime(Runtime::kTypeof, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002905 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906
2907 } else {
2908 Load(node->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002909 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 switch (op) {
2911 case Token::NOT:
2912 case Token::DELETE:
2913 case Token::TYPEOF:
2914 UNREACHABLE(); // handled above
2915 break;
2916
2917 case Token::SUB: {
2918 UnarySubStub stub;
2919 __ CallStub(&stub);
2920 break;
2921 }
2922
2923 case Token::BIT_NOT: {
2924 // smi check
2925 Label smi_label;
2926 Label continue_label;
2927 __ tst(r0, Operand(kSmiTagMask));
2928 __ b(eq, &smi_label);
2929
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002930 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002931 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002932 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002933
2934 __ b(&continue_label);
2935 __ bind(&smi_label);
2936 __ mvn(r0, Operand(r0));
2937 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
2938 __ bind(&continue_label);
2939 break;
2940 }
2941
2942 case Token::VOID:
2943 // since the stack top is cached in r0, popping and then
2944 // pushing a value can be done by just writing to r0.
2945 __ mov(r0, Operand(Factory::undefined_value()));
2946 break;
2947
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002948 case Token::ADD: {
2949 // Smi check.
2950 Label continue_label;
2951 __ tst(r0, Operand(kSmiTagMask));
2952 __ b(eq, &continue_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002953 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002954 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002955 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002956 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002958 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 default:
2960 UNREACHABLE();
2961 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002962 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 }
2964}
2965
2966
ager@chromium.org7c537e22008-10-16 08:43:32 +00002967void CodeGenerator::VisitCountOperation(CountOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968 Comment cmnt(masm_, "[ CountOperation");
2969
2970 bool is_postfix = node->is_postfix();
2971 bool is_increment = node->op() == Token::INC;
2972
2973 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
2974 bool is_const = (var != NULL && var->mode() == Variable::CONST);
2975
2976 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00002977 if (is_postfix) {
2978 __ mov(r0, Operand(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002979 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002980 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981
2982 { Reference target(this, node->expression());
2983 if (target.is_illegal()) return;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002984 target.GetValue(NOT_INSIDE_TYPEOF);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002985 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986
2987 Label slow, exit;
2988
2989 // Load the value (1) into register r1.
2990 __ mov(r1, Operand(Smi::FromInt(1)));
2991
2992 // Check for smi operand.
2993 __ tst(r0, Operand(kSmiTagMask));
2994 __ b(ne, &slow);
2995
2996 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002997 if (is_postfix) {
2998 __ str(r0, frame_->Element(target.size()));
2999 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000
3001 // Perform optimistic increment/decrement.
3002 if (is_increment) {
3003 __ add(r0, r0, Operand(r1), SetCC);
3004 } else {
3005 __ sub(r0, r0, Operand(r1), SetCC);
3006 }
3007
3008 // If the increment/decrement didn't overflow, we're done.
3009 __ b(vc, &exit);
3010
3011 // Revert optimistic increment/decrement.
3012 if (is_increment) {
3013 __ sub(r0, r0, Operand(r1));
3014 } else {
3015 __ add(r0, r0, Operand(r1));
3016 }
3017
3018 // Slow case: Convert to number.
3019 __ bind(&slow);
3020
3021 // Postfix: Convert the operand to a number and store it as the result.
3022 if (is_postfix) {
3023 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
3024 __ CallStub(&stub);
3025 // Store to result (on the stack).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003026 __ str(r0, frame_->Element(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 }
3028
3029 // Compute the new value by calling the right JavaScript native.
3030 if (is_increment) {
3031 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
3032 __ CallStub(&stub);
3033 } else {
3034 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
3035 __ CallStub(&stub);
3036 }
3037
3038 // Store the new value in the target if not const.
3039 __ bind(&exit);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003040 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003041 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042 }
3043
3044 // Postfix: Discard the new value and use the old.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003045 if (is_postfix) frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046}
3047
3048
ager@chromium.org7c537e22008-10-16 08:43:32 +00003049void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 Comment cmnt(masm_, "[ BinaryOperation");
3051 Token::Value op = node->op();
3052
3053 // According to ECMA-262 section 11.11, page 58, the binary logical
3054 // operators must yield the result of one of the two expressions
3055 // before any ToBoolean() conversions. This means that the value
3056 // produced by a && or || operator is not necessarily a boolean.
3057
3058 // NOTE: If the left hand side produces a materialized value (not in
3059 // the CC register), we force the right hand side to do the
3060 // same. This is necessary because we may have to branch to the exit
3061 // after evaluating the left hand side (due to the shortcut
3062 // semantics), but the compiler must (statically) know if the result
3063 // of compiling the binary operation is materialized or not.
3064
3065 if (op == Token::AND) {
3066 Label is_true;
3067 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00003068 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069 &is_true,
3070 false_target(),
3071 false);
3072 if (has_cc()) {
3073 Branch(false, false_target());
3074
3075 // Evaluate right side expression.
3076 __ bind(&is_true);
3077 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00003078 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 true_target(),
3080 false_target(),
3081 false);
3082
3083 } else {
3084 Label pop_and_continue, exit;
3085
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003086 __ ldr(r0, frame_->Top()); // dup the stack top
3087 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 // Avoid popping the result if it converts to 'false' using the
3089 // standard ToBoolean() conversion as described in ECMA-262,
3090 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003091 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003092 Branch(false, &exit);
3093
3094 // Pop the result of evaluating the first part.
3095 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003096 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003097
3098 // Evaluate right side expression.
3099 __ bind(&is_true);
3100 Load(node->right());
3101
3102 // Exit (always with a materialized value).
3103 __ bind(&exit);
3104 }
3105
3106 } else if (op == Token::OR) {
3107 Label is_false;
3108 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00003109 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 true_target(),
3111 &is_false,
3112 false);
3113 if (has_cc()) {
3114 Branch(true, true_target());
3115
3116 // Evaluate right side expression.
3117 __ bind(&is_false);
3118 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00003119 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 true_target(),
3121 false_target(),
3122 false);
3123
3124 } else {
3125 Label pop_and_continue, exit;
3126
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003127 __ ldr(r0, frame_->Top());
3128 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 // Avoid popping the result if it converts to 'true' using the
3130 // standard ToBoolean() conversion as described in ECMA-262,
3131 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003132 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 Branch(true, &exit);
3134
3135 // Pop the result of evaluating the first part.
3136 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003137 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003138
3139 // Evaluate right side expression.
3140 __ bind(&is_false);
3141 Load(node->right());
3142
3143 // Exit (always with a materialized value).
3144 __ bind(&exit);
3145 }
3146
3147 } else {
3148 // Optimize for the case where (at least) one of the expressions
3149 // is a literal small integer.
3150 Literal* lliteral = node->left()->AsLiteral();
3151 Literal* rliteral = node->right()->AsLiteral();
3152
3153 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
3154 Load(node->left());
3155 SmiOperation(node->op(), rliteral->handle(), false);
3156
3157 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
3158 Load(node->right());
3159 SmiOperation(node->op(), lliteral->handle(), true);
3160
3161 } else {
3162 Load(node->left());
3163 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00003164 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003166 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167 }
3168}
3169
3170
ager@chromium.org7c537e22008-10-16 08:43:32 +00003171void CodeGenerator::VisitThisFunction(ThisFunction* node) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003172 __ ldr(r0, frame_->Function());
3173 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174}
3175
3176
ager@chromium.org7c537e22008-10-16 08:43:32 +00003177void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 Comment cmnt(masm_, "[ CompareOperation");
3179
3180 // Get the expressions from the node.
3181 Expression* left = node->left();
3182 Expression* right = node->right();
3183 Token::Value op = node->op();
3184
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003185 // To make null checks efficient, we check if either left or right is the
3186 // literal 'null'. If so, we optimize the code by inlining a null check
3187 // instead of calling the (very) general runtime routine for checking
3188 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003190 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003191 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003192 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003193 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3194 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003195 if (left_is_null || right_is_null) {
3196 Load(left_is_null ? right : left);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003197 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 __ cmp(r0, Operand(Factory::null_value()));
3199
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003200 // The 'null' value is only equal to 'undefined' if using non-strict
3201 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003202 if (op != Token::EQ_STRICT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003203 __ b(eq, true_target());
3204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003205 __ cmp(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003206 __ b(eq, true_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003208 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003209 __ b(eq, false_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003210
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003211 // It can be an undetectable object.
3212 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3213 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3214 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3215 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 }
3217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 cc_reg_ = eq;
3219 return;
3220 }
3221 }
3222
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003223 // To make typeof testing for natives implemented in JavaScript really
3224 // efficient, we generate special code for expressions of the form:
3225 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226 UnaryOperation* operation = left->AsUnaryOperation();
3227 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3228 (operation != NULL && operation->op() == Token::TYPEOF) &&
3229 (right->AsLiteral() != NULL &&
3230 right->AsLiteral()->handle()->IsString())) {
3231 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3232
mads.s.ager31e71382008-08-13 09:32:07 +00003233 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003234 LoadTypeofExpression(operation->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003235 frame_->Pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236
3237 if (check->Equals(Heap::number_symbol())) {
3238 __ tst(r1, Operand(kSmiTagMask));
3239 __ b(eq, true_target());
3240 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3241 __ cmp(r1, Operand(Factory::heap_number_map()));
3242 cc_reg_ = eq;
3243
3244 } else if (check->Equals(Heap::string_symbol())) {
3245 __ tst(r1, Operand(kSmiTagMask));
3246 __ b(eq, false_target());
3247
3248 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3249
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003250 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3252 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3253 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3254 __ b(eq, false_target());
3255
3256 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3257 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3258 cc_reg_ = lt;
3259
3260 } else if (check->Equals(Heap::boolean_symbol())) {
3261 __ cmp(r1, Operand(Factory::true_value()));
3262 __ b(eq, true_target());
3263 __ cmp(r1, Operand(Factory::false_value()));
3264 cc_reg_ = eq;
3265
3266 } else if (check->Equals(Heap::undefined_symbol())) {
3267 __ cmp(r1, Operand(Factory::undefined_value()));
3268 __ b(eq, true_target());
3269
3270 __ tst(r1, Operand(kSmiTagMask));
3271 __ b(eq, false_target());
3272
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003273 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3275 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3276 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3277 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3278
3279 cc_reg_ = eq;
3280
3281 } else if (check->Equals(Heap::function_symbol())) {
3282 __ tst(r1, Operand(kSmiTagMask));
3283 __ b(eq, false_target());
3284 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3285 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3286 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3287 cc_reg_ = eq;
3288
3289 } else if (check->Equals(Heap::object_symbol())) {
3290 __ tst(r1, Operand(kSmiTagMask));
3291 __ b(eq, false_target());
3292
3293 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3294 __ cmp(r1, Operand(Factory::null_value()));
3295 __ b(eq, true_target());
3296
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003297 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003298 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3299 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3300 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3301 __ b(eq, false_target());
3302
3303 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3304 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3305 __ b(lt, false_target());
3306 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3307 cc_reg_ = le;
3308
3309 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003310 // Uncommon case: typeof testing against a string literal that is
3311 // never returned from the typeof operator.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003312 __ b(false_target());
3313 }
3314 return;
3315 }
3316
3317 Load(left);
3318 Load(right);
3319 switch (op) {
3320 case Token::EQ:
3321 Comparison(eq, false);
3322 break;
3323
3324 case Token::LT:
3325 Comparison(lt);
3326 break;
3327
3328 case Token::GT:
3329 Comparison(gt);
3330 break;
3331
3332 case Token::LTE:
3333 Comparison(le);
3334 break;
3335
3336 case Token::GTE:
3337 Comparison(ge);
3338 break;
3339
3340 case Token::EQ_STRICT:
3341 Comparison(eq, true);
3342 break;
3343
3344 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003345 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003346 __ InvokeBuiltin(Builtins::IN, CALL_JS);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003347 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348 break;
3349
3350 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003351 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003352 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003353 __ tst(r0, Operand(r0));
3354 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355 break;
3356
3357 default:
3358 UNREACHABLE();
3359 }
3360}
3361
3362
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003363#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003364#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365
ager@chromium.org7c537e22008-10-16 08:43:32 +00003366Handle<String> Reference::GetName() {
3367 ASSERT(type_ == NAMED);
3368 Property* property = expression_->AsProperty();
3369 if (property == NULL) {
3370 // Global variable reference treated as a named property reference.
3371 VariableProxy* proxy = expression_->AsVariableProxy();
3372 ASSERT(proxy->AsVariable() != NULL);
3373 ASSERT(proxy->AsVariable()->is_global());
3374 return proxy->name();
3375 } else {
3376 Literal* raw_name = property->key()->AsLiteral();
3377 ASSERT(raw_name != NULL);
3378 return Handle<String>(String::cast(*raw_name->handle()));
3379 }
3380}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003381
ager@chromium.org7c537e22008-10-16 08:43:32 +00003382
3383void Reference::GetValue(TypeofState typeof_state) {
3384 ASSERT(!is_illegal());
3385 ASSERT(!cgen_->has_cc());
3386 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003387 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003388 Property* property = expression_->AsProperty();
3389 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003390 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003391 }
3392
3393 switch (type_) {
3394 case SLOT: {
3395 Comment cmnt(masm, "[ Load from Slot");
3396 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3397 ASSERT(slot != NULL);
3398 cgen_->LoadFromSlot(slot, typeof_state);
3399 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003400 }
3401
ager@chromium.org7c537e22008-10-16 08:43:32 +00003402 case NAMED: {
3403 // TODO(1241834): Make sure that this it is safe to ignore the
3404 // distinction between expressions in a typeof and not in a typeof. If
3405 // there is a chance that reference errors can be thrown below, we
3406 // must distinguish between the two kinds of loads (typeof expression
3407 // loads must not throw a reference error).
3408 Comment cmnt(masm, "[ Load from named Property");
3409 // Setup the name register.
3410 Handle<String> name(GetName());
3411 __ mov(r2, Operand(name));
3412 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3413
3414 Variable* var = expression_->AsVariableProxy()->AsVariable();
3415 if (var != NULL) {
3416 ASSERT(var->is_global());
3417 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3418 } else {
3419 __ Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003420 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003421 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003422 break;
3423 }
3424
3425 case KEYED: {
3426 // TODO(1241834): Make sure that this it is safe to ignore the
3427 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003428
3429 // TODO(181): Implement inlined version of array indexing once
3430 // loop nesting is properly tracked on ARM.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003431 Comment cmnt(masm, "[ Load from keyed Property");
3432 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003433 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
3434
3435 Variable* var = expression_->AsVariableProxy()->AsVariable();
3436 if (var != NULL) {
3437 ASSERT(var->is_global());
3438 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3439 } else {
3440 __ Call(ic, RelocInfo::CODE_TARGET);
3441 }
3442 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003443 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003444 }
3445
3446 default:
3447 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003448 }
3449}
3450
3451
ager@chromium.org7c537e22008-10-16 08:43:32 +00003452void Reference::SetValue(InitState init_state) {
3453 ASSERT(!is_illegal());
3454 ASSERT(!cgen_->has_cc());
3455 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003456 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003457 Property* property = expression_->AsProperty();
3458 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003459 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003460 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003461
ager@chromium.org7c537e22008-10-16 08:43:32 +00003462 switch (type_) {
3463 case SLOT: {
3464 Comment cmnt(masm, "[ Store to Slot");
3465 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3466 ASSERT(slot != NULL);
3467 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003468 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003469
ager@chromium.org7c537e22008-10-16 08:43:32 +00003470 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003471 frame->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003472 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003473 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003474
ager@chromium.org7c537e22008-10-16 08:43:32 +00003475 if (init_state == CONST_INIT) {
3476 // Same as the case for a normal store, but ignores attribute
3477 // (e.g. READ_ONLY) of context slot so that we can initialize
3478 // const properties (introduced via eval("const foo = (some
3479 // expr);")). Also, uses the current function context instead of
3480 // the top context.
3481 //
3482 // Note that we must declare the foo upon entry of eval(), via a
3483 // context slot declaration, but we cannot initialize it at the
3484 // same time, because the const declaration may be at the end of
3485 // the eval code (sigh...) and the const variable may have been
3486 // used before (where its value is 'undefined'). Thus, we can only
3487 // do the initialization when we actually encounter the expression
3488 // and when the expression operands are defined and valid, and
3489 // thus we need the split into 2 operations: declaration of the
3490 // context slot followed by initialization.
3491 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3492 } else {
3493 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3494 }
3495 // Storing a variable must keep the (new) value on the expression
3496 // stack. This is necessary for compiling assignment expressions.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003497 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003498
ager@chromium.org7c537e22008-10-16 08:43:32 +00003499 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003500 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003501
ager@chromium.org7c537e22008-10-16 08:43:32 +00003502 Label exit;
3503 if (init_state == CONST_INIT) {
3504 ASSERT(slot->var()->mode() == Variable::CONST);
3505 // Only the first const initialization must be executed (the slot
3506 // still contains 'the hole' value). When the assignment is
3507 // executed, the code is identical to a normal store (see below).
3508 Comment cmnt(masm, "[ Init const");
3509 __ ldr(r2, cgen_->SlotOperand(slot, r2));
3510 __ cmp(r2, Operand(Factory::the_hole_value()));
3511 __ b(ne, &exit);
3512 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003513
ager@chromium.org7c537e22008-10-16 08:43:32 +00003514 // We must execute the store. Storing a variable must keep the
3515 // (new) value on the stack. This is necessary for compiling
3516 // assignment expressions.
3517 //
3518 // Note: We will reach here even with slot->var()->mode() ==
3519 // Variable::CONST because of const declarations which will
3520 // initialize consts to 'the hole' value and by doing so, end up
3521 // calling this code. r2 may be loaded with context; used below in
3522 // RecordWrite.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003523 frame->Pop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003524 __ str(r0, cgen_->SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003525 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003526 if (slot->type() == Slot::CONTEXT) {
3527 // Skip write barrier if the written value is a smi.
3528 __ tst(r0, Operand(kSmiTagMask));
3529 __ b(eq, &exit);
3530 // r2 is loaded with context when calling SlotOperand above.
3531 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3532 __ mov(r3, Operand(offset));
3533 __ RecordWrite(r2, r3, r1);
3534 }
3535 // If we definitely did not jump over the assignment, we do not need
3536 // to bind the exit label. Doing so can defeat peephole
3537 // optimization.
3538 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3539 __ bind(&exit);
3540 }
3541 }
3542 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003543 }
3544
ager@chromium.org7c537e22008-10-16 08:43:32 +00003545 case NAMED: {
3546 Comment cmnt(masm, "[ Store to named Property");
3547 // Call the appropriate IC code.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003548 frame->Pop(r0); // value
ager@chromium.org7c537e22008-10-16 08:43:32 +00003549 // Setup the name register.
3550 Handle<String> name(GetName());
3551 __ mov(r2, Operand(name));
3552 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3553 __ Call(ic, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003554 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003555 break;
3556 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003557
ager@chromium.org7c537e22008-10-16 08:43:32 +00003558 case KEYED: {
3559 Comment cmnt(masm, "[ Store to keyed Property");
3560 Property* property = expression_->AsProperty();
3561 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003562 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003563
3564 // Call IC code.
3565 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3566 // TODO(1222589): Make the IC grab the values from the stack.
3567 frame->Pop(r0); // value
3568 __ Call(ic, RelocInfo::CODE_TARGET);
3569 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003570 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003571 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003572
3573 default:
3574 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003575 }
3576}
3577
3578
3579void GetPropertyStub::Generate(MacroAssembler* masm) {
3580 // sp[0]: key
3581 // sp[1]: receiver
3582 Label slow, fast;
3583 // Get the key and receiver object from the stack.
3584 __ ldm(ia, sp, r0.bit() | r1.bit());
3585 // Check that the key is a smi.
3586 __ tst(r0, Operand(kSmiTagMask));
3587 __ b(ne, &slow);
3588 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3589 // Check that the object isn't a smi.
3590 __ tst(r1, Operand(kSmiTagMask));
3591 __ b(eq, &slow);
3592
3593 // Check that the object is some kind of JS object EXCEPT JS Value type.
3594 // In the case that the object is a value-wrapper object,
3595 // we enter the runtime system to make sure that indexing into string
3596 // objects work as intended.
3597 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3598 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3599 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3600 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3601 __ b(lt, &slow);
3602
3603 // Get the elements array of the object.
3604 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3605 // Check that the object is in fast mode (not dictionary).
3606 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3607 __ cmp(r3, Operand(Factory::hash_table_map()));
3608 __ b(eq, &slow);
3609 // Check that the key (index) is within bounds.
3610 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3611 __ cmp(r0, Operand(r3));
3612 __ b(lo, &fast);
3613
3614 // Slow case: Push extra copies of the arguments (2).
3615 __ bind(&slow);
3616 __ ldm(ia, sp, r0.bit() | r1.bit());
3617 __ stm(db_w, sp, r0.bit() | r1.bit());
3618 // Do tail-call to runtime routine.
3619 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3620
3621 // Fast case: Do the load.
3622 __ bind(&fast);
3623 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3624 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3625 __ cmp(r0, Operand(Factory::the_hole_value()));
3626 // In case the loaded value is the_hole we have to consult GetProperty
3627 // to ensure the prototype chain is searched.
3628 __ b(eq, &slow);
3629
3630 __ StubReturn(1);
3631}
3632
3633
3634void SetPropertyStub::Generate(MacroAssembler* masm) {
3635 // r0 : value
3636 // sp[0] : key
3637 // sp[1] : receiver
3638
3639 Label slow, fast, array, extra, exit;
3640 // Get the key and the object from the stack.
3641 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3642 // Check that the key is a smi.
3643 __ tst(r1, Operand(kSmiTagMask));
3644 __ b(ne, &slow);
3645 // Check that the object isn't a smi.
3646 __ tst(r3, Operand(kSmiTagMask));
3647 __ b(eq, &slow);
3648 // Get the type of the object from its map.
3649 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3650 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3651 // Check if the object is a JS array or not.
3652 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3653 __ b(eq, &array);
3654 // Check that the object is some kind of JS object.
3655 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3656 __ b(lt, &slow);
3657
3658
3659 // Object case: Check key against length in the elements array.
3660 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3661 // Check that the object is in fast mode (not dictionary).
3662 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3663 __ cmp(r2, Operand(Factory::hash_table_map()));
3664 __ b(eq, &slow);
3665 // Untag the key (for checking against untagged length in the fixed array).
3666 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3667 // Compute address to store into and check array bounds.
3668 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3669 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3670 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3671 __ cmp(r1, Operand(ip));
3672 __ b(lo, &fast);
3673
3674
3675 // Slow case: Push extra copies of the arguments (3).
3676 __ bind(&slow);
3677 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3678 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3679 // Do tail-call to runtime routine.
3680 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3681
3682
3683 // Extra capacity case: Check if there is extra capacity to
3684 // perform the store and update the length. Used for adding one
3685 // element to the array by writing to array[array.length].
3686 // r0 == value, r1 == key, r2 == elements, r3 == object
3687 __ bind(&extra);
3688 __ b(ne, &slow); // do not leave holes in the array
3689 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3690 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3691 __ cmp(r1, Operand(ip));
3692 __ b(hs, &slow);
3693 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3694 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3695 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3696 __ mov(r3, Operand(r2));
3697 // NOTE: Computing the address to store into must take the fact
3698 // that the key has been incremented into account.
3699 int displacement = Array::kHeaderSize - kHeapObjectTag -
3700 ((1 << kSmiTagSize) * 2);
3701 __ add(r2, r2, Operand(displacement));
3702 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3703 __ b(&fast);
3704
3705
3706 // Array case: Get the length and the elements array from the JS
3707 // array. Check that the array is in fast mode; if it is the
3708 // length is always a smi.
3709 // r0 == value, r3 == object
3710 __ bind(&array);
3711 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3712 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3713 __ cmp(r1, Operand(Factory::hash_table_map()));
3714 __ b(eq, &slow);
3715
3716 // Check the key against the length in the array, compute the
3717 // address to store into and fall through to fast case.
3718 __ ldr(r1, MemOperand(sp));
3719 // r0 == value, r1 == key, r2 == elements, r3 == object.
3720 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3721 __ cmp(r1, Operand(ip));
3722 __ b(hs, &extra);
3723 __ mov(r3, Operand(r2));
3724 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3725 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3726
3727
3728 // Fast case: Do the store.
3729 // r0 == value, r2 == address to store into, r3 == elements
3730 __ bind(&fast);
3731 __ str(r0, MemOperand(r2));
3732 // Skip write barrier if the written value is a smi.
3733 __ tst(r0, Operand(kSmiTagMask));
3734 __ b(eq, &exit);
3735 // Update write barrier for the elements array address.
3736 __ sub(r1, r2, Operand(r3));
3737 __ RecordWrite(r3, r1, r2);
3738 __ bind(&exit);
3739 __ StubReturn(1);
3740}
3741
3742
3743void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3744 // r1 : x
3745 // r0 : y
3746 // result : r0
3747
3748 switch (op_) {
3749 case Token::ADD: {
3750 Label slow, exit;
3751 // fast path
3752 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3753 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3754 // go slow-path in case of overflow
3755 __ b(vs, &slow);
3756 // go slow-path in case of non-smi operands
3757 ASSERT(kSmiTag == 0); // adjust code below
3758 __ tst(r2, Operand(kSmiTagMask));
3759 __ b(eq, &exit);
3760 // slow path
3761 __ bind(&slow);
3762 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3763 __ push(r1);
3764 __ push(r0);
3765 __ mov(r0, Operand(1)); // set number of arguments
3766 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3767 // done
3768 __ bind(&exit);
3769 break;
3770 }
3771
3772 case Token::SUB: {
3773 Label slow, exit;
3774 // fast path
3775 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3776 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3777 // go slow-path in case of overflow
3778 __ b(vs, &slow);
3779 // go slow-path in case of non-smi operands
3780 ASSERT(kSmiTag == 0); // adjust code below
3781 __ tst(r2, Operand(kSmiTagMask));
3782 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3783 __ b(eq, &exit);
3784 // slow path
3785 __ bind(&slow);
3786 __ push(r1);
3787 __ push(r0);
3788 __ mov(r0, Operand(1)); // set number of arguments
3789 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3790 // done
3791 __ bind(&exit);
3792 break;
3793 }
3794
3795 case Token::MUL: {
3796 Label slow, exit;
3797 // tag check
3798 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3799 ASSERT(kSmiTag == 0); // adjust code below
3800 __ tst(r2, Operand(kSmiTagMask));
3801 __ b(ne, &slow);
3802 // remove tag from one operand (but keep sign), so that result is smi
3803 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3804 // do multiplication
3805 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3806 // go slow on overflows (overflow bit is not set)
3807 __ mov(ip, Operand(r3, ASR, 31));
3808 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3809 __ b(ne, &slow);
3810 // go slow on zero result to handle -0
3811 __ tst(r3, Operand(r3));
3812 __ mov(r0, Operand(r3), LeaveCC, ne);
3813 __ b(ne, &exit);
3814 // slow case
3815 __ bind(&slow);
3816 __ push(r1);
3817 __ push(r0);
3818 __ mov(r0, Operand(1)); // set number of arguments
3819 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3820 // done
3821 __ bind(&exit);
3822 break;
3823 }
3824
3825 case Token::BIT_OR:
3826 case Token::BIT_AND:
3827 case Token::BIT_XOR: {
3828 Label slow, exit;
3829 // tag check
3830 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3831 ASSERT(kSmiTag == 0); // adjust code below
3832 __ tst(r2, Operand(kSmiTagMask));
3833 __ b(ne, &slow);
3834 switch (op_) {
3835 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3836 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3837 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3838 default: UNREACHABLE();
3839 }
3840 __ b(&exit);
3841 __ bind(&slow);
3842 __ push(r1); // restore stack
3843 __ push(r0);
3844 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3845 switch (op_) {
3846 case Token::BIT_OR:
3847 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3848 break;
3849 case Token::BIT_AND:
3850 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3851 break;
3852 case Token::BIT_XOR:
3853 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3854 break;
3855 default:
3856 UNREACHABLE();
3857 }
3858 __ bind(&exit);
3859 break;
3860 }
3861
3862 case Token::SHL:
3863 case Token::SHR:
3864 case Token::SAR: {
3865 Label slow, exit;
3866 // tag check
3867 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3868 ASSERT(kSmiTag == 0); // adjust code below
3869 __ tst(r2, Operand(kSmiTagMask));
3870 __ b(ne, &slow);
3871 // remove tags from operands (but keep sign)
3872 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3873 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3874 // use only the 5 least significant bits of the shift count
3875 __ and_(r2, r2, Operand(0x1f));
3876 // perform operation
3877 switch (op_) {
3878 case Token::SAR:
3879 __ mov(r3, Operand(r3, ASR, r2));
3880 // no checks of result necessary
3881 break;
3882
3883 case Token::SHR:
3884 __ mov(r3, Operand(r3, LSR, r2));
3885 // check that the *unsigned* result fits in a smi
3886 // neither of the two high-order bits can be set:
3887 // - 0x80000000: high bit would be lost when smi tagging
3888 // - 0x40000000: this number would convert to negative when
3889 // smi tagging these two cases can only happen with shifts
3890 // by 0 or 1 when handed a valid smi
3891 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3892 __ b(ne, &slow);
3893 break;
3894
3895 case Token::SHL:
3896 __ mov(r3, Operand(r3, LSL, r2));
3897 // check that the *signed* result fits in a smi
3898 __ add(r2, r3, Operand(0x40000000), SetCC);
3899 __ b(mi, &slow);
3900 break;
3901
3902 default: UNREACHABLE();
3903 }
3904 // tag result and store it in r0
3905 ASSERT(kSmiTag == 0); // adjust code below
3906 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3907 __ b(&exit);
3908 // slow case
3909 __ bind(&slow);
3910 __ push(r1); // restore stack
3911 __ push(r0);
3912 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3913 switch (op_) {
3914 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3915 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3916 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3917 default: UNREACHABLE();
3918 }
3919 __ bind(&exit);
3920 break;
3921 }
3922
3923 default: UNREACHABLE();
3924 }
3925 __ Ret();
3926}
3927
3928
3929void StackCheckStub::Generate(MacroAssembler* masm) {
3930 Label within_limit;
3931 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3932 __ ldr(ip, MemOperand(ip));
3933 __ cmp(sp, Operand(ip));
3934 __ b(hs, &within_limit);
3935 // Do tail-call to runtime routine.
3936 __ push(r0);
3937 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
3938 __ bind(&within_limit);
3939
3940 __ StubReturn(1);
3941}
3942
3943
3944void UnarySubStub::Generate(MacroAssembler* masm) {
3945 Label undo;
3946 Label slow;
3947 Label done;
3948
3949 // Enter runtime system if the value is not a smi.
3950 __ tst(r0, Operand(kSmiTagMask));
3951 __ b(ne, &slow);
3952
3953 // Enter runtime system if the value of the expression is zero
3954 // to make sure that we switch between 0 and -0.
3955 __ cmp(r0, Operand(0));
3956 __ b(eq, &slow);
3957
3958 // The value of the expression is a smi that is not zero. Try
3959 // optimistic subtraction '0 - value'.
3960 __ rsb(r1, r0, Operand(0), SetCC);
3961 __ b(vs, &slow);
3962
3963 // If result is a smi we are done.
3964 __ tst(r1, Operand(kSmiTagMask));
3965 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
3966 __ b(eq, &done);
3967
3968 // Enter runtime system.
3969 __ bind(&slow);
3970 __ push(r0);
3971 __ mov(r0, Operand(0)); // set number of arguments
3972 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3973
3974 __ bind(&done);
3975 __ StubReturn(1);
3976}
3977
3978
3979void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
3980 __ push(r0);
3981 __ mov(r0, Operand(0)); // set number of arguments
3982 switch (kind_) {
3983 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
3984 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
3985 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
3986 default: UNREACHABLE();
3987 }
3988 __ StubReturn(argc_);
3989}
3990
3991
3992void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3993 // r0 holds exception
3994 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
3995 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3996 __ ldr(sp, MemOperand(r3));
3997 __ pop(r2); // pop next in chain
3998 __ str(r2, MemOperand(r3));
3999 // restore parameter- and frame-pointer and pop state.
4000 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4001 // Before returning we restore the context from the frame pointer if not NULL.
4002 // The frame pointer is NULL in the exception handler of a JS entry frame.
4003 __ cmp(fp, Operand(0));
4004 // Set cp to NULL if fp is NULL.
4005 __ mov(cp, Operand(0), LeaveCC, eq);
4006 // Restore cp otherwise.
4007 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
4008 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
4009 __ pop(pc);
4010}
4011
4012
4013void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4014 // Fetch top stack handler.
4015 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4016 __ ldr(r3, MemOperand(r3));
4017
4018 // Unwind the handlers until the ENTRY handler is found.
4019 Label loop, done;
4020 __ bind(&loop);
4021 // Load the type of the current stack handler.
4022 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4023 StackHandlerConstants::kStateOffset;
4024 __ ldr(r2, MemOperand(r3, kStateOffset));
4025 __ cmp(r2, Operand(StackHandler::ENTRY));
4026 __ b(eq, &done);
4027 // Fetch the next handler in the list.
4028 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4029 StackHandlerConstants::kNextOffset;
4030 __ ldr(r3, MemOperand(r3, kNextOffset));
4031 __ jmp(&loop);
4032 __ bind(&done);
4033
4034 // Set the top handler address to next handler past the current ENTRY handler.
4035 __ ldr(r0, MemOperand(r3, kNextOffset));
4036 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4037 __ str(r0, MemOperand(r2));
4038
4039 // Set external caught exception to false.
4040 __ mov(r0, Operand(false));
4041 ExternalReference external_caught(Top::k_external_caught_exception_address);
4042 __ mov(r2, Operand(external_caught));
4043 __ str(r0, MemOperand(r2));
4044
4045 // Set pending exception and r0 to out of memory exception.
4046 Failure* out_of_memory = Failure::OutOfMemoryException();
4047 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4048 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4049 __ str(r0, MemOperand(r2));
4050
4051 // Restore the stack to the address of the ENTRY handler
4052 __ mov(sp, Operand(r3));
4053
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004054 // Stack layout at this point. See also PushTryHandler
4055 // r3, sp -> next handler
4056 // state (ENTRY)
4057 // pp
4058 // fp
4059 // lr
4060
4061 // Discard ENTRY state (r2 is not used), and restore parameter-
4062 // and frame-pointer and pop state.
4063 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004064 // Before returning we restore the context from the frame pointer if not NULL.
4065 // The frame pointer is NULL in the exception handler of a JS entry frame.
4066 __ cmp(fp, Operand(0));
4067 // Set cp to NULL if fp is NULL.
4068 __ mov(cp, Operand(0), LeaveCC, eq);
4069 // Restore cp otherwise.
4070 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
4071 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
4072 __ pop(pc);
4073}
4074
4075
4076void CEntryStub::GenerateCore(MacroAssembler* masm,
4077 Label* throw_normal_exception,
4078 Label* throw_out_of_memory_exception,
4079 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004080 bool do_gc,
4081 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004082 // r0: result parameter for PerformGC, if any
4083 // r4: number of arguments including receiver (C callee-saved)
4084 // r5: pointer to builtin function (C callee-saved)
4085 // r6: pointer to the first argument (C callee-saved)
4086
4087 if (do_gc) {
4088 // Passing r0.
4089 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4090 }
4091
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004092 ExternalReference scope_depth =
4093 ExternalReference::heap_always_allocate_scope_depth();
4094 if (always_allocate) {
4095 __ mov(r0, Operand(scope_depth));
4096 __ ldr(r1, MemOperand(r0));
4097 __ add(r1, r1, Operand(1));
4098 __ str(r1, MemOperand(r0));
4099 }
4100
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004101 // Call C built-in.
4102 // r0 = argc, r1 = argv
4103 __ mov(r0, Operand(r4));
4104 __ mov(r1, Operand(r6));
4105
4106 // TODO(1242173): To let the GC traverse the return address of the exit
4107 // frames, we need to know where the return address is. Right now,
4108 // we push it on the stack to be able to find it again, but we never
4109 // restore from it in case of changes, which makes it impossible to
4110 // support moving the C entry code stub. This should be fixed, but currently
4111 // this is OK because the CEntryStub gets generated so early in the V8 boot
4112 // sequence that it is not moving ever.
4113 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4114 __ push(lr);
4115#if !defined(__arm__)
4116 // Notify the simulator of the transition to C code.
4117 __ swi(assembler::arm::call_rt_r5);
4118#else /* !defined(__arm__) */
4119 __ mov(pc, Operand(r5));
4120#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004121
4122 if (always_allocate) {
4123 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4124 // though (contain the result).
4125 __ mov(r2, Operand(scope_depth));
4126 __ ldr(r3, MemOperand(r2));
4127 __ sub(r3, r3, Operand(1));
4128 __ str(r3, MemOperand(r2));
4129 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004130
4131 // check for failure result
4132 Label failure_returned;
4133 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4134 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4135 __ add(r2, r0, Operand(1));
4136 __ tst(r2, Operand(kFailureTagMask));
4137 __ b(eq, &failure_returned);
4138
4139 // Exit C frame and return.
4140 // r0:r1: result
4141 // sp: stack pointer
4142 // fp: frame pointer
4143 // pp: caller's parameter pointer pp (restored as C callee-saved)
4144 __ LeaveExitFrame(frame_type);
4145
4146 // check if we should retry or throw exception
4147 Label retry;
4148 __ bind(&failure_returned);
4149 ASSERT(Failure::RETRY_AFTER_GC == 0);
4150 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4151 __ b(eq, &retry);
4152
4153 Label continue_exception;
4154 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4155 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4156 __ b(ne, &continue_exception);
4157
4158 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004159 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004160 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004161 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004162 __ ldr(r0, MemOperand(ip));
4163 __ str(r3, MemOperand(ip));
4164
4165 __ bind(&continue_exception);
4166 // Special handling of out of memory exception.
4167 Failure* out_of_memory = Failure::OutOfMemoryException();
4168 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4169 __ b(eq, throw_out_of_memory_exception);
4170
4171 // Handle normal exception.
4172 __ jmp(throw_normal_exception);
4173
4174 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4175}
4176
4177
4178void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4179 // Called from JavaScript; parameters are on stack as if calling JS function
4180 // r0: number of arguments including receiver
4181 // r1: pointer to builtin function
4182 // fp: frame pointer (restored after C call)
4183 // sp: stack pointer (restored as callee's pp after C call)
4184 // cp: current context (C callee-saved)
4185 // pp: caller's parameter pointer pp (C callee-saved)
4186
4187 // NOTE: Invocations of builtins may return failure objects
4188 // instead of a proper result. The builtin entry handles
4189 // this by performing a garbage collection and retrying the
4190 // builtin once.
4191
4192 StackFrame::Type frame_type = is_debug_break
4193 ? StackFrame::EXIT_DEBUG
4194 : StackFrame::EXIT;
4195
4196 // Enter the exit frame that transitions from JavaScript to C++.
4197 __ EnterExitFrame(frame_type);
4198
4199 // r4: number of arguments (C callee-saved)
4200 // r5: pointer to builtin function (C callee-saved)
4201 // r6: pointer to first argument (C callee-saved)
4202
4203 Label throw_out_of_memory_exception;
4204 Label throw_normal_exception;
4205
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004206 // Call into the runtime system. Collect garbage before the call if
4207 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004208 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004209 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004210 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4211 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004212 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004213 &throw_out_of_memory_exception,
4214 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004215 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004216 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004217
4218 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004219 GenerateCore(masm,
4220 &throw_normal_exception,
4221 &throw_out_of_memory_exception,
4222 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004223 true,
4224 false);
4225
4226 // Do full GC and retry runtime call one final time.
4227 Failure* failure = Failure::InternalError();
4228 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4229 GenerateCore(masm,
4230 &throw_normal_exception,
4231 &throw_out_of_memory_exception,
4232 frame_type,
4233 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004234 true);
4235
4236 __ bind(&throw_out_of_memory_exception);
4237 GenerateThrowOutOfMemory(masm);
4238 // control flow for generated will not return.
4239
4240 __ bind(&throw_normal_exception);
4241 GenerateThrowTOS(masm);
4242}
4243
4244
4245void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4246 // r0: code entry
4247 // r1: function
4248 // r2: receiver
4249 // r3: argc
4250 // [sp+0]: argv
4251
4252 Label invoke, exit;
4253
4254 // Called from C, so do not pop argc and args on exit (preserve sp)
4255 // No need to save register-passed args
4256 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4257 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4258
4259 // Get address of argv, see stm above.
4260 // r0: code entry
4261 // r1: function
4262 // r2: receiver
4263 // r3: argc
4264 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4265 __ ldr(r4, MemOperand(r4)); // argv
4266
4267 // Push a frame with special values setup to mark it as an entry frame.
4268 // r0: code entry
4269 // r1: function
4270 // r2: receiver
4271 // r3: argc
4272 // r4: argv
4273 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4274 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4275 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4276 __ mov(r6, Operand(Smi::FromInt(marker)));
4277 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4278 __ ldr(r5, MemOperand(r5));
4279 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4280
4281 // Setup frame pointer for the frame to be pushed.
4282 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4283
4284 // Call a faked try-block that does the invoke.
4285 __ bl(&invoke);
4286
4287 // Caught exception: Store result (exception) in the pending
4288 // exception field in the JSEnv and return a failure sentinel.
4289 // Coming in here the fp will be invalid because the PushTryHandler below
4290 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00004291 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004292 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004293 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004294 __ b(&exit);
4295
4296 // Invoke: Link this frame into the handler chain.
4297 __ bind(&invoke);
4298 // Must preserve r0-r4, r5-r7 are available.
4299 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4300 // If an exception not caught by another handler occurs, this handler returns
4301 // control to the code after the bl(&invoke) above, which restores all
4302 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4303 // before returning a failure to C.
4304
4305 // Clear any pending exceptions.
4306 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4307 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004308 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004309 __ str(r5, MemOperand(ip));
4310
4311 // Invoke the function by calling through JS entry trampoline builtin.
4312 // Notice that we cannot store a reference to the trampoline code directly in
4313 // this stub, because runtime stubs are not traversed when doing GC.
4314
4315 // Expected registers by Builtins::JSEntryTrampoline
4316 // r0: code entry
4317 // r1: function
4318 // r2: receiver
4319 // r3: argc
4320 // r4: argv
4321 if (is_construct) {
4322 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4323 __ mov(ip, Operand(construct_entry));
4324 } else {
4325 ExternalReference entry(Builtins::JSEntryTrampoline);
4326 __ mov(ip, Operand(entry));
4327 }
4328 __ ldr(ip, MemOperand(ip)); // deref address
4329
4330 // Branch and link to JSEntryTrampoline
4331 __ mov(lr, Operand(pc));
4332 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4333
4334 // Unlink this frame from the handler chain. When reading the
4335 // address of the next handler, there is no need to use the address
4336 // displacement since the current stack pointer (sp) points directly
4337 // to the stack handler.
4338 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4339 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4340 __ str(r3, MemOperand(ip));
4341 // No need to restore registers
4342 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4343
4344 __ bind(&exit); // r0 holds result
4345 // Restore the top frame descriptors from the stack.
4346 __ pop(r3);
4347 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4348 __ str(r3, MemOperand(ip));
4349
4350 // Reset the stack to the callee saved registers.
4351 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4352
4353 // Restore callee-saved registers and return.
4354#ifdef DEBUG
4355 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4356#endif
4357 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4358}
4359
4360
ager@chromium.org7c537e22008-10-16 08:43:32 +00004361void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004362 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004363 Label adaptor;
4364 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4365 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4366 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004367 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004368
ager@chromium.org7c537e22008-10-16 08:43:32 +00004369 // Nothing to do: The formal number of parameters has already been
4370 // passed in register r0 by calling function. Just return it.
4371 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004372
ager@chromium.org7c537e22008-10-16 08:43:32 +00004373 // Arguments adaptor case: Read the arguments length from the
4374 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004375 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004376 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004377 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004378}
4379
4380
ager@chromium.org7c537e22008-10-16 08:43:32 +00004381void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4382 // The displacement is the offset of the last parameter (if any)
4383 // relative to the frame pointer.
4384 static const int kDisplacement =
4385 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004386
ager@chromium.org7c537e22008-10-16 08:43:32 +00004387 // Check that the key is a smi.
4388 Label slow;
4389 __ tst(r1, Operand(kSmiTagMask));
4390 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004391
ager@chromium.org7c537e22008-10-16 08:43:32 +00004392 // Check if the calling frame is an arguments adaptor frame.
4393 Label adaptor;
4394 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4395 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4396 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4397 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004398
ager@chromium.org7c537e22008-10-16 08:43:32 +00004399 // Check index against formal parameters count limit passed in
4400 // through register eax. Use unsigned comparison to get negative
4401 // check for free.
4402 __ cmp(r1, r0);
4403 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004404
ager@chromium.org7c537e22008-10-16 08:43:32 +00004405 // Read the argument from the stack and return it.
4406 __ sub(r3, r0, r1);
4407 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4408 __ ldr(r0, MemOperand(r3, kDisplacement));
4409 __ mov(pc, lr);
4410
4411 // Arguments adaptor case: Check index against actual arguments
4412 // limit found in the arguments adaptor frame. Use unsigned
4413 // comparison to get negative check for free.
4414 __ bind(&adaptor);
4415 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4416 __ cmp(r1, r0);
4417 __ b(cs, &slow);
4418
4419 // Read the argument from the adaptor frame and return it.
4420 __ sub(r3, r0, r1);
4421 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4422 __ ldr(r0, MemOperand(r3, kDisplacement));
4423 __ mov(pc, lr);
4424
4425 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4426 // by calling the runtime system.
4427 __ bind(&slow);
4428 __ push(r1);
4429 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4430}
4431
4432
4433void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4434 // Check if the calling frame is an arguments adaptor frame.
4435 Label runtime;
4436 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4437 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4438 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4439 __ b(ne, &runtime);
4440
4441 // Patch the arguments.length and the parameters pointer.
4442 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4443 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4444 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4445 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4446 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4447
4448 // Do the runtime call to allocate the arguments object.
4449 __ bind(&runtime);
4450 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004451}
4452
4453
4454void CallFunctionStub::Generate(MacroAssembler* masm) {
4455 Label slow;
4456 // Get the function to call from the stack.
4457 // function, receiver [, arguments]
4458 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4459
4460 // Check that the function is really a JavaScript function.
4461 // r1: pushed function (to be verified)
4462 __ tst(r1, Operand(kSmiTagMask));
4463 __ b(eq, &slow);
4464 // Get the map of the function object.
4465 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4466 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4467 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4468 __ b(ne, &slow);
4469
4470 // Fast-case: Invoke the function now.
4471 // r1: pushed function
4472 ParameterCount actual(argc_);
4473 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4474
4475 // Slow-case: Non-function called.
4476 __ bind(&slow);
4477 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004478 __ mov(r2, Operand(0));
4479 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
4480 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
4481 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004482}
4483
4484
4485#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004487} } // namespace v8::internal