blob: 9a17746fc75b50cc2d98dfc9f5af151373d073f2 [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"
33#include "prettyprinter.h"
34#include "scopeinfo.h"
35#include "scopes.h"
36#include "runtime.h"
37
38namespace v8 { namespace internal {
39
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040class ArmCodeGenerator;
41
42
43// -----------------------------------------------------------------------------
44// Reference support
45
46// A reference is a C++ stack-allocated object that keeps an ECMA
47// reference on the execution stack while in scope. For variables
48// the reference is empty, indicating that it isn't necessary to
49// store state on the stack for keeping track of references to those.
50// For properties, we keep either one (named) or two (indexed) values
51// on the execution stack to represent the reference.
52
53class Reference BASE_EMBEDDED {
54 public:
55 enum Type { ILLEGAL = -1, EMPTY = 0, NAMED = 1, KEYED = 2 };
56 Reference(ArmCodeGenerator* cgen, Expression* expression);
57 ~Reference();
58
59 Expression* expression() const { return expression_; }
60 Type type() const { return type_; }
61 void set_type(Type value) {
62 ASSERT(type_ == ILLEGAL);
63 type_ = value;
64 }
65 int size() const { return type_; }
66
67 bool is_illegal() const { return type_ == ILLEGAL; }
68
69 private:
70 ArmCodeGenerator* cgen_;
71 Expression* expression_;
72 Type type_;
73};
74
75
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000076// -------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077// Code generation state
78
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000079// The state is passed down the AST by the code generator. It is passed
80// implicitly (in a member variable) to the non-static code generator member
81// functions, and explicitly (as an argument) to the static member functions
82// and the AST node member functions.
83//
84// The state is threaded through the call stack. Constructing a state
85// implicitly pushes it on the owning code generator's stack of states, and
86// destroying one implicitly pops it.
87
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088class CodeGenState BASE_EMBEDDED {
89 public:
90 enum AccessType {
91 UNDEFINED,
92 LOAD,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000093 LOAD_TYPEOF_EXPR
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 };
95
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 // Create an initial code generator state. Destroying the initial state
97 // leaves the code generator with a NULL state.
98 explicit CodeGenState(ArmCodeGenerator* owner);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000100 // Create a code generator state based on a code generator's current
101 // state. The new state has its own access type and pair of branch
102 // labels, and no reference.
103 CodeGenState(ArmCodeGenerator* owner,
104 AccessType access,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 Label* true_target,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000106 Label* false_target);
107
108 // Create a code generator state based on a code generator's current
109 // state. The new state has an access type of LOAD, its own reference,
110 // and inherits the pair of branch labels of the current state.
111 CodeGenState(ArmCodeGenerator* owner, Reference* ref);
112
113 // Destroy a code generator state and restore the owning code generator's
114 // previous state.
115 ~CodeGenState();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
117 AccessType access() const { return access_; }
118 Reference* ref() const { return ref_; }
119 Label* true_target() const { return true_target_; }
120 Label* false_target() const { return false_target_; }
121
122 private:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000123 ArmCodeGenerator* owner_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 AccessType access_;
125 Reference* ref_;
126 Label* true_target_;
127 Label* false_target_;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000128 CodeGenState* previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129};
130
131
132// -----------------------------------------------------------------------------
133// ArmCodeGenerator
134
135class ArmCodeGenerator: public CodeGenerator {
136 public:
137 static Handle<Code> MakeCode(FunctionLiteral* fun,
138 Handle<Script> script,
139 bool is_eval);
140
141 MacroAssembler* masm() { return masm_; }
142
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000143 CodeGenState* state() { return state_; }
144 void set_state(CodeGenState* state) { state_ = state; }
145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 private:
147 // Assembler
148 MacroAssembler* masm_; // to generate code
149
150 // Code generation state
151 Scope* scope_;
152 Condition cc_reg_;
153 CodeGenState* state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 int break_stack_height_;
155
156 // Labels
157 Label function_return_;
158
159 // Construction/destruction
160 ArmCodeGenerator(int buffer_size,
161 Handle<Script> script,
162 bool is_eval);
163
164 virtual ~ArmCodeGenerator() { delete masm_; }
165
166 // Main code generation function
167 void GenCode(FunctionLiteral* fun);
168
169 // The following are used by class Reference.
170 void LoadReference(Reference* ref);
171 void UnloadReference(Reference* ref);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
173 // State
174 bool has_cc() const { return cc_reg_ != al; }
175 CodeGenState::AccessType access() const { return state_->access(); }
176 Reference* ref() const { return state_->ref(); }
177 bool is_referenced() const { return state_->ref() != NULL; }
178 Label* true_target() const { return state_->true_target(); }
179 Label* false_target() const { return state_->false_target(); }
180
181
182 // Expressions
183 MemOperand GlobalObject() const {
184 return ContextOperand(cp, Context::GLOBAL_INDEX);
185 }
186
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000187 static MemOperand ContextOperand(Register context, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 return MemOperand(context, Context::SlotOffset(index));
189 }
190
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000191 static MemOperand ParameterOperand(Scope* scope, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 // index -2 corresponds to the activated closure, -1 corresponds
193 // to the receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000194 ASSERT(-2 <= index && index < scope->num_parameters());
195 int offset = (1 + scope->num_parameters() - index) * kPointerSize;
196 return MemOperand(fp, offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 }
198
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000199 MemOperand ParameterOperand(int index) const {
200 return ParameterOperand(scope_, index);
201 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000203 MemOperand FunctionOperand() const {
204 return MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset);
205 }
206
207 static MemOperand SlotOperand(MacroAssembler* masm,
208 Scope* scope,
209 Slot* slot,
210 Register tmp);
211
212 MemOperand SlotOperand(Slot* slot, Register tmp) {
213 return SlotOperand(masm_, scope_, slot, tmp);
214 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
216 void LoadCondition(Expression* x, CodeGenState::AccessType access,
217 Label* true_target, Label* false_target, bool force_cc);
218 void Load(Expression* x,
219 CodeGenState::AccessType access = CodeGenState::LOAD);
220 void LoadGlobal();
221
222 // Special code for typeof expressions: Unfortunately, we must
223 // be careful when loading the expression in 'typeof'
224 // expressions. We are not allowed to throw reference errors for
225 // non-existing properties of the global object, so we must make it
226 // look like an explicit property access, instead of an access
227 // through the context chain.
228 void LoadTypeofExpression(Expression* x);
229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000231 // References
232
233 // Generate code to fetch the value of a reference. The reference is
234 // expected to be on top of the expression stack. It is left in place and
235 // its value is pushed on top of it.
236 void GetValue(Reference* ref) {
237 ASSERT(!has_cc());
238 ASSERT(!ref->is_illegal());
239 CodeGenState new_state(this, ref);
240 Visit(ref->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 }
242
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000243 // Generate code to store a value in a reference. The stored value is
244 // expected on top of the expression stack, with the reference immediately
245 // below it. The expression stack is left unchanged.
246 void SetValue(Reference* ref) {
247 ASSERT(!has_cc());
248 ASSERT(!ref->is_illegal());
249 ref->expression()->GenerateStoreCode(masm_, scope_, ref, NOT_CONST_INIT);
250 }
251
252 // Generate code to store a value in a reference. The stored value is
253 // expected on top of the expression stack, with the reference immediately
254 // below it. The expression stack is left unchanged.
255 void InitConst(Reference* ref) {
256 ASSERT(!has_cc());
257 ASSERT(!ref->is_illegal());
258 ref->expression()->GenerateStoreCode(masm_, scope_, ref, CONST_INIT);
259 }
260
261 // Generate code to fetch a value from a property of a reference. The
262 // reference is expected on top of the expression stack. It is left in
263 // place and its value is pushed on top of it.
264 void GetReferenceProperty(Expression* key);
265
266 // Generate code to store a value in a property of a reference. The
267 // stored value is expected on top of the expression stack, with the
268 // reference immediately below it. The expression stack is left
269 // unchanged.
270 static void SetReferenceProperty(MacroAssembler* masm,
271 Reference* ref,
272 Expression* key);
273
274
mads.s.ager31e71382008-08-13 09:32:07 +0000275 void ToBoolean(Label* true_target, Label* false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276
kasper.lund7276f142008-07-30 08:49:36 +0000277 void GenericBinaryOperation(Token::Value op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 void Comparison(Condition cc, bool strict = false);
279
280 void SmiOperation(Token::Value op, Handle<Object> value, bool reversed);
281
282 void CallWithArguments(ZoneList<Expression*>* arguments, int position);
283
284 // Declare global variables and functions in the given array of
285 // name/value pairs.
286 virtual void DeclareGlobals(Handle<FixedArray> pairs);
287
288 // Instantiate the function boilerplate.
289 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
290
291 // Control flow
292 void Branch(bool if_true, Label* L);
293 void CheckStack();
294 void CleanStack(int num_bytes);
295
296 // Node visitors
297#define DEF_VISIT(type) \
298 virtual void Visit##type(type* node);
299 NODE_LIST(DEF_VISIT)
300#undef DEF_VISIT
301
302 void RecordStatementPosition(Node* node);
303
304 // Activation frames
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000305 void EnterJSFrame();
306 void ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 virtual void GenerateIsSmi(ZoneList<Expression*>* args);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000309 virtual void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 virtual void GenerateIsArray(ZoneList<Expression*>* args);
311
312 virtual void GenerateArgumentsLength(ZoneList<Expression*>* args);
313 virtual void GenerateArgumentsAccess(ZoneList<Expression*>* args);
314
315 virtual void GenerateValueOf(ZoneList<Expression*>* args);
316 virtual void GenerateSetValueOf(ZoneList<Expression*>* args);
kasper.lund7276f142008-07-30 08:49:36 +0000317
318 virtual void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000319
320 virtual void GenerateObjectEquals(ZoneList<Expression*>* args);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000321
322 friend class Reference;
323 friend class Property;
324 friend class VariableProxy;
325 friend class Slot;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326};
327
328
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000329// -------------------------------------------------------------------------
330// CodeGenState implementation.
331
332CodeGenState::CodeGenState(ArmCodeGenerator* owner)
333 : owner_(owner),
334 access_(UNDEFINED),
335 ref_(NULL),
336 true_target_(NULL),
337 false_target_(NULL),
338 previous_(NULL) {
339 owner_->set_state(this);
340}
341
342
343CodeGenState::CodeGenState(ArmCodeGenerator* owner,
344 AccessType access,
345 Label* true_target,
346 Label* false_target)
347 : owner_(owner),
348 access_(access),
349 ref_(NULL),
350 true_target_(true_target),
351 false_target_(false_target),
352 previous_(owner->state()) {
353 owner_->set_state(this);
354}
355
356
357CodeGenState::CodeGenState(ArmCodeGenerator* owner, Reference* ref)
358 : owner_(owner),
359 access_(LOAD),
360 ref_(ref),
361 true_target_(owner->state()->true_target_),
362 false_target_(owner->state()->false_target_),
363 previous_(owner->state()) {
364 owner_->set_state(this);
365}
366
367
368CodeGenState::~CodeGenState() {
369 ASSERT(owner_->state() == this);
370 owner_->set_state(previous_);
371}
372
373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374// -----------------------------------------------------------------------------
375// ArmCodeGenerator implementation
376
377#define __ masm_->
378
379
380Handle<Code> ArmCodeGenerator::MakeCode(FunctionLiteral* flit,
381 Handle<Script> script,
382 bool is_eval) {
mads.s.ager31e71382008-08-13 09:32:07 +0000383#ifdef ENABLE_DISASSEMBLER
384 bool print_code = FLAG_print_code && !Bootstrapper::IsActive();
385#endif // ENABLE_DISASSEMBLER
386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387#ifdef DEBUG
388 bool print_source = false;
389 bool print_ast = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 const char* ftype;
391
392 if (Bootstrapper::IsActive()) {
393 print_source = FLAG_print_builtin_source;
394 print_ast = FLAG_print_builtin_ast;
395 print_code = FLAG_print_builtin_code;
396 ftype = "builtin";
397 } else {
398 print_source = FLAG_print_source;
399 print_ast = FLAG_print_ast;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 ftype = "user-defined";
401 }
402
403 if (FLAG_trace_codegen || print_source || print_ast) {
404 PrintF("*** Generate code for %s function: ", ftype);
405 flit->name()->ShortPrint();
406 PrintF(" ***\n");
407 }
408
409 if (print_source) {
410 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit));
411 }
412
413 if (print_ast) {
414 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit));
415 }
416#endif // DEBUG
417
418 // Generate code.
419 const int initial_buffer_size = 4 * KB;
420 ArmCodeGenerator cgen(initial_buffer_size, script, is_eval);
421 cgen.GenCode(flit);
422 if (cgen.HasStackOverflow()) {
kasper.lund212ac232008-07-16 07:07:30 +0000423 ASSERT(!Top::has_pending_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 return Handle<Code>::null();
425 }
426
427 // Process any deferred code.
428 cgen.ProcessDeferred();
429
430 // Allocate and install the code.
431 CodeDesc desc;
432 cgen.masm()->GetCode(&desc);
433 ScopeInfo<> sinfo(flit->scope());
434 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
435 Handle<Code> code = Factory::NewCode(desc, &sinfo, flags);
436
437 // Add unresolved entries in the code to the fixup list.
438 Bootstrapper::AddFixup(*code, cgen.masm());
439
mads.s.ager31e71382008-08-13 09:32:07 +0000440#ifdef ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 if (print_code) {
442 // Print the source code if available.
443 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
444 PrintF("--- Raw source ---\n");
445 StringInputBuffer stream(String::cast(script->source()));
446 stream.Seek(flit->start_position());
447 // flit->end_position() points to the last character in the stream. We
448 // need to compensate by adding one to calculate the length.
449 int source_len = flit->end_position() - flit->start_position() + 1;
450 for (int i = 0; i < source_len; i++) {
451 if (stream.has_more()) PrintF("%c", stream.GetNext());
452 }
453 PrintF("\n\n");
454 }
455 PrintF("--- Code ---\n");
mads.s.ager31e71382008-08-13 09:32:07 +0000456 code->Disassemble();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 }
mads.s.ager31e71382008-08-13 09:32:07 +0000458#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 return code;
461}
462
463
464ArmCodeGenerator::ArmCodeGenerator(int buffer_size,
465 Handle<Script> script,
466 bool is_eval)
467 : CodeGenerator(is_eval, script),
468 masm_(new MacroAssembler(NULL, buffer_size)),
469 scope_(NULL),
470 cc_reg_(al),
471 state_(NULL),
472 break_stack_height_(0) {
473}
474
475
476// Calling conventions:
477
mads.s.ager31e71382008-08-13 09:32:07 +0000478// r0: the number of arguments
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479// fp: frame pointer
480// sp: stack pointer
481// pp: caller's parameter pointer
482// cp: callee's context
483
484void ArmCodeGenerator::GenCode(FunctionLiteral* fun) {
485 Scope* scope = fun->scope();
486 ZoneList<Statement*>* body = fun->body();
487
488 // Initialize state.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000489 { CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 scope_ = scope;
491 cc_reg_ = al;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
493 // Entry
494 // stack: function, receiver, arguments, return address
495 // r0: number of arguments
496 // sp: stack pointer
497 // fp: frame pointer
498 // pp: caller's parameter pointer
499 // cp: callee's context
500
501 { Comment cmnt(masm_, "[ enter JS frame");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000502 EnterJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 }
504 // tos: code slot
505#ifdef DEBUG
506 if (strlen(FLAG_stop_at) > 0 &&
507 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000508 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 }
510#endif
511
512 // Allocate space for locals and initialize them.
kasper.lund7276f142008-07-30 08:49:36 +0000513 if (scope->num_stack_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 Comment cmnt(masm_, "[ allocate space for locals");
mads.s.ager31e71382008-08-13 09:32:07 +0000515 // Initialize stack slots with 'undefined' value.
516 __ mov(ip, Operand(Factory::undefined_value()));
517 for (int i = 0; i < scope->num_stack_slots(); i++) {
518 __ push(ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 }
520 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521
522 if (scope->num_heap_slots() > 0) {
523 // Allocate local context.
524 // Get outer context and create a new context based on it.
mads.s.ager31e71382008-08-13 09:32:07 +0000525 __ ldr(r0, FunctionOperand());
526 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +0000527 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
528
529 if (kDebug) {
530 Label verified_true;
531 __ cmp(r0, Operand(cp));
532 __ b(eq, &verified_true);
533 __ stop("NewContext: r0 is expected to be the same as cp");
534 __ bind(&verified_true);
535 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 // Update context local.
537 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
538 }
539
540 // TODO(1241774): Improve this code!!!
541 // 1) only needed if we have a context
542 // 2) no need to recompute context ptr every single time
543 // 3) don't copy parameter operand code from SlotOperand!
544 {
545 Comment cmnt2(masm_, "[ copy context parameters into .context");
546
547 // Note that iteration order is relevant here! If we have the same
548 // parameter twice (e.g., function (x, y, x)), and that parameter
549 // needs to be copied into the context, it must be the last argument
550 // passed to the parameter that needs to be copied. This is a rare
551 // case so we don't check for it, instead we rely on the copying
552 // order: such a parameter is copied repeatedly into the same
553 // context location and thus the last value is what is seen inside
554 // the function.
555 for (int i = 0; i < scope->num_parameters(); i++) {
556 Variable* par = scope->parameter(i);
557 Slot* slot = par->slot();
558 if (slot != NULL && slot->type() == Slot::CONTEXT) {
559 ASSERT(!scope->is_global_scope()); // no parameters in global scope
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000560 __ ldr(r1, ParameterOperand(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 // Loads r2 with context; used below in RecordWrite.
562 __ str(r1, SlotOperand(slot, r2));
563 // Load the offset into r3.
564 int slot_offset =
565 FixedArray::kHeaderSize + slot->index() * kPointerSize;
566 __ mov(r3, Operand(slot_offset));
567 __ RecordWrite(r2, r3, r1);
568 }
569 }
570 }
571
572 // Store the arguments object.
573 // This must happen after context initialization because
574 // the arguments array may be stored in the context!
575 if (scope->arguments() != NULL) {
576 ASSERT(scope->arguments_shadow() != NULL);
577 Comment cmnt(masm_, "[ allocate arguments object");
578 {
579 Reference target(this, scope->arguments());
mads.s.ager31e71382008-08-13 09:32:07 +0000580 __ ldr(r0, FunctionOperand());
581 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 __ CallRuntime(Runtime::kNewArguments, 1);
mads.s.ager31e71382008-08-13 09:32:07 +0000583 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 SetValue(&target);
585 }
586 // The value of arguments must also be stored in .arguments.
587 // TODO(1241813): This code can probably be improved by fusing it with
588 // the code that stores the arguments object above.
589 {
590 Reference target(this, scope->arguments_shadow());
591 Load(scope->arguments());
592 SetValue(&target);
593 }
594 }
595
596 // Generate code to 'execute' declarations and initialize
597 // functions (source elements). In case of an illegal
598 // redeclaration we need to handle that instead of processing the
599 // declarations.
600 if (scope->HasIllegalRedeclaration()) {
601 Comment cmnt(masm_, "[ illegal redeclarations");
602 scope->VisitIllegalRedeclaration(this);
603 } else {
604 Comment cmnt(masm_, "[ declarations");
mads.s.ager31e71382008-08-13 09:32:07 +0000605 // ProcessDeclarations calls DeclareGlobals indirectly
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 ProcessDeclarations(scope->declarations());
mads.s.ager31e71382008-08-13 09:32:07 +0000607
v8.team.kasperl727e9952008-09-02 14:56:44 +0000608 // Bail out if a stack-overflow exception occurred when
kasper.lund212ac232008-07-16 07:07:30 +0000609 // processing declarations.
610 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 }
612
mads.s.ager31e71382008-08-13 09:32:07 +0000613 if (FLAG_trace) {
614 // Push a valid value as the parameter. The runtime call only uses
615 // it as the return value to indicate non-failure.
616 __ mov(r0, Operand(Smi::FromInt(0)));
617 __ push(r0);
618 __ CallRuntime(Runtime::kTraceEnter, 1);
619 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620 CheckStack();
621
622 // Compile the body of the function in a vanilla state. Don't
623 // bother compiling all the code if the scope has an illegal
624 // redeclaration.
625 if (!scope->HasIllegalRedeclaration()) {
626 Comment cmnt(masm_, "[ function body");
627#ifdef DEBUG
628 bool is_builtin = Bootstrapper::IsActive();
629 bool should_trace =
630 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000631 if (should_trace) {
632 // Push a valid value as the parameter. The runtime call only uses
633 // it as the return value to indicate non-failure.
634 __ mov(r0, Operand(Smi::FromInt(0)));
635 __ push(r0);
636 __ CallRuntime(Runtime::kDebugTrace, 1);
637 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638#endif
639 VisitStatements(body);
640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 }
642
643 // exit
644 // r0: result
645 // sp: stack pointer
646 // fp: frame pointer
647 // pp: parameter pointer
648 // cp: callee's context
mads.s.ager31e71382008-08-13 09:32:07 +0000649 __ mov(r0, Operand(Factory::undefined_value()));
650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 __ bind(&function_return_);
mads.s.ager31e71382008-08-13 09:32:07 +0000652 if (FLAG_trace) {
653 // Push the return value on the stack as the parameter.
654 // Runtime::TraceExit returns the parameter as it is.
655 __ push(r0);
656 __ CallRuntime(Runtime::kTraceExit, 1);
657 }
658
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000659 // Tear down the frame which will restore the caller's frame pointer and the
660 // link register.
kasper.lund7276f142008-07-30 08:49:36 +0000661 ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000663 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
664 __ mov(pc, lr);
665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 // Code generation state must be reset.
667 scope_ = NULL;
668 ASSERT(!has_cc());
669 ASSERT(state_ == NULL);
670}
671
672
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000673MemOperand ArmCodeGenerator::SlotOperand(MacroAssembler* masm,
674 Scope* scope,
675 Slot* slot,
676 Register tmp) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 // Currently, this assertion will fail if we try to assign to
678 // a constant variable that is constant because it is read-only
679 // (such as the variable referring to a named function expression).
680 // We need to implement assignments to read-only variables.
681 // Ideally, we should do this during AST generation (by converting
682 // such assignments into expression statements); however, in general
683 // we may not be able to make the decision until past AST generation,
684 // that is when the entire program is known.
685 ASSERT(slot != NULL);
686 int index = slot->index();
687 switch (slot->type()) {
688 case Slot::PARAMETER:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000689 return ParameterOperand(scope, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
691 case Slot::LOCAL: {
692 ASSERT(0 <= index &&
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000693 index < scope->num_stack_slots() &&
kasper.lund7276f142008-07-30 08:49:36 +0000694 index >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 int local_offset = JavaScriptFrameConstants::kLocal0Offset -
kasper.lund7276f142008-07-30 08:49:36 +0000696 index * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 return MemOperand(fp, local_offset);
698 }
699
700 case Slot::CONTEXT: {
701 // Follow the context chain if necessary.
702 ASSERT(!tmp.is(cp)); // do not overwrite context register
703 Register context = cp;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000704 int chain_length = scope->ContextChainLength(slot->var()->scope());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 for (int i = chain_length; i-- > 0;) {
706 // Load the closure.
707 // (All contexts, even 'with' contexts, have a closure,
708 // and it is the same for all contexts inside a function.
709 // There is no need to go to the function context first.)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000710 masm->ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 // Load the function context (which is the incoming, outer context).
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000712 masm->ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 context = tmp;
714 }
715 // We may have a 'with' context now. Get the function context.
716 // (In fact this mov may never be the needed, since the scope analysis
717 // may not permit a direct context access in this case and thus we are
718 // always at a function context. However it is safe to dereference be-
719 // cause the function context of a function context is itself. Before
720 // deleting this mov we should try to create a counter-example first,
721 // though...)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000722 masm->ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 return ContextOperand(tmp, index);
724 }
725
726 default:
727 UNREACHABLE();
728 return MemOperand(r0, 0);
729 }
730}
731
732
mads.s.ager31e71382008-08-13 09:32:07 +0000733// Loads a value on the stack. If it is a boolean value, the result may have
734// been (partially) translated into branches, or it may have set the condition
735// code register. If force_cc is set, the value is forced to set the condition
736// code register and no value is pushed. If the condition code register was set,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737// has_cc() is true and cc_reg_ contains the condition to test for 'true'.
738void ArmCodeGenerator::LoadCondition(Expression* x,
739 CodeGenState::AccessType access,
740 Label* true_target,
741 Label* false_target,
742 bool force_cc) {
743 ASSERT(access == CodeGenState::LOAD ||
744 access == CodeGenState::LOAD_TYPEOF_EXPR);
745 ASSERT(!has_cc() && !is_referenced());
746
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000747 { CodeGenState new_state(this, access, true_target, false_target);
748 Visit(x);
749 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000751 // Convert the TOS value to a boolean in the condition code register.
752 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 }
754 ASSERT(has_cc() || !force_cc);
755}
756
757
758void ArmCodeGenerator::Load(Expression* x, CodeGenState::AccessType access) {
759 ASSERT(access == CodeGenState::LOAD ||
760 access == CodeGenState::LOAD_TYPEOF_EXPR);
761
762 Label true_target;
763 Label false_target;
764 LoadCondition(x, access, &true_target, &false_target, false);
765
766 if (has_cc()) {
767 // convert cc_reg_ into a bool
768 Label loaded, materialize_true;
769 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000770 __ mov(r0, Operand(Factory::false_value()));
771 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 __ b(&loaded);
773 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000774 __ mov(r0, Operand(Factory::true_value()));
775 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776 __ bind(&loaded);
777 cc_reg_ = al;
778 }
779
780 if (true_target.is_linked() || false_target.is_linked()) {
781 // we have at least one condition value
782 // that has been "translated" into a branch,
783 // thus it needs to be loaded explicitly again
784 Label loaded;
785 __ b(&loaded); // don't lose current TOS
786 bool both = true_target.is_linked() && false_target.is_linked();
787 // reincarnate "true", if necessary
788 if (true_target.is_linked()) {
789 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000790 __ mov(r0, Operand(Factory::true_value()));
791 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 }
793 // if both "true" and "false" need to be reincarnated,
794 // jump across code for "false"
795 if (both)
796 __ b(&loaded);
797 // reincarnate "false", if necessary
798 if (false_target.is_linked()) {
799 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000800 __ mov(r0, Operand(Factory::false_value()));
801 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 }
803 // everything is loaded at this point
804 __ bind(&loaded);
805 }
806 ASSERT(!has_cc());
807}
808
809
810void ArmCodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000811 __ ldr(r0, GlobalObject());
812 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813}
814
815
816// TODO(1241834): Get rid of this function in favor of just using Load, now
817// that we have the LOAD_TYPEOF_EXPR access type. => Need to handle
818// global variables w/o reference errors elsewhere.
819void ArmCodeGenerator::LoadTypeofExpression(Expression* x) {
820 Variable* variable = x->AsVariableProxy()->AsVariable();
821 if (variable != NULL && !variable->is_this() && variable->is_global()) {
822 // NOTE: This is somewhat nasty. We force the compiler to load
823 // the variable as if through '<global>.<variable>' to make sure we
824 // do not get reference errors.
825 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
826 Literal key(variable->name());
827 // TODO(1241834): Fetch the position from the variable instead of using
828 // no position.
829 Property property(&global, &key, kNoPosition);
830 Load(&property);
831 } else {
832 Load(x, CodeGenState::LOAD_TYPEOF_EXPR);
833 }
834}
835
836
837Reference::Reference(ArmCodeGenerator* cgen, Expression* expression)
838 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
839 cgen->LoadReference(this);
840}
841
842
843Reference::~Reference() {
844 cgen_->UnloadReference(this);
845}
846
847
848void ArmCodeGenerator::LoadReference(Reference* ref) {
849 Expression* e = ref->expression();
850 Property* property = e->AsProperty();
851 Variable* var = e->AsVariableProxy()->AsVariable();
852
853 if (property != NULL) {
854 Load(property->obj());
855 // Used a named reference if the key is a literal symbol.
856 // We don't use a named reference if they key is a string that can be
857 // legally parsed as an integer. This is because, otherwise we don't
858 // get into the slow case code that handles [] on String objects.
859 Literal* literal = property->key()->AsLiteral();
860 uint32_t dummy;
861 if (literal != NULL && literal->handle()->IsSymbol() &&
862 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
863 ref->set_type(Reference::NAMED);
864 } else {
865 Load(property->key());
866 ref->set_type(Reference::KEYED);
867 }
868 } else if (var != NULL) {
869 if (var->is_global()) {
870 // global variable
871 LoadGlobal();
872 ref->set_type(Reference::NAMED);
873 } else {
874 // local variable
875 ref->set_type(Reference::EMPTY);
876 }
877 } else {
878 Load(e);
879 __ CallRuntime(Runtime::kThrowReferenceError, 1);
mads.s.ager31e71382008-08-13 09:32:07 +0000880 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 }
882}
883
884
885void ArmCodeGenerator::UnloadReference(Reference* ref) {
886 int size = ref->size();
887 if (size <= 0) {
888 // Do nothing. No popping is necessary.
889 } else {
mads.s.ager31e71382008-08-13 09:32:07 +0000890 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 __ add(sp, sp, Operand(size * kPointerSize));
mads.s.ager31e71382008-08-13 09:32:07 +0000892 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 }
894}
895
896
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000897void Property::GenerateStoreCode(MacroAssembler* masm,
898 Scope* scope,
899 Reference* ref,
900 InitState init_state) {
901 Comment cmnt(masm, "[ Store to Property");
902 masm->RecordPosition(position());
903 ArmCodeGenerator::SetReferenceProperty(masm, ref, key());
904}
905
906
907void VariableProxy::GenerateStoreCode(MacroAssembler* masm,
908 Scope* scope,
909 Reference* ref,
910 InitState init_state) {
911 Comment cmnt(masm, "[ Store to VariableProxy");
912 Variable* node = var();
913
914 Expression* expr = node->rewrite();
915 if (expr != NULL) {
916 expr->GenerateStoreCode(masm, scope, ref, init_state);
917 } else {
918 ASSERT(node->is_global());
919 if (node->AsProperty() != NULL) {
920 masm->RecordPosition(node->AsProperty()->position());
921 }
922 ArmCodeGenerator::SetReferenceProperty(masm, ref,
923 new Literal(node->name()));
924 }
925}
926
927
928void Slot::GenerateStoreCode(MacroAssembler* masm,
929 Scope* scope,
930 Reference* ref,
931 InitState init_state) {
932 Comment cmnt(masm, "[ Store to Slot");
933
934 if (type() == Slot::LOOKUP) {
935 ASSERT(var()->mode() == Variable::DYNAMIC);
936
937 // For now, just do a runtime call.
938 masm->push(cp);
939 masm->mov(r0, Operand(var()->name()));
940 masm->push(r0);
941
942 if (init_state == CONST_INIT) {
943 // Same as the case for a normal store, but ignores attribute
944 // (e.g. READ_ONLY) of context slot so that we can initialize const
945 // properties (introduced via eval("const foo = (some expr);")). Also,
946 // uses the current function context instead of the top context.
947 //
948 // Note that we must declare the foo upon entry of eval(), via a
949 // context slot declaration, but we cannot initialize it at the same
950 // time, because the const declaration may be at the end of the eval
951 // code (sigh...) and the const variable may have been used before
952 // (where its value is 'undefined'). Thus, we can only do the
953 // initialization when we actually encounter the expression and when
954 // the expression operands are defined and valid, and thus we need the
955 // split into 2 operations: declaration of the context slot followed
956 // by initialization.
957 masm->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
958 } else {
959 masm->CallRuntime(Runtime::kStoreContextSlot, 3);
960 }
961 // Storing a variable must keep the (new) value on the expression
962 // stack. This is necessary for compiling assignment expressions.
963 masm->push(r0);
964
965 } else {
966 ASSERT(var()->mode() != Variable::DYNAMIC);
967
968 Label exit;
969 if (init_state == CONST_INIT) {
970 ASSERT(var()->mode() == Variable::CONST);
971 // Only the first const initialization must be executed (the slot
972 // still contains 'the hole' value). When the assignment is executed,
973 // the code is identical to a normal store (see below).
974 Comment cmnt(masm, "[ Init const");
975 masm->ldr(r2, ArmCodeGenerator::SlotOperand(masm, scope, this, r2));
976 masm->cmp(r2, Operand(Factory::the_hole_value()));
977 masm->b(ne, &exit);
978 }
979
980 // We must execute the store.
981 // r2 may be loaded with context; used below in RecordWrite.
982 // Storing a variable must keep the (new) value on the stack. This is
983 // necessary for compiling assignment expressions.
984 //
985 // Note: We will reach here even with var()->mode() == Variable::CONST
986 // because of const declarations which will initialize consts to 'the
987 // hole' value and by doing so, end up calling this code. r2 may be
988 // loaded with context; used below in RecordWrite.
989 masm->pop(r0);
990 masm->str(r0, ArmCodeGenerator::SlotOperand(masm, scope, this, r2));
991 masm->push(r0);
992
993 if (type() == Slot::CONTEXT) {
994 // Skip write barrier if the written value is a smi.
995 masm->tst(r0, Operand(kSmiTagMask));
996 masm->b(eq, &exit);
997 // r2 is loaded with context when calling SlotOperand above.
998 int offset = FixedArray::kHeaderSize + index() * kPointerSize;
999 masm->mov(r3, Operand(offset));
1000 masm->RecordWrite(r2, r3, r1);
1001 }
1002 // If we definitely did not jump over the assignment, we do not need to
1003 // bind the exit label. Doing so can defeat peephole optimization.
1004 if (init_state == CONST_INIT || type() == Slot::CONTEXT) {
1005 masm->bind(&exit);
1006 }
1007 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008}
1009
1010
1011// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
1012// register to a boolean in the condition code register. The code
1013// may jump to 'false_target' in case the register converts to 'false'.
mads.s.ager31e71382008-08-13 09:32:07 +00001014void ArmCodeGenerator::ToBoolean(Label* true_target,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +00001016 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 // Only the condition code should be set.
mads.s.ager31e71382008-08-13 09:32:07 +00001018 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019
1020 // Fast case checks
1021
mads.s.ager31e71382008-08-13 09:32:07 +00001022 // Check if the value is 'false'.
1023 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 __ b(eq, false_target);
1025
mads.s.ager31e71382008-08-13 09:32:07 +00001026 // Check if the value is 'true'.
1027 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 __ b(eq, true_target);
1029
mads.s.ager31e71382008-08-13 09:32:07 +00001030 // Check if the value is 'undefined'.
1031 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 __ b(eq, false_target);
1033
mads.s.ager31e71382008-08-13 09:32:07 +00001034 // Check if the value is a smi.
1035 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +00001037 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 __ b(eq, true_target);
1039
1040 // Slow case: call the runtime.
1041 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001042 __ CallRuntime(Runtime::kToBool, 1);
1043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 // Convert result (r0) to condition code
1045 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046
1047 cc_reg_ = ne;
1048}
1049
1050
1051#undef __
1052#define __ masm->
1053
1054
1055class GetPropertyStub : public CodeStub {
1056 public:
1057 GetPropertyStub() { }
1058
1059 private:
1060 Major MajorKey() { return GetProperty; }
1061 int MinorKey() { return 0; }
1062 void Generate(MacroAssembler* masm);
1063
1064 const char* GetName() { return "GetPropertyStub"; }
1065};
1066
1067
1068void GetPropertyStub::Generate(MacroAssembler* masm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001069 // sp[0]: key
1070 // sp[1]: receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 Label slow, fast;
mads.s.ager31e71382008-08-13 09:32:07 +00001072 // Get the key and receiver object from the stack.
1073 __ ldm(ia, sp, r0.bit() | r1.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 // Check that the key is a smi.
1075 __ tst(r0, Operand(kSmiTagMask));
1076 __ b(ne, &slow);
1077 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
1078 // Check that the object isn't a smi.
1079 __ tst(r1, Operand(kSmiTagMask));
1080 __ b(eq, &slow);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001081
1082 // Check that the object is some kind of JS object EXCEPT JS Value type.
1083 // In the case that the object is a value-wrapper object,
1084 // we enter the runtime system to make sure that indexing into string
1085 // objects work as intended.
1086 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
1088 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1089 __ cmp(r2, Operand(JS_OBJECT_TYPE));
1090 __ b(lt, &slow);
1091
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 // Get the elements array of the object.
1093 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1094 // Check that the object is in fast mode (not dictionary).
1095 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1096 __ cmp(r3, Operand(Factory::hash_table_map()));
1097 __ b(eq, &slow);
1098 // Check that the key (index) is within bounds.
1099 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
1100 __ cmp(r0, Operand(r3));
1101 __ b(lo, &fast);
1102
1103 // Slow case: Push extra copies of the arguments (2).
1104 __ bind(&slow);
1105 __ ldm(ia, sp, r0.bit() | r1.bit());
1106 __ stm(db_w, sp, r0.bit() | r1.bit());
1107 // Do tail-call to runtime routine.
mads.s.ager31e71382008-08-13 09:32:07 +00001108 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110 // Fast case: Do the load.
1111 __ bind(&fast);
1112 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
1113 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
1114 __ cmp(r0, Operand(Factory::the_hole_value()));
1115 // In case the loaded value is the_hole we have to consult GetProperty
1116 // to ensure the prototype chain is searched.
1117 __ b(eq, &slow);
1118
1119 masm->StubReturn(1);
1120}
1121
1122
1123class SetPropertyStub : public CodeStub {
1124 public:
1125 SetPropertyStub() { }
1126
1127 private:
1128 Major MajorKey() { return SetProperty; }
1129 int MinorKey() { return 0; }
1130 void Generate(MacroAssembler* masm);
1131
1132 const char* GetName() { return "GetPropertyStub"; }
1133};
1134
1135
mads.s.ager31e71382008-08-13 09:32:07 +00001136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137void SetPropertyStub::Generate(MacroAssembler* masm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001138 // r0 : value
1139 // sp[0] : key
1140 // sp[1] : receiver
1141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 Label slow, fast, array, extra, exit;
1143 // Get the key and the object from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001144 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 // Check that the key is a smi.
1146 __ tst(r1, Operand(kSmiTagMask));
1147 __ b(ne, &slow);
1148 // Check that the object isn't a smi.
1149 __ tst(r3, Operand(kSmiTagMask));
1150 __ b(eq, &slow);
1151 // Get the type of the object from its map.
1152 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
1153 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1154 // Check if the object is a JS array or not.
1155 __ cmp(r2, Operand(JS_ARRAY_TYPE));
1156 __ b(eq, &array);
1157 // Check that the object is some kind of JS object.
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001158 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 __ b(lt, &slow);
1160
1161
1162 // Object case: Check key against length in the elements array.
1163 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
1164 // Check that the object is in fast mode (not dictionary).
1165 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
1166 __ cmp(r2, Operand(Factory::hash_table_map()));
1167 __ b(eq, &slow);
1168 // Untag the key (for checking against untagged length in the fixed array).
1169 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
1170 // Compute address to store into and check array bounds.
1171 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
1172 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
1173 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
1174 __ cmp(r1, Operand(ip));
1175 __ b(lo, &fast);
1176
1177
1178 // Slow case: Push extra copies of the arguments (3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 __ bind(&slow);
1180 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
1181 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
1182 // Do tail-call to runtime routine.
mads.s.ager31e71382008-08-13 09:32:07 +00001183 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184
1185
1186 // Extra capacity case: Check if there is extra capacity to
1187 // perform the store and update the length. Used for adding one
1188 // element to the array by writing to array[array.length].
1189 // r0 == value, r1 == key, r2 == elements, r3 == object
1190 __ bind(&extra);
1191 __ b(ne, &slow); // do not leave holes in the array
1192 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
1193 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
1194 __ cmp(r1, Operand(ip));
1195 __ b(hs, &slow);
1196 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
1197 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
1198 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
1199 __ mov(r3, Operand(r2));
1200 // NOTE: Computing the address to store into must take the fact
1201 // that the key has been incremented into account.
1202 int displacement = Array::kHeaderSize - kHeapObjectTag -
1203 ((1 << kSmiTagSize) * 2);
1204 __ add(r2, r2, Operand(displacement));
1205 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1206 __ b(&fast);
1207
1208
1209 // Array case: Get the length and the elements array from the JS
1210 // array. Check that the array is in fast mode; if it is the
1211 // length is always a smi.
1212 // r0 == value, r3 == object
1213 __ bind(&array);
1214 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
1215 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1216 __ cmp(r1, Operand(Factory::hash_table_map()));
1217 __ b(eq, &slow);
1218
1219 // Check the key against the length in the array, compute the
1220 // address to store into and fall through to fast case.
1221 __ ldr(r1, MemOperand(sp));
1222 // r0 == value, r1 == key, r2 == elements, r3 == object.
1223 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
1224 __ cmp(r1, Operand(ip));
1225 __ b(hs, &extra);
1226 __ mov(r3, Operand(r2));
1227 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
1228 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1229
1230
1231 // Fast case: Do the store.
1232 // r0 == value, r2 == address to store into, r3 == elements
1233 __ bind(&fast);
1234 __ str(r0, MemOperand(r2));
1235 // Skip write barrier if the written value is a smi.
1236 __ tst(r0, Operand(kSmiTagMask));
1237 __ b(eq, &exit);
1238 // Update write barrier for the elements array address.
1239 __ sub(r1, r2, Operand(r3));
1240 __ RecordWrite(r3, r1, r2);
1241 __ bind(&exit);
1242 masm->StubReturn(1);
1243}
1244
1245
kasper.lund7276f142008-07-30 08:49:36 +00001246class GenericBinaryOpStub : public CodeStub {
1247 public:
1248 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
1249
1250 private:
1251 Token::Value op_;
1252
1253 Major MajorKey() { return GenericBinaryOp; }
1254 int MinorKey() { return static_cast<int>(op_); }
1255 void Generate(MacroAssembler* masm);
1256
1257 const char* GetName() {
1258 switch (op_) {
1259 case Token::ADD: return "GenericBinaryOpStub_ADD";
1260 case Token::SUB: return "GenericBinaryOpStub_SUB";
1261 case Token::MUL: return "GenericBinaryOpStub_MUL";
1262 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001263 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
1264 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
1265 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
1266 case Token::SAR: return "GenericBinaryOpStub_SAR";
1267 case Token::SHL: return "GenericBinaryOpStub_SHL";
1268 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +00001269 default: return "GenericBinaryOpStub";
1270 }
1271 }
1272
1273#ifdef DEBUG
1274 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
1275#endif
1276};
1277
1278
1279void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001280 // r1 : x
1281 // r0 : y
1282 // result : r0
1283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 switch (op_) {
1285 case Token::ADD: {
1286 Label slow, exit;
1287 // fast path
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1289 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
1290 // go slow-path in case of overflow
1291 __ b(vs, &slow);
1292 // go slow-path in case of non-smi operands
1293 ASSERT(kSmiTag == 0); // adjust code below
1294 __ tst(r2, Operand(kSmiTagMask));
1295 __ b(eq, &exit);
1296 // slow path
1297 __ bind(&slow);
1298 __ sub(r0, r0, Operand(r1)); // revert optimistic add
mads.s.ager31e71382008-08-13 09:32:07 +00001299 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 __ push(r0);
1301 __ mov(r0, Operand(1)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001302 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 // done
1304 __ bind(&exit);
1305 break;
1306 }
1307
1308 case Token::SUB: {
1309 Label slow, exit;
1310 // fast path
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1312 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
1313 // go slow-path in case of overflow
1314 __ b(vs, &slow);
1315 // go slow-path in case of non-smi operands
1316 ASSERT(kSmiTag == 0); // adjust code below
1317 __ tst(r2, Operand(kSmiTagMask));
1318 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
1319 __ b(eq, &exit);
1320 // slow path
1321 __ bind(&slow);
mads.s.ager31e71382008-08-13 09:32:07 +00001322 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 __ push(r0);
1324 __ mov(r0, Operand(1)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001325 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 // done
1327 __ bind(&exit);
1328 break;
1329 }
1330
1331 case Token::MUL: {
1332 Label slow, exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 // tag check
1334 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1335 ASSERT(kSmiTag == 0); // adjust code below
1336 __ tst(r2, Operand(kSmiTagMask));
1337 __ b(ne, &slow);
1338 // remove tag from one operand (but keep sign), so that result is smi
1339 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
1340 // do multiplication
1341 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
1342 // go slow on overflows (overflow bit is not set)
1343 __ mov(ip, Operand(r3, ASR, 31));
1344 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
1345 __ b(ne, &slow);
1346 // go slow on zero result to handle -0
1347 __ tst(r3, Operand(r3));
1348 __ mov(r0, Operand(r3), LeaveCC, ne);
1349 __ b(ne, &exit);
1350 // slow case
1351 __ bind(&slow);
mads.s.ager31e71382008-08-13 09:32:07 +00001352 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 __ push(r0);
1354 __ mov(r0, Operand(1)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001355 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 // done
1357 __ bind(&exit);
1358 break;
1359 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001360
1361 case Token::BIT_OR:
1362 case Token::BIT_AND:
1363 case Token::BIT_XOR: {
1364 Label slow, exit;
1365 // tag check
1366 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1367 ASSERT(kSmiTag == 0); // adjust code below
1368 __ tst(r2, Operand(kSmiTagMask));
1369 __ b(ne, &slow);
1370 switch (op_) {
1371 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
1372 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
1373 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
1374 default: UNREACHABLE();
1375 }
1376 __ b(&exit);
1377 __ bind(&slow);
1378 __ push(r1); // restore stack
1379 __ push(r0);
1380 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
1381 switch (op_) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001382 case Token::BIT_OR:
1383 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
1384 break;
1385 case Token::BIT_AND:
1386 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
1387 break;
1388 case Token::BIT_XOR:
1389 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
1390 break;
1391 default:
1392 UNREACHABLE();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001393 }
1394 __ bind(&exit);
1395 break;
1396 }
1397
1398 case Token::SHL:
1399 case Token::SHR:
1400 case Token::SAR: {
1401 Label slow, exit;
1402 // tag check
1403 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1404 ASSERT(kSmiTag == 0); // adjust code below
1405 __ tst(r2, Operand(kSmiTagMask));
1406 __ b(ne, &slow);
1407 // remove tags from operands (but keep sign)
1408 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
1409 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
1410 // use only the 5 least significant bits of the shift count
1411 __ and_(r2, r2, Operand(0x1f));
1412 // perform operation
1413 switch (op_) {
1414 case Token::SAR:
1415 __ mov(r3, Operand(r3, ASR, r2));
1416 // no checks of result necessary
1417 break;
1418
1419 case Token::SHR:
1420 __ mov(r3, Operand(r3, LSR, r2));
1421 // check that the *unsigned* result fits in a smi
1422 // neither of the two high-order bits can be set:
1423 // - 0x80000000: high bit would be lost when smi tagging
1424 // - 0x40000000: this number would convert to negative when
1425 // smi tagging these two cases can only happen with shifts
1426 // by 0 or 1 when handed a valid smi
1427 __ and_(r2, r3, Operand(0xc0000000), SetCC);
1428 __ b(ne, &slow);
1429 break;
1430
1431 case Token::SHL:
1432 __ mov(r3, Operand(r3, LSL, r2));
1433 // check that the *signed* result fits in a smi
1434 __ add(r2, r3, Operand(0x40000000), SetCC);
1435 __ b(mi, &slow);
1436 break;
1437
1438 default: UNREACHABLE();
1439 }
1440 // tag result and store it in r0
1441 ASSERT(kSmiTag == 0); // adjust code below
1442 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
1443 __ b(&exit);
1444 // slow case
1445 __ bind(&slow);
1446 __ push(r1); // restore stack
1447 __ push(r0);
1448 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
1449 switch (op_) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001450 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
1451 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
1452 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001453 default: UNREACHABLE();
1454 }
1455 __ bind(&exit);
1456 break;
1457 }
1458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 default: UNREACHABLE();
1460 }
mads.s.ager31e71382008-08-13 09:32:07 +00001461 __ Ret();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462}
1463
1464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465void StackCheckStub::Generate(MacroAssembler* masm) {
1466 Label within_limit;
1467 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
1468 __ ldr(ip, MemOperand(ip));
1469 __ cmp(sp, Operand(ip));
1470 __ b(hs, &within_limit);
1471 // Do tail-call to runtime routine.
1472 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001473 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 __ bind(&within_limit);
1475
1476 masm->StubReturn(1);
1477}
1478
1479
1480void UnarySubStub::Generate(MacroAssembler* masm) {
1481 Label undo;
1482 Label slow;
1483 Label done;
1484
1485 // Enter runtime system if the value is not a smi.
1486 __ tst(r0, Operand(kSmiTagMask));
1487 __ b(ne, &slow);
1488
1489 // Enter runtime system if the value of the expression is zero
1490 // to make sure that we switch between 0 and -0.
1491 __ cmp(r0, Operand(0));
1492 __ b(eq, &slow);
1493
1494 // The value of the expression is a smi that is not zero. Try
1495 // optimistic subtraction '0 - value'.
1496 __ rsb(r1, r0, Operand(0), SetCC);
1497 __ b(vs, &slow);
1498
1499 // If result is a smi we are done.
1500 __ tst(r1, Operand(kSmiTagMask));
1501 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
1502 __ b(eq, &done);
1503
1504 // Enter runtime system.
1505 __ bind(&slow);
1506 __ push(r0);
1507 __ mov(r0, Operand(0)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001508 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509
1510 __ bind(&done);
1511 masm->StubReturn(1);
1512}
1513
1514
1515class InvokeBuiltinStub : public CodeStub {
1516 public:
1517 enum Kind { Inc, Dec, ToNumber };
1518 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
1519
1520 private:
1521 Kind kind_;
1522 int argc_;
1523
1524 Major MajorKey() { return InvokeBuiltin; }
1525 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
1526 void Generate(MacroAssembler* masm);
1527
1528 const char* GetName() { return "InvokeBuiltinStub"; }
1529
1530#ifdef DEBUG
1531 void Print() {
1532 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
1533 static_cast<int>(kind_),
1534 argc_);
1535 }
1536#endif
1537};
1538
1539
1540void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
1541 __ push(r0);
1542 __ mov(r0, Operand(0)); // set number of arguments
1543 switch (kind_) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001544 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
1545 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
1546 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547 default: UNREACHABLE();
1548 }
1549 masm->StubReturn(argc_);
1550}
1551
1552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
1554 // r0 holds exception
1555 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1557 __ ldr(sp, MemOperand(r3));
1558 __ pop(r2); // pop next in chain
1559 __ str(r2, MemOperand(r3));
1560 // restore parameter- and frame-pointer and pop state.
1561 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
1562 // Before returning we restore the context from the frame pointer if not NULL.
1563 // The frame pointer is NULL in the exception handler of a JS entry frame.
1564 __ cmp(fp, Operand(0));
1565 // Set cp to NULL if fp is NULL.
1566 __ mov(cp, Operand(0), LeaveCC, eq);
1567 // Restore cp otherwise.
1568 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1569 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
1570 __ pop(pc);
1571}
1572
1573
1574void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
1575 // Fetch top stack handler.
1576 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1577 __ ldr(r3, MemOperand(r3));
1578
1579 // Unwind the handlers until the ENTRY handler is found.
1580 Label loop, done;
1581 __ bind(&loop);
1582 // Load the type of the current stack handler.
1583 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
1584 StackHandlerConstants::kStateOffset;
1585 __ ldr(r2, MemOperand(r3, kStateOffset));
1586 __ cmp(r2, Operand(StackHandler::ENTRY));
1587 __ b(eq, &done);
1588 // Fetch the next handler in the list.
1589 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
1590 StackHandlerConstants::kNextOffset;
1591 __ ldr(r3, MemOperand(r3, kNextOffset));
1592 __ jmp(&loop);
1593 __ bind(&done);
1594
1595 // Set the top handler address to next handler past the current ENTRY handler.
1596 __ ldr(r0, MemOperand(r3, kNextOffset));
1597 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
1598 __ str(r0, MemOperand(r2));
1599
1600 // Set external caught exception to false.
1601 __ mov(r0, Operand(false));
1602 ExternalReference external_caught(Top::k_external_caught_exception_address);
1603 __ mov(r2, Operand(external_caught));
1604 __ str(r0, MemOperand(r2));
1605
mads.s.ager31e71382008-08-13 09:32:07 +00001606 // Set pending exception and r0 to out of memory exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 Failure* out_of_memory = Failure::OutOfMemoryException();
1608 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1609 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
1610 __ str(r0, MemOperand(r2));
1611
1612 // Restore the stack to the address of the ENTRY handler
1613 __ mov(sp, Operand(r3));
1614
1615 // restore parameter- and frame-pointer and pop state.
1616 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
1617 // Before returning we restore the context from the frame pointer if not NULL.
1618 // The frame pointer is NULL in the exception handler of a JS entry frame.
1619 __ cmp(fp, Operand(0));
1620 // Set cp to NULL if fp is NULL.
1621 __ mov(cp, Operand(0), LeaveCC, eq);
1622 // Restore cp otherwise.
1623 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1624 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
1625 __ pop(pc);
1626}
1627
1628
1629void CEntryStub::GenerateCore(MacroAssembler* masm,
1630 Label* throw_normal_exception,
1631 Label* throw_out_of_memory_exception,
1632 bool do_gc,
1633 bool do_restore) {
1634 // r0: result parameter for PerformGC, if any
mads.s.ager31e71382008-08-13 09:32:07 +00001635 // r4: number of arguments including receiver (C callee-saved)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 // r5: pointer to builtin function (C callee-saved)
1637
1638 if (do_gc) {
1639 __ Call(FUNCTION_ADDR(Runtime::PerformGC), runtime_entry); // passing r0
1640 }
1641
mads.s.ager31e71382008-08-13 09:32:07 +00001642 // Call C built-in.
1643 // r0 = argc.
1644 __ mov(r0, Operand(r4));
1645 // r1 = argv.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 __ add(r1, fp, Operand(r4, LSL, kPointerSizeLog2));
mads.s.ager31e71382008-08-13 09:32:07 +00001647 __ add(r1, r1, Operand(ExitFrameConstants::kPPDisplacement - kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648
1649 // TODO(1242173): To let the GC traverse the return address of the exit
1650 // frames, we need to know where the return address is. Right now,
1651 // we push it on the stack to be able to find it again, but we never
1652 // restore from it in case of changes, which makes it impossible to
1653 // support moving the C entry code stub. This should be fixed, but currently
1654 // this is OK because the CEntryStub gets generated so early in the V8 boot
1655 // sequence that it is not moving ever.
1656 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
1657 __ push(lr);
1658#if !defined(__arm__)
1659 // Notify the simulator of the transition to C code.
1660 __ swi(assembler::arm::call_rt_r5);
1661#else /* !defined(__arm__) */
1662 __ mov(pc, Operand(r5));
1663#endif /* !defined(__arm__) */
1664 // result is in r0 or r0:r1 - do not destroy these registers!
1665
1666 // check for failure result
1667 Label failure_returned;
1668 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
1669 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
1670 __ add(r2, r0, Operand(1));
1671 __ tst(r2, Operand(kFailureTagMask));
1672 __ b(eq, &failure_returned);
1673
1674 // clear top frame
1675 __ mov(r3, Operand(0));
1676 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1677 __ str(r3, MemOperand(ip));
1678
1679 // Restore the memory copy of the registers by digging them out from
1680 // the stack.
1681 if (do_restore) {
1682 // Ok to clobber r2 and r3.
1683 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
1684 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
1685 __ add(r3, fp, Operand(kOffset));
1686 __ CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
1687 }
1688
1689 // Exit C frame and return
1690 // r0:r1: result
1691 // sp: stack pointer
1692 // fp: frame pointer
1693 // pp: caller's parameter pointer pp (restored as C callee-saved)
1694
1695 // Restore current context from top and clear it in debug mode.
1696 __ mov(r3, Operand(Top::context_address()));
1697 __ ldr(cp, MemOperand(r3));
1698 __ mov(sp, Operand(fp)); // respect ABI stack constraint
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001699 __ ldm(ia, sp, fp.bit() | sp.bit() | pc.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
1701 // check if we should retry or throw exception
1702 Label retry;
1703 __ bind(&failure_returned);
1704 ASSERT(Failure::RETRY_AFTER_GC == 0);
1705 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
1706 __ b(eq, &retry);
1707
1708 Label continue_exception;
1709 // If the returned failure is EXCEPTION then promote Top::pending_exception().
1710 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
1711 __ b(ne, &continue_exception);
1712
1713 // Retrieve the pending exception and clear the variable.
1714 __ mov(ip, Operand(Factory::the_hole_value().location()));
1715 __ ldr(r3, MemOperand(ip));
1716 __ mov(ip, Operand(Top::pending_exception_address()));
1717 __ ldr(r0, MemOperand(ip));
1718 __ str(r3, MemOperand(ip));
1719
1720 __ bind(&continue_exception);
1721 // Special handling of out of memory exception.
1722 Failure* out_of_memory = Failure::OutOfMemoryException();
1723 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1724 __ b(eq, throw_out_of_memory_exception);
1725
1726 // Handle normal exception.
1727 __ jmp(throw_normal_exception);
1728
1729 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
1730}
1731
1732
1733void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
1734 // Called from JavaScript; parameters are on stack as if calling JS function
mads.s.ager31e71382008-08-13 09:32:07 +00001735 // r0: number of arguments including receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 // r1: pointer to builtin function
1737 // fp: frame pointer (restored after C call)
1738 // sp: stack pointer (restored as callee's pp after C call)
1739 // cp: current context (C callee-saved)
1740 // pp: caller's parameter pointer pp (C callee-saved)
1741
1742 // NOTE: Invocations of builtins may return failure objects
1743 // instead of a proper result. The builtin entry handles
1744 // this by performing a garbage collection and retrying the
1745 // builtin once.
1746
1747 // Enter C frame
1748 // Compute parameter pointer before making changes and save it as ip register
1749 // so that it is restored as sp register on exit, thereby popping the args.
mads.s.ager31e71382008-08-13 09:32:07 +00001750 // ip = sp + kPointerSize*args_len;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 __ add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001753 // push in reverse order:
1754 // caller_fp, sp_on_exit, caller_pc
1755 __ stm(db_w, sp, fp.bit() | ip.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 __ mov(fp, Operand(sp)); // setup new frame pointer
1757
1758 // Store the current context in top.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001759 __ mov(ip, Operand(ExternalReference(Top::k_context_address)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 __ str(cp, MemOperand(ip));
1761
1762 // remember top frame
1763 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1764 __ str(fp, MemOperand(ip));
1765
1766 // Push debug marker.
1767 __ mov(ip, Operand(is_debug_break ? 1 : 0));
1768 __ push(ip);
1769
1770 if (is_debug_break) {
1771 // Save the state of all registers to the stack from the memory location.
1772 // Use sp as base to push.
1773 __ CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
1774 }
1775
1776 // move number of arguments (argc) into callee-saved register
1777 __ mov(r4, Operand(r0));
1778
1779 // move pointer to builtin function into callee-saved register
1780 __ mov(r5, Operand(r1));
1781
1782 // r0: result parameter for PerformGC, if any (setup below)
1783 // r4: number of arguments
1784 // r5: pointer to builtin function (C callee-saved)
1785
1786 Label entry;
1787 __ bind(&entry);
1788
1789 Label throw_out_of_memory_exception;
1790 Label throw_normal_exception;
1791
1792#ifdef DEBUG
1793 if (FLAG_gc_greedy) {
1794 Failure* failure = Failure::RetryAfterGC(0, NEW_SPACE);
1795 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
1796 }
1797 GenerateCore(masm,
1798 &throw_normal_exception,
1799 &throw_out_of_memory_exception,
1800 FLAG_gc_greedy,
1801 is_debug_break);
1802#else
1803 GenerateCore(masm,
1804 &throw_normal_exception,
1805 &throw_out_of_memory_exception,
1806 false,
1807 is_debug_break);
1808#endif
1809 GenerateCore(masm,
1810 &throw_normal_exception,
1811 &throw_out_of_memory_exception,
1812 true,
1813 is_debug_break);
1814
1815 __ bind(&throw_out_of_memory_exception);
1816 GenerateThrowOutOfMemory(masm);
1817 // control flow for generated will not return.
1818
1819 __ bind(&throw_normal_exception);
1820 GenerateThrowTOS(masm);
1821}
1822
1823
1824void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
1825 // r0: code entry
1826 // r1: function
1827 // r2: receiver
1828 // r3: argc
1829 // [sp+0]: argv
1830
1831 Label invoke, exit;
1832
1833 // Called from C, so do not pop argc and args on exit (preserve sp)
1834 // No need to save register-passed args
1835 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001836 __ stm(db_w, sp, kCalleeSaved | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837
1838 // Get address of argv, see stm above.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001839 // r0: code entry
1840 // r1: function
1841 // r2: receiver
1842 // r3: argc
1843 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 __ ldr(r4, MemOperand(r4)); // argv
1845
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001846 // Push a frame with special values setup to mark it as an entry frame.
1847 // r0: code entry
1848 // r1: function
1849 // r2: receiver
1850 // r3: argc
1851 // r4: argv
1852 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
1853 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
1854 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
1855 __ mov(r6, Operand(Smi::FromInt(marker)));
1856 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1857 __ ldr(r5, MemOperand(r5));
1858 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
1859
1860 // Setup frame pointer for the frame to be pushed.
1861 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862
1863 // Call a faked try-block that does the invoke.
1864 __ bl(&invoke);
1865
1866 // Caught exception: Store result (exception) in the pending
1867 // exception field in the JSEnv and return a failure sentinel.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001868 // Coming in here the fp will be invalid because the PushTryHandler below
1869 // sets it to 0 to signal the existence of the JSEntry frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 __ mov(ip, Operand(Top::pending_exception_address()));
1871 __ str(r0, MemOperand(ip));
1872 __ mov(r0, Operand(Handle<Failure>(Failure::Exception())));
1873 __ b(&exit);
1874
1875 // Invoke: Link this frame into the handler chain.
1876 __ bind(&invoke);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001877 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
1879 // If an exception not caught by another handler occurs, this handler returns
1880 // control to the code after the bl(&invoke) above, which restores all
1881 // kCalleeSaved registers (including cp, pp and fp) to their saved values
1882 // before returning a failure to C.
1883
1884 // Clear any pending exceptions.
1885 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
1886 __ ldr(r5, MemOperand(ip));
1887 __ mov(ip, Operand(Top::pending_exception_address()));
1888 __ str(r5, MemOperand(ip));
1889
1890 // Invoke the function by calling through JS entry trampoline builtin.
1891 // Notice that we cannot store a reference to the trampoline code directly in
1892 // this stub, because runtime stubs are not traversed when doing GC.
1893
1894 // Expected registers by Builtins::JSEntryTrampoline
1895 // r0: code entry
1896 // r1: function
1897 // r2: receiver
1898 // r3: argc
1899 // r4: argv
1900 if (is_construct) {
1901 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
1902 __ mov(ip, Operand(construct_entry));
1903 } else {
1904 ExternalReference entry(Builtins::JSEntryTrampoline);
1905 __ mov(ip, Operand(entry));
1906 }
1907 __ ldr(ip, MemOperand(ip)); // deref address
1908
1909 // Branch and link to JSEntryTrampoline
1910 __ mov(lr, Operand(pc));
1911 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
1912
1913 // Unlink this frame from the handler chain. When reading the
1914 // address of the next handler, there is no need to use the address
1915 // displacement since the current stack pointer (sp) points directly
1916 // to the stack handler.
1917 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
1918 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
1919 __ str(r3, MemOperand(ip));
1920 // No need to restore registers
1921 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
1922
1923 __ bind(&exit); // r0 holds result
1924 // Restore the top frame descriptors from the stack.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001925 __ pop(r3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1927 __ str(r3, MemOperand(ip));
1928
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001929 // Reset the stack to the callee saved registers.
1930 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001932 // Restore callee-saved registers and return.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933#ifdef DEBUG
1934 if (FLAG_debug_code) __ mov(lr, Operand(pc));
1935#endif
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001936 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937}
1938
1939
1940class ArgumentsAccessStub: public CodeStub {
1941 public:
1942 explicit ArgumentsAccessStub(bool is_length) : is_length_(is_length) { }
1943
1944 private:
1945 bool is_length_;
1946
1947 Major MajorKey() { return ArgumentsAccess; }
1948 int MinorKey() { return is_length_ ? 1 : 0; }
1949 void Generate(MacroAssembler* masm);
1950
1951 const char* GetName() { return "ArgumentsAccessStub"; }
1952
1953#ifdef DEBUG
1954 void Print() {
1955 PrintF("ArgumentsAccessStub (is_length %s)\n",
1956 is_length_ ? "true" : "false");
1957 }
1958#endif
1959};
1960
1961
1962void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001963 // ----------- S t a t e -------------
1964 // -- r0: formal number of parameters for the calling function
1965 // -- r1: key (if value access)
1966 // -- lr: return address
1967 // -----------------------------------
1968
1969 // Check that the key is a smi for non-length accesses.
1970 Label slow;
1971 if (!is_length_) {
1972 __ tst(r1, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 __ b(ne, &slow);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001974 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001976 // Check if the calling frame is an arguments adaptor frame.
1977 // r0: formal number of parameters
1978 // r1: key (if access)
1979 Label adaptor;
1980 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1981 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1982 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
1983 __ b(eq, &adaptor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001985 static const int kParamDisplacement =
1986 StandardFrameConstants::kCallerSPOffset - kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001988 if (is_length_) {
1989 // Nothing to do: the formal length of parameters has been passed in r0
1990 // by the calling function.
1991 } else {
1992 // Check index against formal parameter count. Use unsigned comparison to
1993 // get the negative check for free.
1994 // r0: formal number of parameters
1995 // r1: index
1996 __ cmp(r1, r0);
1997 __ b(cs, &slow);
1998
1999 // Read the argument from the current frame.
2000 __ sub(r3, r0, r1);
2001 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
2002 __ ldr(r0, MemOperand(r3, kParamDisplacement));
2003 }
2004
2005 // Return to the calling function.
2006 __ mov(pc, lr);
2007
2008 // An arguments adaptor frame is present. Find the length or the actual
2009 // argument in the calling frame.
2010 // r0: formal number of parameters
2011 // r1: key
2012 // r2: adaptor frame pointer
2013 __ bind(&adaptor);
2014 // Read the arguments length from the adaptor frame. This is the result if
2015 // only accessing the length, otherwise it is used in accessing the value
2016 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
2017
2018 if (!is_length_) {
2019 // Check index against actual arguments count. Use unsigned comparison to
2020 // get the negative check for free.
2021 // r0: actual number of parameter
2022 // r1: index
2023 // r2: adaptor frame point
2024 __ cmp(r1, r0);
2025 __ b(cs, &slow);
2026
2027 // Read the argument from the adaptor frame.
2028 __ sub(r3, r0, r1);
2029 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
2030 __ ldr(r0, MemOperand(r3, kParamDisplacement));
2031 }
2032
2033 // Return to the calling function.
2034 __ mov(pc, lr);
2035
2036 if (!is_length_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 __ bind(&slow);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002038 __ push(r1);
mads.s.ager31e71382008-08-13 09:32:07 +00002039 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 }
2041}
2042
2043
2044#undef __
2045#define __ masm_->
2046
2047
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002048void ArmCodeGenerator::GetReferenceProperty(Expression* key) {
2049 ASSERT(!ref()->is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 Reference::Type type = ref()->type();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002052 // TODO(1241834): Make sure that this it is safe to ignore the distinction
2053 // between access types LOAD and LOAD_TYPEOF_EXPR. If there is a chance
2054 // that reference errors can be thrown below, we must distinguish between
2055 // the two kinds of loads (typeof expression loads must not throw a
2056 // reference error).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057 if (type == Reference::NAMED) {
2058 // Compute the name of the property.
2059 Literal* literal = key->AsLiteral();
2060 Handle<String> name(String::cast(*literal->handle()));
2061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 // Call the appropriate IC code.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002063 // Setup the name register.
2064 __ mov(r2, Operand(name));
2065 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2066 Variable* var = ref()->expression()->AsVariableProxy()->AsVariable();
2067 if (var != NULL) {
2068 ASSERT(var->is_global());
2069 __ Call(ic, code_target_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 __ Call(ic, code_target);
2072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002075 // Access keyed property.
2076 ASSERT(type == Reference::KEYED);
2077
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002078 // TODO(1224671): Implement inline caching for keyed loads as on ia32.
2079 GetPropertyStub stub;
2080 __ CallStub(&stub);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 }
mads.s.ager31e71382008-08-13 09:32:07 +00002082 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083}
2084
2085
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002086void ArmCodeGenerator::SetReferenceProperty(MacroAssembler* masm,
2087 Reference* ref,
2088 Expression* key) {
2089 ASSERT(!ref->is_illegal());
2090 Reference::Type type = ref->type();
2091
2092 if (type == Reference::NAMED) {
2093 // Compute the name of the property.
2094 Literal* literal = key->AsLiteral();
2095 Handle<String> name(String::cast(*literal->handle()));
2096
2097 // Call the appropriate IC code.
2098 masm->pop(r0); // value
2099 // Setup the name register.
2100 masm->mov(r2, Operand(name));
2101 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2102 masm->Call(ic, code_target);
2103
2104 } else {
2105 // Access keyed property.
2106 ASSERT(type == Reference::KEYED);
2107
2108 masm->pop(r0); // value
2109 SetPropertyStub stub;
2110 masm->CallStub(&stub);
2111 }
2112 masm->push(r0);
2113}
2114
2115
kasper.lund7276f142008-07-30 08:49:36 +00002116void ArmCodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +00002117 // sp[0] : y
2118 // sp[1] : x
2119 // result : r0
2120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 // Stub is entered with a call: 'return address' is in lr.
2122 switch (op) {
2123 case Token::ADD: // fall through.
2124 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002125 case Token::MUL:
2126 case Token::BIT_OR:
2127 case Token::BIT_AND:
2128 case Token::BIT_XOR:
2129 case Token::SHL:
2130 case Token::SHR:
2131 case Token::SAR: {
mads.s.ager31e71382008-08-13 09:32:07 +00002132 __ pop(r0); // r0 : y
2133 __ pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +00002134 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 __ CallStub(&stub);
2136 break;
2137 }
2138
2139 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +00002140 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002141 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 break;
2143 }
2144
2145 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +00002146 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002147 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 break;
2149 }
2150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 case Token::COMMA:
mads.s.ager31e71382008-08-13 09:32:07 +00002152 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 // simply discard left value
mads.s.ager31e71382008-08-13 09:32:07 +00002154 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 break;
2156
2157 default:
2158 // Other cases should have been handled before this point.
2159 UNREACHABLE();
2160 break;
2161 }
2162}
2163
2164
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002165class DeferredInlinedSmiOperation: public DeferredCode {
2166 public:
2167 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
2168 int value, bool reversed) :
2169 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
2170 set_comment("[ DeferredInlinedSmiOperation");
2171 }
2172
2173 virtual void Generate() {
2174 switch (op_) {
2175 case Token::ADD: {
2176 if (reversed_) {
2177 // revert optimistic add
2178 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
2179 __ mov(r1, Operand(Smi::FromInt(value_))); // x
2180 } else {
2181 // revert optimistic add
2182 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
2183 __ mov(r0, Operand(Smi::FromInt(value_)));
2184 }
2185 break;
2186 }
2187
2188 case Token::SUB: {
2189 if (reversed_) {
2190 // revert optimistic sub
2191 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
2192 __ mov(r1, Operand(Smi::FromInt(value_)));
2193 } else {
2194 __ add(r1, r0, Operand(Smi::FromInt(value_)));
2195 __ mov(r0, Operand(Smi::FromInt(value_)));
2196 }
2197 break;
2198 }
2199
2200 case Token::BIT_OR:
2201 case Token::BIT_XOR:
2202 case Token::BIT_AND: {
2203 if (reversed_) {
2204 __ mov(r1, Operand(Smi::FromInt(value_)));
2205 } else {
2206 __ mov(r1, Operand(r0));
2207 __ mov(r0, Operand(Smi::FromInt(value_)));
2208 }
2209 break;
2210 }
2211
2212 case Token::SHL:
2213 case Token::SHR:
2214 case Token::SAR: {
2215 if (!reversed_) {
2216 __ mov(r1, Operand(r0));
2217 __ mov(r0, Operand(Smi::FromInt(value_)));
2218 } else {
2219 UNREACHABLE(); // should have been handled in SmiOperation
2220 }
2221 break;
2222 }
2223
2224 default:
2225 // other cases should have been handled before this point.
2226 UNREACHABLE();
2227 break;
2228 }
2229
2230 GenericBinaryOpStub igostub(op_);
2231 __ CallStub(&igostub);
2232 }
2233
2234 private:
2235 Token::Value op_;
2236 int value_;
2237 bool reversed_;
2238};
2239
2240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241void ArmCodeGenerator::SmiOperation(Token::Value op,
2242 Handle<Object> value,
2243 bool reversed) {
2244 // NOTE: This is an attempt to inline (a bit) more of the code for
2245 // some possible smi operations (like + and -) when (at least) one
2246 // of the operands is a literal smi. With this optimization, the
2247 // performance of the system is increased by ~15%, and the generated
2248 // code size is increased by ~1% (measured on a combination of
2249 // different benchmarks).
2250
mads.s.ager31e71382008-08-13 09:32:07 +00002251 // sp[0] : operand
2252
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002253 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254
2255 Label exit;
mads.s.ager31e71382008-08-13 09:32:07 +00002256 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257
2258 switch (op) {
2259 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002260 DeferredCode* deferred =
2261 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002263 __ add(r0, r0, Operand(value), SetCC);
2264 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002266 __ b(ne, deferred->enter());
2267 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 break;
2269 }
2270
2271 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002272 DeferredCode* deferred =
2273 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002276 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002278 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002280 __ b(vs, deferred->enter());
2281 __ tst(r0, Operand(kSmiTagMask));
2282 __ b(ne, deferred->enter());
2283 __ bind(deferred->exit());
2284 break;
2285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002287 case Token::BIT_OR:
2288 case Token::BIT_XOR:
2289 case Token::BIT_AND: {
2290 DeferredCode* deferred =
2291 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
2292 __ tst(r0, Operand(kSmiTagMask));
2293 __ b(ne, deferred->enter());
2294 switch (op) {
2295 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
2296 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
2297 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
2298 default: UNREACHABLE();
2299 }
2300 __ bind(deferred->exit());
2301 break;
2302 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002304 case Token::SHL:
2305 case Token::SHR:
2306 case Token::SAR: {
2307 if (reversed) {
2308 __ mov(ip, Operand(value));
2309 __ push(ip);
2310 __ push(r0);
2311 GenericBinaryOperation(op);
2312
2313 } else {
2314 int shift_value = int_value & 0x1f; // least significant 5 bits
2315 DeferredCode* deferred =
2316 new DeferredInlinedSmiOperation(this, op, shift_value, false);
2317 __ tst(r0, Operand(kSmiTagMask));
2318 __ b(ne, deferred->enter());
2319 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
2320 switch (op) {
2321 case Token::SHL: {
2322 __ mov(r2, Operand(r2, LSL, shift_value));
2323 // check that the *unsigned* result fits in a smi
2324 __ add(r3, r2, Operand(0x40000000), SetCC);
2325 __ b(mi, deferred->enter());
2326 break;
2327 }
2328 case Token::SHR: {
2329 // LSR by immediate 0 means shifting 32 bits.
2330 if (shift_value != 0) {
2331 __ mov(r2, Operand(r2, LSR, shift_value));
2332 }
2333 // check that the *unsigned* result fits in a smi
2334 // neither of the two high-order bits can be set:
2335 // - 0x80000000: high bit would be lost when smi tagging
2336 // - 0x40000000: this number would convert to negative when
2337 // smi tagging these two cases can only happen with shifts
2338 // by 0 or 1 when handed a valid smi
2339 __ and_(r3, r2, Operand(0xc0000000), SetCC);
2340 __ b(ne, deferred->enter());
2341 break;
2342 }
2343 case Token::SAR: {
2344 if (shift_value != 0) {
2345 // ASR by immediate 0 means shifting 32 bits.
2346 __ mov(r2, Operand(r2, ASR, shift_value));
2347 }
2348 break;
2349 }
2350 default: UNREACHABLE();
2351 }
2352 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
2353 __ bind(deferred->exit());
2354 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 break;
2356 }
2357
2358 default:
2359 if (!reversed) {
mads.s.ager31e71382008-08-13 09:32:07 +00002360 __ push(r0);
2361 __ mov(r0, Operand(value));
2362 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363 } else {
2364 __ mov(ip, Operand(value));
2365 __ push(ip);
mads.s.ager31e71382008-08-13 09:32:07 +00002366 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 }
kasper.lund7276f142008-07-30 08:49:36 +00002368 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 break;
2370 }
2371
2372 __ bind(&exit);
2373}
2374
2375
2376void ArmCodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +00002377 // sp[0] : y
2378 // sp[1] : x
2379 // result : cc register
2380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 // Strict only makes sense for equality comparisons.
2382 ASSERT(!strict || cc == eq);
2383
2384 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002385 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
2386 if (cc == gt || cc == le) {
2387 cc = ReverseCondition(cc);
mads.s.ager31e71382008-08-13 09:32:07 +00002388 __ pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002389 __ pop(r0);
2390 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002391 __ pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002392 __ pop(r1);
2393 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394 __ orr(r2, r0, Operand(r1));
2395 __ tst(r2, Operand(kSmiTagMask));
2396 __ b(eq, &smi);
2397
2398 // Perform non-smi comparison by runtime call.
2399 __ push(r1);
2400
2401 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002402 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 int argc;
2404 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002405 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 argc = 1;
2407 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002408 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409 int ncr; // NaN compare result
2410 if (cc == lt || cc == le) {
2411 ncr = GREATER;
2412 } else {
2413 ASSERT(cc == gt || cc == ge); // remaining cases
2414 ncr = LESS;
2415 }
mads.s.ager31e71382008-08-13 09:32:07 +00002416 __ push(r0);
2417 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 argc = 2;
2419 }
2420
2421 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2422 // tagged as a small integer.
mads.s.ager31e71382008-08-13 09:32:07 +00002423 __ push(r0);
2424 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002425 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 __ cmp(r0, Operand(0));
2427 __ b(&exit);
2428
2429 // test smi equality by pointer comparison.
2430 __ bind(&smi);
2431 __ cmp(r1, Operand(r0));
2432
2433 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434 cc_reg_ = cc;
2435}
2436
2437
kasper.lund7276f142008-07-30 08:49:36 +00002438class CallFunctionStub: public CodeStub {
2439 public:
2440 explicit CallFunctionStub(int argc) : argc_(argc) {}
2441
2442 void Generate(MacroAssembler* masm);
2443
2444 private:
2445 int argc_;
2446
2447 const char* GetName() { return "CallFuntionStub"; }
2448
2449#if defined(DEBUG)
2450 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
2451#endif // defined(DEBUG)
2452
2453 Major MajorKey() { return CallFunction; }
2454 int MinorKey() { return argc_; }
2455};
2456
2457
2458void CallFunctionStub::Generate(MacroAssembler* masm) {
2459 Label slow;
kasper.lund7276f142008-07-30 08:49:36 +00002460 // Get the function to call from the stack.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002461 // function, receiver [, arguments]
kasper.lund7276f142008-07-30 08:49:36 +00002462 masm->ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
2463
2464 // Check that the function is really a JavaScript function.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002465 // r1: pushed function (to be verified)
kasper.lund7276f142008-07-30 08:49:36 +00002466 masm->tst(r1, Operand(kSmiTagMask));
2467 masm->b(eq, &slow);
2468 // Get the map of the function object.
2469 masm->ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2470 masm->ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2471 masm->cmp(r2, Operand(JS_FUNCTION_TYPE));
2472 masm->b(ne, &slow);
2473
2474 // Fast-case: Invoke the function now.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002475 // r1: pushed function
2476 ParameterCount actual(argc_);
2477 masm->InvokeFunction(r1, actual, JUMP_FUNCTION);
kasper.lund7276f142008-07-30 08:49:36 +00002478
2479 // Slow-case: Non-function called.
2480 masm->bind(&slow);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002481 masm->mov(r0, Operand(argc_)); // Setup the number of arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002482 masm->InvokeBuiltin(Builtins::CALL_NON_FUNCTION, JUMP_JS);
kasper.lund7276f142008-07-30 08:49:36 +00002483}
2484
2485
mads.s.ager31e71382008-08-13 09:32:07 +00002486// Call the function on the stack with the given arguments.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487void ArmCodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
2488 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002490 for (int i = 0; i < args->length(); i++) {
2491 Load(args->at(i));
2492 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493
kasper.lund7276f142008-07-30 08:49:36 +00002494 // Record the position for debugging purposes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 __ RecordPosition(position);
2496
kasper.lund7276f142008-07-30 08:49:36 +00002497 // Use the shared code stub to call the function.
2498 CallFunctionStub call_function(args->length());
2499 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500
2501 // Restore context and pop function from the stack.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002503 __ pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504}
2505
2506
2507void ArmCodeGenerator::Branch(bool if_true, Label* L) {
2508 ASSERT(has_cc());
2509 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
2510 __ b(cc, L);
2511 cc_reg_ = al;
2512}
2513
2514
2515void ArmCodeGenerator::CheckStack() {
2516 if (FLAG_check_stack) {
2517 Comment cmnt(masm_, "[ check stack");
2518 StackCheckStub stub;
2519 __ CallStub(&stub);
2520 }
2521}
2522
2523
2524void ArmCodeGenerator::VisitBlock(Block* node) {
2525 Comment cmnt(masm_, "[ Block");
2526 if (FLAG_debug_info) RecordStatementPosition(node);
2527 node->set_break_stack_height(break_stack_height_);
2528 VisitStatements(node->statements());
2529 __ bind(node->break_target());
2530}
2531
2532
2533void ArmCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00002534 __ mov(r0, Operand(pairs));
2535 __ push(r0);
2536 __ push(cp);
2537 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
2538 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002540 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541}
2542
2543
2544void ArmCodeGenerator::VisitDeclaration(Declaration* node) {
2545 Comment cmnt(masm_, "[ Declaration");
2546 Variable* var = node->proxy()->var();
2547 ASSERT(var != NULL); // must have been resolved
2548 Slot* slot = var->slot();
2549
2550 // If it was not possible to allocate the variable at compile time,
2551 // we need to "declare" it at runtime to make sure it actually
2552 // exists in the local context.
2553 if (slot != NULL && slot->type() == Slot::LOOKUP) {
2554 // Variables with a "LOOKUP" slot were introduced as non-locals
2555 // during variable resolution and must have mode DYNAMIC.
2556 ASSERT(var->mode() == Variable::DYNAMIC);
2557 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00002558 __ push(cp);
2559 __ mov(r0, Operand(var->name()));
2560 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 // Declaration nodes are always declared in only two modes.
2562 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
2563 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00002564 __ mov(r0, Operand(Smi::FromInt(attr)));
2565 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566 // Push initial value, if any.
2567 // Note: For variables we must not push an initial value (such as
2568 // 'undefined') because we may have a (legal) redeclaration and we
2569 // must not destroy the current value.
2570 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00002571 __ mov(r0, Operand(Factory::the_hole_value()));
2572 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 } else if (node->fun() != NULL) {
2574 Load(node->fun());
2575 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002576 __ mov(r0, Operand(0)); // no initial value!
2577 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002578 }
2579 __ CallRuntime(Runtime::kDeclareContextSlot, 5);
mads.s.ager31e71382008-08-13 09:32:07 +00002580 __ push(r0);
2581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 return;
2583 }
2584
2585 ASSERT(!var->is_global());
2586
2587 // If we have a function or a constant, we need to initialize the variable.
2588 Expression* val = NULL;
2589 if (node->mode() == Variable::CONST) {
2590 val = new Literal(Factory::the_hole_value());
2591 } else {
2592 val = node->fun(); // NULL if we don't have a function
2593 }
2594
2595 if (val != NULL) {
2596 // Set initial value.
2597 Reference target(this, node->proxy());
2598 Load(val);
2599 SetValue(&target);
2600 // Get rid of the assigned value (declarations are statements).
mads.s.ager31e71382008-08-13 09:32:07 +00002601 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602 }
2603}
2604
2605
2606void ArmCodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
2607 Comment cmnt(masm_, "[ ExpressionStatement");
2608 if (FLAG_debug_info) RecordStatementPosition(node);
2609 Expression* expression = node->expression();
2610 expression->MarkAsStatement();
2611 Load(expression);
mads.s.ager31e71382008-08-13 09:32:07 +00002612 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613}
2614
2615
2616void ArmCodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
2617 Comment cmnt(masm_, "// EmptyStatement");
2618 // nothing to do
2619}
2620
2621
2622void ArmCodeGenerator::VisitIfStatement(IfStatement* node) {
2623 Comment cmnt(masm_, "[ IfStatement");
2624 // Generate different code depending on which
2625 // parts of the if statement are present or not.
2626 bool has_then_stm = node->HasThenStatement();
2627 bool has_else_stm = node->HasElseStatement();
2628
2629 if (FLAG_debug_info) RecordStatementPosition(node);
2630
2631 Label exit;
2632 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002633 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 Label then;
2635 Label else_;
2636 // if (cond)
2637 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
2638 Branch(false, &else_);
2639 // then
2640 __ bind(&then);
2641 Visit(node->then_statement());
2642 __ b(&exit);
2643 // else
2644 __ bind(&else_);
2645 Visit(node->else_statement());
2646
2647 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002648 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 ASSERT(!has_else_stm);
2650 Label then;
2651 // if (cond)
2652 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &exit, true);
2653 Branch(false, &exit);
2654 // then
2655 __ bind(&then);
2656 Visit(node->then_statement());
2657
2658 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002659 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 ASSERT(!has_then_stm);
2661 Label else_;
2662 // if (!cond)
2663 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &else_, true);
2664 Branch(true, &exit);
2665 // else
2666 __ bind(&else_);
2667 Visit(node->else_statement());
2668
2669 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002670 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 ASSERT(!has_then_stm && !has_else_stm);
2672 // if (cond)
2673 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &exit, false);
2674 if (has_cc()) {
2675 cc_reg_ = al;
2676 } else {
2677 __ pop(r0); // __ Pop(no_reg)
2678 }
2679 }
2680
2681 // end
2682 __ bind(&exit);
2683}
2684
2685
2686void ArmCodeGenerator::CleanStack(int num_bytes) {
2687 ASSERT(num_bytes >= 0);
2688 if (num_bytes > 0) {
mads.s.ager31e71382008-08-13 09:32:07 +00002689 __ add(sp, sp, Operand(num_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 }
2691}
2692
2693
2694void ArmCodeGenerator::VisitContinueStatement(ContinueStatement* node) {
2695 Comment cmnt(masm_, "[ ContinueStatement");
2696 if (FLAG_debug_info) RecordStatementPosition(node);
2697 CleanStack(break_stack_height_ - node->target()->break_stack_height());
2698 __ b(node->target()->continue_target());
2699}
2700
2701
2702void ArmCodeGenerator::VisitBreakStatement(BreakStatement* node) {
2703 Comment cmnt(masm_, "[ BreakStatement");
2704 if (FLAG_debug_info) RecordStatementPosition(node);
2705 CleanStack(break_stack_height_ - node->target()->break_stack_height());
2706 __ b(node->target()->break_target());
2707}
2708
2709
2710void ArmCodeGenerator::VisitReturnStatement(ReturnStatement* node) {
2711 Comment cmnt(masm_, "[ ReturnStatement");
2712 if (FLAG_debug_info) RecordStatementPosition(node);
2713 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00002714 // Move the function result into r0.
2715 __ pop(r0);
2716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 __ b(&function_return_);
2718}
2719
2720
2721void ArmCodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
2722 Comment cmnt(masm_, "[ WithEnterStatement");
2723 if (FLAG_debug_info) RecordStatementPosition(node);
2724 Load(node->expression());
kasper.lund7276f142008-07-30 08:49:36 +00002725 __ CallRuntime(Runtime::kPushContext, 1);
2726 if (kDebug) {
2727 Label verified_true;
2728 __ cmp(r0, Operand(cp));
2729 __ b(eq, &verified_true);
2730 __ stop("PushContext: r0 is expected to be the same as cp");
2731 __ bind(&verified_true);
2732 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 // Update context local.
2734 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2735}
2736
2737
2738void ArmCodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
2739 Comment cmnt(masm_, "[ WithExitStatement");
2740 // Pop context.
2741 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2742 // Update context local.
2743 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2744}
2745
2746
2747void ArmCodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
2748 Comment cmnt(masm_, "[ SwitchStatement");
2749 if (FLAG_debug_info) RecordStatementPosition(node);
2750 node->set_break_stack_height(break_stack_height_);
2751
2752 Load(node->tag());
2753
2754 Label next, fall_through, default_case;
2755 ZoneList<CaseClause*>* cases = node->cases();
2756 int length = cases->length();
2757
2758 for (int i = 0; i < length; i++) {
2759 CaseClause* clause = cases->at(i);
2760
2761 Comment cmnt(masm_, "[ case clause");
2762
2763 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002764 // Continue matching cases. The program will execute the default case's
2765 // statements if it does not match any of the cases.
2766 __ b(&next);
2767
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 // Bind the default case label, so we can branch to it when we
2769 // have compared against all other cases.
2770 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002771 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772 } else {
2773 __ bind(&next);
2774 next.Unuse();
mads.s.ager31e71382008-08-13 09:32:07 +00002775 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776 __ push(r0); // duplicate TOS
2777 Load(clause->label());
2778 Comparison(eq, true);
2779 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 }
2781
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002782 // Entering the case statement for the first time. Remove the switch value
2783 // from the stack.
2784 __ pop(r0);
2785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002787 // This is also the target for the fall through from the previous case's
2788 // statements which has to skip over the matching code and the popping of
2789 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 __ bind(&fall_through);
2791 fall_through.Unuse();
2792 VisitStatements(clause->statements());
2793 __ b(&fall_through);
2794 }
2795
2796 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002797 // Reached the end of the case statements without matching any of the cases.
2798 if (default_case.is_bound()) {
2799 // A default case exists -> execute its statements.
2800 __ b(&default_case);
2801 } else {
2802 // Remove the switch value from the stack.
2803 __ pop(r0);
2804 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805
2806 __ bind(&fall_through);
2807 __ bind(node->break_target());
2808}
2809
2810
2811void ArmCodeGenerator::VisitLoopStatement(LoopStatement* node) {
2812 Comment cmnt(masm_, "[ LoopStatement");
2813 if (FLAG_debug_info) RecordStatementPosition(node);
2814 node->set_break_stack_height(break_stack_height_);
2815
2816 // simple condition analysis
2817 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
2818 if (node->cond() == NULL) {
2819 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2820 info = ALWAYS_TRUE;
2821 } else {
2822 Literal* lit = node->cond()->AsLiteral();
2823 if (lit != NULL) {
2824 if (lit->IsTrue()) {
2825 info = ALWAYS_TRUE;
2826 } else if (lit->IsFalse()) {
2827 info = ALWAYS_FALSE;
2828 }
2829 }
2830 }
2831
2832 Label loop, entry;
2833
2834 // init
2835 if (node->init() != NULL) {
2836 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2837 Visit(node->init());
2838 }
2839 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
2840 __ b(&entry);
2841 }
2842
2843 // body
2844 __ bind(&loop);
2845 Visit(node->body());
2846
2847 // next
2848 __ bind(node->continue_target());
2849 if (node->next() != NULL) {
2850 // Record source position of the statement as this code which is after the
2851 // code for the body actually belongs to the loop statement and not the
2852 // body.
2853 if (FLAG_debug_info) __ RecordPosition(node->statement_pos());
2854 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2855 Visit(node->next());
2856 }
2857
2858 // cond
2859 __ bind(&entry);
2860 switch (info) {
2861 case ALWAYS_TRUE:
2862 CheckStack(); // TODO(1222600): ignore if body contains calls.
2863 __ b(&loop);
2864 break;
2865 case ALWAYS_FALSE:
2866 break;
2867 case DONT_KNOW:
2868 CheckStack(); // TODO(1222600): ignore if body contains calls.
2869 LoadCondition(node->cond(),
2870 CodeGenState::LOAD,
2871 &loop,
2872 node->break_target(),
2873 true);
2874 Branch(true, &loop);
2875 break;
2876 }
2877
2878 // exit
2879 __ bind(node->break_target());
2880}
2881
2882
2883void ArmCodeGenerator::VisitForInStatement(ForInStatement* node) {
2884 Comment cmnt(masm_, "[ ForInStatement");
2885 if (FLAG_debug_info) RecordStatementPosition(node);
2886
2887 // We keep stuff on the stack while the body is executing.
2888 // Record it, so that a break/continue crossing this statement
2889 // can restore the stack.
2890 const int kForInStackSize = 5 * kPointerSize;
2891 break_stack_height_ += kForInStackSize;
2892 node->set_break_stack_height(break_stack_height_);
2893
2894 Label loop, next, entry, cleanup, exit, primitive, jsobject;
2895 Label filter_key, end_del_check, fixed_array, non_string;
2896
2897 // Get the object to enumerate over (converted to JSObject).
2898 Load(node->enumerable());
mads.s.ager31e71382008-08-13 09:32:07 +00002899 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900
2901 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2902 // to the specification. 12.6.4 mandates a call to ToObject.
2903 __ cmp(r0, Operand(Factory::undefined_value()));
2904 __ b(eq, &exit);
2905 __ cmp(r0, Operand(Factory::null_value()));
2906 __ b(eq, &exit);
2907
2908 // Stack layout in body:
2909 // [iteration counter (Smi)]
2910 // [length of array]
2911 // [FixedArray]
2912 // [Map or 0]
2913 // [Object]
2914
2915 // Check if enumerable is already a JSObject
2916 __ tst(r0, Operand(kSmiTagMask));
2917 __ b(eq, &primitive);
2918 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2919 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002920 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921 __ b(hs, &jsobject);
2922
2923 __ bind(&primitive);
mads.s.ager31e71382008-08-13 09:32:07 +00002924 __ push(r0);
2925 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002926 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002927
2928
2929 __ bind(&jsobject);
2930
2931 // Get the set of properties (as a FixedArray or Map).
2932 __ push(r0); // duplicate the object being enumerated
mads.s.ager31e71382008-08-13 09:32:07 +00002933 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002934 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
2935
2936 // If we got a Map, we can do a fast modification check.
2937 // Otherwise, we got a FixedArray, and we have to do a slow check.
2938 __ mov(r2, Operand(r0));
2939 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
2940 __ cmp(r1, Operand(Factory::meta_map()));
2941 __ b(ne, &fixed_array);
2942
2943 // Get enum cache
2944 __ mov(r1, Operand(r0));
2945 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2946 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2947 __ ldr(r2,
2948 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2949
mads.s.ager31e71382008-08-13 09:32:07 +00002950 __ push(r0); // map
2951 __ push(r2); // enum cache bridge cache
2952 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002953 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00002954 __ push(r0);
2955 __ mov(r0, Operand(Smi::FromInt(0)));
2956 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 __ b(&entry);
2958
2959
2960 __ bind(&fixed_array);
2961
2962 __ mov(r1, Operand(Smi::FromInt(0)));
2963 __ push(r1); // insert 0 in place of Map
mads.s.ager31e71382008-08-13 09:32:07 +00002964 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965
2966 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002967 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00002969 __ push(r0);
2970 __ mov(r0, Operand(Smi::FromInt(0))); // init index
2971 __ push(r0);
2972
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 __ b(&entry);
2974
2975 // Body.
2976 __ bind(&loop);
2977 Visit(node->body());
2978
2979 // Next.
2980 __ bind(node->continue_target());
2981 __ bind(&next);
mads.s.ager31e71382008-08-13 09:32:07 +00002982 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983 __ add(r0, r0, Operand(Smi::FromInt(1)));
mads.s.ager31e71382008-08-13 09:32:07 +00002984 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985
2986 // Condition.
2987 __ bind(&entry);
2988
mads.s.ager31e71382008-08-13 09:32:07 +00002989 // sp[0] : index
2990 // sp[1] : array/enum cache length
2991 // sp[2] : array or enum cache
2992 // sp[3] : 0 or map
2993 // sp[4] : enumerable
2994 __ ldr(r0, MemOperand(sp, 0 * kPointerSize)); // load the current count
2995 __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // load the length
2996 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997 __ b(hs, &cleanup);
2998
mads.s.ager31e71382008-08-13 09:32:07 +00002999 __ ldr(r0, MemOperand(sp, 0 * kPointerSize));
3000
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003001 // Get the i'th entry of the array.
mads.s.ager31e71382008-08-13 09:32:07 +00003002 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3004 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
3005
3006 // Get Map or 0.
mads.s.ager31e71382008-08-13 09:32:07 +00003007 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 // Check if this (still) matches the map of the enumerable.
3009 // If not, we have to filter the key.
mads.s.ager31e71382008-08-13 09:32:07 +00003010 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3012 __ cmp(r1, Operand(r2));
3013 __ b(eq, &end_del_check);
3014
3015 // Convert the entry to a string (or null if it isn't a property anymore).
mads.s.ager31e71382008-08-13 09:32:07 +00003016 __ ldr(r0, MemOperand(sp, 4 * kPointerSize)); // push enumerable
3017 __ push(r0);
3018 __ push(r3); // push entry
3019 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003020 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022
3023 // If the property has been removed while iterating, we just skip it.
3024 __ cmp(r3, Operand(Factory::null_value()));
3025 __ b(eq, &next);
3026
3027
3028 __ bind(&end_del_check);
3029
3030 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00003031 // r3: i'th entry of the enum cache (or string there of)
3032 __ push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033 { Reference each(this, node->each());
3034 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00003035 if (each.size() > 0) {
3036 __ ldr(r0, MemOperand(sp, kPointerSize * each.size()));
3037 __ push(r0);
3038 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039 SetValue(&each);
mads.s.ager31e71382008-08-13 09:32:07 +00003040 if (each.size() > 0) {
3041 __ pop(r0); // discard the value
3042 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003043 }
3044 }
mads.s.ager31e71382008-08-13 09:32:07 +00003045 __ pop(); // pop the i'th entry pushed above
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046 CheckStack(); // TODO(1222600): ignore if body contains calls.
3047 __ jmp(&loop);
3048
3049 // Cleanup.
3050 __ bind(&cleanup);
3051 __ bind(node->break_target());
mads.s.ager31e71382008-08-13 09:32:07 +00003052 __ add(sp, sp, Operand(5 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053
3054 // Exit.
3055 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056
3057 break_stack_height_ -= kForInStackSize;
3058}
3059
3060
3061void ArmCodeGenerator::VisitTryCatch(TryCatch* node) {
3062 Comment cmnt(masm_, "[ TryCatch");
3063
3064 Label try_block, exit;
3065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066 __ bl(&try_block);
3067
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 // --- Catch block ---
3069
3070 // Store the caught exception in the catch variable.
mads.s.ager31e71382008-08-13 09:32:07 +00003071 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072 { Reference ref(this, node->catch_var());
3073 // Load the exception to the top of the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003074 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
3075 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003076 SetValue(&ref);
mads.s.ager31e71382008-08-13 09:32:07 +00003077 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 }
3079
3080 // Remove the exception from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003081 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082
3083 VisitStatements(node->catch_block()->statements());
3084 __ b(&exit);
3085
3086
3087 // --- Try block ---
3088 __ bind(&try_block);
3089
3090 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
3091
3092 // Introduce shadow labels for all escapes from the try block,
3093 // including returns. We should probably try to unify the escaping
3094 // labels and the return label.
3095 int nof_escapes = node->escaping_labels()->length();
3096 List<LabelShadow*> shadows(1 + nof_escapes);
3097 shadows.Add(new LabelShadow(&function_return_));
3098 for (int i = 0; i < nof_escapes; i++) {
3099 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
3100 }
3101
3102 // Generate code for the statements in the try block.
3103 VisitStatements(node->try_block()->statements());
mads.s.ager31e71382008-08-13 09:32:07 +00003104 __ pop(r0); // Discard the result.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003105
3106 // Stop the introduced shadowing and count the number of required unlinks.
3107 int nof_unlinks = 0;
3108 for (int i = 0; i <= nof_escapes; i++) {
3109 shadows[i]->StopShadowing();
3110 if (shadows[i]->is_linked()) nof_unlinks++;
3111 }
3112
3113 // Unlink from try chain.
3114 // TOS contains code slot
3115 const int kNextOffset = StackHandlerConstants::kNextOffset +
3116 StackHandlerConstants::kAddressDisplacement;
3117 __ ldr(r1, MemOperand(sp, kNextOffset)); // read next_sp
3118 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3119 __ str(r1, MemOperand(r3));
3120 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
3121 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
3122 // Code slot popped.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 if (nof_unlinks > 0) __ b(&exit);
3124
3125 // Generate unlink code for all used shadow labels.
3126 for (int i = 0; i <= nof_escapes; i++) {
3127 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00003128 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 __ bind(shadows[i]);
3130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003131 // Reload sp from the top handler, because some statements that we
3132 // break from (eg, for...in) may have left stuff on the stack.
3133 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3134 __ ldr(sp, MemOperand(r3));
3135
3136 __ ldr(r1, MemOperand(sp, kNextOffset));
3137 __ str(r1, MemOperand(r3));
3138 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
3139 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
3140 // Code slot popped.
3141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 __ b(shadows[i]->shadowed());
3143 }
3144 }
3145
3146 __ bind(&exit);
3147}
3148
3149
3150void ArmCodeGenerator::VisitTryFinally(TryFinally* node) {
3151 Comment cmnt(masm_, "[ TryFinally");
3152
3153 // State: Used to keep track of reason for entering the finally
3154 // block. Should probably be extended to hold information for
3155 // break/continue from within the try block.
3156 enum { FALLING, THROWING, JUMPING };
3157
3158 Label exit, unlink, try_block, finally_block;
3159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 __ bl(&try_block);
3161
mads.s.ager31e71382008-08-13 09:32:07 +00003162 __ push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 // In case of thrown exceptions, this is where we continue.
3164 __ mov(r2, Operand(Smi::FromInt(THROWING)));
3165 __ b(&finally_block);
3166
3167
3168 // --- Try block ---
3169 __ bind(&try_block);
3170
3171 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
3172
3173 // Introduce shadow labels for all escapes from the try block,
3174 // including returns. We should probably try to unify the escaping
3175 // labels and the return label.
3176 int nof_escapes = node->escaping_labels()->length();
3177 List<LabelShadow*> shadows(1 + nof_escapes);
3178 shadows.Add(new LabelShadow(&function_return_));
3179 for (int i = 0; i < nof_escapes; i++) {
3180 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
3181 }
3182
3183 // Generate code for the statements in the try block.
3184 VisitStatements(node->try_block()->statements());
3185
3186 // Stop the introduced shadowing and count the number of required
3187 // unlinks.
3188 int nof_unlinks = 0;
3189 for (int i = 0; i <= nof_escapes; i++) {
3190 shadows[i]->StopShadowing();
3191 if (shadows[i]->is_linked()) nof_unlinks++;
3192 }
3193
3194 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00003195 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
3196 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003197 __ mov(r2, Operand(Smi::FromInt(FALLING)));
3198 if (nof_unlinks > 0) __ b(&unlink);
3199
3200 // Generate code that sets the state for all used shadow labels.
3201 for (int i = 0; i <= nof_escapes; i++) {
3202 if (shadows[i]->is_linked()) {
3203 __ bind(shadows[i]);
mads.s.ager31e71382008-08-13 09:32:07 +00003204 if (shadows[i]->shadowed() == &function_return_) {
3205 __ push(r0); // Materialize the return value on the stack
3206 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 // Fake TOS for break and continue (not return).
mads.s.ager31e71382008-08-13 09:32:07 +00003208 __ mov(r0, Operand(Factory::undefined_value()));
3209 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003210 }
3211 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
3212 __ b(&unlink);
3213 }
3214 }
3215
mads.s.ager31e71382008-08-13 09:32:07 +00003216 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003217 __ bind(&unlink);
3218
mads.s.ager31e71382008-08-13 09:32:07 +00003219 __ pop(r0); // Store TOS in r0 across stack manipulation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220 // Reload sp from the top handler, because some statements that we
3221 // break from (eg, for...in) may have left stuff on the stack.
3222 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3223 __ ldr(sp, MemOperand(r3));
3224 const int kNextOffset = StackHandlerConstants::kNextOffset +
3225 StackHandlerConstants::kAddressDisplacement;
3226 __ ldr(r1, MemOperand(sp, kNextOffset));
3227 __ str(r1, MemOperand(r3));
3228 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
3229 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
3230 // Code slot popped.
mads.s.ager31e71382008-08-13 09:32:07 +00003231 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232
3233 // --- Finally block ---
3234 __ bind(&finally_block);
3235
3236 // Push the state on the stack. If necessary move the state to a
3237 // local variable to avoid having extra values on the stack while
3238 // evaluating the finally block.
mads.s.ager31e71382008-08-13 09:32:07 +00003239 __ push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003240 if (node->finally_var() != NULL) {
3241 Reference target(this, node->finally_var());
3242 SetValue(&target);
3243 ASSERT(target.size() == 0); // no extra stuff on the stack
mads.s.ager31e71382008-08-13 09:32:07 +00003244 __ pop(); // remove the extra avalue that was pushed above
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003245 }
3246
3247 // Generate code for the statements in the finally block.
3248 VisitStatements(node->finally_block()->statements());
3249
mads.s.ager31e71382008-08-13 09:32:07 +00003250 // Get the state from the stack - or the local variable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251 if (node->finally_var() != NULL) {
3252 Reference target(this, node->finally_var());
3253 GetValue(&target);
3254 }
mads.s.ager31e71382008-08-13 09:32:07 +00003255 __ pop(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003256
mads.s.ager31e71382008-08-13 09:32:07 +00003257 __ pop(r0); // Restore value or faked TOS.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 // Generate code that jumps to the right destination for all used
3259 // shadow labels.
3260 for (int i = 0; i <= nof_escapes; i++) {
3261 if (shadows[i]->is_bound()) {
3262 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
3263 if (shadows[i]->shadowed() != &function_return_) {
3264 Label next;
3265 __ b(ne, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 __ b(shadows[i]->shadowed());
3267 __ bind(&next);
3268 } else {
3269 __ b(eq, shadows[i]->shadowed());
3270 }
3271 }
3272 }
3273
3274 // Check if we need to rethrow the exception.
3275 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
3276 __ b(ne, &exit);
3277
3278 // Rethrow exception.
mads.s.ager31e71382008-08-13 09:32:07 +00003279 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280 __ CallRuntime(Runtime::kReThrow, 1);
3281
3282 // Done.
3283 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284}
3285
3286
3287void ArmCodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
3288 Comment cmnt(masm_, "[ DebuggerStatament");
3289 if (FLAG_debug_info) RecordStatementPosition(node);
3290 __ CallRuntime(Runtime::kDebugBreak, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003291 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292}
3293
3294
3295void ArmCodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
3296 ASSERT(boilerplate->IsBoilerplate());
3297
3298 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003299 __ mov(r0, Operand(boilerplate));
3300 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301
3302 // Create a new closure.
mads.s.ager31e71382008-08-13 09:32:07 +00003303 __ push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003304 __ CallRuntime(Runtime::kNewClosure, 2);
mads.s.ager31e71382008-08-13 09:32:07 +00003305 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003306}
3307
3308
3309void ArmCodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
3310 Comment cmnt(masm_, "[ FunctionLiteral");
3311
3312 // Build the function boilerplate and instantiate it.
3313 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00003314 // Check for stack-overflow exception.
3315 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003316 InstantiateBoilerplate(boilerplate);
3317}
3318
3319
3320void ArmCodeGenerator::VisitFunctionBoilerplateLiteral(
3321 FunctionBoilerplateLiteral* node) {
3322 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
3323 InstantiateBoilerplate(node->boilerplate());
3324}
3325
3326
3327void ArmCodeGenerator::VisitConditional(Conditional* node) {
3328 Comment cmnt(masm_, "[ Conditional");
3329 Label then, else_, exit;
3330 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
3331 Branch(false, &else_);
3332 __ bind(&then);
3333 Load(node->then_expression(), access());
3334 __ b(&exit);
3335 __ bind(&else_);
3336 Load(node->else_expression(), access());
3337 __ bind(&exit);
3338}
3339
3340
3341void ArmCodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003342 ASSERT(access() != CodeGenState::UNDEFINED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003343 Comment cmnt(masm_, "[ Slot");
3344
3345 if (node->type() == Slot::LOOKUP) {
3346 ASSERT(node->var()->mode() == Variable::DYNAMIC);
3347
3348 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00003349 __ push(cp);
3350 __ mov(r0, Operand(node->var()->name()));
3351 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003353 if (access() == CodeGenState::LOAD) {
3354 __ CallRuntime(Runtime::kLoadContextSlot, 2);
3355 } else {
3356 ASSERT(access() == CodeGenState::LOAD_TYPEOF_EXPR);
3357 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003359 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360
3361 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003362 // Note: We would like to keep the assert below, but it fires because of
3363 // some nasty code in LoadTypeofExpression() which should be removed...
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 // ASSERT(node->var()->mode() != Variable::DYNAMIC);
3365
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003366 // Special handling for locals allocated in registers.
3367 __ ldr(r0, SlotOperand(node, r2));
3368 __ push(r0);
3369 if (node->var()->mode() == Variable::CONST) {
3370 // Const slots may contain 'the hole' value (the constant hasn't been
3371 // initialized yet) which needs to be converted into the 'undefined'
3372 // value.
3373 Comment cmnt(masm_, "[ Unhole const");
3374 __ pop(r0);
3375 __ cmp(r0, Operand(Factory::the_hole_value()));
3376 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
3377 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003378 }
3379 }
3380}
3381
3382
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003383void ArmCodeGenerator::VisitVariableProxy(VariableProxy* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003384 Comment cmnt(masm_, "[ VariableProxy");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003385 Variable* var_node = node->var();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003386
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003387 Expression* expr = var_node->rewrite();
3388 if (expr != NULL) {
3389 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003391 ASSERT(var_node->is_global());
3392 if (is_referenced()) {
3393 if (var_node->AsProperty() != NULL) {
3394 __ RecordPosition(var_node->AsProperty()->position());
3395 }
3396 GetReferenceProperty(new Literal(var_node->name()));
3397 } else {
3398 Reference property(this, node);
3399 GetValue(&property);
3400 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003401 }
3402}
3403
3404
3405void ArmCodeGenerator::VisitLiteral(Literal* node) {
3406 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00003407 __ mov(r0, Operand(node->handle()));
3408 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003409}
3410
3411
3412void ArmCodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
3413 Comment cmnt(masm_, "[ RexExp Literal");
3414
3415 // Retrieve the literal array and check the allocated entry.
3416
3417 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003418 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419
3420 // Load the literals array of the function.
3421 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3422
3423 // Load the literal at the ast saved index.
3424 int literal_offset =
3425 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3426 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3427
3428 Label done;
3429 __ cmp(r2, Operand(Factory::undefined_value()));
3430 __ b(ne, &done);
3431
3432 // If the entry is undefined we call the runtime system to computed
3433 // the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00003434 __ push(r1); // literal array (0)
3435 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
3436 __ push(r0); // literal index (1)
3437 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
3438 __ push(r0);
3439 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
3440 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003442 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443
mads.s.ager31e71382008-08-13 09:32:07 +00003444 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445 // Push the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00003446 __ push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447}
3448
3449
3450// This deferred code stub will be used for creating the boilerplate
3451// by calling Runtime_CreateObjectLiteral.
3452// Each created boilerplate is stored in the JSFunction and they are
3453// therefore context dependent.
3454class ObjectLiteralDeferred: public DeferredCode {
3455 public:
3456 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
3457 : DeferredCode(generator), node_(node) {
3458 set_comment("[ ObjectLiteralDeferred");
3459 }
3460 virtual void Generate();
3461 private:
3462 ObjectLiteral* node_;
3463};
3464
3465
3466void ObjectLiteralDeferred::Generate() {
3467 // If the entry is undefined we call the runtime system to computed
3468 // the literal.
3469
3470 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00003471 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003472 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00003473 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
3474 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003475 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00003476 __ mov(r0, Operand(node_->constant_properties()));
3477 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00003479 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003480}
3481
3482
3483void ArmCodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
3484 Comment cmnt(masm_, "[ ObjectLiteral");
3485
3486 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
3487
3488 // Retrieve the literal array and check the allocated entry.
3489
3490 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003491 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492
3493 // Load the literals array of the function.
3494 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3495
3496 // Load the literal at the ast saved index.
3497 int literal_offset =
3498 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3499 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3500
3501 // Check whether we need to materialize the object literal boilerplate.
3502 // If so, jump to the deferred code.
3503 __ cmp(r2, Operand(Factory::undefined_value()));
3504 __ b(eq, deferred->enter());
3505 __ bind(deferred->exit());
3506
3507 // Push the object literal boilerplate.
mads.s.ager31e71382008-08-13 09:32:07 +00003508 __ push(r2);
3509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003510 // Clone the boilerplate object.
3511 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003512 __ push(r0); // save the result
3513 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514
3515 for (int i = 0; i < node->properties()->length(); i++) {
3516 ObjectLiteral::Property* property = node->properties()->at(i);
3517 Literal* key = property->key();
3518 Expression* value = property->value();
3519 switch (property->kind()) {
3520 case ObjectLiteral::Property::CONSTANT: break;
3521 case ObjectLiteral::Property::COMPUTED: // fall through
3522 case ObjectLiteral::Property::PROTOTYPE: {
mads.s.ager31e71382008-08-13 09:32:07 +00003523 __ push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524 Load(key);
3525 Load(value);
3526 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00003527 // restore r0
3528 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529 break;
3530 }
3531 case ObjectLiteral::Property::SETTER: {
3532 __ push(r0);
3533 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003534 __ mov(r0, Operand(Smi::FromInt(1)));
3535 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536 Load(value);
3537 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003538 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539 break;
3540 }
3541 case ObjectLiteral::Property::GETTER: {
3542 __ push(r0);
3543 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003544 __ mov(r0, Operand(Smi::FromInt(0)));
3545 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546 Load(value);
3547 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003548 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549 break;
3550 }
3551 }
3552 }
3553}
3554
3555
3556void ArmCodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
3557 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003558
3559 // Call runtime to create the array literal.
3560 __ mov(r0, Operand(node->literals()));
3561 __ push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003562 // Load the function of this frame.
3563 __ ldr(r0, FunctionOperand());
3564 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003565 __ push(r0);
3566 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
3567
3568 // Push the resulting array literal on the stack.
3569 __ push(r0);
3570
3571 // Generate code to set the elements in the array that are not
3572 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573 for (int i = 0; i < node->values()->length(); i++) {
3574 Expression* value = node->values()->at(i);
3575
3576 // If value is literal the property value is already
3577 // set in the boilerplate object.
3578 if (value->AsLiteral() == NULL) {
3579 // The property must be set by generated code.
3580 Load(value);
mads.s.ager31e71382008-08-13 09:32:07 +00003581 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582
3583 // Fetch the object literal
3584 __ ldr(r1, MemOperand(sp, 0));
3585 // Get the elements array.
3586 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3587
3588 // Write to the indexed properties array.
3589 int offset = i * kPointerSize + Array::kHeaderSize;
3590 __ str(r0, FieldMemOperand(r1, offset));
3591
3592 // Update the write barrier for the array address.
3593 __ mov(r3, Operand(offset));
3594 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003595 }
3596 }
3597}
3598
3599
3600void ArmCodeGenerator::VisitAssignment(Assignment* node) {
3601 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003602 if (FLAG_debug_info) RecordStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003604 Reference target(this, node->target());
3605 if (target.is_illegal()) return;
3606
3607 if (node->op() == Token::ASSIGN ||
3608 node->op() == Token::INIT_VAR ||
3609 node->op() == Token::INIT_CONST) {
3610 Load(node->value());
3611
3612 } else {
3613 GetValue(&target);
3614 Literal* literal = node->value()->AsLiteral();
3615 if (literal != NULL && literal->handle()->IsSmi()) {
3616 SmiOperation(node->binary_op(), literal->handle(), false);
mads.s.ager31e71382008-08-13 09:32:07 +00003617 __ push(r0);
3618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003619 } else {
3620 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00003621 GenericBinaryOperation(node->binary_op());
mads.s.ager31e71382008-08-13 09:32:07 +00003622 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003623 }
3624 }
3625
3626 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3627 if (var != NULL &&
3628 (var->mode() == Variable::CONST) &&
3629 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3630 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632 } else {
3633 __ RecordPosition(node->position());
3634 if (node->op() == Token::INIT_CONST) {
3635 // Dynamic constant initializations must use the function context
3636 // and initialize the actual constant declared. Dynamic variable
3637 // initializations are simply assignments and use SetValue.
3638 InitConst(&target);
3639 } else {
3640 SetValue(&target);
3641 }
3642 }
3643}
3644
3645
3646void ArmCodeGenerator::VisitThrow(Throw* node) {
3647 Comment cmnt(masm_, "[ Throw");
3648
3649 Load(node->exception());
3650 __ RecordPosition(node->position());
3651 __ CallRuntime(Runtime::kThrow, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003652 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653}
3654
3655
3656void ArmCodeGenerator::VisitProperty(Property* node) {
3657 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003659 if (is_referenced()) {
3660 __ RecordPosition(node->position());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003661 GetReferenceProperty(node->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663 Reference property(this, node);
3664 __ RecordPosition(node->position());
3665 GetValue(&property);
3666 }
3667}
3668
3669
3670void ArmCodeGenerator::VisitCall(Call* node) {
3671 Comment cmnt(masm_, "[ Call");
3672
3673 ZoneList<Expression*>* args = node->arguments();
3674
3675 if (FLAG_debug_info) RecordStatementPosition(node);
3676 // Standard function call.
3677
3678 // Check if the function is a variable or a property.
3679 Expression* function = node->expression();
3680 Variable* var = function->AsVariableProxy()->AsVariable();
3681 Property* property = function->AsProperty();
3682
3683 // ------------------------------------------------------------------------
3684 // Fast-case: Use inline caching.
3685 // ---
3686 // According to ECMA-262, section 11.2.3, page 44, the function to call
3687 // must be resolved after the arguments have been evaluated. The IC code
3688 // automatically handles this by loading the arguments before the function
3689 // is resolved in cache misses (this also holds for megamorphic calls).
3690 // ------------------------------------------------------------------------
3691
3692 if (var != NULL && !var->is_this() && var->is_global()) {
3693 // ----------------------------------
3694 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3695 // ----------------------------------
3696
3697 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003698 __ mov(r0, Operand(var->name()));
3699 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003700 LoadGlobal();
3701
3702 // Load the arguments.
3703 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704
3705 // Setup the receiver register and call the IC initialization code.
3706 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 __ RecordPosition(node->position());
3708 __ Call(stub, code_target_context);
3709 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003710 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003711 __ pop();
3712 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713
3714 } else if (var != NULL && var->slot() != NULL &&
3715 var->slot()->type() == Slot::LOOKUP) {
3716 // ----------------------------------
3717 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3718 // ----------------------------------
3719
3720 // Load the function
mads.s.ager31e71382008-08-13 09:32:07 +00003721 __ push(cp);
3722 __ mov(r0, Operand(var->name()));
3723 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 __ CallRuntime(Runtime::kLoadContextSlot, 2);
3725 // r0: slot value; r1: receiver
3726
3727 // Load the receiver.
mads.s.ager31e71382008-08-13 09:32:07 +00003728 __ push(r0); // function
3729 __ push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003730
3731 // Call the function.
3732 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00003733 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003734
3735 } else if (property != NULL) {
3736 // Check if the key is a literal string.
3737 Literal* literal = property->key()->AsLiteral();
3738
3739 if (literal != NULL && literal->handle()->IsSymbol()) {
3740 // ------------------------------------------------------------------
3741 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3742 // ------------------------------------------------------------------
3743
3744 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003745 __ mov(r0, Operand(literal->handle()));
3746 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747 Load(property->obj());
3748
3749 // Load the arguments.
3750 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003751
3752 // Set the receiver register and call the IC initialization code.
3753 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003754 __ RecordPosition(node->position());
3755 __ Call(stub, code_target);
3756 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3757
3758 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003759 __ pop();
3760
3761 __ push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762
3763 } else {
3764 // -------------------------------------------
3765 // JavaScript example: 'array[index](1, 2, 3)'
3766 // -------------------------------------------
3767
3768 // Load the function to call from the property through a reference.
3769 Reference ref(this, property);
mads.s.ager31e71382008-08-13 09:32:07 +00003770 GetValue(&ref); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003771
3772 // Pass receiver to called function.
mads.s.ager31e71382008-08-13 09:32:07 +00003773 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
3774 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775 // Call the function.
3776 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00003777 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 }
3779
3780 } else {
3781 // ----------------------------------
3782 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3783 // ----------------------------------
3784
3785 // Load the function.
3786 Load(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787 // Pass the global object as the receiver.
3788 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003789 // Call the function.
3790 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00003791 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792 }
3793}
3794
3795
3796void ArmCodeGenerator::VisitCallNew(CallNew* node) {
3797 Comment cmnt(masm_, "[ CallNew");
3798
3799 // According to ECMA-262, section 11.2.2, page 44, the function
3800 // expression in new calls must be evaluated before the
3801 // arguments. This is different from ordinary calls, where the
3802 // actual function to call is resolved after the arguments have been
3803 // evaluated.
3804
3805 // Compute function to call and use the global object as the
3806 // receiver.
3807 Load(node->expression());
3808 LoadGlobal();
3809
3810 // Push the arguments ("left-to-right") on the stack.
3811 ZoneList<Expression*>* args = node->arguments();
3812 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3813
mads.s.ager31e71382008-08-13 09:32:07 +00003814 // r0: the number of arguments.
3815 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003817 // Load the function into r1 as per calling convention.
3818 __ ldr(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
3819
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820 // Call the construct call builtin that handles allocation and
3821 // constructor invocation.
3822 __ RecordPosition(position);
3823 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
3824 js_construct_call);
mads.s.ager31e71382008-08-13 09:32:07 +00003825
3826 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
3827 __ str(r0, MemOperand(sp, 0 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003828}
3829
3830
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003831void ArmCodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
3832 ASSERT(args->length() == 1);
3833 Label leave;
3834 Load(args->at(0));
mads.s.ager31e71382008-08-13 09:32:07 +00003835 __ pop(r0); // r0 contains object.
3836 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003837 __ tst(r0, Operand(kSmiTagMask));
3838 __ b(eq, &leave);
3839 // It is a heap object - get map.
3840 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3841 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003842 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003843 __ cmp(r1, Operand(JS_VALUE_TYPE));
3844 __ b(ne, &leave);
3845 // Load the value.
3846 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
3847 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00003848 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849}
3850
3851
3852void ArmCodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
3853 ASSERT(args->length() == 2);
3854 Label leave;
3855 Load(args->at(0)); // Load the object.
3856 Load(args->at(1)); // Load the value.
mads.s.ager31e71382008-08-13 09:32:07 +00003857 __ pop(r0); // r0 contains value
3858 __ pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003859 // if (object->IsSmi()) return object.
3860 __ tst(r1, Operand(kSmiTagMask));
3861 __ b(eq, &leave);
3862 // It is a heap object - get map.
3863 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3864 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3865 // if (!object->IsJSValue()) return object.
3866 __ cmp(r2, Operand(JS_VALUE_TYPE));
3867 __ b(ne, &leave);
3868 // Store the value.
3869 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3870 // Update the write barrier.
3871 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3872 __ RecordWrite(r1, r2, r3);
3873 // Leave.
3874 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00003875 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876}
3877
3878
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879void ArmCodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
3880 ASSERT(args->length() == 1);
3881 Load(args->at(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 __ pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003883 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884 cc_reg_ = eq;
3885}
3886
3887
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003888void ArmCodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
3889 ASSERT(args->length() == 1);
3890 Load(args->at(0));
3891 __ pop(r0);
3892 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3893 cc_reg_ = eq;
3894}
3895
3896
kasper.lund7276f142008-07-30 08:49:36 +00003897// This should generate code that performs a charCodeAt() call or returns
3898// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3899// It is not yet implemented on ARM, so it always goes to the slow case.
3900void ArmCodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
3901 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003902 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +00003903 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003904}
3905
3906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907void ArmCodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003908 ASSERT(args->length() == 1);
3909 Load(args->at(0));
3910 Label answer;
3911 // We need the CC bits to come out as not_equal in the case where the
3912 // object is a smi. This can't be done with the usual test opcode so
3913 // we use XOR to get the right CC bits.
3914 __ pop(r0);
3915 __ and_(r1, r0, Operand(kSmiTagMask));
3916 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3917 __ b(ne, &answer);
3918 // It is a heap object - get the map.
3919 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3920 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3921 // Check if the object is a JS array or not.
3922 __ cmp(r1, Operand(JS_ARRAY_TYPE));
3923 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 cc_reg_ = eq;
3925}
3926
3927
3928void ArmCodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
3929 ASSERT(args->length() == 0);
3930
mads.s.ager31e71382008-08-13 09:32:07 +00003931 // Seed the result with the formal parameters count, which will be used
3932 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3934
3935 // Call the shared stub to get to the arguments.length.
3936 ArgumentsAccessStub stub(true);
3937 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00003938 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939}
3940
3941
3942void ArmCodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
3943 ASSERT(args->length() == 1);
3944
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003945 // Satisfy contract with ArgumentsAccessStub:
3946 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947 Load(args->at(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003948 __ pop(r1);
3949 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
3951 // Call the shared stub to get to arguments[key].
3952 ArgumentsAccessStub stub(false);
3953 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00003954 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955}
3956
3957
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003958void ArmCodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
3959 ASSERT(args->length() == 2);
3960
3961 // Load the two objects into registers and perform the comparison.
3962 Load(args->at(0));
3963 Load(args->at(1));
3964 __ pop(r0);
3965 __ pop(r1);
3966 __ cmp(r0, Operand(r1));
3967 cc_reg_ = eq;
3968}
3969
3970
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971void ArmCodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003972 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003973
3974 ZoneList<Expression*>* args = node->arguments();
3975 Comment cmnt(masm_, "[ CallRuntime");
3976 Runtime::Function* function = node->function();
3977
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003978 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003979 // Push the arguments ("left-to-right").
3980 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 // Call the C runtime function.
3983 __ CallRuntime(function, args->length());
mads.s.ager31e71382008-08-13 09:32:07 +00003984 __ push(r0);
3985
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00003987 // Prepare stack for calling JS runtime function.
3988 __ mov(r0, Operand(node->name()));
3989 __ push(r0);
3990 // Push the builtins object found in the current global object.
3991 __ ldr(r1, GlobalObject());
3992 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
3993 __ push(r0);
3994
3995 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003997 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003998 Handle<Code> stub = ComputeCallInitialize(args->length());
3999 __ Call(stub, code_target);
4000 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00004001 __ pop();
4002 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 }
4004}
4005
4006
4007void ArmCodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
4008 Comment cmnt(masm_, "[ UnaryOperation");
4009
4010 Token::Value op = node->op();
4011
4012 if (op == Token::NOT) {
4013 LoadCondition(node->expression(),
4014 CodeGenState::LOAD,
4015 false_target(),
4016 true_target(),
4017 true);
4018 cc_reg_ = NegateCondition(cc_reg_);
4019
4020 } else if (op == Token::DELETE) {
4021 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004022 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004023 if (property != NULL) {
4024 Load(property->obj());
4025 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00004026 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004027 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004028
mads.s.ager31e71382008-08-13 09:32:07 +00004029 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030 Slot* slot = variable->slot();
4031 if (variable->is_global()) {
4032 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004033 __ mov(r0, Operand(variable->name()));
4034 __ push(r0);
4035 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004036 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037
4038 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4039 // lookup the context holding the named variable
mads.s.ager31e71382008-08-13 09:32:07 +00004040 __ push(cp);
4041 __ mov(r0, Operand(variable->name()));
4042 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004043 __ CallRuntime(Runtime::kLookupContext, 2);
4044 // r0: context
mads.s.ager31e71382008-08-13 09:32:07 +00004045 __ push(r0);
4046 __ mov(r0, Operand(variable->name()));
4047 __ push(r0);
4048 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004049 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050
mads.s.ager31e71382008-08-13 09:32:07 +00004051 } else {
4052 // Default: Result of deleting non-global, not dynamically
4053 // introduced variables is false.
4054 __ mov(r0, Operand(Factory::false_value()));
4055 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056
4057 } else {
4058 // Default: Result of deleting expressions is true.
4059 Load(node->expression()); // may have side-effects
mads.s.ager31e71382008-08-13 09:32:07 +00004060 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061 __ mov(r0, Operand(Factory::true_value()));
4062 }
mads.s.ager31e71382008-08-13 09:32:07 +00004063 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004064
4065 } else if (op == Token::TYPEOF) {
4066 // Special case for loading the typeof expression; see comment on
4067 // LoadTypeofExpression().
4068 LoadTypeofExpression(node->expression());
4069 __ CallRuntime(Runtime::kTypeof, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004070 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071
4072 } else {
4073 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00004074 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075 switch (op) {
4076 case Token::NOT:
4077 case Token::DELETE:
4078 case Token::TYPEOF:
4079 UNREACHABLE(); // handled above
4080 break;
4081
4082 case Token::SUB: {
4083 UnarySubStub stub;
4084 __ CallStub(&stub);
4085 break;
4086 }
4087
4088 case Token::BIT_NOT: {
4089 // smi check
4090 Label smi_label;
4091 Label continue_label;
4092 __ tst(r0, Operand(kSmiTagMask));
4093 __ b(eq, &smi_label);
4094
mads.s.ager31e71382008-08-13 09:32:07 +00004095 __ push(r0);
4096 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004097 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098
4099 __ b(&continue_label);
4100 __ bind(&smi_label);
4101 __ mvn(r0, Operand(r0));
4102 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
4103 __ bind(&continue_label);
4104 break;
4105 }
4106
4107 case Token::VOID:
4108 // since the stack top is cached in r0, popping and then
4109 // pushing a value can be done by just writing to r0.
4110 __ mov(r0, Operand(Factory::undefined_value()));
4111 break;
4112
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004113 case Token::ADD: {
4114 // Smi check.
4115 Label continue_label;
4116 __ tst(r0, Operand(kSmiTagMask));
4117 __ b(eq, &continue_label);
mads.s.ager31e71382008-08-13 09:32:07 +00004118 __ push(r0);
4119 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004120 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004121 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004122 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004123 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004124 default:
4125 UNREACHABLE();
4126 }
mads.s.ager31e71382008-08-13 09:32:07 +00004127 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004128 }
4129}
4130
4131
4132void ArmCodeGenerator::VisitCountOperation(CountOperation* node) {
4133 Comment cmnt(masm_, "[ CountOperation");
4134
4135 bool is_postfix = node->is_postfix();
4136 bool is_increment = node->op() == Token::INC;
4137
4138 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4139 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4140
4141 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004142 if (is_postfix) {
4143 __ mov(r0, Operand(0));
4144 __ push(r0);
4145 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146
4147 { Reference target(this, node->expression());
4148 if (target.is_illegal()) return;
4149 GetValue(&target);
mads.s.ager31e71382008-08-13 09:32:07 +00004150 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004151
4152 Label slow, exit;
4153
4154 // Load the value (1) into register r1.
4155 __ mov(r1, Operand(Smi::FromInt(1)));
4156
4157 // Check for smi operand.
4158 __ tst(r0, Operand(kSmiTagMask));
4159 __ b(ne, &slow);
4160
4161 // Postfix: Store the old value as the result.
4162 if (is_postfix) __ str(r0, MemOperand(sp, target.size() * kPointerSize));
4163
4164 // Perform optimistic increment/decrement.
4165 if (is_increment) {
4166 __ add(r0, r0, Operand(r1), SetCC);
4167 } else {
4168 __ sub(r0, r0, Operand(r1), SetCC);
4169 }
4170
4171 // If the increment/decrement didn't overflow, we're done.
4172 __ b(vc, &exit);
4173
4174 // Revert optimistic increment/decrement.
4175 if (is_increment) {
4176 __ sub(r0, r0, Operand(r1));
4177 } else {
4178 __ add(r0, r0, Operand(r1));
4179 }
4180
4181 // Slow case: Convert to number.
4182 __ bind(&slow);
4183
4184 // Postfix: Convert the operand to a number and store it as the result.
4185 if (is_postfix) {
4186 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
4187 __ CallStub(&stub);
4188 // Store to result (on the stack).
4189 __ str(r0, MemOperand(sp, target.size() * kPointerSize));
4190 }
4191
4192 // Compute the new value by calling the right JavaScript native.
4193 if (is_increment) {
4194 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
4195 __ CallStub(&stub);
4196 } else {
4197 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
4198 __ CallStub(&stub);
4199 }
4200
4201 // Store the new value in the target if not const.
4202 __ bind(&exit);
mads.s.ager31e71382008-08-13 09:32:07 +00004203 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004204 if (!is_const) SetValue(&target);
4205 }
4206
4207 // Postfix: Discard the new value and use the old.
4208 if (is_postfix) __ pop(r0);
4209}
4210
4211
4212void ArmCodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4213 Comment cmnt(masm_, "[ BinaryOperation");
4214 Token::Value op = node->op();
4215
4216 // According to ECMA-262 section 11.11, page 58, the binary logical
4217 // operators must yield the result of one of the two expressions
4218 // before any ToBoolean() conversions. This means that the value
4219 // produced by a && or || operator is not necessarily a boolean.
4220
4221 // NOTE: If the left hand side produces a materialized value (not in
4222 // the CC register), we force the right hand side to do the
4223 // same. This is necessary because we may have to branch to the exit
4224 // after evaluating the left hand side (due to the shortcut
4225 // semantics), but the compiler must (statically) know if the result
4226 // of compiling the binary operation is materialized or not.
4227
4228 if (op == Token::AND) {
4229 Label is_true;
4230 LoadCondition(node->left(),
4231 CodeGenState::LOAD,
4232 &is_true,
4233 false_target(),
4234 false);
4235 if (has_cc()) {
4236 Branch(false, false_target());
4237
4238 // Evaluate right side expression.
4239 __ bind(&is_true);
4240 LoadCondition(node->right(),
4241 CodeGenState::LOAD,
4242 true_target(),
4243 false_target(),
4244 false);
4245
4246 } else {
4247 Label pop_and_continue, exit;
4248
mads.s.ager31e71382008-08-13 09:32:07 +00004249 __ ldr(r0, MemOperand(sp, 0)); // dup the stack top
4250 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251 // Avoid popping the result if it converts to 'false' using the
4252 // standard ToBoolean() conversion as described in ECMA-262,
4253 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004254 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004255 Branch(false, &exit);
4256
4257 // Pop the result of evaluating the first part.
4258 __ bind(&pop_and_continue);
4259 __ pop(r0);
4260
4261 // Evaluate right side expression.
4262 __ bind(&is_true);
4263 Load(node->right());
4264
4265 // Exit (always with a materialized value).
4266 __ bind(&exit);
4267 }
4268
4269 } else if (op == Token::OR) {
4270 Label is_false;
4271 LoadCondition(node->left(),
4272 CodeGenState::LOAD,
4273 true_target(),
4274 &is_false,
4275 false);
4276 if (has_cc()) {
4277 Branch(true, true_target());
4278
4279 // Evaluate right side expression.
4280 __ bind(&is_false);
4281 LoadCondition(node->right(),
4282 CodeGenState::LOAD,
4283 true_target(),
4284 false_target(),
4285 false);
4286
4287 } else {
4288 Label pop_and_continue, exit;
4289
mads.s.ager31e71382008-08-13 09:32:07 +00004290 __ ldr(r0, MemOperand(sp, 0));
4291 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004292 // Avoid popping the result if it converts to 'true' using the
4293 // standard ToBoolean() conversion as described in ECMA-262,
4294 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004295 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004296 Branch(true, &exit);
4297
4298 // Pop the result of evaluating the first part.
4299 __ bind(&pop_and_continue);
4300 __ pop(r0);
4301
4302 // Evaluate right side expression.
4303 __ bind(&is_false);
4304 Load(node->right());
4305
4306 // Exit (always with a materialized value).
4307 __ bind(&exit);
4308 }
4309
4310 } else {
4311 // Optimize for the case where (at least) one of the expressions
4312 // is a literal small integer.
4313 Literal* lliteral = node->left()->AsLiteral();
4314 Literal* rliteral = node->right()->AsLiteral();
4315
4316 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
4317 Load(node->left());
4318 SmiOperation(node->op(), rliteral->handle(), false);
4319
4320 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
4321 Load(node->right());
4322 SmiOperation(node->op(), lliteral->handle(), true);
4323
4324 } else {
4325 Load(node->left());
4326 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00004327 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004328 }
mads.s.ager31e71382008-08-13 09:32:07 +00004329 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004330 }
4331}
4332
4333
4334void ArmCodeGenerator::VisitThisFunction(ThisFunction* node) {
mads.s.ager31e71382008-08-13 09:32:07 +00004335 __ ldr(r0, FunctionOperand());
4336 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004337}
4338
4339
4340void ArmCodeGenerator::VisitCompareOperation(CompareOperation* node) {
4341 Comment cmnt(masm_, "[ CompareOperation");
4342
4343 // Get the expressions from the node.
4344 Expression* left = node->left();
4345 Expression* right = node->right();
4346 Token::Value op = node->op();
4347
4348 // NOTE: To make null checks efficient, we check if either left or
4349 // right is the literal 'null'. If so, we optimize the code by
4350 // inlining a null check instead of calling the (very) general
4351 // runtime routine for checking equality.
4352
4353 bool left_is_null =
4354 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
4355 bool right_is_null =
4356 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4357
4358 if (op == Token::EQ || op == Token::EQ_STRICT) {
4359 // The 'null' value is only equal to 'null' or 'undefined'.
4360 if (left_is_null || right_is_null) {
4361 Load(left_is_null ? right : left);
4362 Label exit, undetectable;
mads.s.ager31e71382008-08-13 09:32:07 +00004363 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004364 __ cmp(r0, Operand(Factory::null_value()));
4365
4366 // The 'null' value is only equal to 'undefined' if using
4367 // non-strict comparisons.
4368 if (op != Token::EQ_STRICT) {
4369 __ b(eq, &exit);
4370 __ cmp(r0, Operand(Factory::undefined_value()));
4371
4372 // NOTE: it can be undetectable object.
4373 __ b(eq, &exit);
4374 __ tst(r0, Operand(kSmiTagMask));
4375
4376 __ b(ne, &undetectable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004377 __ b(false_target());
4378
4379 __ bind(&undetectable);
4380 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4381 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4382 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4383 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4384 }
4385
4386 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004387
4388 cc_reg_ = eq;
4389 return;
4390 }
4391 }
4392
4393
4394 // NOTE: To make typeof testing for natives implemented in
4395 // JavaScript really efficient, we generate special code for
4396 // expressions of the form: 'typeof <expression> == <string>'.
4397
4398 UnaryOperation* operation = left->AsUnaryOperation();
4399 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4400 (operation != NULL && operation->op() == Token::TYPEOF) &&
4401 (right->AsLiteral() != NULL &&
4402 right->AsLiteral()->handle()->IsString())) {
4403 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4404
mads.s.ager31e71382008-08-13 09:32:07 +00004405 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004406 LoadTypeofExpression(operation->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00004407 __ pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004408
4409 if (check->Equals(Heap::number_symbol())) {
4410 __ tst(r1, Operand(kSmiTagMask));
4411 __ b(eq, true_target());
4412 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4413 __ cmp(r1, Operand(Factory::heap_number_map()));
4414 cc_reg_ = eq;
4415
4416 } else if (check->Equals(Heap::string_symbol())) {
4417 __ tst(r1, Operand(kSmiTagMask));
4418 __ b(eq, false_target());
4419
4420 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4421
4422 // NOTE: it might be an undetectable string object
4423 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4424 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4425 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4426 __ b(eq, false_target());
4427
4428 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4429 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4430 cc_reg_ = lt;
4431
4432 } else if (check->Equals(Heap::boolean_symbol())) {
4433 __ cmp(r1, Operand(Factory::true_value()));
4434 __ b(eq, true_target());
4435 __ cmp(r1, Operand(Factory::false_value()));
4436 cc_reg_ = eq;
4437
4438 } else if (check->Equals(Heap::undefined_symbol())) {
4439 __ cmp(r1, Operand(Factory::undefined_value()));
4440 __ b(eq, true_target());
4441
4442 __ tst(r1, Operand(kSmiTagMask));
4443 __ b(eq, false_target());
4444
4445 // NOTE: it can be undetectable object.
4446 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4447 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4448 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4449 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4450
4451 cc_reg_ = eq;
4452
4453 } else if (check->Equals(Heap::function_symbol())) {
4454 __ tst(r1, Operand(kSmiTagMask));
4455 __ b(eq, false_target());
4456 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4457 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4458 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4459 cc_reg_ = eq;
4460
4461 } else if (check->Equals(Heap::object_symbol())) {
4462 __ tst(r1, Operand(kSmiTagMask));
4463 __ b(eq, false_target());
4464
4465 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4466 __ cmp(r1, Operand(Factory::null_value()));
4467 __ b(eq, true_target());
4468
4469 // NOTE: it might be an undetectable object.
4470 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4471 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4472 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
4473 __ b(eq, false_target());
4474
4475 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4476 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
4477 __ b(lt, false_target());
4478 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4479 cc_reg_ = le;
4480
4481 } else {
4482 // Uncommon case: Typeof testing against a string literal that
4483 // is never returned from the typeof operator.
4484 __ b(false_target());
4485 }
4486 return;
4487 }
4488
4489 Load(left);
4490 Load(right);
4491 switch (op) {
4492 case Token::EQ:
4493 Comparison(eq, false);
4494 break;
4495
4496 case Token::LT:
4497 Comparison(lt);
4498 break;
4499
4500 case Token::GT:
4501 Comparison(gt);
4502 break;
4503
4504 case Token::LTE:
4505 Comparison(le);
4506 break;
4507
4508 case Token::GTE:
4509 Comparison(ge);
4510 break;
4511
4512 case Token::EQ_STRICT:
4513 Comparison(eq, true);
4514 break;
4515
4516 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00004517 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004518 __ InvokeBuiltin(Builtins::IN, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00004519 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004520 break;
4521
4522 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00004523 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004524 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00004525 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004526 break;
4527
4528 default:
4529 UNREACHABLE();
4530 }
4531}
4532
4533
4534void ArmCodeGenerator::RecordStatementPosition(Node* node) {
4535 if (FLAG_debug_info) {
4536 int statement_pos = node->statement_pos();
4537 if (statement_pos == kNoPosition) return;
4538 __ RecordStatementPosition(statement_pos);
4539 }
4540}
4541
4542
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004543void ArmCodeGenerator::EnterJSFrame() {
4544#if defined(DEBUG)
4545 { Label done, fail;
4546 __ tst(r1, Operand(kSmiTagMask));
4547 __ b(eq, &fail);
4548 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4549 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4550 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4551 __ b(eq, &done);
4552 __ bind(&fail);
4553 __ stop("ArmCodeGenerator::EnterJSFrame - r1 not a function");
4554 __ bind(&done);
4555 }
4556#endif // DEBUG
4557
4558 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
4559 __ add(fp, sp, Operand(2 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004560}
4561
4562
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004563void ArmCodeGenerator::ExitJSFrame() {
4564 // Drop the execution stack down to the frame pointer and restore the caller
4565 // frame pointer and return address.
4566 __ mov(sp, fp);
4567 __ ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004568}
4569
4570
4571#undef __
4572
4573
4574// -----------------------------------------------------------------------------
4575// CodeGenerator interface
4576
4577// MakeCode() is just a wrapper for CodeGenerator::MakeCode()
4578// so we don't have to expose the entire CodeGenerator class in
4579// the .h file.
4580Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun,
4581 Handle<Script> script,
4582 bool is_eval) {
4583 Handle<Code> code = ArmCodeGenerator::MakeCode(fun, script, is_eval);
4584 if (!code.is_null()) {
4585 Counters::total_compiled_code_size.Increment(code->instruction_size());
4586 }
4587 return code;
4588}
4589
4590
4591} } // namespace v8::internal