blob: dfe574510489035958a4bcd536718d67d229df1f [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
33#include "prettyprinter.h"
34#include "scopeinfo.h"
35#include "scopes.h"
36#include "runtime.h"
37
38namespace v8 { namespace internal {
39
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040class ArmCodeGenerator;
41
42
43// -----------------------------------------------------------------------------
44// Reference support
45
46// A reference is a C++ stack-allocated object that keeps an ECMA
47// reference on the execution stack while in scope. For variables
48// the reference is empty, indicating that it isn't necessary to
49// store state on the stack for keeping track of references to those.
50// For properties, we keep either one (named) or two (indexed) values
51// on the execution stack to represent the reference.
52
53class Reference BASE_EMBEDDED {
54 public:
55 enum Type { ILLEGAL = -1, EMPTY = 0, NAMED = 1, KEYED = 2 };
56 Reference(ArmCodeGenerator* cgen, Expression* expression);
57 ~Reference();
58
59 Expression* expression() const { return expression_; }
60 Type type() const { return type_; }
61 void set_type(Type value) {
62 ASSERT(type_ == ILLEGAL);
63 type_ = value;
64 }
65 int size() const { return type_; }
66
67 bool is_illegal() const { return type_ == ILLEGAL; }
68
69 private:
70 ArmCodeGenerator* cgen_;
71 Expression* expression_;
72 Type type_;
73};
74
75
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000076// -------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077// Code generation state
78
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000079// The state is passed down the AST by the code generator. It is passed
80// implicitly (in a member variable) to the non-static code generator member
81// functions, and explicitly (as an argument) to the static member functions
82// and the AST node member functions.
83//
84// The state is threaded through the call stack. Constructing a state
85// implicitly pushes it on the owning code generator's stack of states, and
86// destroying one implicitly pops it.
87
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088class CodeGenState BASE_EMBEDDED {
89 public:
90 enum AccessType {
91 UNDEFINED,
92 LOAD,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000093 LOAD_TYPEOF_EXPR
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 };
95
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 // Create an initial code generator state. Destroying the initial state
97 // leaves the code generator with a NULL state.
98 explicit CodeGenState(ArmCodeGenerator* owner);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000100 // Create a code generator state based on a code generator's current
101 // state. The new state has its own access type and pair of branch
102 // labels, and no reference.
103 CodeGenState(ArmCodeGenerator* owner,
104 AccessType access,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 Label* true_target,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000106 Label* false_target);
107
108 // Create a code generator state based on a code generator's current
109 // state. The new state has an access type of LOAD, its own reference,
110 // and inherits the pair of branch labels of the current state.
111 CodeGenState(ArmCodeGenerator* owner, Reference* ref);
112
113 // Destroy a code generator state and restore the owning code generator's
114 // previous state.
115 ~CodeGenState();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
117 AccessType access() const { return access_; }
118 Reference* ref() const { return ref_; }
119 Label* true_target() const { return true_target_; }
120 Label* false_target() const { return false_target_; }
121
122 private:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000123 ArmCodeGenerator* owner_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 AccessType access_;
125 Reference* ref_;
126 Label* true_target_;
127 Label* false_target_;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000128 CodeGenState* previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129};
130
131
132// -----------------------------------------------------------------------------
133// ArmCodeGenerator
134
135class ArmCodeGenerator: public CodeGenerator {
136 public:
137 static Handle<Code> MakeCode(FunctionLiteral* fun,
138 Handle<Script> script,
139 bool is_eval);
140
141 MacroAssembler* masm() { return masm_; }
142
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000143 CodeGenState* state() { return state_; }
144 void set_state(CodeGenState* state) { state_ = state; }
145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 private:
147 // Assembler
148 MacroAssembler* masm_; // to generate code
149
150 // Code generation state
151 Scope* scope_;
152 Condition cc_reg_;
153 CodeGenState* state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 int break_stack_height_;
155
156 // Labels
157 Label function_return_;
158
159 // Construction/destruction
160 ArmCodeGenerator(int buffer_size,
161 Handle<Script> script,
162 bool is_eval);
163
164 virtual ~ArmCodeGenerator() { delete masm_; }
165
166 // Main code generation function
167 void GenCode(FunctionLiteral* fun);
168
169 // The following are used by class Reference.
170 void LoadReference(Reference* ref);
171 void UnloadReference(Reference* ref);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
173 // State
174 bool has_cc() const { return cc_reg_ != al; }
175 CodeGenState::AccessType access() const { return state_->access(); }
176 Reference* ref() const { return state_->ref(); }
177 bool is_referenced() const { return state_->ref() != NULL; }
178 Label* true_target() const { return state_->true_target(); }
179 Label* false_target() const { return state_->false_target(); }
180
181
182 // Expressions
183 MemOperand GlobalObject() const {
184 return ContextOperand(cp, Context::GLOBAL_INDEX);
185 }
186
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000187 static MemOperand ContextOperand(Register context, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 return MemOperand(context, Context::SlotOffset(index));
189 }
190
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000191 static MemOperand ParameterOperand(Scope* scope, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 // index -2 corresponds to the activated closure, -1 corresponds
193 // to the receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000194 ASSERT(-2 <= index && index < scope->num_parameters());
195 int offset = (1 + scope->num_parameters() - index) * kPointerSize;
196 return MemOperand(fp, offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 }
198
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000199 MemOperand ParameterOperand(int index) const {
200 return ParameterOperand(scope_, index);
201 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000203 MemOperand FunctionOperand() const {
204 return MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset);
205 }
206
207 static MemOperand SlotOperand(MacroAssembler* masm,
208 Scope* scope,
209 Slot* slot,
210 Register tmp);
211
212 MemOperand SlotOperand(Slot* slot, Register tmp) {
213 return SlotOperand(masm_, scope_, slot, tmp);
214 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
216 void LoadCondition(Expression* x, CodeGenState::AccessType access,
217 Label* true_target, Label* false_target, bool force_cc);
218 void Load(Expression* x,
219 CodeGenState::AccessType access = CodeGenState::LOAD);
220 void LoadGlobal();
221
222 // Special code for typeof expressions: Unfortunately, we must
223 // be careful when loading the expression in 'typeof'
224 // expressions. We are not allowed to throw reference errors for
225 // non-existing properties of the global object, so we must make it
226 // look like an explicit property access, instead of an access
227 // through the context chain.
228 void LoadTypeofExpression(Expression* x);
229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000231 // References
232
233 // Generate code to fetch the value of a reference. The reference is
234 // expected to be on top of the expression stack. It is left in place and
235 // its value is pushed on top of it.
236 void GetValue(Reference* ref) {
237 ASSERT(!has_cc());
238 ASSERT(!ref->is_illegal());
239 CodeGenState new_state(this, ref);
240 Visit(ref->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 }
242
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000243 // Generate code to store a value in a reference. The stored value is
244 // expected on top of the expression stack, with the reference immediately
245 // below it. The expression stack is left unchanged.
246 void SetValue(Reference* ref) {
247 ASSERT(!has_cc());
248 ASSERT(!ref->is_illegal());
249 ref->expression()->GenerateStoreCode(masm_, scope_, ref, NOT_CONST_INIT);
250 }
251
252 // Generate code to store a value in a reference. The stored value is
253 // expected on top of the expression stack, with the reference immediately
254 // below it. The expression stack is left unchanged.
255 void InitConst(Reference* ref) {
256 ASSERT(!has_cc());
257 ASSERT(!ref->is_illegal());
258 ref->expression()->GenerateStoreCode(masm_, scope_, ref, CONST_INIT);
259 }
260
261 // Generate code to fetch a value from a property of a reference. The
262 // reference is expected on top of the expression stack. It is left in
263 // place and its value is pushed on top of it.
264 void GetReferenceProperty(Expression* key);
265
266 // Generate code to store a value in a property of a reference. The
267 // stored value is expected on top of the expression stack, with the
268 // reference immediately below it. The expression stack is left
269 // unchanged.
270 static void SetReferenceProperty(MacroAssembler* masm,
271 Reference* ref,
272 Expression* key);
273
274
mads.s.ager31e71382008-08-13 09:32:07 +0000275 void ToBoolean(Label* true_target, Label* false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276
kasper.lund7276f142008-07-30 08:49:36 +0000277 void GenericBinaryOperation(Token::Value op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 void Comparison(Condition cc, bool strict = false);
279
280 void SmiOperation(Token::Value op, Handle<Object> value, bool reversed);
281
282 void CallWithArguments(ZoneList<Expression*>* arguments, int position);
283
284 // Declare global variables and functions in the given array of
285 // name/value pairs.
286 virtual void DeclareGlobals(Handle<FixedArray> pairs);
287
288 // Instantiate the function boilerplate.
289 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
290
291 // Control flow
292 void Branch(bool if_true, Label* L);
293 void CheckStack();
294 void CleanStack(int num_bytes);
295
296 // Node visitors
297#define DEF_VISIT(type) \
298 virtual void Visit##type(type* node);
299 NODE_LIST(DEF_VISIT)
300#undef DEF_VISIT
301
302 void RecordStatementPosition(Node* node);
303
304 // Activation frames
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000305 void EnterJSFrame();
306 void ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 virtual void GenerateIsSmi(ZoneList<Expression*>* args);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000309 virtual void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 virtual void GenerateIsArray(ZoneList<Expression*>* args);
311
312 virtual void GenerateArgumentsLength(ZoneList<Expression*>* args);
313 virtual void GenerateArgumentsAccess(ZoneList<Expression*>* args);
314
315 virtual void GenerateValueOf(ZoneList<Expression*>* args);
316 virtual void GenerateSetValueOf(ZoneList<Expression*>* args);
kasper.lund7276f142008-07-30 08:49:36 +0000317
318 virtual void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000319
320 virtual void GenerateObjectEquals(ZoneList<Expression*>* args);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000321
322 friend class Reference;
323 friend class Property;
324 friend class VariableProxy;
325 friend class Slot;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326};
327
328
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000329// -------------------------------------------------------------------------
330// CodeGenState implementation.
331
332CodeGenState::CodeGenState(ArmCodeGenerator* owner)
333 : owner_(owner),
334 access_(UNDEFINED),
335 ref_(NULL),
336 true_target_(NULL),
337 false_target_(NULL),
338 previous_(NULL) {
339 owner_->set_state(this);
340}
341
342
343CodeGenState::CodeGenState(ArmCodeGenerator* owner,
344 AccessType access,
345 Label* true_target,
346 Label* false_target)
347 : owner_(owner),
348 access_(access),
349 ref_(NULL),
350 true_target_(true_target),
351 false_target_(false_target),
352 previous_(owner->state()) {
353 owner_->set_state(this);
354}
355
356
357CodeGenState::CodeGenState(ArmCodeGenerator* owner, Reference* ref)
358 : owner_(owner),
359 access_(LOAD),
360 ref_(ref),
361 true_target_(owner->state()->true_target_),
362 false_target_(owner->state()->false_target_),
363 previous_(owner->state()) {
364 owner_->set_state(this);
365}
366
367
368CodeGenState::~CodeGenState() {
369 ASSERT(owner_->state() == this);
370 owner_->set_state(previous_);
371}
372
373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374// -----------------------------------------------------------------------------
375// ArmCodeGenerator implementation
376
377#define __ masm_->
378
379
380Handle<Code> ArmCodeGenerator::MakeCode(FunctionLiteral* flit,
381 Handle<Script> script,
382 bool is_eval) {
mads.s.ager31e71382008-08-13 09:32:07 +0000383#ifdef ENABLE_DISASSEMBLER
384 bool print_code = FLAG_print_code && !Bootstrapper::IsActive();
385#endif // ENABLE_DISASSEMBLER
386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387#ifdef DEBUG
388 bool print_source = false;
389 bool print_ast = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 const char* ftype;
391
392 if (Bootstrapper::IsActive()) {
393 print_source = FLAG_print_builtin_source;
394 print_ast = FLAG_print_builtin_ast;
395 print_code = FLAG_print_builtin_code;
396 ftype = "builtin";
397 } else {
398 print_source = FLAG_print_source;
399 print_ast = FLAG_print_ast;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 ftype = "user-defined";
401 }
402
403 if (FLAG_trace_codegen || print_source || print_ast) {
404 PrintF("*** Generate code for %s function: ", ftype);
405 flit->name()->ShortPrint();
406 PrintF(" ***\n");
407 }
408
409 if (print_source) {
410 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit));
411 }
412
413 if (print_ast) {
414 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit));
415 }
416#endif // DEBUG
417
418 // Generate code.
419 const int initial_buffer_size = 4 * KB;
420 ArmCodeGenerator cgen(initial_buffer_size, script, is_eval);
421 cgen.GenCode(flit);
422 if (cgen.HasStackOverflow()) {
kasper.lund212ac232008-07-16 07:07:30 +0000423 ASSERT(!Top::has_pending_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 return Handle<Code>::null();
425 }
426
427 // Process any deferred code.
428 cgen.ProcessDeferred();
429
430 // Allocate and install the code.
431 CodeDesc desc;
432 cgen.masm()->GetCode(&desc);
433 ScopeInfo<> sinfo(flit->scope());
434 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
435 Handle<Code> code = Factory::NewCode(desc, &sinfo, flags);
436
437 // Add unresolved entries in the code to the fixup list.
438 Bootstrapper::AddFixup(*code, cgen.masm());
439
mads.s.ager31e71382008-08-13 09:32:07 +0000440#ifdef ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 if (print_code) {
442 // Print the source code if available.
443 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
444 PrintF("--- Raw source ---\n");
445 StringInputBuffer stream(String::cast(script->source()));
446 stream.Seek(flit->start_position());
447 // flit->end_position() points to the last character in the stream. We
448 // need to compensate by adding one to calculate the length.
449 int source_len = flit->end_position() - flit->start_position() + 1;
450 for (int i = 0; i < source_len; i++) {
451 if (stream.has_more()) PrintF("%c", stream.GetNext());
452 }
453 PrintF("\n\n");
454 }
455 PrintF("--- Code ---\n");
mads.s.ager31e71382008-08-13 09:32:07 +0000456 code->Disassemble();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 }
mads.s.ager31e71382008-08-13 09:32:07 +0000458#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 return code;
461}
462
463
464ArmCodeGenerator::ArmCodeGenerator(int buffer_size,
465 Handle<Script> script,
466 bool is_eval)
467 : CodeGenerator(is_eval, script),
468 masm_(new MacroAssembler(NULL, buffer_size)),
469 scope_(NULL),
470 cc_reg_(al),
471 state_(NULL),
472 break_stack_height_(0) {
473}
474
475
476// Calling conventions:
477
mads.s.ager31e71382008-08-13 09:32:07 +0000478// r0: the number of arguments
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479// fp: frame pointer
480// sp: stack pointer
481// pp: caller's parameter pointer
482// cp: callee's context
483
484void ArmCodeGenerator::GenCode(FunctionLiteral* fun) {
485 Scope* scope = fun->scope();
486 ZoneList<Statement*>* body = fun->body();
487
488 // Initialize state.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000489 { CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 scope_ = scope;
491 cc_reg_ = al;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
493 // Entry
494 // stack: function, receiver, arguments, return address
495 // r0: number of arguments
496 // sp: stack pointer
497 // fp: frame pointer
498 // pp: caller's parameter pointer
499 // cp: callee's context
500
501 { Comment cmnt(masm_, "[ enter JS frame");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000502 EnterJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 }
504 // tos: code slot
505#ifdef DEBUG
506 if (strlen(FLAG_stop_at) > 0 &&
507 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000508 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 }
510#endif
511
512 // Allocate space for locals and initialize them.
kasper.lund7276f142008-07-30 08:49:36 +0000513 if (scope->num_stack_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 Comment cmnt(masm_, "[ allocate space for locals");
mads.s.ager31e71382008-08-13 09:32:07 +0000515 // Initialize stack slots with 'undefined' value.
516 __ mov(ip, Operand(Factory::undefined_value()));
517 for (int i = 0; i < scope->num_stack_slots(); i++) {
518 __ push(ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 }
520 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521
522 if (scope->num_heap_slots() > 0) {
523 // Allocate local context.
524 // Get outer context and create a new context based on it.
mads.s.ager31e71382008-08-13 09:32:07 +0000525 __ ldr(r0, FunctionOperand());
526 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +0000527 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
528
529 if (kDebug) {
530 Label verified_true;
531 __ cmp(r0, Operand(cp));
532 __ b(eq, &verified_true);
533 __ stop("NewContext: r0 is expected to be the same as cp");
534 __ bind(&verified_true);
535 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 // Update context local.
537 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
538 }
539
540 // TODO(1241774): Improve this code!!!
541 // 1) only needed if we have a context
542 // 2) no need to recompute context ptr every single time
543 // 3) don't copy parameter operand code from SlotOperand!
544 {
545 Comment cmnt2(masm_, "[ copy context parameters into .context");
546
547 // Note that iteration order is relevant here! If we have the same
548 // parameter twice (e.g., function (x, y, x)), and that parameter
549 // needs to be copied into the context, it must be the last argument
550 // passed to the parameter that needs to be copied. This is a rare
551 // case so we don't check for it, instead we rely on the copying
552 // order: such a parameter is copied repeatedly into the same
553 // context location and thus the last value is what is seen inside
554 // the function.
555 for (int i = 0; i < scope->num_parameters(); i++) {
556 Variable* par = scope->parameter(i);
557 Slot* slot = par->slot();
558 if (slot != NULL && slot->type() == Slot::CONTEXT) {
559 ASSERT(!scope->is_global_scope()); // no parameters in global scope
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000560 __ ldr(r1, ParameterOperand(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 // Loads r2 with context; used below in RecordWrite.
562 __ str(r1, SlotOperand(slot, r2));
563 // Load the offset into r3.
564 int slot_offset =
565 FixedArray::kHeaderSize + slot->index() * kPointerSize;
566 __ mov(r3, Operand(slot_offset));
567 __ RecordWrite(r2, r3, r1);
568 }
569 }
570 }
571
572 // Store the arguments object.
573 // This must happen after context initialization because
574 // the arguments array may be stored in the context!
575 if (scope->arguments() != NULL) {
576 ASSERT(scope->arguments_shadow() != NULL);
577 Comment cmnt(masm_, "[ allocate arguments object");
578 {
579 Reference target(this, scope->arguments());
mads.s.ager31e71382008-08-13 09:32:07 +0000580 __ ldr(r0, FunctionOperand());
581 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 __ CallRuntime(Runtime::kNewArguments, 1);
mads.s.ager31e71382008-08-13 09:32:07 +0000583 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 SetValue(&target);
585 }
586 // The value of arguments must also be stored in .arguments.
587 // TODO(1241813): This code can probably be improved by fusing it with
588 // the code that stores the arguments object above.
589 {
590 Reference target(this, scope->arguments_shadow());
591 Load(scope->arguments());
592 SetValue(&target);
593 }
594 }
595
596 // Generate code to 'execute' declarations and initialize
597 // functions (source elements). In case of an illegal
598 // redeclaration we need to handle that instead of processing the
599 // declarations.
600 if (scope->HasIllegalRedeclaration()) {
601 Comment cmnt(masm_, "[ illegal redeclarations");
602 scope->VisitIllegalRedeclaration(this);
603 } else {
604 Comment cmnt(masm_, "[ declarations");
mads.s.ager31e71382008-08-13 09:32:07 +0000605 // ProcessDeclarations calls DeclareGlobals indirectly
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 ProcessDeclarations(scope->declarations());
mads.s.ager31e71382008-08-13 09:32:07 +0000607
v8.team.kasperl727e9952008-09-02 14:56:44 +0000608 // Bail out if a stack-overflow exception occurred when
kasper.lund212ac232008-07-16 07:07:30 +0000609 // processing declarations.
610 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 }
612
mads.s.ager31e71382008-08-13 09:32:07 +0000613 if (FLAG_trace) {
614 // Push a valid value as the parameter. The runtime call only uses
615 // it as the return value to indicate non-failure.
616 __ mov(r0, Operand(Smi::FromInt(0)));
617 __ push(r0);
618 __ CallRuntime(Runtime::kTraceEnter, 1);
619 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620 CheckStack();
621
622 // Compile the body of the function in a vanilla state. Don't
623 // bother compiling all the code if the scope has an illegal
624 // redeclaration.
625 if (!scope->HasIllegalRedeclaration()) {
626 Comment cmnt(masm_, "[ function body");
627#ifdef DEBUG
628 bool is_builtin = Bootstrapper::IsActive();
629 bool should_trace =
630 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000631 if (should_trace) {
632 // Push a valid value as the parameter. The runtime call only uses
633 // it as the return value to indicate non-failure.
634 __ mov(r0, Operand(Smi::FromInt(0)));
635 __ push(r0);
636 __ CallRuntime(Runtime::kDebugTrace, 1);
637 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638#endif
639 VisitStatements(body);
640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 }
642
643 // exit
644 // r0: result
645 // sp: stack pointer
646 // fp: frame pointer
647 // pp: parameter pointer
648 // cp: callee's context
mads.s.ager31e71382008-08-13 09:32:07 +0000649 __ mov(r0, Operand(Factory::undefined_value()));
650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 __ bind(&function_return_);
mads.s.ager31e71382008-08-13 09:32:07 +0000652 if (FLAG_trace) {
653 // Push the return value on the stack as the parameter.
654 // Runtime::TraceExit returns the parameter as it is.
655 __ push(r0);
656 __ CallRuntime(Runtime::kTraceExit, 1);
657 }
658
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000659 // Tear down the frame which will restore the caller's frame pointer and the
660 // link register.
kasper.lund7276f142008-07-30 08:49:36 +0000661 ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000663 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
664 __ mov(pc, lr);
665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 // Code generation state must be reset.
667 scope_ = NULL;
668 ASSERT(!has_cc());
669 ASSERT(state_ == NULL);
670}
671
672
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000673MemOperand ArmCodeGenerator::SlotOperand(MacroAssembler* masm,
674 Scope* scope,
675 Slot* slot,
676 Register tmp) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 // Currently, this assertion will fail if we try to assign to
678 // a constant variable that is constant because it is read-only
679 // (such as the variable referring to a named function expression).
680 // We need to implement assignments to read-only variables.
681 // Ideally, we should do this during AST generation (by converting
682 // such assignments into expression statements); however, in general
683 // we may not be able to make the decision until past AST generation,
684 // that is when the entire program is known.
685 ASSERT(slot != NULL);
686 int index = slot->index();
687 switch (slot->type()) {
688 case Slot::PARAMETER:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000689 return ParameterOperand(scope, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
691 case Slot::LOCAL: {
692 ASSERT(0 <= index &&
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000693 index < scope->num_stack_slots() &&
kasper.lund7276f142008-07-30 08:49:36 +0000694 index >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 int local_offset = JavaScriptFrameConstants::kLocal0Offset -
kasper.lund7276f142008-07-30 08:49:36 +0000696 index * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 return MemOperand(fp, local_offset);
698 }
699
700 case Slot::CONTEXT: {
701 // Follow the context chain if necessary.
702 ASSERT(!tmp.is(cp)); // do not overwrite context register
703 Register context = cp;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000704 int chain_length = scope->ContextChainLength(slot->var()->scope());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 for (int i = chain_length; i-- > 0;) {
706 // Load the closure.
707 // (All contexts, even 'with' contexts, have a closure,
708 // and it is the same for all contexts inside a function.
709 // There is no need to go to the function context first.)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000710 masm->ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 // Load the function context (which is the incoming, outer context).
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000712 masm->ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 context = tmp;
714 }
715 // We may have a 'with' context now. Get the function context.
716 // (In fact this mov may never be the needed, since the scope analysis
717 // may not permit a direct context access in this case and thus we are
718 // always at a function context. However it is safe to dereference be-
719 // cause the function context of a function context is itself. Before
720 // deleting this mov we should try to create a counter-example first,
721 // though...)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000722 masm->ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 return ContextOperand(tmp, index);
724 }
725
726 default:
727 UNREACHABLE();
728 return MemOperand(r0, 0);
729 }
730}
731
732
mads.s.ager31e71382008-08-13 09:32:07 +0000733// Loads a value on the stack. If it is a boolean value, the result may have
734// been (partially) translated into branches, or it may have set the condition
735// code register. If force_cc is set, the value is forced to set the condition
736// code register and no value is pushed. If the condition code register was set,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737// has_cc() is true and cc_reg_ contains the condition to test for 'true'.
738void ArmCodeGenerator::LoadCondition(Expression* x,
739 CodeGenState::AccessType access,
740 Label* true_target,
741 Label* false_target,
742 bool force_cc) {
743 ASSERT(access == CodeGenState::LOAD ||
744 access == CodeGenState::LOAD_TYPEOF_EXPR);
745 ASSERT(!has_cc() && !is_referenced());
746
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000747 { CodeGenState new_state(this, access, true_target, false_target);
748 Visit(x);
749 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000751 // Convert the TOS value to a boolean in the condition code register.
752 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 }
754 ASSERT(has_cc() || !force_cc);
755}
756
757
758void ArmCodeGenerator::Load(Expression* x, CodeGenState::AccessType access) {
759 ASSERT(access == CodeGenState::LOAD ||
760 access == CodeGenState::LOAD_TYPEOF_EXPR);
761
762 Label true_target;
763 Label false_target;
764 LoadCondition(x, access, &true_target, &false_target, false);
765
766 if (has_cc()) {
767 // convert cc_reg_ into a bool
768 Label loaded, materialize_true;
769 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000770 __ mov(r0, Operand(Factory::false_value()));
771 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 __ b(&loaded);
773 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000774 __ mov(r0, Operand(Factory::true_value()));
775 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776 __ bind(&loaded);
777 cc_reg_ = al;
778 }
779
780 if (true_target.is_linked() || false_target.is_linked()) {
781 // we have at least one condition value
782 // that has been "translated" into a branch,
783 // thus it needs to be loaded explicitly again
784 Label loaded;
785 __ b(&loaded); // don't lose current TOS
786 bool both = true_target.is_linked() && false_target.is_linked();
787 // reincarnate "true", if necessary
788 if (true_target.is_linked()) {
789 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000790 __ mov(r0, Operand(Factory::true_value()));
791 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 }
793 // if both "true" and "false" need to be reincarnated,
794 // jump across code for "false"
795 if (both)
796 __ b(&loaded);
797 // reincarnate "false", if necessary
798 if (false_target.is_linked()) {
799 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000800 __ mov(r0, Operand(Factory::false_value()));
801 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 }
803 // everything is loaded at this point
804 __ bind(&loaded);
805 }
806 ASSERT(!has_cc());
807}
808
809
810void ArmCodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000811 __ ldr(r0, GlobalObject());
812 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813}
814
815
816// TODO(1241834): Get rid of this function in favor of just using Load, now
817// that we have the LOAD_TYPEOF_EXPR access type. => Need to handle
818// global variables w/o reference errors elsewhere.
819void ArmCodeGenerator::LoadTypeofExpression(Expression* x) {
820 Variable* variable = x->AsVariableProxy()->AsVariable();
821 if (variable != NULL && !variable->is_this() && variable->is_global()) {
822 // NOTE: This is somewhat nasty. We force the compiler to load
823 // the variable as if through '<global>.<variable>' to make sure we
824 // do not get reference errors.
825 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
826 Literal key(variable->name());
827 // TODO(1241834): Fetch the position from the variable instead of using
828 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000829 Property property(&global, &key, RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 Load(&property);
831 } else {
832 Load(x, CodeGenState::LOAD_TYPEOF_EXPR);
833 }
834}
835
836
837Reference::Reference(ArmCodeGenerator* cgen, Expression* expression)
838 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
839 cgen->LoadReference(this);
840}
841
842
843Reference::~Reference() {
844 cgen_->UnloadReference(this);
845}
846
847
848void ArmCodeGenerator::LoadReference(Reference* ref) {
849 Expression* e = ref->expression();
850 Property* property = e->AsProperty();
851 Variable* var = e->AsVariableProxy()->AsVariable();
852
853 if (property != NULL) {
854 Load(property->obj());
855 // Used a named reference if the key is a literal symbol.
856 // We don't use a named reference if they key is a string that can be
857 // legally parsed as an integer. This is because, otherwise we don't
858 // get into the slow case code that handles [] on String objects.
859 Literal* literal = property->key()->AsLiteral();
860 uint32_t dummy;
861 if (literal != NULL && literal->handle()->IsSymbol() &&
862 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
863 ref->set_type(Reference::NAMED);
864 } else {
865 Load(property->key());
866 ref->set_type(Reference::KEYED);
867 }
868 } else if (var != NULL) {
869 if (var->is_global()) {
870 // global variable
871 LoadGlobal();
872 ref->set_type(Reference::NAMED);
873 } else {
874 // local variable
875 ref->set_type(Reference::EMPTY);
876 }
877 } else {
878 Load(e);
879 __ CallRuntime(Runtime::kThrowReferenceError, 1);
mads.s.ager31e71382008-08-13 09:32:07 +0000880 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 }
882}
883
884
885void ArmCodeGenerator::UnloadReference(Reference* ref) {
886 int size = ref->size();
887 if (size <= 0) {
888 // Do nothing. No popping is necessary.
889 } else {
mads.s.ager31e71382008-08-13 09:32:07 +0000890 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 __ add(sp, sp, Operand(size * kPointerSize));
mads.s.ager31e71382008-08-13 09:32:07 +0000892 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 }
894}
895
896
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000897void Property::GenerateStoreCode(MacroAssembler* masm,
898 Scope* scope,
899 Reference* ref,
900 InitState init_state) {
901 Comment cmnt(masm, "[ Store to Property");
902 masm->RecordPosition(position());
903 ArmCodeGenerator::SetReferenceProperty(masm, ref, key());
904}
905
906
907void VariableProxy::GenerateStoreCode(MacroAssembler* masm,
908 Scope* scope,
909 Reference* ref,
910 InitState init_state) {
911 Comment cmnt(masm, "[ Store to VariableProxy");
912 Variable* node = var();
913
914 Expression* expr = node->rewrite();
915 if (expr != NULL) {
916 expr->GenerateStoreCode(masm, scope, ref, init_state);
917 } else {
918 ASSERT(node->is_global());
919 if (node->AsProperty() != NULL) {
920 masm->RecordPosition(node->AsProperty()->position());
921 }
922 ArmCodeGenerator::SetReferenceProperty(masm, ref,
923 new Literal(node->name()));
924 }
925}
926
927
928void Slot::GenerateStoreCode(MacroAssembler* masm,
929 Scope* scope,
930 Reference* ref,
931 InitState init_state) {
932 Comment cmnt(masm, "[ Store to Slot");
933
934 if (type() == Slot::LOOKUP) {
935 ASSERT(var()->mode() == Variable::DYNAMIC);
936
937 // For now, just do a runtime call.
938 masm->push(cp);
939 masm->mov(r0, Operand(var()->name()));
940 masm->push(r0);
941
942 if (init_state == CONST_INIT) {
943 // Same as the case for a normal store, but ignores attribute
944 // (e.g. READ_ONLY) of context slot so that we can initialize const
945 // properties (introduced via eval("const foo = (some expr);")). Also,
946 // uses the current function context instead of the top context.
947 //
948 // Note that we must declare the foo upon entry of eval(), via a
949 // context slot declaration, but we cannot initialize it at the same
950 // time, because the const declaration may be at the end of the eval
951 // code (sigh...) and the const variable may have been used before
952 // (where its value is 'undefined'). Thus, we can only do the
953 // initialization when we actually encounter the expression and when
954 // the expression operands are defined and valid, and thus we need the
955 // split into 2 operations: declaration of the context slot followed
956 // by initialization.
957 masm->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
958 } else {
959 masm->CallRuntime(Runtime::kStoreContextSlot, 3);
960 }
961 // Storing a variable must keep the (new) value on the expression
962 // stack. This is necessary for compiling assignment expressions.
963 masm->push(r0);
964
965 } else {
966 ASSERT(var()->mode() != Variable::DYNAMIC);
967
968 Label exit;
969 if (init_state == CONST_INIT) {
970 ASSERT(var()->mode() == Variable::CONST);
971 // Only the first const initialization must be executed (the slot
972 // still contains 'the hole' value). When the assignment is executed,
973 // the code is identical to a normal store (see below).
974 Comment cmnt(masm, "[ Init const");
975 masm->ldr(r2, ArmCodeGenerator::SlotOperand(masm, scope, this, r2));
976 masm->cmp(r2, Operand(Factory::the_hole_value()));
977 masm->b(ne, &exit);
978 }
979
980 // We must execute the store.
981 // r2 may be loaded with context; used below in RecordWrite.
982 // Storing a variable must keep the (new) value on the stack. This is
983 // necessary for compiling assignment expressions.
984 //
985 // Note: We will reach here even with var()->mode() == Variable::CONST
986 // because of const declarations which will initialize consts to 'the
987 // hole' value and by doing so, end up calling this code. r2 may be
988 // loaded with context; used below in RecordWrite.
989 masm->pop(r0);
990 masm->str(r0, ArmCodeGenerator::SlotOperand(masm, scope, this, r2));
991 masm->push(r0);
992
993 if (type() == Slot::CONTEXT) {
994 // Skip write barrier if the written value is a smi.
995 masm->tst(r0, Operand(kSmiTagMask));
996 masm->b(eq, &exit);
997 // r2 is loaded with context when calling SlotOperand above.
998 int offset = FixedArray::kHeaderSize + index() * kPointerSize;
999 masm->mov(r3, Operand(offset));
1000 masm->RecordWrite(r2, r3, r1);
1001 }
1002 // If we definitely did not jump over the assignment, we do not need to
1003 // bind the exit label. Doing so can defeat peephole optimization.
1004 if (init_state == CONST_INIT || type() == Slot::CONTEXT) {
1005 masm->bind(&exit);
1006 }
1007 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008}
1009
1010
1011// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
1012// register to a boolean in the condition code register. The code
1013// may jump to 'false_target' in case the register converts to 'false'.
mads.s.ager31e71382008-08-13 09:32:07 +00001014void ArmCodeGenerator::ToBoolean(Label* true_target,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +00001016 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 // Only the condition code should be set.
mads.s.ager31e71382008-08-13 09:32:07 +00001018 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019
1020 // Fast case checks
1021
mads.s.ager31e71382008-08-13 09:32:07 +00001022 // Check if the value is 'false'.
1023 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 __ b(eq, false_target);
1025
mads.s.ager31e71382008-08-13 09:32:07 +00001026 // Check if the value is 'true'.
1027 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 __ b(eq, true_target);
1029
mads.s.ager31e71382008-08-13 09:32:07 +00001030 // Check if the value is 'undefined'.
1031 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 __ b(eq, false_target);
1033
mads.s.ager31e71382008-08-13 09:32:07 +00001034 // Check if the value is a smi.
1035 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +00001037 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 __ b(eq, true_target);
1039
1040 // Slow case: call the runtime.
1041 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001042 __ CallRuntime(Runtime::kToBool, 1);
1043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 // Convert result (r0) to condition code
1045 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046
1047 cc_reg_ = ne;
1048}
1049
1050
1051#undef __
1052#define __ masm->
1053
1054
1055class GetPropertyStub : public CodeStub {
1056 public:
1057 GetPropertyStub() { }
1058
1059 private:
1060 Major MajorKey() { return GetProperty; }
1061 int MinorKey() { return 0; }
1062 void Generate(MacroAssembler* masm);
1063
1064 const char* GetName() { return "GetPropertyStub"; }
1065};
1066
1067
1068void GetPropertyStub::Generate(MacroAssembler* masm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001069 // sp[0]: key
1070 // sp[1]: receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 Label slow, fast;
mads.s.ager31e71382008-08-13 09:32:07 +00001072 // Get the key and receiver object from the stack.
1073 __ ldm(ia, sp, r0.bit() | r1.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 // Check that the key is a smi.
1075 __ tst(r0, Operand(kSmiTagMask));
1076 __ b(ne, &slow);
1077 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
1078 // Check that the object isn't a smi.
1079 __ tst(r1, Operand(kSmiTagMask));
1080 __ b(eq, &slow);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001081
1082 // Check that the object is some kind of JS object EXCEPT JS Value type.
1083 // In the case that the object is a value-wrapper object,
1084 // we enter the runtime system to make sure that indexing into string
1085 // objects work as intended.
1086 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
1088 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1089 __ cmp(r2, Operand(JS_OBJECT_TYPE));
1090 __ b(lt, &slow);
1091
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 // Get the elements array of the object.
1093 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
1094 // Check that the object is in fast mode (not dictionary).
1095 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1096 __ cmp(r3, Operand(Factory::hash_table_map()));
1097 __ b(eq, &slow);
1098 // Check that the key (index) is within bounds.
1099 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
1100 __ cmp(r0, Operand(r3));
1101 __ b(lo, &fast);
1102
1103 // Slow case: Push extra copies of the arguments (2).
1104 __ bind(&slow);
1105 __ ldm(ia, sp, r0.bit() | r1.bit());
1106 __ stm(db_w, sp, r0.bit() | r1.bit());
1107 // Do tail-call to runtime routine.
mads.s.ager31e71382008-08-13 09:32:07 +00001108 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110 // Fast case: Do the load.
1111 __ bind(&fast);
1112 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
1113 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
1114 __ cmp(r0, Operand(Factory::the_hole_value()));
1115 // In case the loaded value is the_hole we have to consult GetProperty
1116 // to ensure the prototype chain is searched.
1117 __ b(eq, &slow);
1118
1119 masm->StubReturn(1);
1120}
1121
1122
1123class SetPropertyStub : public CodeStub {
1124 public:
1125 SetPropertyStub() { }
1126
1127 private:
1128 Major MajorKey() { return SetProperty; }
1129 int MinorKey() { return 0; }
1130 void Generate(MacroAssembler* masm);
1131
1132 const char* GetName() { return "GetPropertyStub"; }
1133};
1134
1135
mads.s.ager31e71382008-08-13 09:32:07 +00001136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137void SetPropertyStub::Generate(MacroAssembler* masm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001138 // r0 : value
1139 // sp[0] : key
1140 // sp[1] : receiver
1141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 Label slow, fast, array, extra, exit;
1143 // Get the key and the object from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001144 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 // Check that the key is a smi.
1146 __ tst(r1, Operand(kSmiTagMask));
1147 __ b(ne, &slow);
1148 // Check that the object isn't a smi.
1149 __ tst(r3, Operand(kSmiTagMask));
1150 __ b(eq, &slow);
1151 // Get the type of the object from its map.
1152 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
1153 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
1154 // Check if the object is a JS array or not.
1155 __ cmp(r2, Operand(JS_ARRAY_TYPE));
1156 __ b(eq, &array);
1157 // Check that the object is some kind of JS object.
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001158 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 __ b(lt, &slow);
1160
1161
1162 // Object case: Check key against length in the elements array.
1163 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
1164 // Check that the object is in fast mode (not dictionary).
1165 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
1166 __ cmp(r2, Operand(Factory::hash_table_map()));
1167 __ b(eq, &slow);
1168 // Untag the key (for checking against untagged length in the fixed array).
1169 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
1170 // Compute address to store into and check array bounds.
1171 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
1172 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
1173 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
1174 __ cmp(r1, Operand(ip));
1175 __ b(lo, &fast);
1176
1177
1178 // Slow case: Push extra copies of the arguments (3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 __ bind(&slow);
1180 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
1181 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
1182 // Do tail-call to runtime routine.
mads.s.ager31e71382008-08-13 09:32:07 +00001183 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184
1185
1186 // Extra capacity case: Check if there is extra capacity to
1187 // perform the store and update the length. Used for adding one
1188 // element to the array by writing to array[array.length].
1189 // r0 == value, r1 == key, r2 == elements, r3 == object
1190 __ bind(&extra);
1191 __ b(ne, &slow); // do not leave holes in the array
1192 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
1193 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
1194 __ cmp(r1, Operand(ip));
1195 __ b(hs, &slow);
1196 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
1197 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
1198 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
1199 __ mov(r3, Operand(r2));
1200 // NOTE: Computing the address to store into must take the fact
1201 // that the key has been incremented into account.
1202 int displacement = Array::kHeaderSize - kHeapObjectTag -
1203 ((1 << kSmiTagSize) * 2);
1204 __ add(r2, r2, Operand(displacement));
1205 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1206 __ b(&fast);
1207
1208
1209 // Array case: Get the length and the elements array from the JS
1210 // array. Check that the array is in fast mode; if it is the
1211 // length is always a smi.
1212 // r0 == value, r3 == object
1213 __ bind(&array);
1214 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
1215 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1216 __ cmp(r1, Operand(Factory::hash_table_map()));
1217 __ b(eq, &slow);
1218
1219 // Check the key against the length in the array, compute the
1220 // address to store into and fall through to fast case.
1221 __ ldr(r1, MemOperand(sp));
1222 // r0 == value, r1 == key, r2 == elements, r3 == object.
1223 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
1224 __ cmp(r1, Operand(ip));
1225 __ b(hs, &extra);
1226 __ mov(r3, Operand(r2));
1227 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
1228 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1229
1230
1231 // Fast case: Do the store.
1232 // r0 == value, r2 == address to store into, r3 == elements
1233 __ bind(&fast);
1234 __ str(r0, MemOperand(r2));
1235 // Skip write barrier if the written value is a smi.
1236 __ tst(r0, Operand(kSmiTagMask));
1237 __ b(eq, &exit);
1238 // Update write barrier for the elements array address.
1239 __ sub(r1, r2, Operand(r3));
1240 __ RecordWrite(r3, r1, r2);
1241 __ bind(&exit);
1242 masm->StubReturn(1);
1243}
1244
1245
kasper.lund7276f142008-07-30 08:49:36 +00001246class GenericBinaryOpStub : public CodeStub {
1247 public:
1248 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
1249
1250 private:
1251 Token::Value op_;
1252
1253 Major MajorKey() { return GenericBinaryOp; }
1254 int MinorKey() { return static_cast<int>(op_); }
1255 void Generate(MacroAssembler* masm);
1256
1257 const char* GetName() {
1258 switch (op_) {
1259 case Token::ADD: return "GenericBinaryOpStub_ADD";
1260 case Token::SUB: return "GenericBinaryOpStub_SUB";
1261 case Token::MUL: return "GenericBinaryOpStub_MUL";
1262 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001263 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
1264 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
1265 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
1266 case Token::SAR: return "GenericBinaryOpStub_SAR";
1267 case Token::SHL: return "GenericBinaryOpStub_SHL";
1268 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +00001269 default: return "GenericBinaryOpStub";
1270 }
1271 }
1272
1273#ifdef DEBUG
1274 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
1275#endif
1276};
1277
1278
1279void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001280 // r1 : x
1281 // r0 : y
1282 // result : r0
1283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 switch (op_) {
1285 case Token::ADD: {
1286 Label slow, exit;
1287 // fast path
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1289 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
1290 // go slow-path in case of overflow
1291 __ b(vs, &slow);
1292 // go slow-path in case of non-smi operands
1293 ASSERT(kSmiTag == 0); // adjust code below
1294 __ tst(r2, Operand(kSmiTagMask));
1295 __ b(eq, &exit);
1296 // slow path
1297 __ bind(&slow);
1298 __ sub(r0, r0, Operand(r1)); // revert optimistic add
mads.s.ager31e71382008-08-13 09:32:07 +00001299 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 __ push(r0);
1301 __ mov(r0, Operand(1)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001302 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 // done
1304 __ bind(&exit);
1305 break;
1306 }
1307
1308 case Token::SUB: {
1309 Label slow, exit;
1310 // fast path
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1312 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
1313 // go slow-path in case of overflow
1314 __ b(vs, &slow);
1315 // go slow-path in case of non-smi operands
1316 ASSERT(kSmiTag == 0); // adjust code below
1317 __ tst(r2, Operand(kSmiTagMask));
1318 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
1319 __ b(eq, &exit);
1320 // slow path
1321 __ bind(&slow);
mads.s.ager31e71382008-08-13 09:32:07 +00001322 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 __ push(r0);
1324 __ mov(r0, Operand(1)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001325 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 // done
1327 __ bind(&exit);
1328 break;
1329 }
1330
1331 case Token::MUL: {
1332 Label slow, exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 // tag check
1334 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1335 ASSERT(kSmiTag == 0); // adjust code below
1336 __ tst(r2, Operand(kSmiTagMask));
1337 __ b(ne, &slow);
1338 // remove tag from one operand (but keep sign), so that result is smi
1339 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
1340 // do multiplication
1341 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
1342 // go slow on overflows (overflow bit is not set)
1343 __ mov(ip, Operand(r3, ASR, 31));
1344 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
1345 __ b(ne, &slow);
1346 // go slow on zero result to handle -0
1347 __ tst(r3, Operand(r3));
1348 __ mov(r0, Operand(r3), LeaveCC, ne);
1349 __ b(ne, &exit);
1350 // slow case
1351 __ bind(&slow);
mads.s.ager31e71382008-08-13 09:32:07 +00001352 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 __ push(r0);
1354 __ mov(r0, Operand(1)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001355 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 // done
1357 __ bind(&exit);
1358 break;
1359 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001360
1361 case Token::BIT_OR:
1362 case Token::BIT_AND:
1363 case Token::BIT_XOR: {
1364 Label slow, exit;
1365 // tag check
1366 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1367 ASSERT(kSmiTag == 0); // adjust code below
1368 __ tst(r2, Operand(kSmiTagMask));
1369 __ b(ne, &slow);
1370 switch (op_) {
1371 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
1372 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
1373 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
1374 default: UNREACHABLE();
1375 }
1376 __ b(&exit);
1377 __ bind(&slow);
1378 __ push(r1); // restore stack
1379 __ push(r0);
1380 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
1381 switch (op_) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001382 case Token::BIT_OR:
1383 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
1384 break;
1385 case Token::BIT_AND:
1386 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
1387 break;
1388 case Token::BIT_XOR:
1389 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
1390 break;
1391 default:
1392 UNREACHABLE();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001393 }
1394 __ bind(&exit);
1395 break;
1396 }
1397
1398 case Token::SHL:
1399 case Token::SHR:
1400 case Token::SAR: {
1401 Label slow, exit;
1402 // tag check
1403 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1404 ASSERT(kSmiTag == 0); // adjust code below
1405 __ tst(r2, Operand(kSmiTagMask));
1406 __ b(ne, &slow);
1407 // remove tags from operands (but keep sign)
1408 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
1409 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
1410 // use only the 5 least significant bits of the shift count
1411 __ and_(r2, r2, Operand(0x1f));
1412 // perform operation
1413 switch (op_) {
1414 case Token::SAR:
1415 __ mov(r3, Operand(r3, ASR, r2));
1416 // no checks of result necessary
1417 break;
1418
1419 case Token::SHR:
1420 __ mov(r3, Operand(r3, LSR, r2));
1421 // check that the *unsigned* result fits in a smi
1422 // neither of the two high-order bits can be set:
1423 // - 0x80000000: high bit would be lost when smi tagging
1424 // - 0x40000000: this number would convert to negative when
1425 // smi tagging these two cases can only happen with shifts
1426 // by 0 or 1 when handed a valid smi
1427 __ and_(r2, r3, Operand(0xc0000000), SetCC);
1428 __ b(ne, &slow);
1429 break;
1430
1431 case Token::SHL:
1432 __ mov(r3, Operand(r3, LSL, r2));
1433 // check that the *signed* result fits in a smi
1434 __ add(r2, r3, Operand(0x40000000), SetCC);
1435 __ b(mi, &slow);
1436 break;
1437
1438 default: UNREACHABLE();
1439 }
1440 // tag result and store it in r0
1441 ASSERT(kSmiTag == 0); // adjust code below
1442 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
1443 __ b(&exit);
1444 // slow case
1445 __ bind(&slow);
1446 __ push(r1); // restore stack
1447 __ push(r0);
1448 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
1449 switch (op_) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001450 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
1451 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
1452 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001453 default: UNREACHABLE();
1454 }
1455 __ bind(&exit);
1456 break;
1457 }
1458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 default: UNREACHABLE();
1460 }
mads.s.ager31e71382008-08-13 09:32:07 +00001461 __ Ret();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462}
1463
1464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465void StackCheckStub::Generate(MacroAssembler* masm) {
1466 Label within_limit;
1467 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
1468 __ ldr(ip, MemOperand(ip));
1469 __ cmp(sp, Operand(ip));
1470 __ b(hs, &within_limit);
1471 // Do tail-call to runtime routine.
1472 __ push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001473 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 __ bind(&within_limit);
1475
1476 masm->StubReturn(1);
1477}
1478
1479
1480void UnarySubStub::Generate(MacroAssembler* masm) {
1481 Label undo;
1482 Label slow;
1483 Label done;
1484
1485 // Enter runtime system if the value is not a smi.
1486 __ tst(r0, Operand(kSmiTagMask));
1487 __ b(ne, &slow);
1488
1489 // Enter runtime system if the value of the expression is zero
1490 // to make sure that we switch between 0 and -0.
1491 __ cmp(r0, Operand(0));
1492 __ b(eq, &slow);
1493
1494 // The value of the expression is a smi that is not zero. Try
1495 // optimistic subtraction '0 - value'.
1496 __ rsb(r1, r0, Operand(0), SetCC);
1497 __ b(vs, &slow);
1498
1499 // If result is a smi we are done.
1500 __ tst(r1, Operand(kSmiTagMask));
1501 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
1502 __ b(eq, &done);
1503
1504 // Enter runtime system.
1505 __ bind(&slow);
1506 __ push(r0);
1507 __ mov(r0, Operand(0)); // set number of arguments
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001508 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509
1510 __ bind(&done);
1511 masm->StubReturn(1);
1512}
1513
1514
1515class InvokeBuiltinStub : public CodeStub {
1516 public:
1517 enum Kind { Inc, Dec, ToNumber };
1518 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
1519
1520 private:
1521 Kind kind_;
1522 int argc_;
1523
1524 Major MajorKey() { return InvokeBuiltin; }
1525 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
1526 void Generate(MacroAssembler* masm);
1527
1528 const char* GetName() { return "InvokeBuiltinStub"; }
1529
1530#ifdef DEBUG
1531 void Print() {
1532 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
1533 static_cast<int>(kind_),
1534 argc_);
1535 }
1536#endif
1537};
1538
1539
1540void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
1541 __ push(r0);
1542 __ mov(r0, Operand(0)); // set number of arguments
1543 switch (kind_) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001544 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
1545 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
1546 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547 default: UNREACHABLE();
1548 }
1549 masm->StubReturn(argc_);
1550}
1551
1552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
1554 // r0 holds exception
1555 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1557 __ ldr(sp, MemOperand(r3));
1558 __ pop(r2); // pop next in chain
1559 __ str(r2, MemOperand(r3));
1560 // restore parameter- and frame-pointer and pop state.
1561 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
1562 // Before returning we restore the context from the frame pointer if not NULL.
1563 // The frame pointer is NULL in the exception handler of a JS entry frame.
1564 __ cmp(fp, Operand(0));
1565 // Set cp to NULL if fp is NULL.
1566 __ mov(cp, Operand(0), LeaveCC, eq);
1567 // Restore cp otherwise.
1568 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1569 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
1570 __ pop(pc);
1571}
1572
1573
1574void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
1575 // Fetch top stack handler.
1576 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1577 __ ldr(r3, MemOperand(r3));
1578
1579 // Unwind the handlers until the ENTRY handler is found.
1580 Label loop, done;
1581 __ bind(&loop);
1582 // Load the type of the current stack handler.
1583 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
1584 StackHandlerConstants::kStateOffset;
1585 __ ldr(r2, MemOperand(r3, kStateOffset));
1586 __ cmp(r2, Operand(StackHandler::ENTRY));
1587 __ b(eq, &done);
1588 // Fetch the next handler in the list.
1589 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
1590 StackHandlerConstants::kNextOffset;
1591 __ ldr(r3, MemOperand(r3, kNextOffset));
1592 __ jmp(&loop);
1593 __ bind(&done);
1594
1595 // Set the top handler address to next handler past the current ENTRY handler.
1596 __ ldr(r0, MemOperand(r3, kNextOffset));
1597 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
1598 __ str(r0, MemOperand(r2));
1599
1600 // Set external caught exception to false.
1601 __ mov(r0, Operand(false));
1602 ExternalReference external_caught(Top::k_external_caught_exception_address);
1603 __ mov(r2, Operand(external_caught));
1604 __ str(r0, MemOperand(r2));
1605
mads.s.ager31e71382008-08-13 09:32:07 +00001606 // Set pending exception and r0 to out of memory exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 Failure* out_of_memory = Failure::OutOfMemoryException();
1608 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1609 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
1610 __ str(r0, MemOperand(r2));
1611
1612 // Restore the stack to the address of the ENTRY handler
1613 __ mov(sp, Operand(r3));
1614
1615 // restore parameter- and frame-pointer and pop state.
1616 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
1617 // Before returning we restore the context from the frame pointer if not NULL.
1618 // The frame pointer is NULL in the exception handler of a JS entry frame.
1619 __ cmp(fp, Operand(0));
1620 // Set cp to NULL if fp is NULL.
1621 __ mov(cp, Operand(0), LeaveCC, eq);
1622 // Restore cp otherwise.
1623 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1624 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
1625 __ pop(pc);
1626}
1627
1628
1629void CEntryStub::GenerateCore(MacroAssembler* masm,
1630 Label* throw_normal_exception,
1631 Label* throw_out_of_memory_exception,
ager@chromium.org236ad962008-09-25 09:45:57 +00001632 StackFrame::Type frame_type,
1633 bool do_gc) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 // r0: result parameter for PerformGC, if any
mads.s.ager31e71382008-08-13 09:32:07 +00001635 // r4: number of arguments including receiver (C callee-saved)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 // r5: pointer to builtin function (C callee-saved)
ager@chromium.org236ad962008-09-25 09:45:57 +00001637 // r6: pointer to the first argument (C callee-saved)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638
1639 if (do_gc) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001640 // Passing r0.
1641 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 }
1643
mads.s.ager31e71382008-08-13 09:32:07 +00001644 // Call C built-in.
ager@chromium.org236ad962008-09-25 09:45:57 +00001645 // r0 = argc, r1 = argv
mads.s.ager31e71382008-08-13 09:32:07 +00001646 __ mov(r0, Operand(r4));
ager@chromium.org236ad962008-09-25 09:45:57 +00001647 __ mov(r1, Operand(r6));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648
1649 // TODO(1242173): To let the GC traverse the return address of the exit
1650 // frames, we need to know where the return address is. Right now,
1651 // we push it on the stack to be able to find it again, but we never
1652 // restore from it in case of changes, which makes it impossible to
1653 // support moving the C entry code stub. This should be fixed, but currently
1654 // this is OK because the CEntryStub gets generated so early in the V8 boot
1655 // sequence that it is not moving ever.
1656 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
1657 __ push(lr);
1658#if !defined(__arm__)
1659 // Notify the simulator of the transition to C code.
1660 __ swi(assembler::arm::call_rt_r5);
1661#else /* !defined(__arm__) */
1662 __ mov(pc, Operand(r5));
1663#endif /* !defined(__arm__) */
1664 // result is in r0 or r0:r1 - do not destroy these registers!
1665
1666 // check for failure result
1667 Label failure_returned;
1668 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
1669 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
1670 __ add(r2, r0, Operand(1));
1671 __ tst(r2, Operand(kFailureTagMask));
1672 __ b(eq, &failure_returned);
1673
ager@chromium.org236ad962008-09-25 09:45:57 +00001674 // Exit C frame and return.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 // r0:r1: result
1676 // sp: stack pointer
1677 // fp: frame pointer
1678 // pp: caller's parameter pointer pp (restored as C callee-saved)
ager@chromium.org236ad962008-09-25 09:45:57 +00001679 __ LeaveExitFrame(frame_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
1681 // check if we should retry or throw exception
1682 Label retry;
1683 __ bind(&failure_returned);
1684 ASSERT(Failure::RETRY_AFTER_GC == 0);
1685 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
1686 __ b(eq, &retry);
1687
1688 Label continue_exception;
1689 // If the returned failure is EXCEPTION then promote Top::pending_exception().
1690 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
1691 __ b(ne, &continue_exception);
1692
1693 // Retrieve the pending exception and clear the variable.
1694 __ mov(ip, Operand(Factory::the_hole_value().location()));
1695 __ ldr(r3, MemOperand(ip));
1696 __ mov(ip, Operand(Top::pending_exception_address()));
1697 __ ldr(r0, MemOperand(ip));
1698 __ str(r3, MemOperand(ip));
1699
1700 __ bind(&continue_exception);
1701 // Special handling of out of memory exception.
1702 Failure* out_of_memory = Failure::OutOfMemoryException();
1703 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1704 __ b(eq, throw_out_of_memory_exception);
1705
1706 // Handle normal exception.
1707 __ jmp(throw_normal_exception);
1708
1709 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
1710}
1711
1712
1713void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
1714 // Called from JavaScript; parameters are on stack as if calling JS function
mads.s.ager31e71382008-08-13 09:32:07 +00001715 // r0: number of arguments including receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 // r1: pointer to builtin function
1717 // fp: frame pointer (restored after C call)
1718 // sp: stack pointer (restored as callee's pp after C call)
1719 // cp: current context (C callee-saved)
1720 // pp: caller's parameter pointer pp (C callee-saved)
1721
1722 // NOTE: Invocations of builtins may return failure objects
1723 // instead of a proper result. The builtin entry handles
1724 // this by performing a garbage collection and retrying the
1725 // builtin once.
1726
ager@chromium.org236ad962008-09-25 09:45:57 +00001727 StackFrame::Type frame_type = is_debug_break
1728 ? StackFrame::EXIT_DEBUG
1729 : StackFrame::EXIT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730
ager@chromium.org236ad962008-09-25 09:45:57 +00001731 // Enter the exit frame that transitions from JavaScript to C++.
1732 __ EnterExitFrame(frame_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733
ager@chromium.org236ad962008-09-25 09:45:57 +00001734 // r4: number of arguments (C callee-saved)
1735 // r5: pointer to builtin function (C callee-saved)
1736 // r6: pointer to first argument (C callee-saved)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737
1738 Label throw_out_of_memory_exception;
1739 Label throw_normal_exception;
1740
1741#ifdef DEBUG
1742 if (FLAG_gc_greedy) {
1743 Failure* failure = Failure::RetryAfterGC(0, NEW_SPACE);
1744 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
1745 }
1746 GenerateCore(masm,
1747 &throw_normal_exception,
1748 &throw_out_of_memory_exception,
ager@chromium.org236ad962008-09-25 09:45:57 +00001749 frame_type,
1750 FLAG_gc_greedy);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751#else
1752 GenerateCore(masm,
1753 &throw_normal_exception,
1754 &throw_out_of_memory_exception,
ager@chromium.org236ad962008-09-25 09:45:57 +00001755 frame_type,
1756 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757#endif
1758 GenerateCore(masm,
1759 &throw_normal_exception,
1760 &throw_out_of_memory_exception,
ager@chromium.org236ad962008-09-25 09:45:57 +00001761 frame_type,
1762 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763
1764 __ bind(&throw_out_of_memory_exception);
1765 GenerateThrowOutOfMemory(masm);
1766 // control flow for generated will not return.
1767
1768 __ bind(&throw_normal_exception);
1769 GenerateThrowTOS(masm);
1770}
1771
1772
1773void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
1774 // r0: code entry
1775 // r1: function
1776 // r2: receiver
1777 // r3: argc
1778 // [sp+0]: argv
1779
1780 Label invoke, exit;
1781
1782 // Called from C, so do not pop argc and args on exit (preserve sp)
1783 // No need to save register-passed args
1784 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001785 __ stm(db_w, sp, kCalleeSaved | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786
1787 // Get address of argv, see stm above.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001788 // r0: code entry
1789 // r1: function
1790 // r2: receiver
1791 // r3: argc
1792 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 __ ldr(r4, MemOperand(r4)); // argv
1794
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001795 // Push a frame with special values setup to mark it as an entry frame.
1796 // r0: code entry
1797 // r1: function
1798 // r2: receiver
1799 // r3: argc
1800 // r4: argv
1801 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
1802 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
1803 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
1804 __ mov(r6, Operand(Smi::FromInt(marker)));
1805 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1806 __ ldr(r5, MemOperand(r5));
1807 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
1808
1809 // Setup frame pointer for the frame to be pushed.
1810 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811
1812 // Call a faked try-block that does the invoke.
1813 __ bl(&invoke);
1814
1815 // Caught exception: Store result (exception) in the pending
1816 // exception field in the JSEnv and return a failure sentinel.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001817 // Coming in here the fp will be invalid because the PushTryHandler below
1818 // sets it to 0 to signal the existence of the JSEntry frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 __ mov(ip, Operand(Top::pending_exception_address()));
1820 __ str(r0, MemOperand(ip));
1821 __ mov(r0, Operand(Handle<Failure>(Failure::Exception())));
1822 __ b(&exit);
1823
1824 // Invoke: Link this frame into the handler chain.
1825 __ bind(&invoke);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001826 // Must preserve r0-r4, r5-r7 are available.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
1828 // If an exception not caught by another handler occurs, this handler returns
1829 // control to the code after the bl(&invoke) above, which restores all
1830 // kCalleeSaved registers (including cp, pp and fp) to their saved values
1831 // before returning a failure to C.
1832
1833 // Clear any pending exceptions.
1834 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
1835 __ ldr(r5, MemOperand(ip));
1836 __ mov(ip, Operand(Top::pending_exception_address()));
1837 __ str(r5, MemOperand(ip));
1838
1839 // Invoke the function by calling through JS entry trampoline builtin.
1840 // Notice that we cannot store a reference to the trampoline code directly in
1841 // this stub, because runtime stubs are not traversed when doing GC.
1842
1843 // Expected registers by Builtins::JSEntryTrampoline
1844 // r0: code entry
1845 // r1: function
1846 // r2: receiver
1847 // r3: argc
1848 // r4: argv
1849 if (is_construct) {
1850 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
1851 __ mov(ip, Operand(construct_entry));
1852 } else {
1853 ExternalReference entry(Builtins::JSEntryTrampoline);
1854 __ mov(ip, Operand(entry));
1855 }
1856 __ ldr(ip, MemOperand(ip)); // deref address
1857
1858 // Branch and link to JSEntryTrampoline
1859 __ mov(lr, Operand(pc));
1860 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
1861
1862 // Unlink this frame from the handler chain. When reading the
1863 // address of the next handler, there is no need to use the address
1864 // displacement since the current stack pointer (sp) points directly
1865 // to the stack handler.
1866 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
1867 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
1868 __ str(r3, MemOperand(ip));
1869 // No need to restore registers
1870 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
1871
1872 __ bind(&exit); // r0 holds result
1873 // Restore the top frame descriptors from the stack.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001874 __ pop(r3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1876 __ str(r3, MemOperand(ip));
1877
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001878 // Reset the stack to the callee saved registers.
1879 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001881 // Restore callee-saved registers and return.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882#ifdef DEBUG
1883 if (FLAG_debug_code) __ mov(lr, Operand(pc));
1884#endif
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001885 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886}
1887
1888
1889class ArgumentsAccessStub: public CodeStub {
1890 public:
1891 explicit ArgumentsAccessStub(bool is_length) : is_length_(is_length) { }
1892
1893 private:
1894 bool is_length_;
1895
1896 Major MajorKey() { return ArgumentsAccess; }
1897 int MinorKey() { return is_length_ ? 1 : 0; }
1898 void Generate(MacroAssembler* masm);
1899
1900 const char* GetName() { return "ArgumentsAccessStub"; }
1901
1902#ifdef DEBUG
1903 void Print() {
1904 PrintF("ArgumentsAccessStub (is_length %s)\n",
1905 is_length_ ? "true" : "false");
1906 }
1907#endif
1908};
1909
1910
1911void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001912 // ----------- S t a t e -------------
1913 // -- r0: formal number of parameters for the calling function
1914 // -- r1: key (if value access)
1915 // -- lr: return address
1916 // -----------------------------------
1917
1918 // Check that the key is a smi for non-length accesses.
1919 Label slow;
1920 if (!is_length_) {
1921 __ tst(r1, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 __ b(ne, &slow);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001923 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001925 // Check if the calling frame is an arguments adaptor frame.
1926 // r0: formal number of parameters
1927 // r1: key (if access)
1928 Label adaptor;
1929 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1930 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1931 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
1932 __ b(eq, &adaptor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001934 static const int kParamDisplacement =
1935 StandardFrameConstants::kCallerSPOffset - kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001937 if (is_length_) {
1938 // Nothing to do: the formal length of parameters has been passed in r0
1939 // by the calling function.
1940 } else {
1941 // Check index against formal parameter count. Use unsigned comparison to
1942 // get the negative check for free.
1943 // r0: formal number of parameters
1944 // r1: index
1945 __ cmp(r1, r0);
1946 __ b(cs, &slow);
1947
1948 // Read the argument from the current frame.
1949 __ sub(r3, r0, r1);
1950 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
1951 __ ldr(r0, MemOperand(r3, kParamDisplacement));
1952 }
1953
1954 // Return to the calling function.
1955 __ mov(pc, lr);
1956
1957 // An arguments adaptor frame is present. Find the length or the actual
1958 // argument in the calling frame.
1959 // r0: formal number of parameters
1960 // r1: key
1961 // r2: adaptor frame pointer
1962 __ bind(&adaptor);
1963 // Read the arguments length from the adaptor frame. This is the result if
1964 // only accessing the length, otherwise it is used in accessing the value
1965 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1966
1967 if (!is_length_) {
1968 // Check index against actual arguments count. Use unsigned comparison to
1969 // get the negative check for free.
1970 // r0: actual number of parameter
1971 // r1: index
1972 // r2: adaptor frame point
1973 __ cmp(r1, r0);
1974 __ b(cs, &slow);
1975
1976 // Read the argument from the adaptor frame.
1977 __ sub(r3, r0, r1);
1978 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
1979 __ ldr(r0, MemOperand(r3, kParamDisplacement));
1980 }
1981
1982 // Return to the calling function.
1983 __ mov(pc, lr);
1984
1985 if (!is_length_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 __ bind(&slow);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001987 __ push(r1);
mads.s.ager31e71382008-08-13 09:32:07 +00001988 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 }
1990}
1991
1992
1993#undef __
1994#define __ masm_->
1995
1996
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001997void ArmCodeGenerator::GetReferenceProperty(Expression* key) {
1998 ASSERT(!ref()->is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 Reference::Type type = ref()->type();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002001 // TODO(1241834): Make sure that this it is safe to ignore the distinction
2002 // between access types LOAD and LOAD_TYPEOF_EXPR. If there is a chance
2003 // that reference errors can be thrown below, we must distinguish between
2004 // the two kinds of loads (typeof expression loads must not throw a
2005 // reference error).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 if (type == Reference::NAMED) {
2007 // Compute the name of the property.
2008 Literal* literal = key->AsLiteral();
2009 Handle<String> name(String::cast(*literal->handle()));
2010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 // Call the appropriate IC code.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002012 // Setup the name register.
2013 __ mov(r2, Operand(name));
2014 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2015 Variable* var = ref()->expression()->AsVariableProxy()->AsVariable();
2016 if (var != NULL) {
2017 ASSERT(var->is_global());
ager@chromium.org236ad962008-09-25 09:45:57 +00002018 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 } else {
ager@chromium.org236ad962008-09-25 09:45:57 +00002020 __ Call(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002024 // Access keyed property.
2025 ASSERT(type == Reference::KEYED);
2026
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002027 // TODO(1224671): Implement inline caching for keyed loads as on ia32.
2028 GetPropertyStub stub;
2029 __ CallStub(&stub);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 }
mads.s.ager31e71382008-08-13 09:32:07 +00002031 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032}
2033
2034
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002035void ArmCodeGenerator::SetReferenceProperty(MacroAssembler* masm,
2036 Reference* ref,
2037 Expression* key) {
2038 ASSERT(!ref->is_illegal());
2039 Reference::Type type = ref->type();
2040
2041 if (type == Reference::NAMED) {
2042 // Compute the name of the property.
2043 Literal* literal = key->AsLiteral();
2044 Handle<String> name(String::cast(*literal->handle()));
2045
2046 // Call the appropriate IC code.
2047 masm->pop(r0); // value
2048 // Setup the name register.
2049 masm->mov(r2, Operand(name));
2050 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ager@chromium.org236ad962008-09-25 09:45:57 +00002051 masm->Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002052
2053 } else {
2054 // Access keyed property.
2055 ASSERT(type == Reference::KEYED);
2056
2057 masm->pop(r0); // value
2058 SetPropertyStub stub;
2059 masm->CallStub(&stub);
2060 }
2061 masm->push(r0);
2062}
2063
2064
kasper.lund7276f142008-07-30 08:49:36 +00002065void ArmCodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +00002066 // sp[0] : y
2067 // sp[1] : x
2068 // result : r0
2069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 // Stub is entered with a call: 'return address' is in lr.
2071 switch (op) {
2072 case Token::ADD: // fall through.
2073 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002074 case Token::MUL:
2075 case Token::BIT_OR:
2076 case Token::BIT_AND:
2077 case Token::BIT_XOR:
2078 case Token::SHL:
2079 case Token::SHR:
2080 case Token::SAR: {
mads.s.ager31e71382008-08-13 09:32:07 +00002081 __ pop(r0); // r0 : y
2082 __ pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +00002083 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 __ CallStub(&stub);
2085 break;
2086 }
2087
2088 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +00002089 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002090 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 break;
2092 }
2093
2094 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +00002095 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002096 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 break;
2098 }
2099
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100 case Token::COMMA:
mads.s.ager31e71382008-08-13 09:32:07 +00002101 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 // simply discard left value
mads.s.ager31e71382008-08-13 09:32:07 +00002103 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 break;
2105
2106 default:
2107 // Other cases should have been handled before this point.
2108 UNREACHABLE();
2109 break;
2110 }
2111}
2112
2113
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002114class DeferredInlinedSmiOperation: public DeferredCode {
2115 public:
2116 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
2117 int value, bool reversed) :
2118 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
2119 set_comment("[ DeferredInlinedSmiOperation");
2120 }
2121
2122 virtual void Generate() {
2123 switch (op_) {
2124 case Token::ADD: {
2125 if (reversed_) {
2126 // revert optimistic add
2127 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
2128 __ mov(r1, Operand(Smi::FromInt(value_))); // x
2129 } else {
2130 // revert optimistic add
2131 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
2132 __ mov(r0, Operand(Smi::FromInt(value_)));
2133 }
2134 break;
2135 }
2136
2137 case Token::SUB: {
2138 if (reversed_) {
2139 // revert optimistic sub
2140 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
2141 __ mov(r1, Operand(Smi::FromInt(value_)));
2142 } else {
2143 __ add(r1, r0, Operand(Smi::FromInt(value_)));
2144 __ mov(r0, Operand(Smi::FromInt(value_)));
2145 }
2146 break;
2147 }
2148
2149 case Token::BIT_OR:
2150 case Token::BIT_XOR:
2151 case Token::BIT_AND: {
2152 if (reversed_) {
2153 __ mov(r1, Operand(Smi::FromInt(value_)));
2154 } else {
2155 __ mov(r1, Operand(r0));
2156 __ mov(r0, Operand(Smi::FromInt(value_)));
2157 }
2158 break;
2159 }
2160
2161 case Token::SHL:
2162 case Token::SHR:
2163 case Token::SAR: {
2164 if (!reversed_) {
2165 __ mov(r1, Operand(r0));
2166 __ mov(r0, Operand(Smi::FromInt(value_)));
2167 } else {
2168 UNREACHABLE(); // should have been handled in SmiOperation
2169 }
2170 break;
2171 }
2172
2173 default:
2174 // other cases should have been handled before this point.
2175 UNREACHABLE();
2176 break;
2177 }
2178
2179 GenericBinaryOpStub igostub(op_);
2180 __ CallStub(&igostub);
2181 }
2182
2183 private:
2184 Token::Value op_;
2185 int value_;
2186 bool reversed_;
2187};
2188
2189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190void ArmCodeGenerator::SmiOperation(Token::Value op,
2191 Handle<Object> value,
2192 bool reversed) {
2193 // NOTE: This is an attempt to inline (a bit) more of the code for
2194 // some possible smi operations (like + and -) when (at least) one
2195 // of the operands is a literal smi. With this optimization, the
2196 // performance of the system is increased by ~15%, and the generated
2197 // code size is increased by ~1% (measured on a combination of
2198 // different benchmarks).
2199
mads.s.ager31e71382008-08-13 09:32:07 +00002200 // sp[0] : operand
2201
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002202 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203
2204 Label exit;
mads.s.ager31e71382008-08-13 09:32:07 +00002205 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206
2207 switch (op) {
2208 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002209 DeferredCode* deferred =
2210 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002212 __ add(r0, r0, Operand(value), SetCC);
2213 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002215 __ b(ne, deferred->enter());
2216 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 break;
2218 }
2219
2220 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002221 DeferredCode* deferred =
2222 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002225 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002227 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002229 __ b(vs, deferred->enter());
2230 __ tst(r0, Operand(kSmiTagMask));
2231 __ b(ne, deferred->enter());
2232 __ bind(deferred->exit());
2233 break;
2234 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002236 case Token::BIT_OR:
2237 case Token::BIT_XOR:
2238 case Token::BIT_AND: {
2239 DeferredCode* deferred =
2240 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
2241 __ tst(r0, Operand(kSmiTagMask));
2242 __ b(ne, deferred->enter());
2243 switch (op) {
2244 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
2245 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
2246 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
2247 default: UNREACHABLE();
2248 }
2249 __ bind(deferred->exit());
2250 break;
2251 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002253 case Token::SHL:
2254 case Token::SHR:
2255 case Token::SAR: {
2256 if (reversed) {
2257 __ mov(ip, Operand(value));
2258 __ push(ip);
2259 __ push(r0);
2260 GenericBinaryOperation(op);
2261
2262 } else {
2263 int shift_value = int_value & 0x1f; // least significant 5 bits
2264 DeferredCode* deferred =
2265 new DeferredInlinedSmiOperation(this, op, shift_value, false);
2266 __ tst(r0, Operand(kSmiTagMask));
2267 __ b(ne, deferred->enter());
2268 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
2269 switch (op) {
2270 case Token::SHL: {
2271 __ mov(r2, Operand(r2, LSL, shift_value));
2272 // check that the *unsigned* result fits in a smi
2273 __ add(r3, r2, Operand(0x40000000), SetCC);
2274 __ b(mi, deferred->enter());
2275 break;
2276 }
2277 case Token::SHR: {
2278 // LSR by immediate 0 means shifting 32 bits.
2279 if (shift_value != 0) {
2280 __ mov(r2, Operand(r2, LSR, shift_value));
2281 }
2282 // check that the *unsigned* result fits in a smi
2283 // neither of the two high-order bits can be set:
2284 // - 0x80000000: high bit would be lost when smi tagging
2285 // - 0x40000000: this number would convert to negative when
2286 // smi tagging these two cases can only happen with shifts
2287 // by 0 or 1 when handed a valid smi
2288 __ and_(r3, r2, Operand(0xc0000000), SetCC);
2289 __ b(ne, deferred->enter());
2290 break;
2291 }
2292 case Token::SAR: {
2293 if (shift_value != 0) {
2294 // ASR by immediate 0 means shifting 32 bits.
2295 __ mov(r2, Operand(r2, ASR, shift_value));
2296 }
2297 break;
2298 }
2299 default: UNREACHABLE();
2300 }
2301 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
2302 __ bind(deferred->exit());
2303 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 break;
2305 }
2306
2307 default:
2308 if (!reversed) {
mads.s.ager31e71382008-08-13 09:32:07 +00002309 __ push(r0);
2310 __ mov(r0, Operand(value));
2311 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 } else {
2313 __ mov(ip, Operand(value));
2314 __ push(ip);
mads.s.ager31e71382008-08-13 09:32:07 +00002315 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 }
kasper.lund7276f142008-07-30 08:49:36 +00002317 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 break;
2319 }
2320
2321 __ bind(&exit);
2322}
2323
2324
2325void ArmCodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +00002326 // sp[0] : y
2327 // sp[1] : x
2328 // result : cc register
2329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 // Strict only makes sense for equality comparisons.
2331 ASSERT(!strict || cc == eq);
2332
2333 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002334 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
2335 if (cc == gt || cc == le) {
2336 cc = ReverseCondition(cc);
mads.s.ager31e71382008-08-13 09:32:07 +00002337 __ pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002338 __ pop(r0);
2339 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002340 __ pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002341 __ pop(r1);
2342 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 __ orr(r2, r0, Operand(r1));
2344 __ tst(r2, Operand(kSmiTagMask));
2345 __ b(eq, &smi);
2346
2347 // Perform non-smi comparison by runtime call.
2348 __ push(r1);
2349
2350 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002351 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 int argc;
2353 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002354 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 argc = 1;
2356 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002357 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358 int ncr; // NaN compare result
2359 if (cc == lt || cc == le) {
2360 ncr = GREATER;
2361 } else {
2362 ASSERT(cc == gt || cc == ge); // remaining cases
2363 ncr = LESS;
2364 }
mads.s.ager31e71382008-08-13 09:32:07 +00002365 __ push(r0);
2366 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 argc = 2;
2368 }
2369
2370 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2371 // tagged as a small integer.
mads.s.ager31e71382008-08-13 09:32:07 +00002372 __ push(r0);
2373 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002374 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375 __ cmp(r0, Operand(0));
2376 __ b(&exit);
2377
2378 // test smi equality by pointer comparison.
2379 __ bind(&smi);
2380 __ cmp(r1, Operand(r0));
2381
2382 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 cc_reg_ = cc;
2384}
2385
2386
kasper.lund7276f142008-07-30 08:49:36 +00002387class CallFunctionStub: public CodeStub {
2388 public:
2389 explicit CallFunctionStub(int argc) : argc_(argc) {}
2390
2391 void Generate(MacroAssembler* masm);
2392
2393 private:
2394 int argc_;
2395
2396 const char* GetName() { return "CallFuntionStub"; }
2397
2398#if defined(DEBUG)
2399 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
2400#endif // defined(DEBUG)
2401
2402 Major MajorKey() { return CallFunction; }
2403 int MinorKey() { return argc_; }
2404};
2405
2406
2407void CallFunctionStub::Generate(MacroAssembler* masm) {
2408 Label slow;
kasper.lund7276f142008-07-30 08:49:36 +00002409 // Get the function to call from the stack.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002410 // function, receiver [, arguments]
kasper.lund7276f142008-07-30 08:49:36 +00002411 masm->ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
2412
2413 // Check that the function is really a JavaScript function.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002414 // r1: pushed function (to be verified)
kasper.lund7276f142008-07-30 08:49:36 +00002415 masm->tst(r1, Operand(kSmiTagMask));
2416 masm->b(eq, &slow);
2417 // Get the map of the function object.
2418 masm->ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2419 masm->ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2420 masm->cmp(r2, Operand(JS_FUNCTION_TYPE));
2421 masm->b(ne, &slow);
2422
2423 // Fast-case: Invoke the function now.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002424 // r1: pushed function
2425 ParameterCount actual(argc_);
2426 masm->InvokeFunction(r1, actual, JUMP_FUNCTION);
kasper.lund7276f142008-07-30 08:49:36 +00002427
2428 // Slow-case: Non-function called.
2429 masm->bind(&slow);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002430 masm->mov(r0, Operand(argc_)); // Setup the number of arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002431 masm->InvokeBuiltin(Builtins::CALL_NON_FUNCTION, JUMP_JS);
kasper.lund7276f142008-07-30 08:49:36 +00002432}
2433
2434
mads.s.ager31e71382008-08-13 09:32:07 +00002435// Call the function on the stack with the given arguments.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436void ArmCodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
2437 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002439 for (int i = 0; i < args->length(); i++) {
2440 Load(args->at(i));
2441 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442
kasper.lund7276f142008-07-30 08:49:36 +00002443 // Record the position for debugging purposes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 __ RecordPosition(position);
2445
kasper.lund7276f142008-07-30 08:49:36 +00002446 // Use the shared code stub to call the function.
2447 CallFunctionStub call_function(args->length());
2448 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449
2450 // Restore context and pop function from the stack.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002452 __ pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453}
2454
2455
2456void ArmCodeGenerator::Branch(bool if_true, Label* L) {
2457 ASSERT(has_cc());
2458 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
2459 __ b(cc, L);
2460 cc_reg_ = al;
2461}
2462
2463
2464void ArmCodeGenerator::CheckStack() {
2465 if (FLAG_check_stack) {
2466 Comment cmnt(masm_, "[ check stack");
2467 StackCheckStub stub;
2468 __ CallStub(&stub);
2469 }
2470}
2471
2472
2473void ArmCodeGenerator::VisitBlock(Block* node) {
2474 Comment cmnt(masm_, "[ Block");
2475 if (FLAG_debug_info) RecordStatementPosition(node);
2476 node->set_break_stack_height(break_stack_height_);
2477 VisitStatements(node->statements());
2478 __ bind(node->break_target());
2479}
2480
2481
2482void ArmCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00002483 __ mov(r0, Operand(pairs));
2484 __ push(r0);
2485 __ push(cp);
2486 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
2487 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002489 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490}
2491
2492
2493void ArmCodeGenerator::VisitDeclaration(Declaration* node) {
2494 Comment cmnt(masm_, "[ Declaration");
2495 Variable* var = node->proxy()->var();
2496 ASSERT(var != NULL); // must have been resolved
2497 Slot* slot = var->slot();
2498
2499 // If it was not possible to allocate the variable at compile time,
2500 // we need to "declare" it at runtime to make sure it actually
2501 // exists in the local context.
2502 if (slot != NULL && slot->type() == Slot::LOOKUP) {
2503 // Variables with a "LOOKUP" slot were introduced as non-locals
2504 // during variable resolution and must have mode DYNAMIC.
2505 ASSERT(var->mode() == Variable::DYNAMIC);
2506 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00002507 __ push(cp);
2508 __ mov(r0, Operand(var->name()));
2509 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 // Declaration nodes are always declared in only two modes.
2511 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
2512 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00002513 __ mov(r0, Operand(Smi::FromInt(attr)));
2514 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 // Push initial value, if any.
2516 // Note: For variables we must not push an initial value (such as
2517 // 'undefined') because we may have a (legal) redeclaration and we
2518 // must not destroy the current value.
2519 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00002520 __ mov(r0, Operand(Factory::the_hole_value()));
2521 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 } else if (node->fun() != NULL) {
2523 Load(node->fun());
2524 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002525 __ mov(r0, Operand(0)); // no initial value!
2526 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 }
2528 __ CallRuntime(Runtime::kDeclareContextSlot, 5);
mads.s.ager31e71382008-08-13 09:32:07 +00002529 __ push(r0);
2530
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 return;
2532 }
2533
2534 ASSERT(!var->is_global());
2535
2536 // If we have a function or a constant, we need to initialize the variable.
2537 Expression* val = NULL;
2538 if (node->mode() == Variable::CONST) {
2539 val = new Literal(Factory::the_hole_value());
2540 } else {
2541 val = node->fun(); // NULL if we don't have a function
2542 }
2543
2544 if (val != NULL) {
2545 // Set initial value.
2546 Reference target(this, node->proxy());
2547 Load(val);
2548 SetValue(&target);
2549 // Get rid of the assigned value (declarations are statements).
mads.s.ager31e71382008-08-13 09:32:07 +00002550 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551 }
2552}
2553
2554
2555void ArmCodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
2556 Comment cmnt(masm_, "[ ExpressionStatement");
2557 if (FLAG_debug_info) RecordStatementPosition(node);
2558 Expression* expression = node->expression();
2559 expression->MarkAsStatement();
2560 Load(expression);
mads.s.ager31e71382008-08-13 09:32:07 +00002561 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562}
2563
2564
2565void ArmCodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
2566 Comment cmnt(masm_, "// EmptyStatement");
2567 // nothing to do
2568}
2569
2570
2571void ArmCodeGenerator::VisitIfStatement(IfStatement* node) {
2572 Comment cmnt(masm_, "[ IfStatement");
2573 // Generate different code depending on which
2574 // parts of the if statement are present or not.
2575 bool has_then_stm = node->HasThenStatement();
2576 bool has_else_stm = node->HasElseStatement();
2577
2578 if (FLAG_debug_info) RecordStatementPosition(node);
2579
2580 Label exit;
2581 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002582 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 Label then;
2584 Label else_;
2585 // if (cond)
2586 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
2587 Branch(false, &else_);
2588 // then
2589 __ bind(&then);
2590 Visit(node->then_statement());
2591 __ b(&exit);
2592 // else
2593 __ bind(&else_);
2594 Visit(node->else_statement());
2595
2596 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002597 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598 ASSERT(!has_else_stm);
2599 Label then;
2600 // if (cond)
2601 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &exit, true);
2602 Branch(false, &exit);
2603 // then
2604 __ bind(&then);
2605 Visit(node->then_statement());
2606
2607 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00002608 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 ASSERT(!has_then_stm);
2610 Label else_;
2611 // if (!cond)
2612 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &else_, true);
2613 Branch(true, &exit);
2614 // else
2615 __ bind(&else_);
2616 Visit(node->else_statement());
2617
2618 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002619 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620 ASSERT(!has_then_stm && !has_else_stm);
2621 // if (cond)
2622 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &exit, false);
2623 if (has_cc()) {
2624 cc_reg_ = al;
2625 } else {
2626 __ pop(r0); // __ Pop(no_reg)
2627 }
2628 }
2629
2630 // end
2631 __ bind(&exit);
2632}
2633
2634
2635void ArmCodeGenerator::CleanStack(int num_bytes) {
2636 ASSERT(num_bytes >= 0);
2637 if (num_bytes > 0) {
mads.s.ager31e71382008-08-13 09:32:07 +00002638 __ add(sp, sp, Operand(num_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 }
2640}
2641
2642
2643void ArmCodeGenerator::VisitContinueStatement(ContinueStatement* node) {
2644 Comment cmnt(masm_, "[ ContinueStatement");
2645 if (FLAG_debug_info) RecordStatementPosition(node);
2646 CleanStack(break_stack_height_ - node->target()->break_stack_height());
2647 __ b(node->target()->continue_target());
2648}
2649
2650
2651void ArmCodeGenerator::VisitBreakStatement(BreakStatement* node) {
2652 Comment cmnt(masm_, "[ BreakStatement");
2653 if (FLAG_debug_info) RecordStatementPosition(node);
2654 CleanStack(break_stack_height_ - node->target()->break_stack_height());
2655 __ b(node->target()->break_target());
2656}
2657
2658
2659void ArmCodeGenerator::VisitReturnStatement(ReturnStatement* node) {
2660 Comment cmnt(masm_, "[ ReturnStatement");
2661 if (FLAG_debug_info) RecordStatementPosition(node);
2662 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00002663 // Move the function result into r0.
2664 __ pop(r0);
2665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 __ b(&function_return_);
2667}
2668
2669
2670void ArmCodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
2671 Comment cmnt(masm_, "[ WithEnterStatement");
2672 if (FLAG_debug_info) RecordStatementPosition(node);
2673 Load(node->expression());
kasper.lund7276f142008-07-30 08:49:36 +00002674 __ CallRuntime(Runtime::kPushContext, 1);
2675 if (kDebug) {
2676 Label verified_true;
2677 __ cmp(r0, Operand(cp));
2678 __ b(eq, &verified_true);
2679 __ stop("PushContext: r0 is expected to be the same as cp");
2680 __ bind(&verified_true);
2681 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 // Update context local.
2683 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2684}
2685
2686
2687void ArmCodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
2688 Comment cmnt(masm_, "[ WithExitStatement");
2689 // Pop context.
2690 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2691 // Update context local.
2692 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2693}
2694
2695
2696void ArmCodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
2697 Comment cmnt(masm_, "[ SwitchStatement");
2698 if (FLAG_debug_info) RecordStatementPosition(node);
2699 node->set_break_stack_height(break_stack_height_);
2700
2701 Load(node->tag());
2702
2703 Label next, fall_through, default_case;
2704 ZoneList<CaseClause*>* cases = node->cases();
2705 int length = cases->length();
2706
2707 for (int i = 0; i < length; i++) {
2708 CaseClause* clause = cases->at(i);
2709
2710 Comment cmnt(masm_, "[ case clause");
2711
2712 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002713 // Continue matching cases. The program will execute the default case's
2714 // statements if it does not match any of the cases.
2715 __ b(&next);
2716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 // Bind the default case label, so we can branch to it when we
2718 // have compared against all other cases.
2719 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002720 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 } else {
2722 __ bind(&next);
2723 next.Unuse();
mads.s.ager31e71382008-08-13 09:32:07 +00002724 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 __ push(r0); // duplicate TOS
2726 Load(clause->label());
2727 Comparison(eq, true);
2728 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 }
2730
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002731 // Entering the case statement for the first time. Remove the switch value
2732 // from the stack.
2733 __ pop(r0);
2734
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002736 // This is also the target for the fall through from the previous case's
2737 // statements which has to skip over the matching code and the popping of
2738 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739 __ bind(&fall_through);
2740 fall_through.Unuse();
2741 VisitStatements(clause->statements());
2742 __ b(&fall_through);
2743 }
2744
2745 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002746 // Reached the end of the case statements without matching any of the cases.
2747 if (default_case.is_bound()) {
2748 // A default case exists -> execute its statements.
2749 __ b(&default_case);
2750 } else {
2751 // Remove the switch value from the stack.
2752 __ pop(r0);
2753 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754
2755 __ bind(&fall_through);
2756 __ bind(node->break_target());
2757}
2758
2759
2760void ArmCodeGenerator::VisitLoopStatement(LoopStatement* node) {
2761 Comment cmnt(masm_, "[ LoopStatement");
2762 if (FLAG_debug_info) RecordStatementPosition(node);
2763 node->set_break_stack_height(break_stack_height_);
2764
2765 // simple condition analysis
2766 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
2767 if (node->cond() == NULL) {
2768 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2769 info = ALWAYS_TRUE;
2770 } else {
2771 Literal* lit = node->cond()->AsLiteral();
2772 if (lit != NULL) {
2773 if (lit->IsTrue()) {
2774 info = ALWAYS_TRUE;
2775 } else if (lit->IsFalse()) {
2776 info = ALWAYS_FALSE;
2777 }
2778 }
2779 }
2780
2781 Label loop, entry;
2782
2783 // init
2784 if (node->init() != NULL) {
2785 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2786 Visit(node->init());
2787 }
2788 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
2789 __ b(&entry);
2790 }
2791
2792 // body
2793 __ bind(&loop);
2794 Visit(node->body());
2795
2796 // next
2797 __ bind(node->continue_target());
2798 if (node->next() != NULL) {
2799 // Record source position of the statement as this code which is after the
2800 // code for the body actually belongs to the loop statement and not the
2801 // body.
2802 if (FLAG_debug_info) __ RecordPosition(node->statement_pos());
2803 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2804 Visit(node->next());
2805 }
2806
2807 // cond
2808 __ bind(&entry);
2809 switch (info) {
2810 case ALWAYS_TRUE:
2811 CheckStack(); // TODO(1222600): ignore if body contains calls.
2812 __ b(&loop);
2813 break;
2814 case ALWAYS_FALSE:
2815 break;
2816 case DONT_KNOW:
2817 CheckStack(); // TODO(1222600): ignore if body contains calls.
2818 LoadCondition(node->cond(),
2819 CodeGenState::LOAD,
2820 &loop,
2821 node->break_target(),
2822 true);
2823 Branch(true, &loop);
2824 break;
2825 }
2826
2827 // exit
2828 __ bind(node->break_target());
2829}
2830
2831
2832void ArmCodeGenerator::VisitForInStatement(ForInStatement* node) {
2833 Comment cmnt(masm_, "[ ForInStatement");
2834 if (FLAG_debug_info) RecordStatementPosition(node);
2835
2836 // We keep stuff on the stack while the body is executing.
2837 // Record it, so that a break/continue crossing this statement
2838 // can restore the stack.
2839 const int kForInStackSize = 5 * kPointerSize;
2840 break_stack_height_ += kForInStackSize;
2841 node->set_break_stack_height(break_stack_height_);
2842
2843 Label loop, next, entry, cleanup, exit, primitive, jsobject;
2844 Label filter_key, end_del_check, fixed_array, non_string;
2845
2846 // Get the object to enumerate over (converted to JSObject).
2847 Load(node->enumerable());
mads.s.ager31e71382008-08-13 09:32:07 +00002848 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849
2850 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2851 // to the specification. 12.6.4 mandates a call to ToObject.
2852 __ cmp(r0, Operand(Factory::undefined_value()));
2853 __ b(eq, &exit);
2854 __ cmp(r0, Operand(Factory::null_value()));
2855 __ b(eq, &exit);
2856
2857 // Stack layout in body:
2858 // [iteration counter (Smi)]
2859 // [length of array]
2860 // [FixedArray]
2861 // [Map or 0]
2862 // [Object]
2863
2864 // Check if enumerable is already a JSObject
2865 __ tst(r0, Operand(kSmiTagMask));
2866 __ b(eq, &primitive);
2867 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2868 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002869 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870 __ b(hs, &jsobject);
2871
2872 __ bind(&primitive);
mads.s.ager31e71382008-08-13 09:32:07 +00002873 __ push(r0);
2874 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002875 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002876
2877
2878 __ bind(&jsobject);
2879
2880 // Get the set of properties (as a FixedArray or Map).
2881 __ push(r0); // duplicate the object being enumerated
mads.s.ager31e71382008-08-13 09:32:07 +00002882 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
2884
2885 // If we got a Map, we can do a fast modification check.
2886 // Otherwise, we got a FixedArray, and we have to do a slow check.
2887 __ mov(r2, Operand(r0));
2888 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
2889 __ cmp(r1, Operand(Factory::meta_map()));
2890 __ b(ne, &fixed_array);
2891
2892 // Get enum cache
2893 __ mov(r1, Operand(r0));
2894 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2895 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2896 __ ldr(r2,
2897 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2898
mads.s.ager31e71382008-08-13 09:32:07 +00002899 __ push(r0); // map
2900 __ push(r2); // enum cache bridge cache
2901 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00002903 __ push(r0);
2904 __ mov(r0, Operand(Smi::FromInt(0)));
2905 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 __ b(&entry);
2907
2908
2909 __ bind(&fixed_array);
2910
2911 __ mov(r1, Operand(Smi::FromInt(0)));
2912 __ push(r1); // insert 0 in place of Map
mads.s.ager31e71382008-08-13 09:32:07 +00002913 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914
2915 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002916 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
mads.s.ager31e71382008-08-13 09:32:07 +00002918 __ push(r0);
2919 __ mov(r0, Operand(Smi::FromInt(0))); // init index
2920 __ push(r0);
2921
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 __ b(&entry);
2923
2924 // Body.
2925 __ bind(&loop);
2926 Visit(node->body());
2927
2928 // Next.
2929 __ bind(node->continue_target());
2930 __ bind(&next);
mads.s.ager31e71382008-08-13 09:32:07 +00002931 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932 __ add(r0, r0, Operand(Smi::FromInt(1)));
mads.s.ager31e71382008-08-13 09:32:07 +00002933 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002934
2935 // Condition.
2936 __ bind(&entry);
2937
mads.s.ager31e71382008-08-13 09:32:07 +00002938 // sp[0] : index
2939 // sp[1] : array/enum cache length
2940 // sp[2] : array or enum cache
2941 // sp[3] : 0 or map
2942 // sp[4] : enumerable
2943 __ ldr(r0, MemOperand(sp, 0 * kPointerSize)); // load the current count
2944 __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // load the length
2945 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946 __ b(hs, &cleanup);
2947
mads.s.ager31e71382008-08-13 09:32:07 +00002948 __ ldr(r0, MemOperand(sp, 0 * kPointerSize));
2949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950 // Get the i'th entry of the array.
mads.s.ager31e71382008-08-13 09:32:07 +00002951 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2953 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2954
2955 // Get Map or 0.
mads.s.ager31e71382008-08-13 09:32:07 +00002956 __ ldr(r2, MemOperand(sp, 3 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 // Check if this (still) matches the map of the enumerable.
2958 // If not, we have to filter the key.
mads.s.ager31e71382008-08-13 09:32:07 +00002959 __ ldr(r1, MemOperand(sp, 4 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2961 __ cmp(r1, Operand(r2));
2962 __ b(eq, &end_del_check);
2963
2964 // Convert the entry to a string (or null if it isn't a property anymore).
mads.s.ager31e71382008-08-13 09:32:07 +00002965 __ ldr(r0, MemOperand(sp, 4 * kPointerSize)); // push enumerable
2966 __ push(r0);
2967 __ push(r3); // push entry
2968 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002969 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971
2972 // If the property has been removed while iterating, we just skip it.
2973 __ cmp(r3, Operand(Factory::null_value()));
2974 __ b(eq, &next);
2975
2976
2977 __ bind(&end_del_check);
2978
2979 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00002980 // r3: i'th entry of the enum cache (or string there of)
2981 __ push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 { Reference each(this, node->each());
2983 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002984 if (each.size() > 0) {
2985 __ ldr(r0, MemOperand(sp, kPointerSize * each.size()));
2986 __ push(r0);
2987 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988 SetValue(&each);
mads.s.ager31e71382008-08-13 09:32:07 +00002989 if (each.size() > 0) {
2990 __ pop(r0); // discard the value
2991 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 }
2993 }
mads.s.ager31e71382008-08-13 09:32:07 +00002994 __ pop(); // pop the i'th entry pushed above
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995 CheckStack(); // TODO(1222600): ignore if body contains calls.
2996 __ jmp(&loop);
2997
2998 // Cleanup.
2999 __ bind(&cleanup);
3000 __ bind(node->break_target());
mads.s.ager31e71382008-08-13 09:32:07 +00003001 __ add(sp, sp, Operand(5 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002
3003 // Exit.
3004 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005
3006 break_stack_height_ -= kForInStackSize;
3007}
3008
3009
3010void ArmCodeGenerator::VisitTryCatch(TryCatch* node) {
3011 Comment cmnt(masm_, "[ TryCatch");
3012
3013 Label try_block, exit;
3014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 __ bl(&try_block);
3016
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017 // --- Catch block ---
3018
3019 // Store the caught exception in the catch variable.
mads.s.ager31e71382008-08-13 09:32:07 +00003020 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 { Reference ref(this, node->catch_var());
3022 // Load the exception to the top of the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003023 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
3024 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003025 SetValue(&ref);
mads.s.ager31e71382008-08-13 09:32:07 +00003026 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 }
3028
3029 // Remove the exception from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003030 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003031
3032 VisitStatements(node->catch_block()->statements());
3033 __ b(&exit);
3034
3035
3036 // --- Try block ---
3037 __ bind(&try_block);
3038
3039 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
3040
3041 // Introduce shadow labels for all escapes from the try block,
3042 // including returns. We should probably try to unify the escaping
3043 // labels and the return label.
3044 int nof_escapes = node->escaping_labels()->length();
3045 List<LabelShadow*> shadows(1 + nof_escapes);
3046 shadows.Add(new LabelShadow(&function_return_));
3047 for (int i = 0; i < nof_escapes; i++) {
3048 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
3049 }
3050
3051 // Generate code for the statements in the try block.
3052 VisitStatements(node->try_block()->statements());
mads.s.ager31e71382008-08-13 09:32:07 +00003053 __ pop(r0); // Discard the result.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054
3055 // Stop the introduced shadowing and count the number of required unlinks.
3056 int nof_unlinks = 0;
3057 for (int i = 0; i <= nof_escapes; i++) {
3058 shadows[i]->StopShadowing();
3059 if (shadows[i]->is_linked()) nof_unlinks++;
3060 }
3061
3062 // Unlink from try chain.
3063 // TOS contains code slot
3064 const int kNextOffset = StackHandlerConstants::kNextOffset +
3065 StackHandlerConstants::kAddressDisplacement;
3066 __ ldr(r1, MemOperand(sp, kNextOffset)); // read next_sp
3067 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3068 __ str(r1, MemOperand(r3));
3069 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
3070 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
3071 // Code slot popped.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072 if (nof_unlinks > 0) __ b(&exit);
3073
3074 // Generate unlink code for all used shadow labels.
3075 for (int i = 0; i <= nof_escapes; i++) {
3076 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00003077 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 __ bind(shadows[i]);
3079
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080 // Reload sp from the top handler, because some statements that we
3081 // break from (eg, for...in) may have left stuff on the stack.
3082 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3083 __ ldr(sp, MemOperand(r3));
3084
3085 __ ldr(r1, MemOperand(sp, kNextOffset));
3086 __ str(r1, MemOperand(r3));
3087 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
3088 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
3089 // Code slot popped.
3090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003091 __ b(shadows[i]->shadowed());
3092 }
3093 }
3094
3095 __ bind(&exit);
3096}
3097
3098
3099void ArmCodeGenerator::VisitTryFinally(TryFinally* node) {
3100 Comment cmnt(masm_, "[ TryFinally");
3101
3102 // State: Used to keep track of reason for entering the finally
3103 // block. Should probably be extended to hold information for
3104 // break/continue from within the try block.
3105 enum { FALLING, THROWING, JUMPING };
3106
3107 Label exit, unlink, try_block, finally_block;
3108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 __ bl(&try_block);
3110
mads.s.ager31e71382008-08-13 09:32:07 +00003111 __ push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112 // In case of thrown exceptions, this is where we continue.
3113 __ mov(r2, Operand(Smi::FromInt(THROWING)));
3114 __ b(&finally_block);
3115
3116
3117 // --- Try block ---
3118 __ bind(&try_block);
3119
3120 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
3121
3122 // Introduce shadow labels for all escapes from the try block,
3123 // including returns. We should probably try to unify the escaping
3124 // labels and the return label.
3125 int nof_escapes = node->escaping_labels()->length();
3126 List<LabelShadow*> shadows(1 + nof_escapes);
3127 shadows.Add(new LabelShadow(&function_return_));
3128 for (int i = 0; i < nof_escapes; i++) {
3129 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
3130 }
3131
3132 // Generate code for the statements in the try block.
3133 VisitStatements(node->try_block()->statements());
3134
3135 // Stop the introduced shadowing and count the number of required
3136 // unlinks.
3137 int nof_unlinks = 0;
3138 for (int i = 0; i <= nof_escapes; i++) {
3139 shadows[i]->StopShadowing();
3140 if (shadows[i]->is_linked()) nof_unlinks++;
3141 }
3142
3143 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00003144 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
3145 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 __ mov(r2, Operand(Smi::FromInt(FALLING)));
3147 if (nof_unlinks > 0) __ b(&unlink);
3148
3149 // Generate code that sets the state for all used shadow labels.
3150 for (int i = 0; i <= nof_escapes; i++) {
3151 if (shadows[i]->is_linked()) {
3152 __ bind(shadows[i]);
mads.s.ager31e71382008-08-13 09:32:07 +00003153 if (shadows[i]->shadowed() == &function_return_) {
3154 __ push(r0); // Materialize the return value on the stack
3155 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156 // Fake TOS for break and continue (not return).
mads.s.ager31e71382008-08-13 09:32:07 +00003157 __ mov(r0, Operand(Factory::undefined_value()));
3158 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159 }
3160 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
3161 __ b(&unlink);
3162 }
3163 }
3164
mads.s.ager31e71382008-08-13 09:32:07 +00003165 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003166 __ bind(&unlink);
3167
mads.s.ager31e71382008-08-13 09:32:07 +00003168 __ pop(r0); // Store TOS in r0 across stack manipulation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 // Reload sp from the top handler, because some statements that we
3170 // break from (eg, for...in) may have left stuff on the stack.
3171 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3172 __ ldr(sp, MemOperand(r3));
3173 const int kNextOffset = StackHandlerConstants::kNextOffset +
3174 StackHandlerConstants::kAddressDisplacement;
3175 __ ldr(r1, MemOperand(sp, kNextOffset));
3176 __ str(r1, MemOperand(r3));
3177 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
3178 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
3179 // Code slot popped.
mads.s.ager31e71382008-08-13 09:32:07 +00003180 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003181
3182 // --- Finally block ---
3183 __ bind(&finally_block);
3184
3185 // Push the state on the stack. If necessary move the state to a
3186 // local variable to avoid having extra values on the stack while
3187 // evaluating the finally block.
mads.s.ager31e71382008-08-13 09:32:07 +00003188 __ push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189 if (node->finally_var() != NULL) {
3190 Reference target(this, node->finally_var());
3191 SetValue(&target);
3192 ASSERT(target.size() == 0); // no extra stuff on the stack
mads.s.ager31e71382008-08-13 09:32:07 +00003193 __ pop(); // remove the extra avalue that was pushed above
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194 }
3195
3196 // Generate code for the statements in the finally block.
3197 VisitStatements(node->finally_block()->statements());
3198
mads.s.ager31e71382008-08-13 09:32:07 +00003199 // Get the state from the stack - or the local variable.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003200 if (node->finally_var() != NULL) {
3201 Reference target(this, node->finally_var());
3202 GetValue(&target);
3203 }
mads.s.ager31e71382008-08-13 09:32:07 +00003204 __ pop(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003205
mads.s.ager31e71382008-08-13 09:32:07 +00003206 __ pop(r0); // Restore value or faked TOS.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 // Generate code that jumps to the right destination for all used
3208 // shadow labels.
3209 for (int i = 0; i <= nof_escapes; i++) {
3210 if (shadows[i]->is_bound()) {
3211 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
3212 if (shadows[i]->shadowed() != &function_return_) {
3213 Label next;
3214 __ b(ne, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003215 __ b(shadows[i]->shadowed());
3216 __ bind(&next);
3217 } else {
3218 __ b(eq, shadows[i]->shadowed());
3219 }
3220 }
3221 }
3222
3223 // Check if we need to rethrow the exception.
3224 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
3225 __ b(ne, &exit);
3226
3227 // Rethrow exception.
mads.s.ager31e71382008-08-13 09:32:07 +00003228 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229 __ CallRuntime(Runtime::kReThrow, 1);
3230
3231 // Done.
3232 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003233}
3234
3235
3236void ArmCodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
3237 Comment cmnt(masm_, "[ DebuggerStatament");
3238 if (FLAG_debug_info) RecordStatementPosition(node);
3239 __ CallRuntime(Runtime::kDebugBreak, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003240 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241}
3242
3243
3244void ArmCodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
3245 ASSERT(boilerplate->IsBoilerplate());
3246
3247 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003248 __ mov(r0, Operand(boilerplate));
3249 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003250
3251 // Create a new closure.
mads.s.ager31e71382008-08-13 09:32:07 +00003252 __ push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253 __ CallRuntime(Runtime::kNewClosure, 2);
mads.s.ager31e71382008-08-13 09:32:07 +00003254 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003255}
3256
3257
3258void ArmCodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
3259 Comment cmnt(masm_, "[ FunctionLiteral");
3260
3261 // Build the function boilerplate and instantiate it.
3262 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00003263 // Check for stack-overflow exception.
3264 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003265 InstantiateBoilerplate(boilerplate);
3266}
3267
3268
3269void ArmCodeGenerator::VisitFunctionBoilerplateLiteral(
3270 FunctionBoilerplateLiteral* node) {
3271 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
3272 InstantiateBoilerplate(node->boilerplate());
3273}
3274
3275
3276void ArmCodeGenerator::VisitConditional(Conditional* node) {
3277 Comment cmnt(masm_, "[ Conditional");
3278 Label then, else_, exit;
3279 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
3280 Branch(false, &else_);
3281 __ bind(&then);
3282 Load(node->then_expression(), access());
3283 __ b(&exit);
3284 __ bind(&else_);
3285 Load(node->else_expression(), access());
3286 __ bind(&exit);
3287}
3288
3289
3290void ArmCodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003291 ASSERT(access() != CodeGenState::UNDEFINED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292 Comment cmnt(masm_, "[ Slot");
3293
3294 if (node->type() == Slot::LOOKUP) {
3295 ASSERT(node->var()->mode() == Variable::DYNAMIC);
3296
3297 // For now, just do a runtime call.
mads.s.ager31e71382008-08-13 09:32:07 +00003298 __ push(cp);
3299 __ mov(r0, Operand(node->var()->name()));
3300 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003302 if (access() == CodeGenState::LOAD) {
3303 __ CallRuntime(Runtime::kLoadContextSlot, 2);
3304 } else {
3305 ASSERT(access() == CodeGenState::LOAD_TYPEOF_EXPR);
3306 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003308 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003309
3310 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003311 // Note: We would like to keep the assert below, but it fires because of
3312 // some nasty code in LoadTypeofExpression() which should be removed...
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313 // ASSERT(node->var()->mode() != Variable::DYNAMIC);
3314
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003315 // Special handling for locals allocated in registers.
3316 __ ldr(r0, SlotOperand(node, r2));
3317 __ push(r0);
3318 if (node->var()->mode() == Variable::CONST) {
3319 // Const slots may contain 'the hole' value (the constant hasn't been
3320 // initialized yet) which needs to be converted into the 'undefined'
3321 // value.
3322 Comment cmnt(masm_, "[ Unhole const");
3323 __ pop(r0);
3324 __ cmp(r0, Operand(Factory::the_hole_value()));
3325 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
3326 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327 }
3328 }
3329}
3330
3331
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003332void ArmCodeGenerator::VisitVariableProxy(VariableProxy* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003333 Comment cmnt(masm_, "[ VariableProxy");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003334 Variable* var_node = node->var();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003336 Expression* expr = var_node->rewrite();
3337 if (expr != NULL) {
3338 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003340 ASSERT(var_node->is_global());
3341 if (is_referenced()) {
3342 if (var_node->AsProperty() != NULL) {
3343 __ RecordPosition(var_node->AsProperty()->position());
3344 }
3345 GetReferenceProperty(new Literal(var_node->name()));
3346 } else {
3347 Reference property(this, node);
3348 GetValue(&property);
3349 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 }
3351}
3352
3353
3354void ArmCodeGenerator::VisitLiteral(Literal* node) {
3355 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00003356 __ mov(r0, Operand(node->handle()));
3357 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358}
3359
3360
3361void ArmCodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
3362 Comment cmnt(masm_, "[ RexExp Literal");
3363
3364 // Retrieve the literal array and check the allocated entry.
3365
3366 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003367 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003368
3369 // Load the literals array of the function.
3370 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3371
3372 // Load the literal at the ast saved index.
3373 int literal_offset =
3374 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3375 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3376
3377 Label done;
3378 __ cmp(r2, Operand(Factory::undefined_value()));
3379 __ b(ne, &done);
3380
3381 // If the entry is undefined we call the runtime system to computed
3382 // the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00003383 __ push(r1); // literal array (0)
3384 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
3385 __ push(r0); // literal index (1)
3386 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
3387 __ push(r0);
3388 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
3389 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003391 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003392
mads.s.ager31e71382008-08-13 09:32:07 +00003393 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 // Push the literal.
mads.s.ager31e71382008-08-13 09:32:07 +00003395 __ push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396}
3397
3398
3399// This deferred code stub will be used for creating the boilerplate
3400// by calling Runtime_CreateObjectLiteral.
3401// Each created boilerplate is stored in the JSFunction and they are
3402// therefore context dependent.
3403class ObjectLiteralDeferred: public DeferredCode {
3404 public:
3405 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
3406 : DeferredCode(generator), node_(node) {
3407 set_comment("[ ObjectLiteralDeferred");
3408 }
3409 virtual void Generate();
3410 private:
3411 ObjectLiteral* node_;
3412};
3413
3414
3415void ObjectLiteralDeferred::Generate() {
3416 // If the entry is undefined we call the runtime system to computed
3417 // the literal.
3418
3419 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00003420 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003421 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00003422 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
3423 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003424 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00003425 __ mov(r0, Operand(node_->constant_properties()));
3426 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00003428 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003429}
3430
3431
3432void ArmCodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
3433 Comment cmnt(masm_, "[ ObjectLiteral");
3434
3435 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
3436
3437 // Retrieve the literal array and check the allocated entry.
3438
3439 // Load the function of this activation.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003440 __ ldr(r1, FunctionOperand());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441
3442 // Load the literals array of the function.
3443 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3444
3445 // Load the literal at the ast saved index.
3446 int literal_offset =
3447 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3448 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3449
3450 // Check whether we need to materialize the object literal boilerplate.
3451 // If so, jump to the deferred code.
3452 __ cmp(r2, Operand(Factory::undefined_value()));
3453 __ b(eq, deferred->enter());
3454 __ bind(deferred->exit());
3455
3456 // Push the object literal boilerplate.
mads.s.ager31e71382008-08-13 09:32:07 +00003457 __ push(r2);
3458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459 // Clone the boilerplate object.
3460 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003461 __ push(r0); // save the result
3462 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463
3464 for (int i = 0; i < node->properties()->length(); i++) {
3465 ObjectLiteral::Property* property = node->properties()->at(i);
3466 Literal* key = property->key();
3467 Expression* value = property->value();
3468 switch (property->kind()) {
3469 case ObjectLiteral::Property::CONSTANT: break;
3470 case ObjectLiteral::Property::COMPUTED: // fall through
3471 case ObjectLiteral::Property::PROTOTYPE: {
mads.s.ager31e71382008-08-13 09:32:07 +00003472 __ push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 Load(key);
3474 Load(value);
3475 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00003476 // restore r0
3477 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478 break;
3479 }
3480 case ObjectLiteral::Property::SETTER: {
3481 __ push(r0);
3482 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003483 __ mov(r0, Operand(Smi::FromInt(1)));
3484 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485 Load(value);
3486 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003487 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488 break;
3489 }
3490 case ObjectLiteral::Property::GETTER: {
3491 __ push(r0);
3492 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003493 __ mov(r0, Operand(Smi::FromInt(0)));
3494 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003495 Load(value);
3496 __ CallRuntime(Runtime::kDefineAccessor, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003497 __ ldr(r0, MemOperand(sp, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498 break;
3499 }
3500 }
3501 }
3502}
3503
3504
3505void ArmCodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
3506 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003507
3508 // Call runtime to create the array literal.
3509 __ mov(r0, Operand(node->literals()));
3510 __ push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003511 // Load the function of this frame.
3512 __ ldr(r0, FunctionOperand());
3513 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003514 __ push(r0);
3515 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
3516
3517 // Push the resulting array literal on the stack.
3518 __ push(r0);
3519
3520 // Generate code to set the elements in the array that are not
3521 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522 for (int i = 0; i < node->values()->length(); i++) {
3523 Expression* value = node->values()->at(i);
3524
3525 // If value is literal the property value is already
3526 // set in the boilerplate object.
3527 if (value->AsLiteral() == NULL) {
3528 // The property must be set by generated code.
3529 Load(value);
mads.s.ager31e71382008-08-13 09:32:07 +00003530 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003531
3532 // Fetch the object literal
3533 __ ldr(r1, MemOperand(sp, 0));
3534 // Get the elements array.
3535 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3536
3537 // Write to the indexed properties array.
3538 int offset = i * kPointerSize + Array::kHeaderSize;
3539 __ str(r0, FieldMemOperand(r1, offset));
3540
3541 // Update the write barrier for the array address.
3542 __ mov(r3, Operand(offset));
3543 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003544 }
3545 }
3546}
3547
3548
3549void ArmCodeGenerator::VisitAssignment(Assignment* node) {
3550 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 if (FLAG_debug_info) RecordStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 Reference target(this, node->target());
3554 if (target.is_illegal()) return;
3555
3556 if (node->op() == Token::ASSIGN ||
3557 node->op() == Token::INIT_VAR ||
3558 node->op() == Token::INIT_CONST) {
3559 Load(node->value());
3560
3561 } else {
3562 GetValue(&target);
3563 Literal* literal = node->value()->AsLiteral();
3564 if (literal != NULL && literal->handle()->IsSmi()) {
3565 SmiOperation(node->binary_op(), literal->handle(), false);
mads.s.ager31e71382008-08-13 09:32:07 +00003566 __ push(r0);
3567
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003568 } else {
3569 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00003570 GenericBinaryOperation(node->binary_op());
mads.s.ager31e71382008-08-13 09:32:07 +00003571 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 }
3573 }
3574
3575 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3576 if (var != NULL &&
3577 (var->mode() == Variable::CONST) &&
3578 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3579 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581 } else {
3582 __ RecordPosition(node->position());
3583 if (node->op() == Token::INIT_CONST) {
3584 // Dynamic constant initializations must use the function context
3585 // and initialize the actual constant declared. Dynamic variable
3586 // initializations are simply assignments and use SetValue.
3587 InitConst(&target);
3588 } else {
3589 SetValue(&target);
3590 }
3591 }
3592}
3593
3594
3595void ArmCodeGenerator::VisitThrow(Throw* node) {
3596 Comment cmnt(masm_, "[ Throw");
3597
3598 Load(node->exception());
3599 __ RecordPosition(node->position());
3600 __ CallRuntime(Runtime::kThrow, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003601 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003602}
3603
3604
3605void ArmCodeGenerator::VisitProperty(Property* node) {
3606 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 if (is_referenced()) {
3609 __ RecordPosition(node->position());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003610 GetReferenceProperty(node->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611 } else {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612 Reference property(this, node);
3613 __ RecordPosition(node->position());
3614 GetValue(&property);
3615 }
3616}
3617
3618
3619void ArmCodeGenerator::VisitCall(Call* node) {
3620 Comment cmnt(masm_, "[ Call");
3621
3622 ZoneList<Expression*>* args = node->arguments();
3623
3624 if (FLAG_debug_info) RecordStatementPosition(node);
3625 // Standard function call.
3626
3627 // Check if the function is a variable or a property.
3628 Expression* function = node->expression();
3629 Variable* var = function->AsVariableProxy()->AsVariable();
3630 Property* property = function->AsProperty();
3631
3632 // ------------------------------------------------------------------------
3633 // Fast-case: Use inline caching.
3634 // ---
3635 // According to ECMA-262, section 11.2.3, page 44, the function to call
3636 // must be resolved after the arguments have been evaluated. The IC code
3637 // automatically handles this by loading the arguments before the function
3638 // is resolved in cache misses (this also holds for megamorphic calls).
3639 // ------------------------------------------------------------------------
3640
3641 if (var != NULL && !var->is_this() && var->is_global()) {
3642 // ----------------------------------
3643 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3644 // ----------------------------------
3645
3646 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003647 __ mov(r0, Operand(var->name()));
3648 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003649 LoadGlobal();
3650
3651 // Load the arguments.
3652 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653
3654 // Setup the receiver register and call the IC initialization code.
3655 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00003657 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003658 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003659 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003660 __ pop();
3661 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662
3663 } else if (var != NULL && var->slot() != NULL &&
3664 var->slot()->type() == Slot::LOOKUP) {
3665 // ----------------------------------
3666 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3667 // ----------------------------------
3668
3669 // Load the function
mads.s.ager31e71382008-08-13 09:32:07 +00003670 __ push(cp);
3671 __ mov(r0, Operand(var->name()));
3672 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673 __ CallRuntime(Runtime::kLoadContextSlot, 2);
3674 // r0: slot value; r1: receiver
3675
3676 // Load the receiver.
mads.s.ager31e71382008-08-13 09:32:07 +00003677 __ push(r0); // function
3678 __ push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003679
3680 // Call the function.
3681 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00003682 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683
3684 } else if (property != NULL) {
3685 // Check if the key is a literal string.
3686 Literal* literal = property->key()->AsLiteral();
3687
3688 if (literal != NULL && literal->handle()->IsSymbol()) {
3689 // ------------------------------------------------------------------
3690 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3691 // ------------------------------------------------------------------
3692
3693 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003694 __ mov(r0, Operand(literal->handle()));
3695 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696 Load(property->obj());
3697
3698 // Load the arguments.
3699 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003700
3701 // Set the receiver register and call the IC initialization code.
3702 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 __ RecordPosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00003704 __ Call(stub, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3706
3707 // Remove the function from the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003708 __ pop();
3709
3710 __ push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711
3712 } else {
3713 // -------------------------------------------
3714 // JavaScript example: 'array[index](1, 2, 3)'
3715 // -------------------------------------------
3716
3717 // Load the function to call from the property through a reference.
3718 Reference ref(this, property);
mads.s.ager31e71382008-08-13 09:32:07 +00003719 GetValue(&ref); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003720
3721 // Pass receiver to called function.
mads.s.ager31e71382008-08-13 09:32:07 +00003722 __ ldr(r0, MemOperand(sp, ref.size() * kPointerSize));
3723 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 // Call the function.
3725 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00003726 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 }
3728
3729 } else {
3730 // ----------------------------------
3731 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3732 // ----------------------------------
3733
3734 // Load the function.
3735 Load(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736 // Pass the global object as the receiver.
3737 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003738 // Call the function.
3739 CallWithArguments(args, node->position());
mads.s.ager31e71382008-08-13 09:32:07 +00003740 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741 }
3742}
3743
3744
3745void ArmCodeGenerator::VisitCallNew(CallNew* node) {
3746 Comment cmnt(masm_, "[ CallNew");
3747
3748 // According to ECMA-262, section 11.2.2, page 44, the function
3749 // expression in new calls must be evaluated before the
3750 // arguments. This is different from ordinary calls, where the
3751 // actual function to call is resolved after the arguments have been
3752 // evaluated.
3753
3754 // Compute function to call and use the global object as the
3755 // receiver.
3756 Load(node->expression());
3757 LoadGlobal();
3758
3759 // Push the arguments ("left-to-right") on the stack.
3760 ZoneList<Expression*>* args = node->arguments();
3761 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3762
mads.s.ager31e71382008-08-13 09:32:07 +00003763 // r0: the number of arguments.
3764 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003766 // Load the function into r1 as per calling convention.
3767 __ ldr(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
3768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769 // Call the construct call builtin that handles allocation and
3770 // constructor invocation.
ager@chromium.org236ad962008-09-25 09:45:57 +00003771 __ RecordPosition(RelocInfo::POSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00003773 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00003774
3775 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
3776 __ str(r0, MemOperand(sp, 0 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777}
3778
3779
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003780void ArmCodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
3781 ASSERT(args->length() == 1);
3782 Label leave;
3783 Load(args->at(0));
mads.s.ager31e71382008-08-13 09:32:07 +00003784 __ pop(r0); // r0 contains object.
3785 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786 __ tst(r0, Operand(kSmiTagMask));
3787 __ b(eq, &leave);
3788 // It is a heap object - get map.
3789 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3790 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003791 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792 __ cmp(r1, Operand(JS_VALUE_TYPE));
3793 __ b(ne, &leave);
3794 // Load the value.
3795 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
3796 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00003797 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798}
3799
3800
3801void ArmCodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
3802 ASSERT(args->length() == 2);
3803 Label leave;
3804 Load(args->at(0)); // Load the object.
3805 Load(args->at(1)); // Load the value.
mads.s.ager31e71382008-08-13 09:32:07 +00003806 __ pop(r0); // r0 contains value
3807 __ pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808 // if (object->IsSmi()) return object.
3809 __ tst(r1, Operand(kSmiTagMask));
3810 __ b(eq, &leave);
3811 // It is a heap object - get map.
3812 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3813 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3814 // if (!object->IsJSValue()) return object.
3815 __ cmp(r2, Operand(JS_VALUE_TYPE));
3816 __ b(ne, &leave);
3817 // Store the value.
3818 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3819 // Update the write barrier.
3820 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3821 __ RecordWrite(r1, r2, r3);
3822 // Leave.
3823 __ bind(&leave);
mads.s.ager31e71382008-08-13 09:32:07 +00003824 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825}
3826
3827
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003828void ArmCodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
3829 ASSERT(args->length() == 1);
3830 Load(args->at(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003831 __ pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003832 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 cc_reg_ = eq;
3834}
3835
3836
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003837void ArmCodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
3838 ASSERT(args->length() == 1);
3839 Load(args->at(0));
3840 __ pop(r0);
3841 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3842 cc_reg_ = eq;
3843}
3844
3845
kasper.lund7276f142008-07-30 08:49:36 +00003846// This should generate code that performs a charCodeAt() call or returns
3847// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3848// It is not yet implemented on ARM, so it always goes to the slow case.
3849void ArmCodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
3850 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003851 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +00003852 __ push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003853}
3854
3855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856void ArmCodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003857 ASSERT(args->length() == 1);
3858 Load(args->at(0));
3859 Label answer;
3860 // We need the CC bits to come out as not_equal in the case where the
3861 // object is a smi. This can't be done with the usual test opcode so
3862 // we use XOR to get the right CC bits.
3863 __ pop(r0);
3864 __ and_(r1, r0, Operand(kSmiTagMask));
3865 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3866 __ b(ne, &answer);
3867 // It is a heap object - get the map.
3868 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3869 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3870 // Check if the object is a JS array or not.
3871 __ cmp(r1, Operand(JS_ARRAY_TYPE));
3872 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873 cc_reg_ = eq;
3874}
3875
3876
3877void ArmCodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
3878 ASSERT(args->length() == 0);
3879
mads.s.ager31e71382008-08-13 09:32:07 +00003880 // Seed the result with the formal parameters count, which will be used
3881 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3883
3884 // Call the shared stub to get to the arguments.length.
3885 ArgumentsAccessStub stub(true);
3886 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00003887 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888}
3889
3890
3891void ArmCodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
3892 ASSERT(args->length() == 1);
3893
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003894 // Satisfy contract with ArgumentsAccessStub:
3895 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 Load(args->at(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003897 __ pop(r1);
3898 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899
3900 // Call the shared stub to get to arguments[key].
3901 ArgumentsAccessStub stub(false);
3902 __ CallStub(&stub);
mads.s.ager31e71382008-08-13 09:32:07 +00003903 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904}
3905
3906
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003907void ArmCodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
3908 ASSERT(args->length() == 2);
3909
3910 // Load the two objects into registers and perform the comparison.
3911 Load(args->at(0));
3912 Load(args->at(1));
3913 __ pop(r0);
3914 __ pop(r1);
3915 __ cmp(r0, Operand(r1));
3916 cc_reg_ = eq;
3917}
3918
3919
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920void ArmCodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003921 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003922
3923 ZoneList<Expression*>* args = node->arguments();
3924 Comment cmnt(masm_, "[ CallRuntime");
3925 Runtime::Function* function = node->function();
3926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003928 // Push the arguments ("left-to-right").
3929 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 // Call the C runtime function.
3932 __ CallRuntime(function, args->length());
mads.s.ager31e71382008-08-13 09:32:07 +00003933 __ push(r0);
3934
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00003936 // Prepare stack for calling JS runtime function.
3937 __ mov(r0, Operand(node->name()));
3938 __ push(r0);
3939 // Push the builtins object found in the current global object.
3940 __ ldr(r1, GlobalObject());
3941 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
3942 __ push(r0);
3943
3944 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003946 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00003948 __ Call(stub, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003950 __ pop();
3951 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003952 }
3953}
3954
3955
3956void ArmCodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
3957 Comment cmnt(masm_, "[ UnaryOperation");
3958
3959 Token::Value op = node->op();
3960
3961 if (op == Token::NOT) {
3962 LoadCondition(node->expression(),
3963 CodeGenState::LOAD,
3964 false_target(),
3965 true_target(),
3966 true);
3967 cc_reg_ = NegateCondition(cc_reg_);
3968
3969 } else if (op == Token::DELETE) {
3970 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003971 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972 if (property != NULL) {
3973 Load(property->obj());
3974 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00003975 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003976 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977
mads.s.ager31e71382008-08-13 09:32:07 +00003978 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003979 Slot* slot = variable->slot();
3980 if (variable->is_global()) {
3981 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003982 __ mov(r0, Operand(variable->name()));
3983 __ push(r0);
3984 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003985 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986
3987 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3988 // lookup the context holding the named variable
mads.s.ager31e71382008-08-13 09:32:07 +00003989 __ push(cp);
3990 __ mov(r0, Operand(variable->name()));
3991 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003992 __ CallRuntime(Runtime::kLookupContext, 2);
3993 // r0: context
mads.s.ager31e71382008-08-13 09:32:07 +00003994 __ push(r0);
3995 __ mov(r0, Operand(variable->name()));
3996 __ push(r0);
3997 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003998 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999
mads.s.ager31e71382008-08-13 09:32:07 +00004000 } else {
4001 // Default: Result of deleting non-global, not dynamically
4002 // introduced variables is false.
4003 __ mov(r0, Operand(Factory::false_value()));
4004 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004005
4006 } else {
4007 // Default: Result of deleting expressions is true.
4008 Load(node->expression()); // may have side-effects
mads.s.ager31e71382008-08-13 09:32:07 +00004009 __ pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004010 __ mov(r0, Operand(Factory::true_value()));
4011 }
mads.s.ager31e71382008-08-13 09:32:07 +00004012 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004013
4014 } else if (op == Token::TYPEOF) {
4015 // Special case for loading the typeof expression; see comment on
4016 // LoadTypeofExpression().
4017 LoadTypeofExpression(node->expression());
4018 __ CallRuntime(Runtime::kTypeof, 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004019 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020
4021 } else {
4022 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00004023 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 switch (op) {
4025 case Token::NOT:
4026 case Token::DELETE:
4027 case Token::TYPEOF:
4028 UNREACHABLE(); // handled above
4029 break;
4030
4031 case Token::SUB: {
4032 UnarySubStub stub;
4033 __ CallStub(&stub);
4034 break;
4035 }
4036
4037 case Token::BIT_NOT: {
4038 // smi check
4039 Label smi_label;
4040 Label continue_label;
4041 __ tst(r0, Operand(kSmiTagMask));
4042 __ b(eq, &smi_label);
4043
mads.s.ager31e71382008-08-13 09:32:07 +00004044 __ push(r0);
4045 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004046 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004047
4048 __ b(&continue_label);
4049 __ bind(&smi_label);
4050 __ mvn(r0, Operand(r0));
4051 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
4052 __ bind(&continue_label);
4053 break;
4054 }
4055
4056 case Token::VOID:
4057 // since the stack top is cached in r0, popping and then
4058 // pushing a value can be done by just writing to r0.
4059 __ mov(r0, Operand(Factory::undefined_value()));
4060 break;
4061
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004062 case Token::ADD: {
4063 // Smi check.
4064 Label continue_label;
4065 __ tst(r0, Operand(kSmiTagMask));
4066 __ b(eq, &continue_label);
mads.s.ager31e71382008-08-13 09:32:07 +00004067 __ push(r0);
4068 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004069 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004070 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004073 default:
4074 UNREACHABLE();
4075 }
mads.s.ager31e71382008-08-13 09:32:07 +00004076 __ push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077 }
4078}
4079
4080
4081void ArmCodeGenerator::VisitCountOperation(CountOperation* node) {
4082 Comment cmnt(masm_, "[ CountOperation");
4083
4084 bool is_postfix = node->is_postfix();
4085 bool is_increment = node->op() == Token::INC;
4086
4087 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4088 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4089
4090 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004091 if (is_postfix) {
4092 __ mov(r0, Operand(0));
4093 __ push(r0);
4094 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095
4096 { Reference target(this, node->expression());
4097 if (target.is_illegal()) return;
4098 GetValue(&target);
mads.s.ager31e71382008-08-13 09:32:07 +00004099 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004100
4101 Label slow, exit;
4102
4103 // Load the value (1) into register r1.
4104 __ mov(r1, Operand(Smi::FromInt(1)));
4105
4106 // Check for smi operand.
4107 __ tst(r0, Operand(kSmiTagMask));
4108 __ b(ne, &slow);
4109
4110 // Postfix: Store the old value as the result.
4111 if (is_postfix) __ str(r0, MemOperand(sp, target.size() * kPointerSize));
4112
4113 // Perform optimistic increment/decrement.
4114 if (is_increment) {
4115 __ add(r0, r0, Operand(r1), SetCC);
4116 } else {
4117 __ sub(r0, r0, Operand(r1), SetCC);
4118 }
4119
4120 // If the increment/decrement didn't overflow, we're done.
4121 __ b(vc, &exit);
4122
4123 // Revert optimistic increment/decrement.
4124 if (is_increment) {
4125 __ sub(r0, r0, Operand(r1));
4126 } else {
4127 __ add(r0, r0, Operand(r1));
4128 }
4129
4130 // Slow case: Convert to number.
4131 __ bind(&slow);
4132
4133 // Postfix: Convert the operand to a number and store it as the result.
4134 if (is_postfix) {
4135 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
4136 __ CallStub(&stub);
4137 // Store to result (on the stack).
4138 __ str(r0, MemOperand(sp, target.size() * kPointerSize));
4139 }
4140
4141 // Compute the new value by calling the right JavaScript native.
4142 if (is_increment) {
4143 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
4144 __ CallStub(&stub);
4145 } else {
4146 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
4147 __ CallStub(&stub);
4148 }
4149
4150 // Store the new value in the target if not const.
4151 __ bind(&exit);
mads.s.ager31e71382008-08-13 09:32:07 +00004152 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004153 if (!is_const) SetValue(&target);
4154 }
4155
4156 // Postfix: Discard the new value and use the old.
4157 if (is_postfix) __ pop(r0);
4158}
4159
4160
4161void ArmCodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4162 Comment cmnt(masm_, "[ BinaryOperation");
4163 Token::Value op = node->op();
4164
4165 // According to ECMA-262 section 11.11, page 58, the binary logical
4166 // operators must yield the result of one of the two expressions
4167 // before any ToBoolean() conversions. This means that the value
4168 // produced by a && or || operator is not necessarily a boolean.
4169
4170 // NOTE: If the left hand side produces a materialized value (not in
4171 // the CC register), we force the right hand side to do the
4172 // same. This is necessary because we may have to branch to the exit
4173 // after evaluating the left hand side (due to the shortcut
4174 // semantics), but the compiler must (statically) know if the result
4175 // of compiling the binary operation is materialized or not.
4176
4177 if (op == Token::AND) {
4178 Label is_true;
4179 LoadCondition(node->left(),
4180 CodeGenState::LOAD,
4181 &is_true,
4182 false_target(),
4183 false);
4184 if (has_cc()) {
4185 Branch(false, false_target());
4186
4187 // Evaluate right side expression.
4188 __ bind(&is_true);
4189 LoadCondition(node->right(),
4190 CodeGenState::LOAD,
4191 true_target(),
4192 false_target(),
4193 false);
4194
4195 } else {
4196 Label pop_and_continue, exit;
4197
mads.s.ager31e71382008-08-13 09:32:07 +00004198 __ ldr(r0, MemOperand(sp, 0)); // dup the stack top
4199 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004200 // Avoid popping the result if it converts to 'false' using the
4201 // standard ToBoolean() conversion as described in ECMA-262,
4202 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004203 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004204 Branch(false, &exit);
4205
4206 // Pop the result of evaluating the first part.
4207 __ bind(&pop_and_continue);
4208 __ pop(r0);
4209
4210 // Evaluate right side expression.
4211 __ bind(&is_true);
4212 Load(node->right());
4213
4214 // Exit (always with a materialized value).
4215 __ bind(&exit);
4216 }
4217
4218 } else if (op == Token::OR) {
4219 Label is_false;
4220 LoadCondition(node->left(),
4221 CodeGenState::LOAD,
4222 true_target(),
4223 &is_false,
4224 false);
4225 if (has_cc()) {
4226 Branch(true, true_target());
4227
4228 // Evaluate right side expression.
4229 __ bind(&is_false);
4230 LoadCondition(node->right(),
4231 CodeGenState::LOAD,
4232 true_target(),
4233 false_target(),
4234 false);
4235
4236 } else {
4237 Label pop_and_continue, exit;
4238
mads.s.ager31e71382008-08-13 09:32:07 +00004239 __ ldr(r0, MemOperand(sp, 0));
4240 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004241 // Avoid popping the result if it converts to 'true' using the
4242 // standard ToBoolean() conversion as described in ECMA-262,
4243 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004244 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004245 Branch(true, &exit);
4246
4247 // Pop the result of evaluating the first part.
4248 __ bind(&pop_and_continue);
4249 __ pop(r0);
4250
4251 // Evaluate right side expression.
4252 __ bind(&is_false);
4253 Load(node->right());
4254
4255 // Exit (always with a materialized value).
4256 __ bind(&exit);
4257 }
4258
4259 } else {
4260 // Optimize for the case where (at least) one of the expressions
4261 // is a literal small integer.
4262 Literal* lliteral = node->left()->AsLiteral();
4263 Literal* rliteral = node->right()->AsLiteral();
4264
4265 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
4266 Load(node->left());
4267 SmiOperation(node->op(), rliteral->handle(), false);
4268
4269 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
4270 Load(node->right());
4271 SmiOperation(node->op(), lliteral->handle(), true);
4272
4273 } else {
4274 Load(node->left());
4275 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00004276 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004277 }
mads.s.ager31e71382008-08-13 09:32:07 +00004278 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004279 }
4280}
4281
4282
4283void ArmCodeGenerator::VisitThisFunction(ThisFunction* node) {
mads.s.ager31e71382008-08-13 09:32:07 +00004284 __ ldr(r0, FunctionOperand());
4285 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004286}
4287
4288
4289void ArmCodeGenerator::VisitCompareOperation(CompareOperation* node) {
4290 Comment cmnt(masm_, "[ CompareOperation");
4291
4292 // Get the expressions from the node.
4293 Expression* left = node->left();
4294 Expression* right = node->right();
4295 Token::Value op = node->op();
4296
4297 // NOTE: To make null checks efficient, we check if either left or
4298 // right is the literal 'null'. If so, we optimize the code by
4299 // inlining a null check instead of calling the (very) general
4300 // runtime routine for checking equality.
4301
4302 bool left_is_null =
4303 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
4304 bool right_is_null =
4305 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4306
4307 if (op == Token::EQ || op == Token::EQ_STRICT) {
4308 // The 'null' value is only equal to 'null' or 'undefined'.
4309 if (left_is_null || right_is_null) {
4310 Load(left_is_null ? right : left);
4311 Label exit, undetectable;
mads.s.ager31e71382008-08-13 09:32:07 +00004312 __ pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004313 __ cmp(r0, Operand(Factory::null_value()));
4314
4315 // The 'null' value is only equal to 'undefined' if using
4316 // non-strict comparisons.
4317 if (op != Token::EQ_STRICT) {
4318 __ b(eq, &exit);
4319 __ cmp(r0, Operand(Factory::undefined_value()));
4320
4321 // NOTE: it can be undetectable object.
4322 __ b(eq, &exit);
4323 __ tst(r0, Operand(kSmiTagMask));
4324
4325 __ b(ne, &undetectable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004326 __ b(false_target());
4327
4328 __ bind(&undetectable);
4329 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4330 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4331 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4332 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4333 }
4334
4335 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004336
4337 cc_reg_ = eq;
4338 return;
4339 }
4340 }
4341
4342
4343 // NOTE: To make typeof testing for natives implemented in
4344 // JavaScript really efficient, we generate special code for
4345 // expressions of the form: 'typeof <expression> == <string>'.
4346
4347 UnaryOperation* operation = left->AsUnaryOperation();
4348 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4349 (operation != NULL && operation->op() == Token::TYPEOF) &&
4350 (right->AsLiteral() != NULL &&
4351 right->AsLiteral()->handle()->IsString())) {
4352 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4353
mads.s.ager31e71382008-08-13 09:32:07 +00004354 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004355 LoadTypeofExpression(operation->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00004356 __ pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004357
4358 if (check->Equals(Heap::number_symbol())) {
4359 __ tst(r1, Operand(kSmiTagMask));
4360 __ b(eq, true_target());
4361 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4362 __ cmp(r1, Operand(Factory::heap_number_map()));
4363 cc_reg_ = eq;
4364
4365 } else if (check->Equals(Heap::string_symbol())) {
4366 __ tst(r1, Operand(kSmiTagMask));
4367 __ b(eq, false_target());
4368
4369 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4370
4371 // NOTE: it might be an undetectable string object
4372 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4373 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4374 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4375 __ b(eq, false_target());
4376
4377 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4378 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4379 cc_reg_ = lt;
4380
4381 } else if (check->Equals(Heap::boolean_symbol())) {
4382 __ cmp(r1, Operand(Factory::true_value()));
4383 __ b(eq, true_target());
4384 __ cmp(r1, Operand(Factory::false_value()));
4385 cc_reg_ = eq;
4386
4387 } else if (check->Equals(Heap::undefined_symbol())) {
4388 __ cmp(r1, Operand(Factory::undefined_value()));
4389 __ b(eq, true_target());
4390
4391 __ tst(r1, Operand(kSmiTagMask));
4392 __ b(eq, false_target());
4393
4394 // NOTE: it can be undetectable object.
4395 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4396 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4397 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4398 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4399
4400 cc_reg_ = eq;
4401
4402 } else if (check->Equals(Heap::function_symbol())) {
4403 __ tst(r1, Operand(kSmiTagMask));
4404 __ b(eq, false_target());
4405 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4406 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4407 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4408 cc_reg_ = eq;
4409
4410 } else if (check->Equals(Heap::object_symbol())) {
4411 __ tst(r1, Operand(kSmiTagMask));
4412 __ b(eq, false_target());
4413
4414 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4415 __ cmp(r1, Operand(Factory::null_value()));
4416 __ b(eq, true_target());
4417
4418 // NOTE: it might be an undetectable object.
4419 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4420 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4421 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
4422 __ b(eq, false_target());
4423
4424 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4425 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
4426 __ b(lt, false_target());
4427 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4428 cc_reg_ = le;
4429
4430 } else {
4431 // Uncommon case: Typeof testing against a string literal that
4432 // is never returned from the typeof operator.
4433 __ b(false_target());
4434 }
4435 return;
4436 }
4437
4438 Load(left);
4439 Load(right);
4440 switch (op) {
4441 case Token::EQ:
4442 Comparison(eq, false);
4443 break;
4444
4445 case Token::LT:
4446 Comparison(lt);
4447 break;
4448
4449 case Token::GT:
4450 Comparison(gt);
4451 break;
4452
4453 case Token::LTE:
4454 Comparison(le);
4455 break;
4456
4457 case Token::GTE:
4458 Comparison(ge);
4459 break;
4460
4461 case Token::EQ_STRICT:
4462 Comparison(eq, true);
4463 break;
4464
4465 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00004466 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004467 __ InvokeBuiltin(Builtins::IN, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00004468 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004469 break;
4470
4471 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00004472 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004473 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
mads.s.ager31e71382008-08-13 09:32:07 +00004474 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004475 break;
4476
4477 default:
4478 UNREACHABLE();
4479 }
4480}
4481
4482
4483void ArmCodeGenerator::RecordStatementPosition(Node* node) {
4484 if (FLAG_debug_info) {
4485 int statement_pos = node->statement_pos();
ager@chromium.org236ad962008-09-25 09:45:57 +00004486 if (statement_pos == RelocInfo::kNoPosition) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004487 __ RecordStatementPosition(statement_pos);
4488 }
4489}
4490
4491
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004492void ArmCodeGenerator::EnterJSFrame() {
4493#if defined(DEBUG)
4494 { Label done, fail;
4495 __ tst(r1, Operand(kSmiTagMask));
4496 __ b(eq, &fail);
4497 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4498 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4499 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4500 __ b(eq, &done);
4501 __ bind(&fail);
4502 __ stop("ArmCodeGenerator::EnterJSFrame - r1 not a function");
4503 __ bind(&done);
4504 }
4505#endif // DEBUG
4506
4507 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
4508 __ add(fp, sp, Operand(2 * kPointerSize)); // Adjust FP to point to saved FP.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004509}
4510
4511
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004512void ArmCodeGenerator::ExitJSFrame() {
4513 // Drop the execution stack down to the frame pointer and restore the caller
4514 // frame pointer and return address.
4515 __ mov(sp, fp);
4516 __ ldm(ia_w, sp, fp.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004517}
4518
4519
4520#undef __
4521
4522
4523// -----------------------------------------------------------------------------
4524// CodeGenerator interface
4525
4526// MakeCode() is just a wrapper for CodeGenerator::MakeCode()
4527// so we don't have to expose the entire CodeGenerator class in
4528// the .h file.
4529Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun,
4530 Handle<Script> script,
4531 bool is_eval) {
4532 Handle<Code> code = ArmCodeGenerator::MakeCode(fun, script, is_eval);
4533 if (!code.is_null()) {
4534 Counters::total_compiled_code_size.Increment(code->instruction_size());
4535 }
4536 return code;
4537}
4538
4539
4540} } // namespace v8::internal