blob: 4b1ada14f0b1550316c39748ec8d4be80710963f [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
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000038// -------------------------------------------------------------------------
39// CodeGenState implementation.
40
ager@chromium.org7c537e22008-10-16 08:43:32 +000041CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000042 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000043 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000044 true_target_(NULL),
45 false_target_(NULL),
46 previous_(NULL) {
47 owner_->set_state(this);
48}
49
50
ager@chromium.org7c537e22008-10-16 08:43:32 +000051CodeGenState::CodeGenState(CodeGenerator* owner,
52 TypeofState typeof_state,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000053 Label* true_target,
54 Label* false_target)
55 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000056 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000057 true_target_(true_target),
58 false_target_(false_target),
59 previous_(owner->state()) {
60 owner_->set_state(this);
61}
62
63
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000064CodeGenState::~CodeGenState() {
65 ASSERT(owner_->state() == this);
66 owner_->set_state(previous_);
67}
68
69
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070// -----------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +000071// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000072
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000073#define __ masm_->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074
ager@chromium.org7c537e22008-10-16 08:43:32 +000075CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
76 bool is_eval)
77 : is_eval_(is_eval),
78 script_(script),
79 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080 masm_(new MacroAssembler(NULL, buffer_size)),
81 scope_(NULL),
82 cc_reg_(al),
83 state_(NULL),
84 break_stack_height_(0) {
85}
86
87
88// Calling conventions:
89
mads.s.ager31e71382008-08-13 09:32:07 +000090// r0: the number of arguments
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091// fp: frame pointer
92// sp: stack pointer
93// pp: caller's parameter pointer
94// cp: callee's context
95
ager@chromium.org7c537e22008-10-16 08:43:32 +000096void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 Scope* scope = fun->scope();
98 ZoneList<Statement*>* body = fun->body();
99
100 // Initialize state.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000101 { CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 scope_ = scope;
103 cc_reg_ = al;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104
105 // Entry
106 // stack: function, receiver, arguments, return address
107 // r0: number of arguments
108 // sp: stack pointer
109 // fp: frame pointer
110 // pp: caller's parameter pointer
111 // cp: callee's context
112
113 { Comment cmnt(masm_, "[ enter JS frame");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000114 EnterJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 }
116 // tos: code slot
117#ifdef DEBUG
118 if (strlen(FLAG_stop_at) > 0 &&
119 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000120 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 }
122#endif
123
124 // Allocate space for locals and initialize them.
kasper.lund7276f142008-07-30 08:49:36 +0000125 if (scope->num_stack_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 Comment cmnt(masm_, "[ allocate space for locals");
mads.s.ager31e71382008-08-13 09:32:07 +0000127 // Initialize stack slots with 'undefined' value.
128 __ mov(ip, Operand(Factory::undefined_value()));
129 for (int i = 0; i < scope->num_stack_slots(); i++) {
130 __ push(ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 }
132 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133
134 if (scope->num_heap_slots() > 0) {
135 // Allocate local context.
136 // Get outer context and create a new context based on it.
mads.s.ager31e71382008-08-13 09:32:07 +0000137 __ ldr(r0, FunctionOperand());
138 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +0000139 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
140
141 if (kDebug) {
142 Label verified_true;
143 __ cmp(r0, Operand(cp));
144 __ b(eq, &verified_true);
145 __ stop("NewContext: r0 is expected to be the same as cp");
146 __ bind(&verified_true);
147 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 // Update context local.
149 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
150 }
151
152 // TODO(1241774): Improve this code!!!
153 // 1) only needed if we have a context
154 // 2) no need to recompute context ptr every single time
155 // 3) don't copy parameter operand code from SlotOperand!
156 {
157 Comment cmnt2(masm_, "[ copy context parameters into .context");
158
159 // Note that iteration order is relevant here! If we have the same
160 // parameter twice (e.g., function (x, y, x)), and that parameter
161 // needs to be copied into the context, it must be the last argument
162 // passed to the parameter that needs to be copied. This is a rare
163 // case so we don't check for it, instead we rely on the copying
164 // order: such a parameter is copied repeatedly into the same
165 // context location and thus the last value is what is seen inside
166 // the function.
167 for (int i = 0; i < scope->num_parameters(); i++) {
168 Variable* par = scope->parameter(i);
169 Slot* slot = par->slot();
170 if (slot != NULL && slot->type() == Slot::CONTEXT) {
171 ASSERT(!scope->is_global_scope()); // no parameters in global scope
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000172 __ ldr(r1, ParameterOperand(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 // Loads r2 with context; used below in RecordWrite.
174 __ str(r1, SlotOperand(slot, r2));
175 // Load the offset into r3.
176 int slot_offset =
177 FixedArray::kHeaderSize + slot->index() * kPointerSize;
178 __ mov(r3, Operand(slot_offset));
179 __ RecordWrite(r2, r3, r1);
180 }
181 }
182 }
183
184 // Store the arguments object.
185 // This must happen after context initialization because
186 // the arguments array may be stored in the context!
187 if (scope->arguments() != NULL) {
188 ASSERT(scope->arguments_shadow() != NULL);
189 Comment cmnt(masm_, "[ allocate arguments object");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000190 { Reference shadow_ref(this, scope->arguments_shadow());
191 { Reference arguments_ref(this, scope->arguments());
192 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
193 __ ldr(r2, FunctionOperand());
194 // The receiver is below the arguments, the return address,
195 // and the frame pointer on the stack.
196 const int kReceiverDisplacement = 2 + scope->num_parameters();
197 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
198 __ mov(r0, Operand(Smi::FromInt(scope->num_parameters())));
199 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
200 __ CallStub(&stub);
201 __ push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000202 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000203 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000204 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000206 __ pop(r0); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 }
208
209 // Generate code to 'execute' declarations and initialize
210 // functions (source elements). In case of an illegal
211 // redeclaration we need to handle that instead of processing the
212 // declarations.
213 if (scope->HasIllegalRedeclaration()) {
214 Comment cmnt(masm_, "[ illegal redeclarations");
215 scope->VisitIllegalRedeclaration(this);
216 } else {
217 Comment cmnt(masm_, "[ declarations");
mads.s.ager31e71382008-08-13 09:32:07 +0000218 // ProcessDeclarations calls DeclareGlobals indirectly
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 ProcessDeclarations(scope->declarations());
mads.s.ager31e71382008-08-13 09:32:07 +0000220
v8.team.kasperl727e9952008-09-02 14:56:44 +0000221 // Bail out if a stack-overflow exception occurred when
kasper.lund212ac232008-07-16 07:07:30 +0000222 // processing declarations.
223 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 }
225
mads.s.ager31e71382008-08-13 09:32:07 +0000226 if (FLAG_trace) {
227 // Push a valid value as the parameter. The runtime call only uses
228 // it as the return value to indicate non-failure.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000229 __ CallRuntime(Runtime::kTraceEnter, 0);
mads.s.ager31e71382008-08-13 09:32:07 +0000230 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 CheckStack();
232
233 // Compile the body of the function in a vanilla state. Don't
234 // bother compiling all the code if the scope has an illegal
235 // redeclaration.
236 if (!scope->HasIllegalRedeclaration()) {
237 Comment cmnt(masm_, "[ function body");
238#ifdef DEBUG
239 bool is_builtin = Bootstrapper::IsActive();
240 bool should_trace =
241 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000242 if (should_trace) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000243 __ CallRuntime(Runtime::kDebugTrace, 0);
mads.s.ager31e71382008-08-13 09:32:07 +0000244 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245#endif
246 VisitStatements(body);
247 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 }
249
250 // exit
251 // r0: result
252 // sp: stack pointer
253 // fp: frame pointer
254 // pp: parameter pointer
255 // cp: callee's context
mads.s.ager31e71382008-08-13 09:32:07 +0000256 __ mov(r0, Operand(Factory::undefined_value()));
257
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 __ bind(&function_return_);
mads.s.ager31e71382008-08-13 09:32:07 +0000259 if (FLAG_trace) {
260 // Push the return value on the stack as the parameter.
261 // Runtime::TraceExit returns the parameter as it is.
262 __ push(r0);
263 __ CallRuntime(Runtime::kTraceExit, 1);
264 }
265
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000266 // Tear down the frame which will restore the caller's frame pointer and the
267 // link register.
kasper.lund7276f142008-07-30 08:49:36 +0000268 ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000270 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
271 __ mov(pc, lr);
272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 // Code generation state must be reset.
274 scope_ = NULL;
275 ASSERT(!has_cc());
276 ASSERT(state_ == NULL);
277}
278
279
ager@chromium.org7c537e22008-10-16 08:43:32 +0000280MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
281 // Currently, this assertion will fail if we try to assign to
282 // a constant variable that is constant because it is read-only
283 // (such as the variable referring to a named function expression).
284 // We need to implement assignments to read-only variables.
285 // Ideally, we should do this during AST generation (by converting
286 // such assignments into expression statements); however, in general
287 // we may not be able to make the decision until past AST generation,
288 // that is when the entire program is known.
289 ASSERT(slot != NULL);
290 int index = slot->index();
291 switch (slot->type()) {
292 case Slot::PARAMETER:
293 return ParameterOperand(index);
294
295 case Slot::LOCAL: {
296 ASSERT(0 <= index && index < scope()->num_stack_slots());
297 const int kLocalOffset = JavaScriptFrameConstants::kLocal0Offset;
298 return MemOperand(fp, kLocalOffset - index * kPointerSize);
299 }
300
301 case Slot::CONTEXT: {
302 // Follow the context chain if necessary.
303 ASSERT(!tmp.is(cp)); // do not overwrite context register
304 Register context = cp;
305 int chain_length = scope()->ContextChainLength(slot->var()->scope());
306 for (int i = chain_length; i-- > 0;) {
307 // Load the closure.
308 // (All contexts, even 'with' contexts, have a closure,
309 // and it is the same for all contexts inside a function.
310 // There is no need to go to the function context first.)
311 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
312 // Load the function context (which is the incoming, outer context).
313 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
314 context = tmp;
315 }
316 // We may have a 'with' context now. Get the function context.
317 // (In fact this mov may never be the needed, since the scope analysis
318 // may not permit a direct context access in this case and thus we are
319 // always at a function context. However it is safe to dereference be-
320 // cause the function context of a function context is itself. Before
321 // deleting this mov we should try to create a counter-example first,
322 // though...)
323 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
324 return ContextOperand(tmp, index);
325 }
326
327 default:
328 UNREACHABLE();
329 return MemOperand(r0, 0);
330 }
331}
332
333
mads.s.ager31e71382008-08-13 09:32:07 +0000334// Loads a value on the stack. If it is a boolean value, the result may have
335// been (partially) translated into branches, or it may have set the condition
336// code register. If force_cc is set, the value is forced to set the condition
337// code register and no value is pushed. If the condition code register was set,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338// has_cc() is true and cc_reg_ contains the condition to test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000339void CodeGenerator::LoadCondition(Expression* x,
340 TypeofState typeof_state,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 Label* true_target,
342 Label* false_target,
343 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000344 ASSERT(!has_cc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345
ager@chromium.org7c537e22008-10-16 08:43:32 +0000346 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000347 Visit(x);
348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000350 // Convert the TOS value to a boolean in the condition code register.
351 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 }
353 ASSERT(has_cc() || !force_cc);
354}
355
356
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 Label true_target;
359 Label false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000360 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361
362 if (has_cc()) {
363 // convert cc_reg_ into a bool
364 Label loaded, materialize_true;
365 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000366 __ mov(r0, Operand(Factory::false_value()));
367 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 __ b(&loaded);
369 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000370 __ mov(r0, Operand(Factory::true_value()));
371 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 __ bind(&loaded);
373 cc_reg_ = al;
374 }
375
376 if (true_target.is_linked() || false_target.is_linked()) {
377 // we have at least one condition value
378 // that has been "translated" into a branch,
379 // thus it needs to be loaded explicitly again
380 Label loaded;
381 __ b(&loaded); // don't lose current TOS
382 bool both = true_target.is_linked() && false_target.is_linked();
383 // reincarnate "true", if necessary
384 if (true_target.is_linked()) {
385 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000386 __ mov(r0, Operand(Factory::true_value()));
387 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 }
389 // if both "true" and "false" need to be reincarnated,
390 // jump across code for "false"
391 if (both)
392 __ b(&loaded);
393 // reincarnate "false", if necessary
394 if (false_target.is_linked()) {
395 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000396 __ mov(r0, Operand(Factory::false_value()));
397 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 }
399 // everything is loaded at this point
400 __ bind(&loaded);
401 }
402 ASSERT(!has_cc());
403}
404
405
ager@chromium.org7c537e22008-10-16 08:43:32 +0000406void CodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000407 __ ldr(r0, GlobalObject());
408 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409}
410
411
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000412void CodeGenerator::LoadGlobalReceiver(Register s) {
413 __ ldr(s, ContextOperand(cp, Context::GLOBAL_INDEX));
414 __ ldr(s, FieldMemOperand(s, GlobalObject::kGlobalReceiverOffset));
415 __ push(s);
416}
417
418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000420// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
421// variables w/o reference errors elsewhere.
422void CodeGenerator::LoadTypeofExpression(Expression* x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 Variable* variable = x->AsVariableProxy()->AsVariable();
424 if (variable != NULL && !variable->is_this() && variable->is_global()) {
425 // NOTE: This is somewhat nasty. We force the compiler to load
426 // the variable as if through '<global>.<variable>' to make sure we
427 // do not get reference errors.
428 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
429 Literal key(variable->name());
430 // TODO(1241834): Fetch the position from the variable instead of using
431 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000432 Property property(&global, &key, RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 Load(&property);
434 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000435 Load(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 }
437}
438
439
ager@chromium.org7c537e22008-10-16 08:43:32 +0000440Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
442 cgen->LoadReference(this);
443}
444
445
446Reference::~Reference() {
447 cgen_->UnloadReference(this);
448}
449
450
ager@chromium.org7c537e22008-10-16 08:43:32 +0000451void CodeGenerator::LoadReference(Reference* ref) {
452 Comment cmnt(masm_, "[ LoadReference");
453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 Expression* e = ref->expression();
455 Property* property = e->AsProperty();
456 Variable* var = e->AsVariableProxy()->AsVariable();
457
458 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000459 // The expression is either a property or a variable proxy that rewrites
460 // to a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 Load(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000462 // We use a named reference if the key is a literal symbol, unless it is
463 // a string that can be legally parsed as an integer. This is because
464 // otherwise we will not get into the slow case code that handles [] on
465 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 Literal* literal = property->key()->AsLiteral();
467 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000468 if (literal != NULL &&
469 literal->handle()->IsSymbol() &&
470 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 ref->set_type(Reference::NAMED);
472 } else {
473 Load(property->key());
474 ref->set_type(Reference::KEYED);
475 }
476 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000477 // The expression is a variable proxy that does not rewrite to a
478 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 LoadGlobal();
481 ref->set_type(Reference::NAMED);
482 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000483 ASSERT(var->slot() != NULL);
484 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 }
486 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000487 // Anything else is a runtime error.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 Load(e);
489 __ CallRuntime(Runtime::kThrowReferenceError, 1);
490 }
491}
492
493
ager@chromium.org7c537e22008-10-16 08:43:32 +0000494void CodeGenerator::UnloadReference(Reference* ref) {
495 Comment cmnt(masm_, "[ UnloadReference");
496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 int size = ref->size();
498 if (size <= 0) {
499 // Do nothing. No popping is necessary.
500 } else {
mads.s.ager31e71382008-08-13 09:32:07 +0000501 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 __ add(sp, sp, Operand(size * kPointerSize));
mads.s.ager31e71382008-08-13 09:32:07 +0000503 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 }
505}
506
507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
509// register to a boolean in the condition code register. The code
510// may jump to 'false_target' in case the register converts to 'false'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000511void CodeGenerator::ToBoolean(Label* true_target,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000513 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 // Only the condition code should be set.
mads.s.ager31e71382008-08-13 09:32:07 +0000515 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516
517 // Fast case checks
518
mads.s.ager31e71382008-08-13 09:32:07 +0000519 // Check if the value is 'false'.
520 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 __ b(eq, false_target);
522
mads.s.ager31e71382008-08-13 09:32:07 +0000523 // Check if the value is 'true'.
524 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 __ b(eq, true_target);
526
mads.s.ager31e71382008-08-13 09:32:07 +0000527 // Check if the value is 'undefined'.
528 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 __ b(eq, false_target);
530
mads.s.ager31e71382008-08-13 09:32:07 +0000531 // Check if the value is a smi.
532 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000534 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 __ b(eq, true_target);
536
537 // Slow case: call the runtime.
538 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000539 __ CallRuntime(Runtime::kToBool, 1);
540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 // Convert result (r0) to condition code
542 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
544 cc_reg_ = ne;
545}
546
547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548class GetPropertyStub : public CodeStub {
549 public:
550 GetPropertyStub() { }
551
552 private:
553 Major MajorKey() { return GetProperty; }
554 int MinorKey() { return 0; }
555 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556};
557
558
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559class SetPropertyStub : public CodeStub {
560 public:
561 SetPropertyStub() { }
562
563 private:
564 Major MajorKey() { return SetProperty; }
565 int MinorKey() { return 0; }
566 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567};
568
569
kasper.lund7276f142008-07-30 08:49:36 +0000570class GenericBinaryOpStub : public CodeStub {
571 public:
572 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
573
574 private:
575 Token::Value op_;
576
577 Major MajorKey() { return GenericBinaryOp; }
578 int MinorKey() { return static_cast<int>(op_); }
579 void Generate(MacroAssembler* masm);
580
581 const char* GetName() {
582 switch (op_) {
583 case Token::ADD: return "GenericBinaryOpStub_ADD";
584 case Token::SUB: return "GenericBinaryOpStub_SUB";
585 case Token::MUL: return "GenericBinaryOpStub_MUL";
586 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000587 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
588 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
589 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
590 case Token::SAR: return "GenericBinaryOpStub_SAR";
591 case Token::SHL: return "GenericBinaryOpStub_SHL";
592 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000593 default: return "GenericBinaryOpStub";
594 }
595 }
596
597#ifdef DEBUG
598 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
599#endif
600};
601
602
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603class InvokeBuiltinStub : public CodeStub {
604 public:
605 enum Kind { Inc, Dec, ToNumber };
606 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
607
608 private:
609 Kind kind_;
610 int argc_;
611
612 Major MajorKey() { return InvokeBuiltin; }
613 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
614 void Generate(MacroAssembler* masm);
615
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616#ifdef DEBUG
617 void Print() {
618 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
619 static_cast<int>(kind_),
620 argc_);
621 }
622#endif
623};
624
625
ager@chromium.org7c537e22008-10-16 08:43:32 +0000626void CodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +0000627 // sp[0] : y
628 // sp[1] : x
629 // result : r0
630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 // Stub is entered with a call: 'return address' is in lr.
632 switch (op) {
633 case Token::ADD: // fall through.
634 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000635 case Token::MUL:
636 case Token::BIT_OR:
637 case Token::BIT_AND:
638 case Token::BIT_XOR:
639 case Token::SHL:
640 case Token::SHR:
641 case Token::SAR: {
mads.s.ager31e71382008-08-13 09:32:07 +0000642 __ pop(r0); // r0 : y
643 __ pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +0000644 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 __ CallStub(&stub);
646 break;
647 }
648
649 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +0000650 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000651 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 break;
653 }
654
655 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +0000656 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000657 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 break;
659 }
660
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 case Token::COMMA:
mads.s.ager31e71382008-08-13 09:32:07 +0000662 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 // simply discard left value
mads.s.ager31e71382008-08-13 09:32:07 +0000664 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 break;
666
667 default:
668 // Other cases should have been handled before this point.
669 UNREACHABLE();
670 break;
671 }
672}
673
674
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000675class DeferredInlinedSmiOperation: public DeferredCode {
676 public:
677 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
678 int value, bool reversed) :
679 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
680 set_comment("[ DeferredInlinedSmiOperation");
681 }
682
683 virtual void Generate() {
684 switch (op_) {
685 case Token::ADD: {
686 if (reversed_) {
687 // revert optimistic add
688 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
689 __ mov(r1, Operand(Smi::FromInt(value_))); // x
690 } else {
691 // revert optimistic add
692 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
693 __ mov(r0, Operand(Smi::FromInt(value_)));
694 }
695 break;
696 }
697
698 case Token::SUB: {
699 if (reversed_) {
700 // revert optimistic sub
701 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
702 __ mov(r1, Operand(Smi::FromInt(value_)));
703 } else {
704 __ add(r1, r0, Operand(Smi::FromInt(value_)));
705 __ mov(r0, Operand(Smi::FromInt(value_)));
706 }
707 break;
708 }
709
710 case Token::BIT_OR:
711 case Token::BIT_XOR:
712 case Token::BIT_AND: {
713 if (reversed_) {
714 __ mov(r1, Operand(Smi::FromInt(value_)));
715 } else {
716 __ mov(r1, Operand(r0));
717 __ mov(r0, Operand(Smi::FromInt(value_)));
718 }
719 break;
720 }
721
722 case Token::SHL:
723 case Token::SHR:
724 case Token::SAR: {
725 if (!reversed_) {
726 __ mov(r1, Operand(r0));
727 __ mov(r0, Operand(Smi::FromInt(value_)));
728 } else {
729 UNREACHABLE(); // should have been handled in SmiOperation
730 }
731 break;
732 }
733
734 default:
735 // other cases should have been handled before this point.
736 UNREACHABLE();
737 break;
738 }
739
740 GenericBinaryOpStub igostub(op_);
741 __ CallStub(&igostub);
742 }
743
744 private:
745 Token::Value op_;
746 int value_;
747 bool reversed_;
748};
749
750
ager@chromium.org7c537e22008-10-16 08:43:32 +0000751void CodeGenerator::SmiOperation(Token::Value op,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 Handle<Object> value,
753 bool reversed) {
754 // NOTE: This is an attempt to inline (a bit) more of the code for
755 // some possible smi operations (like + and -) when (at least) one
756 // of the operands is a literal smi. With this optimization, the
757 // performance of the system is increased by ~15%, and the generated
758 // code size is increased by ~1% (measured on a combination of
759 // different benchmarks).
760
mads.s.ager31e71382008-08-13 09:32:07 +0000761 // sp[0] : operand
762
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000763 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764
765 Label exit;
mads.s.ager31e71382008-08-13 09:32:07 +0000766 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
768 switch (op) {
769 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000770 DeferredCode* deferred =
771 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000773 __ add(r0, r0, Operand(value), SetCC);
774 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000776 __ b(ne, deferred->enter());
777 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 break;
779 }
780
781 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000782 DeferredCode* deferred =
783 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000786 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000788 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000790 __ b(vs, deferred->enter());
791 __ tst(r0, Operand(kSmiTagMask));
792 __ b(ne, deferred->enter());
793 __ bind(deferred->exit());
794 break;
795 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000797 case Token::BIT_OR:
798 case Token::BIT_XOR:
799 case Token::BIT_AND: {
800 DeferredCode* deferred =
801 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
802 __ tst(r0, Operand(kSmiTagMask));
803 __ b(ne, deferred->enter());
804 switch (op) {
805 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
806 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
807 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
808 default: UNREACHABLE();
809 }
810 __ bind(deferred->exit());
811 break;
812 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000814 case Token::SHL:
815 case Token::SHR:
816 case Token::SAR: {
817 if (reversed) {
818 __ mov(ip, Operand(value));
819 __ push(ip);
820 __ push(r0);
821 GenericBinaryOperation(op);
822
823 } else {
824 int shift_value = int_value & 0x1f; // least significant 5 bits
825 DeferredCode* deferred =
826 new DeferredInlinedSmiOperation(this, op, shift_value, false);
827 __ tst(r0, Operand(kSmiTagMask));
828 __ b(ne, deferred->enter());
829 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
830 switch (op) {
831 case Token::SHL: {
832 __ mov(r2, Operand(r2, LSL, shift_value));
833 // check that the *unsigned* result fits in a smi
834 __ add(r3, r2, Operand(0x40000000), SetCC);
835 __ b(mi, deferred->enter());
836 break;
837 }
838 case Token::SHR: {
839 // LSR by immediate 0 means shifting 32 bits.
840 if (shift_value != 0) {
841 __ mov(r2, Operand(r2, LSR, shift_value));
842 }
843 // check that the *unsigned* result fits in a smi
844 // neither of the two high-order bits can be set:
845 // - 0x80000000: high bit would be lost when smi tagging
846 // - 0x40000000: this number would convert to negative when
847 // smi tagging these two cases can only happen with shifts
848 // by 0 or 1 when handed a valid smi
849 __ and_(r3, r2, Operand(0xc0000000), SetCC);
850 __ b(ne, deferred->enter());
851 break;
852 }
853 case Token::SAR: {
854 if (shift_value != 0) {
855 // ASR by immediate 0 means shifting 32 bits.
856 __ mov(r2, Operand(r2, ASR, shift_value));
857 }
858 break;
859 }
860 default: UNREACHABLE();
861 }
862 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
863 __ bind(deferred->exit());
864 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 break;
866 }
867
868 default:
869 if (!reversed) {
mads.s.ager31e71382008-08-13 09:32:07 +0000870 __ push(r0);
871 __ mov(r0, Operand(value));
872 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 } else {
874 __ mov(ip, Operand(value));
875 __ push(ip);
mads.s.ager31e71382008-08-13 09:32:07 +0000876 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 }
kasper.lund7276f142008-07-30 08:49:36 +0000878 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 break;
880 }
881
882 __ bind(&exit);
883}
884
885
ager@chromium.org7c537e22008-10-16 08:43:32 +0000886void CodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +0000887 // sp[0] : y
888 // sp[1] : x
889 // result : cc register
890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 // Strict only makes sense for equality comparisons.
892 ASSERT(!strict || cc == eq);
893
894 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000895 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
896 if (cc == gt || cc == le) {
897 cc = ReverseCondition(cc);
mads.s.ager31e71382008-08-13 09:32:07 +0000898 __ pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000899 __ pop(r0);
900 } else {
mads.s.ager31e71382008-08-13 09:32:07 +0000901 __ pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000902 __ pop(r1);
903 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 __ orr(r2, r0, Operand(r1));
905 __ tst(r2, Operand(kSmiTagMask));
906 __ b(eq, &smi);
907
908 // Perform non-smi comparison by runtime call.
909 __ push(r1);
910
911 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000912 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 int argc;
914 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000915 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 argc = 1;
917 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000918 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 int ncr; // NaN compare result
920 if (cc == lt || cc == le) {
921 ncr = GREATER;
922 } else {
923 ASSERT(cc == gt || cc == ge); // remaining cases
924 ncr = LESS;
925 }
mads.s.ager31e71382008-08-13 09:32:07 +0000926 __ push(r0);
927 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 argc = 2;
929 }
930
931 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
932 // tagged as a small integer.
mads.s.ager31e71382008-08-13 09:32:07 +0000933 __ push(r0);
934 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000935 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 __ cmp(r0, Operand(0));
937 __ b(&exit);
938
939 // test smi equality by pointer comparison.
940 __ bind(&smi);
941 __ cmp(r1, Operand(r0));
942
943 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 cc_reg_ = cc;
945}
946
947
kasper.lund7276f142008-07-30 08:49:36 +0000948class CallFunctionStub: public CodeStub {
949 public:
950 explicit CallFunctionStub(int argc) : argc_(argc) {}
951
952 void Generate(MacroAssembler* masm);
953
954 private:
955 int argc_;
956
kasper.lund7276f142008-07-30 08:49:36 +0000957#if defined(DEBUG)
958 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
959#endif // defined(DEBUG)
960
961 Major MajorKey() { return CallFunction; }
962 int MinorKey() { return argc_; }
963};
964
965
mads.s.ager31e71382008-08-13 09:32:07 +0000966// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000967void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +0000970 for (int i = 0; i < args->length(); i++) {
971 Load(args->at(i));
972 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
kasper.lund7276f142008-07-30 08:49:36 +0000974 // Record the position for debugging purposes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 __ RecordPosition(position);
976
kasper.lund7276f142008-07-30 08:49:36 +0000977 // Use the shared code stub to call the function.
978 CallFunctionStub call_function(args->length());
979 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
981 // Restore context and pop function from the stack.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +0000983 __ pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984}
985
986
ager@chromium.org7c537e22008-10-16 08:43:32 +0000987void CodeGenerator::Branch(bool if_true, Label* L) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 ASSERT(has_cc());
989 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
990 __ b(cc, L);
991 cc_reg_ = al;
992}
993
994
ager@chromium.org7c537e22008-10-16 08:43:32 +0000995void CodeGenerator::CheckStack() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 if (FLAG_check_stack) {
997 Comment cmnt(masm_, "[ check stack");
998 StackCheckStub stub;
999 __ CallStub(&stub);
1000 }
1001}
1002
1003
ager@chromium.org7c537e22008-10-16 08:43:32 +00001004void CodeGenerator::VisitBlock(Block* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 Comment cmnt(masm_, "[ Block");
1006 if (FLAG_debug_info) RecordStatementPosition(node);
1007 node->set_break_stack_height(break_stack_height_);
1008 VisitStatements(node->statements());
1009 __ bind(node->break_target());
1010}
1011
1012
ager@chromium.org7c537e22008-10-16 08:43:32 +00001013void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00001014 __ mov(r0, Operand(pairs));
1015 __ push(r0);
1016 __ push(cp);
1017 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1018 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001020 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021}
1022
1023
ager@chromium.org7c537e22008-10-16 08:43:32 +00001024void CodeGenerator::VisitDeclaration(Declaration* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 Comment cmnt(masm_, "[ Declaration");
1026 Variable* var = node->proxy()->var();
1027 ASSERT(var != NULL); // must have been resolved
1028 Slot* slot = var->slot();
1029
1030 // If it was not possible to allocate the variable at compile time,
1031 // we need to "declare" it at runtime to make sure it actually
1032 // exists in the local context.
1033 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1034 // Variables with a "LOOKUP" slot were introduced as non-locals
1035 // during variable resolution and must have mode DYNAMIC.
1036 ASSERT(var->mode() == Variable::DYNAMIC);
1037 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00001038 __ push(cp);
1039 __ mov(r0, Operand(var->name()));
1040 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041 // Declaration nodes are always declared in only two modes.
1042 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1043 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001044 __ mov(r0, Operand(Smi::FromInt(attr)));
1045 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 // Push initial value, if any.
1047 // Note: For variables we must not push an initial value (such as
1048 // 'undefined') because we may have a (legal) redeclaration and we
1049 // must not destroy the current value.
1050 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001051 __ mov(r0, Operand(Factory::the_hole_value()));
1052 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 } else if (node->fun() != NULL) {
1054 Load(node->fun());
1055 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001056 __ mov(r0, Operand(0)); // no initial value!
1057 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001059 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
1060 // Ignore the return value (declarations are statements).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 return;
1062 }
1063
1064 ASSERT(!var->is_global());
1065
1066 // If we have a function or a constant, we need to initialize the variable.
1067 Expression* val = NULL;
1068 if (node->mode() == Variable::CONST) {
1069 val = new Literal(Factory::the_hole_value());
1070 } else {
1071 val = node->fun(); // NULL if we don't have a function
1072 }
1073
1074 if (val != NULL) {
1075 // Set initial value.
1076 Reference target(this, node->proxy());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001077 ASSERT(target.is_slot());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 Load(val);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001079 target.SetValue(NOT_CONST_INIT);
1080 // Get rid of the assigned value (declarations are statements). It's
1081 // safe to pop the value lying on top of the reference before unloading
1082 // the reference itself (which preserves the top of stack) because we
1083 // know it is a zero-sized reference.
mads.s.ager31e71382008-08-13 09:32:07 +00001084 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 }
1086}
1087
1088
ager@chromium.org7c537e22008-10-16 08:43:32 +00001089void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 Comment cmnt(masm_, "[ ExpressionStatement");
1091 if (FLAG_debug_info) RecordStatementPosition(node);
1092 Expression* expression = node->expression();
1093 expression->MarkAsStatement();
1094 Load(expression);
mads.s.ager31e71382008-08-13 09:32:07 +00001095 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096}
1097
1098
ager@chromium.org7c537e22008-10-16 08:43:32 +00001099void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 Comment cmnt(masm_, "// EmptyStatement");
1101 // nothing to do
1102}
1103
1104
ager@chromium.org7c537e22008-10-16 08:43:32 +00001105void CodeGenerator::VisitIfStatement(IfStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 Comment cmnt(masm_, "[ IfStatement");
1107 // Generate different code depending on which
1108 // parts of the if statement are present or not.
1109 bool has_then_stm = node->HasThenStatement();
1110 bool has_else_stm = node->HasElseStatement();
1111
1112 if (FLAG_debug_info) RecordStatementPosition(node);
1113
1114 Label exit;
1115 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001116 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 Label then;
1118 Label else_;
1119 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001120 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 Branch(false, &else_);
1122 // then
1123 __ bind(&then);
1124 Visit(node->then_statement());
1125 __ b(&exit);
1126 // else
1127 __ bind(&else_);
1128 Visit(node->else_statement());
1129
1130 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001131 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 ASSERT(!has_else_stm);
1133 Label then;
1134 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001135 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &exit, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136 Branch(false, &exit);
1137 // then
1138 __ bind(&then);
1139 Visit(node->then_statement());
1140
1141 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001142 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 ASSERT(!has_then_stm);
1144 Label else_;
1145 // if (!cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001146 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 Branch(true, &exit);
1148 // else
1149 __ bind(&else_);
1150 Visit(node->else_statement());
1151
1152 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001153 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 ASSERT(!has_then_stm && !has_else_stm);
1155 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001156 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &exit, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 if (has_cc()) {
1158 cc_reg_ = al;
1159 } else {
1160 __ pop(r0); // __ Pop(no_reg)
1161 }
1162 }
1163
1164 // end
1165 __ bind(&exit);
1166}
1167
1168
ager@chromium.org7c537e22008-10-16 08:43:32 +00001169void CodeGenerator::CleanStack(int num_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 ASSERT(num_bytes >= 0);
1171 if (num_bytes > 0) {
mads.s.ager31e71382008-08-13 09:32:07 +00001172 __ add(sp, sp, Operand(num_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 }
1174}
1175
1176
ager@chromium.org7c537e22008-10-16 08:43:32 +00001177void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 Comment cmnt(masm_, "[ ContinueStatement");
1179 if (FLAG_debug_info) RecordStatementPosition(node);
1180 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1181 __ b(node->target()->continue_target());
1182}
1183
1184
ager@chromium.org7c537e22008-10-16 08:43:32 +00001185void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 Comment cmnt(masm_, "[ BreakStatement");
1187 if (FLAG_debug_info) RecordStatementPosition(node);
1188 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1189 __ b(node->target()->break_target());
1190}
1191
1192
ager@chromium.org7c537e22008-10-16 08:43:32 +00001193void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194 Comment cmnt(masm_, "[ ReturnStatement");
1195 if (FLAG_debug_info) RecordStatementPosition(node);
1196 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00001197 // Move the function result into r0.
1198 __ pop(r0);
1199
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 __ b(&function_return_);
1201}
1202
1203
ager@chromium.org7c537e22008-10-16 08:43:32 +00001204void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 Comment cmnt(masm_, "[ WithEnterStatement");
1206 if (FLAG_debug_info) RecordStatementPosition(node);
1207 Load(node->expression());
kasper.lund7276f142008-07-30 08:49:36 +00001208 __ CallRuntime(Runtime::kPushContext, 1);
1209 if (kDebug) {
1210 Label verified_true;
1211 __ cmp(r0, Operand(cp));
1212 __ b(eq, &verified_true);
1213 __ stop("PushContext: r0 is expected to be the same as cp");
1214 __ bind(&verified_true);
1215 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 // Update context local.
1217 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1218}
1219
1220
ager@chromium.org7c537e22008-10-16 08:43:32 +00001221void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 Comment cmnt(masm_, "[ WithExitStatement");
1223 // Pop context.
1224 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1225 // Update context local.
1226 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1227}
1228
1229
ager@chromium.org7c537e22008-10-16 08:43:32 +00001230int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
1231 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001232}
1233
ager@chromium.org7c537e22008-10-16 08:43:32 +00001234int CodeGenerator::FastCaseSwitchMinCaseCount() {
1235 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001236}
1237
1238
ager@chromium.org7c537e22008-10-16 08:43:32 +00001239void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001240 SwitchStatement* node,
1241 int min_index,
1242 int range,
1243 Label* fail_label,
1244 Vector<Label*> case_targets,
1245 Vector<Label> case_labels) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001246
1247 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
1248
1249 __ pop(r0);
1250 if (min_index != 0) {
1251 // small positive numbers can be immediate operands.
1252 if (min_index < 0) {
1253 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1254 } else {
1255 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1256 }
1257 }
1258 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
1259 __ b(ne, fail_label);
1260 __ cmp(r0, Operand(Smi::FromInt(range)));
1261 __ b(ge, fail_label);
1262 __ add(pc, pc, Operand(r0, LSL, 2 - kSmiTagSize));
1263 // One extra instruction offsets the table, so the table's start address is
1264 // the pc-register at the above add.
1265 __ stop("Unreachable: Switch table alignment");
1266
1267 // table containing branch operations.
1268 for (int i = 0; i < range; i++) {
1269 __ b(case_targets[i]);
1270 }
1271
1272 GenerateFastCaseSwitchCases(node, case_labels);
1273}
1274
1275
ager@chromium.org7c537e22008-10-16 08:43:32 +00001276void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 Comment cmnt(masm_, "[ SwitchStatement");
1278 if (FLAG_debug_info) RecordStatementPosition(node);
1279 node->set_break_stack_height(break_stack_height_);
1280
1281 Load(node->tag());
1282
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001283 if (TryGenerateFastCaseSwitchStatement(node)) {
1284 return;
1285 }
1286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 Label next, fall_through, default_case;
1288 ZoneList<CaseClause*>* cases = node->cases();
1289 int length = cases->length();
1290
1291 for (int i = 0; i < length; i++) {
1292 CaseClause* clause = cases->at(i);
1293
1294 Comment cmnt(masm_, "[ case clause");
1295
1296 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001297 // Continue matching cases. The program will execute the default case's
1298 // statements if it does not match any of the cases.
1299 __ b(&next);
1300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 // Bind the default case label, so we can branch to it when we
1302 // have compared against all other cases.
1303 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001304 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 } else {
1306 __ bind(&next);
1307 next.Unuse();
mads.s.ager31e71382008-08-13 09:32:07 +00001308 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 __ push(r0); // duplicate TOS
1310 Load(clause->label());
1311 Comparison(eq, true);
1312 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 }
1314
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001315 // Entering the case statement for the first time. Remove the switch value
1316 // from the stack.
1317 __ pop(r0);
1318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001320 // This is also the target for the fall through from the previous case's
1321 // statements which has to skip over the matching code and the popping of
1322 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 __ bind(&fall_through);
1324 fall_through.Unuse();
1325 VisitStatements(clause->statements());
1326 __ b(&fall_through);
1327 }
1328
1329 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001330 // Reached the end of the case statements without matching any of the cases.
1331 if (default_case.is_bound()) {
1332 // A default case exists -> execute its statements.
1333 __ b(&default_case);
1334 } else {
1335 // Remove the switch value from the stack.
1336 __ pop(r0);
1337 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338
1339 __ bind(&fall_through);
1340 __ bind(node->break_target());
1341}
1342
1343
ager@chromium.org7c537e22008-10-16 08:43:32 +00001344void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 Comment cmnt(masm_, "[ LoopStatement");
1346 if (FLAG_debug_info) RecordStatementPosition(node);
1347 node->set_break_stack_height(break_stack_height_);
1348
1349 // simple condition analysis
1350 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1351 if (node->cond() == NULL) {
1352 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1353 info = ALWAYS_TRUE;
1354 } else {
1355 Literal* lit = node->cond()->AsLiteral();
1356 if (lit != NULL) {
1357 if (lit->IsTrue()) {
1358 info = ALWAYS_TRUE;
1359 } else if (lit->IsFalse()) {
1360 info = ALWAYS_FALSE;
1361 }
1362 }
1363 }
1364
1365 Label loop, entry;
1366
1367 // init
1368 if (node->init() != NULL) {
1369 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1370 Visit(node->init());
1371 }
1372 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
1373 __ b(&entry);
1374 }
1375
1376 // body
1377 __ bind(&loop);
1378 Visit(node->body());
1379
1380 // next
1381 __ bind(node->continue_target());
1382 if (node->next() != NULL) {
1383 // Record source position of the statement as this code which is after the
1384 // code for the body actually belongs to the loop statement and not the
1385 // body.
1386 if (FLAG_debug_info) __ RecordPosition(node->statement_pos());
1387 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1388 Visit(node->next());
1389 }
1390
1391 // cond
1392 __ bind(&entry);
1393 switch (info) {
1394 case ALWAYS_TRUE:
1395 CheckStack(); // TODO(1222600): ignore if body contains calls.
1396 __ b(&loop);
1397 break;
1398 case ALWAYS_FALSE:
1399 break;
1400 case DONT_KNOW:
1401 CheckStack(); // TODO(1222600): ignore if body contains calls.
1402 LoadCondition(node->cond(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00001403 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 &loop,
1405 node->break_target(),
1406 true);
1407 Branch(true, &loop);
1408 break;
1409 }
1410
1411 // exit
1412 __ bind(node->break_target());
1413}
1414
1415
ager@chromium.org7c537e22008-10-16 08:43:32 +00001416void CodeGenerator::VisitForInStatement(ForInStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 Comment cmnt(masm_, "[ ForInStatement");
1418 if (FLAG_debug_info) RecordStatementPosition(node);
1419
1420 // We keep stuff on the stack while the body is executing.
1421 // Record it, so that a break/continue crossing this statement
1422 // can restore the stack.
1423 const int kForInStackSize = 5 * kPointerSize;
1424 break_stack_height_ += kForInStackSize;
1425 node->set_break_stack_height(break_stack_height_);
1426
1427 Label loop, next, entry, cleanup, exit, primitive, jsobject;
1428 Label filter_key, end_del_check, fixed_array, non_string;
1429
1430 // Get the object to enumerate over (converted to JSObject).
1431 Load(node->enumerable());
mads.s.ager31e71382008-08-13 09:32:07 +00001432 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433
1434 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1435 // to the specification. 12.6.4 mandates a call to ToObject.
1436 __ cmp(r0, Operand(Factory::undefined_value()));
1437 __ b(eq, &exit);
1438 __ cmp(r0, Operand(Factory::null_value()));
1439 __ b(eq, &exit);
1440
1441 // Stack layout in body:
1442 // [iteration counter (Smi)]
1443 // [length of array]
1444 // [FixedArray]
1445 // [Map or 0]
1446 // [Object]
1447
1448 // Check if enumerable is already a JSObject
1449 __ tst(r0, Operand(kSmiTagMask));
1450 __ b(eq, &primitive);
1451 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1452 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001453 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 __ b(hs, &jsobject);
1455
1456 __ bind(&primitive);
mads.s.ager31e71382008-08-13 09:32:07 +00001457 __ push(r0);
1458 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001459 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460
1461
1462 __ bind(&jsobject);
1463
1464 // Get the set of properties (as a FixedArray or Map).
1465 __ push(r0); // duplicate the object being enumerated
mads.s.ager31e71382008-08-13 09:32:07 +00001466 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1468
1469 // If we got a Map, we can do a fast modification check.
1470 // Otherwise, we got a FixedArray, and we have to do a slow check.
1471 __ mov(r2, Operand(r0));
1472 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1473 __ cmp(r1, Operand(Factory::meta_map()));
1474 __ b(ne, &fixed_array);
1475
1476 // Get enum cache
1477 __ mov(r1, Operand(r0));
1478 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1479 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1480 __ ldr(r2,
1481 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1482
mads.s.ager31e71382008-08-13 09:32:07 +00001483 __ push(r0); // map
1484 __ push(r2); // enum cache bridge cache
1485 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00001487 __ push(r0);
1488 __ mov(r0, Operand(Smi::FromInt(0)));
1489 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 __ b(&entry);
1491
1492
1493 __ bind(&fixed_array);
1494
1495 __ mov(r1, Operand(Smi::FromInt(0)));
1496 __ push(r1); // insert 0 in place of Map
mads.s.ager31e71382008-08-13 09:32:07 +00001497 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498
1499 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001500 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00001502 __ push(r0);
1503 __ mov(r0, Operand(Smi::FromInt(0))); // init index
1504 __ push(r0);
1505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 __ b(&entry);
1507
1508 // Body.
1509 __ bind(&loop);
1510 Visit(node->body());
1511
1512 // Next.
1513 __ bind(node->continue_target());
1514 __ bind(&next);
mads.s.ager31e71382008-08-13 09:32:07 +00001515 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 __ add(r0, r0, Operand(Smi::FromInt(1)));
mads.s.ager31e71382008-08-13 09:32:07 +00001517 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518
1519 // Condition.
1520 __ bind(&entry);
1521
mads.s.ager31e71382008-08-13 09:32:07 +00001522 // sp[0] : index
1523 // sp[1] : array/enum cache length
1524 // sp[2] : array or enum cache
1525 // sp[3] : 0 or map
1526 // sp[4] : enumerable
1527 __ ldr(r0, MemOperand(sp, 0 * kPointerSize)); // load the current count
1528 __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // load the length
1529 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 __ b(hs, &cleanup);
1531
mads.s.ager31e71382008-08-13 09:32:07 +00001532 __ ldr(r0, MemOperand(sp, 0 * kPointerSize));
1533
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 // Get the i'th entry of the array.
mads.s.ager31e71382008-08-13 09:32:07 +00001535 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1537 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1538
1539 // Get Map or 0.
mads.s.ager31e71382008-08-13 09:32:07 +00001540 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 // Check if this (still) matches the map of the enumerable.
1542 // If not, we have to filter the key.
mads.s.ager31e71382008-08-13 09:32:07 +00001543 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1545 __ cmp(r1, Operand(r2));
1546 __ b(eq, &end_del_check);
1547
1548 // Convert the entry to a string (or null if it isn't a property anymore).
mads.s.ager31e71382008-08-13 09:32:07 +00001549 __ ldr(r0, MemOperand(sp, 4 * kPointerSize)); // push enumerable
1550 __ push(r0);
1551 __ push(r3); // push entry
1552 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001553 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555
1556 // If the property has been removed while iterating, we just skip it.
1557 __ cmp(r3, Operand(Factory::null_value()));
1558 __ b(eq, &next);
1559
1560
1561 __ bind(&end_del_check);
1562
1563 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00001564 // r3: i'th entry of the enum cache (or string there of)
1565 __ push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 { Reference each(this, node->each());
1567 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001568 if (each.size() > 0) {
1569 __ ldr(r0, MemOperand(sp, kPointerSize * each.size()));
1570 __ push(r0);
1571 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001572 // If the reference was to a slot we rely on the convenient property
1573 // that it doesn't matter whether a value (eg, r3 pushed above) is
1574 // right on top of or right underneath a zero-sized reference.
1575 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001576 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001577 // It's safe to pop the value lying on top of the reference before
1578 // unloading the reference itself (which preserves the top of stack,
1579 // ie, now the topmost value of the non-zero sized reference), since
1580 // we will discard the top of stack after unloading the reference
1581 // anyway.
1582 __ pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001583 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 }
1585 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001586 // Discard the i'th entry pushed above or else the remainder of the
1587 // reference, whichever is currently on top of the stack.
1588 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 CheckStack(); // TODO(1222600): ignore if body contains calls.
1590 __ jmp(&loop);
1591
1592 // Cleanup.
1593 __ bind(&cleanup);
1594 __ bind(node->break_target());
mads.s.ager31e71382008-08-13 09:32:07 +00001595 __ add(sp, sp, Operand(5 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596
1597 // Exit.
1598 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599
1600 break_stack_height_ -= kForInStackSize;
1601}
1602
1603
ager@chromium.org7c537e22008-10-16 08:43:32 +00001604void CodeGenerator::VisitTryCatch(TryCatch* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 Comment cmnt(masm_, "[ TryCatch");
1606
1607 Label try_block, exit;
1608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 __ bl(&try_block);
1610
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 // --- Catch block ---
1612
1613 // Store the caught exception in the catch variable.
mads.s.ager31e71382008-08-13 09:32:07 +00001614 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001616 ASSERT(ref.is_slot());
1617 // Here we make use of the convenient property that it doesn't matter
1618 // whether a value is immediately on top of or underneath a zero-sized
1619 // reference.
1620 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 }
1622
1623 // Remove the exception from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001624 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625
1626 VisitStatements(node->catch_block()->statements());
1627 __ b(&exit);
1628
1629
1630 // --- Try block ---
1631 __ bind(&try_block);
1632
1633 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1634
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001635 // Shadow the labels for all escapes from the try block, including
1636 // returns. During shadowing, the original label is hidden as the
1637 // LabelShadow and operations on the original actually affect the
1638 // shadowing label.
1639 //
1640 // We should probably try to unify the escaping labels and the return
1641 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 int nof_escapes = node->escaping_labels()->length();
1643 List<LabelShadow*> shadows(1 + nof_escapes);
1644 shadows.Add(new LabelShadow(&function_return_));
1645 for (int i = 0; i < nof_escapes; i++) {
1646 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1647 }
1648
1649 // Generate code for the statements in the try block.
1650 VisitStatements(node->try_block()->statements());
mads.s.ager31e71382008-08-13 09:32:07 +00001651 __ pop(r0); // Discard the result.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652
1653 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001654 // After shadowing stops, the original labels are unshadowed and the
1655 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 int nof_unlinks = 0;
1657 for (int i = 0; i <= nof_escapes; i++) {
1658 shadows[i]->StopShadowing();
1659 if (shadows[i]->is_linked()) nof_unlinks++;
1660 }
1661
1662 // Unlink from try chain.
1663 // TOS contains code slot
1664 const int kNextOffset = StackHandlerConstants::kNextOffset +
1665 StackHandlerConstants::kAddressDisplacement;
1666 __ ldr(r1, MemOperand(sp, kNextOffset)); // read next_sp
1667 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1668 __ str(r1, MemOperand(r3));
1669 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
1670 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1671 // Code slot popped.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 if (nof_unlinks > 0) __ b(&exit);
1673
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001674 // Generate unlink code for the (formerly) shadowing labels that have been
1675 // jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 for (int i = 0; i <= nof_escapes; i++) {
1677 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001678 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679 __ bind(shadows[i]);
1680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 // Reload sp from the top handler, because some statements that we
1682 // break from (eg, for...in) may have left stuff on the stack.
1683 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1684 __ ldr(sp, MemOperand(r3));
1685
1686 __ ldr(r1, MemOperand(sp, kNextOffset));
1687 __ str(r1, MemOperand(r3));
1688 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
1689 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1690 // Code slot popped.
1691
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001692 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 }
1694 }
1695
1696 __ bind(&exit);
1697}
1698
1699
ager@chromium.org7c537e22008-10-16 08:43:32 +00001700void CodeGenerator::VisitTryFinally(TryFinally* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 Comment cmnt(masm_, "[ TryFinally");
1702
1703 // State: Used to keep track of reason for entering the finally
1704 // block. Should probably be extended to hold information for
1705 // break/continue from within the try block.
1706 enum { FALLING, THROWING, JUMPING };
1707
1708 Label exit, unlink, try_block, finally_block;
1709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710 __ bl(&try_block);
1711
mads.s.ager31e71382008-08-13 09:32:07 +00001712 __ push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 // In case of thrown exceptions, this is where we continue.
1714 __ mov(r2, Operand(Smi::FromInt(THROWING)));
1715 __ b(&finally_block);
1716
1717
1718 // --- Try block ---
1719 __ bind(&try_block);
1720
1721 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1722
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001723 // Shadow the labels for all escapes from the try block, including
1724 // returns. Shadowing hides the original label as the LabelShadow and
1725 // operations on the original actually affect the 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());
1738
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001739 // Stop the introduced shadowing and count the number of required unlinks.
1740 // After shadowing stops, the original labels are unshadowed and the
1741 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 int nof_unlinks = 0;
1743 for (int i = 0; i <= nof_escapes; i++) {
1744 shadows[i]->StopShadowing();
1745 if (shadows[i]->is_linked()) nof_unlinks++;
1746 }
1747
1748 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00001749 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
1750 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 __ mov(r2, Operand(Smi::FromInt(FALLING)));
1752 if (nof_unlinks > 0) __ b(&unlink);
1753
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001754 // Generate code to set the state for the (formerly) shadowing labels that
1755 // have been jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 for (int i = 0; i <= nof_escapes; i++) {
1757 if (shadows[i]->is_linked()) {
1758 __ bind(shadows[i]);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001759 if (shadows[i]->original_label() == &function_return_) {
1760 // If this label shadowed the function return, materialize the
1761 // return value on the stack.
1762 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001763 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001764 // Fake TOS for labels that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00001765 __ mov(r0, Operand(Factory::undefined_value()));
1766 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 }
1768 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
1769 __ b(&unlink);
1770 }
1771 }
1772
mads.s.ager31e71382008-08-13 09:32:07 +00001773 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 __ bind(&unlink);
1775
mads.s.ager31e71382008-08-13 09:32:07 +00001776 __ pop(r0); // Store TOS in r0 across stack manipulation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 // Reload sp from the top handler, because some statements that we
1778 // break from (eg, for...in) may have left stuff on the stack.
1779 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1780 __ ldr(sp, MemOperand(r3));
1781 const int kNextOffset = StackHandlerConstants::kNextOffset +
1782 StackHandlerConstants::kAddressDisplacement;
1783 __ ldr(r1, MemOperand(sp, kNextOffset));
1784 __ str(r1, MemOperand(r3));
1785 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
1786 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
1787 // Code slot popped.
mads.s.ager31e71382008-08-13 09:32:07 +00001788 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789
1790 // --- Finally block ---
1791 __ bind(&finally_block);
1792
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001793 // Push the state on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001794 __ push(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001795
1796 // We keep two elements on the stack - the (possibly faked) result
1797 // and the state - while evaluating the finally block. Record it, so
1798 // that a break/continue crossing this statement can restore the
1799 // stack.
1800 const int kFinallyStackSize = 2 * kPointerSize;
1801 break_stack_height_ += kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802
1803 // Generate code for the statements in the finally block.
1804 VisitStatements(node->finally_block()->statements());
1805
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001806 // Restore state and return value or faked TOS.
mads.s.ager31e71382008-08-13 09:32:07 +00001807 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001808 __ pop(r0);
1809 break_stack_height_ -= kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001811 // Generate code to jump to the right destination for all used (formerly)
1812 // shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 for (int i = 0; i <= nof_escapes; i++) {
1814 if (shadows[i]->is_bound()) {
1815 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001816 if (shadows[i]->original_label() != &function_return_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 Label next;
1818 __ b(ne, &next);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001819 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 __ bind(&next);
1821 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001822 __ b(eq, shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 }
1824 }
1825 }
1826
1827 // Check if we need to rethrow the exception.
1828 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
1829 __ b(ne, &exit);
1830
1831 // Rethrow exception.
mads.s.ager31e71382008-08-13 09:32:07 +00001832 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 __ CallRuntime(Runtime::kReThrow, 1);
1834
1835 // Done.
1836 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837}
1838
1839
ager@chromium.org7c537e22008-10-16 08:43:32 +00001840void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 Comment cmnt(masm_, "[ DebuggerStatament");
1842 if (FLAG_debug_info) RecordStatementPosition(node);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001843 __ CallRuntime(Runtime::kDebugBreak, 0);
1844 // Ignore the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845}
1846
1847
ager@chromium.org7c537e22008-10-16 08:43:32 +00001848void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 ASSERT(boilerplate->IsBoilerplate());
1850
1851 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001852 __ mov(r0, Operand(boilerplate));
1853 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
1855 // Create a new closure.
mads.s.ager31e71382008-08-13 09:32:07 +00001856 __ push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 __ CallRuntime(Runtime::kNewClosure, 2);
mads.s.ager31e71382008-08-13 09:32:07 +00001858 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859}
1860
1861
ager@chromium.org7c537e22008-10-16 08:43:32 +00001862void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 Comment cmnt(masm_, "[ FunctionLiteral");
1864
1865 // Build the function boilerplate and instantiate it.
1866 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00001867 // Check for stack-overflow exception.
1868 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 InstantiateBoilerplate(boilerplate);
1870}
1871
1872
ager@chromium.org7c537e22008-10-16 08:43:32 +00001873void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 FunctionBoilerplateLiteral* node) {
1875 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
1876 InstantiateBoilerplate(node->boilerplate());
1877}
1878
1879
ager@chromium.org7c537e22008-10-16 08:43:32 +00001880void CodeGenerator::VisitConditional(Conditional* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 Comment cmnt(masm_, "[ Conditional");
1882 Label then, else_, exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001883 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 Branch(false, &else_);
1885 __ bind(&then);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001886 Load(node->then_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 __ b(&exit);
1888 __ bind(&else_);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001889 Load(node->else_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 __ bind(&exit);
1891}
1892
1893
ager@chromium.org7c537e22008-10-16 08:43:32 +00001894void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
1895 if (slot->type() == Slot::LOOKUP) {
1896 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897
1898 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00001899 __ push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001900 __ mov(r0, Operand(slot->var()->name()));
mads.s.ager31e71382008-08-13 09:32:07 +00001901 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902
ager@chromium.org7c537e22008-10-16 08:43:32 +00001903 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001904 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001905 } else {
1906 __ CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001908 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
1910 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001911 // Note: We would like to keep the assert below, but it fires because of
1912 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org7c537e22008-10-16 08:43:32 +00001913 // ASSERT(slot->var()->mode() != Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001915 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001916 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001917 __ push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001918 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001919 // Const slots may contain 'the hole' value (the constant hasn't been
1920 // initialized yet) which needs to be converted into the 'undefined'
1921 // value.
1922 Comment cmnt(masm_, "[ Unhole const");
1923 __ pop(r0);
1924 __ cmp(r0, Operand(Factory::the_hole_value()));
1925 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
1926 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 }
1928 }
1929}
1930
1931
ager@chromium.org7c537e22008-10-16 08:43:32 +00001932void CodeGenerator::VisitSlot(Slot* node) {
1933 Comment cmnt(masm_, "[ Slot");
1934 LoadFromSlot(node, typeof_state());
1935}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
ager@chromium.org7c537e22008-10-16 08:43:32 +00001937
1938void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
1939 Comment cmnt(masm_, "[ VariableProxy");
1940
1941 Variable* var = node->var();
1942 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001943 if (expr != NULL) {
1944 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001946 ASSERT(var->is_global());
1947 Reference ref(this, node);
1948 ref.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949 }
1950}
1951
1952
ager@chromium.org7c537e22008-10-16 08:43:32 +00001953void CodeGenerator::VisitLiteral(Literal* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00001955 __ mov(r0, Operand(node->handle()));
1956 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957}
1958
1959
ager@chromium.org7c537e22008-10-16 08:43:32 +00001960void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 Comment cmnt(masm_, "[ RexExp Literal");
1962
1963 // Retrieve the literal array and check the allocated entry.
1964
1965 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001966 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967
1968 // Load the literals array of the function.
1969 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
1970
1971 // Load the literal at the ast saved index.
1972 int literal_offset =
1973 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
1974 __ ldr(r2, FieldMemOperand(r1, literal_offset));
1975
1976 Label done;
1977 __ cmp(r2, Operand(Factory::undefined_value()));
1978 __ b(ne, &done);
1979
1980 // If the entry is undefined we call the runtime system to computed
1981 // the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00001982 __ push(r1); // literal array (0)
1983 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
1984 __ push(r0); // literal index (1)
1985 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
1986 __ push(r0);
1987 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
1988 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00001990 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991
mads.s.ager31e71382008-08-13 09:32:07 +00001992 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 // Push the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00001994 __ push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995}
1996
1997
1998// This deferred code stub will be used for creating the boilerplate
1999// by calling Runtime_CreateObjectLiteral.
2000// Each created boilerplate is stored in the JSFunction and they are
2001// therefore context dependent.
2002class ObjectLiteralDeferred: public DeferredCode {
2003 public:
2004 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
2005 : DeferredCode(generator), node_(node) {
2006 set_comment("[ ObjectLiteralDeferred");
2007 }
2008 virtual void Generate();
2009 private:
2010 ObjectLiteral* node_;
2011};
2012
2013
2014void ObjectLiteralDeferred::Generate() {
2015 // If the entry is undefined we call the runtime system to computed
2016 // the literal.
2017
2018 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00002019 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002021 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2022 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002024 __ mov(r0, Operand(node_->constant_properties()));
2025 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002027 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028}
2029
2030
ager@chromium.org7c537e22008-10-16 08:43:32 +00002031void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 Comment cmnt(masm_, "[ ObjectLiteral");
2033
2034 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
2035
2036 // Retrieve the literal array and check the allocated entry.
2037
2038 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002039 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
2041 // Load the literals array of the function.
2042 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2043
2044 // Load the literal at the ast saved index.
2045 int literal_offset =
2046 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2047 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2048
2049 // Check whether we need to materialize the object literal boilerplate.
2050 // If so, jump to the deferred code.
2051 __ cmp(r2, Operand(Factory::undefined_value()));
2052 __ b(eq, deferred->enter());
2053 __ bind(deferred->exit());
2054
2055 // Push the object literal boilerplate.
mads.s.ager31e71382008-08-13 09:32:07 +00002056 __ push(r2);
2057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 // Clone the boilerplate object.
2059 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002060 __ push(r0); // save the result
2061 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062
2063 for (int i = 0; i < node->properties()->length(); i++) {
2064 ObjectLiteral::Property* property = node->properties()->at(i);
2065 Literal* key = property->key();
2066 Expression* value = property->value();
2067 switch (property->kind()) {
2068 case ObjectLiteral::Property::CONSTANT: break;
2069 case ObjectLiteral::Property::COMPUTED: // fall through
2070 case ObjectLiteral::Property::PROTOTYPE: {
mads.s.ager31e71382008-08-13 09:32:07 +00002071 __ push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 Load(key);
2073 Load(value);
2074 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002075 // restore r0
2076 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 break;
2078 }
2079 case ObjectLiteral::Property::SETTER: {
2080 __ push(r0);
2081 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002082 __ mov(r0, Operand(Smi::FromInt(1)));
2083 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 Load(value);
2085 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002086 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 break;
2088 }
2089 case ObjectLiteral::Property::GETTER: {
2090 __ push(r0);
2091 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002092 __ mov(r0, Operand(Smi::FromInt(0)));
2093 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 Load(value);
2095 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002096 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 break;
2098 }
2099 }
2100 }
2101}
2102
2103
ager@chromium.org7c537e22008-10-16 08:43:32 +00002104void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002106
2107 // Call runtime to create the array literal.
2108 __ mov(r0, Operand(node->literals()));
2109 __ push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002110 // Load the function of this frame.
2111 __ ldr(r0, FunctionOperand());
2112 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002113 __ push(r0);
2114 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
2115
2116 // Push the resulting array literal on the stack.
2117 __ push(r0);
2118
2119 // Generate code to set the elements in the array that are not
2120 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 for (int i = 0; i < node->values()->length(); i++) {
2122 Expression* value = node->values()->at(i);
2123
2124 // If value is literal the property value is already
2125 // set in the boilerplate object.
2126 if (value->AsLiteral() == NULL) {
2127 // The property must be set by generated code.
2128 Load(value);
mads.s.ager31e71382008-08-13 09:32:07 +00002129 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
2131 // Fetch the object literal
2132 __ ldr(r1, MemOperand(sp, 0));
2133 // Get the elements array.
2134 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
2135
2136 // Write to the indexed properties array.
2137 int offset = i * kPointerSize + Array::kHeaderSize;
2138 __ str(r0, FieldMemOperand(r1, offset));
2139
2140 // Update the write barrier for the array address.
2141 __ mov(r3, Operand(offset));
2142 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 }
2144 }
2145}
2146
2147
ager@chromium.org7c537e22008-10-16 08:43:32 +00002148void CodeGenerator::VisitAssignment(Assignment* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 if (FLAG_debug_info) RecordStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 Reference target(this, node->target());
2153 if (target.is_illegal()) return;
2154
2155 if (node->op() == Token::ASSIGN ||
2156 node->op() == Token::INIT_VAR ||
2157 node->op() == Token::INIT_CONST) {
2158 Load(node->value());
2159
2160 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002161 target.GetValue(NOT_INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 Literal* literal = node->value()->AsLiteral();
2163 if (literal != NULL && literal->handle()->IsSmi()) {
2164 SmiOperation(node->binary_op(), literal->handle(), false);
mads.s.ager31e71382008-08-13 09:32:07 +00002165 __ push(r0);
2166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 } else {
2168 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00002169 GenericBinaryOperation(node->binary_op());
mads.s.ager31e71382008-08-13 09:32:07 +00002170 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 }
2172 }
2173
2174 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2175 if (var != NULL &&
2176 (var->mode() == Variable::CONST) &&
2177 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2178 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 } else {
2181 __ RecordPosition(node->position());
2182 if (node->op() == Token::INIT_CONST) {
2183 // Dynamic constant initializations must use the function context
2184 // and initialize the actual constant declared. Dynamic variable
2185 // initializations are simply assignments and use SetValue.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002186 target.SetValue(CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002188 target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 }
2190 }
2191}
2192
2193
ager@chromium.org7c537e22008-10-16 08:43:32 +00002194void CodeGenerator::VisitThrow(Throw* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 Comment cmnt(masm_, "[ Throw");
2196
2197 Load(node->exception());
2198 __ RecordPosition(node->position());
2199 __ CallRuntime(Runtime::kThrow, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002200 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201}
2202
2203
ager@chromium.org7c537e22008-10-16 08:43:32 +00002204void CodeGenerator::VisitProperty(Property* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002206
ager@chromium.org7c537e22008-10-16 08:43:32 +00002207 Reference property(this, node);
2208 property.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209}
2210
2211
ager@chromium.org7c537e22008-10-16 08:43:32 +00002212void CodeGenerator::VisitCall(Call* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 Comment cmnt(masm_, "[ Call");
2214
2215 ZoneList<Expression*>* args = node->arguments();
2216
2217 if (FLAG_debug_info) RecordStatementPosition(node);
2218 // Standard function call.
2219
2220 // Check if the function is a variable or a property.
2221 Expression* function = node->expression();
2222 Variable* var = function->AsVariableProxy()->AsVariable();
2223 Property* property = function->AsProperty();
2224
2225 // ------------------------------------------------------------------------
2226 // Fast-case: Use inline caching.
2227 // ---
2228 // According to ECMA-262, section 11.2.3, page 44, the function to call
2229 // must be resolved after the arguments have been evaluated. The IC code
2230 // automatically handles this by loading the arguments before the function
2231 // is resolved in cache misses (this also holds for megamorphic calls).
2232 // ------------------------------------------------------------------------
2233
2234 if (var != NULL && !var->is_this() && var->is_global()) {
2235 // ----------------------------------
2236 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2237 // ----------------------------------
2238
2239 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002240 __ mov(r0, Operand(var->name()));
2241 __ push(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002242
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002243 // Pass the global object as the receiver and let the IC stub
2244 // patch the stack to use the global proxy as 'this' in the
2245 // invoked function.
2246 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247
2248 // Load the arguments.
2249 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250
2251 // Setup the receiver register and call the IC initialization code.
2252 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002254 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002257 __ pop();
2258 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259
2260 } else if (var != NULL && var->slot() != NULL &&
2261 var->slot()->type() == Slot::LOOKUP) {
2262 // ----------------------------------
2263 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2264 // ----------------------------------
2265
2266 // Load the function
mads.s.ager31e71382008-08-13 09:32:07 +00002267 __ push(cp);
2268 __ mov(r0, Operand(var->name()));
2269 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2271 // r0: slot value; r1: receiver
2272
2273 // Load the receiver.
mads.s.ager31e71382008-08-13 09:32:07 +00002274 __ push(r0); // function
2275 __ push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276
2277 // Call the function.
2278 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00002279 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280
2281 } else if (property != NULL) {
2282 // Check if the key is a literal string.
2283 Literal* literal = property->key()->AsLiteral();
2284
2285 if (literal != NULL && literal->handle()->IsSymbol()) {
2286 // ------------------------------------------------------------------
2287 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2288 // ------------------------------------------------------------------
2289
2290 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002291 __ mov(r0, Operand(literal->handle()));
2292 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 Load(property->obj());
2294
2295 // Load the arguments.
2296 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297
2298 // Set the receiver register and call the IC initialization code.
2299 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002301 __ Call(stub, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2303
2304 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002305 __ pop();
2306
2307 __ push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308
2309 } else {
2310 // -------------------------------------------
2311 // JavaScript example: 'array[index](1, 2, 3)'
2312 // -------------------------------------------
2313
2314 // Load the function to call from the property through a reference.
2315 Reference ref(this, property);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002316 ref.GetValue(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317
2318 // Pass receiver to called function.
mads.s.ager31e71382008-08-13 09:32:07 +00002319 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
2320 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 // Call the function.
2322 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00002323 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 }
2325
2326 } else {
2327 // ----------------------------------
2328 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
2329 // ----------------------------------
2330
2331 // Load the function.
2332 Load(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002333
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002334 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002335 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 // Call the function.
2338 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00002339 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 }
2341}
2342
2343
ager@chromium.org7c537e22008-10-16 08:43:32 +00002344void CodeGenerator::VisitCallNew(CallNew* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 Comment cmnt(masm_, "[ CallNew");
2346
2347 // According to ECMA-262, section 11.2.2, page 44, the function
2348 // expression in new calls must be evaluated before the
2349 // arguments. This is different from ordinary calls, where the
2350 // actual function to call is resolved after the arguments have been
2351 // evaluated.
2352
2353 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002354 // receiver. There is no need to use the global proxy here because
2355 // it will always be replaced with a newly allocated object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002357 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358
2359 // Push the arguments ("left-to-right") on the stack.
2360 ZoneList<Expression*>* args = node->arguments();
2361 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2362
mads.s.ager31e71382008-08-13 09:32:07 +00002363 // r0: the number of arguments.
2364 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002366 // Load the function into r1 as per calling convention.
2367 __ ldr(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
2368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 // Call the construct call builtin that handles allocation and
2370 // constructor invocation.
ager@chromium.org236ad962008-09-25 09:45:57 +00002371 __ RecordPosition(RelocInfo::POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00002373 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00002374
2375 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
2376 __ str(r0, MemOperand(sp, 0 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377}
2378
2379
ager@chromium.org7c537e22008-10-16 08:43:32 +00002380void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 ASSERT(args->length() == 1);
2382 Label leave;
2383 Load(args->at(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002384 __ pop(r0); // r0 contains object.
2385 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 __ tst(r0, Operand(kSmiTagMask));
2387 __ b(eq, &leave);
2388 // It is a heap object - get map.
2389 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2390 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002391 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392 __ cmp(r1, Operand(JS_VALUE_TYPE));
2393 __ b(ne, &leave);
2394 // Load the value.
2395 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2396 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00002397 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398}
2399
2400
ager@chromium.org7c537e22008-10-16 08:43:32 +00002401void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 ASSERT(args->length() == 2);
2403 Label leave;
2404 Load(args->at(0)); // Load the object.
2405 Load(args->at(1)); // Load the value.
mads.s.ager31e71382008-08-13 09:32:07 +00002406 __ pop(r0); // r0 contains value
2407 __ pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002408 // if (object->IsSmi()) return object.
2409 __ tst(r1, Operand(kSmiTagMask));
2410 __ b(eq, &leave);
2411 // It is a heap object - get map.
2412 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2413 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2414 // if (!object->IsJSValue()) return object.
2415 __ cmp(r2, Operand(JS_VALUE_TYPE));
2416 __ b(ne, &leave);
2417 // Store the value.
2418 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2419 // Update the write barrier.
2420 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2421 __ RecordWrite(r1, r2, r3);
2422 // Leave.
2423 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00002424 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425}
2426
2427
ager@chromium.org7c537e22008-10-16 08:43:32 +00002428void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 ASSERT(args->length() == 1);
2430 Load(args->at(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 __ pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002432 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 cc_reg_ = eq;
2434}
2435
2436
ager@chromium.org7c537e22008-10-16 08:43:32 +00002437void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002438 ASSERT(args->length() == 1);
2439 Load(args->at(0));
2440 __ pop(r0);
2441 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2442 cc_reg_ = eq;
2443}
2444
2445
kasper.lund7276f142008-07-30 08:49:36 +00002446// This should generate code that performs a charCodeAt() call or returns
2447// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2448// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002449void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00002450 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002451 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +00002452 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002453}
2454
2455
ager@chromium.org7c537e22008-10-16 08:43:32 +00002456void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002457 ASSERT(args->length() == 1);
2458 Load(args->at(0));
2459 Label answer;
2460 // We need the CC bits to come out as not_equal in the case where the
2461 // object is a smi. This can't be done with the usual test opcode so
2462 // we use XOR to get the right CC bits.
2463 __ pop(r0);
2464 __ and_(r1, r0, Operand(kSmiTagMask));
2465 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2466 __ b(ne, &answer);
2467 // It is a heap object - get the map.
2468 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2469 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2470 // Check if the object is a JS array or not.
2471 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2472 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 cc_reg_ = eq;
2474}
2475
2476
ager@chromium.org7c537e22008-10-16 08:43:32 +00002477void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 ASSERT(args->length() == 0);
2479
mads.s.ager31e71382008-08-13 09:32:07 +00002480 // Seed the result with the formal parameters count, which will be used
2481 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2483
2484 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002485 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00002487 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488}
2489
2490
ager@chromium.org7c537e22008-10-16 08:43:32 +00002491void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 ASSERT(args->length() == 1);
2493
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002494 // Satisfy contract with ArgumentsAccessStub:
2495 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 Load(args->at(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002497 __ pop(r1);
2498 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499
2500 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002501 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00002503 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504}
2505
2506
ager@chromium.org7c537e22008-10-16 08:43:32 +00002507void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002508 ASSERT(args->length() == 2);
2509
2510 // Load the two objects into registers and perform the comparison.
2511 Load(args->at(0));
2512 Load(args->at(1));
2513 __ pop(r0);
2514 __ pop(r1);
2515 __ cmp(r0, Operand(r1));
2516 cc_reg_ = eq;
2517}
2518
2519
ager@chromium.org7c537e22008-10-16 08:43:32 +00002520void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002521 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522
2523 ZoneList<Expression*>* args = node->arguments();
2524 Comment cmnt(masm_, "[ CallRuntime");
2525 Runtime::Function* function = node->function();
2526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002528 // Push the arguments ("left-to-right").
2529 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2530
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 // Call the C runtime function.
2532 __ CallRuntime(function, args->length());
mads.s.ager31e71382008-08-13 09:32:07 +00002533 __ push(r0);
2534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002536 // Prepare stack for calling JS runtime function.
2537 __ mov(r0, Operand(node->name()));
2538 __ push(r0);
2539 // Push the builtins object found in the current global object.
2540 __ ldr(r1, GlobalObject());
2541 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
2542 __ push(r0);
2543
2544 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2545
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002548 __ Call(stub, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002550 __ pop();
2551 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552 }
2553}
2554
2555
ager@chromium.org7c537e22008-10-16 08:43:32 +00002556void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 Comment cmnt(masm_, "[ UnaryOperation");
2558
2559 Token::Value op = node->op();
2560
2561 if (op == Token::NOT) {
2562 LoadCondition(node->expression(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002563 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 false_target(),
2565 true_target(),
2566 true);
2567 cc_reg_ = NegateCondition(cc_reg_);
2568
2569 } else if (op == Token::DELETE) {
2570 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002571 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 if (property != NULL) {
2573 Load(property->obj());
2574 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002575 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002576 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577
mads.s.ager31e71382008-08-13 09:32:07 +00002578 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 Slot* slot = variable->slot();
2580 if (variable->is_global()) {
2581 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00002582 __ mov(r0, Operand(variable->name()));
2583 __ push(r0);
2584 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002585 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586
2587 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2588 // lookup the context holding the named variable
mads.s.ager31e71382008-08-13 09:32:07 +00002589 __ push(cp);
2590 __ mov(r0, Operand(variable->name()));
2591 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 __ CallRuntime(Runtime::kLookupContext, 2);
2593 // r0: context
mads.s.ager31e71382008-08-13 09:32:07 +00002594 __ push(r0);
2595 __ mov(r0, Operand(variable->name()));
2596 __ push(r0);
2597 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002598 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599
mads.s.ager31e71382008-08-13 09:32:07 +00002600 } else {
2601 // Default: Result of deleting non-global, not dynamically
2602 // introduced variables is false.
2603 __ mov(r0, Operand(Factory::false_value()));
2604 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605
2606 } else {
2607 // Default: Result of deleting expressions is true.
2608 Load(node->expression()); // may have side-effects
mads.s.ager31e71382008-08-13 09:32:07 +00002609 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 __ mov(r0, Operand(Factory::true_value()));
2611 }
mads.s.ager31e71382008-08-13 09:32:07 +00002612 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613
2614 } else if (op == Token::TYPEOF) {
2615 // Special case for loading the typeof expression; see comment on
2616 // LoadTypeofExpression().
2617 LoadTypeofExpression(node->expression());
2618 __ CallRuntime(Runtime::kTypeof, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002619 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620
2621 } else {
2622 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00002623 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 switch (op) {
2625 case Token::NOT:
2626 case Token::DELETE:
2627 case Token::TYPEOF:
2628 UNREACHABLE(); // handled above
2629 break;
2630
2631 case Token::SUB: {
2632 UnarySubStub stub;
2633 __ CallStub(&stub);
2634 break;
2635 }
2636
2637 case Token::BIT_NOT: {
2638 // smi check
2639 Label smi_label;
2640 Label continue_label;
2641 __ tst(r0, Operand(kSmiTagMask));
2642 __ b(eq, &smi_label);
2643
mads.s.ager31e71382008-08-13 09:32:07 +00002644 __ push(r0);
2645 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002646 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647
2648 __ b(&continue_label);
2649 __ bind(&smi_label);
2650 __ mvn(r0, Operand(r0));
2651 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
2652 __ bind(&continue_label);
2653 break;
2654 }
2655
2656 case Token::VOID:
2657 // since the stack top is cached in r0, popping and then
2658 // pushing a value can be done by just writing to r0.
2659 __ mov(r0, Operand(Factory::undefined_value()));
2660 break;
2661
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002662 case Token::ADD: {
2663 // Smi check.
2664 Label continue_label;
2665 __ tst(r0, Operand(kSmiTagMask));
2666 __ b(eq, &continue_label);
mads.s.ager31e71382008-08-13 09:32:07 +00002667 __ push(r0);
2668 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002669 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002670 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002672 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 default:
2674 UNREACHABLE();
2675 }
mads.s.ager31e71382008-08-13 09:32:07 +00002676 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 }
2678}
2679
2680
ager@chromium.org7c537e22008-10-16 08:43:32 +00002681void CodeGenerator::VisitCountOperation(CountOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 Comment cmnt(masm_, "[ CountOperation");
2683
2684 bool is_postfix = node->is_postfix();
2685 bool is_increment = node->op() == Token::INC;
2686
2687 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
2688 bool is_const = (var != NULL && var->mode() == Variable::CONST);
2689
2690 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00002691 if (is_postfix) {
2692 __ mov(r0, Operand(0));
2693 __ push(r0);
2694 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695
2696 { Reference target(this, node->expression());
2697 if (target.is_illegal()) return;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002698 target.GetValue(NOT_INSIDE_TYPEOF);
mads.s.ager31e71382008-08-13 09:32:07 +00002699 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700
2701 Label slow, exit;
2702
2703 // Load the value (1) into register r1.
2704 __ mov(r1, Operand(Smi::FromInt(1)));
2705
2706 // Check for smi operand.
2707 __ tst(r0, Operand(kSmiTagMask));
2708 __ b(ne, &slow);
2709
2710 // Postfix: Store the old value as the result.
2711 if (is_postfix) __ str(r0, MemOperand(sp, target.size() * kPointerSize));
2712
2713 // Perform optimistic increment/decrement.
2714 if (is_increment) {
2715 __ add(r0, r0, Operand(r1), SetCC);
2716 } else {
2717 __ sub(r0, r0, Operand(r1), SetCC);
2718 }
2719
2720 // If the increment/decrement didn't overflow, we're done.
2721 __ b(vc, &exit);
2722
2723 // Revert optimistic increment/decrement.
2724 if (is_increment) {
2725 __ sub(r0, r0, Operand(r1));
2726 } else {
2727 __ add(r0, r0, Operand(r1));
2728 }
2729
2730 // Slow case: Convert to number.
2731 __ bind(&slow);
2732
2733 // Postfix: Convert the operand to a number and store it as the result.
2734 if (is_postfix) {
2735 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
2736 __ CallStub(&stub);
2737 // Store to result (on the stack).
2738 __ str(r0, MemOperand(sp, target.size() * kPointerSize));
2739 }
2740
2741 // Compute the new value by calling the right JavaScript native.
2742 if (is_increment) {
2743 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
2744 __ CallStub(&stub);
2745 } else {
2746 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
2747 __ CallStub(&stub);
2748 }
2749
2750 // Store the new value in the target if not const.
2751 __ bind(&exit);
mads.s.ager31e71382008-08-13 09:32:07 +00002752 __ push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002753 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754 }
2755
2756 // Postfix: Discard the new value and use the old.
2757 if (is_postfix) __ pop(r0);
2758}
2759
2760
ager@chromium.org7c537e22008-10-16 08:43:32 +00002761void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 Comment cmnt(masm_, "[ BinaryOperation");
2763 Token::Value op = node->op();
2764
2765 // According to ECMA-262 section 11.11, page 58, the binary logical
2766 // operators must yield the result of one of the two expressions
2767 // before any ToBoolean() conversions. This means that the value
2768 // produced by a && or || operator is not necessarily a boolean.
2769
2770 // NOTE: If the left hand side produces a materialized value (not in
2771 // the CC register), we force the right hand side to do the
2772 // same. This is necessary because we may have to branch to the exit
2773 // after evaluating the left hand side (due to the shortcut
2774 // semantics), but the compiler must (statically) know if the result
2775 // of compiling the binary operation is materialized or not.
2776
2777 if (op == Token::AND) {
2778 Label is_true;
2779 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002780 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002781 &is_true,
2782 false_target(),
2783 false);
2784 if (has_cc()) {
2785 Branch(false, false_target());
2786
2787 // Evaluate right side expression.
2788 __ bind(&is_true);
2789 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002790 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791 true_target(),
2792 false_target(),
2793 false);
2794
2795 } else {
2796 Label pop_and_continue, exit;
2797
mads.s.ager31e71382008-08-13 09:32:07 +00002798 __ ldr(r0, MemOperand(sp, 0)); // dup the stack top
2799 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 // Avoid popping the result if it converts to 'false' using the
2801 // standard ToBoolean() conversion as described in ECMA-262,
2802 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002803 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002804 Branch(false, &exit);
2805
2806 // Pop the result of evaluating the first part.
2807 __ bind(&pop_and_continue);
2808 __ pop(r0);
2809
2810 // Evaluate right side expression.
2811 __ bind(&is_true);
2812 Load(node->right());
2813
2814 // Exit (always with a materialized value).
2815 __ bind(&exit);
2816 }
2817
2818 } else if (op == Token::OR) {
2819 Label is_false;
2820 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002821 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822 true_target(),
2823 &is_false,
2824 false);
2825 if (has_cc()) {
2826 Branch(true, true_target());
2827
2828 // Evaluate right side expression.
2829 __ bind(&is_false);
2830 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002831 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 true_target(),
2833 false_target(),
2834 false);
2835
2836 } else {
2837 Label pop_and_continue, exit;
2838
mads.s.ager31e71382008-08-13 09:32:07 +00002839 __ ldr(r0, MemOperand(sp, 0));
2840 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002841 // Avoid popping the result if it converts to 'true' using the
2842 // standard ToBoolean() conversion as described in ECMA-262,
2843 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002844 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845 Branch(true, &exit);
2846
2847 // Pop the result of evaluating the first part.
2848 __ bind(&pop_and_continue);
2849 __ pop(r0);
2850
2851 // Evaluate right side expression.
2852 __ bind(&is_false);
2853 Load(node->right());
2854
2855 // Exit (always with a materialized value).
2856 __ bind(&exit);
2857 }
2858
2859 } else {
2860 // Optimize for the case where (at least) one of the expressions
2861 // is a literal small integer.
2862 Literal* lliteral = node->left()->AsLiteral();
2863 Literal* rliteral = node->right()->AsLiteral();
2864
2865 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
2866 Load(node->left());
2867 SmiOperation(node->op(), rliteral->handle(), false);
2868
2869 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
2870 Load(node->right());
2871 SmiOperation(node->op(), lliteral->handle(), true);
2872
2873 } else {
2874 Load(node->left());
2875 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00002876 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877 }
mads.s.ager31e71382008-08-13 09:32:07 +00002878 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879 }
2880}
2881
2882
ager@chromium.org7c537e22008-10-16 08:43:32 +00002883void CodeGenerator::VisitThisFunction(ThisFunction* node) {
mads.s.ager31e71382008-08-13 09:32:07 +00002884 __ ldr(r0, FunctionOperand());
2885 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886}
2887
2888
ager@chromium.org7c537e22008-10-16 08:43:32 +00002889void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890 Comment cmnt(masm_, "[ CompareOperation");
2891
2892 // Get the expressions from the node.
2893 Expression* left = node->left();
2894 Expression* right = node->right();
2895 Token::Value op = node->op();
2896
2897 // NOTE: To make null checks efficient, we check if either left or
2898 // right is the literal 'null'. If so, we optimize the code by
2899 // inlining a null check instead of calling the (very) general
2900 // runtime routine for checking equality.
2901
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002903 bool left_is_null =
2904 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
2905 bool right_is_null =
2906 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002907 // The 'null' value is only equal to 'null' or 'undefined'.
2908 if (left_is_null || right_is_null) {
2909 Load(left_is_null ? right : left);
2910 Label exit, undetectable;
mads.s.ager31e71382008-08-13 09:32:07 +00002911 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 __ cmp(r0, Operand(Factory::null_value()));
2913
2914 // The 'null' value is only equal to 'undefined' if using
2915 // non-strict comparisons.
2916 if (op != Token::EQ_STRICT) {
2917 __ b(eq, &exit);
2918 __ cmp(r0, Operand(Factory::undefined_value()));
2919
2920 // NOTE: it can be undetectable object.
2921 __ b(eq, &exit);
2922 __ tst(r0, Operand(kSmiTagMask));
2923
2924 __ b(ne, &undetectable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925 __ b(false_target());
2926
2927 __ bind(&undetectable);
2928 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2929 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
2930 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
2931 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
2932 }
2933
2934 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935
2936 cc_reg_ = eq;
2937 return;
2938 }
2939 }
2940
2941
2942 // NOTE: To make typeof testing for natives implemented in
2943 // JavaScript really efficient, we generate special code for
2944 // expressions of the form: 'typeof <expression> == <string>'.
2945
2946 UnaryOperation* operation = left->AsUnaryOperation();
2947 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
2948 (operation != NULL && operation->op() == Token::TYPEOF) &&
2949 (right->AsLiteral() != NULL &&
2950 right->AsLiteral()->handle()->IsString())) {
2951 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
2952
mads.s.ager31e71382008-08-13 09:32:07 +00002953 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954 LoadTypeofExpression(operation->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00002955 __ pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002956
2957 if (check->Equals(Heap::number_symbol())) {
2958 __ tst(r1, Operand(kSmiTagMask));
2959 __ b(eq, true_target());
2960 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2961 __ cmp(r1, Operand(Factory::heap_number_map()));
2962 cc_reg_ = eq;
2963
2964 } else if (check->Equals(Heap::string_symbol())) {
2965 __ tst(r1, Operand(kSmiTagMask));
2966 __ b(eq, false_target());
2967
2968 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2969
2970 // NOTE: it might be an undetectable string object
2971 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
2972 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
2973 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
2974 __ b(eq, false_target());
2975
2976 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2977 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
2978 cc_reg_ = lt;
2979
2980 } else if (check->Equals(Heap::boolean_symbol())) {
2981 __ cmp(r1, Operand(Factory::true_value()));
2982 __ b(eq, true_target());
2983 __ cmp(r1, Operand(Factory::false_value()));
2984 cc_reg_ = eq;
2985
2986 } else if (check->Equals(Heap::undefined_symbol())) {
2987 __ cmp(r1, Operand(Factory::undefined_value()));
2988 __ b(eq, true_target());
2989
2990 __ tst(r1, Operand(kSmiTagMask));
2991 __ b(eq, false_target());
2992
2993 // NOTE: it can be undetectable object.
2994 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2995 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
2996 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
2997 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
2998
2999 cc_reg_ = eq;
3000
3001 } else if (check->Equals(Heap::function_symbol())) {
3002 __ tst(r1, Operand(kSmiTagMask));
3003 __ b(eq, false_target());
3004 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3005 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3006 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3007 cc_reg_ = eq;
3008
3009 } else if (check->Equals(Heap::object_symbol())) {
3010 __ tst(r1, Operand(kSmiTagMask));
3011 __ b(eq, false_target());
3012
3013 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3014 __ cmp(r1, Operand(Factory::null_value()));
3015 __ b(eq, true_target());
3016
3017 // NOTE: it might be an undetectable object.
3018 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3019 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3020 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3021 __ b(eq, false_target());
3022
3023 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3024 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3025 __ b(lt, false_target());
3026 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3027 cc_reg_ = le;
3028
3029 } else {
3030 // Uncommon case: Typeof testing against a string literal that
3031 // is never returned from the typeof operator.
3032 __ b(false_target());
3033 }
3034 return;
3035 }
3036
3037 Load(left);
3038 Load(right);
3039 switch (op) {
3040 case Token::EQ:
3041 Comparison(eq, false);
3042 break;
3043
3044 case Token::LT:
3045 Comparison(lt);
3046 break;
3047
3048 case Token::GT:
3049 Comparison(gt);
3050 break;
3051
3052 case Token::LTE:
3053 Comparison(le);
3054 break;
3055
3056 case Token::GTE:
3057 Comparison(ge);
3058 break;
3059
3060 case Token::EQ_STRICT:
3061 Comparison(eq, true);
3062 break;
3063
3064 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003065 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003066 __ InvokeBuiltin(Builtins::IN, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00003067 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 break;
3069
3070 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003071 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003072 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003073 __ tst(r0, Operand(r0));
3074 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075 break;
3076
3077 default:
3078 UNREACHABLE();
3079 }
3080}
3081
3082
ager@chromium.org7c537e22008-10-16 08:43:32 +00003083void CodeGenerator::RecordStatementPosition(Node* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 if (FLAG_debug_info) {
3085 int statement_pos = node->statement_pos();
ager@chromium.org236ad962008-09-25 09:45:57 +00003086 if (statement_pos == RelocInfo::kNoPosition) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087 __ RecordStatementPosition(statement_pos);
3088 }
3089}
3090
3091
ager@chromium.org7c537e22008-10-16 08:43:32 +00003092void CodeGenerator::EnterJSFrame() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003093#if defined(DEBUG)
3094 { Label done, fail;
3095 __ tst(r1, Operand(kSmiTagMask));
3096 __ b(eq, &fail);
3097 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3098 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3099 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
3100 __ b(eq, &done);
3101 __ bind(&fail);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003102 __ stop("CodeGenerator::EnterJSFrame - r1 not a function");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003103 __ bind(&done);
3104 }
3105#endif // DEBUG
3106
3107 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
3108 __ add(fp, sp, Operand(2 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109}
3110
3111
ager@chromium.org7c537e22008-10-16 08:43:32 +00003112void CodeGenerator::ExitJSFrame() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003113 // Drop the execution stack down to the frame pointer and restore the caller
3114 // frame pointer and return address.
3115 __ mov(sp, fp);
3116 __ ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117}
3118
3119
3120#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003121#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003122
ager@chromium.org7c537e22008-10-16 08:43:32 +00003123Handle<String> Reference::GetName() {
3124 ASSERT(type_ == NAMED);
3125 Property* property = expression_->AsProperty();
3126 if (property == NULL) {
3127 // Global variable reference treated as a named property reference.
3128 VariableProxy* proxy = expression_->AsVariableProxy();
3129 ASSERT(proxy->AsVariable() != NULL);
3130 ASSERT(proxy->AsVariable()->is_global());
3131 return proxy->name();
3132 } else {
3133 Literal* raw_name = property->key()->AsLiteral();
3134 ASSERT(raw_name != NULL);
3135 return Handle<String>(String::cast(*raw_name->handle()));
3136 }
3137}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003138
ager@chromium.org7c537e22008-10-16 08:43:32 +00003139
3140void Reference::GetValue(TypeofState typeof_state) {
3141 ASSERT(!is_illegal());
3142 ASSERT(!cgen_->has_cc());
3143 MacroAssembler* masm = cgen_->masm();
3144 Property* property = expression_->AsProperty();
3145 if (property != NULL) {
3146 __ RecordPosition(property->position());
3147 }
3148
3149 switch (type_) {
3150 case SLOT: {
3151 Comment cmnt(masm, "[ Load from Slot");
3152 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3153 ASSERT(slot != NULL);
3154 cgen_->LoadFromSlot(slot, typeof_state);
3155 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003156 }
3157
ager@chromium.org7c537e22008-10-16 08:43:32 +00003158 case NAMED: {
3159 // TODO(1241834): Make sure that this it is safe to ignore the
3160 // distinction between expressions in a typeof and not in a typeof. If
3161 // there is a chance that reference errors can be thrown below, we
3162 // must distinguish between the two kinds of loads (typeof expression
3163 // loads must not throw a reference error).
3164 Comment cmnt(masm, "[ Load from named Property");
3165 // Setup the name register.
3166 Handle<String> name(GetName());
3167 __ mov(r2, Operand(name));
3168 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3169
3170 Variable* var = expression_->AsVariableProxy()->AsVariable();
3171 if (var != NULL) {
3172 ASSERT(var->is_global());
3173 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3174 } else {
3175 __ Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003176 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003177 __ push(r0);
3178 break;
3179 }
3180
3181 case KEYED: {
3182 // TODO(1241834): Make sure that this it is safe to ignore the
3183 // distinction between expressions in a typeof and not in a typeof.
3184 Comment cmnt(masm, "[ Load from keyed Property");
3185 ASSERT(property != NULL);
3186 // TODO(1224671): Implement inline caching for keyed loads as on ia32.
3187 GetPropertyStub stub;
3188 __ CallStub(&stub);
3189 __ push(r0);
3190 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003191 }
3192
3193 default:
3194 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003195 }
3196}
3197
3198
ager@chromium.org7c537e22008-10-16 08:43:32 +00003199void Reference::SetValue(InitState init_state) {
3200 ASSERT(!is_illegal());
3201 ASSERT(!cgen_->has_cc());
3202 MacroAssembler* masm = cgen_->masm();
3203 Property* property = expression_->AsProperty();
3204 if (property != NULL) {
3205 __ RecordPosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003206 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003207
ager@chromium.org7c537e22008-10-16 08:43:32 +00003208 switch (type_) {
3209 case SLOT: {
3210 Comment cmnt(masm, "[ Store to Slot");
3211 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3212 ASSERT(slot != NULL);
3213 if (slot->type() == Slot::LOOKUP) {
3214 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003215
ager@chromium.org7c537e22008-10-16 08:43:32 +00003216 // For now, just do a runtime call.
3217 __ push(cp);
3218 __ mov(r0, Operand(slot->var()->name()));
3219 __ push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003220
ager@chromium.org7c537e22008-10-16 08:43:32 +00003221 if (init_state == CONST_INIT) {
3222 // Same as the case for a normal store, but ignores attribute
3223 // (e.g. READ_ONLY) of context slot so that we can initialize
3224 // const properties (introduced via eval("const foo = (some
3225 // expr);")). Also, uses the current function context instead of
3226 // the top context.
3227 //
3228 // Note that we must declare the foo upon entry of eval(), via a
3229 // context slot declaration, but we cannot initialize it at the
3230 // same time, because the const declaration may be at the end of
3231 // the eval code (sigh...) and the const variable may have been
3232 // used before (where its value is 'undefined'). Thus, we can only
3233 // do the initialization when we actually encounter the expression
3234 // and when the expression operands are defined and valid, and
3235 // thus we need the split into 2 operations: declaration of the
3236 // context slot followed by initialization.
3237 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3238 } else {
3239 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3240 }
3241 // Storing a variable must keep the (new) value on the expression
3242 // stack. This is necessary for compiling assignment expressions.
3243 __ push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003244
ager@chromium.org7c537e22008-10-16 08:43:32 +00003245 } else {
3246 ASSERT(slot->var()->mode() != Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003247
ager@chromium.org7c537e22008-10-16 08:43:32 +00003248 Label exit;
3249 if (init_state == CONST_INIT) {
3250 ASSERT(slot->var()->mode() == Variable::CONST);
3251 // Only the first const initialization must be executed (the slot
3252 // still contains 'the hole' value). When the assignment is
3253 // executed, the code is identical to a normal store (see below).
3254 Comment cmnt(masm, "[ Init const");
3255 __ ldr(r2, cgen_->SlotOperand(slot, r2));
3256 __ cmp(r2, Operand(Factory::the_hole_value()));
3257 __ b(ne, &exit);
3258 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003259
ager@chromium.org7c537e22008-10-16 08:43:32 +00003260 // We must execute the store. Storing a variable must keep the
3261 // (new) value on the stack. This is necessary for compiling
3262 // assignment expressions.
3263 //
3264 // Note: We will reach here even with slot->var()->mode() ==
3265 // Variable::CONST because of const declarations which will
3266 // initialize consts to 'the hole' value and by doing so, end up
3267 // calling this code. r2 may be loaded with context; used below in
3268 // RecordWrite.
3269 __ pop(r0);
3270 __ str(r0, cgen_->SlotOperand(slot, r2));
3271 __ push(r0);
3272 if (slot->type() == Slot::CONTEXT) {
3273 // Skip write barrier if the written value is a smi.
3274 __ tst(r0, Operand(kSmiTagMask));
3275 __ b(eq, &exit);
3276 // r2 is loaded with context when calling SlotOperand above.
3277 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3278 __ mov(r3, Operand(offset));
3279 __ RecordWrite(r2, r3, r1);
3280 }
3281 // If we definitely did not jump over the assignment, we do not need
3282 // to bind the exit label. Doing so can defeat peephole
3283 // optimization.
3284 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3285 __ bind(&exit);
3286 }
3287 }
3288 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003289 }
3290
ager@chromium.org7c537e22008-10-16 08:43:32 +00003291 case NAMED: {
3292 Comment cmnt(masm, "[ Store to named Property");
3293 // Call the appropriate IC code.
3294 __ pop(r0); // value
3295 // Setup the name register.
3296 Handle<String> name(GetName());
3297 __ mov(r2, Operand(name));
3298 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3299 __ Call(ic, RelocInfo::CODE_TARGET);
3300 __ push(r0);
3301 break;
3302 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003303
ager@chromium.org7c537e22008-10-16 08:43:32 +00003304 case KEYED: {
3305 Comment cmnt(masm, "[ Store to keyed Property");
3306 Property* property = expression_->AsProperty();
3307 ASSERT(property != NULL);
3308 __ RecordPosition(property->position());
3309 __ pop(r0); // value
3310 SetPropertyStub stub;
3311 __ CallStub(&stub);
3312 __ push(r0);
3313 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003314 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003315
3316 default:
3317 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003318 }
3319}
3320
3321
3322void GetPropertyStub::Generate(MacroAssembler* masm) {
3323 // sp[0]: key
3324 // sp[1]: receiver
3325 Label slow, fast;
3326 // Get the key and receiver object from the stack.
3327 __ ldm(ia, sp, r0.bit() | r1.bit());
3328 // Check that the key is a smi.
3329 __ tst(r0, Operand(kSmiTagMask));
3330 __ b(ne, &slow);
3331 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3332 // Check that the object isn't a smi.
3333 __ tst(r1, Operand(kSmiTagMask));
3334 __ b(eq, &slow);
3335
3336 // Check that the object is some kind of JS object EXCEPT JS Value type.
3337 // In the case that the object is a value-wrapper object,
3338 // we enter the runtime system to make sure that indexing into string
3339 // objects work as intended.
3340 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3341 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3342 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3343 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3344 __ b(lt, &slow);
3345
3346 // Get the elements array of the object.
3347 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3348 // Check that the object is in fast mode (not dictionary).
3349 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3350 __ cmp(r3, Operand(Factory::hash_table_map()));
3351 __ b(eq, &slow);
3352 // Check that the key (index) is within bounds.
3353 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3354 __ cmp(r0, Operand(r3));
3355 __ b(lo, &fast);
3356
3357 // Slow case: Push extra copies of the arguments (2).
3358 __ bind(&slow);
3359 __ ldm(ia, sp, r0.bit() | r1.bit());
3360 __ stm(db_w, sp, r0.bit() | r1.bit());
3361 // Do tail-call to runtime routine.
3362 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3363
3364 // Fast case: Do the load.
3365 __ bind(&fast);
3366 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3367 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3368 __ cmp(r0, Operand(Factory::the_hole_value()));
3369 // In case the loaded value is the_hole we have to consult GetProperty
3370 // to ensure the prototype chain is searched.
3371 __ b(eq, &slow);
3372
3373 __ StubReturn(1);
3374}
3375
3376
3377void SetPropertyStub::Generate(MacroAssembler* masm) {
3378 // r0 : value
3379 // sp[0] : key
3380 // sp[1] : receiver
3381
3382 Label slow, fast, array, extra, exit;
3383 // Get the key and the object from the stack.
3384 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3385 // Check that the key is a smi.
3386 __ tst(r1, Operand(kSmiTagMask));
3387 __ b(ne, &slow);
3388 // Check that the object isn't a smi.
3389 __ tst(r3, Operand(kSmiTagMask));
3390 __ b(eq, &slow);
3391 // Get the type of the object from its map.
3392 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3393 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3394 // Check if the object is a JS array or not.
3395 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3396 __ b(eq, &array);
3397 // Check that the object is some kind of JS object.
3398 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3399 __ b(lt, &slow);
3400
3401
3402 // Object case: Check key against length in the elements array.
3403 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3404 // Check that the object is in fast mode (not dictionary).
3405 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3406 __ cmp(r2, Operand(Factory::hash_table_map()));
3407 __ b(eq, &slow);
3408 // Untag the key (for checking against untagged length in the fixed array).
3409 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3410 // Compute address to store into and check array bounds.
3411 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3412 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3413 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3414 __ cmp(r1, Operand(ip));
3415 __ b(lo, &fast);
3416
3417
3418 // Slow case: Push extra copies of the arguments (3).
3419 __ bind(&slow);
3420 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3421 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3422 // Do tail-call to runtime routine.
3423 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3424
3425
3426 // Extra capacity case: Check if there is extra capacity to
3427 // perform the store and update the length. Used for adding one
3428 // element to the array by writing to array[array.length].
3429 // r0 == value, r1 == key, r2 == elements, r3 == object
3430 __ bind(&extra);
3431 __ b(ne, &slow); // do not leave holes in the array
3432 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3433 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3434 __ cmp(r1, Operand(ip));
3435 __ b(hs, &slow);
3436 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3437 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3438 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3439 __ mov(r3, Operand(r2));
3440 // NOTE: Computing the address to store into must take the fact
3441 // that the key has been incremented into account.
3442 int displacement = Array::kHeaderSize - kHeapObjectTag -
3443 ((1 << kSmiTagSize) * 2);
3444 __ add(r2, r2, Operand(displacement));
3445 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3446 __ b(&fast);
3447
3448
3449 // Array case: Get the length and the elements array from the JS
3450 // array. Check that the array is in fast mode; if it is the
3451 // length is always a smi.
3452 // r0 == value, r3 == object
3453 __ bind(&array);
3454 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3455 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3456 __ cmp(r1, Operand(Factory::hash_table_map()));
3457 __ b(eq, &slow);
3458
3459 // Check the key against the length in the array, compute the
3460 // address to store into and fall through to fast case.
3461 __ ldr(r1, MemOperand(sp));
3462 // r0 == value, r1 == key, r2 == elements, r3 == object.
3463 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3464 __ cmp(r1, Operand(ip));
3465 __ b(hs, &extra);
3466 __ mov(r3, Operand(r2));
3467 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3468 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3469
3470
3471 // Fast case: Do the store.
3472 // r0 == value, r2 == address to store into, r3 == elements
3473 __ bind(&fast);
3474 __ str(r0, MemOperand(r2));
3475 // Skip write barrier if the written value is a smi.
3476 __ tst(r0, Operand(kSmiTagMask));
3477 __ b(eq, &exit);
3478 // Update write barrier for the elements array address.
3479 __ sub(r1, r2, Operand(r3));
3480 __ RecordWrite(r3, r1, r2);
3481 __ bind(&exit);
3482 __ StubReturn(1);
3483}
3484
3485
3486void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3487 // r1 : x
3488 // r0 : y
3489 // result : r0
3490
3491 switch (op_) {
3492 case Token::ADD: {
3493 Label slow, exit;
3494 // fast path
3495 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3496 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3497 // go slow-path in case of overflow
3498 __ b(vs, &slow);
3499 // go slow-path in case of non-smi operands
3500 ASSERT(kSmiTag == 0); // adjust code below
3501 __ tst(r2, Operand(kSmiTagMask));
3502 __ b(eq, &exit);
3503 // slow path
3504 __ bind(&slow);
3505 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3506 __ push(r1);
3507 __ push(r0);
3508 __ mov(r0, Operand(1)); // set number of arguments
3509 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3510 // done
3511 __ bind(&exit);
3512 break;
3513 }
3514
3515 case Token::SUB: {
3516 Label slow, exit;
3517 // fast path
3518 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3519 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3520 // go slow-path in case of overflow
3521 __ b(vs, &slow);
3522 // go slow-path in case of non-smi operands
3523 ASSERT(kSmiTag == 0); // adjust code below
3524 __ tst(r2, Operand(kSmiTagMask));
3525 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3526 __ b(eq, &exit);
3527 // slow path
3528 __ bind(&slow);
3529 __ push(r1);
3530 __ push(r0);
3531 __ mov(r0, Operand(1)); // set number of arguments
3532 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3533 // done
3534 __ bind(&exit);
3535 break;
3536 }
3537
3538 case Token::MUL: {
3539 Label slow, exit;
3540 // tag check
3541 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3542 ASSERT(kSmiTag == 0); // adjust code below
3543 __ tst(r2, Operand(kSmiTagMask));
3544 __ b(ne, &slow);
3545 // remove tag from one operand (but keep sign), so that result is smi
3546 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3547 // do multiplication
3548 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3549 // go slow on overflows (overflow bit is not set)
3550 __ mov(ip, Operand(r3, ASR, 31));
3551 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3552 __ b(ne, &slow);
3553 // go slow on zero result to handle -0
3554 __ tst(r3, Operand(r3));
3555 __ mov(r0, Operand(r3), LeaveCC, ne);
3556 __ b(ne, &exit);
3557 // slow case
3558 __ bind(&slow);
3559 __ push(r1);
3560 __ push(r0);
3561 __ mov(r0, Operand(1)); // set number of arguments
3562 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3563 // done
3564 __ bind(&exit);
3565 break;
3566 }
3567
3568 case Token::BIT_OR:
3569 case Token::BIT_AND:
3570 case Token::BIT_XOR: {
3571 Label slow, exit;
3572 // tag check
3573 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3574 ASSERT(kSmiTag == 0); // adjust code below
3575 __ tst(r2, Operand(kSmiTagMask));
3576 __ b(ne, &slow);
3577 switch (op_) {
3578 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3579 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3580 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3581 default: UNREACHABLE();
3582 }
3583 __ b(&exit);
3584 __ bind(&slow);
3585 __ push(r1); // restore stack
3586 __ push(r0);
3587 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3588 switch (op_) {
3589 case Token::BIT_OR:
3590 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3591 break;
3592 case Token::BIT_AND:
3593 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3594 break;
3595 case Token::BIT_XOR:
3596 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3597 break;
3598 default:
3599 UNREACHABLE();
3600 }
3601 __ bind(&exit);
3602 break;
3603 }
3604
3605 case Token::SHL:
3606 case Token::SHR:
3607 case Token::SAR: {
3608 Label slow, exit;
3609 // tag check
3610 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3611 ASSERT(kSmiTag == 0); // adjust code below
3612 __ tst(r2, Operand(kSmiTagMask));
3613 __ b(ne, &slow);
3614 // remove tags from operands (but keep sign)
3615 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3616 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3617 // use only the 5 least significant bits of the shift count
3618 __ and_(r2, r2, Operand(0x1f));
3619 // perform operation
3620 switch (op_) {
3621 case Token::SAR:
3622 __ mov(r3, Operand(r3, ASR, r2));
3623 // no checks of result necessary
3624 break;
3625
3626 case Token::SHR:
3627 __ mov(r3, Operand(r3, LSR, r2));
3628 // check that the *unsigned* result fits in a smi
3629 // neither of the two high-order bits can be set:
3630 // - 0x80000000: high bit would be lost when smi tagging
3631 // - 0x40000000: this number would convert to negative when
3632 // smi tagging these two cases can only happen with shifts
3633 // by 0 or 1 when handed a valid smi
3634 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3635 __ b(ne, &slow);
3636 break;
3637
3638 case Token::SHL:
3639 __ mov(r3, Operand(r3, LSL, r2));
3640 // check that the *signed* result fits in a smi
3641 __ add(r2, r3, Operand(0x40000000), SetCC);
3642 __ b(mi, &slow);
3643 break;
3644
3645 default: UNREACHABLE();
3646 }
3647 // tag result and store it in r0
3648 ASSERT(kSmiTag == 0); // adjust code below
3649 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3650 __ b(&exit);
3651 // slow case
3652 __ bind(&slow);
3653 __ push(r1); // restore stack
3654 __ push(r0);
3655 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3656 switch (op_) {
3657 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3658 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3659 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3660 default: UNREACHABLE();
3661 }
3662 __ bind(&exit);
3663 break;
3664 }
3665
3666 default: UNREACHABLE();
3667 }
3668 __ Ret();
3669}
3670
3671
3672void StackCheckStub::Generate(MacroAssembler* masm) {
3673 Label within_limit;
3674 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3675 __ ldr(ip, MemOperand(ip));
3676 __ cmp(sp, Operand(ip));
3677 __ b(hs, &within_limit);
3678 // Do tail-call to runtime routine.
3679 __ push(r0);
3680 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
3681 __ bind(&within_limit);
3682
3683 __ StubReturn(1);
3684}
3685
3686
3687void UnarySubStub::Generate(MacroAssembler* masm) {
3688 Label undo;
3689 Label slow;
3690 Label done;
3691
3692 // Enter runtime system if the value is not a smi.
3693 __ tst(r0, Operand(kSmiTagMask));
3694 __ b(ne, &slow);
3695
3696 // Enter runtime system if the value of the expression is zero
3697 // to make sure that we switch between 0 and -0.
3698 __ cmp(r0, Operand(0));
3699 __ b(eq, &slow);
3700
3701 // The value of the expression is a smi that is not zero. Try
3702 // optimistic subtraction '0 - value'.
3703 __ rsb(r1, r0, Operand(0), SetCC);
3704 __ b(vs, &slow);
3705
3706 // If result is a smi we are done.
3707 __ tst(r1, Operand(kSmiTagMask));
3708 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
3709 __ b(eq, &done);
3710
3711 // Enter runtime system.
3712 __ bind(&slow);
3713 __ push(r0);
3714 __ mov(r0, Operand(0)); // set number of arguments
3715 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3716
3717 __ bind(&done);
3718 __ StubReturn(1);
3719}
3720
3721
3722void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
3723 __ push(r0);
3724 __ mov(r0, Operand(0)); // set number of arguments
3725 switch (kind_) {
3726 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
3727 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
3728 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
3729 default: UNREACHABLE();
3730 }
3731 __ StubReturn(argc_);
3732}
3733
3734
3735void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3736 // r0 holds exception
3737 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
3738 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3739 __ ldr(sp, MemOperand(r3));
3740 __ pop(r2); // pop next in chain
3741 __ str(r2, MemOperand(r3));
3742 // restore parameter- and frame-pointer and pop state.
3743 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
3744 // Before returning we restore the context from the frame pointer if not NULL.
3745 // The frame pointer is NULL in the exception handler of a JS entry frame.
3746 __ cmp(fp, Operand(0));
3747 // Set cp to NULL if fp is NULL.
3748 __ mov(cp, Operand(0), LeaveCC, eq);
3749 // Restore cp otherwise.
3750 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3751 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3752 __ pop(pc);
3753}
3754
3755
3756void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
3757 // Fetch top stack handler.
3758 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3759 __ ldr(r3, MemOperand(r3));
3760
3761 // Unwind the handlers until the ENTRY handler is found.
3762 Label loop, done;
3763 __ bind(&loop);
3764 // Load the type of the current stack handler.
3765 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
3766 StackHandlerConstants::kStateOffset;
3767 __ ldr(r2, MemOperand(r3, kStateOffset));
3768 __ cmp(r2, Operand(StackHandler::ENTRY));
3769 __ b(eq, &done);
3770 // Fetch the next handler in the list.
3771 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
3772 StackHandlerConstants::kNextOffset;
3773 __ ldr(r3, MemOperand(r3, kNextOffset));
3774 __ jmp(&loop);
3775 __ bind(&done);
3776
3777 // Set the top handler address to next handler past the current ENTRY handler.
3778 __ ldr(r0, MemOperand(r3, kNextOffset));
3779 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
3780 __ str(r0, MemOperand(r2));
3781
3782 // Set external caught exception to false.
3783 __ mov(r0, Operand(false));
3784 ExternalReference external_caught(Top::k_external_caught_exception_address);
3785 __ mov(r2, Operand(external_caught));
3786 __ str(r0, MemOperand(r2));
3787
3788 // Set pending exception and r0 to out of memory exception.
3789 Failure* out_of_memory = Failure::OutOfMemoryException();
3790 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3791 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
3792 __ str(r0, MemOperand(r2));
3793
3794 // Restore the stack to the address of the ENTRY handler
3795 __ mov(sp, Operand(r3));
3796
3797 // restore parameter- and frame-pointer and pop state.
3798 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
3799 // Before returning we restore the context from the frame pointer if not NULL.
3800 // The frame pointer is NULL in the exception handler of a JS entry frame.
3801 __ cmp(fp, Operand(0));
3802 // Set cp to NULL if fp is NULL.
3803 __ mov(cp, Operand(0), LeaveCC, eq);
3804 // Restore cp otherwise.
3805 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3806 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3807 __ pop(pc);
3808}
3809
3810
3811void CEntryStub::GenerateCore(MacroAssembler* masm,
3812 Label* throw_normal_exception,
3813 Label* throw_out_of_memory_exception,
3814 StackFrame::Type frame_type,
3815 bool do_gc) {
3816 // r0: result parameter for PerformGC, if any
3817 // r4: number of arguments including receiver (C callee-saved)
3818 // r5: pointer to builtin function (C callee-saved)
3819 // r6: pointer to the first argument (C callee-saved)
3820
3821 if (do_gc) {
3822 // Passing r0.
3823 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3824 }
3825
3826 // Call C built-in.
3827 // r0 = argc, r1 = argv
3828 __ mov(r0, Operand(r4));
3829 __ mov(r1, Operand(r6));
3830
3831 // TODO(1242173): To let the GC traverse the return address of the exit
3832 // frames, we need to know where the return address is. Right now,
3833 // we push it on the stack to be able to find it again, but we never
3834 // restore from it in case of changes, which makes it impossible to
3835 // support moving the C entry code stub. This should be fixed, but currently
3836 // this is OK because the CEntryStub gets generated so early in the V8 boot
3837 // sequence that it is not moving ever.
3838 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
3839 __ push(lr);
3840#if !defined(__arm__)
3841 // Notify the simulator of the transition to C code.
3842 __ swi(assembler::arm::call_rt_r5);
3843#else /* !defined(__arm__) */
3844 __ mov(pc, Operand(r5));
3845#endif /* !defined(__arm__) */
3846 // result is in r0 or r0:r1 - do not destroy these registers!
3847
3848 // check for failure result
3849 Label failure_returned;
3850 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3851 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
3852 __ add(r2, r0, Operand(1));
3853 __ tst(r2, Operand(kFailureTagMask));
3854 __ b(eq, &failure_returned);
3855
3856 // Exit C frame and return.
3857 // r0:r1: result
3858 // sp: stack pointer
3859 // fp: frame pointer
3860 // pp: caller's parameter pointer pp (restored as C callee-saved)
3861 __ LeaveExitFrame(frame_type);
3862
3863 // check if we should retry or throw exception
3864 Label retry;
3865 __ bind(&failure_returned);
3866 ASSERT(Failure::RETRY_AFTER_GC == 0);
3867 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3868 __ b(eq, &retry);
3869
3870 Label continue_exception;
3871 // If the returned failure is EXCEPTION then promote Top::pending_exception().
3872 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3873 __ b(ne, &continue_exception);
3874
3875 // Retrieve the pending exception and clear the variable.
3876 __ mov(ip, Operand(Factory::the_hole_value().location()));
3877 __ ldr(r3, MemOperand(ip));
3878 __ mov(ip, Operand(Top::pending_exception_address()));
3879 __ ldr(r0, MemOperand(ip));
3880 __ str(r3, MemOperand(ip));
3881
3882 __ bind(&continue_exception);
3883 // Special handling of out of memory exception.
3884 Failure* out_of_memory = Failure::OutOfMemoryException();
3885 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3886 __ b(eq, throw_out_of_memory_exception);
3887
3888 // Handle normal exception.
3889 __ jmp(throw_normal_exception);
3890
3891 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
3892}
3893
3894
3895void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
3896 // Called from JavaScript; parameters are on stack as if calling JS function
3897 // r0: number of arguments including receiver
3898 // r1: pointer to builtin function
3899 // fp: frame pointer (restored after C call)
3900 // sp: stack pointer (restored as callee's pp after C call)
3901 // cp: current context (C callee-saved)
3902 // pp: caller's parameter pointer pp (C callee-saved)
3903
3904 // NOTE: Invocations of builtins may return failure objects
3905 // instead of a proper result. The builtin entry handles
3906 // this by performing a garbage collection and retrying the
3907 // builtin once.
3908
3909 StackFrame::Type frame_type = is_debug_break
3910 ? StackFrame::EXIT_DEBUG
3911 : StackFrame::EXIT;
3912
3913 // Enter the exit frame that transitions from JavaScript to C++.
3914 __ EnterExitFrame(frame_type);
3915
3916 // r4: number of arguments (C callee-saved)
3917 // r5: pointer to builtin function (C callee-saved)
3918 // r6: pointer to first argument (C callee-saved)
3919
3920 Label throw_out_of_memory_exception;
3921 Label throw_normal_exception;
3922
3923#ifdef DEBUG
3924 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003925 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003926 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
3927 }
3928 GenerateCore(masm,
3929 &throw_normal_exception,
3930 &throw_out_of_memory_exception,
3931 frame_type,
3932 FLAG_gc_greedy);
3933#else
3934 GenerateCore(masm,
3935 &throw_normal_exception,
3936 &throw_out_of_memory_exception,
3937 frame_type,
3938 false);
3939#endif
3940 GenerateCore(masm,
3941 &throw_normal_exception,
3942 &throw_out_of_memory_exception,
3943 frame_type,
3944 true);
3945
3946 __ bind(&throw_out_of_memory_exception);
3947 GenerateThrowOutOfMemory(masm);
3948 // control flow for generated will not return.
3949
3950 __ bind(&throw_normal_exception);
3951 GenerateThrowTOS(masm);
3952}
3953
3954
3955void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3956 // r0: code entry
3957 // r1: function
3958 // r2: receiver
3959 // r3: argc
3960 // [sp+0]: argv
3961
3962 Label invoke, exit;
3963
3964 // Called from C, so do not pop argc and args on exit (preserve sp)
3965 // No need to save register-passed args
3966 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
3967 __ stm(db_w, sp, kCalleeSaved | lr.bit());
3968
3969 // Get address of argv, see stm above.
3970 // r0: code entry
3971 // r1: function
3972 // r2: receiver
3973 // r3: argc
3974 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
3975 __ ldr(r4, MemOperand(r4)); // argv
3976
3977 // Push a frame with special values setup to mark it as an entry frame.
3978 // r0: code entry
3979 // r1: function
3980 // r2: receiver
3981 // r3: argc
3982 // r4: argv
3983 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3984 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
3985 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
3986 __ mov(r6, Operand(Smi::FromInt(marker)));
3987 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
3988 __ ldr(r5, MemOperand(r5));
3989 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
3990
3991 // Setup frame pointer for the frame to be pushed.
3992 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3993
3994 // Call a faked try-block that does the invoke.
3995 __ bl(&invoke);
3996
3997 // Caught exception: Store result (exception) in the pending
3998 // exception field in the JSEnv and return a failure sentinel.
3999 // Coming in here the fp will be invalid because the PushTryHandler below
4000 // sets it to 0 to signal the existence of the JSEntry frame.
4001 __ mov(ip, Operand(Top::pending_exception_address()));
4002 __ str(r0, MemOperand(ip));
4003 __ mov(r0, Operand(Handle<Failure>(Failure::Exception())));
4004 __ b(&exit);
4005
4006 // Invoke: Link this frame into the handler chain.
4007 __ bind(&invoke);
4008 // Must preserve r0-r4, r5-r7 are available.
4009 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4010 // If an exception not caught by another handler occurs, this handler returns
4011 // control to the code after the bl(&invoke) above, which restores all
4012 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4013 // before returning a failure to C.
4014
4015 // Clear any pending exceptions.
4016 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4017 __ ldr(r5, MemOperand(ip));
4018 __ mov(ip, Operand(Top::pending_exception_address()));
4019 __ str(r5, MemOperand(ip));
4020
4021 // Invoke the function by calling through JS entry trampoline builtin.
4022 // Notice that we cannot store a reference to the trampoline code directly in
4023 // this stub, because runtime stubs are not traversed when doing GC.
4024
4025 // Expected registers by Builtins::JSEntryTrampoline
4026 // r0: code entry
4027 // r1: function
4028 // r2: receiver
4029 // r3: argc
4030 // r4: argv
4031 if (is_construct) {
4032 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4033 __ mov(ip, Operand(construct_entry));
4034 } else {
4035 ExternalReference entry(Builtins::JSEntryTrampoline);
4036 __ mov(ip, Operand(entry));
4037 }
4038 __ ldr(ip, MemOperand(ip)); // deref address
4039
4040 // Branch and link to JSEntryTrampoline
4041 __ mov(lr, Operand(pc));
4042 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4043
4044 // Unlink this frame from the handler chain. When reading the
4045 // address of the next handler, there is no need to use the address
4046 // displacement since the current stack pointer (sp) points directly
4047 // to the stack handler.
4048 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4049 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4050 __ str(r3, MemOperand(ip));
4051 // No need to restore registers
4052 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4053
4054 __ bind(&exit); // r0 holds result
4055 // Restore the top frame descriptors from the stack.
4056 __ pop(r3);
4057 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4058 __ str(r3, MemOperand(ip));
4059
4060 // Reset the stack to the callee saved registers.
4061 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4062
4063 // Restore callee-saved registers and return.
4064#ifdef DEBUG
4065 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4066#endif
4067 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4068}
4069
4070
ager@chromium.org7c537e22008-10-16 08:43:32 +00004071void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004072 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004073 Label adaptor;
4074 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4075 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4076 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004077 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004078
ager@chromium.org7c537e22008-10-16 08:43:32 +00004079 // Nothing to do: The formal number of parameters has already been
4080 // passed in register r0 by calling function. Just return it.
4081 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004082
ager@chromium.org7c537e22008-10-16 08:43:32 +00004083 // Arguments adaptor case: Read the arguments length from the
4084 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004085 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004086 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004087 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004088}
4089
4090
ager@chromium.org7c537e22008-10-16 08:43:32 +00004091void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4092 // The displacement is the offset of the last parameter (if any)
4093 // relative to the frame pointer.
4094 static const int kDisplacement =
4095 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004096
ager@chromium.org7c537e22008-10-16 08:43:32 +00004097 // Check that the key is a smi.
4098 Label slow;
4099 __ tst(r1, Operand(kSmiTagMask));
4100 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004101
ager@chromium.org7c537e22008-10-16 08:43:32 +00004102 // Check if the calling frame is an arguments adaptor frame.
4103 Label adaptor;
4104 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4105 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4106 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4107 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004108
ager@chromium.org7c537e22008-10-16 08:43:32 +00004109 // Check index against formal parameters count limit passed in
4110 // through register eax. Use unsigned comparison to get negative
4111 // check for free.
4112 __ cmp(r1, r0);
4113 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004114
ager@chromium.org7c537e22008-10-16 08:43:32 +00004115 // Read the argument from the stack and return it.
4116 __ sub(r3, r0, r1);
4117 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4118 __ ldr(r0, MemOperand(r3, kDisplacement));
4119 __ mov(pc, lr);
4120
4121 // Arguments adaptor case: Check index against actual arguments
4122 // limit found in the arguments adaptor frame. Use unsigned
4123 // comparison to get negative check for free.
4124 __ bind(&adaptor);
4125 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4126 __ cmp(r1, r0);
4127 __ b(cs, &slow);
4128
4129 // Read the argument from the adaptor frame and return it.
4130 __ sub(r3, r0, r1);
4131 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4132 __ ldr(r0, MemOperand(r3, kDisplacement));
4133 __ mov(pc, lr);
4134
4135 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4136 // by calling the runtime system.
4137 __ bind(&slow);
4138 __ push(r1);
4139 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4140}
4141
4142
4143void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4144 // Check if the calling frame is an arguments adaptor frame.
4145 Label runtime;
4146 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4147 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4148 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4149 __ b(ne, &runtime);
4150
4151 // Patch the arguments.length and the parameters pointer.
4152 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4153 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4154 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4155 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4156 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4157
4158 // Do the runtime call to allocate the arguments object.
4159 __ bind(&runtime);
4160 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004161}
4162
4163
4164void CallFunctionStub::Generate(MacroAssembler* masm) {
4165 Label slow;
4166 // Get the function to call from the stack.
4167 // function, receiver [, arguments]
4168 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4169
4170 // Check that the function is really a JavaScript function.
4171 // r1: pushed function (to be verified)
4172 __ tst(r1, Operand(kSmiTagMask));
4173 __ b(eq, &slow);
4174 // Get the map of the function object.
4175 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4176 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4177 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4178 __ b(ne, &slow);
4179
4180 // Fast-case: Invoke the function now.
4181 // r1: pushed function
4182 ParameterCount actual(argc_);
4183 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4184
4185 // Slow-case: Non-function called.
4186 __ bind(&slow);
4187 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
4188 __ InvokeBuiltin(Builtins::CALL_NON_FUNCTION, JUMP_JS);
4189}
4190
4191
4192#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004194} } // namespace v8::internal