blob: e5120ae73480e599ad2d0f541a3aeb18b4309f1d [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "scopes.h"
34#include "runtime.h"
35
36namespace v8 { namespace internal {
37
ager@chromium.org3bf7b912008-11-17 09:09:45 +000038#define __ masm_->
39
40// -------------------------------------------------------------------------
41// VirtualFrame implementation.
42
43VirtualFrame::VirtualFrame(CodeGenerator* cgen) {
44 ASSERT(cgen->scope() != NULL);
45
46 masm_ = cgen->masm();
47 frame_local_count_ = cgen->scope()->num_stack_slots();
48 parameter_count_ = cgen->scope()->num_parameters();
49}
50
51
52void VirtualFrame::Enter() {
53 Comment cmnt(masm_, "[ Enter JS frame");
54#ifdef DEBUG
55 { Label done, fail;
56 __ tst(r1, Operand(kSmiTagMask));
57 __ b(eq, &fail);
58 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
59 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
60 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
61 __ b(eq, &done);
62 __ bind(&fail);
63 __ stop("CodeGenerator::EnterJSFrame - r1 not a function");
64 __ bind(&done);
65 }
66#endif // DEBUG
67
68 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
69 // Adjust FP to point to saved FP.
70 __ add(fp, sp, Operand(2 * kPointerSize));
71}
72
73
74void VirtualFrame::Exit() {
75 Comment cmnt(masm_, "[ Exit JS frame");
76 // Drop the execution stack down to the frame pointer and restore the caller
77 // frame pointer and return address.
78 __ mov(sp, fp);
79 __ ldm(ia_w, sp, fp.bit() | lr.bit());
80}
81
82
83void VirtualFrame::AllocateLocals() {
84 if (frame_local_count_ > 0) {
85 Comment cmnt(masm_, "[ Allocate space for locals");
86 // Initialize stack slots with 'undefined' value.
87 __ mov(ip, Operand(Factory::undefined_value()));
88 for (int i = 0; i < frame_local_count_; i++) {
89 __ push(ip);
90 }
91 }
92}
93
94
95void VirtualFrame::Drop(int count) {
96 ASSERT(count >= 0);
97 if (count > 0) {
98 __ add(sp, sp, Operand(count * kPointerSize));
99 }
100}
101
102
103void VirtualFrame::Pop() { Drop(1); }
104
105
106void VirtualFrame::Pop(Register reg) {
107 __ pop(reg);
108}
109
110
111void VirtualFrame::Push(Register reg) {
112 __ push(reg);
113}
114
115
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000116// -------------------------------------------------------------------------
117// CodeGenState implementation.
118
ager@chromium.org7c537e22008-10-16 08:43:32 +0000119CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000120 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000121 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000122 true_target_(NULL),
123 false_target_(NULL),
124 previous_(NULL) {
125 owner_->set_state(this);
126}
127
128
ager@chromium.org7c537e22008-10-16 08:43:32 +0000129CodeGenState::CodeGenState(CodeGenerator* owner,
130 TypeofState typeof_state,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000131 Label* true_target,
132 Label* false_target)
133 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000134 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000135 true_target_(true_target),
136 false_target_(false_target),
137 previous_(owner->state()) {
138 owner_->set_state(this);
139}
140
141
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000142CodeGenState::~CodeGenState() {
143 ASSERT(owner_->state() == this);
144 owner_->set_state(previous_);
145}
146
147
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000148// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000149// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150
ager@chromium.org7c537e22008-10-16 08:43:32 +0000151CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
152 bool is_eval)
153 : is_eval_(is_eval),
154 script_(script),
155 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 masm_(new MacroAssembler(NULL, buffer_size)),
157 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000158 frame_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 cc_reg_(al),
160 state_(NULL),
161 break_stack_height_(0) {
162}
163
164
165// Calling conventions:
mads.s.ager31e71382008-08-13 09:32:07 +0000166// r0: the number of arguments
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167// fp: frame pointer
168// sp: stack pointer
169// pp: caller's parameter pointer
170// cp: callee's context
171
ager@chromium.org7c537e22008-10-16 08:43:32 +0000172void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 ZoneList<Statement*>* body = fun->body();
174
175 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000176 ASSERT(scope_ == NULL);
177 scope_ = fun->scope();
178 ASSERT(frame_ == NULL);
179 VirtualFrame virtual_frame(this);
180 frame_ = &virtual_frame;
181 cc_reg_ = al;
182 {
183 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184
185 // Entry
186 // stack: function, receiver, arguments, return address
187 // r0: number of arguments
188 // sp: stack pointer
189 // fp: frame pointer
190 // pp: caller's parameter pointer
191 // cp: callee's context
192
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000193 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 // tos: code slot
195#ifdef DEBUG
196 if (strlen(FLAG_stop_at) > 0 &&
197 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000198 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 }
200#endif
201
202 // Allocate space for locals and initialize them.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000203 frame_->AllocateLocals();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000205 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 // Allocate local context.
207 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000208 __ ldr(r0, frame_->Function());
209 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +0000210 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
211
212 if (kDebug) {
213 Label verified_true;
214 __ cmp(r0, Operand(cp));
215 __ b(eq, &verified_true);
216 __ stop("NewContext: r0 is expected to be the same as cp");
217 __ bind(&verified_true);
218 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000220 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 }
222
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000223 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // 1) only needed if we have a context
225 // 2) no need to recompute context ptr every single time
226 // 3) don't copy parameter operand code from SlotOperand!
227 {
228 Comment cmnt2(masm_, "[ copy context parameters into .context");
229
230 // Note that iteration order is relevant here! If we have the same
231 // parameter twice (e.g., function (x, y, x)), and that parameter
232 // needs to be copied into the context, it must be the last argument
233 // passed to the parameter that needs to be copied. This is a rare
234 // case so we don't check for it, instead we rely on the copying
235 // order: such a parameter is copied repeatedly into the same
236 // context location and thus the last value is what is seen inside
237 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000238 for (int i = 0; i < scope_->num_parameters(); i++) {
239 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 Slot* slot = par->slot();
241 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000242 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
243 __ ldr(r1, frame_->Parameter(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 // Loads r2 with context; used below in RecordWrite.
245 __ str(r1, SlotOperand(slot, r2));
246 // Load the offset into r3.
247 int slot_offset =
248 FixedArray::kHeaderSize + slot->index() * kPointerSize;
249 __ mov(r3, Operand(slot_offset));
250 __ RecordWrite(r2, r3, r1);
251 }
252 }
253 }
254
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000255 // Store the arguments object. This must happen after context
256 // initialization because the arguments object may be stored in the
257 // context.
258 if (scope_->arguments() != NULL) {
259 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000261 { Reference shadow_ref(this, scope_->arguments_shadow());
262 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000263 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000264 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000265 // The receiver is below the arguments, the return address,
266 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000267 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000268 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000269 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000270 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
271 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000272 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000273 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000274 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000275 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000277 frame_->Pop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 }
279
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000280 // Generate code to 'execute' declarations and initialize functions
281 // (source elements). In case of an illegal redeclaration we need to
282 // handle that instead of processing the declarations.
283 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000285 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 } else {
287 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000288 ProcessDeclarations(scope_->declarations());
289 // Bail out if a stack-overflow exception occurred when processing
290 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000291 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 }
293
mads.s.ager31e71382008-08-13 09:32:07 +0000294 if (FLAG_trace) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000295 __ CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000296 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000297 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 CheckStack();
299
300 // Compile the body of the function in a vanilla state. Don't
301 // bother compiling all the code if the scope has an illegal
302 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000303 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 Comment cmnt(masm_, "[ function body");
305#ifdef DEBUG
306 bool is_builtin = Bootstrapper::IsActive();
307 bool should_trace =
308 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000309 if (should_trace) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000310 __ CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000311 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313#endif
314 VisitStatements(body);
315 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 }
317
318 // exit
319 // r0: result
320 // sp: stack pointer
321 // fp: frame pointer
322 // pp: parameter pointer
323 // cp: callee's context
mads.s.ager31e71382008-08-13 09:32:07 +0000324 __ mov(r0, Operand(Factory::undefined_value()));
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 __ bind(&function_return_);
mads.s.ager31e71382008-08-13 09:32:07 +0000327 if (FLAG_trace) {
328 // Push the return value on the stack as the parameter.
329 // Runtime::TraceExit returns the parameter as it is.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000330 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000331 __ CallRuntime(Runtime::kTraceExit, 1);
332 }
333
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000334 // Tear down the frame which will restore the caller's frame pointer and the
335 // link register.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000336 frame_->Exit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000338 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
339 __ mov(pc, lr);
340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // Code generation state must be reset.
342 scope_ = NULL;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000343 frame_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 ASSERT(!has_cc());
345 ASSERT(state_ == NULL);
346}
347
348
ager@chromium.org7c537e22008-10-16 08:43:32 +0000349MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
350 // Currently, this assertion will fail if we try to assign to
351 // a constant variable that is constant because it is read-only
352 // (such as the variable referring to a named function expression).
353 // We need to implement assignments to read-only variables.
354 // Ideally, we should do this during AST generation (by converting
355 // such assignments into expression statements); however, in general
356 // we may not be able to make the decision until past AST generation,
357 // that is when the entire program is known.
358 ASSERT(slot != NULL);
359 int index = slot->index();
360 switch (slot->type()) {
361 case Slot::PARAMETER:
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000362 return frame_->Parameter(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000363
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000364 case Slot::LOCAL:
365 return frame_->Local(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000366
367 case Slot::CONTEXT: {
368 // Follow the context chain if necessary.
369 ASSERT(!tmp.is(cp)); // do not overwrite context register
370 Register context = cp;
371 int chain_length = scope()->ContextChainLength(slot->var()->scope());
372 for (int i = chain_length; i-- > 0;) {
373 // Load the closure.
374 // (All contexts, even 'with' contexts, have a closure,
375 // and it is the same for all contexts inside a function.
376 // There is no need to go to the function context first.)
377 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
378 // Load the function context (which is the incoming, outer context).
379 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
380 context = tmp;
381 }
382 // We may have a 'with' context now. Get the function context.
383 // (In fact this mov may never be the needed, since the scope analysis
384 // may not permit a direct context access in this case and thus we are
385 // always at a function context. However it is safe to dereference be-
386 // cause the function context of a function context is itself. Before
387 // deleting this mov we should try to create a counter-example first,
388 // though...)
389 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
390 return ContextOperand(tmp, index);
391 }
392
393 default:
394 UNREACHABLE();
395 return MemOperand(r0, 0);
396 }
397}
398
399
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000400// Loads a value on TOS. If it is a boolean value, the result may have been
401// (partially) translated into branches, or it may have set the condition
402// code register. If force_cc is set, the value is forced to set the
403// condition code register and no value is pushed. If the condition code
404// register was set, has_cc() is true and cc_reg_ contains the condition to
405// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000406void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000407 TypeofState typeof_state,
408 Label* true_target,
409 Label* false_target,
410 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000411 ASSERT(!has_cc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000414 Visit(x);
415 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000417 // Convert the TOS value to a boolean in the condition code register.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000418 // Visiting an expression may possibly choose neither (a) to leave a
419 // value in the condition code register nor (b) to leave a value in TOS
420 // (eg, by compiling to only jumps to the targets). In that case the
421 // code generated by ToBoolean is wrong because it assumes the value of
422 // the expression in TOS. So long as there is always a value in TOS or
423 // the condition code register when control falls through to here (there
424 // is), the code generated by ToBoolean is dead and therefore safe.
mads.s.ager31e71382008-08-13 09:32:07 +0000425 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 }
427 ASSERT(has_cc() || !force_cc);
428}
429
430
ager@chromium.org7c537e22008-10-16 08:43:32 +0000431void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 Label true_target;
433 Label false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000434 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435
436 if (has_cc()) {
437 // convert cc_reg_ into a bool
438 Label loaded, materialize_true;
439 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000440 __ mov(r0, Operand(Factory::false_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000441 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 __ b(&loaded);
443 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000444 __ mov(r0, Operand(Factory::true_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000445 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446 __ bind(&loaded);
447 cc_reg_ = al;
448 }
449
450 if (true_target.is_linked() || false_target.is_linked()) {
451 // we have at least one condition value
452 // that has been "translated" into a branch,
453 // thus it needs to be loaded explicitly again
454 Label loaded;
455 __ b(&loaded); // don't lose current TOS
456 bool both = true_target.is_linked() && false_target.is_linked();
457 // reincarnate "true", if necessary
458 if (true_target.is_linked()) {
459 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000460 __ mov(r0, Operand(Factory::true_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000461 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 }
463 // if both "true" and "false" need to be reincarnated,
464 // jump across code for "false"
465 if (both)
466 __ b(&loaded);
467 // reincarnate "false", if necessary
468 if (false_target.is_linked()) {
469 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000470 __ mov(r0, Operand(Factory::false_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000471 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 }
473 // everything is loaded at this point
474 __ bind(&loaded);
475 }
476 ASSERT(!has_cc());
477}
478
479
ager@chromium.org7c537e22008-10-16 08:43:32 +0000480void CodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000481 __ ldr(r0, GlobalObject());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000482 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483}
484
485
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000486void CodeGenerator::LoadGlobalReceiver(Register scratch) {
487 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
488 __ ldr(scratch,
489 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
490 frame_->Push(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000491}
492
493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000495// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
496// variables w/o reference errors elsewhere.
497void CodeGenerator::LoadTypeofExpression(Expression* x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 Variable* variable = x->AsVariableProxy()->AsVariable();
499 if (variable != NULL && !variable->is_this() && variable->is_global()) {
500 // NOTE: This is somewhat nasty. We force the compiler to load
501 // the variable as if through '<global>.<variable>' to make sure we
502 // do not get reference errors.
503 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
504 Literal key(variable->name());
505 // TODO(1241834): Fetch the position from the variable instead of using
506 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000507 Property property(&global, &key, RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 Load(&property);
509 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000510 Load(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 }
512}
513
514
ager@chromium.org7c537e22008-10-16 08:43:32 +0000515Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
517 cgen->LoadReference(this);
518}
519
520
521Reference::~Reference() {
522 cgen_->UnloadReference(this);
523}
524
525
ager@chromium.org7c537e22008-10-16 08:43:32 +0000526void CodeGenerator::LoadReference(Reference* ref) {
527 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 Expression* e = ref->expression();
529 Property* property = e->AsProperty();
530 Variable* var = e->AsVariableProxy()->AsVariable();
531
532 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000533 // The expression is either a property or a variable proxy that rewrites
534 // to a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 Load(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000536 // We use a named reference if the key is a literal symbol, unless it is
537 // a string that can be legally parsed as an integer. This is because
538 // otherwise we will not get into the slow case code that handles [] on
539 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 Literal* literal = property->key()->AsLiteral();
541 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000542 if (literal != NULL &&
543 literal->handle()->IsSymbol() &&
544 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 ref->set_type(Reference::NAMED);
546 } else {
547 Load(property->key());
548 ref->set_type(Reference::KEYED);
549 }
550 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000551 // The expression is a variable proxy that does not rewrite to a
552 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 LoadGlobal();
555 ref->set_type(Reference::NAMED);
556 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000557 ASSERT(var->slot() != NULL);
558 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
560 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000561 // Anything else is a runtime error.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 Load(e);
563 __ CallRuntime(Runtime::kThrowReferenceError, 1);
564 }
565}
566
567
ager@chromium.org7c537e22008-10-16 08:43:32 +0000568void CodeGenerator::UnloadReference(Reference* ref) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000569 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000570 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000572 if (size > 0) {
573 frame_->Pop(r0);
574 frame_->Drop(size);
575 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 }
577}
578
579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
581// register to a boolean in the condition code register. The code
582// may jump to 'false_target' in case the register converts to 'false'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000583void CodeGenerator::ToBoolean(Label* true_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000584 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000585 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 // Only the condition code should be set.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000587 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588
589 // Fast case checks
590
mads.s.ager31e71382008-08-13 09:32:07 +0000591 // Check if the value is 'false'.
592 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 __ b(eq, false_target);
594
mads.s.ager31e71382008-08-13 09:32:07 +0000595 // Check if the value is 'true'.
596 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 __ b(eq, true_target);
598
mads.s.ager31e71382008-08-13 09:32:07 +0000599 // Check if the value is 'undefined'.
600 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 __ b(eq, false_target);
602
mads.s.ager31e71382008-08-13 09:32:07 +0000603 // Check if the value is a smi.
604 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000606 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 __ b(eq, true_target);
608
609 // Slow case: call the runtime.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000610 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000611 __ CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000612 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614
615 cc_reg_ = ne;
616}
617
618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619class GetPropertyStub : public CodeStub {
620 public:
621 GetPropertyStub() { }
622
623 private:
624 Major MajorKey() { return GetProperty; }
625 int MinorKey() { return 0; }
626 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627};
628
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630class SetPropertyStub : public CodeStub {
631 public:
632 SetPropertyStub() { }
633
634 private:
635 Major MajorKey() { return SetProperty; }
636 int MinorKey() { return 0; }
637 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638};
639
640
kasper.lund7276f142008-07-30 08:49:36 +0000641class GenericBinaryOpStub : public CodeStub {
642 public:
643 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
644
645 private:
646 Token::Value op_;
647
648 Major MajorKey() { return GenericBinaryOp; }
649 int MinorKey() { return static_cast<int>(op_); }
650 void Generate(MacroAssembler* masm);
651
652 const char* GetName() {
653 switch (op_) {
654 case Token::ADD: return "GenericBinaryOpStub_ADD";
655 case Token::SUB: return "GenericBinaryOpStub_SUB";
656 case Token::MUL: return "GenericBinaryOpStub_MUL";
657 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000658 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
659 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
660 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
661 case Token::SAR: return "GenericBinaryOpStub_SAR";
662 case Token::SHL: return "GenericBinaryOpStub_SHL";
663 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000664 default: return "GenericBinaryOpStub";
665 }
666 }
667
668#ifdef DEBUG
669 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
670#endif
671};
672
673
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674class InvokeBuiltinStub : public CodeStub {
675 public:
676 enum Kind { Inc, Dec, ToNumber };
677 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
678
679 private:
680 Kind kind_;
681 int argc_;
682
683 Major MajorKey() { return InvokeBuiltin; }
684 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
685 void Generate(MacroAssembler* masm);
686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687#ifdef DEBUG
688 void Print() {
689 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
690 static_cast<int>(kind_),
691 argc_);
692 }
693#endif
694};
695
696
ager@chromium.org7c537e22008-10-16 08:43:32 +0000697void CodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +0000698 // sp[0] : y
699 // sp[1] : x
700 // result : r0
701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 // Stub is entered with a call: 'return address' is in lr.
703 switch (op) {
704 case Token::ADD: // fall through.
705 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000706 case Token::MUL:
707 case Token::BIT_OR:
708 case Token::BIT_AND:
709 case Token::BIT_XOR:
710 case Token::SHL:
711 case Token::SHR:
712 case Token::SAR: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000713 frame_->Pop(r0); // r0 : y
714 frame_->Pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +0000715 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 __ CallStub(&stub);
717 break;
718 }
719
720 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +0000721 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000722 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 break;
724 }
725
726 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +0000727 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000728 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729 break;
730 }
731
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 case Token::COMMA:
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000733 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734 // simply discard left value
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000735 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 break;
737
738 default:
739 // Other cases should have been handled before this point.
740 UNREACHABLE();
741 break;
742 }
743}
744
745
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000746class DeferredInlinedSmiOperation: public DeferredCode {
747 public:
748 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
749 int value, bool reversed) :
750 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
751 set_comment("[ DeferredInlinedSmiOperation");
752 }
753
754 virtual void Generate() {
755 switch (op_) {
756 case Token::ADD: {
757 if (reversed_) {
758 // revert optimistic add
759 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
760 __ mov(r1, Operand(Smi::FromInt(value_))); // x
761 } else {
762 // revert optimistic add
763 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
764 __ mov(r0, Operand(Smi::FromInt(value_)));
765 }
766 break;
767 }
768
769 case Token::SUB: {
770 if (reversed_) {
771 // revert optimistic sub
772 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
773 __ mov(r1, Operand(Smi::FromInt(value_)));
774 } else {
775 __ add(r1, r0, Operand(Smi::FromInt(value_)));
776 __ mov(r0, Operand(Smi::FromInt(value_)));
777 }
778 break;
779 }
780
781 case Token::BIT_OR:
782 case Token::BIT_XOR:
783 case Token::BIT_AND: {
784 if (reversed_) {
785 __ mov(r1, Operand(Smi::FromInt(value_)));
786 } else {
787 __ mov(r1, Operand(r0));
788 __ mov(r0, Operand(Smi::FromInt(value_)));
789 }
790 break;
791 }
792
793 case Token::SHL:
794 case Token::SHR:
795 case Token::SAR: {
796 if (!reversed_) {
797 __ mov(r1, Operand(r0));
798 __ mov(r0, Operand(Smi::FromInt(value_)));
799 } else {
800 UNREACHABLE(); // should have been handled in SmiOperation
801 }
802 break;
803 }
804
805 default:
806 // other cases should have been handled before this point.
807 UNREACHABLE();
808 break;
809 }
810
811 GenericBinaryOpStub igostub(op_);
812 __ CallStub(&igostub);
813 }
814
815 private:
816 Token::Value op_;
817 int value_;
818 bool reversed_;
819};
820
821
ager@chromium.org7c537e22008-10-16 08:43:32 +0000822void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000823 Handle<Object> value,
824 bool reversed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 // NOTE: This is an attempt to inline (a bit) more of the code for
826 // some possible smi operations (like + and -) when (at least) one
827 // of the operands is a literal smi. With this optimization, the
828 // performance of the system is increased by ~15%, and the generated
829 // code size is increased by ~1% (measured on a combination of
830 // different benchmarks).
831
mads.s.ager31e71382008-08-13 09:32:07 +0000832 // sp[0] : operand
833
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000834 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835
836 Label exit;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000837 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838
839 switch (op) {
840 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000841 DeferredCode* deferred =
842 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000844 __ add(r0, r0, Operand(value), SetCC);
845 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000847 __ b(ne, deferred->enter());
848 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 break;
850 }
851
852 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000853 DeferredCode* deferred =
854 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000857 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000859 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000861 __ b(vs, deferred->enter());
862 __ tst(r0, Operand(kSmiTagMask));
863 __ b(ne, deferred->enter());
864 __ bind(deferred->exit());
865 break;
866 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000868 case Token::BIT_OR:
869 case Token::BIT_XOR:
870 case Token::BIT_AND: {
871 DeferredCode* deferred =
872 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
873 __ tst(r0, Operand(kSmiTagMask));
874 __ b(ne, deferred->enter());
875 switch (op) {
876 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
877 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
878 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
879 default: UNREACHABLE();
880 }
881 __ bind(deferred->exit());
882 break;
883 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000885 case Token::SHL:
886 case Token::SHR:
887 case Token::SAR: {
888 if (reversed) {
889 __ mov(ip, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000890 frame_->Push(ip);
891 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 GenericBinaryOperation(op);
893
894 } else {
895 int shift_value = int_value & 0x1f; // least significant 5 bits
896 DeferredCode* deferred =
897 new DeferredInlinedSmiOperation(this, op, shift_value, false);
898 __ tst(r0, Operand(kSmiTagMask));
899 __ b(ne, deferred->enter());
900 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
901 switch (op) {
902 case Token::SHL: {
903 __ mov(r2, Operand(r2, LSL, shift_value));
904 // check that the *unsigned* result fits in a smi
905 __ add(r3, r2, Operand(0x40000000), SetCC);
906 __ b(mi, deferred->enter());
907 break;
908 }
909 case Token::SHR: {
910 // LSR by immediate 0 means shifting 32 bits.
911 if (shift_value != 0) {
912 __ mov(r2, Operand(r2, LSR, shift_value));
913 }
914 // check that the *unsigned* result fits in a smi
915 // neither of the two high-order bits can be set:
916 // - 0x80000000: high bit would be lost when smi tagging
917 // - 0x40000000: this number would convert to negative when
918 // smi tagging these two cases can only happen with shifts
919 // by 0 or 1 when handed a valid smi
920 __ and_(r3, r2, Operand(0xc0000000), SetCC);
921 __ b(ne, deferred->enter());
922 break;
923 }
924 case Token::SAR: {
925 if (shift_value != 0) {
926 // ASR by immediate 0 means shifting 32 bits.
927 __ mov(r2, Operand(r2, ASR, shift_value));
928 }
929 break;
930 }
931 default: UNREACHABLE();
932 }
933 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
934 __ bind(deferred->exit());
935 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 break;
937 }
938
939 default:
940 if (!reversed) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000941 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000942 __ mov(r0, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000943 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 } else {
945 __ mov(ip, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000946 frame_->Push(ip);
947 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 }
kasper.lund7276f142008-07-30 08:49:36 +0000949 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 break;
951 }
952
953 __ bind(&exit);
954}
955
956
ager@chromium.org7c537e22008-10-16 08:43:32 +0000957void CodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +0000958 // sp[0] : y
959 // sp[1] : x
960 // result : cc register
961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 // Strict only makes sense for equality comparisons.
963 ASSERT(!strict || cc == eq);
964
965 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000966 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
967 if (cc == gt || cc == le) {
968 cc = ReverseCondition(cc);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000969 frame_->Pop(r1);
970 frame_->Pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000971 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000972 frame_->Pop(r0);
973 frame_->Pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000974 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 __ orr(r2, r0, Operand(r1));
976 __ tst(r2, Operand(kSmiTagMask));
977 __ b(eq, &smi);
978
979 // Perform non-smi comparison by runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000980 frame_->Push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
982 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000983 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 int argc;
985 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000986 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 argc = 1;
988 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000989 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 int ncr; // NaN compare result
991 if (cc == lt || cc == le) {
992 ncr = GREATER;
993 } else {
994 ASSERT(cc == gt || cc == ge); // remaining cases
995 ncr = LESS;
996 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000997 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000998 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 argc = 2;
1000 }
1001
1002 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1003 // tagged as a small integer.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001004 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001005 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001006 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 __ cmp(r0, Operand(0));
1008 __ b(&exit);
1009
1010 // test smi equality by pointer comparison.
1011 __ bind(&smi);
1012 __ cmp(r1, Operand(r0));
1013
1014 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 cc_reg_ = cc;
1016}
1017
1018
kasper.lund7276f142008-07-30 08:49:36 +00001019class CallFunctionStub: public CodeStub {
1020 public:
1021 explicit CallFunctionStub(int argc) : argc_(argc) {}
1022
1023 void Generate(MacroAssembler* masm);
1024
1025 private:
1026 int argc_;
1027
kasper.lund7276f142008-07-30 08:49:36 +00001028#if defined(DEBUG)
1029 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1030#endif // defined(DEBUG)
1031
1032 Major MajorKey() { return CallFunction; }
1033 int MinorKey() { return argc_; }
1034};
1035
1036
mads.s.ager31e71382008-08-13 09:32:07 +00001037// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001038void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001041 for (int i = 0; i < args->length(); i++) {
1042 Load(args->at(i));
1043 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044
kasper.lund7276f142008-07-30 08:49:36 +00001045 // Record the position for debugging purposes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 __ RecordPosition(position);
1047
kasper.lund7276f142008-07-30 08:49:36 +00001048 // Use the shared code stub to call the function.
1049 CallFunctionStub call_function(args->length());
1050 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051
1052 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001053 __ ldr(cp, frame_->Context());
1054 frame_->Pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055}
1056
1057
ager@chromium.org7c537e22008-10-16 08:43:32 +00001058void CodeGenerator::Branch(bool if_true, Label* L) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 ASSERT(has_cc());
1060 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
1061 __ b(cc, L);
1062 cc_reg_ = al;
1063}
1064
1065
ager@chromium.org7c537e22008-10-16 08:43:32 +00001066void CodeGenerator::CheckStack() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 if (FLAG_check_stack) {
1068 Comment cmnt(masm_, "[ check stack");
1069 StackCheckStub stub;
1070 __ CallStub(&stub);
1071 }
1072}
1073
1074
ager@chromium.org7c537e22008-10-16 08:43:32 +00001075void CodeGenerator::VisitBlock(Block* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 Comment cmnt(masm_, "[ Block");
1077 if (FLAG_debug_info) RecordStatementPosition(node);
1078 node->set_break_stack_height(break_stack_height_);
1079 VisitStatements(node->statements());
1080 __ bind(node->break_target());
1081}
1082
1083
ager@chromium.org7c537e22008-10-16 08:43:32 +00001084void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00001085 __ mov(r0, Operand(pairs));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001086 frame_->Push(r0);
1087 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001088 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001089 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001091 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092}
1093
1094
ager@chromium.org7c537e22008-10-16 08:43:32 +00001095void CodeGenerator::VisitDeclaration(Declaration* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 Comment cmnt(masm_, "[ Declaration");
1097 Variable* var = node->proxy()->var();
1098 ASSERT(var != NULL); // must have been resolved
1099 Slot* slot = var->slot();
1100
1101 // If it was not possible to allocate the variable at compile time,
1102 // we need to "declare" it at runtime to make sure it actually
1103 // exists in the local context.
1104 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1105 // Variables with a "LOOKUP" slot were introduced as non-locals
1106 // during variable resolution and must have mode DYNAMIC.
1107 ASSERT(var->mode() == Variable::DYNAMIC);
1108 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001109 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001110 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001111 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 // Declaration nodes are always declared in only two modes.
1113 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1114 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001115 __ mov(r0, Operand(Smi::FromInt(attr)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001116 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 // Push initial value, if any.
1118 // Note: For variables we must not push an initial value (such as
1119 // 'undefined') because we may have a (legal) redeclaration and we
1120 // must not destroy the current value.
1121 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001122 __ mov(r0, Operand(Factory::the_hole_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001123 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 } else if (node->fun() != NULL) {
1125 Load(node->fun());
1126 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001127 __ mov(r0, Operand(0)); // no initial value!
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001128 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001130 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
1131 // Ignore the return value (declarations are statements).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 return;
1133 }
1134
1135 ASSERT(!var->is_global());
1136
1137 // If we have a function or a constant, we need to initialize the variable.
1138 Expression* val = NULL;
1139 if (node->mode() == Variable::CONST) {
1140 val = new Literal(Factory::the_hole_value());
1141 } else {
1142 val = node->fun(); // NULL if we don't have a function
1143 }
1144
1145 if (val != NULL) {
1146 // Set initial value.
1147 Reference target(this, node->proxy());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001148 ASSERT(target.is_slot());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 Load(val);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001150 target.SetValue(NOT_CONST_INIT);
1151 // Get rid of the assigned value (declarations are statements). It's
1152 // safe to pop the value lying on top of the reference before unloading
1153 // the reference itself (which preserves the top of stack) because we
1154 // know it is a zero-sized reference.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001155 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 }
1157}
1158
1159
ager@chromium.org7c537e22008-10-16 08:43:32 +00001160void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 Comment cmnt(masm_, "[ ExpressionStatement");
1162 if (FLAG_debug_info) RecordStatementPosition(node);
1163 Expression* expression = node->expression();
1164 expression->MarkAsStatement();
1165 Load(expression);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001166 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167}
1168
1169
ager@chromium.org7c537e22008-10-16 08:43:32 +00001170void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 Comment cmnt(masm_, "// EmptyStatement");
1172 // nothing to do
1173}
1174
1175
ager@chromium.org7c537e22008-10-16 08:43:32 +00001176void CodeGenerator::VisitIfStatement(IfStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 Comment cmnt(masm_, "[ IfStatement");
1178 // Generate different code depending on which
1179 // parts of the if statement are present or not.
1180 bool has_then_stm = node->HasThenStatement();
1181 bool has_else_stm = node->HasElseStatement();
1182
1183 if (FLAG_debug_info) RecordStatementPosition(node);
1184
1185 Label exit;
1186 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001187 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 Label then;
1189 Label else_;
1190 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001191 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 Branch(false, &else_);
1193 // then
1194 __ bind(&then);
1195 Visit(node->then_statement());
1196 __ b(&exit);
1197 // else
1198 __ bind(&else_);
1199 Visit(node->else_statement());
1200
1201 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001202 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 ASSERT(!has_else_stm);
1204 Label then;
1205 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001206 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &exit, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 Branch(false, &exit);
1208 // then
1209 __ bind(&then);
1210 Visit(node->then_statement());
1211
1212 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001213 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 ASSERT(!has_then_stm);
1215 Label else_;
1216 // if (!cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001217 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 Branch(true, &exit);
1219 // else
1220 __ bind(&else_);
1221 Visit(node->else_statement());
1222
1223 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001224 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 ASSERT(!has_then_stm && !has_else_stm);
1226 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001227 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &exit, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 if (has_cc()) {
1229 cc_reg_ = al;
1230 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001231 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 }
1233 }
1234
1235 // end
1236 __ bind(&exit);
1237}
1238
1239
ager@chromium.org7c537e22008-10-16 08:43:32 +00001240void CodeGenerator::CleanStack(int num_bytes) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001241 ASSERT(num_bytes % kPointerSize == 0);
1242 frame_->Drop(num_bytes / kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243}
1244
1245
ager@chromium.org7c537e22008-10-16 08:43:32 +00001246void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 Comment cmnt(masm_, "[ ContinueStatement");
1248 if (FLAG_debug_info) RecordStatementPosition(node);
1249 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1250 __ b(node->target()->continue_target());
1251}
1252
1253
ager@chromium.org7c537e22008-10-16 08:43:32 +00001254void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 Comment cmnt(masm_, "[ BreakStatement");
1256 if (FLAG_debug_info) RecordStatementPosition(node);
1257 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1258 __ b(node->target()->break_target());
1259}
1260
1261
ager@chromium.org7c537e22008-10-16 08:43:32 +00001262void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 Comment cmnt(masm_, "[ ReturnStatement");
1264 if (FLAG_debug_info) RecordStatementPosition(node);
1265 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00001266 // Move the function result into r0.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001267 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 __ b(&function_return_);
1270}
1271
1272
ager@chromium.org7c537e22008-10-16 08:43:32 +00001273void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 Comment cmnt(masm_, "[ WithEnterStatement");
1275 if (FLAG_debug_info) RecordStatementPosition(node);
1276 Load(node->expression());
kasper.lund7276f142008-07-30 08:49:36 +00001277 __ CallRuntime(Runtime::kPushContext, 1);
1278 if (kDebug) {
1279 Label verified_true;
1280 __ cmp(r0, Operand(cp));
1281 __ b(eq, &verified_true);
1282 __ stop("PushContext: r0 is expected to be the same as cp");
1283 __ bind(&verified_true);
1284 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001286 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287}
1288
1289
ager@chromium.org7c537e22008-10-16 08:43:32 +00001290void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 Comment cmnt(masm_, "[ WithExitStatement");
1292 // Pop context.
1293 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1294 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001295 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296}
1297
1298
ager@chromium.org7c537e22008-10-16 08:43:32 +00001299int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
1300 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001301}
1302
ager@chromium.org7c537e22008-10-16 08:43:32 +00001303int CodeGenerator::FastCaseSwitchMinCaseCount() {
1304 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001305}
1306
1307
ager@chromium.org7c537e22008-10-16 08:43:32 +00001308void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001309 SwitchStatement* node,
1310 int min_index,
1311 int range,
1312 Label* fail_label,
1313 Vector<Label*> case_targets,
1314 Vector<Label> case_labels) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001315
1316 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
1317
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001318 frame_->Pop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001319
1320 // Test for a Smi value in a HeapNumber.
1321 Label is_smi;
1322 __ tst(r0, Operand(kSmiTagMask));
1323 __ b(eq, &is_smi);
1324 __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag));
1325 __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag));
1326 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
1327 __ b(ne, fail_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001328 frame_->Push(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001329 __ CallRuntime(Runtime::kNumberToSmi, 1);
1330 __ bind(&is_smi);
1331
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001332 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001333 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001334 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001335 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1336 if (Smi::IsValid(-min_index)) {
1337 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1338 } else {
1339 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1340 __ add(r0, r0, Operand(Smi::FromInt(1)));
1341 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001342 } else {
1343 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1344 }
1345 }
1346 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
1347 __ b(ne, fail_label);
1348 __ cmp(r0, Operand(Smi::FromInt(range)));
1349 __ b(ge, fail_label);
1350 __ add(pc, pc, Operand(r0, LSL, 2 - kSmiTagSize));
1351 // One extra instruction offsets the table, so the table's start address is
1352 // the pc-register at the above add.
1353 __ stop("Unreachable: Switch table alignment");
1354
ager@chromium.org870a0b62008-11-04 11:43:05 +00001355 // Table containing branch operations.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001356 for (int i = 0; i < range; i++) {
1357 __ b(case_targets[i]);
1358 }
1359
1360 GenerateFastCaseSwitchCases(node, case_labels);
1361}
1362
1363
ager@chromium.org7c537e22008-10-16 08:43:32 +00001364void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 Comment cmnt(masm_, "[ SwitchStatement");
1366 if (FLAG_debug_info) RecordStatementPosition(node);
1367 node->set_break_stack_height(break_stack_height_);
1368
1369 Load(node->tag());
1370
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001371 if (TryGenerateFastCaseSwitchStatement(node)) {
1372 return;
1373 }
1374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 Label next, fall_through, default_case;
1376 ZoneList<CaseClause*>* cases = node->cases();
1377 int length = cases->length();
1378
1379 for (int i = 0; i < length; i++) {
1380 CaseClause* clause = cases->at(i);
1381
1382 Comment cmnt(masm_, "[ case clause");
1383
1384 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001385 // Continue matching cases. The program will execute the default case's
1386 // statements if it does not match any of the cases.
1387 __ b(&next);
1388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 // Bind the default case label, so we can branch to it when we
1390 // have compared against all other cases.
1391 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001392 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 } else {
1394 __ bind(&next);
1395 next.Unuse();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001396 __ ldr(r0, frame_->Top());
1397 frame_->Push(r0); // duplicate TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398 Load(clause->label());
1399 Comparison(eq, true);
1400 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 }
1402
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001403 // Entering the case statement for the first time. Remove the switch value
1404 // from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001405 frame_->Pop();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001406
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001408 // This is also the target for the fall through from the previous case's
1409 // statements which has to skip over the matching code and the popping of
1410 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 __ bind(&fall_through);
1412 fall_through.Unuse();
1413 VisitStatements(clause->statements());
1414 __ b(&fall_through);
1415 }
1416
1417 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001418 // Reached the end of the case statements without matching any of the cases.
1419 if (default_case.is_bound()) {
1420 // A default case exists -> execute its statements.
1421 __ b(&default_case);
1422 } else {
1423 // Remove the switch value from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001424 frame_->Pop();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001425 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426
1427 __ bind(&fall_through);
1428 __ bind(node->break_target());
1429}
1430
1431
ager@chromium.org7c537e22008-10-16 08:43:32 +00001432void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 Comment cmnt(masm_, "[ LoopStatement");
1434 if (FLAG_debug_info) RecordStatementPosition(node);
1435 node->set_break_stack_height(break_stack_height_);
1436
1437 // simple condition analysis
1438 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1439 if (node->cond() == NULL) {
1440 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1441 info = ALWAYS_TRUE;
1442 } else {
1443 Literal* lit = node->cond()->AsLiteral();
1444 if (lit != NULL) {
1445 if (lit->IsTrue()) {
1446 info = ALWAYS_TRUE;
1447 } else if (lit->IsFalse()) {
1448 info = ALWAYS_FALSE;
1449 }
1450 }
1451 }
1452
1453 Label loop, entry;
1454
1455 // init
1456 if (node->init() != NULL) {
1457 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1458 Visit(node->init());
1459 }
1460 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
1461 __ b(&entry);
1462 }
1463
1464 // body
1465 __ bind(&loop);
1466 Visit(node->body());
1467
1468 // next
1469 __ bind(node->continue_target());
1470 if (node->next() != NULL) {
1471 // Record source position of the statement as this code which is after the
1472 // code for the body actually belongs to the loop statement and not the
1473 // body.
1474 if (FLAG_debug_info) __ RecordPosition(node->statement_pos());
1475 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1476 Visit(node->next());
1477 }
1478
1479 // cond
1480 __ bind(&entry);
1481 switch (info) {
1482 case ALWAYS_TRUE:
1483 CheckStack(); // TODO(1222600): ignore if body contains calls.
1484 __ b(&loop);
1485 break;
1486 case ALWAYS_FALSE:
1487 break;
1488 case DONT_KNOW:
1489 CheckStack(); // TODO(1222600): ignore if body contains calls.
1490 LoadCondition(node->cond(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00001491 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 &loop,
1493 node->break_target(),
1494 true);
1495 Branch(true, &loop);
1496 break;
1497 }
1498
1499 // exit
1500 __ bind(node->break_target());
1501}
1502
1503
ager@chromium.org7c537e22008-10-16 08:43:32 +00001504void CodeGenerator::VisitForInStatement(ForInStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 Comment cmnt(masm_, "[ ForInStatement");
1506 if (FLAG_debug_info) RecordStatementPosition(node);
1507
1508 // We keep stuff on the stack while the body is executing.
1509 // Record it, so that a break/continue crossing this statement
1510 // can restore the stack.
1511 const int kForInStackSize = 5 * kPointerSize;
1512 break_stack_height_ += kForInStackSize;
1513 node->set_break_stack_height(break_stack_height_);
1514
1515 Label loop, next, entry, cleanup, exit, primitive, jsobject;
1516 Label filter_key, end_del_check, fixed_array, non_string;
1517
1518 // Get the object to enumerate over (converted to JSObject).
1519 Load(node->enumerable());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001520 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521
1522 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1523 // to the specification. 12.6.4 mandates a call to ToObject.
1524 __ cmp(r0, Operand(Factory::undefined_value()));
1525 __ b(eq, &exit);
1526 __ cmp(r0, Operand(Factory::null_value()));
1527 __ b(eq, &exit);
1528
1529 // Stack layout in body:
1530 // [iteration counter (Smi)]
1531 // [length of array]
1532 // [FixedArray]
1533 // [Map or 0]
1534 // [Object]
1535
1536 // Check if enumerable is already a JSObject
1537 __ tst(r0, Operand(kSmiTagMask));
1538 __ b(eq, &primitive);
1539 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1540 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001541 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 __ b(hs, &jsobject);
1543
1544 __ bind(&primitive);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001545 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001546 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001547 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
1549
1550 __ bind(&jsobject);
1551
1552 // Get the set of properties (as a FixedArray or Map).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001553 frame_->Push(r0); // duplicate the object being enumerated
1554 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1556
1557 // If we got a Map, we can do a fast modification check.
1558 // Otherwise, we got a FixedArray, and we have to do a slow check.
1559 __ mov(r2, Operand(r0));
1560 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1561 __ cmp(r1, Operand(Factory::meta_map()));
1562 __ b(ne, &fixed_array);
1563
1564 // Get enum cache
1565 __ mov(r1, Operand(r0));
1566 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1567 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1568 __ ldr(r2,
1569 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1570
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001571 frame_->Push(r0); // map
1572 frame_->Push(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001573 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001575 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001576 __ mov(r0, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001577 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 __ b(&entry);
1579
1580
1581 __ bind(&fixed_array);
1582
1583 __ mov(r1, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001584 frame_->Push(r1); // insert 0 in place of Map
1585 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586
1587 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001588 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001590 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001591 __ mov(r0, Operand(Smi::FromInt(0))); // init index
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001592 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001593
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594 __ b(&entry);
1595
1596 // Body.
1597 __ bind(&loop);
1598 Visit(node->body());
1599
1600 // Next.
1601 __ bind(node->continue_target());
1602 __ bind(&next);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001603 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 __ add(r0, r0, Operand(Smi::FromInt(1)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001605 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606
1607 // Condition.
1608 __ bind(&entry);
1609
mads.s.ager31e71382008-08-13 09:32:07 +00001610 // sp[0] : index
1611 // sp[1] : array/enum cache length
1612 // sp[2] : array or enum cache
1613 // sp[3] : 0 or map
1614 // sp[4] : enumerable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001615 __ ldr(r0, frame_->Element(0)); // load the current count
1616 __ ldr(r1, frame_->Element(1)); // load the length
mads.s.ager31e71382008-08-13 09:32:07 +00001617 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 __ b(hs, &cleanup);
1619
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001620 __ ldr(r0, frame_->Element(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 // Get the i'th entry of the array.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001623 __ ldr(r2, frame_->Element(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1625 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1626
1627 // Get Map or 0.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001628 __ ldr(r2, frame_->Element(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 // Check if this (still) matches the map of the enumerable.
1630 // If not, we have to filter the key.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001631 __ ldr(r1, frame_->Element(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1633 __ cmp(r1, Operand(r2));
1634 __ b(eq, &end_del_check);
1635
1636 // Convert the entry to a string (or null if it isn't a property anymore).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001637 __ ldr(r0, frame_->Element(4)); // push enumerable
1638 frame_->Push(r0);
1639 frame_->Push(r3); // push entry
mads.s.ager31e71382008-08-13 09:32:07 +00001640 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001641 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643
1644 // If the property has been removed while iterating, we just skip it.
1645 __ cmp(r3, Operand(Factory::null_value()));
1646 __ b(eq, &next);
1647
1648
1649 __ bind(&end_del_check);
1650
1651 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00001652 // r3: i'th entry of the enum cache (or string there of)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001653 frame_->Push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 { Reference each(this, node->each());
1655 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001656 if (each.size() > 0) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001657 __ ldr(r0, frame_->Element(each.size()));
1658 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001659 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001660 // If the reference was to a slot we rely on the convenient property
1661 // that it doesn't matter whether a value (eg, r3 pushed above) is
1662 // right on top of or right underneath a zero-sized reference.
1663 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001664 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001665 // It's safe to pop the value lying on top of the reference before
1666 // unloading the reference itself (which preserves the top of stack,
1667 // ie, now the topmost value of the non-zero sized reference), since
1668 // we will discard the top of stack after unloading the reference
1669 // anyway.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001670 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001671 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 }
1673 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001674 // Discard the i'th entry pushed above or else the remainder of the
1675 // reference, whichever is currently on top of the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001676 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 CheckStack(); // TODO(1222600): ignore if body contains calls.
1678 __ jmp(&loop);
1679
1680 // Cleanup.
1681 __ bind(&cleanup);
1682 __ bind(node->break_target());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001683 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684
1685 // Exit.
1686 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687
1688 break_stack_height_ -= kForInStackSize;
1689}
1690
1691
ager@chromium.org7c537e22008-10-16 08:43:32 +00001692void CodeGenerator::VisitTryCatch(TryCatch* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 Comment cmnt(masm_, "[ TryCatch");
1694
1695 Label try_block, exit;
1696
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 __ bl(&try_block);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // --- Catch block ---
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001699 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
1701 // Store the caught exception in the catch variable.
1702 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001703 ASSERT(ref.is_slot());
1704 // Here we make use of the convenient property that it doesn't matter
1705 // whether a value is immediately on top of or underneath a zero-sized
1706 // reference.
1707 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 }
1709
1710 // Remove the exception from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001711 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712
1713 VisitStatements(node->catch_block()->statements());
1714 __ b(&exit);
1715
1716
1717 // --- Try block ---
1718 __ bind(&try_block);
1719
1720 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1721
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001722 // Shadow the labels for all escapes from the try block, including
1723 // returns. During shadowing, the original label is hidden as the
1724 // LabelShadow and operations on the original actually affect the
1725 // shadowing label.
1726 //
1727 // We should probably try to unify the escaping labels and the return
1728 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 int nof_escapes = node->escaping_labels()->length();
1730 List<LabelShadow*> shadows(1 + nof_escapes);
1731 shadows.Add(new LabelShadow(&function_return_));
1732 for (int i = 0; i < nof_escapes; i++) {
1733 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1734 }
1735
1736 // Generate code for the statements in the try block.
1737 VisitStatements(node->try_block()->statements());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001738 frame_->Pop(); // Discard the result.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739
1740 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001741 // After shadowing stops, the original labels are unshadowed and the
1742 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 int nof_unlinks = 0;
1744 for (int i = 0; i <= nof_escapes; i++) {
1745 shadows[i]->StopShadowing();
1746 if (shadows[i]->is_linked()) nof_unlinks++;
1747 }
1748
1749 // Unlink from try chain.
1750 // TOS contains code slot
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001751 const int kNextIndex = (StackHandlerConstants::kNextOffset
1752 + StackHandlerConstants::kAddressDisplacement)
1753 / kPointerSize;
1754 __ ldr(r1, frame_->Element(kNextIndex)); // read next_sp
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1756 __ str(r1, MemOperand(r3));
1757 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001758 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 // Code slot popped.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 if (nof_unlinks > 0) __ b(&exit);
1761
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001762 // Generate unlink code for the (formerly) shadowing labels that have been
1763 // jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 for (int i = 0; i <= nof_escapes; i++) {
1765 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001766 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 __ bind(shadows[i]);
1768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 // Reload sp from the top handler, because some statements that we
1770 // break from (eg, for...in) may have left stuff on the stack.
1771 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1772 __ ldr(sp, MemOperand(r3));
1773
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001774 __ ldr(r1, frame_->Element(kNextIndex));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 __ str(r1, MemOperand(r3));
1776 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001777 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 // Code slot popped.
1779
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001780 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 }
1782 }
1783
1784 __ bind(&exit);
1785}
1786
1787
ager@chromium.org7c537e22008-10-16 08:43:32 +00001788void CodeGenerator::VisitTryFinally(TryFinally* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 Comment cmnt(masm_, "[ TryFinally");
1790
1791 // State: Used to keep track of reason for entering the finally
1792 // block. Should probably be extended to hold information for
1793 // break/continue from within the try block.
1794 enum { FALLING, THROWING, JUMPING };
1795
1796 Label exit, unlink, try_block, finally_block;
1797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 __ bl(&try_block);
1799
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001800 frame_->Push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 // In case of thrown exceptions, this is where we continue.
1802 __ mov(r2, Operand(Smi::FromInt(THROWING)));
1803 __ b(&finally_block);
1804
1805
1806 // --- Try block ---
1807 __ bind(&try_block);
1808
1809 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1810
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001811 // Shadow the labels for all escapes from the try block, including
1812 // returns. Shadowing hides the original label as the LabelShadow and
1813 // operations on the original actually affect the shadowing label.
1814 //
1815 // We should probably try to unify the escaping labels and the return
1816 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 int nof_escapes = node->escaping_labels()->length();
1818 List<LabelShadow*> shadows(1 + nof_escapes);
1819 shadows.Add(new LabelShadow(&function_return_));
1820 for (int i = 0; i < nof_escapes; i++) {
1821 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1822 }
1823
1824 // Generate code for the statements in the try block.
1825 VisitStatements(node->try_block()->statements());
1826
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001827 // Stop the introduced shadowing and count the number of required unlinks.
1828 // After shadowing stops, the original labels are unshadowed and the
1829 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 int nof_unlinks = 0;
1831 for (int i = 0; i <= nof_escapes; i++) {
1832 shadows[i]->StopShadowing();
1833 if (shadows[i]->is_linked()) nof_unlinks++;
1834 }
1835
1836 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00001837 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001838 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 __ mov(r2, Operand(Smi::FromInt(FALLING)));
1840 if (nof_unlinks > 0) __ b(&unlink);
1841
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001842 // Generate code to set the state for the (formerly) shadowing labels that
1843 // have been jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 for (int i = 0; i <= nof_escapes; i++) {
1845 if (shadows[i]->is_linked()) {
1846 __ bind(shadows[i]);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001847 if (shadows[i]->original_label() == &function_return_) {
1848 // If this label shadowed the function return, materialize the
1849 // return value on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001850 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001851 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001852 // Fake TOS for labels that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00001853 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001854 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 }
1856 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
1857 __ b(&unlink);
1858 }
1859 }
1860
mads.s.ager31e71382008-08-13 09:32:07 +00001861 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 __ bind(&unlink);
1863
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001864 frame_->Pop(r0); // Store TOS in r0 across stack manipulation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 // Reload sp from the top handler, because some statements that we
1866 // break from (eg, for...in) may have left stuff on the stack.
1867 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1868 __ ldr(sp, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001869 const int kNextIndex = (StackHandlerConstants::kNextOffset
1870 + StackHandlerConstants::kAddressDisplacement)
1871 / kPointerSize;
1872 __ ldr(r1, frame_->Element(kNextIndex));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 __ str(r1, MemOperand(r3));
1874 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001875 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 // Code slot popped.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001877 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878
1879 // --- Finally block ---
1880 __ bind(&finally_block);
1881
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001882 // Push the state on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001883 frame_->Push(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001884
1885 // We keep two elements on the stack - the (possibly faked) result
1886 // and the state - while evaluating the finally block. Record it, so
1887 // that a break/continue crossing this statement can restore the
1888 // stack.
1889 const int kFinallyStackSize = 2 * kPointerSize;
1890 break_stack_height_ += kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891
1892 // Generate code for the statements in the finally block.
1893 VisitStatements(node->finally_block()->statements());
1894
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001895 // Restore state and return value or faked TOS.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001896 frame_->Pop(r2);
1897 frame_->Pop(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001898 break_stack_height_ -= kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001900 // Generate code to jump to the right destination for all used (formerly)
1901 // shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 for (int i = 0; i <= nof_escapes; i++) {
1903 if (shadows[i]->is_bound()) {
1904 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001905 if (shadows[i]->original_label() != &function_return_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 Label next;
1907 __ b(ne, &next);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001908 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 __ bind(&next);
1910 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001911 __ b(eq, shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912 }
1913 }
1914 }
1915
1916 // Check if we need to rethrow the exception.
1917 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
1918 __ b(ne, &exit);
1919
1920 // Rethrow exception.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001921 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 __ CallRuntime(Runtime::kReThrow, 1);
1923
1924 // Done.
1925 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926}
1927
1928
ager@chromium.org7c537e22008-10-16 08:43:32 +00001929void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 Comment cmnt(masm_, "[ DebuggerStatament");
1931 if (FLAG_debug_info) RecordStatementPosition(node);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001932 __ CallRuntime(Runtime::kDebugBreak, 0);
1933 // Ignore the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934}
1935
1936
ager@chromium.org7c537e22008-10-16 08:43:32 +00001937void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 ASSERT(boilerplate->IsBoilerplate());
1939
1940 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001941 __ mov(r0, Operand(boilerplate));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001942 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943
1944 // Create a new closure.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001945 frame_->Push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 __ CallRuntime(Runtime::kNewClosure, 2);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001947 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948}
1949
1950
ager@chromium.org7c537e22008-10-16 08:43:32 +00001951void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 Comment cmnt(masm_, "[ FunctionLiteral");
1953
1954 // Build the function boilerplate and instantiate it.
1955 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00001956 // Check for stack-overflow exception.
1957 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 InstantiateBoilerplate(boilerplate);
1959}
1960
1961
ager@chromium.org7c537e22008-10-16 08:43:32 +00001962void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 FunctionBoilerplateLiteral* node) {
1964 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
1965 InstantiateBoilerplate(node->boilerplate());
1966}
1967
1968
ager@chromium.org7c537e22008-10-16 08:43:32 +00001969void CodeGenerator::VisitConditional(Conditional* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 Comment cmnt(masm_, "[ Conditional");
1971 Label then, else_, exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001972 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 Branch(false, &else_);
1974 __ bind(&then);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001975 Load(node->then_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 __ b(&exit);
1977 __ bind(&else_);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001978 Load(node->else_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 __ bind(&exit);
1980}
1981
1982
ager@chromium.org7c537e22008-10-16 08:43:32 +00001983void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
1984 if (slot->type() == Slot::LOOKUP) {
1985 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986
1987 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001988 frame_->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001989 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001990 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991
ager@chromium.org7c537e22008-10-16 08:43:32 +00001992 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001993 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001994 } else {
1995 __ CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001997 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998
1999 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002000 // Note: We would like to keep the assert below, but it fires because of
2001 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org7c537e22008-10-16 08:43:32 +00002002 // ASSERT(slot->var()->mode() != Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002004 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002005 __ ldr(r0, SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002006 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002007 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002008 // Const slots may contain 'the hole' value (the constant hasn't been
2009 // initialized yet) which needs to be converted into the 'undefined'
2010 // value.
2011 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002012 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002013 __ cmp(r0, Operand(Factory::the_hole_value()));
2014 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002015 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 }
2017 }
2018}
2019
2020
ager@chromium.org7c537e22008-10-16 08:43:32 +00002021void CodeGenerator::VisitSlot(Slot* node) {
2022 Comment cmnt(masm_, "[ Slot");
2023 LoadFromSlot(node, typeof_state());
2024}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025
ager@chromium.org7c537e22008-10-16 08:43:32 +00002026
2027void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
2028 Comment cmnt(masm_, "[ VariableProxy");
2029
2030 Variable* var = node->var();
2031 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002032 if (expr != NULL) {
2033 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002035 ASSERT(var->is_global());
2036 Reference ref(this, node);
2037 ref.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 }
2039}
2040
2041
ager@chromium.org7c537e22008-10-16 08:43:32 +00002042void CodeGenerator::VisitLiteral(Literal* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002044 __ mov(r0, Operand(node->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002045 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046}
2047
2048
ager@chromium.org7c537e22008-10-16 08:43:32 +00002049void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 Comment cmnt(masm_, "[ RexExp Literal");
2051
2052 // Retrieve the literal array and check the allocated entry.
2053
2054 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002055 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056
2057 // Load the literals array of the function.
2058 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2059
2060 // Load the literal at the ast saved index.
2061 int literal_offset =
2062 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2063 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2064
2065 Label done;
2066 __ cmp(r2, Operand(Factory::undefined_value()));
2067 __ b(ne, &done);
2068
2069 // If the entry is undefined we call the runtime system to computed
2070 // the literal.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002071 frame_->Push(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002072 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002073 frame_->Push(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002074 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002075 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002076 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002077 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002079 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080
mads.s.ager31e71382008-08-13 09:32:07 +00002081 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082 // Push the literal.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002083 frame_->Push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084}
2085
2086
2087// This deferred code stub will be used for creating the boilerplate
2088// by calling Runtime_CreateObjectLiteral.
2089// Each created boilerplate is stored in the JSFunction and they are
2090// therefore context dependent.
2091class ObjectLiteralDeferred: public DeferredCode {
2092 public:
2093 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
2094 : DeferredCode(generator), node_(node) {
2095 set_comment("[ ObjectLiteralDeferred");
2096 }
2097 virtual void Generate();
2098 private:
2099 ObjectLiteral* node_;
2100};
2101
2102
2103void ObjectLiteralDeferred::Generate() {
2104 // If the entry is undefined we call the runtime system to computed
2105 // the literal.
2106
2107 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00002108 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002110 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2111 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002113 __ mov(r0, Operand(node_->constant_properties()));
2114 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002116 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117}
2118
2119
ager@chromium.org7c537e22008-10-16 08:43:32 +00002120void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 Comment cmnt(masm_, "[ ObjectLiteral");
2122
2123 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
2124
2125 // Retrieve the literal array and check the allocated entry.
2126
2127 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002128 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129
2130 // Load the literals array of the function.
2131 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2132
2133 // Load the literal at the ast saved index.
2134 int literal_offset =
2135 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2136 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2137
2138 // Check whether we need to materialize the object literal boilerplate.
2139 // If so, jump to the deferred code.
2140 __ cmp(r2, Operand(Factory::undefined_value()));
2141 __ b(eq, deferred->enter());
2142 __ bind(deferred->exit());
2143
2144 // Push the object literal boilerplate.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002145 frame_->Push(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 // Clone the boilerplate object.
2148 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002149 frame_->Push(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002150 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151
2152 for (int i = 0; i < node->properties()->length(); i++) {
2153 ObjectLiteral::Property* property = node->properties()->at(i);
2154 Literal* key = property->key();
2155 Expression* value = property->value();
2156 switch (property->kind()) {
2157 case ObjectLiteral::Property::CONSTANT: break;
2158 case ObjectLiteral::Property::COMPUTED: // fall through
2159 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002160 frame_->Push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 Load(key);
2162 Load(value);
2163 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002164 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002165 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 break;
2167 }
2168 case ObjectLiteral::Property::SETTER: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002169 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002171 __ mov(r0, Operand(Smi::FromInt(1)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002172 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 Load(value);
2174 __ CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002175 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 break;
2177 }
2178 case ObjectLiteral::Property::GETTER: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002179 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002181 __ mov(r0, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002182 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 Load(value);
2184 __ CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002185 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 break;
2187 }
2188 }
2189 }
2190}
2191
2192
ager@chromium.org7c537e22008-10-16 08:43:32 +00002193void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002195
2196 // Call runtime to create the array literal.
2197 __ mov(r0, Operand(node->literals()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002198 frame_->Push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002199 // Load the function of this frame.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002200 __ ldr(r0, frame_->Function());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002201 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002202 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002203 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
2204
2205 // Push the resulting array literal on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002206 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002207
2208 // Generate code to set the elements in the array that are not
2209 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 for (int i = 0; i < node->values()->length(); i++) {
2211 Expression* value = node->values()->at(i);
2212
2213 // If value is literal the property value is already
2214 // set in the boilerplate object.
2215 if (value->AsLiteral() == NULL) {
2216 // The property must be set by generated code.
2217 Load(value);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002218 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219
2220 // Fetch the object literal
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002221 __ ldr(r1, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 // Get the elements array.
2223 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
2224
2225 // Write to the indexed properties array.
2226 int offset = i * kPointerSize + Array::kHeaderSize;
2227 __ str(r0, FieldMemOperand(r1, offset));
2228
2229 // Update the write barrier for the array address.
2230 __ mov(r3, Operand(offset));
2231 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 }
2233 }
2234}
2235
2236
ager@chromium.org7c537e22008-10-16 08:43:32 +00002237void CodeGenerator::VisitAssignment(Assignment* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 if (FLAG_debug_info) RecordStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 Reference target(this, node->target());
2242 if (target.is_illegal()) return;
2243
2244 if (node->op() == Token::ASSIGN ||
2245 node->op() == Token::INIT_VAR ||
2246 node->op() == Token::INIT_CONST) {
2247 Load(node->value());
2248
2249 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002250 target.GetValue(NOT_INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 Literal* literal = node->value()->AsLiteral();
2252 if (literal != NULL && literal->handle()->IsSmi()) {
2253 SmiOperation(node->binary_op(), literal->handle(), false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002254 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 } else {
2257 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00002258 GenericBinaryOperation(node->binary_op());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002259 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 }
2261 }
2262
2263 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2264 if (var != NULL &&
2265 (var->mode() == Variable::CONST) &&
2266 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2267 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 } else {
2270 __ RecordPosition(node->position());
2271 if (node->op() == Token::INIT_CONST) {
2272 // Dynamic constant initializations must use the function context
2273 // and initialize the actual constant declared. Dynamic variable
2274 // initializations are simply assignments and use SetValue.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002275 target.SetValue(CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002277 target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278 }
2279 }
2280}
2281
2282
ager@chromium.org7c537e22008-10-16 08:43:32 +00002283void CodeGenerator::VisitThrow(Throw* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 Comment cmnt(masm_, "[ Throw");
2285
2286 Load(node->exception());
2287 __ RecordPosition(node->position());
2288 __ CallRuntime(Runtime::kThrow, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002289 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290}
2291
2292
ager@chromium.org7c537e22008-10-16 08:43:32 +00002293void CodeGenerator::VisitProperty(Property* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002295
ager@chromium.org7c537e22008-10-16 08:43:32 +00002296 Reference property(this, node);
2297 property.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298}
2299
2300
ager@chromium.org7c537e22008-10-16 08:43:32 +00002301void CodeGenerator::VisitCall(Call* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 Comment cmnt(masm_, "[ Call");
2303
2304 ZoneList<Expression*>* args = node->arguments();
2305
2306 if (FLAG_debug_info) RecordStatementPosition(node);
2307 // Standard function call.
2308
2309 // Check if the function is a variable or a property.
2310 Expression* function = node->expression();
2311 Variable* var = function->AsVariableProxy()->AsVariable();
2312 Property* property = function->AsProperty();
2313
2314 // ------------------------------------------------------------------------
2315 // Fast-case: Use inline caching.
2316 // ---
2317 // According to ECMA-262, section 11.2.3, page 44, the function to call
2318 // must be resolved after the arguments have been evaluated. The IC code
2319 // automatically handles this by loading the arguments before the function
2320 // is resolved in cache misses (this also holds for megamorphic calls).
2321 // ------------------------------------------------------------------------
2322
2323 if (var != NULL && !var->is_this() && var->is_global()) {
2324 // ----------------------------------
2325 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2326 // ----------------------------------
2327
2328 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002329 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002330 frame_->Push(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002331
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002332 // Pass the global object as the receiver and let the IC stub
2333 // patch the stack to use the global proxy as 'this' in the
2334 // invoked function.
2335 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336
2337 // Load the arguments.
2338 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339
2340 // Setup the receiver register and call the IC initialization code.
2341 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002343 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002344 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002346 frame_->Pop();
2347 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348
2349 } else if (var != NULL && var->slot() != NULL &&
2350 var->slot()->type() == Slot::LOOKUP) {
2351 // ----------------------------------
2352 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2353 // ----------------------------------
2354
2355 // Load the function
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002356 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002357 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002358 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2360 // r0: slot value; r1: receiver
2361
2362 // Load the receiver.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002363 frame_->Push(r0); // function
2364 frame_->Push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365
2366 // Call the function.
2367 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002368 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369
2370 } else if (property != NULL) {
2371 // Check if the key is a literal string.
2372 Literal* literal = property->key()->AsLiteral();
2373
2374 if (literal != NULL && literal->handle()->IsSymbol()) {
2375 // ------------------------------------------------------------------
2376 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2377 // ------------------------------------------------------------------
2378
2379 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002380 __ mov(r0, Operand(literal->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002381 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 Load(property->obj());
2383
2384 // Load the arguments.
2385 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386
2387 // Set the receiver register and call the IC initialization code.
2388 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002390 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002391 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392
2393 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002394 frame_->Pop();
mads.s.ager31e71382008-08-13 09:32:07 +00002395
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002396 frame_->Push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397
2398 } else {
2399 // -------------------------------------------
2400 // JavaScript example: 'array[index](1, 2, 3)'
2401 // -------------------------------------------
2402
2403 // Load the function to call from the property through a reference.
2404 Reference ref(this, property);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002405 ref.GetValue(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406
2407 // Pass receiver to called function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002408 __ ldr(r0, frame_->Element(ref.size()));
2409 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 // Call the function.
2411 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002412 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 }
2414
2415 } else {
2416 // ----------------------------------
2417 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
2418 // ----------------------------------
2419
2420 // Load the function.
2421 Load(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002422
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002423 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002424 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002425
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 // Call the function.
2427 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002428 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 }
2430}
2431
2432
ager@chromium.org7c537e22008-10-16 08:43:32 +00002433void CodeGenerator::VisitCallNew(CallNew* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434 Comment cmnt(masm_, "[ CallNew");
2435
2436 // According to ECMA-262, section 11.2.2, page 44, the function
2437 // expression in new calls must be evaluated before the
2438 // arguments. This is different from ordinary calls, where the
2439 // actual function to call is resolved after the arguments have been
2440 // evaluated.
2441
2442 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002443 // receiver. There is no need to use the global proxy here because
2444 // it will always be replaced with a newly allocated object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002446 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447
2448 // Push the arguments ("left-to-right") on the stack.
2449 ZoneList<Expression*>* args = node->arguments();
2450 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2451
mads.s.ager31e71382008-08-13 09:32:07 +00002452 // r0: the number of arguments.
2453 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002455 // Load the function into r1 as per calling convention.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002456 __ ldr(r1, frame_->Element(args->length() + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 // Call the construct call builtin that handles allocation and
2459 // constructor invocation.
ager@chromium.org236ad962008-09-25 09:45:57 +00002460 __ RecordPosition(RelocInfo::POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00002462 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00002463
2464 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002465 __ str(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466}
2467
2468
ager@chromium.org7c537e22008-10-16 08:43:32 +00002469void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 ASSERT(args->length() == 1);
2471 Label leave;
2472 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002473 frame_->Pop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00002474 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 __ tst(r0, Operand(kSmiTagMask));
2476 __ b(eq, &leave);
2477 // It is a heap object - get map.
2478 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2479 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002480 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 __ cmp(r1, Operand(JS_VALUE_TYPE));
2482 __ b(ne, &leave);
2483 // Load the value.
2484 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2485 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002486 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487}
2488
2489
ager@chromium.org7c537e22008-10-16 08:43:32 +00002490void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 ASSERT(args->length() == 2);
2492 Label leave;
2493 Load(args->at(0)); // Load the object.
2494 Load(args->at(1)); // Load the value.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002495 frame_->Pop(r0); // r0 contains value
2496 frame_->Pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 // if (object->IsSmi()) return object.
2498 __ tst(r1, Operand(kSmiTagMask));
2499 __ b(eq, &leave);
2500 // It is a heap object - get map.
2501 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2502 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2503 // if (!object->IsJSValue()) return object.
2504 __ cmp(r2, Operand(JS_VALUE_TYPE));
2505 __ b(ne, &leave);
2506 // Store the value.
2507 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2508 // Update the write barrier.
2509 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2510 __ RecordWrite(r1, r2, r3);
2511 // Leave.
2512 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002513 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002514}
2515
2516
ager@chromium.org7c537e22008-10-16 08:43:32 +00002517void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518 ASSERT(args->length() == 1);
2519 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002520 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002521 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 cc_reg_ = eq;
2523}
2524
2525
ager@chromium.org7c537e22008-10-16 08:43:32 +00002526void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002527 ASSERT(args->length() == 1);
2528 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002529 frame_->Pop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002530 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2531 cc_reg_ = eq;
2532}
2533
2534
kasper.lund7276f142008-07-30 08:49:36 +00002535// This should generate code that performs a charCodeAt() call or returns
2536// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2537// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002538void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00002539 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002540 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002541 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002542}
2543
2544
ager@chromium.org7c537e22008-10-16 08:43:32 +00002545void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002546 ASSERT(args->length() == 1);
2547 Load(args->at(0));
2548 Label answer;
2549 // We need the CC bits to come out as not_equal in the case where the
2550 // object is a smi. This can't be done with the usual test opcode so
2551 // we use XOR to get the right CC bits.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002552 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002553 __ and_(r1, r0, Operand(kSmiTagMask));
2554 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2555 __ b(ne, &answer);
2556 // It is a heap object - get the map.
2557 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2558 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2559 // Check if the object is a JS array or not.
2560 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2561 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 cc_reg_ = eq;
2563}
2564
2565
ager@chromium.org7c537e22008-10-16 08:43:32 +00002566void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 ASSERT(args->length() == 0);
2568
mads.s.ager31e71382008-08-13 09:32:07 +00002569 // Seed the result with the formal parameters count, which will be used
2570 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2572
2573 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002574 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002576 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577}
2578
2579
ager@chromium.org7c537e22008-10-16 08:43:32 +00002580void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 ASSERT(args->length() == 1);
2582
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002583 // Satisfy contract with ArgumentsAccessStub:
2584 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002585 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002586 frame_->Pop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002587 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588
2589 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002590 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002592 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593}
2594
2595
ager@chromium.org7c537e22008-10-16 08:43:32 +00002596void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002597 ASSERT(args->length() == 2);
2598
2599 // Load the two objects into registers and perform the comparison.
2600 Load(args->at(0));
2601 Load(args->at(1));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002602 frame_->Pop(r0);
2603 frame_->Pop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002604 __ cmp(r0, Operand(r1));
2605 cc_reg_ = eq;
2606}
2607
2608
ager@chromium.org7c537e22008-10-16 08:43:32 +00002609void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002610 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611
2612 ZoneList<Expression*>* args = node->arguments();
2613 Comment cmnt(masm_, "[ CallRuntime");
2614 Runtime::Function* function = node->function();
2615
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002617 // Push the arguments ("left-to-right").
2618 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620 // Call the C runtime function.
2621 __ CallRuntime(function, args->length());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002622 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002623
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002625 // Prepare stack for calling JS runtime function.
2626 __ mov(r0, Operand(node->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002627 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002628 // Push the builtins object found in the current global object.
2629 __ ldr(r1, GlobalObject());
2630 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002631 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002632
2633 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002637 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002638 __ ldr(cp, frame_->Context());
2639 frame_->Pop();
2640 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 }
2642}
2643
2644
ager@chromium.org7c537e22008-10-16 08:43:32 +00002645void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646 Comment cmnt(masm_, "[ UnaryOperation");
2647
2648 Token::Value op = node->op();
2649
2650 if (op == Token::NOT) {
2651 LoadCondition(node->expression(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002652 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 false_target(),
2654 true_target(),
2655 true);
2656 cc_reg_ = NegateCondition(cc_reg_);
2657
2658 } else if (op == Token::DELETE) {
2659 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002660 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 if (property != NULL) {
2662 Load(property->obj());
2663 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002664 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002665 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666
mads.s.ager31e71382008-08-13 09:32:07 +00002667 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 Slot* slot = variable->slot();
2669 if (variable->is_global()) {
2670 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00002671 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002672 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002673 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002674 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675
2676 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2677 // lookup the context holding the named variable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002678 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002679 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002680 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 __ CallRuntime(Runtime::kLookupContext, 2);
2682 // r0: context
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002683 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002684 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002685 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002686 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002687 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688
mads.s.ager31e71382008-08-13 09:32:07 +00002689 } else {
2690 // Default: Result of deleting non-global, not dynamically
2691 // introduced variables is false.
2692 __ mov(r0, Operand(Factory::false_value()));
2693 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694
2695 } else {
2696 // Default: Result of deleting expressions is true.
2697 Load(node->expression()); // may have side-effects
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002698 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 __ mov(r0, Operand(Factory::true_value()));
2700 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002701 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702
2703 } else if (op == Token::TYPEOF) {
2704 // Special case for loading the typeof expression; see comment on
2705 // LoadTypeofExpression().
2706 LoadTypeofExpression(node->expression());
2707 __ CallRuntime(Runtime::kTypeof, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002708 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709
2710 } else {
2711 Load(node->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002712 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 switch (op) {
2714 case Token::NOT:
2715 case Token::DELETE:
2716 case Token::TYPEOF:
2717 UNREACHABLE(); // handled above
2718 break;
2719
2720 case Token::SUB: {
2721 UnarySubStub stub;
2722 __ CallStub(&stub);
2723 break;
2724 }
2725
2726 case Token::BIT_NOT: {
2727 // smi check
2728 Label smi_label;
2729 Label continue_label;
2730 __ tst(r0, Operand(kSmiTagMask));
2731 __ b(eq, &smi_label);
2732
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002733 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002734 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002735 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002736
2737 __ b(&continue_label);
2738 __ bind(&smi_label);
2739 __ mvn(r0, Operand(r0));
2740 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
2741 __ bind(&continue_label);
2742 break;
2743 }
2744
2745 case Token::VOID:
2746 // since the stack top is cached in r0, popping and then
2747 // pushing a value can be done by just writing to r0.
2748 __ mov(r0, Operand(Factory::undefined_value()));
2749 break;
2750
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002751 case Token::ADD: {
2752 // Smi check.
2753 Label continue_label;
2754 __ tst(r0, Operand(kSmiTagMask));
2755 __ b(eq, &continue_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002756 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002757 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002758 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002759 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002761 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 default:
2763 UNREACHABLE();
2764 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002765 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 }
2767}
2768
2769
ager@chromium.org7c537e22008-10-16 08:43:32 +00002770void CodeGenerator::VisitCountOperation(CountOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 Comment cmnt(masm_, "[ CountOperation");
2772
2773 bool is_postfix = node->is_postfix();
2774 bool is_increment = node->op() == Token::INC;
2775
2776 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
2777 bool is_const = (var != NULL && var->mode() == Variable::CONST);
2778
2779 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00002780 if (is_postfix) {
2781 __ mov(r0, Operand(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002782 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002783 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784
2785 { Reference target(this, node->expression());
2786 if (target.is_illegal()) return;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002787 target.GetValue(NOT_INSIDE_TYPEOF);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002788 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789
2790 Label slow, exit;
2791
2792 // Load the value (1) into register r1.
2793 __ mov(r1, Operand(Smi::FromInt(1)));
2794
2795 // Check for smi operand.
2796 __ tst(r0, Operand(kSmiTagMask));
2797 __ b(ne, &slow);
2798
2799 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002800 if (is_postfix) {
2801 __ str(r0, frame_->Element(target.size()));
2802 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803
2804 // Perform optimistic increment/decrement.
2805 if (is_increment) {
2806 __ add(r0, r0, Operand(r1), SetCC);
2807 } else {
2808 __ sub(r0, r0, Operand(r1), SetCC);
2809 }
2810
2811 // If the increment/decrement didn't overflow, we're done.
2812 __ b(vc, &exit);
2813
2814 // Revert optimistic increment/decrement.
2815 if (is_increment) {
2816 __ sub(r0, r0, Operand(r1));
2817 } else {
2818 __ add(r0, r0, Operand(r1));
2819 }
2820
2821 // Slow case: Convert to number.
2822 __ bind(&slow);
2823
2824 // Postfix: Convert the operand to a number and store it as the result.
2825 if (is_postfix) {
2826 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
2827 __ CallStub(&stub);
2828 // Store to result (on the stack).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002829 __ str(r0, frame_->Element(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830 }
2831
2832 // Compute the new value by calling the right JavaScript native.
2833 if (is_increment) {
2834 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
2835 __ CallStub(&stub);
2836 } else {
2837 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
2838 __ CallStub(&stub);
2839 }
2840
2841 // Store the new value in the target if not const.
2842 __ bind(&exit);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002843 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002844 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845 }
2846
2847 // Postfix: Discard the new value and use the old.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002848 if (is_postfix) frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849}
2850
2851
ager@chromium.org7c537e22008-10-16 08:43:32 +00002852void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002853 Comment cmnt(masm_, "[ BinaryOperation");
2854 Token::Value op = node->op();
2855
2856 // According to ECMA-262 section 11.11, page 58, the binary logical
2857 // operators must yield the result of one of the two expressions
2858 // before any ToBoolean() conversions. This means that the value
2859 // produced by a && or || operator is not necessarily a boolean.
2860
2861 // NOTE: If the left hand side produces a materialized value (not in
2862 // the CC register), we force the right hand side to do the
2863 // same. This is necessary because we may have to branch to the exit
2864 // after evaluating the left hand side (due to the shortcut
2865 // semantics), but the compiler must (statically) know if the result
2866 // of compiling the binary operation is materialized or not.
2867
2868 if (op == Token::AND) {
2869 Label is_true;
2870 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002871 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872 &is_true,
2873 false_target(),
2874 false);
2875 if (has_cc()) {
2876 Branch(false, false_target());
2877
2878 // Evaluate right side expression.
2879 __ bind(&is_true);
2880 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002881 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882 true_target(),
2883 false_target(),
2884 false);
2885
2886 } else {
2887 Label pop_and_continue, exit;
2888
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002889 __ ldr(r0, frame_->Top()); // dup the stack top
2890 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891 // Avoid popping the result if it converts to 'false' using the
2892 // standard ToBoolean() conversion as described in ECMA-262,
2893 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002894 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895 Branch(false, &exit);
2896
2897 // Pop the result of evaluating the first part.
2898 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002899 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900
2901 // Evaluate right side expression.
2902 __ bind(&is_true);
2903 Load(node->right());
2904
2905 // Exit (always with a materialized value).
2906 __ bind(&exit);
2907 }
2908
2909 } else if (op == Token::OR) {
2910 Label is_false;
2911 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002912 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002913 true_target(),
2914 &is_false,
2915 false);
2916 if (has_cc()) {
2917 Branch(true, true_target());
2918
2919 // Evaluate right side expression.
2920 __ bind(&is_false);
2921 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002922 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923 true_target(),
2924 false_target(),
2925 false);
2926
2927 } else {
2928 Label pop_and_continue, exit;
2929
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002930 __ ldr(r0, frame_->Top());
2931 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932 // Avoid popping the result if it converts to 'true' using the
2933 // standard ToBoolean() conversion as described in ECMA-262,
2934 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002935 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936 Branch(true, &exit);
2937
2938 // Pop the result of evaluating the first part.
2939 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002940 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941
2942 // Evaluate right side expression.
2943 __ bind(&is_false);
2944 Load(node->right());
2945
2946 // Exit (always with a materialized value).
2947 __ bind(&exit);
2948 }
2949
2950 } else {
2951 // Optimize for the case where (at least) one of the expressions
2952 // is a literal small integer.
2953 Literal* lliteral = node->left()->AsLiteral();
2954 Literal* rliteral = node->right()->AsLiteral();
2955
2956 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
2957 Load(node->left());
2958 SmiOperation(node->op(), rliteral->handle(), false);
2959
2960 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
2961 Load(node->right());
2962 SmiOperation(node->op(), lliteral->handle(), true);
2963
2964 } else {
2965 Load(node->left());
2966 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00002967 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002969 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 }
2971}
2972
2973
ager@chromium.org7c537e22008-10-16 08:43:32 +00002974void CodeGenerator::VisitThisFunction(ThisFunction* node) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002975 __ ldr(r0, frame_->Function());
2976 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977}
2978
2979
ager@chromium.org7c537e22008-10-16 08:43:32 +00002980void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981 Comment cmnt(masm_, "[ CompareOperation");
2982
2983 // Get the expressions from the node.
2984 Expression* left = node->left();
2985 Expression* right = node->right();
2986 Token::Value op = node->op();
2987
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002988 // To make null checks efficient, we check if either left or right is the
2989 // literal 'null'. If so, we optimize the code by inlining a null check
2990 // instead of calling the (very) general runtime routine for checking
2991 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002993 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002994 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002995 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002996 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
2997 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002998 if (left_is_null || right_is_null) {
2999 Load(left_is_null ? right : left);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003000 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003001 __ cmp(r0, Operand(Factory::null_value()));
3002
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003003 // The 'null' value is only equal to 'undefined' if using non-strict
3004 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005 if (op != Token::EQ_STRICT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003006 __ b(eq, true_target());
3007
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 __ cmp(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003009 __ b(eq, true_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003012 __ b(eq, false_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003013
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003014 // It can be an undetectable object.
3015 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3016 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3017 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3018 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003019 }
3020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 cc_reg_ = eq;
3022 return;
3023 }
3024 }
3025
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003026 // To make typeof testing for natives implemented in JavaScript really
3027 // efficient, we generate special code for expressions of the form:
3028 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029 UnaryOperation* operation = left->AsUnaryOperation();
3030 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3031 (operation != NULL && operation->op() == Token::TYPEOF) &&
3032 (right->AsLiteral() != NULL &&
3033 right->AsLiteral()->handle()->IsString())) {
3034 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3035
mads.s.ager31e71382008-08-13 09:32:07 +00003036 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 LoadTypeofExpression(operation->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003038 frame_->Pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039
3040 if (check->Equals(Heap::number_symbol())) {
3041 __ tst(r1, Operand(kSmiTagMask));
3042 __ b(eq, true_target());
3043 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3044 __ cmp(r1, Operand(Factory::heap_number_map()));
3045 cc_reg_ = eq;
3046
3047 } else if (check->Equals(Heap::string_symbol())) {
3048 __ tst(r1, Operand(kSmiTagMask));
3049 __ b(eq, false_target());
3050
3051 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3052
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003053 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3055 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3056 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3057 __ b(eq, false_target());
3058
3059 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3060 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3061 cc_reg_ = lt;
3062
3063 } else if (check->Equals(Heap::boolean_symbol())) {
3064 __ cmp(r1, Operand(Factory::true_value()));
3065 __ b(eq, true_target());
3066 __ cmp(r1, Operand(Factory::false_value()));
3067 cc_reg_ = eq;
3068
3069 } else if (check->Equals(Heap::undefined_symbol())) {
3070 __ cmp(r1, Operand(Factory::undefined_value()));
3071 __ b(eq, true_target());
3072
3073 __ tst(r1, Operand(kSmiTagMask));
3074 __ b(eq, false_target());
3075
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003076 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3078 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3079 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3080 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3081
3082 cc_reg_ = eq;
3083
3084 } else if (check->Equals(Heap::function_symbol())) {
3085 __ tst(r1, Operand(kSmiTagMask));
3086 __ b(eq, false_target());
3087 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3088 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3089 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3090 cc_reg_ = eq;
3091
3092 } else if (check->Equals(Heap::object_symbol())) {
3093 __ tst(r1, Operand(kSmiTagMask));
3094 __ b(eq, false_target());
3095
3096 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3097 __ cmp(r1, Operand(Factory::null_value()));
3098 __ b(eq, true_target());
3099
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003100 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3102 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3103 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3104 __ b(eq, false_target());
3105
3106 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3107 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3108 __ b(lt, false_target());
3109 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3110 cc_reg_ = le;
3111
3112 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003113 // Uncommon case: typeof testing against a string literal that is
3114 // never returned from the typeof operator.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115 __ b(false_target());
3116 }
3117 return;
3118 }
3119
3120 Load(left);
3121 Load(right);
3122 switch (op) {
3123 case Token::EQ:
3124 Comparison(eq, false);
3125 break;
3126
3127 case Token::LT:
3128 Comparison(lt);
3129 break;
3130
3131 case Token::GT:
3132 Comparison(gt);
3133 break;
3134
3135 case Token::LTE:
3136 Comparison(le);
3137 break;
3138
3139 case Token::GTE:
3140 Comparison(ge);
3141 break;
3142
3143 case Token::EQ_STRICT:
3144 Comparison(eq, true);
3145 break;
3146
3147 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003148 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003149 __ InvokeBuiltin(Builtins::IN, CALL_JS);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003150 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151 break;
3152
3153 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003154 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003155 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003156 __ tst(r0, Operand(r0));
3157 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003158 break;
3159
3160 default:
3161 UNREACHABLE();
3162 }
3163}
3164
3165
ager@chromium.org7c537e22008-10-16 08:43:32 +00003166void CodeGenerator::RecordStatementPosition(Node* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167 if (FLAG_debug_info) {
3168 int statement_pos = node->statement_pos();
ager@chromium.org236ad962008-09-25 09:45:57 +00003169 if (statement_pos == RelocInfo::kNoPosition) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 __ RecordStatementPosition(statement_pos);
3171 }
3172}
3173
3174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003176#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003177
ager@chromium.org7c537e22008-10-16 08:43:32 +00003178Handle<String> Reference::GetName() {
3179 ASSERT(type_ == NAMED);
3180 Property* property = expression_->AsProperty();
3181 if (property == NULL) {
3182 // Global variable reference treated as a named property reference.
3183 VariableProxy* proxy = expression_->AsVariableProxy();
3184 ASSERT(proxy->AsVariable() != NULL);
3185 ASSERT(proxy->AsVariable()->is_global());
3186 return proxy->name();
3187 } else {
3188 Literal* raw_name = property->key()->AsLiteral();
3189 ASSERT(raw_name != NULL);
3190 return Handle<String>(String::cast(*raw_name->handle()));
3191 }
3192}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003193
ager@chromium.org7c537e22008-10-16 08:43:32 +00003194
3195void Reference::GetValue(TypeofState typeof_state) {
3196 ASSERT(!is_illegal());
3197 ASSERT(!cgen_->has_cc());
3198 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003199 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003200 Property* property = expression_->AsProperty();
3201 if (property != NULL) {
3202 __ RecordPosition(property->position());
3203 }
3204
3205 switch (type_) {
3206 case SLOT: {
3207 Comment cmnt(masm, "[ Load from Slot");
3208 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3209 ASSERT(slot != NULL);
3210 cgen_->LoadFromSlot(slot, typeof_state);
3211 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003212 }
3213
ager@chromium.org7c537e22008-10-16 08:43:32 +00003214 case NAMED: {
3215 // TODO(1241834): Make sure that this it is safe to ignore the
3216 // distinction between expressions in a typeof and not in a typeof. If
3217 // there is a chance that reference errors can be thrown below, we
3218 // must distinguish between the two kinds of loads (typeof expression
3219 // loads must not throw a reference error).
3220 Comment cmnt(masm, "[ Load from named Property");
3221 // Setup the name register.
3222 Handle<String> name(GetName());
3223 __ mov(r2, Operand(name));
3224 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3225
3226 Variable* var = expression_->AsVariableProxy()->AsVariable();
3227 if (var != NULL) {
3228 ASSERT(var->is_global());
3229 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3230 } else {
3231 __ Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003232 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003233 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003234 break;
3235 }
3236
3237 case KEYED: {
3238 // TODO(1241834): Make sure that this it is safe to ignore the
3239 // distinction between expressions in a typeof and not in a typeof.
3240 Comment cmnt(masm, "[ Load from keyed Property");
3241 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003242 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
3243
3244 Variable* var = expression_->AsVariableProxy()->AsVariable();
3245 if (var != NULL) {
3246 ASSERT(var->is_global());
3247 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3248 } else {
3249 __ Call(ic, RelocInfo::CODE_TARGET);
3250 }
3251 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003252 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003253 }
3254
3255 default:
3256 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003257 }
3258}
3259
3260
ager@chromium.org7c537e22008-10-16 08:43:32 +00003261void Reference::SetValue(InitState init_state) {
3262 ASSERT(!is_illegal());
3263 ASSERT(!cgen_->has_cc());
3264 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003265 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003266 Property* property = expression_->AsProperty();
3267 if (property != NULL) {
3268 __ RecordPosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003269 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003270
ager@chromium.org7c537e22008-10-16 08:43:32 +00003271 switch (type_) {
3272 case SLOT: {
3273 Comment cmnt(masm, "[ Store to Slot");
3274 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3275 ASSERT(slot != NULL);
3276 if (slot->type() == Slot::LOOKUP) {
3277 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003278
ager@chromium.org7c537e22008-10-16 08:43:32 +00003279 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003280 frame->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003281 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003282 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003283
ager@chromium.org7c537e22008-10-16 08:43:32 +00003284 if (init_state == CONST_INIT) {
3285 // Same as the case for a normal store, but ignores attribute
3286 // (e.g. READ_ONLY) of context slot so that we can initialize
3287 // const properties (introduced via eval("const foo = (some
3288 // expr);")). Also, uses the current function context instead of
3289 // the top context.
3290 //
3291 // Note that we must declare the foo upon entry of eval(), via a
3292 // context slot declaration, but we cannot initialize it at the
3293 // same time, because the const declaration may be at the end of
3294 // the eval code (sigh...) and the const variable may have been
3295 // used before (where its value is 'undefined'). Thus, we can only
3296 // do the initialization when we actually encounter the expression
3297 // and when the expression operands are defined and valid, and
3298 // thus we need the split into 2 operations: declaration of the
3299 // context slot followed by initialization.
3300 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3301 } else {
3302 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3303 }
3304 // Storing a variable must keep the (new) value on the expression
3305 // stack. This is necessary for compiling assignment expressions.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003306 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003307
ager@chromium.org7c537e22008-10-16 08:43:32 +00003308 } else {
3309 ASSERT(slot->var()->mode() != Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003310
ager@chromium.org7c537e22008-10-16 08:43:32 +00003311 Label exit;
3312 if (init_state == CONST_INIT) {
3313 ASSERT(slot->var()->mode() == Variable::CONST);
3314 // Only the first const initialization must be executed (the slot
3315 // still contains 'the hole' value). When the assignment is
3316 // executed, the code is identical to a normal store (see below).
3317 Comment cmnt(masm, "[ Init const");
3318 __ ldr(r2, cgen_->SlotOperand(slot, r2));
3319 __ cmp(r2, Operand(Factory::the_hole_value()));
3320 __ b(ne, &exit);
3321 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003322
ager@chromium.org7c537e22008-10-16 08:43:32 +00003323 // We must execute the store. Storing a variable must keep the
3324 // (new) value on the stack. This is necessary for compiling
3325 // assignment expressions.
3326 //
3327 // Note: We will reach here even with slot->var()->mode() ==
3328 // Variable::CONST because of const declarations which will
3329 // initialize consts to 'the hole' value and by doing so, end up
3330 // calling this code. r2 may be loaded with context; used below in
3331 // RecordWrite.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003332 frame->Pop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003333 __ str(r0, cgen_->SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003334 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003335 if (slot->type() == Slot::CONTEXT) {
3336 // Skip write barrier if the written value is a smi.
3337 __ tst(r0, Operand(kSmiTagMask));
3338 __ b(eq, &exit);
3339 // r2 is loaded with context when calling SlotOperand above.
3340 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3341 __ mov(r3, Operand(offset));
3342 __ RecordWrite(r2, r3, r1);
3343 }
3344 // If we definitely did not jump over the assignment, we do not need
3345 // to bind the exit label. Doing so can defeat peephole
3346 // optimization.
3347 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3348 __ bind(&exit);
3349 }
3350 }
3351 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003352 }
3353
ager@chromium.org7c537e22008-10-16 08:43:32 +00003354 case NAMED: {
3355 Comment cmnt(masm, "[ Store to named Property");
3356 // Call the appropriate IC code.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003357 frame->Pop(r0); // value
ager@chromium.org7c537e22008-10-16 08:43:32 +00003358 // Setup the name register.
3359 Handle<String> name(GetName());
3360 __ mov(r2, Operand(name));
3361 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3362 __ Call(ic, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003363 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003364 break;
3365 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003366
ager@chromium.org7c537e22008-10-16 08:43:32 +00003367 case KEYED: {
3368 Comment cmnt(masm, "[ Store to keyed Property");
3369 Property* property = expression_->AsProperty();
3370 ASSERT(property != NULL);
3371 __ RecordPosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003372
3373 // Call IC code.
3374 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3375 // TODO(1222589): Make the IC grab the values from the stack.
3376 frame->Pop(r0); // value
3377 __ Call(ic, RelocInfo::CODE_TARGET);
3378 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003379 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003380 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003381
3382 default:
3383 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003384 }
3385}
3386
3387
3388void GetPropertyStub::Generate(MacroAssembler* masm) {
3389 // sp[0]: key
3390 // sp[1]: receiver
3391 Label slow, fast;
3392 // Get the key and receiver object from the stack.
3393 __ ldm(ia, sp, r0.bit() | r1.bit());
3394 // Check that the key is a smi.
3395 __ tst(r0, Operand(kSmiTagMask));
3396 __ b(ne, &slow);
3397 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3398 // Check that the object isn't a smi.
3399 __ tst(r1, Operand(kSmiTagMask));
3400 __ b(eq, &slow);
3401
3402 // Check that the object is some kind of JS object EXCEPT JS Value type.
3403 // In the case that the object is a value-wrapper object,
3404 // we enter the runtime system to make sure that indexing into string
3405 // objects work as intended.
3406 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3407 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3408 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3409 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3410 __ b(lt, &slow);
3411
3412 // Get the elements array of the object.
3413 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3414 // Check that the object is in fast mode (not dictionary).
3415 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3416 __ cmp(r3, Operand(Factory::hash_table_map()));
3417 __ b(eq, &slow);
3418 // Check that the key (index) is within bounds.
3419 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3420 __ cmp(r0, Operand(r3));
3421 __ b(lo, &fast);
3422
3423 // Slow case: Push extra copies of the arguments (2).
3424 __ bind(&slow);
3425 __ ldm(ia, sp, r0.bit() | r1.bit());
3426 __ stm(db_w, sp, r0.bit() | r1.bit());
3427 // Do tail-call to runtime routine.
3428 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3429
3430 // Fast case: Do the load.
3431 __ bind(&fast);
3432 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3433 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3434 __ cmp(r0, Operand(Factory::the_hole_value()));
3435 // In case the loaded value is the_hole we have to consult GetProperty
3436 // to ensure the prototype chain is searched.
3437 __ b(eq, &slow);
3438
3439 __ StubReturn(1);
3440}
3441
3442
3443void SetPropertyStub::Generate(MacroAssembler* masm) {
3444 // r0 : value
3445 // sp[0] : key
3446 // sp[1] : receiver
3447
3448 Label slow, fast, array, extra, exit;
3449 // Get the key and the object from the stack.
3450 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3451 // Check that the key is a smi.
3452 __ tst(r1, Operand(kSmiTagMask));
3453 __ b(ne, &slow);
3454 // Check that the object isn't a smi.
3455 __ tst(r3, Operand(kSmiTagMask));
3456 __ b(eq, &slow);
3457 // Get the type of the object from its map.
3458 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3459 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3460 // Check if the object is a JS array or not.
3461 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3462 __ b(eq, &array);
3463 // Check that the object is some kind of JS object.
3464 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3465 __ b(lt, &slow);
3466
3467
3468 // Object case: Check key against length in the elements array.
3469 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3470 // Check that the object is in fast mode (not dictionary).
3471 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3472 __ cmp(r2, Operand(Factory::hash_table_map()));
3473 __ b(eq, &slow);
3474 // Untag the key (for checking against untagged length in the fixed array).
3475 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3476 // Compute address to store into and check array bounds.
3477 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3478 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3479 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3480 __ cmp(r1, Operand(ip));
3481 __ b(lo, &fast);
3482
3483
3484 // Slow case: Push extra copies of the arguments (3).
3485 __ bind(&slow);
3486 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3487 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3488 // Do tail-call to runtime routine.
3489 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3490
3491
3492 // Extra capacity case: Check if there is extra capacity to
3493 // perform the store and update the length. Used for adding one
3494 // element to the array by writing to array[array.length].
3495 // r0 == value, r1 == key, r2 == elements, r3 == object
3496 __ bind(&extra);
3497 __ b(ne, &slow); // do not leave holes in the array
3498 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3499 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3500 __ cmp(r1, Operand(ip));
3501 __ b(hs, &slow);
3502 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3503 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3504 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3505 __ mov(r3, Operand(r2));
3506 // NOTE: Computing the address to store into must take the fact
3507 // that the key has been incremented into account.
3508 int displacement = Array::kHeaderSize - kHeapObjectTag -
3509 ((1 << kSmiTagSize) * 2);
3510 __ add(r2, r2, Operand(displacement));
3511 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3512 __ b(&fast);
3513
3514
3515 // Array case: Get the length and the elements array from the JS
3516 // array. Check that the array is in fast mode; if it is the
3517 // length is always a smi.
3518 // r0 == value, r3 == object
3519 __ bind(&array);
3520 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3521 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3522 __ cmp(r1, Operand(Factory::hash_table_map()));
3523 __ b(eq, &slow);
3524
3525 // Check the key against the length in the array, compute the
3526 // address to store into and fall through to fast case.
3527 __ ldr(r1, MemOperand(sp));
3528 // r0 == value, r1 == key, r2 == elements, r3 == object.
3529 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3530 __ cmp(r1, Operand(ip));
3531 __ b(hs, &extra);
3532 __ mov(r3, Operand(r2));
3533 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3534 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3535
3536
3537 // Fast case: Do the store.
3538 // r0 == value, r2 == address to store into, r3 == elements
3539 __ bind(&fast);
3540 __ str(r0, MemOperand(r2));
3541 // Skip write barrier if the written value is a smi.
3542 __ tst(r0, Operand(kSmiTagMask));
3543 __ b(eq, &exit);
3544 // Update write barrier for the elements array address.
3545 __ sub(r1, r2, Operand(r3));
3546 __ RecordWrite(r3, r1, r2);
3547 __ bind(&exit);
3548 __ StubReturn(1);
3549}
3550
3551
3552void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3553 // r1 : x
3554 // r0 : y
3555 // result : r0
3556
3557 switch (op_) {
3558 case Token::ADD: {
3559 Label slow, exit;
3560 // fast path
3561 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3562 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3563 // go slow-path in case of overflow
3564 __ b(vs, &slow);
3565 // go slow-path in case of non-smi operands
3566 ASSERT(kSmiTag == 0); // adjust code below
3567 __ tst(r2, Operand(kSmiTagMask));
3568 __ b(eq, &exit);
3569 // slow path
3570 __ bind(&slow);
3571 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3572 __ push(r1);
3573 __ push(r0);
3574 __ mov(r0, Operand(1)); // set number of arguments
3575 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3576 // done
3577 __ bind(&exit);
3578 break;
3579 }
3580
3581 case Token::SUB: {
3582 Label slow, exit;
3583 // fast path
3584 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3585 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3586 // go slow-path in case of overflow
3587 __ b(vs, &slow);
3588 // go slow-path in case of non-smi operands
3589 ASSERT(kSmiTag == 0); // adjust code below
3590 __ tst(r2, Operand(kSmiTagMask));
3591 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3592 __ b(eq, &exit);
3593 // slow path
3594 __ bind(&slow);
3595 __ push(r1);
3596 __ push(r0);
3597 __ mov(r0, Operand(1)); // set number of arguments
3598 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3599 // done
3600 __ bind(&exit);
3601 break;
3602 }
3603
3604 case Token::MUL: {
3605 Label slow, exit;
3606 // tag check
3607 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3608 ASSERT(kSmiTag == 0); // adjust code below
3609 __ tst(r2, Operand(kSmiTagMask));
3610 __ b(ne, &slow);
3611 // remove tag from one operand (but keep sign), so that result is smi
3612 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3613 // do multiplication
3614 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3615 // go slow on overflows (overflow bit is not set)
3616 __ mov(ip, Operand(r3, ASR, 31));
3617 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3618 __ b(ne, &slow);
3619 // go slow on zero result to handle -0
3620 __ tst(r3, Operand(r3));
3621 __ mov(r0, Operand(r3), LeaveCC, ne);
3622 __ b(ne, &exit);
3623 // slow case
3624 __ bind(&slow);
3625 __ push(r1);
3626 __ push(r0);
3627 __ mov(r0, Operand(1)); // set number of arguments
3628 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3629 // done
3630 __ bind(&exit);
3631 break;
3632 }
3633
3634 case Token::BIT_OR:
3635 case Token::BIT_AND:
3636 case Token::BIT_XOR: {
3637 Label slow, exit;
3638 // tag check
3639 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3640 ASSERT(kSmiTag == 0); // adjust code below
3641 __ tst(r2, Operand(kSmiTagMask));
3642 __ b(ne, &slow);
3643 switch (op_) {
3644 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3645 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3646 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3647 default: UNREACHABLE();
3648 }
3649 __ b(&exit);
3650 __ bind(&slow);
3651 __ push(r1); // restore stack
3652 __ push(r0);
3653 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3654 switch (op_) {
3655 case Token::BIT_OR:
3656 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3657 break;
3658 case Token::BIT_AND:
3659 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3660 break;
3661 case Token::BIT_XOR:
3662 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3663 break;
3664 default:
3665 UNREACHABLE();
3666 }
3667 __ bind(&exit);
3668 break;
3669 }
3670
3671 case Token::SHL:
3672 case Token::SHR:
3673 case Token::SAR: {
3674 Label slow, exit;
3675 // tag check
3676 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3677 ASSERT(kSmiTag == 0); // adjust code below
3678 __ tst(r2, Operand(kSmiTagMask));
3679 __ b(ne, &slow);
3680 // remove tags from operands (but keep sign)
3681 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3682 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3683 // use only the 5 least significant bits of the shift count
3684 __ and_(r2, r2, Operand(0x1f));
3685 // perform operation
3686 switch (op_) {
3687 case Token::SAR:
3688 __ mov(r3, Operand(r3, ASR, r2));
3689 // no checks of result necessary
3690 break;
3691
3692 case Token::SHR:
3693 __ mov(r3, Operand(r3, LSR, r2));
3694 // check that the *unsigned* result fits in a smi
3695 // neither of the two high-order bits can be set:
3696 // - 0x80000000: high bit would be lost when smi tagging
3697 // - 0x40000000: this number would convert to negative when
3698 // smi tagging these two cases can only happen with shifts
3699 // by 0 or 1 when handed a valid smi
3700 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3701 __ b(ne, &slow);
3702 break;
3703
3704 case Token::SHL:
3705 __ mov(r3, Operand(r3, LSL, r2));
3706 // check that the *signed* result fits in a smi
3707 __ add(r2, r3, Operand(0x40000000), SetCC);
3708 __ b(mi, &slow);
3709 break;
3710
3711 default: UNREACHABLE();
3712 }
3713 // tag result and store it in r0
3714 ASSERT(kSmiTag == 0); // adjust code below
3715 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3716 __ b(&exit);
3717 // slow case
3718 __ bind(&slow);
3719 __ push(r1); // restore stack
3720 __ push(r0);
3721 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3722 switch (op_) {
3723 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3724 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3725 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3726 default: UNREACHABLE();
3727 }
3728 __ bind(&exit);
3729 break;
3730 }
3731
3732 default: UNREACHABLE();
3733 }
3734 __ Ret();
3735}
3736
3737
3738void StackCheckStub::Generate(MacroAssembler* masm) {
3739 Label within_limit;
3740 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3741 __ ldr(ip, MemOperand(ip));
3742 __ cmp(sp, Operand(ip));
3743 __ b(hs, &within_limit);
3744 // Do tail-call to runtime routine.
3745 __ push(r0);
3746 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
3747 __ bind(&within_limit);
3748
3749 __ StubReturn(1);
3750}
3751
3752
3753void UnarySubStub::Generate(MacroAssembler* masm) {
3754 Label undo;
3755 Label slow;
3756 Label done;
3757
3758 // Enter runtime system if the value is not a smi.
3759 __ tst(r0, Operand(kSmiTagMask));
3760 __ b(ne, &slow);
3761
3762 // Enter runtime system if the value of the expression is zero
3763 // to make sure that we switch between 0 and -0.
3764 __ cmp(r0, Operand(0));
3765 __ b(eq, &slow);
3766
3767 // The value of the expression is a smi that is not zero. Try
3768 // optimistic subtraction '0 - value'.
3769 __ rsb(r1, r0, Operand(0), SetCC);
3770 __ b(vs, &slow);
3771
3772 // If result is a smi we are done.
3773 __ tst(r1, Operand(kSmiTagMask));
3774 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
3775 __ b(eq, &done);
3776
3777 // Enter runtime system.
3778 __ bind(&slow);
3779 __ push(r0);
3780 __ mov(r0, Operand(0)); // set number of arguments
3781 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3782
3783 __ bind(&done);
3784 __ StubReturn(1);
3785}
3786
3787
3788void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
3789 __ push(r0);
3790 __ mov(r0, Operand(0)); // set number of arguments
3791 switch (kind_) {
3792 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
3793 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
3794 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
3795 default: UNREACHABLE();
3796 }
3797 __ StubReturn(argc_);
3798}
3799
3800
3801void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3802 // r0 holds exception
3803 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
3804 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3805 __ ldr(sp, MemOperand(r3));
3806 __ pop(r2); // pop next in chain
3807 __ str(r2, MemOperand(r3));
3808 // restore parameter- and frame-pointer and pop state.
3809 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
3810 // Before returning we restore the context from the frame pointer if not NULL.
3811 // The frame pointer is NULL in the exception handler of a JS entry frame.
3812 __ cmp(fp, Operand(0));
3813 // Set cp to NULL if fp is NULL.
3814 __ mov(cp, Operand(0), LeaveCC, eq);
3815 // Restore cp otherwise.
3816 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3817 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3818 __ pop(pc);
3819}
3820
3821
3822void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
3823 // Fetch top stack handler.
3824 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3825 __ ldr(r3, MemOperand(r3));
3826
3827 // Unwind the handlers until the ENTRY handler is found.
3828 Label loop, done;
3829 __ bind(&loop);
3830 // Load the type of the current stack handler.
3831 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
3832 StackHandlerConstants::kStateOffset;
3833 __ ldr(r2, MemOperand(r3, kStateOffset));
3834 __ cmp(r2, Operand(StackHandler::ENTRY));
3835 __ b(eq, &done);
3836 // Fetch the next handler in the list.
3837 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
3838 StackHandlerConstants::kNextOffset;
3839 __ ldr(r3, MemOperand(r3, kNextOffset));
3840 __ jmp(&loop);
3841 __ bind(&done);
3842
3843 // Set the top handler address to next handler past the current ENTRY handler.
3844 __ ldr(r0, MemOperand(r3, kNextOffset));
3845 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
3846 __ str(r0, MemOperand(r2));
3847
3848 // Set external caught exception to false.
3849 __ mov(r0, Operand(false));
3850 ExternalReference external_caught(Top::k_external_caught_exception_address);
3851 __ mov(r2, Operand(external_caught));
3852 __ str(r0, MemOperand(r2));
3853
3854 // Set pending exception and r0 to out of memory exception.
3855 Failure* out_of_memory = Failure::OutOfMemoryException();
3856 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3857 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
3858 __ str(r0, MemOperand(r2));
3859
3860 // Restore the stack to the address of the ENTRY handler
3861 __ mov(sp, Operand(r3));
3862
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003863 // Stack layout at this point. See also PushTryHandler
3864 // r3, sp -> next handler
3865 // state (ENTRY)
3866 // pp
3867 // fp
3868 // lr
3869
3870 // Discard ENTRY state (r2 is not used), and restore parameter-
3871 // and frame-pointer and pop state.
3872 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003873 // Before returning we restore the context from the frame pointer if not NULL.
3874 // The frame pointer is NULL in the exception handler of a JS entry frame.
3875 __ cmp(fp, Operand(0));
3876 // Set cp to NULL if fp is NULL.
3877 __ mov(cp, Operand(0), LeaveCC, eq);
3878 // Restore cp otherwise.
3879 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3880 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3881 __ pop(pc);
3882}
3883
3884
3885void CEntryStub::GenerateCore(MacroAssembler* masm,
3886 Label* throw_normal_exception,
3887 Label* throw_out_of_memory_exception,
3888 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003889 bool do_gc,
3890 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003891 // r0: result parameter for PerformGC, if any
3892 // r4: number of arguments including receiver (C callee-saved)
3893 // r5: pointer to builtin function (C callee-saved)
3894 // r6: pointer to the first argument (C callee-saved)
3895
3896 if (do_gc) {
3897 // Passing r0.
3898 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3899 }
3900
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003901 ExternalReference scope_depth =
3902 ExternalReference::heap_always_allocate_scope_depth();
3903 if (always_allocate) {
3904 __ mov(r0, Operand(scope_depth));
3905 __ ldr(r1, MemOperand(r0));
3906 __ add(r1, r1, Operand(1));
3907 __ str(r1, MemOperand(r0));
3908 }
3909
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003910 // Call C built-in.
3911 // r0 = argc, r1 = argv
3912 __ mov(r0, Operand(r4));
3913 __ mov(r1, Operand(r6));
3914
3915 // TODO(1242173): To let the GC traverse the return address of the exit
3916 // frames, we need to know where the return address is. Right now,
3917 // we push it on the stack to be able to find it again, but we never
3918 // restore from it in case of changes, which makes it impossible to
3919 // support moving the C entry code stub. This should be fixed, but currently
3920 // this is OK because the CEntryStub gets generated so early in the V8 boot
3921 // sequence that it is not moving ever.
3922 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
3923 __ push(lr);
3924#if !defined(__arm__)
3925 // Notify the simulator of the transition to C code.
3926 __ swi(assembler::arm::call_rt_r5);
3927#else /* !defined(__arm__) */
3928 __ mov(pc, Operand(r5));
3929#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003930
3931 if (always_allocate) {
3932 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3933 // though (contain the result).
3934 __ mov(r2, Operand(scope_depth));
3935 __ ldr(r3, MemOperand(r2));
3936 __ sub(r3, r3, Operand(1));
3937 __ str(r3, MemOperand(r2));
3938 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003939
3940 // check for failure result
3941 Label failure_returned;
3942 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3943 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
3944 __ add(r2, r0, Operand(1));
3945 __ tst(r2, Operand(kFailureTagMask));
3946 __ b(eq, &failure_returned);
3947
3948 // Exit C frame and return.
3949 // r0:r1: result
3950 // sp: stack pointer
3951 // fp: frame pointer
3952 // pp: caller's parameter pointer pp (restored as C callee-saved)
3953 __ LeaveExitFrame(frame_type);
3954
3955 // check if we should retry or throw exception
3956 Label retry;
3957 __ bind(&failure_returned);
3958 ASSERT(Failure::RETRY_AFTER_GC == 0);
3959 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3960 __ b(eq, &retry);
3961
3962 Label continue_exception;
3963 // If the returned failure is EXCEPTION then promote Top::pending_exception().
3964 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3965 __ b(ne, &continue_exception);
3966
3967 // Retrieve the pending exception and clear the variable.
3968 __ mov(ip, Operand(Factory::the_hole_value().location()));
3969 __ ldr(r3, MemOperand(ip));
3970 __ mov(ip, Operand(Top::pending_exception_address()));
3971 __ ldr(r0, MemOperand(ip));
3972 __ str(r3, MemOperand(ip));
3973
3974 __ bind(&continue_exception);
3975 // Special handling of out of memory exception.
3976 Failure* out_of_memory = Failure::OutOfMemoryException();
3977 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3978 __ b(eq, throw_out_of_memory_exception);
3979
3980 // Handle normal exception.
3981 __ jmp(throw_normal_exception);
3982
3983 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
3984}
3985
3986
3987void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
3988 // Called from JavaScript; parameters are on stack as if calling JS function
3989 // r0: number of arguments including receiver
3990 // r1: pointer to builtin function
3991 // fp: frame pointer (restored after C call)
3992 // sp: stack pointer (restored as callee's pp after C call)
3993 // cp: current context (C callee-saved)
3994 // pp: caller's parameter pointer pp (C callee-saved)
3995
3996 // NOTE: Invocations of builtins may return failure objects
3997 // instead of a proper result. The builtin entry handles
3998 // this by performing a garbage collection and retrying the
3999 // builtin once.
4000
4001 StackFrame::Type frame_type = is_debug_break
4002 ? StackFrame::EXIT_DEBUG
4003 : StackFrame::EXIT;
4004
4005 // Enter the exit frame that transitions from JavaScript to C++.
4006 __ EnterExitFrame(frame_type);
4007
4008 // r4: number of arguments (C callee-saved)
4009 // r5: pointer to builtin function (C callee-saved)
4010 // r6: pointer to first argument (C callee-saved)
4011
4012 Label throw_out_of_memory_exception;
4013 Label throw_normal_exception;
4014
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004015 // Call into the runtime system. Collect garbage before the call if
4016 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004017 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004018 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004019 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4020 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004021 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004022 &throw_out_of_memory_exception,
4023 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004024 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004025 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004026
4027 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004028 GenerateCore(masm,
4029 &throw_normal_exception,
4030 &throw_out_of_memory_exception,
4031 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004032 true,
4033 false);
4034
4035 // Do full GC and retry runtime call one final time.
4036 Failure* failure = Failure::InternalError();
4037 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4038 GenerateCore(masm,
4039 &throw_normal_exception,
4040 &throw_out_of_memory_exception,
4041 frame_type,
4042 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004043 true);
4044
4045 __ bind(&throw_out_of_memory_exception);
4046 GenerateThrowOutOfMemory(masm);
4047 // control flow for generated will not return.
4048
4049 __ bind(&throw_normal_exception);
4050 GenerateThrowTOS(masm);
4051}
4052
4053
4054void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4055 // r0: code entry
4056 // r1: function
4057 // r2: receiver
4058 // r3: argc
4059 // [sp+0]: argv
4060
4061 Label invoke, exit;
4062
4063 // Called from C, so do not pop argc and args on exit (preserve sp)
4064 // No need to save register-passed args
4065 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4066 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4067
4068 // Get address of argv, see stm above.
4069 // r0: code entry
4070 // r1: function
4071 // r2: receiver
4072 // r3: argc
4073 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4074 __ ldr(r4, MemOperand(r4)); // argv
4075
4076 // Push a frame with special values setup to mark it as an entry frame.
4077 // r0: code entry
4078 // r1: function
4079 // r2: receiver
4080 // r3: argc
4081 // r4: argv
4082 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4083 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4084 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4085 __ mov(r6, Operand(Smi::FromInt(marker)));
4086 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4087 __ ldr(r5, MemOperand(r5));
4088 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4089
4090 // Setup frame pointer for the frame to be pushed.
4091 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4092
4093 // Call a faked try-block that does the invoke.
4094 __ bl(&invoke);
4095
4096 // Caught exception: Store result (exception) in the pending
4097 // exception field in the JSEnv and return a failure sentinel.
4098 // Coming in here the fp will be invalid because the PushTryHandler below
4099 // sets it to 0 to signal the existence of the JSEntry frame.
4100 __ mov(ip, Operand(Top::pending_exception_address()));
4101 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004102 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004103 __ b(&exit);
4104
4105 // Invoke: Link this frame into the handler chain.
4106 __ bind(&invoke);
4107 // Must preserve r0-r4, r5-r7 are available.
4108 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4109 // If an exception not caught by another handler occurs, this handler returns
4110 // control to the code after the bl(&invoke) above, which restores all
4111 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4112 // before returning a failure to C.
4113
4114 // Clear any pending exceptions.
4115 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4116 __ ldr(r5, MemOperand(ip));
4117 __ mov(ip, Operand(Top::pending_exception_address()));
4118 __ str(r5, MemOperand(ip));
4119
4120 // Invoke the function by calling through JS entry trampoline builtin.
4121 // Notice that we cannot store a reference to the trampoline code directly in
4122 // this stub, because runtime stubs are not traversed when doing GC.
4123
4124 // Expected registers by Builtins::JSEntryTrampoline
4125 // r0: code entry
4126 // r1: function
4127 // r2: receiver
4128 // r3: argc
4129 // r4: argv
4130 if (is_construct) {
4131 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4132 __ mov(ip, Operand(construct_entry));
4133 } else {
4134 ExternalReference entry(Builtins::JSEntryTrampoline);
4135 __ mov(ip, Operand(entry));
4136 }
4137 __ ldr(ip, MemOperand(ip)); // deref address
4138
4139 // Branch and link to JSEntryTrampoline
4140 __ mov(lr, Operand(pc));
4141 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4142
4143 // Unlink this frame from the handler chain. When reading the
4144 // address of the next handler, there is no need to use the address
4145 // displacement since the current stack pointer (sp) points directly
4146 // to the stack handler.
4147 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4148 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4149 __ str(r3, MemOperand(ip));
4150 // No need to restore registers
4151 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4152
4153 __ bind(&exit); // r0 holds result
4154 // Restore the top frame descriptors from the stack.
4155 __ pop(r3);
4156 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4157 __ str(r3, MemOperand(ip));
4158
4159 // Reset the stack to the callee saved registers.
4160 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4161
4162 // Restore callee-saved registers and return.
4163#ifdef DEBUG
4164 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4165#endif
4166 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4167}
4168
4169
ager@chromium.org7c537e22008-10-16 08:43:32 +00004170void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004171 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004172 Label adaptor;
4173 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4174 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4175 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004176 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004177
ager@chromium.org7c537e22008-10-16 08:43:32 +00004178 // Nothing to do: The formal number of parameters has already been
4179 // passed in register r0 by calling function. Just return it.
4180 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004181
ager@chromium.org7c537e22008-10-16 08:43:32 +00004182 // Arguments adaptor case: Read the arguments length from the
4183 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004184 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004185 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004186 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004187}
4188
4189
ager@chromium.org7c537e22008-10-16 08:43:32 +00004190void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4191 // The displacement is the offset of the last parameter (if any)
4192 // relative to the frame pointer.
4193 static const int kDisplacement =
4194 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004195
ager@chromium.org7c537e22008-10-16 08:43:32 +00004196 // Check that the key is a smi.
4197 Label slow;
4198 __ tst(r1, Operand(kSmiTagMask));
4199 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004200
ager@chromium.org7c537e22008-10-16 08:43:32 +00004201 // Check if the calling frame is an arguments adaptor frame.
4202 Label adaptor;
4203 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4204 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4205 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4206 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004207
ager@chromium.org7c537e22008-10-16 08:43:32 +00004208 // Check index against formal parameters count limit passed in
4209 // through register eax. Use unsigned comparison to get negative
4210 // check for free.
4211 __ cmp(r1, r0);
4212 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004213
ager@chromium.org7c537e22008-10-16 08:43:32 +00004214 // Read the argument from the stack and return it.
4215 __ sub(r3, r0, r1);
4216 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4217 __ ldr(r0, MemOperand(r3, kDisplacement));
4218 __ mov(pc, lr);
4219
4220 // Arguments adaptor case: Check index against actual arguments
4221 // limit found in the arguments adaptor frame. Use unsigned
4222 // comparison to get negative check for free.
4223 __ bind(&adaptor);
4224 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4225 __ cmp(r1, r0);
4226 __ b(cs, &slow);
4227
4228 // Read the argument from the adaptor frame and return it.
4229 __ sub(r3, r0, r1);
4230 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4231 __ ldr(r0, MemOperand(r3, kDisplacement));
4232 __ mov(pc, lr);
4233
4234 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4235 // by calling the runtime system.
4236 __ bind(&slow);
4237 __ push(r1);
4238 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4239}
4240
4241
4242void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4243 // Check if the calling frame is an arguments adaptor frame.
4244 Label runtime;
4245 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4246 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4247 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4248 __ b(ne, &runtime);
4249
4250 // Patch the arguments.length and the parameters pointer.
4251 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4252 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4253 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4254 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4255 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4256
4257 // Do the runtime call to allocate the arguments object.
4258 __ bind(&runtime);
4259 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004260}
4261
4262
4263void CallFunctionStub::Generate(MacroAssembler* masm) {
4264 Label slow;
4265 // Get the function to call from the stack.
4266 // function, receiver [, arguments]
4267 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4268
4269 // Check that the function is really a JavaScript function.
4270 // r1: pushed function (to be verified)
4271 __ tst(r1, Operand(kSmiTagMask));
4272 __ b(eq, &slow);
4273 // Get the map of the function object.
4274 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4275 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4276 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4277 __ b(ne, &slow);
4278
4279 // Fast-case: Invoke the function now.
4280 // r1: pushed function
4281 ParameterCount actual(argc_);
4282 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4283
4284 // Slow-case: Non-function called.
4285 __ bind(&slow);
4286 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004287 __ mov(r2, Operand(0));
4288 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
4289 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
4290 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004291}
4292
4293
4294#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004296} } // namespace v8::internal