blob: 89fc0db7c1f0bf48f60a82ddf1e810ef9e9df55e [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_; }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000061 void set_type(Type value) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062 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
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000141 MacroAssembler* masm() { return masm_; }
142
143 Scope* scope() const { return scope_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000145 CodeGenState* state() { return state_; }
146 void set_state(CodeGenState* state) { state_ = state; }
147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 private:
149 // Assembler
150 MacroAssembler* masm_; // to generate code
151
152 // Code generation state
153 Scope* scope_;
154 Condition cc_reg_;
155 CodeGenState* state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 int break_stack_height_;
157
158 // Labels
159 Label function_return_;
160
161 // Construction/destruction
162 ArmCodeGenerator(int buffer_size,
163 Handle<Script> script,
164 bool is_eval);
165
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000166 virtual ~ArmCodeGenerator() { delete masm_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167
168 // Main code generation function
169 void GenCode(FunctionLiteral* fun);
170
171 // The following are used by class Reference.
172 void LoadReference(Reference* ref);
173 void UnloadReference(Reference* ref);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174
175 // State
176 bool has_cc() const { return cc_reg_ != al; }
177 CodeGenState::AccessType access() const { return state_->access(); }
178 Reference* ref() const { return state_->ref(); }
179 bool is_referenced() const { return state_->ref() != NULL; }
180 Label* true_target() const { return state_->true_target(); }
181 Label* false_target() const { return state_->false_target(); }
182
183
184 // Expressions
185 MemOperand GlobalObject() const {
186 return ContextOperand(cp, Context::GLOBAL_INDEX);
187 }
188
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000189 static MemOperand ContextOperand(Register context, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 return MemOperand(context, Context::SlotOffset(index));
191 }
192
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000193 static MemOperand ParameterOperand(const CodeGenerator* cgen, int index) {
194 int num_parameters = cgen->scope()->num_parameters();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 // index -2 corresponds to the activated closure, -1 corresponds
196 // to the receiver
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000197 ASSERT(-2 <= index && index < num_parameters);
198 int offset = (1 + num_parameters - index) * kPointerSize;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000199 return MemOperand(fp, offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 }
201
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000202 MemOperand ParameterOperand(int index) const {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000203 return ParameterOperand(this, index);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000204 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000206 MemOperand FunctionOperand() const {
207 return MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset);
208 }
209
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000210 static MemOperand SlotOperand(CodeGenerator* cgen,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000211 Slot* slot,
212 Register tmp);
213
214 MemOperand SlotOperand(Slot* slot, Register tmp) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000215 return SlotOperand(this, slot, tmp);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000216 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
218 void LoadCondition(Expression* x, CodeGenState::AccessType access,
219 Label* true_target, Label* false_target, bool force_cc);
220 void Load(Expression* x,
221 CodeGenState::AccessType access = CodeGenState::LOAD);
222 void LoadGlobal();
223
224 // Special code for typeof expressions: Unfortunately, we must
225 // be careful when loading the expression in 'typeof'
226 // expressions. We are not allowed to throw reference errors for
227 // non-existing properties of the global object, so we must make it
228 // look like an explicit property access, instead of an access
229 // through the context chain.
230 void LoadTypeofExpression(Expression* x);
231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000233 // References
234
235 // Generate code to fetch the value of a reference. The reference is
236 // expected to be on top of the expression stack. It is left in place and
237 // its value is pushed on top of it.
238 void GetValue(Reference* ref) {
239 ASSERT(!has_cc());
240 ASSERT(!ref->is_illegal());
241 CodeGenState new_state(this, ref);
242 Visit(ref->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 }
244
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000245 // Generate code to store a value in a reference. The stored value is
246 // expected on top of the expression stack, with the reference immediately
247 // below it. The expression stack is left unchanged.
248 void SetValue(Reference* ref) {
249 ASSERT(!has_cc());
250 ASSERT(!ref->is_illegal());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000251 ref->expression()->GenerateStoreCode(this, ref, NOT_CONST_INIT);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000252 }
253
254 // Generate code to store a value in a reference. The stored value is
255 // expected on top of the expression stack, with the reference immediately
256 // below it. The expression stack is left unchanged.
257 void InitConst(Reference* ref) {
258 ASSERT(!has_cc());
259 ASSERT(!ref->is_illegal());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000260 ref->expression()->GenerateStoreCode(this, ref, CONST_INIT);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000261 }
262
263 // Generate code to fetch a value from a property of a reference. The
264 // reference is expected on top of the expression stack. It is left in
265 // place and its value is pushed on top of it.
266 void GetReferenceProperty(Expression* key);
267
268 // Generate code to store a value in a property of a reference. The
269 // stored value is expected on top of the expression stack, with the
270 // reference immediately below it. The expression stack is left
271 // unchanged.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000272 static void SetReferenceProperty(CodeGenerator* cgen,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000273 Reference* ref,
274 Expression* key);
275
276
mads.s.ager31e71382008-08-13 09:32:07 +0000277 void ToBoolean(Label* true_target, Label* false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278
kasper.lund7276f142008-07-30 08:49:36 +0000279 void GenericBinaryOperation(Token::Value op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 void Comparison(Condition cc, bool strict = false);
281
282 void SmiOperation(Token::Value op, Handle<Object> value, bool reversed);
283
284 void CallWithArguments(ZoneList<Expression*>* arguments, int position);
285
286 // Declare global variables and functions in the given array of
287 // name/value pairs.
288 virtual void DeclareGlobals(Handle<FixedArray> pairs);
289
290 // Instantiate the function boilerplate.
291 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
292
293 // Control flow
294 void Branch(bool if_true, Label* L);
295 void CheckStack();
296 void CleanStack(int num_bytes);
297
298 // Node visitors
299#define DEF_VISIT(type) \
300 virtual void Visit##type(type* node);
301 NODE_LIST(DEF_VISIT)
302#undef DEF_VISIT
303
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000304 // Fast-case switch
305 static const int kFastCaseSwitchMaxOverheadFactor = 10;
306 static const int kFastCaseSwitchMinCaseCount = 5;
307 virtual int FastCaseSwitchMaxOverheadFactor();
308 virtual int FastCaseSwitchMinCaseCount();
309 virtual void GenerateFastCaseSwitchJumpTable(
310 SwitchStatement* node, int min_index, int range, Label *fail_label,
311 SmartPointer<Label*> &case_targets, SmartPointer<Label> &case_labels);
312
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 void RecordStatementPosition(Node* node);
314
315 // Activation frames
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000316 void EnterJSFrame();
317 void ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 virtual void GenerateIsSmi(ZoneList<Expression*>* args);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000320 virtual void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 virtual void GenerateIsArray(ZoneList<Expression*>* args);
322
323 virtual void GenerateArgumentsLength(ZoneList<Expression*>* args);
324 virtual void GenerateArgumentsAccess(ZoneList<Expression*>* args);
325
326 virtual void GenerateValueOf(ZoneList<Expression*>* args);
327 virtual void GenerateSetValueOf(ZoneList<Expression*>* args);
kasper.lund7276f142008-07-30 08:49:36 +0000328
329 virtual void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000330
331 virtual void GenerateObjectEquals(ZoneList<Expression*>* args);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000332
333 friend class Reference;
334 friend class Property;
335 friend class VariableProxy;
336 friend class Slot;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337};
338
339
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000340// -------------------------------------------------------------------------
341// CodeGenState implementation.
342
343CodeGenState::CodeGenState(ArmCodeGenerator* owner)
344 : owner_(owner),
345 access_(UNDEFINED),
346 ref_(NULL),
347 true_target_(NULL),
348 false_target_(NULL),
349 previous_(NULL) {
350 owner_->set_state(this);
351}
352
353
354CodeGenState::CodeGenState(ArmCodeGenerator* owner,
355 AccessType access,
356 Label* true_target,
357 Label* false_target)
358 : owner_(owner),
359 access_(access),
360 ref_(NULL),
361 true_target_(true_target),
362 false_target_(false_target),
363 previous_(owner->state()) {
364 owner_->set_state(this);
365}
366
367
368CodeGenState::CodeGenState(ArmCodeGenerator* owner, Reference* ref)
369 : owner_(owner),
370 access_(LOAD),
371 ref_(ref),
372 true_target_(owner->state()->true_target_),
373 false_target_(owner->state()->false_target_),
374 previous_(owner->state()) {
375 owner_->set_state(this);
376}
377
378
379CodeGenState::~CodeGenState() {
380 ASSERT(owner_->state() == this);
381 owner_->set_state(previous_);
382}
383
384
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385// -----------------------------------------------------------------------------
386// ArmCodeGenerator implementation
387
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000388#define __ masm_->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389
390Handle<Code> ArmCodeGenerator::MakeCode(FunctionLiteral* flit,
391 Handle<Script> script,
392 bool is_eval) {
mads.s.ager31e71382008-08-13 09:32:07 +0000393#ifdef ENABLE_DISASSEMBLER
394 bool print_code = FLAG_print_code && !Bootstrapper::IsActive();
395#endif // ENABLE_DISASSEMBLER
396
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397#ifdef DEBUG
398 bool print_source = false;
399 bool print_ast = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 const char* ftype;
401
402 if (Bootstrapper::IsActive()) {
403 print_source = FLAG_print_builtin_source;
404 print_ast = FLAG_print_builtin_ast;
405 print_code = FLAG_print_builtin_code;
406 ftype = "builtin";
407 } else {
408 print_source = FLAG_print_source;
409 print_ast = FLAG_print_ast;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410 ftype = "user-defined";
411 }
412
413 if (FLAG_trace_codegen || print_source || print_ast) {
414 PrintF("*** Generate code for %s function: ", ftype);
415 flit->name()->ShortPrint();
416 PrintF(" ***\n");
417 }
418
419 if (print_source) {
420 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit));
421 }
422
423 if (print_ast) {
424 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit));
425 }
426#endif // DEBUG
427
428 // Generate code.
429 const int initial_buffer_size = 4 * KB;
430 ArmCodeGenerator cgen(initial_buffer_size, script, is_eval);
431 cgen.GenCode(flit);
432 if (cgen.HasStackOverflow()) {
kasper.lund212ac232008-07-16 07:07:30 +0000433 ASSERT(!Top::has_pending_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 return Handle<Code>::null();
435 }
436
437 // Process any deferred code.
438 cgen.ProcessDeferred();
439
440 // Allocate and install the code.
441 CodeDesc desc;
442 cgen.masm()->GetCode(&desc);
443 ScopeInfo<> sinfo(flit->scope());
444 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
445 Handle<Code> code = Factory::NewCode(desc, &sinfo, flags);
446
447 // Add unresolved entries in the code to the fixup list.
448 Bootstrapper::AddFixup(*code, cgen.masm());
449
mads.s.ager31e71382008-08-13 09:32:07 +0000450#ifdef ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 if (print_code) {
452 // Print the source code if available.
453 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
454 PrintF("--- Raw source ---\n");
455 StringInputBuffer stream(String::cast(script->source()));
456 stream.Seek(flit->start_position());
457 // flit->end_position() points to the last character in the stream. We
458 // need to compensate by adding one to calculate the length.
459 int source_len = flit->end_position() - flit->start_position() + 1;
460 for (int i = 0; i < source_len; i++) {
461 if (stream.has_more()) PrintF("%c", stream.GetNext());
462 }
463 PrintF("\n\n");
464 }
465 PrintF("--- Code ---\n");
mads.s.ager31e71382008-08-13 09:32:07 +0000466 code->Disassemble();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 }
mads.s.ager31e71382008-08-13 09:32:07 +0000468#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469
470 return code;
471}
472
473
474ArmCodeGenerator::ArmCodeGenerator(int buffer_size,
475 Handle<Script> script,
476 bool is_eval)
477 : CodeGenerator(is_eval, script),
478 masm_(new MacroAssembler(NULL, buffer_size)),
479 scope_(NULL),
480 cc_reg_(al),
481 state_(NULL),
482 break_stack_height_(0) {
483}
484
485
486// Calling conventions:
487
mads.s.ager31e71382008-08-13 09:32:07 +0000488// r0: the number of arguments
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489// fp: frame pointer
490// sp: stack pointer
491// pp: caller's parameter pointer
492// cp: callee's context
493
494void ArmCodeGenerator::GenCode(FunctionLiteral* fun) {
495 Scope* scope = fun->scope();
496 ZoneList<Statement*>* body = fun->body();
497
498 // Initialize state.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000499 { CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 scope_ = scope;
501 cc_reg_ = al;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502
503 // Entry
504 // stack: function, receiver, arguments, return address
505 // r0: number of arguments
506 // sp: stack pointer
507 // fp: frame pointer
508 // pp: caller's parameter pointer
509 // cp: callee's context
510
511 { Comment cmnt(masm_, "[ enter JS frame");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000512 EnterJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 }
514 // tos: code slot
515#ifdef DEBUG
516 if (strlen(FLAG_stop_at) > 0 &&
517 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000518 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 }
520#endif
521
522 // Allocate space for locals and initialize them.
kasper.lund7276f142008-07-30 08:49:36 +0000523 if (scope->num_stack_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 Comment cmnt(masm_, "[ allocate space for locals");
mads.s.ager31e71382008-08-13 09:32:07 +0000525 // Initialize stack slots with 'undefined' value.
526 __ mov(ip, Operand(Factory::undefined_value()));
527 for (int i = 0; i < scope->num_stack_slots(); i++) {
528 __ push(ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 }
530 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531
532 if (scope->num_heap_slots() > 0) {
533 // Allocate local context.
534 // Get outer context and create a new context based on it.
mads.s.ager31e71382008-08-13 09:32:07 +0000535 __ ldr(r0, FunctionOperand());
536 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +0000537 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
538
539 if (kDebug) {
540 Label verified_true;
541 __ cmp(r0, Operand(cp));
542 __ b(eq, &verified_true);
543 __ stop("NewContext: r0 is expected to be the same as cp");
544 __ bind(&verified_true);
545 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 // Update context local.
547 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
548 }
549
550 // TODO(1241774): Improve this code!!!
551 // 1) only needed if we have a context
552 // 2) no need to recompute context ptr every single time
553 // 3) don't copy parameter operand code from SlotOperand!
554 {
555 Comment cmnt2(masm_, "[ copy context parameters into .context");
556
557 // Note that iteration order is relevant here! If we have the same
558 // parameter twice (e.g., function (x, y, x)), and that parameter
559 // needs to be copied into the context, it must be the last argument
560 // passed to the parameter that needs to be copied. This is a rare
561 // case so we don't check for it, instead we rely on the copying
562 // order: such a parameter is copied repeatedly into the same
563 // context location and thus the last value is what is seen inside
564 // the function.
565 for (int i = 0; i < scope->num_parameters(); i++) {
566 Variable* par = scope->parameter(i);
567 Slot* slot = par->slot();
568 if (slot != NULL && slot->type() == Slot::CONTEXT) {
569 ASSERT(!scope->is_global_scope()); // no parameters in global scope
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000570 __ ldr(r1, ParameterOperand(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 // Loads r2 with context; used below in RecordWrite.
572 __ str(r1, SlotOperand(slot, r2));
573 // Load the offset into r3.
574 int slot_offset =
575 FixedArray::kHeaderSize + slot->index() * kPointerSize;
576 __ mov(r3, Operand(slot_offset));
577 __ RecordWrite(r2, r3, r1);
578 }
579 }
580 }
581
582 // Store the arguments object.
583 // This must happen after context initialization because
584 // the arguments array may be stored in the context!
585 if (scope->arguments() != NULL) {
586 ASSERT(scope->arguments_shadow() != NULL);
587 Comment cmnt(masm_, "[ allocate arguments object");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000588 { Reference shadow_ref(this, scope->arguments_shadow());
589 { Reference arguments_ref(this, scope->arguments());
590 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
591 __ ldr(r2, FunctionOperand());
592 // The receiver is below the arguments, the return address,
593 // and the frame pointer on the stack.
594 const int kReceiverDisplacement = 2 + scope->num_parameters();
595 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
596 __ mov(r0, Operand(Smi::FromInt(scope->num_parameters())));
597 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
598 __ CallStub(&stub);
599 __ push(r0);
600 SetValue(&arguments_ref);
601 }
602 SetValue(&shadow_ref);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000604 __ pop(r0); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 }
606
607 // Generate code to 'execute' declarations and initialize
608 // functions (source elements). In case of an illegal
609 // redeclaration we need to handle that instead of processing the
610 // declarations.
611 if (scope->HasIllegalRedeclaration()) {
612 Comment cmnt(masm_, "[ illegal redeclarations");
613 scope->VisitIllegalRedeclaration(this);
614 } else {
615 Comment cmnt(masm_, "[ declarations");
mads.s.ager31e71382008-08-13 09:32:07 +0000616 // ProcessDeclarations calls DeclareGlobals indirectly
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 ProcessDeclarations(scope->declarations());
mads.s.ager31e71382008-08-13 09:32:07 +0000618
v8.team.kasperl727e9952008-09-02 14:56:44 +0000619 // Bail out if a stack-overflow exception occurred when
kasper.lund212ac232008-07-16 07:07:30 +0000620 // processing declarations.
621 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 }
623
mads.s.ager31e71382008-08-13 09:32:07 +0000624 if (FLAG_trace) {
625 // Push a valid value as the parameter. The runtime call only uses
626 // it as the return value to indicate non-failure.
627 __ mov(r0, Operand(Smi::FromInt(0)));
628 __ push(r0);
629 __ CallRuntime(Runtime::kTraceEnter, 1);
630 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 CheckStack();
632
633 // Compile the body of the function in a vanilla state. Don't
634 // bother compiling all the code if the scope has an illegal
635 // redeclaration.
636 if (!scope->HasIllegalRedeclaration()) {
637 Comment cmnt(masm_, "[ function body");
638#ifdef DEBUG
639 bool is_builtin = Bootstrapper::IsActive();
640 bool should_trace =
641 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000642 if (should_trace) {
643 // Push a valid value as the parameter. The runtime call only uses
644 // it as the return value to indicate non-failure.
645 __ mov(r0, Operand(Smi::FromInt(0)));
646 __ push(r0);
647 __ CallRuntime(Runtime::kDebugTrace, 1);
648 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649#endif
650 VisitStatements(body);
651 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 }
653
654 // exit
655 // r0: result
656 // sp: stack pointer
657 // fp: frame pointer
658 // pp: parameter pointer
659 // cp: callee's context
mads.s.ager31e71382008-08-13 09:32:07 +0000660 __ mov(r0, Operand(Factory::undefined_value()));
661
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 __ bind(&function_return_);
mads.s.ager31e71382008-08-13 09:32:07 +0000663 if (FLAG_trace) {
664 // Push the return value on the stack as the parameter.
665 // Runtime::TraceExit returns the parameter as it is.
666 __ push(r0);
667 __ CallRuntime(Runtime::kTraceExit, 1);
668 }
669
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000670 // Tear down the frame which will restore the caller's frame pointer and the
671 // link register.
kasper.lund7276f142008-07-30 08:49:36 +0000672 ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000674 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
675 __ mov(pc, lr);
676
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 // Code generation state must be reset.
678 scope_ = NULL;
679 ASSERT(!has_cc());
680 ASSERT(state_ == NULL);
681}
682
683
mads.s.ager31e71382008-08-13 09:32:07 +0000684// Loads a value on the stack. If it is a boolean value, the result may have
685// been (partially) translated into branches, or it may have set the condition
686// code register. If force_cc is set, the value is forced to set the condition
687// code register and no value is pushed. If the condition code register was set,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688// has_cc() is true and cc_reg_ contains the condition to test for 'true'.
689void ArmCodeGenerator::LoadCondition(Expression* x,
690 CodeGenState::AccessType access,
691 Label* true_target,
692 Label* false_target,
693 bool force_cc) {
694 ASSERT(access == CodeGenState::LOAD ||
695 access == CodeGenState::LOAD_TYPEOF_EXPR);
696 ASSERT(!has_cc() && !is_referenced());
697
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000698 { CodeGenState new_state(this, access, true_target, false_target);
699 Visit(x);
700 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000702 // Convert the TOS value to a boolean in the condition code register.
703 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 }
705 ASSERT(has_cc() || !force_cc);
706}
707
708
709void ArmCodeGenerator::Load(Expression* x, CodeGenState::AccessType access) {
710 ASSERT(access == CodeGenState::LOAD ||
711 access == CodeGenState::LOAD_TYPEOF_EXPR);
712
713 Label true_target;
714 Label false_target;
715 LoadCondition(x, access, &true_target, &false_target, false);
716
717 if (has_cc()) {
718 // convert cc_reg_ into a bool
719 Label loaded, materialize_true;
720 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000721 __ mov(r0, Operand(Factory::false_value()));
722 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 __ b(&loaded);
724 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000725 __ mov(r0, Operand(Factory::true_value()));
726 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 __ bind(&loaded);
728 cc_reg_ = al;
729 }
730
731 if (true_target.is_linked() || false_target.is_linked()) {
732 // we have at least one condition value
733 // that has been "translated" into a branch,
734 // thus it needs to be loaded explicitly again
735 Label loaded;
736 __ b(&loaded); // don't lose current TOS
737 bool both = true_target.is_linked() && false_target.is_linked();
738 // reincarnate "true", if necessary
739 if (true_target.is_linked()) {
740 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000741 __ mov(r0, Operand(Factory::true_value()));
742 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 }
744 // if both "true" and "false" need to be reincarnated,
745 // jump across code for "false"
746 if (both)
747 __ b(&loaded);
748 // reincarnate "false", if necessary
749 if (false_target.is_linked()) {
750 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000751 __ mov(r0, Operand(Factory::false_value()));
752 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 }
754 // everything is loaded at this point
755 __ bind(&loaded);
756 }
757 ASSERT(!has_cc());
758}
759
760
761void ArmCodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000762 __ ldr(r0, GlobalObject());
763 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764}
765
766
767// TODO(1241834): Get rid of this function in favor of just using Load, now
768// that we have the LOAD_TYPEOF_EXPR access type. => Need to handle
769// global variables w/o reference errors elsewhere.
770void ArmCodeGenerator::LoadTypeofExpression(Expression* x) {
771 Variable* variable = x->AsVariableProxy()->AsVariable();
772 if (variable != NULL && !variable->is_this() && variable->is_global()) {
773 // NOTE: This is somewhat nasty. We force the compiler to load
774 // the variable as if through '<global>.<variable>' to make sure we
775 // do not get reference errors.
776 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
777 Literal key(variable->name());
778 // TODO(1241834): Fetch the position from the variable instead of using
779 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000780 Property property(&global, &key, RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 Load(&property);
782 } else {
783 Load(x, CodeGenState::LOAD_TYPEOF_EXPR);
784 }
785}
786
787
788Reference::Reference(ArmCodeGenerator* cgen, Expression* expression)
789 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
790 cgen->LoadReference(this);
791}
792
793
794Reference::~Reference() {
795 cgen_->UnloadReference(this);
796}
797
798
799void ArmCodeGenerator::LoadReference(Reference* ref) {
800 Expression* e = ref->expression();
801 Property* property = e->AsProperty();
802 Variable* var = e->AsVariableProxy()->AsVariable();
803
804 if (property != NULL) {
805 Load(property->obj());
806 // Used a named reference if the key is a literal symbol.
807 // We don't use a named reference if they key is a string that can be
808 // legally parsed as an integer. This is because, otherwise we don't
809 // get into the slow case code that handles [] on String objects.
810 Literal* literal = property->key()->AsLiteral();
811 uint32_t dummy;
812 if (literal != NULL && literal->handle()->IsSymbol() &&
813 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
814 ref->set_type(Reference::NAMED);
815 } else {
816 Load(property->key());
817 ref->set_type(Reference::KEYED);
818 }
819 } else if (var != NULL) {
820 if (var->is_global()) {
821 // global variable
822 LoadGlobal();
823 ref->set_type(Reference::NAMED);
824 } else {
825 // local variable
826 ref->set_type(Reference::EMPTY);
827 }
828 } else {
829 Load(e);
830 __ CallRuntime(Runtime::kThrowReferenceError, 1);
mads.s.ager31e71382008-08-13 09:32:07 +0000831 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 }
833}
834
835
836void ArmCodeGenerator::UnloadReference(Reference* ref) {
837 int size = ref->size();
838 if (size <= 0) {
839 // Do nothing. No popping is necessary.
840 } else {
mads.s.ager31e71382008-08-13 09:32:07 +0000841 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 __ add(sp, sp, Operand(size * kPointerSize));
mads.s.ager31e71382008-08-13 09:32:07 +0000843 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844 }
845}
846
847
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
849// register to a boolean in the condition code register. The code
850// may jump to 'false_target' in case the register converts to 'false'.
mads.s.ager31e71382008-08-13 09:32:07 +0000851void ArmCodeGenerator::ToBoolean(Label* true_target,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000853 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 // Only the condition code should be set.
mads.s.ager31e71382008-08-13 09:32:07 +0000855 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 // Fast case checks
858
mads.s.ager31e71382008-08-13 09:32:07 +0000859 // Check if the value is 'false'.
860 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 __ b(eq, false_target);
862
mads.s.ager31e71382008-08-13 09:32:07 +0000863 // Check if the value is 'true'.
864 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 __ b(eq, true_target);
866
mads.s.ager31e71382008-08-13 09:32:07 +0000867 // Check if the value is 'undefined'.
868 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 __ b(eq, false_target);
870
mads.s.ager31e71382008-08-13 09:32:07 +0000871 // Check if the value is a smi.
872 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000874 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 __ b(eq, true_target);
876
877 // Slow case: call the runtime.
878 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000879 __ CallRuntime(Runtime::kToBool, 1);
880
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 // Convert result (r0) to condition code
882 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 cc_reg_ = ne;
885}
886
887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888class GetPropertyStub : public CodeStub {
889 public:
890 GetPropertyStub() { }
891
892 private:
893 Major MajorKey() { return GetProperty; }
894 int MinorKey() { return 0; }
895 void Generate(MacroAssembler* masm);
896
897 const char* GetName() { return "GetPropertyStub"; }
898};
899
900
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901class SetPropertyStub : public CodeStub {
902 public:
903 SetPropertyStub() { }
904
905 private:
906 Major MajorKey() { return SetProperty; }
907 int MinorKey() { return 0; }
908 void Generate(MacroAssembler* masm);
909
910 const char* GetName() { return "GetPropertyStub"; }
911};
912
913
kasper.lund7276f142008-07-30 08:49:36 +0000914class GenericBinaryOpStub : public CodeStub {
915 public:
916 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
917
918 private:
919 Token::Value op_;
920
921 Major MajorKey() { return GenericBinaryOp; }
922 int MinorKey() { return static_cast<int>(op_); }
923 void Generate(MacroAssembler* masm);
924
925 const char* GetName() {
926 switch (op_) {
927 case Token::ADD: return "GenericBinaryOpStub_ADD";
928 case Token::SUB: return "GenericBinaryOpStub_SUB";
929 case Token::MUL: return "GenericBinaryOpStub_MUL";
930 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000931 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
932 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
933 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
934 case Token::SAR: return "GenericBinaryOpStub_SAR";
935 case Token::SHL: return "GenericBinaryOpStub_SHL";
936 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000937 default: return "GenericBinaryOpStub";
938 }
939 }
940
941#ifdef DEBUG
942 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
943#endif
944};
945
946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947class InvokeBuiltinStub : public CodeStub {
948 public:
949 enum Kind { Inc, Dec, ToNumber };
950 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
951
952 private:
953 Kind kind_;
954 int argc_;
955
956 Major MajorKey() { return InvokeBuiltin; }
957 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
958 void Generate(MacroAssembler* masm);
959
960 const char* GetName() { return "InvokeBuiltinStub"; }
961
962#ifdef DEBUG
963 void Print() {
964 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
965 static_cast<int>(kind_),
966 argc_);
967 }
968#endif
969};
970
971
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000972void ArmCodeGenerator::GetReferenceProperty(Expression* key) {
973 ASSERT(!ref()->is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 Reference::Type type = ref()->type();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000976 // TODO(1241834): Make sure that this it is safe to ignore the distinction
977 // between access types LOAD and LOAD_TYPEOF_EXPR. If there is a chance
978 // that reference errors can be thrown below, we must distinguish between
979 // the two kinds of loads (typeof expression loads must not throw a
980 // reference error).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 if (type == Reference::NAMED) {
982 // Compute the name of the property.
983 Literal* literal = key->AsLiteral();
984 Handle<String> name(String::cast(*literal->handle()));
985
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 // Call the appropriate IC code.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000987 // Setup the name register.
988 __ mov(r2, Operand(name));
989 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
990 Variable* var = ref()->expression()->AsVariableProxy()->AsVariable();
991 if (var != NULL) {
992 ASSERT(var->is_global());
ager@chromium.org236ad962008-09-25 09:45:57 +0000993 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +0000995 __ Call(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 } else {
mads.s.ager31e71382008-08-13 09:32:07 +0000999 // Access keyed property.
1000 ASSERT(type == Reference::KEYED);
1001
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001002 // TODO(1224671): Implement inline caching for keyed loads as on ia32.
1003 GetPropertyStub stub;
1004 __ CallStub(&stub);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 }
mads.s.ager31e71382008-08-13 09:32:07 +00001006 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007}
1008
1009
kasper.lund7276f142008-07-30 08:49:36 +00001010void ArmCodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +00001011 // sp[0] : y
1012 // sp[1] : x
1013 // result : r0
1014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 // Stub is entered with a call: 'return address' is in lr.
1016 switch (op) {
1017 case Token::ADD: // fall through.
1018 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001019 case Token::MUL:
1020 case Token::BIT_OR:
1021 case Token::BIT_AND:
1022 case Token::BIT_XOR:
1023 case Token::SHL:
1024 case Token::SHR:
1025 case Token::SAR: {
mads.s.ager31e71382008-08-13 09:32:07 +00001026 __ pop(r0); // r0 : y
1027 __ pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +00001028 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 __ CallStub(&stub);
1030 break;
1031 }
1032
1033 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +00001034 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001035 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 break;
1037 }
1038
1039 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +00001040 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001041 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 break;
1043 }
1044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 case Token::COMMA:
mads.s.ager31e71382008-08-13 09:32:07 +00001046 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 // simply discard left value
mads.s.ager31e71382008-08-13 09:32:07 +00001048 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 break;
1050
1051 default:
1052 // Other cases should have been handled before this point.
1053 UNREACHABLE();
1054 break;
1055 }
1056}
1057
1058
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001059class DeferredInlinedSmiOperation: public DeferredCode {
1060 public:
1061 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
1062 int value, bool reversed) :
1063 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
1064 set_comment("[ DeferredInlinedSmiOperation");
1065 }
1066
1067 virtual void Generate() {
1068 switch (op_) {
1069 case Token::ADD: {
1070 if (reversed_) {
1071 // revert optimistic add
1072 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
1073 __ mov(r1, Operand(Smi::FromInt(value_))); // x
1074 } else {
1075 // revert optimistic add
1076 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
1077 __ mov(r0, Operand(Smi::FromInt(value_)));
1078 }
1079 break;
1080 }
1081
1082 case Token::SUB: {
1083 if (reversed_) {
1084 // revert optimistic sub
1085 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
1086 __ mov(r1, Operand(Smi::FromInt(value_)));
1087 } else {
1088 __ add(r1, r0, Operand(Smi::FromInt(value_)));
1089 __ mov(r0, Operand(Smi::FromInt(value_)));
1090 }
1091 break;
1092 }
1093
1094 case Token::BIT_OR:
1095 case Token::BIT_XOR:
1096 case Token::BIT_AND: {
1097 if (reversed_) {
1098 __ mov(r1, Operand(Smi::FromInt(value_)));
1099 } else {
1100 __ mov(r1, Operand(r0));
1101 __ mov(r0, Operand(Smi::FromInt(value_)));
1102 }
1103 break;
1104 }
1105
1106 case Token::SHL:
1107 case Token::SHR:
1108 case Token::SAR: {
1109 if (!reversed_) {
1110 __ mov(r1, Operand(r0));
1111 __ mov(r0, Operand(Smi::FromInt(value_)));
1112 } else {
1113 UNREACHABLE(); // should have been handled in SmiOperation
1114 }
1115 break;
1116 }
1117
1118 default:
1119 // other cases should have been handled before this point.
1120 UNREACHABLE();
1121 break;
1122 }
1123
1124 GenericBinaryOpStub igostub(op_);
1125 __ CallStub(&igostub);
1126 }
1127
1128 private:
1129 Token::Value op_;
1130 int value_;
1131 bool reversed_;
1132};
1133
1134
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135void ArmCodeGenerator::SmiOperation(Token::Value op,
1136 Handle<Object> value,
1137 bool reversed) {
1138 // NOTE: This is an attempt to inline (a bit) more of the code for
1139 // some possible smi operations (like + and -) when (at least) one
1140 // of the operands is a literal smi. With this optimization, the
1141 // performance of the system is increased by ~15%, and the generated
1142 // code size is increased by ~1% (measured on a combination of
1143 // different benchmarks).
1144
mads.s.ager31e71382008-08-13 09:32:07 +00001145 // sp[0] : operand
1146
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001147 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
1149 Label exit;
mads.s.ager31e71382008-08-13 09:32:07 +00001150 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 switch (op) {
1153 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001154 DeferredCode* deferred =
1155 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001157 __ add(r0, r0, Operand(value), SetCC);
1158 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001160 __ b(ne, deferred->enter());
1161 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 break;
1163 }
1164
1165 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001166 DeferredCode* deferred =
1167 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001170 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001172 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001174 __ b(vs, deferred->enter());
1175 __ tst(r0, Operand(kSmiTagMask));
1176 __ b(ne, deferred->enter());
1177 __ bind(deferred->exit());
1178 break;
1179 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001181 case Token::BIT_OR:
1182 case Token::BIT_XOR:
1183 case Token::BIT_AND: {
1184 DeferredCode* deferred =
1185 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
1186 __ tst(r0, Operand(kSmiTagMask));
1187 __ b(ne, deferred->enter());
1188 switch (op) {
1189 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
1190 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
1191 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
1192 default: UNREACHABLE();
1193 }
1194 __ bind(deferred->exit());
1195 break;
1196 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001198 case Token::SHL:
1199 case Token::SHR:
1200 case Token::SAR: {
1201 if (reversed) {
1202 __ mov(ip, Operand(value));
1203 __ push(ip);
1204 __ push(r0);
1205 GenericBinaryOperation(op);
1206
1207 } else {
1208 int shift_value = int_value & 0x1f; // least significant 5 bits
1209 DeferredCode* deferred =
1210 new DeferredInlinedSmiOperation(this, op, shift_value, false);
1211 __ tst(r0, Operand(kSmiTagMask));
1212 __ b(ne, deferred->enter());
1213 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
1214 switch (op) {
1215 case Token::SHL: {
1216 __ mov(r2, Operand(r2, LSL, shift_value));
1217 // check that the *unsigned* result fits in a smi
1218 __ add(r3, r2, Operand(0x40000000), SetCC);
1219 __ b(mi, deferred->enter());
1220 break;
1221 }
1222 case Token::SHR: {
1223 // LSR by immediate 0 means shifting 32 bits.
1224 if (shift_value != 0) {
1225 __ mov(r2, Operand(r2, LSR, shift_value));
1226 }
1227 // check that the *unsigned* result fits in a smi
1228 // neither of the two high-order bits can be set:
1229 // - 0x80000000: high bit would be lost when smi tagging
1230 // - 0x40000000: this number would convert to negative when
1231 // smi tagging these two cases can only happen with shifts
1232 // by 0 or 1 when handed a valid smi
1233 __ and_(r3, r2, Operand(0xc0000000), SetCC);
1234 __ b(ne, deferred->enter());
1235 break;
1236 }
1237 case Token::SAR: {
1238 if (shift_value != 0) {
1239 // ASR by immediate 0 means shifting 32 bits.
1240 __ mov(r2, Operand(r2, ASR, shift_value));
1241 }
1242 break;
1243 }
1244 default: UNREACHABLE();
1245 }
1246 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1247 __ bind(deferred->exit());
1248 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 break;
1250 }
1251
1252 default:
1253 if (!reversed) {
mads.s.ager31e71382008-08-13 09:32:07 +00001254 __ push(r0);
1255 __ mov(r0, Operand(value));
1256 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 } else {
1258 __ mov(ip, Operand(value));
1259 __ push(ip);
mads.s.ager31e71382008-08-13 09:32:07 +00001260 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 }
kasper.lund7276f142008-07-30 08:49:36 +00001262 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 break;
1264 }
1265
1266 __ bind(&exit);
1267}
1268
1269
1270void ArmCodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +00001271 // sp[0] : y
1272 // sp[1] : x
1273 // result : cc register
1274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 // Strict only makes sense for equality comparisons.
1276 ASSERT(!strict || cc == eq);
1277
1278 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001279 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1280 if (cc == gt || cc == le) {
1281 cc = ReverseCondition(cc);
mads.s.ager31e71382008-08-13 09:32:07 +00001282 __ pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001283 __ pop(r0);
1284 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001285 __ pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001286 __ pop(r1);
1287 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 __ orr(r2, r0, Operand(r1));
1289 __ tst(r2, Operand(kSmiTagMask));
1290 __ b(eq, &smi);
1291
1292 // Perform non-smi comparison by runtime call.
1293 __ push(r1);
1294
1295 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001296 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 int argc;
1298 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001299 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 argc = 1;
1301 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001302 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 int ncr; // NaN compare result
1304 if (cc == lt || cc == le) {
1305 ncr = GREATER;
1306 } else {
1307 ASSERT(cc == gt || cc == ge); // remaining cases
1308 ncr = LESS;
1309 }
mads.s.ager31e71382008-08-13 09:32:07 +00001310 __ push(r0);
1311 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 argc = 2;
1313 }
1314
1315 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1316 // tagged as a small integer.
mads.s.ager31e71382008-08-13 09:32:07 +00001317 __ push(r0);
1318 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001319 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 __ cmp(r0, Operand(0));
1321 __ b(&exit);
1322
1323 // test smi equality by pointer comparison.
1324 __ bind(&smi);
1325 __ cmp(r1, Operand(r0));
1326
1327 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 cc_reg_ = cc;
1329}
1330
1331
kasper.lund7276f142008-07-30 08:49:36 +00001332class CallFunctionStub: public CodeStub {
1333 public:
1334 explicit CallFunctionStub(int argc) : argc_(argc) {}
1335
1336 void Generate(MacroAssembler* masm);
1337
1338 private:
1339 int argc_;
1340
1341 const char* GetName() { return "CallFuntionStub"; }
1342
1343#if defined(DEBUG)
1344 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1345#endif // defined(DEBUG)
1346
1347 Major MajorKey() { return CallFunction; }
1348 int MinorKey() { return argc_; }
1349};
1350
1351
mads.s.ager31e71382008-08-13 09:32:07 +00001352// Call the function on the stack with the given arguments.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353void ArmCodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
1354 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001356 for (int i = 0; i < args->length(); i++) {
1357 Load(args->at(i));
1358 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
kasper.lund7276f142008-07-30 08:49:36 +00001360 // Record the position for debugging purposes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 __ RecordPosition(position);
1362
kasper.lund7276f142008-07-30 08:49:36 +00001363 // Use the shared code stub to call the function.
1364 CallFunctionStub call_function(args->length());
1365 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366
1367 // Restore context and pop function from the stack.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00001369 __ pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370}
1371
1372
1373void ArmCodeGenerator::Branch(bool if_true, Label* L) {
1374 ASSERT(has_cc());
1375 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
1376 __ b(cc, L);
1377 cc_reg_ = al;
1378}
1379
1380
1381void ArmCodeGenerator::CheckStack() {
1382 if (FLAG_check_stack) {
1383 Comment cmnt(masm_, "[ check stack");
1384 StackCheckStub stub;
1385 __ CallStub(&stub);
1386 }
1387}
1388
1389
1390void ArmCodeGenerator::VisitBlock(Block* node) {
1391 Comment cmnt(masm_, "[ Block");
1392 if (FLAG_debug_info) RecordStatementPosition(node);
1393 node->set_break_stack_height(break_stack_height_);
1394 VisitStatements(node->statements());
1395 __ bind(node->break_target());
1396}
1397
1398
1399void ArmCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00001400 __ mov(r0, Operand(pairs));
1401 __ push(r0);
1402 __ push(cp);
1403 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1404 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001406 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407}
1408
1409
1410void ArmCodeGenerator::VisitDeclaration(Declaration* node) {
1411 Comment cmnt(masm_, "[ Declaration");
1412 Variable* var = node->proxy()->var();
1413 ASSERT(var != NULL); // must have been resolved
1414 Slot* slot = var->slot();
1415
1416 // If it was not possible to allocate the variable at compile time,
1417 // we need to "declare" it at runtime to make sure it actually
1418 // exists in the local context.
1419 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1420 // Variables with a "LOOKUP" slot were introduced as non-locals
1421 // during variable resolution and must have mode DYNAMIC.
1422 ASSERT(var->mode() == Variable::DYNAMIC);
1423 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00001424 __ push(cp);
1425 __ mov(r0, Operand(var->name()));
1426 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 // Declaration nodes are always declared in only two modes.
1428 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1429 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001430 __ mov(r0, Operand(Smi::FromInt(attr)));
1431 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 // Push initial value, if any.
1433 // Note: For variables we must not push an initial value (such as
1434 // 'undefined') because we may have a (legal) redeclaration and we
1435 // must not destroy the current value.
1436 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001437 __ mov(r0, Operand(Factory::the_hole_value()));
1438 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 } else if (node->fun() != NULL) {
1440 Load(node->fun());
1441 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001442 __ mov(r0, Operand(0)); // no initial value!
1443 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 }
1445 __ CallRuntime(Runtime::kDeclareContextSlot, 5);
mads.s.ager31e71382008-08-13 09:32:07 +00001446 __ push(r0);
1447
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 return;
1449 }
1450
1451 ASSERT(!var->is_global());
1452
1453 // If we have a function or a constant, we need to initialize the variable.
1454 Expression* val = NULL;
1455 if (node->mode() == Variable::CONST) {
1456 val = new Literal(Factory::the_hole_value());
1457 } else {
1458 val = node->fun(); // NULL if we don't have a function
1459 }
1460
1461 if (val != NULL) {
1462 // Set initial value.
1463 Reference target(this, node->proxy());
1464 Load(val);
1465 SetValue(&target);
1466 // Get rid of the assigned value (declarations are statements).
mads.s.ager31e71382008-08-13 09:32:07 +00001467 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 }
1469}
1470
1471
1472void ArmCodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
1473 Comment cmnt(masm_, "[ ExpressionStatement");
1474 if (FLAG_debug_info) RecordStatementPosition(node);
1475 Expression* expression = node->expression();
1476 expression->MarkAsStatement();
1477 Load(expression);
mads.s.ager31e71382008-08-13 09:32:07 +00001478 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479}
1480
1481
1482void ArmCodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
1483 Comment cmnt(masm_, "// EmptyStatement");
1484 // nothing to do
1485}
1486
1487
1488void ArmCodeGenerator::VisitIfStatement(IfStatement* node) {
1489 Comment cmnt(masm_, "[ IfStatement");
1490 // Generate different code depending on which
1491 // parts of the if statement are present or not.
1492 bool has_then_stm = node->HasThenStatement();
1493 bool has_else_stm = node->HasElseStatement();
1494
1495 if (FLAG_debug_info) RecordStatementPosition(node);
1496
1497 Label exit;
1498 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001499 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500 Label then;
1501 Label else_;
1502 // if (cond)
1503 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
1504 Branch(false, &else_);
1505 // then
1506 __ bind(&then);
1507 Visit(node->then_statement());
1508 __ b(&exit);
1509 // else
1510 __ bind(&else_);
1511 Visit(node->else_statement());
1512
1513 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001514 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515 ASSERT(!has_else_stm);
1516 Label then;
1517 // if (cond)
1518 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &exit, true);
1519 Branch(false, &exit);
1520 // then
1521 __ bind(&then);
1522 Visit(node->then_statement());
1523
1524 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001525 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 ASSERT(!has_then_stm);
1527 Label else_;
1528 // if (!cond)
1529 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &else_, true);
1530 Branch(true, &exit);
1531 // else
1532 __ bind(&else_);
1533 Visit(node->else_statement());
1534
1535 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001536 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 ASSERT(!has_then_stm && !has_else_stm);
1538 // if (cond)
1539 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &exit, false);
1540 if (has_cc()) {
1541 cc_reg_ = al;
1542 } else {
1543 __ pop(r0); // __ Pop(no_reg)
1544 }
1545 }
1546
1547 // end
1548 __ bind(&exit);
1549}
1550
1551
1552void ArmCodeGenerator::CleanStack(int num_bytes) {
1553 ASSERT(num_bytes >= 0);
1554 if (num_bytes > 0) {
mads.s.ager31e71382008-08-13 09:32:07 +00001555 __ add(sp, sp, Operand(num_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 }
1557}
1558
1559
1560void ArmCodeGenerator::VisitContinueStatement(ContinueStatement* node) {
1561 Comment cmnt(masm_, "[ ContinueStatement");
1562 if (FLAG_debug_info) RecordStatementPosition(node);
1563 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1564 __ b(node->target()->continue_target());
1565}
1566
1567
1568void ArmCodeGenerator::VisitBreakStatement(BreakStatement* node) {
1569 Comment cmnt(masm_, "[ BreakStatement");
1570 if (FLAG_debug_info) RecordStatementPosition(node);
1571 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1572 __ b(node->target()->break_target());
1573}
1574
1575
1576void ArmCodeGenerator::VisitReturnStatement(ReturnStatement* node) {
1577 Comment cmnt(masm_, "[ ReturnStatement");
1578 if (FLAG_debug_info) RecordStatementPosition(node);
1579 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00001580 // Move the function result into r0.
1581 __ pop(r0);
1582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583 __ b(&function_return_);
1584}
1585
1586
1587void ArmCodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
1588 Comment cmnt(masm_, "[ WithEnterStatement");
1589 if (FLAG_debug_info) RecordStatementPosition(node);
1590 Load(node->expression());
kasper.lund7276f142008-07-30 08:49:36 +00001591 __ CallRuntime(Runtime::kPushContext, 1);
1592 if (kDebug) {
1593 Label verified_true;
1594 __ cmp(r0, Operand(cp));
1595 __ b(eq, &verified_true);
1596 __ stop("PushContext: r0 is expected to be the same as cp");
1597 __ bind(&verified_true);
1598 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599 // Update context local.
1600 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1601}
1602
1603
1604void ArmCodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
1605 Comment cmnt(masm_, "[ WithExitStatement");
1606 // Pop context.
1607 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1608 // Update context local.
1609 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1610}
1611
1612
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001613int ArmCodeGenerator::FastCaseSwitchMaxOverheadFactor() {
1614 return kFastCaseSwitchMaxOverheadFactor;
1615}
1616
1617int ArmCodeGenerator::FastCaseSwitchMinCaseCount() {
1618 return kFastCaseSwitchMinCaseCount;
1619}
1620
1621
1622void ArmCodeGenerator::GenerateFastCaseSwitchJumpTable(
1623 SwitchStatement* node, int min_index, int range, Label *fail_label,
1624 SmartPointer<Label*> &case_targets, SmartPointer<Label> &case_labels) {
1625
1626 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
1627
1628 __ pop(r0);
1629 if (min_index != 0) {
1630 // small positive numbers can be immediate operands.
1631 if (min_index < 0) {
1632 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1633 } else {
1634 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1635 }
1636 }
1637 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
1638 __ b(ne, fail_label);
1639 __ cmp(r0, Operand(Smi::FromInt(range)));
1640 __ b(ge, fail_label);
1641 __ add(pc, pc, Operand(r0, LSL, 2 - kSmiTagSize));
1642 // One extra instruction offsets the table, so the table's start address is
1643 // the pc-register at the above add.
1644 __ stop("Unreachable: Switch table alignment");
1645
1646 // table containing branch operations.
1647 for (int i = 0; i < range; i++) {
1648 __ b(case_targets[i]);
1649 }
1650
1651 GenerateFastCaseSwitchCases(node, case_labels);
1652}
1653
1654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655void ArmCodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
1656 Comment cmnt(masm_, "[ SwitchStatement");
1657 if (FLAG_debug_info) RecordStatementPosition(node);
1658 node->set_break_stack_height(break_stack_height_);
1659
1660 Load(node->tag());
1661
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001662 if (TryGenerateFastCaseSwitchStatement(node)) {
1663 return;
1664 }
1665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 Label next, fall_through, default_case;
1667 ZoneList<CaseClause*>* cases = node->cases();
1668 int length = cases->length();
1669
1670 for (int i = 0; i < length; i++) {
1671 CaseClause* clause = cases->at(i);
1672
1673 Comment cmnt(masm_, "[ case clause");
1674
1675 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001676 // Continue matching cases. The program will execute the default case's
1677 // statements if it does not match any of the cases.
1678 __ b(&next);
1679
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 // Bind the default case label, so we can branch to it when we
1681 // have compared against all other cases.
1682 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001683 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 } else {
1685 __ bind(&next);
1686 next.Unuse();
mads.s.ager31e71382008-08-13 09:32:07 +00001687 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 __ push(r0); // duplicate TOS
1689 Load(clause->label());
1690 Comparison(eq, true);
1691 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 }
1693
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001694 // Entering the case statement for the first time. Remove the switch value
1695 // from the stack.
1696 __ pop(r0);
1697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001699 // This is also the target for the fall through from the previous case's
1700 // statements which has to skip over the matching code and the popping of
1701 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702 __ bind(&fall_through);
1703 fall_through.Unuse();
1704 VisitStatements(clause->statements());
1705 __ b(&fall_through);
1706 }
1707
1708 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001709 // Reached the end of the case statements without matching any of the cases.
1710 if (default_case.is_bound()) {
1711 // A default case exists -> execute its statements.
1712 __ b(&default_case);
1713 } else {
1714 // Remove the switch value from the stack.
1715 __ pop(r0);
1716 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717
1718 __ bind(&fall_through);
1719 __ bind(node->break_target());
1720}
1721
1722
1723void ArmCodeGenerator::VisitLoopStatement(LoopStatement* node) {
1724 Comment cmnt(masm_, "[ LoopStatement");
1725 if (FLAG_debug_info) RecordStatementPosition(node);
1726 node->set_break_stack_height(break_stack_height_);
1727
1728 // simple condition analysis
1729 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1730 if (node->cond() == NULL) {
1731 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1732 info = ALWAYS_TRUE;
1733 } else {
1734 Literal* lit = node->cond()->AsLiteral();
1735 if (lit != NULL) {
1736 if (lit->IsTrue()) {
1737 info = ALWAYS_TRUE;
1738 } else if (lit->IsFalse()) {
1739 info = ALWAYS_FALSE;
1740 }
1741 }
1742 }
1743
1744 Label loop, entry;
1745
1746 // init
1747 if (node->init() != NULL) {
1748 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1749 Visit(node->init());
1750 }
1751 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
1752 __ b(&entry);
1753 }
1754
1755 // body
1756 __ bind(&loop);
1757 Visit(node->body());
1758
1759 // next
1760 __ bind(node->continue_target());
1761 if (node->next() != NULL) {
1762 // Record source position of the statement as this code which is after the
1763 // code for the body actually belongs to the loop statement and not the
1764 // body.
1765 if (FLAG_debug_info) __ RecordPosition(node->statement_pos());
1766 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1767 Visit(node->next());
1768 }
1769
1770 // cond
1771 __ bind(&entry);
1772 switch (info) {
1773 case ALWAYS_TRUE:
1774 CheckStack(); // TODO(1222600): ignore if body contains calls.
1775 __ b(&loop);
1776 break;
1777 case ALWAYS_FALSE:
1778 break;
1779 case DONT_KNOW:
1780 CheckStack(); // TODO(1222600): ignore if body contains calls.
1781 LoadCondition(node->cond(),
1782 CodeGenState::LOAD,
1783 &loop,
1784 node->break_target(),
1785 true);
1786 Branch(true, &loop);
1787 break;
1788 }
1789
1790 // exit
1791 __ bind(node->break_target());
1792}
1793
1794
1795void ArmCodeGenerator::VisitForInStatement(ForInStatement* node) {
1796 Comment cmnt(masm_, "[ ForInStatement");
1797 if (FLAG_debug_info) RecordStatementPosition(node);
1798
1799 // We keep stuff on the stack while the body is executing.
1800 // Record it, so that a break/continue crossing this statement
1801 // can restore the stack.
1802 const int kForInStackSize = 5 * kPointerSize;
1803 break_stack_height_ += kForInStackSize;
1804 node->set_break_stack_height(break_stack_height_);
1805
1806 Label loop, next, entry, cleanup, exit, primitive, jsobject;
1807 Label filter_key, end_del_check, fixed_array, non_string;
1808
1809 // Get the object to enumerate over (converted to JSObject).
1810 Load(node->enumerable());
mads.s.ager31e71382008-08-13 09:32:07 +00001811 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812
1813 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1814 // to the specification. 12.6.4 mandates a call to ToObject.
1815 __ cmp(r0, Operand(Factory::undefined_value()));
1816 __ b(eq, &exit);
1817 __ cmp(r0, Operand(Factory::null_value()));
1818 __ b(eq, &exit);
1819
1820 // Stack layout in body:
1821 // [iteration counter (Smi)]
1822 // [length of array]
1823 // [FixedArray]
1824 // [Map or 0]
1825 // [Object]
1826
1827 // Check if enumerable is already a JSObject
1828 __ tst(r0, Operand(kSmiTagMask));
1829 __ b(eq, &primitive);
1830 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1831 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001832 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 __ b(hs, &jsobject);
1834
1835 __ bind(&primitive);
mads.s.ager31e71382008-08-13 09:32:07 +00001836 __ push(r0);
1837 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001838 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839
1840
1841 __ bind(&jsobject);
1842
1843 // Get the set of properties (as a FixedArray or Map).
1844 __ push(r0); // duplicate the object being enumerated
mads.s.ager31e71382008-08-13 09:32:07 +00001845 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1847
1848 // If we got a Map, we can do a fast modification check.
1849 // Otherwise, we got a FixedArray, and we have to do a slow check.
1850 __ mov(r2, Operand(r0));
1851 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1852 __ cmp(r1, Operand(Factory::meta_map()));
1853 __ b(ne, &fixed_array);
1854
1855 // Get enum cache
1856 __ mov(r1, Operand(r0));
1857 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1858 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1859 __ ldr(r2,
1860 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1861
mads.s.ager31e71382008-08-13 09:32:07 +00001862 __ push(r0); // map
1863 __ push(r2); // enum cache bridge cache
1864 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00001866 __ push(r0);
1867 __ mov(r0, Operand(Smi::FromInt(0)));
1868 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 __ b(&entry);
1870
1871
1872 __ bind(&fixed_array);
1873
1874 __ mov(r1, Operand(Smi::FromInt(0)));
1875 __ push(r1); // insert 0 in place of Map
mads.s.ager31e71382008-08-13 09:32:07 +00001876 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877
1878 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001879 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00001881 __ push(r0);
1882 __ mov(r0, Operand(Smi::FromInt(0))); // init index
1883 __ push(r0);
1884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 __ b(&entry);
1886
1887 // Body.
1888 __ bind(&loop);
1889 Visit(node->body());
1890
1891 // Next.
1892 __ bind(node->continue_target());
1893 __ bind(&next);
mads.s.ager31e71382008-08-13 09:32:07 +00001894 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 __ add(r0, r0, Operand(Smi::FromInt(1)));
mads.s.ager31e71382008-08-13 09:32:07 +00001896 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897
1898 // Condition.
1899 __ bind(&entry);
1900
mads.s.ager31e71382008-08-13 09:32:07 +00001901 // sp[0] : index
1902 // sp[1] : array/enum cache length
1903 // sp[2] : array or enum cache
1904 // sp[3] : 0 or map
1905 // sp[4] : enumerable
1906 __ ldr(r0, MemOperand(sp, 0 * kPointerSize)); // load the current count
1907 __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // load the length
1908 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 __ b(hs, &cleanup);
1910
mads.s.ager31e71382008-08-13 09:32:07 +00001911 __ ldr(r0, MemOperand(sp, 0 * kPointerSize));
1912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 // Get the i'th entry of the array.
mads.s.ager31e71382008-08-13 09:32:07 +00001914 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1916 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1917
1918 // Get Map or 0.
mads.s.ager31e71382008-08-13 09:32:07 +00001919 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 // Check if this (still) matches the map of the enumerable.
1921 // If not, we have to filter the key.
mads.s.ager31e71382008-08-13 09:32:07 +00001922 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1924 __ cmp(r1, Operand(r2));
1925 __ b(eq, &end_del_check);
1926
1927 // Convert the entry to a string (or null if it isn't a property anymore).
mads.s.ager31e71382008-08-13 09:32:07 +00001928 __ ldr(r0, MemOperand(sp, 4 * kPointerSize)); // push enumerable
1929 __ push(r0);
1930 __ push(r3); // push entry
1931 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001932 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934
1935 // If the property has been removed while iterating, we just skip it.
1936 __ cmp(r3, Operand(Factory::null_value()));
1937 __ b(eq, &next);
1938
1939
1940 __ bind(&end_del_check);
1941
1942 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00001943 // r3: i'th entry of the enum cache (or string there of)
1944 __ push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 { Reference each(this, node->each());
1946 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001947 if (each.size() > 0) {
1948 __ ldr(r0, MemOperand(sp, kPointerSize * each.size()));
1949 __ push(r0);
1950 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 SetValue(&each);
mads.s.ager31e71382008-08-13 09:32:07 +00001952 if (each.size() > 0) {
1953 __ pop(r0); // discard the value
1954 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 }
1956 }
mads.s.ager31e71382008-08-13 09:32:07 +00001957 __ pop(); // pop the i'th entry pushed above
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 CheckStack(); // TODO(1222600): ignore if body contains calls.
1959 __ jmp(&loop);
1960
1961 // Cleanup.
1962 __ bind(&cleanup);
1963 __ bind(node->break_target());
mads.s.ager31e71382008-08-13 09:32:07 +00001964 __ add(sp, sp, Operand(5 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965
1966 // Exit.
1967 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968
1969 break_stack_height_ -= kForInStackSize;
1970}
1971
1972
1973void ArmCodeGenerator::VisitTryCatch(TryCatch* node) {
1974 Comment cmnt(masm_, "[ TryCatch");
1975
1976 Label try_block, exit;
1977
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 __ bl(&try_block);
1979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 // --- Catch block ---
1981
1982 // Store the caught exception in the catch variable.
mads.s.ager31e71382008-08-13 09:32:07 +00001983 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 { Reference ref(this, node->catch_var());
1985 // Load the exception to the top of the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001986 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
1987 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 SetValue(&ref);
mads.s.ager31e71382008-08-13 09:32:07 +00001989 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 }
1991
1992 // Remove the exception from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001993 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994
1995 VisitStatements(node->catch_block()->statements());
1996 __ b(&exit);
1997
1998
1999 // --- Try block ---
2000 __ bind(&try_block);
2001
2002 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
2003
2004 // Introduce shadow labels for all escapes from the try block,
2005 // including returns. We should probably try to unify the escaping
2006 // labels and the return label.
2007 int nof_escapes = node->escaping_labels()->length();
2008 List<LabelShadow*> shadows(1 + nof_escapes);
2009 shadows.Add(new LabelShadow(&function_return_));
2010 for (int i = 0; i < nof_escapes; i++) {
2011 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
2012 }
2013
2014 // Generate code for the statements in the try block.
2015 VisitStatements(node->try_block()->statements());
mads.s.ager31e71382008-08-13 09:32:07 +00002016 __ pop(r0); // Discard the result.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017
2018 // Stop the introduced shadowing and count the number of required unlinks.
2019 int nof_unlinks = 0;
2020 for (int i = 0; i <= nof_escapes; i++) {
2021 shadows[i]->StopShadowing();
2022 if (shadows[i]->is_linked()) nof_unlinks++;
2023 }
2024
2025 // Unlink from try chain.
2026 // TOS contains code slot
2027 const int kNextOffset = StackHandlerConstants::kNextOffset +
2028 StackHandlerConstants::kAddressDisplacement;
2029 __ ldr(r1, MemOperand(sp, kNextOffset)); // read next_sp
2030 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2031 __ str(r1, MemOperand(r3));
2032 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
2033 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2034 // Code slot popped.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 if (nof_unlinks > 0) __ b(&exit);
2036
2037 // Generate unlink code for all used shadow labels.
2038 for (int i = 0; i <= nof_escapes; i++) {
2039 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002040 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 __ bind(shadows[i]);
2042
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 // Reload sp from the top handler, because some statements that we
2044 // break from (eg, for...in) may have left stuff on the stack.
2045 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2046 __ ldr(sp, MemOperand(r3));
2047
2048 __ ldr(r1, MemOperand(sp, kNextOffset));
2049 __ str(r1, MemOperand(r3));
2050 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
2051 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2052 // Code slot popped.
2053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 __ b(shadows[i]->shadowed());
2055 }
2056 }
2057
2058 __ bind(&exit);
2059}
2060
2061
2062void ArmCodeGenerator::VisitTryFinally(TryFinally* node) {
2063 Comment cmnt(masm_, "[ TryFinally");
2064
2065 // State: Used to keep track of reason for entering the finally
2066 // block. Should probably be extended to hold information for
2067 // break/continue from within the try block.
2068 enum { FALLING, THROWING, JUMPING };
2069
2070 Label exit, unlink, try_block, finally_block;
2071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 __ bl(&try_block);
2073
mads.s.ager31e71382008-08-13 09:32:07 +00002074 __ push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 // In case of thrown exceptions, this is where we continue.
2076 __ mov(r2, Operand(Smi::FromInt(THROWING)));
2077 __ b(&finally_block);
2078
2079
2080 // --- Try block ---
2081 __ bind(&try_block);
2082
2083 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
2084
2085 // Introduce shadow labels for all escapes from the try block,
2086 // including returns. We should probably try to unify the escaping
2087 // labels and the return label.
2088 int nof_escapes = node->escaping_labels()->length();
2089 List<LabelShadow*> shadows(1 + nof_escapes);
2090 shadows.Add(new LabelShadow(&function_return_));
2091 for (int i = 0; i < nof_escapes; i++) {
2092 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
2093 }
2094
2095 // Generate code for the statements in the try block.
2096 VisitStatements(node->try_block()->statements());
2097
2098 // Stop the introduced shadowing and count the number of required
2099 // unlinks.
2100 int nof_unlinks = 0;
2101 for (int i = 0; i <= nof_escapes; i++) {
2102 shadows[i]->StopShadowing();
2103 if (shadows[i]->is_linked()) nof_unlinks++;
2104 }
2105
2106 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00002107 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
2108 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2110 if (nof_unlinks > 0) __ b(&unlink);
2111
2112 // Generate code that sets the state for all used shadow labels.
2113 for (int i = 0; i <= nof_escapes; i++) {
2114 if (shadows[i]->is_linked()) {
2115 __ bind(shadows[i]);
mads.s.ager31e71382008-08-13 09:32:07 +00002116 if (shadows[i]->shadowed() == &function_return_) {
2117 __ push(r0); // Materialize the return value on the stack
2118 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 // Fake TOS for break and continue (not return).
mads.s.ager31e71382008-08-13 09:32:07 +00002120 __ mov(r0, Operand(Factory::undefined_value()));
2121 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 }
2123 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
2124 __ b(&unlink);
2125 }
2126 }
2127
mads.s.ager31e71382008-08-13 09:32:07 +00002128 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 __ bind(&unlink);
2130
mads.s.ager31e71382008-08-13 09:32:07 +00002131 __ pop(r0); // Store TOS in r0 across stack manipulation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 // Reload sp from the top handler, because some statements that we
2133 // break from (eg, for...in) may have left stuff on the stack.
2134 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2135 __ ldr(sp, MemOperand(r3));
2136 const int kNextOffset = StackHandlerConstants::kNextOffset +
2137 StackHandlerConstants::kAddressDisplacement;
2138 __ ldr(r1, MemOperand(sp, kNextOffset));
2139 __ str(r1, MemOperand(r3));
2140 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
2141 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2142 // Code slot popped.
mads.s.ager31e71382008-08-13 09:32:07 +00002143 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144
2145 // --- Finally block ---
2146 __ bind(&finally_block);
2147
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002148 // Push the state on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002149 __ push(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002150
2151 // We keep two elements on the stack - the (possibly faked) result
2152 // and the state - while evaluating the finally block. Record it, so
2153 // that a break/continue crossing this statement can restore the
2154 // stack.
2155 const int kFinallyStackSize = 2 * kPointerSize;
2156 break_stack_height_ += kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157
2158 // Generate code for the statements in the finally block.
2159 VisitStatements(node->finally_block()->statements());
2160
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002161 // Restore state and return value or faked TOS.
mads.s.ager31e71382008-08-13 09:32:07 +00002162 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002163 __ pop(r0);
2164 break_stack_height_ -= kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165
2166 // Generate code that jumps to the right destination for all used
2167 // shadow labels.
2168 for (int i = 0; i <= nof_escapes; i++) {
2169 if (shadows[i]->is_bound()) {
2170 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2171 if (shadows[i]->shadowed() != &function_return_) {
2172 Label next;
2173 __ b(ne, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 __ b(shadows[i]->shadowed());
2175 __ bind(&next);
2176 } else {
2177 __ b(eq, shadows[i]->shadowed());
2178 }
2179 }
2180 }
2181
2182 // Check if we need to rethrow the exception.
2183 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2184 __ b(ne, &exit);
2185
2186 // Rethrow exception.
mads.s.ager31e71382008-08-13 09:32:07 +00002187 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 __ CallRuntime(Runtime::kReThrow, 1);
2189
2190 // Done.
2191 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192}
2193
2194
2195void ArmCodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
2196 Comment cmnt(masm_, "[ DebuggerStatament");
2197 if (FLAG_debug_info) RecordStatementPosition(node);
2198 __ CallRuntime(Runtime::kDebugBreak, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002199 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200}
2201
2202
2203void ArmCodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
2204 ASSERT(boilerplate->IsBoilerplate());
2205
2206 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002207 __ mov(r0, Operand(boilerplate));
2208 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209
2210 // Create a new closure.
mads.s.ager31e71382008-08-13 09:32:07 +00002211 __ push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 __ CallRuntime(Runtime::kNewClosure, 2);
mads.s.ager31e71382008-08-13 09:32:07 +00002213 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214}
2215
2216
2217void ArmCodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
2218 Comment cmnt(masm_, "[ FunctionLiteral");
2219
2220 // Build the function boilerplate and instantiate it.
2221 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002222 // Check for stack-overflow exception.
2223 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 InstantiateBoilerplate(boilerplate);
2225}
2226
2227
2228void ArmCodeGenerator::VisitFunctionBoilerplateLiteral(
2229 FunctionBoilerplateLiteral* node) {
2230 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2231 InstantiateBoilerplate(node->boilerplate());
2232}
2233
2234
2235void ArmCodeGenerator::VisitConditional(Conditional* node) {
2236 Comment cmnt(masm_, "[ Conditional");
2237 Label then, else_, exit;
2238 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
2239 Branch(false, &else_);
2240 __ bind(&then);
2241 Load(node->then_expression(), access());
2242 __ b(&exit);
2243 __ bind(&else_);
2244 Load(node->else_expression(), access());
2245 __ bind(&exit);
2246}
2247
2248
2249void ArmCodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002250 ASSERT(access() != CodeGenState::UNDEFINED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 Comment cmnt(masm_, "[ Slot");
2252
2253 if (node->type() == Slot::LOOKUP) {
2254 ASSERT(node->var()->mode() == Variable::DYNAMIC);
2255
2256 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00002257 __ push(cp);
2258 __ mov(r0, Operand(node->var()->name()));
2259 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002261 if (access() == CodeGenState::LOAD) {
2262 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2263 } else {
2264 ASSERT(access() == CodeGenState::LOAD_TYPEOF_EXPR);
2265 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002267 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268
2269 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002270 // Note: We would like to keep the assert below, but it fires because of
2271 // some nasty code in LoadTypeofExpression() which should be removed...
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 // ASSERT(node->var()->mode() != Variable::DYNAMIC);
2273
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002274 // Special handling for locals allocated in registers.
2275 __ ldr(r0, SlotOperand(node, r2));
2276 __ push(r0);
2277 if (node->var()->mode() == Variable::CONST) {
2278 // Const slots may contain 'the hole' value (the constant hasn't been
2279 // initialized yet) which needs to be converted into the 'undefined'
2280 // value.
2281 Comment cmnt(masm_, "[ Unhole const");
2282 __ pop(r0);
2283 __ cmp(r0, Operand(Factory::the_hole_value()));
2284 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2285 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 }
2287 }
2288}
2289
2290
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002291void ArmCodeGenerator::VisitVariableProxy(VariableProxy* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 Comment cmnt(masm_, "[ VariableProxy");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002293 Variable* var_node = node->var();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002295 Expression* expr = var_node->rewrite();
2296 if (expr != NULL) {
2297 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002299 ASSERT(var_node->is_global());
2300 if (is_referenced()) {
2301 if (var_node->AsProperty() != NULL) {
2302 __ RecordPosition(var_node->AsProperty()->position());
2303 }
2304 GetReferenceProperty(new Literal(var_node->name()));
2305 } else {
2306 Reference property(this, node);
2307 GetValue(&property);
2308 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309 }
2310}
2311
2312
2313void ArmCodeGenerator::VisitLiteral(Literal* node) {
2314 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002315 __ mov(r0, Operand(node->handle()));
2316 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317}
2318
2319
2320void ArmCodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
2321 Comment cmnt(masm_, "[ RexExp Literal");
2322
2323 // Retrieve the literal array and check the allocated entry.
2324
2325 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002326 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327
2328 // Load the literals array of the function.
2329 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2330
2331 // Load the literal at the ast saved index.
2332 int literal_offset =
2333 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2334 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2335
2336 Label done;
2337 __ cmp(r2, Operand(Factory::undefined_value()));
2338 __ b(ne, &done);
2339
2340 // If the entry is undefined we call the runtime system to computed
2341 // the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00002342 __ push(r1); // literal array (0)
2343 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
2344 __ push(r0); // literal index (1)
2345 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
2346 __ push(r0);
2347 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
2348 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002350 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351
mads.s.ager31e71382008-08-13 09:32:07 +00002352 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 // Push the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00002354 __ push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355}
2356
2357
2358// This deferred code stub will be used for creating the boilerplate
2359// by calling Runtime_CreateObjectLiteral.
2360// Each created boilerplate is stored in the JSFunction and they are
2361// therefore context dependent.
2362class ObjectLiteralDeferred: public DeferredCode {
2363 public:
2364 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
2365 : DeferredCode(generator), node_(node) {
2366 set_comment("[ ObjectLiteralDeferred");
2367 }
2368 virtual void Generate();
2369 private:
2370 ObjectLiteral* node_;
2371};
2372
2373
2374void ObjectLiteralDeferred::Generate() {
2375 // If the entry is undefined we call the runtime system to computed
2376 // the literal.
2377
2378 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00002379 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002381 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2382 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002384 __ mov(r0, Operand(node_->constant_properties()));
2385 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002387 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388}
2389
2390
2391void ArmCodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
2392 Comment cmnt(masm_, "[ ObjectLiteral");
2393
2394 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
2395
2396 // Retrieve the literal array and check the allocated entry.
2397
2398 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002399 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400
2401 // Load the literals array of the function.
2402 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2403
2404 // Load the literal at the ast saved index.
2405 int literal_offset =
2406 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2407 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2408
2409 // Check whether we need to materialize the object literal boilerplate.
2410 // If so, jump to the deferred code.
2411 __ cmp(r2, Operand(Factory::undefined_value()));
2412 __ b(eq, deferred->enter());
2413 __ bind(deferred->exit());
2414
2415 // Push the object literal boilerplate.
mads.s.ager31e71382008-08-13 09:32:07 +00002416 __ push(r2);
2417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 // Clone the boilerplate object.
2419 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002420 __ push(r0); // save the result
2421 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422
2423 for (int i = 0; i < node->properties()->length(); i++) {
2424 ObjectLiteral::Property* property = node->properties()->at(i);
2425 Literal* key = property->key();
2426 Expression* value = property->value();
2427 switch (property->kind()) {
2428 case ObjectLiteral::Property::CONSTANT: break;
2429 case ObjectLiteral::Property::COMPUTED: // fall through
2430 case ObjectLiteral::Property::PROTOTYPE: {
mads.s.ager31e71382008-08-13 09:32:07 +00002431 __ push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432 Load(key);
2433 Load(value);
2434 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002435 // restore r0
2436 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 break;
2438 }
2439 case ObjectLiteral::Property::SETTER: {
2440 __ push(r0);
2441 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002442 __ mov(r0, Operand(Smi::FromInt(1)));
2443 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 Load(value);
2445 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002446 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 break;
2448 }
2449 case ObjectLiteral::Property::GETTER: {
2450 __ push(r0);
2451 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002452 __ mov(r0, Operand(Smi::FromInt(0)));
2453 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454 Load(value);
2455 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002456 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457 break;
2458 }
2459 }
2460 }
2461}
2462
2463
2464void ArmCodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
2465 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002466
2467 // Call runtime to create the array literal.
2468 __ mov(r0, Operand(node->literals()));
2469 __ push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002470 // Load the function of this frame.
2471 __ ldr(r0, FunctionOperand());
2472 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002473 __ push(r0);
2474 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
2475
2476 // Push the resulting array literal on the stack.
2477 __ push(r0);
2478
2479 // Generate code to set the elements in the array that are not
2480 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 for (int i = 0; i < node->values()->length(); i++) {
2482 Expression* value = node->values()->at(i);
2483
2484 // If value is literal the property value is already
2485 // set in the boilerplate object.
2486 if (value->AsLiteral() == NULL) {
2487 // The property must be set by generated code.
2488 Load(value);
mads.s.ager31e71382008-08-13 09:32:07 +00002489 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490
2491 // Fetch the object literal
2492 __ ldr(r1, MemOperand(sp, 0));
2493 // Get the elements array.
2494 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
2495
2496 // Write to the indexed properties array.
2497 int offset = i * kPointerSize + Array::kHeaderSize;
2498 __ str(r0, FieldMemOperand(r1, offset));
2499
2500 // Update the write barrier for the array address.
2501 __ mov(r3, Operand(offset));
2502 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 }
2504 }
2505}
2506
2507
2508void ArmCodeGenerator::VisitAssignment(Assignment* node) {
2509 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 if (FLAG_debug_info) RecordStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 Reference target(this, node->target());
2513 if (target.is_illegal()) return;
2514
2515 if (node->op() == Token::ASSIGN ||
2516 node->op() == Token::INIT_VAR ||
2517 node->op() == Token::INIT_CONST) {
2518 Load(node->value());
2519
2520 } else {
2521 GetValue(&target);
2522 Literal* literal = node->value()->AsLiteral();
2523 if (literal != NULL && literal->handle()->IsSmi()) {
2524 SmiOperation(node->binary_op(), literal->handle(), false);
mads.s.ager31e71382008-08-13 09:32:07 +00002525 __ push(r0);
2526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 } else {
2528 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00002529 GenericBinaryOperation(node->binary_op());
mads.s.ager31e71382008-08-13 09:32:07 +00002530 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 }
2532 }
2533
2534 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2535 if (var != NULL &&
2536 (var->mode() == Variable::CONST) &&
2537 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2538 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 } else {
2541 __ RecordPosition(node->position());
2542 if (node->op() == Token::INIT_CONST) {
2543 // Dynamic constant initializations must use the function context
2544 // and initialize the actual constant declared. Dynamic variable
2545 // initializations are simply assignments and use SetValue.
2546 InitConst(&target);
2547 } else {
2548 SetValue(&target);
2549 }
2550 }
2551}
2552
2553
2554void ArmCodeGenerator::VisitThrow(Throw* node) {
2555 Comment cmnt(masm_, "[ Throw");
2556
2557 Load(node->exception());
2558 __ RecordPosition(node->position());
2559 __ CallRuntime(Runtime::kThrow, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002560 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561}
2562
2563
2564void ArmCodeGenerator::VisitProperty(Property* node) {
2565 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 if (is_referenced()) {
2568 __ RecordPosition(node->position());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002569 GetReferenceProperty(node->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 Reference property(this, node);
2572 __ RecordPosition(node->position());
2573 GetValue(&property);
2574 }
2575}
2576
2577
2578void ArmCodeGenerator::VisitCall(Call* node) {
2579 Comment cmnt(masm_, "[ Call");
2580
2581 ZoneList<Expression*>* args = node->arguments();
2582
2583 if (FLAG_debug_info) RecordStatementPosition(node);
2584 // Standard function call.
2585
2586 // Check if the function is a variable or a property.
2587 Expression* function = node->expression();
2588 Variable* var = function->AsVariableProxy()->AsVariable();
2589 Property* property = function->AsProperty();
2590
2591 // ------------------------------------------------------------------------
2592 // Fast-case: Use inline caching.
2593 // ---
2594 // According to ECMA-262, section 11.2.3, page 44, the function to call
2595 // must be resolved after the arguments have been evaluated. The IC code
2596 // automatically handles this by loading the arguments before the function
2597 // is resolved in cache misses (this also holds for megamorphic calls).
2598 // ------------------------------------------------------------------------
2599
2600 if (var != NULL && !var->is_this() && var->is_global()) {
2601 // ----------------------------------
2602 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2603 // ----------------------------------
2604
2605 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002606 __ mov(r0, Operand(var->name()));
2607 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 LoadGlobal();
2609
2610 // Load the arguments.
2611 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612
2613 // Setup the receiver register and call the IC initialization code.
2614 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002616 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002619 __ pop();
2620 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
2622 } else if (var != NULL && var->slot() != NULL &&
2623 var->slot()->type() == Slot::LOOKUP) {
2624 // ----------------------------------
2625 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2626 // ----------------------------------
2627
2628 // Load the function
mads.s.ager31e71382008-08-13 09:32:07 +00002629 __ push(cp);
2630 __ mov(r0, Operand(var->name()));
2631 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2633 // r0: slot value; r1: receiver
2634
2635 // Load the receiver.
mads.s.ager31e71382008-08-13 09:32:07 +00002636 __ push(r0); // function
2637 __ push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638
2639 // Call the function.
2640 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00002641 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642
2643 } else if (property != NULL) {
2644 // Check if the key is a literal string.
2645 Literal* literal = property->key()->AsLiteral();
2646
2647 if (literal != NULL && literal->handle()->IsSymbol()) {
2648 // ------------------------------------------------------------------
2649 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2650 // ------------------------------------------------------------------
2651
2652 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002653 __ mov(r0, Operand(literal->handle()));
2654 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 Load(property->obj());
2656
2657 // Load the arguments.
2658 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659
2660 // Set the receiver register and call the IC initialization code.
2661 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002663 __ Call(stub, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2665
2666 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002667 __ pop();
2668
2669 __ push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670
2671 } else {
2672 // -------------------------------------------
2673 // JavaScript example: 'array[index](1, 2, 3)'
2674 // -------------------------------------------
2675
2676 // Load the function to call from the property through a reference.
2677 Reference ref(this, property);
mads.s.ager31e71382008-08-13 09:32:07 +00002678 GetValue(&ref); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679
2680 // Pass receiver to called function.
mads.s.ager31e71382008-08-13 09:32:07 +00002681 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
2682 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683 // Call the function.
2684 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00002685 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686 }
2687
2688 } else {
2689 // ----------------------------------
2690 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
2691 // ----------------------------------
2692
2693 // Load the function.
2694 Load(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 // Pass the global object as the receiver.
2696 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 // Call the function.
2698 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00002699 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 }
2701}
2702
2703
2704void ArmCodeGenerator::VisitCallNew(CallNew* node) {
2705 Comment cmnt(masm_, "[ CallNew");
2706
2707 // According to ECMA-262, section 11.2.2, page 44, the function
2708 // expression in new calls must be evaluated before the
2709 // arguments. This is different from ordinary calls, where the
2710 // actual function to call is resolved after the arguments have been
2711 // evaluated.
2712
2713 // Compute function to call and use the global object as the
2714 // receiver.
2715 Load(node->expression());
2716 LoadGlobal();
2717
2718 // Push the arguments ("left-to-right") on the stack.
2719 ZoneList<Expression*>* args = node->arguments();
2720 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2721
mads.s.ager31e71382008-08-13 09:32:07 +00002722 // r0: the number of arguments.
2723 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002725 // Load the function into r1 as per calling convention.
2726 __ ldr(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
2727
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 // Call the construct call builtin that handles allocation and
2729 // constructor invocation.
ager@chromium.org236ad962008-09-25 09:45:57 +00002730 __ RecordPosition(RelocInfo::POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00002732 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00002733
2734 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
2735 __ str(r0, MemOperand(sp, 0 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002736}
2737
2738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739void ArmCodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
2740 ASSERT(args->length() == 1);
2741 Label leave;
2742 Load(args->at(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002743 __ pop(r0); // r0 contains object.
2744 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002745 __ tst(r0, Operand(kSmiTagMask));
2746 __ b(eq, &leave);
2747 // It is a heap object - get map.
2748 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2749 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002750 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 __ cmp(r1, Operand(JS_VALUE_TYPE));
2752 __ b(ne, &leave);
2753 // Load the value.
2754 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2755 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00002756 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757}
2758
2759
2760void ArmCodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
2761 ASSERT(args->length() == 2);
2762 Label leave;
2763 Load(args->at(0)); // Load the object.
2764 Load(args->at(1)); // Load the value.
mads.s.ager31e71382008-08-13 09:32:07 +00002765 __ pop(r0); // r0 contains value
2766 __ pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 // if (object->IsSmi()) return object.
2768 __ tst(r1, Operand(kSmiTagMask));
2769 __ b(eq, &leave);
2770 // It is a heap object - get map.
2771 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2772 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2773 // if (!object->IsJSValue()) return object.
2774 __ cmp(r2, Operand(JS_VALUE_TYPE));
2775 __ b(ne, &leave);
2776 // Store the value.
2777 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2778 // Update the write barrier.
2779 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2780 __ RecordWrite(r1, r2, r3);
2781 // Leave.
2782 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00002783 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784}
2785
2786
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787void ArmCodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
2788 ASSERT(args->length() == 1);
2789 Load(args->at(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 __ pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002791 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792 cc_reg_ = eq;
2793}
2794
2795
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002796void ArmCodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
2797 ASSERT(args->length() == 1);
2798 Load(args->at(0));
2799 __ pop(r0);
2800 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2801 cc_reg_ = eq;
2802}
2803
2804
kasper.lund7276f142008-07-30 08:49:36 +00002805// This should generate code that performs a charCodeAt() call or returns
2806// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2807// It is not yet implemented on ARM, so it always goes to the slow case.
2808void ArmCodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
2809 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002810 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +00002811 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002812}
2813
2814
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815void ArmCodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002816 ASSERT(args->length() == 1);
2817 Load(args->at(0));
2818 Label answer;
2819 // We need the CC bits to come out as not_equal in the case where the
2820 // object is a smi. This can't be done with the usual test opcode so
2821 // we use XOR to get the right CC bits.
2822 __ pop(r0);
2823 __ and_(r1, r0, Operand(kSmiTagMask));
2824 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2825 __ b(ne, &answer);
2826 // It is a heap object - get the map.
2827 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2828 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2829 // Check if the object is a JS array or not.
2830 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2831 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 cc_reg_ = eq;
2833}
2834
2835
2836void ArmCodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
2837 ASSERT(args->length() == 0);
2838
mads.s.ager31e71382008-08-13 09:32:07 +00002839 // Seed the result with the formal parameters count, which will be used
2840 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002841 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2842
2843 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002844 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00002846 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847}
2848
2849
2850void ArmCodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
2851 ASSERT(args->length() == 1);
2852
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002853 // Satisfy contract with ArgumentsAccessStub:
2854 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855 Load(args->at(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002856 __ pop(r1);
2857 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858
2859 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002860 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00002862 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002863}
2864
2865
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002866void ArmCodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
2867 ASSERT(args->length() == 2);
2868
2869 // Load the two objects into registers and perform the comparison.
2870 Load(args->at(0));
2871 Load(args->at(1));
2872 __ pop(r0);
2873 __ pop(r1);
2874 __ cmp(r0, Operand(r1));
2875 cc_reg_ = eq;
2876}
2877
2878
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879void ArmCodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002880 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881
2882 ZoneList<Expression*>* args = node->arguments();
2883 Comment cmnt(masm_, "[ CallRuntime");
2884 Runtime::Function* function = node->function();
2885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002887 // Push the arguments ("left-to-right").
2888 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890 // Call the C runtime function.
2891 __ CallRuntime(function, args->length());
mads.s.ager31e71382008-08-13 09:32:07 +00002892 __ push(r0);
2893
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002895 // Prepare stack for calling JS runtime function.
2896 __ mov(r0, Operand(node->name()));
2897 __ push(r0);
2898 // Push the builtins object found in the current global object.
2899 __ ldr(r1, GlobalObject());
2900 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
2901 __ push(r0);
2902
2903 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002907 __ Call(stub, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002909 __ pop();
2910 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 }
2912}
2913
2914
2915void ArmCodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
2916 Comment cmnt(masm_, "[ UnaryOperation");
2917
2918 Token::Value op = node->op();
2919
2920 if (op == Token::NOT) {
2921 LoadCondition(node->expression(),
2922 CodeGenState::LOAD,
2923 false_target(),
2924 true_target(),
2925 true);
2926 cc_reg_ = NegateCondition(cc_reg_);
2927
2928 } else if (op == Token::DELETE) {
2929 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002930 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931 if (property != NULL) {
2932 Load(property->obj());
2933 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002934 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002935 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936
mads.s.ager31e71382008-08-13 09:32:07 +00002937 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 Slot* slot = variable->slot();
2939 if (variable->is_global()) {
2940 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00002941 __ mov(r0, Operand(variable->name()));
2942 __ push(r0);
2943 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002944 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002945
2946 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2947 // lookup the context holding the named variable
mads.s.ager31e71382008-08-13 09:32:07 +00002948 __ push(cp);
2949 __ mov(r0, Operand(variable->name()));
2950 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 __ CallRuntime(Runtime::kLookupContext, 2);
2952 // r0: context
mads.s.ager31e71382008-08-13 09:32:07 +00002953 __ push(r0);
2954 __ mov(r0, Operand(variable->name()));
2955 __ push(r0);
2956 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002957 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958
mads.s.ager31e71382008-08-13 09:32:07 +00002959 } else {
2960 // Default: Result of deleting non-global, not dynamically
2961 // introduced variables is false.
2962 __ mov(r0, Operand(Factory::false_value()));
2963 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964
2965 } else {
2966 // Default: Result of deleting expressions is true.
2967 Load(node->expression()); // may have side-effects
mads.s.ager31e71382008-08-13 09:32:07 +00002968 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969 __ mov(r0, Operand(Factory::true_value()));
2970 }
mads.s.ager31e71382008-08-13 09:32:07 +00002971 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002972
2973 } else if (op == Token::TYPEOF) {
2974 // Special case for loading the typeof expression; see comment on
2975 // LoadTypeofExpression().
2976 LoadTypeofExpression(node->expression());
2977 __ CallRuntime(Runtime::kTypeof, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002978 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979
2980 } else {
2981 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00002982 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983 switch (op) {
2984 case Token::NOT:
2985 case Token::DELETE:
2986 case Token::TYPEOF:
2987 UNREACHABLE(); // handled above
2988 break;
2989
2990 case Token::SUB: {
2991 UnarySubStub stub;
2992 __ CallStub(&stub);
2993 break;
2994 }
2995
2996 case Token::BIT_NOT: {
2997 // smi check
2998 Label smi_label;
2999 Label continue_label;
3000 __ tst(r0, Operand(kSmiTagMask));
3001 __ b(eq, &smi_label);
3002
mads.s.ager31e71382008-08-13 09:32:07 +00003003 __ push(r0);
3004 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003005 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006
3007 __ b(&continue_label);
3008 __ bind(&smi_label);
3009 __ mvn(r0, Operand(r0));
3010 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
3011 __ bind(&continue_label);
3012 break;
3013 }
3014
3015 case Token::VOID:
3016 // since the stack top is cached in r0, popping and then
3017 // pushing a value can be done by just writing to r0.
3018 __ mov(r0, Operand(Factory::undefined_value()));
3019 break;
3020
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003021 case Token::ADD: {
3022 // Smi check.
3023 Label continue_label;
3024 __ tst(r0, Operand(kSmiTagMask));
3025 __ b(eq, &continue_label);
mads.s.ager31e71382008-08-13 09:32:07 +00003026 __ push(r0);
3027 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003028 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003029 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003031 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003032 default:
3033 UNREACHABLE();
3034 }
mads.s.ager31e71382008-08-13 09:32:07 +00003035 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036 }
3037}
3038
3039
3040void ArmCodeGenerator::VisitCountOperation(CountOperation* node) {
3041 Comment cmnt(masm_, "[ CountOperation");
3042
3043 bool is_postfix = node->is_postfix();
3044 bool is_increment = node->op() == Token::INC;
3045
3046 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3047 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3048
3049 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003050 if (is_postfix) {
3051 __ mov(r0, Operand(0));
3052 __ push(r0);
3053 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054
3055 { Reference target(this, node->expression());
3056 if (target.is_illegal()) return;
3057 GetValue(&target);
mads.s.ager31e71382008-08-13 09:32:07 +00003058 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059
3060 Label slow, exit;
3061
3062 // Load the value (1) into register r1.
3063 __ mov(r1, Operand(Smi::FromInt(1)));
3064
3065 // Check for smi operand.
3066 __ tst(r0, Operand(kSmiTagMask));
3067 __ b(ne, &slow);
3068
3069 // Postfix: Store the old value as the result.
3070 if (is_postfix) __ str(r0, MemOperand(sp, target.size() * kPointerSize));
3071
3072 // Perform optimistic increment/decrement.
3073 if (is_increment) {
3074 __ add(r0, r0, Operand(r1), SetCC);
3075 } else {
3076 __ sub(r0, r0, Operand(r1), SetCC);
3077 }
3078
3079 // If the increment/decrement didn't overflow, we're done.
3080 __ b(vc, &exit);
3081
3082 // Revert optimistic increment/decrement.
3083 if (is_increment) {
3084 __ sub(r0, r0, Operand(r1));
3085 } else {
3086 __ add(r0, r0, Operand(r1));
3087 }
3088
3089 // Slow case: Convert to number.
3090 __ bind(&slow);
3091
3092 // Postfix: Convert the operand to a number and store it as the result.
3093 if (is_postfix) {
3094 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
3095 __ CallStub(&stub);
3096 // Store to result (on the stack).
3097 __ str(r0, MemOperand(sp, target.size() * kPointerSize));
3098 }
3099
3100 // Compute the new value by calling the right JavaScript native.
3101 if (is_increment) {
3102 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
3103 __ CallStub(&stub);
3104 } else {
3105 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
3106 __ CallStub(&stub);
3107 }
3108
3109 // Store the new value in the target if not const.
3110 __ bind(&exit);
mads.s.ager31e71382008-08-13 09:32:07 +00003111 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112 if (!is_const) SetValue(&target);
3113 }
3114
3115 // Postfix: Discard the new value and use the old.
3116 if (is_postfix) __ pop(r0);
3117}
3118
3119
3120void ArmCodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
3121 Comment cmnt(masm_, "[ BinaryOperation");
3122 Token::Value op = node->op();
3123
3124 // According to ECMA-262 section 11.11, page 58, the binary logical
3125 // operators must yield the result of one of the two expressions
3126 // before any ToBoolean() conversions. This means that the value
3127 // produced by a && or || operator is not necessarily a boolean.
3128
3129 // NOTE: If the left hand side produces a materialized value (not in
3130 // the CC register), we force the right hand side to do the
3131 // same. This is necessary because we may have to branch to the exit
3132 // after evaluating the left hand side (due to the shortcut
3133 // semantics), but the compiler must (statically) know if the result
3134 // of compiling the binary operation is materialized or not.
3135
3136 if (op == Token::AND) {
3137 Label is_true;
3138 LoadCondition(node->left(),
3139 CodeGenState::LOAD,
3140 &is_true,
3141 false_target(),
3142 false);
3143 if (has_cc()) {
3144 Branch(false, false_target());
3145
3146 // Evaluate right side expression.
3147 __ bind(&is_true);
3148 LoadCondition(node->right(),
3149 CodeGenState::LOAD,
3150 true_target(),
3151 false_target(),
3152 false);
3153
3154 } else {
3155 Label pop_and_continue, exit;
3156
mads.s.ager31e71382008-08-13 09:32:07 +00003157 __ ldr(r0, MemOperand(sp, 0)); // dup the stack top
3158 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159 // Avoid popping the result if it converts to 'false' using the
3160 // standard ToBoolean() conversion as described in ECMA-262,
3161 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003162 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 Branch(false, &exit);
3164
3165 // Pop the result of evaluating the first part.
3166 __ bind(&pop_and_continue);
3167 __ pop(r0);
3168
3169 // Evaluate right side expression.
3170 __ bind(&is_true);
3171 Load(node->right());
3172
3173 // Exit (always with a materialized value).
3174 __ bind(&exit);
3175 }
3176
3177 } else if (op == Token::OR) {
3178 Label is_false;
3179 LoadCondition(node->left(),
3180 CodeGenState::LOAD,
3181 true_target(),
3182 &is_false,
3183 false);
3184 if (has_cc()) {
3185 Branch(true, true_target());
3186
3187 // Evaluate right side expression.
3188 __ bind(&is_false);
3189 LoadCondition(node->right(),
3190 CodeGenState::LOAD,
3191 true_target(),
3192 false_target(),
3193 false);
3194
3195 } else {
3196 Label pop_and_continue, exit;
3197
mads.s.ager31e71382008-08-13 09:32:07 +00003198 __ ldr(r0, MemOperand(sp, 0));
3199 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003200 // Avoid popping the result if it converts to 'true' using the
3201 // standard ToBoolean() conversion as described in ECMA-262,
3202 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003203 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003204 Branch(true, &exit);
3205
3206 // Pop the result of evaluating the first part.
3207 __ bind(&pop_and_continue);
3208 __ pop(r0);
3209
3210 // Evaluate right side expression.
3211 __ bind(&is_false);
3212 Load(node->right());
3213
3214 // Exit (always with a materialized value).
3215 __ bind(&exit);
3216 }
3217
3218 } else {
3219 // Optimize for the case where (at least) one of the expressions
3220 // is a literal small integer.
3221 Literal* lliteral = node->left()->AsLiteral();
3222 Literal* rliteral = node->right()->AsLiteral();
3223
3224 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
3225 Load(node->left());
3226 SmiOperation(node->op(), rliteral->handle(), false);
3227
3228 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
3229 Load(node->right());
3230 SmiOperation(node->op(), lliteral->handle(), true);
3231
3232 } else {
3233 Load(node->left());
3234 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00003235 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236 }
mads.s.ager31e71382008-08-13 09:32:07 +00003237 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 }
3239}
3240
3241
3242void ArmCodeGenerator::VisitThisFunction(ThisFunction* node) {
mads.s.ager31e71382008-08-13 09:32:07 +00003243 __ ldr(r0, FunctionOperand());
3244 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003245}
3246
3247
3248void ArmCodeGenerator::VisitCompareOperation(CompareOperation* node) {
3249 Comment cmnt(masm_, "[ CompareOperation");
3250
3251 // Get the expressions from the node.
3252 Expression* left = node->left();
3253 Expression* right = node->right();
3254 Token::Value op = node->op();
3255
3256 // NOTE: To make null checks efficient, we check if either left or
3257 // right is the literal 'null'. If so, we optimize the code by
3258 // inlining a null check instead of calling the (very) general
3259 // runtime routine for checking equality.
3260
3261 bool left_is_null =
3262 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
3263 bool right_is_null =
3264 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3265
3266 if (op == Token::EQ || op == Token::EQ_STRICT) {
3267 // The 'null' value is only equal to 'null' or 'undefined'.
3268 if (left_is_null || right_is_null) {
3269 Load(left_is_null ? right : left);
3270 Label exit, undetectable;
mads.s.ager31e71382008-08-13 09:32:07 +00003271 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272 __ cmp(r0, Operand(Factory::null_value()));
3273
3274 // The 'null' value is only equal to 'undefined' if using
3275 // non-strict comparisons.
3276 if (op != Token::EQ_STRICT) {
3277 __ b(eq, &exit);
3278 __ cmp(r0, Operand(Factory::undefined_value()));
3279
3280 // NOTE: it can be undetectable object.
3281 __ b(eq, &exit);
3282 __ tst(r0, Operand(kSmiTagMask));
3283
3284 __ b(ne, &undetectable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285 __ b(false_target());
3286
3287 __ bind(&undetectable);
3288 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3289 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3290 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3291 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3292 }
3293
3294 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003295
3296 cc_reg_ = eq;
3297 return;
3298 }
3299 }
3300
3301
3302 // NOTE: To make typeof testing for natives implemented in
3303 // JavaScript really efficient, we generate special code for
3304 // expressions of the form: 'typeof <expression> == <string>'.
3305
3306 UnaryOperation* operation = left->AsUnaryOperation();
3307 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3308 (operation != NULL && operation->op() == Token::TYPEOF) &&
3309 (right->AsLiteral() != NULL &&
3310 right->AsLiteral()->handle()->IsString())) {
3311 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3312
mads.s.ager31e71382008-08-13 09:32:07 +00003313 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314 LoadTypeofExpression(operation->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00003315 __ pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003316
3317 if (check->Equals(Heap::number_symbol())) {
3318 __ tst(r1, Operand(kSmiTagMask));
3319 __ b(eq, true_target());
3320 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3321 __ cmp(r1, Operand(Factory::heap_number_map()));
3322 cc_reg_ = eq;
3323
3324 } else if (check->Equals(Heap::string_symbol())) {
3325 __ tst(r1, Operand(kSmiTagMask));
3326 __ b(eq, false_target());
3327
3328 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3329
3330 // NOTE: it might be an undetectable string object
3331 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3332 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3333 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3334 __ b(eq, false_target());
3335
3336 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3337 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3338 cc_reg_ = lt;
3339
3340 } else if (check->Equals(Heap::boolean_symbol())) {
3341 __ cmp(r1, Operand(Factory::true_value()));
3342 __ b(eq, true_target());
3343 __ cmp(r1, Operand(Factory::false_value()));
3344 cc_reg_ = eq;
3345
3346 } else if (check->Equals(Heap::undefined_symbol())) {
3347 __ cmp(r1, Operand(Factory::undefined_value()));
3348 __ b(eq, true_target());
3349
3350 __ tst(r1, Operand(kSmiTagMask));
3351 __ b(eq, false_target());
3352
3353 // NOTE: it can be undetectable object.
3354 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3355 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3356 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3357 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3358
3359 cc_reg_ = eq;
3360
3361 } else if (check->Equals(Heap::function_symbol())) {
3362 __ tst(r1, Operand(kSmiTagMask));
3363 __ b(eq, false_target());
3364 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3365 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3366 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3367 cc_reg_ = eq;
3368
3369 } else if (check->Equals(Heap::object_symbol())) {
3370 __ tst(r1, Operand(kSmiTagMask));
3371 __ b(eq, false_target());
3372
3373 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3374 __ cmp(r1, Operand(Factory::null_value()));
3375 __ b(eq, true_target());
3376
3377 // NOTE: it might be an undetectable object.
3378 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3379 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3380 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3381 __ b(eq, false_target());
3382
3383 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3384 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3385 __ b(lt, false_target());
3386 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3387 cc_reg_ = le;
3388
3389 } else {
3390 // Uncommon case: Typeof testing against a string literal that
3391 // is never returned from the typeof operator.
3392 __ b(false_target());
3393 }
3394 return;
3395 }
3396
3397 Load(left);
3398 Load(right);
3399 switch (op) {
3400 case Token::EQ:
3401 Comparison(eq, false);
3402 break;
3403
3404 case Token::LT:
3405 Comparison(lt);
3406 break;
3407
3408 case Token::GT:
3409 Comparison(gt);
3410 break;
3411
3412 case Token::LTE:
3413 Comparison(le);
3414 break;
3415
3416 case Token::GTE:
3417 Comparison(ge);
3418 break;
3419
3420 case Token::EQ_STRICT:
3421 Comparison(eq, true);
3422 break;
3423
3424 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003425 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003426 __ InvokeBuiltin(Builtins::IN, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00003427 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428 break;
3429
3430 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003431 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003432 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00003433 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003434 break;
3435
3436 default:
3437 UNREACHABLE();
3438 }
3439}
3440
3441
3442void ArmCodeGenerator::RecordStatementPosition(Node* node) {
3443 if (FLAG_debug_info) {
3444 int statement_pos = node->statement_pos();
ager@chromium.org236ad962008-09-25 09:45:57 +00003445 if (statement_pos == RelocInfo::kNoPosition) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003446 __ RecordStatementPosition(statement_pos);
3447 }
3448}
3449
3450
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003451void ArmCodeGenerator::EnterJSFrame() {
3452#if defined(DEBUG)
3453 { Label done, fail;
3454 __ tst(r1, Operand(kSmiTagMask));
3455 __ b(eq, &fail);
3456 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3457 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3458 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
3459 __ b(eq, &done);
3460 __ bind(&fail);
3461 __ stop("ArmCodeGenerator::EnterJSFrame - r1 not a function");
3462 __ bind(&done);
3463 }
3464#endif // DEBUG
3465
3466 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
3467 __ add(fp, sp, Operand(2 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003468}
3469
3470
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003471void ArmCodeGenerator::ExitJSFrame() {
3472 // Drop the execution stack down to the frame pointer and restore the caller
3473 // frame pointer and return address.
3474 __ mov(sp, fp);
3475 __ ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003476}
3477
3478
3479#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003480#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003482MemOperand ArmCodeGenerator::SlotOperand(CodeGenerator* cgen,
3483 Slot* slot,
3484 Register tmp) {
3485 // Currently, this assertion will fail if we try to assign to
3486 // a constant variable that is constant because it is read-only
3487 // (such as the variable referring to a named function expression).
3488 // We need to implement assignments to read-only variables.
3489 // Ideally, we should do this during AST generation (by converting
3490 // such assignments into expression statements); however, in general
3491 // we may not be able to make the decision until past AST generation,
3492 // that is when the entire program is known.
3493 ASSERT(slot != NULL);
3494 int index = slot->index();
3495 switch (slot->type()) {
3496 case Slot::PARAMETER:
3497 return ParameterOperand(cgen, index);
3498
3499 case Slot::LOCAL: {
3500 ASSERT(0 <= index &&
3501 index < cgen->scope()->num_stack_slots() &&
3502 index >= 0);
3503 int local_offset = JavaScriptFrameConstants::kLocal0Offset -
3504 index * kPointerSize;
3505 return MemOperand(fp, local_offset);
3506 }
3507
3508 case Slot::CONTEXT: {
3509 MacroAssembler* masm = cgen->masm();
3510 // Follow the context chain if necessary.
3511 ASSERT(!tmp.is(cp)); // do not overwrite context register
3512 Register context = cp;
3513 int chain_length =
3514 cgen->scope()->ContextChainLength(slot->var()->scope());
3515 for (int i = chain_length; i-- > 0;) {
3516 // Load the closure.
3517 // (All contexts, even 'with' contexts, have a closure,
3518 // and it is the same for all contexts inside a function.
3519 // There is no need to go to the function context first.)
3520 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3521 // Load the function context (which is the incoming, outer context).
3522 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3523 context = tmp;
3524 }
3525 // We may have a 'with' context now. Get the function context.
3526 // (In fact this mov may never be the needed, since the scope analysis
3527 // may not permit a direct context access in this case and thus we are
3528 // always at a function context. However it is safe to dereference be-
3529 // cause the function context of a function context is itself. Before
3530 // deleting this mov we should try to create a counter-example first,
3531 // though...)
3532 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
3533 return ContextOperand(tmp, index);
3534 }
3535
3536 default:
3537 UNREACHABLE();
3538 return MemOperand(r0, 0);
3539 }
3540}
3541
3542
3543void Property::GenerateStoreCode(CodeGenerator* cgen,
3544 Reference* ref,
3545 InitState init_state) {
3546 MacroAssembler* masm = cgen->masm();
3547 Comment cmnt(masm, "[ Store to Property");
3548 __ RecordPosition(position());
3549 ArmCodeGenerator::SetReferenceProperty(cgen, ref, key());
3550}
3551
3552
3553void VariableProxy::GenerateStoreCode(CodeGenerator* cgen,
3554 Reference* ref,
3555 InitState init_state) {
3556 MacroAssembler* masm = cgen->masm();
3557 Comment cmnt(masm, "[ Store to VariableProxy");
3558 Variable* node = var();
3559
3560 Expression* expr = node->rewrite();
3561 if (expr != NULL) {
3562 expr->GenerateStoreCode(cgen, ref, init_state);
3563 } else {
3564 ASSERT(node->is_global());
3565 if (node->AsProperty() != NULL) {
3566 __ RecordPosition(node->AsProperty()->position());
3567 }
3568 Expression* key = new Literal(node->name());
3569 ArmCodeGenerator::SetReferenceProperty(cgen, ref, key);
3570 }
3571}
3572
3573
3574void Slot::GenerateStoreCode(CodeGenerator* cgen,
3575 Reference* ref,
3576 InitState init_state) {
3577 MacroAssembler* masm = cgen->masm();
3578 Comment cmnt(masm, "[ Store to Slot");
3579
3580 if (type() == Slot::LOOKUP) {
3581 ASSERT(var()->mode() == Variable::DYNAMIC);
3582
3583 // For now, just do a runtime call.
3584 __ push(cp);
3585 __ mov(r0, Operand(var()->name()));
3586 __ push(r0);
3587
3588 if (init_state == CONST_INIT) {
3589 // Same as the case for a normal store, but ignores attribute
3590 // (e.g. READ_ONLY) of context slot so that we can initialize const
3591 // properties (introduced via eval("const foo = (some expr);")). Also,
3592 // uses the current function context instead of the top context.
3593 //
3594 // Note that we must declare the foo upon entry of eval(), via a
3595 // context slot declaration, but we cannot initialize it at the same
3596 // time, because the const declaration may be at the end of the eval
3597 // code (sigh...) and the const variable may have been used before
3598 // (where its value is 'undefined'). Thus, we can only do the
3599 // initialization when we actually encounter the expression and when
3600 // the expression operands are defined and valid, and thus we need the
3601 // split into 2 operations: declaration of the context slot followed
3602 // by initialization.
3603 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3604 } else {
3605 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3606 }
3607 // Storing a variable must keep the (new) value on the expression
3608 // stack. This is necessary for compiling assignment expressions.
3609 __ push(r0);
3610
3611 } else {
3612 ASSERT(var()->mode() != Variable::DYNAMIC);
3613
3614 Label exit;
3615 if (init_state == CONST_INIT) {
3616 ASSERT(var()->mode() == Variable::CONST);
3617 // Only the first const initialization must be executed (the slot
3618 // still contains 'the hole' value). When the assignment is executed,
3619 // the code is identical to a normal store (see below).
3620 Comment cmnt(masm, "[ Init const");
3621 __ ldr(r2, ArmCodeGenerator::SlotOperand(cgen, this, r2));
3622 __ cmp(r2, Operand(Factory::the_hole_value()));
3623 __ b(ne, &exit);
3624 }
3625
3626 // We must execute the store.
3627 // r2 may be loaded with context; used below in RecordWrite.
3628 // Storing a variable must keep the (new) value on the stack. This is
3629 // necessary for compiling assignment expressions.
3630 //
3631 // Note: We will reach here even with var()->mode() == Variable::CONST
3632 // because of const declarations which will initialize consts to 'the
3633 // hole' value and by doing so, end up calling this code. r2 may be
3634 // loaded with context; used below in RecordWrite.
3635 __ pop(r0);
3636 __ str(r0, ArmCodeGenerator::SlotOperand(cgen, this, r2));
3637 __ push(r0);
3638
3639 if (type() == Slot::CONTEXT) {
3640 // Skip write barrier if the written value is a smi.
3641 __ tst(r0, Operand(kSmiTagMask));
3642 __ b(eq, &exit);
3643 // r2 is loaded with context when calling SlotOperand above.
3644 int offset = FixedArray::kHeaderSize + index() * kPointerSize;
3645 __ mov(r3, Operand(offset));
3646 __ RecordWrite(r2, r3, r1);
3647 }
3648 // If we definitely did not jump over the assignment, we do not need to
3649 // bind the exit label. Doing so can defeat peephole optimization.
3650 if (init_state == CONST_INIT || type() == Slot::CONTEXT) {
3651 __ bind(&exit);
3652 }
3653 }
3654}
3655
3656
3657void GetPropertyStub::Generate(MacroAssembler* masm) {
3658 // sp[0]: key
3659 // sp[1]: receiver
3660 Label slow, fast;
3661 // Get the key and receiver object from the stack.
3662 __ ldm(ia, sp, r0.bit() | r1.bit());
3663 // Check that the key is a smi.
3664 __ tst(r0, Operand(kSmiTagMask));
3665 __ b(ne, &slow);
3666 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3667 // Check that the object isn't a smi.
3668 __ tst(r1, Operand(kSmiTagMask));
3669 __ b(eq, &slow);
3670
3671 // Check that the object is some kind of JS object EXCEPT JS Value type.
3672 // In the case that the object is a value-wrapper object,
3673 // we enter the runtime system to make sure that indexing into string
3674 // objects work as intended.
3675 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3676 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3677 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3678 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3679 __ b(lt, &slow);
3680
3681 // Get the elements array of the object.
3682 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3683 // Check that the object is in fast mode (not dictionary).
3684 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3685 __ cmp(r3, Operand(Factory::hash_table_map()));
3686 __ b(eq, &slow);
3687 // Check that the key (index) is within bounds.
3688 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3689 __ cmp(r0, Operand(r3));
3690 __ b(lo, &fast);
3691
3692 // Slow case: Push extra copies of the arguments (2).
3693 __ bind(&slow);
3694 __ ldm(ia, sp, r0.bit() | r1.bit());
3695 __ stm(db_w, sp, r0.bit() | r1.bit());
3696 // Do tail-call to runtime routine.
3697 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3698
3699 // Fast case: Do the load.
3700 __ bind(&fast);
3701 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3702 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3703 __ cmp(r0, Operand(Factory::the_hole_value()));
3704 // In case the loaded value is the_hole we have to consult GetProperty
3705 // to ensure the prototype chain is searched.
3706 __ b(eq, &slow);
3707
3708 __ StubReturn(1);
3709}
3710
3711
3712void SetPropertyStub::Generate(MacroAssembler* masm) {
3713 // r0 : value
3714 // sp[0] : key
3715 // sp[1] : receiver
3716
3717 Label slow, fast, array, extra, exit;
3718 // Get the key and the object from the stack.
3719 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3720 // Check that the key is a smi.
3721 __ tst(r1, Operand(kSmiTagMask));
3722 __ b(ne, &slow);
3723 // Check that the object isn't a smi.
3724 __ tst(r3, Operand(kSmiTagMask));
3725 __ b(eq, &slow);
3726 // Get the type of the object from its map.
3727 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3728 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3729 // Check if the object is a JS array or not.
3730 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3731 __ b(eq, &array);
3732 // Check that the object is some kind of JS object.
3733 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3734 __ b(lt, &slow);
3735
3736
3737 // Object case: Check key against length in the elements array.
3738 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3739 // Check that the object is in fast mode (not dictionary).
3740 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3741 __ cmp(r2, Operand(Factory::hash_table_map()));
3742 __ b(eq, &slow);
3743 // Untag the key (for checking against untagged length in the fixed array).
3744 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3745 // Compute address to store into and check array bounds.
3746 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3747 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3748 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3749 __ cmp(r1, Operand(ip));
3750 __ b(lo, &fast);
3751
3752
3753 // Slow case: Push extra copies of the arguments (3).
3754 __ bind(&slow);
3755 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3756 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3757 // Do tail-call to runtime routine.
3758 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3759
3760
3761 // Extra capacity case: Check if there is extra capacity to
3762 // perform the store and update the length. Used for adding one
3763 // element to the array by writing to array[array.length].
3764 // r0 == value, r1 == key, r2 == elements, r3 == object
3765 __ bind(&extra);
3766 __ b(ne, &slow); // do not leave holes in the array
3767 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3768 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3769 __ cmp(r1, Operand(ip));
3770 __ b(hs, &slow);
3771 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3772 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3773 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3774 __ mov(r3, Operand(r2));
3775 // NOTE: Computing the address to store into must take the fact
3776 // that the key has been incremented into account.
3777 int displacement = Array::kHeaderSize - kHeapObjectTag -
3778 ((1 << kSmiTagSize) * 2);
3779 __ add(r2, r2, Operand(displacement));
3780 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3781 __ b(&fast);
3782
3783
3784 // Array case: Get the length and the elements array from the JS
3785 // array. Check that the array is in fast mode; if it is the
3786 // length is always a smi.
3787 // r0 == value, r3 == object
3788 __ bind(&array);
3789 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3790 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3791 __ cmp(r1, Operand(Factory::hash_table_map()));
3792 __ b(eq, &slow);
3793
3794 // Check the key against the length in the array, compute the
3795 // address to store into and fall through to fast case.
3796 __ ldr(r1, MemOperand(sp));
3797 // r0 == value, r1 == key, r2 == elements, r3 == object.
3798 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3799 __ cmp(r1, Operand(ip));
3800 __ b(hs, &extra);
3801 __ mov(r3, Operand(r2));
3802 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3803 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3804
3805
3806 // Fast case: Do the store.
3807 // r0 == value, r2 == address to store into, r3 == elements
3808 __ bind(&fast);
3809 __ str(r0, MemOperand(r2));
3810 // Skip write barrier if the written value is a smi.
3811 __ tst(r0, Operand(kSmiTagMask));
3812 __ b(eq, &exit);
3813 // Update write barrier for the elements array address.
3814 __ sub(r1, r2, Operand(r3));
3815 __ RecordWrite(r3, r1, r2);
3816 __ bind(&exit);
3817 __ StubReturn(1);
3818}
3819
3820
3821void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3822 // r1 : x
3823 // r0 : y
3824 // result : r0
3825
3826 switch (op_) {
3827 case Token::ADD: {
3828 Label slow, exit;
3829 // fast path
3830 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3831 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3832 // go slow-path in case of overflow
3833 __ b(vs, &slow);
3834 // go slow-path in case of non-smi operands
3835 ASSERT(kSmiTag == 0); // adjust code below
3836 __ tst(r2, Operand(kSmiTagMask));
3837 __ b(eq, &exit);
3838 // slow path
3839 __ bind(&slow);
3840 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3841 __ push(r1);
3842 __ push(r0);
3843 __ mov(r0, Operand(1)); // set number of arguments
3844 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3845 // done
3846 __ bind(&exit);
3847 break;
3848 }
3849
3850 case Token::SUB: {
3851 Label slow, exit;
3852 // fast path
3853 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3854 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3855 // go slow-path in case of overflow
3856 __ b(vs, &slow);
3857 // go slow-path in case of non-smi operands
3858 ASSERT(kSmiTag == 0); // adjust code below
3859 __ tst(r2, Operand(kSmiTagMask));
3860 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3861 __ b(eq, &exit);
3862 // slow path
3863 __ bind(&slow);
3864 __ push(r1);
3865 __ push(r0);
3866 __ mov(r0, Operand(1)); // set number of arguments
3867 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3868 // done
3869 __ bind(&exit);
3870 break;
3871 }
3872
3873 case Token::MUL: {
3874 Label slow, exit;
3875 // tag check
3876 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3877 ASSERT(kSmiTag == 0); // adjust code below
3878 __ tst(r2, Operand(kSmiTagMask));
3879 __ b(ne, &slow);
3880 // remove tag from one operand (but keep sign), so that result is smi
3881 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3882 // do multiplication
3883 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3884 // go slow on overflows (overflow bit is not set)
3885 __ mov(ip, Operand(r3, ASR, 31));
3886 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3887 __ b(ne, &slow);
3888 // go slow on zero result to handle -0
3889 __ tst(r3, Operand(r3));
3890 __ mov(r0, Operand(r3), LeaveCC, ne);
3891 __ b(ne, &exit);
3892 // slow case
3893 __ bind(&slow);
3894 __ push(r1);
3895 __ push(r0);
3896 __ mov(r0, Operand(1)); // set number of arguments
3897 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3898 // done
3899 __ bind(&exit);
3900 break;
3901 }
3902
3903 case Token::BIT_OR:
3904 case Token::BIT_AND:
3905 case Token::BIT_XOR: {
3906 Label slow, exit;
3907 // tag check
3908 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3909 ASSERT(kSmiTag == 0); // adjust code below
3910 __ tst(r2, Operand(kSmiTagMask));
3911 __ b(ne, &slow);
3912 switch (op_) {
3913 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3914 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3915 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3916 default: UNREACHABLE();
3917 }
3918 __ b(&exit);
3919 __ bind(&slow);
3920 __ push(r1); // restore stack
3921 __ push(r0);
3922 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3923 switch (op_) {
3924 case Token::BIT_OR:
3925 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3926 break;
3927 case Token::BIT_AND:
3928 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3929 break;
3930 case Token::BIT_XOR:
3931 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3932 break;
3933 default:
3934 UNREACHABLE();
3935 }
3936 __ bind(&exit);
3937 break;
3938 }
3939
3940 case Token::SHL:
3941 case Token::SHR:
3942 case Token::SAR: {
3943 Label slow, exit;
3944 // tag check
3945 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3946 ASSERT(kSmiTag == 0); // adjust code below
3947 __ tst(r2, Operand(kSmiTagMask));
3948 __ b(ne, &slow);
3949 // remove tags from operands (but keep sign)
3950 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3951 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3952 // use only the 5 least significant bits of the shift count
3953 __ and_(r2, r2, Operand(0x1f));
3954 // perform operation
3955 switch (op_) {
3956 case Token::SAR:
3957 __ mov(r3, Operand(r3, ASR, r2));
3958 // no checks of result necessary
3959 break;
3960
3961 case Token::SHR:
3962 __ mov(r3, Operand(r3, LSR, r2));
3963 // check that the *unsigned* result fits in a smi
3964 // neither of the two high-order bits can be set:
3965 // - 0x80000000: high bit would be lost when smi tagging
3966 // - 0x40000000: this number would convert to negative when
3967 // smi tagging these two cases can only happen with shifts
3968 // by 0 or 1 when handed a valid smi
3969 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3970 __ b(ne, &slow);
3971 break;
3972
3973 case Token::SHL:
3974 __ mov(r3, Operand(r3, LSL, r2));
3975 // check that the *signed* result fits in a smi
3976 __ add(r2, r3, Operand(0x40000000), SetCC);
3977 __ b(mi, &slow);
3978 break;
3979
3980 default: UNREACHABLE();
3981 }
3982 // tag result and store it in r0
3983 ASSERT(kSmiTag == 0); // adjust code below
3984 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3985 __ b(&exit);
3986 // slow case
3987 __ bind(&slow);
3988 __ push(r1); // restore stack
3989 __ push(r0);
3990 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3991 switch (op_) {
3992 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3993 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3994 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3995 default: UNREACHABLE();
3996 }
3997 __ bind(&exit);
3998 break;
3999 }
4000
4001 default: UNREACHABLE();
4002 }
4003 __ Ret();
4004}
4005
4006
4007void StackCheckStub::Generate(MacroAssembler* masm) {
4008 Label within_limit;
4009 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
4010 __ ldr(ip, MemOperand(ip));
4011 __ cmp(sp, Operand(ip));
4012 __ b(hs, &within_limit);
4013 // Do tail-call to runtime routine.
4014 __ push(r0);
4015 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
4016 __ bind(&within_limit);
4017
4018 __ StubReturn(1);
4019}
4020
4021
4022void UnarySubStub::Generate(MacroAssembler* masm) {
4023 Label undo;
4024 Label slow;
4025 Label done;
4026
4027 // Enter runtime system if the value is not a smi.
4028 __ tst(r0, Operand(kSmiTagMask));
4029 __ b(ne, &slow);
4030
4031 // Enter runtime system if the value of the expression is zero
4032 // to make sure that we switch between 0 and -0.
4033 __ cmp(r0, Operand(0));
4034 __ b(eq, &slow);
4035
4036 // The value of the expression is a smi that is not zero. Try
4037 // optimistic subtraction '0 - value'.
4038 __ rsb(r1, r0, Operand(0), SetCC);
4039 __ b(vs, &slow);
4040
4041 // If result is a smi we are done.
4042 __ tst(r1, Operand(kSmiTagMask));
4043 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
4044 __ b(eq, &done);
4045
4046 // Enter runtime system.
4047 __ bind(&slow);
4048 __ push(r0);
4049 __ mov(r0, Operand(0)); // set number of arguments
4050 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4051
4052 __ bind(&done);
4053 __ StubReturn(1);
4054}
4055
4056
4057void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
4058 __ push(r0);
4059 __ mov(r0, Operand(0)); // set number of arguments
4060 switch (kind_) {
4061 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
4062 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
4063 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
4064 default: UNREACHABLE();
4065 }
4066 __ StubReturn(argc_);
4067}
4068
4069
4070void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
4071 // r0 holds exception
4072 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
4073 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4074 __ ldr(sp, MemOperand(r3));
4075 __ pop(r2); // pop next in chain
4076 __ str(r2, MemOperand(r3));
4077 // restore parameter- and frame-pointer and pop state.
4078 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4079 // Before returning we restore the context from the frame pointer if not NULL.
4080 // The frame pointer is NULL in the exception handler of a JS entry frame.
4081 __ cmp(fp, Operand(0));
4082 // Set cp to NULL if fp is NULL.
4083 __ mov(cp, Operand(0), LeaveCC, eq);
4084 // Restore cp otherwise.
4085 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
4086 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
4087 __ pop(pc);
4088}
4089
4090
4091void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4092 // Fetch top stack handler.
4093 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4094 __ ldr(r3, MemOperand(r3));
4095
4096 // Unwind the handlers until the ENTRY handler is found.
4097 Label loop, done;
4098 __ bind(&loop);
4099 // Load the type of the current stack handler.
4100 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4101 StackHandlerConstants::kStateOffset;
4102 __ ldr(r2, MemOperand(r3, kStateOffset));
4103 __ cmp(r2, Operand(StackHandler::ENTRY));
4104 __ b(eq, &done);
4105 // Fetch the next handler in the list.
4106 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4107 StackHandlerConstants::kNextOffset;
4108 __ ldr(r3, MemOperand(r3, kNextOffset));
4109 __ jmp(&loop);
4110 __ bind(&done);
4111
4112 // Set the top handler address to next handler past the current ENTRY handler.
4113 __ ldr(r0, MemOperand(r3, kNextOffset));
4114 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4115 __ str(r0, MemOperand(r2));
4116
4117 // Set external caught exception to false.
4118 __ mov(r0, Operand(false));
4119 ExternalReference external_caught(Top::k_external_caught_exception_address);
4120 __ mov(r2, Operand(external_caught));
4121 __ str(r0, MemOperand(r2));
4122
4123 // Set pending exception and r0 to out of memory exception.
4124 Failure* out_of_memory = Failure::OutOfMemoryException();
4125 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4126 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4127 __ str(r0, MemOperand(r2));
4128
4129 // Restore the stack to the address of the ENTRY handler
4130 __ mov(sp, Operand(r3));
4131
4132 // restore parameter- and frame-pointer and pop state.
4133 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4134 // Before returning we restore the context from the frame pointer if not NULL.
4135 // The frame pointer is NULL in the exception handler of a JS entry frame.
4136 __ cmp(fp, Operand(0));
4137 // Set cp to NULL if fp is NULL.
4138 __ mov(cp, Operand(0), LeaveCC, eq);
4139 // Restore cp otherwise.
4140 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
4141 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
4142 __ pop(pc);
4143}
4144
4145
4146void CEntryStub::GenerateCore(MacroAssembler* masm,
4147 Label* throw_normal_exception,
4148 Label* throw_out_of_memory_exception,
4149 StackFrame::Type frame_type,
4150 bool do_gc) {
4151 // r0: result parameter for PerformGC, if any
4152 // r4: number of arguments including receiver (C callee-saved)
4153 // r5: pointer to builtin function (C callee-saved)
4154 // r6: pointer to the first argument (C callee-saved)
4155
4156 if (do_gc) {
4157 // Passing r0.
4158 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4159 }
4160
4161 // Call C built-in.
4162 // r0 = argc, r1 = argv
4163 __ mov(r0, Operand(r4));
4164 __ mov(r1, Operand(r6));
4165
4166 // TODO(1242173): To let the GC traverse the return address of the exit
4167 // frames, we need to know where the return address is. Right now,
4168 // we push it on the stack to be able to find it again, but we never
4169 // restore from it in case of changes, which makes it impossible to
4170 // support moving the C entry code stub. This should be fixed, but currently
4171 // this is OK because the CEntryStub gets generated so early in the V8 boot
4172 // sequence that it is not moving ever.
4173 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4174 __ push(lr);
4175#if !defined(__arm__)
4176 // Notify the simulator of the transition to C code.
4177 __ swi(assembler::arm::call_rt_r5);
4178#else /* !defined(__arm__) */
4179 __ mov(pc, Operand(r5));
4180#endif /* !defined(__arm__) */
4181 // result is in r0 or r0:r1 - do not destroy these registers!
4182
4183 // check for failure result
4184 Label failure_returned;
4185 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4186 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4187 __ add(r2, r0, Operand(1));
4188 __ tst(r2, Operand(kFailureTagMask));
4189 __ b(eq, &failure_returned);
4190
4191 // Exit C frame and return.
4192 // r0:r1: result
4193 // sp: stack pointer
4194 // fp: frame pointer
4195 // pp: caller's parameter pointer pp (restored as C callee-saved)
4196 __ LeaveExitFrame(frame_type);
4197
4198 // check if we should retry or throw exception
4199 Label retry;
4200 __ bind(&failure_returned);
4201 ASSERT(Failure::RETRY_AFTER_GC == 0);
4202 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4203 __ b(eq, &retry);
4204
4205 Label continue_exception;
4206 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4207 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4208 __ b(ne, &continue_exception);
4209
4210 // Retrieve the pending exception and clear the variable.
4211 __ mov(ip, Operand(Factory::the_hole_value().location()));
4212 __ ldr(r3, MemOperand(ip));
4213 __ mov(ip, Operand(Top::pending_exception_address()));
4214 __ ldr(r0, MemOperand(ip));
4215 __ str(r3, MemOperand(ip));
4216
4217 __ bind(&continue_exception);
4218 // Special handling of out of memory exception.
4219 Failure* out_of_memory = Failure::OutOfMemoryException();
4220 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4221 __ b(eq, throw_out_of_memory_exception);
4222
4223 // Handle normal exception.
4224 __ jmp(throw_normal_exception);
4225
4226 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4227}
4228
4229
4230void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4231 // Called from JavaScript; parameters are on stack as if calling JS function
4232 // r0: number of arguments including receiver
4233 // r1: pointer to builtin function
4234 // fp: frame pointer (restored after C call)
4235 // sp: stack pointer (restored as callee's pp after C call)
4236 // cp: current context (C callee-saved)
4237 // pp: caller's parameter pointer pp (C callee-saved)
4238
4239 // NOTE: Invocations of builtins may return failure objects
4240 // instead of a proper result. The builtin entry handles
4241 // this by performing a garbage collection and retrying the
4242 // builtin once.
4243
4244 StackFrame::Type frame_type = is_debug_break
4245 ? StackFrame::EXIT_DEBUG
4246 : StackFrame::EXIT;
4247
4248 // Enter the exit frame that transitions from JavaScript to C++.
4249 __ EnterExitFrame(frame_type);
4250
4251 // r4: number of arguments (C callee-saved)
4252 // r5: pointer to builtin function (C callee-saved)
4253 // r6: pointer to first argument (C callee-saved)
4254
4255 Label throw_out_of_memory_exception;
4256 Label throw_normal_exception;
4257
4258#ifdef DEBUG
4259 if (FLAG_gc_greedy) {
4260 Failure* failure = Failure::RetryAfterGC(0, NEW_SPACE);
4261 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4262 }
4263 GenerateCore(masm,
4264 &throw_normal_exception,
4265 &throw_out_of_memory_exception,
4266 frame_type,
4267 FLAG_gc_greedy);
4268#else
4269 GenerateCore(masm,
4270 &throw_normal_exception,
4271 &throw_out_of_memory_exception,
4272 frame_type,
4273 false);
4274#endif
4275 GenerateCore(masm,
4276 &throw_normal_exception,
4277 &throw_out_of_memory_exception,
4278 frame_type,
4279 true);
4280
4281 __ bind(&throw_out_of_memory_exception);
4282 GenerateThrowOutOfMemory(masm);
4283 // control flow for generated will not return.
4284
4285 __ bind(&throw_normal_exception);
4286 GenerateThrowTOS(masm);
4287}
4288
4289
4290void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4291 // r0: code entry
4292 // r1: function
4293 // r2: receiver
4294 // r3: argc
4295 // [sp+0]: argv
4296
4297 Label invoke, exit;
4298
4299 // Called from C, so do not pop argc and args on exit (preserve sp)
4300 // No need to save register-passed args
4301 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4302 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4303
4304 // Get address of argv, see stm above.
4305 // r0: code entry
4306 // r1: function
4307 // r2: receiver
4308 // r3: argc
4309 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4310 __ ldr(r4, MemOperand(r4)); // argv
4311
4312 // Push a frame with special values setup to mark it as an entry frame.
4313 // r0: code entry
4314 // r1: function
4315 // r2: receiver
4316 // r3: argc
4317 // r4: argv
4318 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4319 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4320 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4321 __ mov(r6, Operand(Smi::FromInt(marker)));
4322 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4323 __ ldr(r5, MemOperand(r5));
4324 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4325
4326 // Setup frame pointer for the frame to be pushed.
4327 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4328
4329 // Call a faked try-block that does the invoke.
4330 __ bl(&invoke);
4331
4332 // Caught exception: Store result (exception) in the pending
4333 // exception field in the JSEnv and return a failure sentinel.
4334 // Coming in here the fp will be invalid because the PushTryHandler below
4335 // sets it to 0 to signal the existence of the JSEntry frame.
4336 __ mov(ip, Operand(Top::pending_exception_address()));
4337 __ str(r0, MemOperand(ip));
4338 __ mov(r0, Operand(Handle<Failure>(Failure::Exception())));
4339 __ b(&exit);
4340
4341 // Invoke: Link this frame into the handler chain.
4342 __ bind(&invoke);
4343 // Must preserve r0-r4, r5-r7 are available.
4344 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4345 // If an exception not caught by another handler occurs, this handler returns
4346 // control to the code after the bl(&invoke) above, which restores all
4347 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4348 // before returning a failure to C.
4349
4350 // Clear any pending exceptions.
4351 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4352 __ ldr(r5, MemOperand(ip));
4353 __ mov(ip, Operand(Top::pending_exception_address()));
4354 __ str(r5, MemOperand(ip));
4355
4356 // Invoke the function by calling through JS entry trampoline builtin.
4357 // Notice that we cannot store a reference to the trampoline code directly in
4358 // this stub, because runtime stubs are not traversed when doing GC.
4359
4360 // Expected registers by Builtins::JSEntryTrampoline
4361 // r0: code entry
4362 // r1: function
4363 // r2: receiver
4364 // r3: argc
4365 // r4: argv
4366 if (is_construct) {
4367 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4368 __ mov(ip, Operand(construct_entry));
4369 } else {
4370 ExternalReference entry(Builtins::JSEntryTrampoline);
4371 __ mov(ip, Operand(entry));
4372 }
4373 __ ldr(ip, MemOperand(ip)); // deref address
4374
4375 // Branch and link to JSEntryTrampoline
4376 __ mov(lr, Operand(pc));
4377 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4378
4379 // Unlink this frame from the handler chain. When reading the
4380 // address of the next handler, there is no need to use the address
4381 // displacement since the current stack pointer (sp) points directly
4382 // to the stack handler.
4383 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4384 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4385 __ str(r3, MemOperand(ip));
4386 // No need to restore registers
4387 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4388
4389 __ bind(&exit); // r0 holds result
4390 // Restore the top frame descriptors from the stack.
4391 __ pop(r3);
4392 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4393 __ str(r3, MemOperand(ip));
4394
4395 // Reset the stack to the callee saved registers.
4396 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4397
4398 // Restore callee-saved registers and return.
4399#ifdef DEBUG
4400 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4401#endif
4402 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4403}
4404
4405
4406void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
4407 // ----------- S t a t e -------------
4408 // -- r0: formal number of parameters for the calling function
4409 // -- r1: key (if value access)
4410 // -- lr: return address
4411 // -----------------------------------
4412
4413 // If we're reading an element we need to check that the key is a smi.
4414 Label slow;
4415 if (type_ == READ_ELEMENT) {
4416 __ tst(r1, Operand(kSmiTagMask));
4417 __ b(ne, &slow);
4418 }
4419
4420 // Check if the calling frame is an arguments adaptor frame.
4421 // r0: formal number of parameters
4422 // r1: key (if access)
4423 Label adaptor;
4424 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4425 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4426 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4427 if (type_ == NEW_OBJECT) {
4428 __ b(ne, &slow);
4429 } else {
4430 __ b(eq, &adaptor);
4431 }
4432
4433 static const int kParamDisplacement =
4434 StandardFrameConstants::kCallerSPOffset - kPointerSize;
4435
4436 if (type_ == READ_LENGTH) {
4437 // Nothing to do: The formal number of parameters has already been
4438 // passed in register r0 by calling function. Just return it.
4439 __ mov(pc, lr);
4440 } else if (type_ == READ_ELEMENT) {
4441 // Check index against formal parameter count. Use unsigned comparison to
4442 // get the negative check for free.
4443 // r0: formal number of parameters
4444 // r1: index
4445 __ cmp(r1, r0);
4446 __ b(cs, &slow);
4447
4448 // Read the argument from the current frame and return it.
4449 __ sub(r3, r0, r1);
4450 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4451 __ ldr(r0, MemOperand(r3, kParamDisplacement));
4452 __ mov(pc, lr);
4453 } else {
4454 ASSERT(type_ == NEW_OBJECT);
4455 // Do nothing here.
4456 }
4457
4458 // An arguments adaptor frame is present. Find the length or the actual
4459 // argument in the calling frame.
4460 // r0: formal number of parameters
4461 // r1: key
4462 // r2: adaptor frame pointer
4463 __ bind(&adaptor);
4464 // Read the arguments length from the adaptor frame. This is the result if
4465 // only accessing the length, otherwise it is used in accessing the value
4466 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4467
4468 if (type_ == READ_LENGTH) {
4469 // Return the length in r0.
4470 __ mov(pc, lr);
4471 } else if (type_ == READ_ELEMENT) {
4472 // Check index against actual arguments count. Use unsigned comparison to
4473 // get the negative check for free.
4474 // r0: actual number of parameter
4475 // r1: index
4476 // r2: adaptor frame point
4477 __ cmp(r1, r0);
4478 __ b(cs, &slow);
4479
4480 // Read the argument from the adaptor frame and return it.
4481 __ sub(r3, r0, r1);
4482 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4483 __ ldr(r0, MemOperand(r3, kParamDisplacement));
4484 __ mov(pc, lr);
4485 } else {
4486 ASSERT(type_ == NEW_OBJECT);
4487 // Patch the arguments.length and the parameters pointer.
4488 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4489 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4490 __ add(r3, r3, Operand(kParamDisplacement + 1 * kPointerSize));
4491 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4492 __ bind(&slow);
4493 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
4494 }
4495
4496 // Return to the calling function.
4497 if (type_ == READ_ELEMENT) {
4498 __ bind(&slow);
4499 __ push(r1);
4500 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4501 }
4502}
4503
4504
4505void ArmCodeGenerator::SetReferenceProperty(CodeGenerator* cgen,
4506 Reference* ref,
4507 Expression* key) {
4508 ASSERT(!ref->is_illegal());
4509 MacroAssembler* masm = cgen->masm();
4510
4511 if (ref->type() == Reference::NAMED) {
4512 // Compute the name of the property.
4513 Literal* literal = key->AsLiteral();
4514 Handle<String> name(String::cast(*literal->handle()));
4515
4516 // Call the appropriate IC code.
4517 __ pop(r0); // value
4518 // Setup the name register.
4519 __ mov(r2, Operand(name));
4520 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
4521 __ Call(ic, RelocInfo::CODE_TARGET);
4522
4523 } else {
4524 // Access keyed property.
4525 ASSERT(ref->type() == Reference::KEYED);
4526
4527 __ pop(r0); // value
4528 SetPropertyStub stub;
4529 __ CallStub(&stub);
4530 }
4531 __ push(r0);
4532}
4533
4534
4535void CallFunctionStub::Generate(MacroAssembler* masm) {
4536 Label slow;
4537 // Get the function to call from the stack.
4538 // function, receiver [, arguments]
4539 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4540
4541 // Check that the function is really a JavaScript function.
4542 // r1: pushed function (to be verified)
4543 __ tst(r1, Operand(kSmiTagMask));
4544 __ b(eq, &slow);
4545 // Get the map of the function object.
4546 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4547 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4548 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4549 __ b(ne, &slow);
4550
4551 // Fast-case: Invoke the function now.
4552 // r1: pushed function
4553 ParameterCount actual(argc_);
4554 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4555
4556 // Slow-case: Non-function called.
4557 __ bind(&slow);
4558 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
4559 __ InvokeBuiltin(Builtins::CALL_NON_FUNCTION, JUMP_JS);
4560}
4561
4562
4563#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004564
4565// -----------------------------------------------------------------------------
4566// CodeGenerator interface
4567
4568// MakeCode() is just a wrapper for CodeGenerator::MakeCode()
4569// so we don't have to expose the entire CodeGenerator class in
4570// the .h file.
4571Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun,
4572 Handle<Script> script,
4573 bool is_eval) {
4574 Handle<Code> code = ArmCodeGenerator::MakeCode(fun, script, is_eval);
4575 if (!code.is_null()) {
4576 Counters::total_compiled_code_size.Increment(code->instruction_size());
4577 }
4578 return code;
4579}
4580
4581
4582} } // namespace v8::internal