blob: 59764260270ad2e894da143bc167fd0cff00d5bc [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// Copyright 2006-2008 Google Inc. All Rights Reserved.
2// 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 +000040DEFINE_bool(trace, false, "trace function calls");
41DECLARE_bool(debug_info);
42DECLARE_bool(debug_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
44#ifdef DEBUG
45DECLARE_bool(gc_greedy);
46DEFINE_bool(trace_codegen, false,
47 "print name of functions for which code is generated");
48DEFINE_bool(print_code, false, "print generated code");
49DEFINE_bool(print_builtin_code, false, "print generated code for builtins");
50DEFINE_bool(print_source, false, "pretty print source code");
51DEFINE_bool(print_builtin_source, false,
52 "pretty print source code for builtins");
53DEFINE_bool(print_ast, false, "print source AST");
54DEFINE_bool(print_builtin_ast, false, "print source AST for builtins");
55DEFINE_bool(trace_calls, false, "trace calls");
56DEFINE_bool(trace_builtin_calls, false, "trace builtins calls");
57DEFINE_string(stop_at, "", "function name where to insert a breakpoint");
58#endif // DEBUG
59
60
61DEFINE_bool(check_stack, true,
62 "check stack for overflow, interrupt, breakpoint");
63
64
65class ArmCodeGenerator;
66
67
68// -----------------------------------------------------------------------------
69// Reference support
70
71// A reference is a C++ stack-allocated object that keeps an ECMA
72// reference on the execution stack while in scope. For variables
73// the reference is empty, indicating that it isn't necessary to
74// store state on the stack for keeping track of references to those.
75// For properties, we keep either one (named) or two (indexed) values
76// on the execution stack to represent the reference.
77
78class Reference BASE_EMBEDDED {
79 public:
80 enum Type { ILLEGAL = -1, EMPTY = 0, NAMED = 1, KEYED = 2 };
81 Reference(ArmCodeGenerator* cgen, Expression* expression);
82 ~Reference();
83
84 Expression* expression() const { return expression_; }
85 Type type() const { return type_; }
86 void set_type(Type value) {
87 ASSERT(type_ == ILLEGAL);
88 type_ = value;
89 }
90 int size() const { return type_; }
91
92 bool is_illegal() const { return type_ == ILLEGAL; }
93
94 private:
95 ArmCodeGenerator* cgen_;
96 Expression* expression_;
97 Type type_;
98};
99
100
101// -----------------------------------------------------------------------------
102// Code generation state
103
104class CodeGenState BASE_EMBEDDED {
105 public:
106 enum AccessType {
107 UNDEFINED,
108 LOAD,
109 LOAD_TYPEOF_EXPR,
110 STORE,
111 INIT_CONST
112 };
113
114 CodeGenState()
115 : access_(UNDEFINED),
116 ref_(NULL),
117 true_target_(NULL),
118 false_target_(NULL) {
119 }
120
121 CodeGenState(AccessType access,
122 Reference* ref,
123 Label* true_target,
124 Label* false_target)
125 : access_(access),
126 ref_(ref),
127 true_target_(true_target),
128 false_target_(false_target) {
129 }
130
131 AccessType access() const { return access_; }
132 Reference* ref() const { return ref_; }
133 Label* true_target() const { return true_target_; }
134 Label* false_target() const { return false_target_; }
135
136 private:
137 AccessType access_;
138 Reference* ref_;
139 Label* true_target_;
140 Label* false_target_;
141};
142
143
144// -----------------------------------------------------------------------------
145// ArmCodeGenerator
146
147class ArmCodeGenerator: public CodeGenerator {
148 public:
149 static Handle<Code> MakeCode(FunctionLiteral* fun,
150 Handle<Script> script,
151 bool is_eval);
152
153 MacroAssembler* masm() { return masm_; }
154
155 private:
156 // Assembler
157 MacroAssembler* masm_; // to generate code
158
159 // Code generation state
160 Scope* scope_;
161 Condition cc_reg_;
162 CodeGenState* state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 int break_stack_height_;
164
165 // Labels
166 Label function_return_;
167
168 // Construction/destruction
169 ArmCodeGenerator(int buffer_size,
170 Handle<Script> script,
171 bool is_eval);
172
173 virtual ~ArmCodeGenerator() { delete masm_; }
174
175 // Main code generation function
176 void GenCode(FunctionLiteral* fun);
177
178 // The following are used by class Reference.
179 void LoadReference(Reference* ref);
180 void UnloadReference(Reference* ref);
181 friend class Reference;
182
183 // State
184 bool has_cc() const { return cc_reg_ != al; }
185 CodeGenState::AccessType access() const { return state_->access(); }
186 Reference* ref() const { return state_->ref(); }
187 bool is_referenced() const { return state_->ref() != NULL; }
188 Label* true_target() const { return state_->true_target(); }
189 Label* false_target() const { return state_->false_target(); }
190
191
192 // Expressions
193 MemOperand GlobalObject() const {
194 return ContextOperand(cp, Context::GLOBAL_INDEX);
195 }
196
197 MemOperand ContextOperand(Register context, int index) const {
198 return MemOperand(context, Context::SlotOffset(index));
199 }
200
201 MemOperand ParameterOperand(int index) const {
202 // index -2 corresponds to the activated closure, -1 corresponds
203 // to the receiver
204 ASSERT(-2 <= index && index < scope_->num_parameters());
205 int offset = JavaScriptFrameConstants::kParam0Offset - index * kPointerSize;
206 return MemOperand(pp, offset);
207 }
208
209 MemOperand FunctionOperand() const { return ParameterOperand(-2); }
210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 MemOperand SlotOperand(Slot* slot, Register tmp);
212
213 void LoadCondition(Expression* x, CodeGenState::AccessType access,
214 Label* true_target, Label* false_target, bool force_cc);
215 void Load(Expression* x,
216 CodeGenState::AccessType access = CodeGenState::LOAD);
217 void LoadGlobal();
218
219 // Special code for typeof expressions: Unfortunately, we must
220 // be careful when loading the expression in 'typeof'
221 // expressions. We are not allowed to throw reference errors for
222 // non-existing properties of the global object, so we must make it
223 // look like an explicit property access, instead of an access
224 // through the context chain.
225 void LoadTypeofExpression(Expression* x);
226
227 // References
228 void AccessReference(Reference* ref, CodeGenState::AccessType access);
229
230 void GetValue(Reference* ref) { AccessReference(ref, CodeGenState::LOAD); }
231 void SetValue(Reference* ref) { AccessReference(ref, CodeGenState::STORE); }
232 void InitConst(Reference* ref) {
233 AccessReference(ref, CodeGenState::INIT_CONST);
234 }
235
236 void ToBoolean(Register reg, Label* true_target, Label* false_target);
237
238
239 // Access property from the reference (must be at the TOS).
240 void AccessReferenceProperty(Expression* key,
241 CodeGenState::AccessType access);
242
kasper.lund7276f142008-07-30 08:49:36 +0000243 void GenericBinaryOperation(Token::Value op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 void Comparison(Condition cc, bool strict = false);
245
246 void SmiOperation(Token::Value op, Handle<Object> value, bool reversed);
247
248 void CallWithArguments(ZoneList<Expression*>* arguments, int position);
249
250 // Declare global variables and functions in the given array of
251 // name/value pairs.
252 virtual void DeclareGlobals(Handle<FixedArray> pairs);
253
254 // Instantiate the function boilerplate.
255 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
256
257 // Control flow
258 void Branch(bool if_true, Label* L);
259 void CheckStack();
260 void CleanStack(int num_bytes);
261
262 // Node visitors
263#define DEF_VISIT(type) \
264 virtual void Visit##type(type* node);
265 NODE_LIST(DEF_VISIT)
266#undef DEF_VISIT
267
268 void RecordStatementPosition(Node* node);
269
270 // Activation frames
kasper.lund7276f142008-07-30 08:49:36 +0000271 void EnterJSFrame(int argc); // preserves r1
272 void ExitJSFrame(ExitJSFlag flag = RETURN); // preserves r0-r2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273
274 virtual void GenerateShiftDownAndTailCall(ZoneList<Expression*>* args);
275 virtual void GenerateSetThisFunction(ZoneList<Expression*>* args);
276 virtual void GenerateGetThisFunction(ZoneList<Expression*>* args);
277 virtual void GenerateSetThis(ZoneList<Expression*>* args);
278 virtual void GenerateGetArgumentsLength(ZoneList<Expression*>* args);
279 virtual void GenerateSetArgumentsLength(ZoneList<Expression*>* args);
280 virtual void GenerateTailCallWithArguments(ZoneList<Expression*>* args);
281 virtual void GenerateSetArgument(ZoneList<Expression*>* args);
282 virtual void GenerateSquashFrame(ZoneList<Expression*>* args);
283 virtual void GenerateExpandFrame(ZoneList<Expression*>* args);
284 virtual void GenerateIsSmi(ZoneList<Expression*>* args);
285 virtual void GenerateIsArray(ZoneList<Expression*>* args);
286
287 virtual void GenerateArgumentsLength(ZoneList<Expression*>* args);
288 virtual void GenerateArgumentsAccess(ZoneList<Expression*>* args);
289
290 virtual void GenerateValueOf(ZoneList<Expression*>* args);
291 virtual void GenerateSetValueOf(ZoneList<Expression*>* args);
kasper.lund7276f142008-07-30 08:49:36 +0000292
293 virtual void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294};
295
296
297// -----------------------------------------------------------------------------
298// ArmCodeGenerator implementation
299
300#define __ masm_->
301
302
303Handle<Code> ArmCodeGenerator::MakeCode(FunctionLiteral* flit,
304 Handle<Script> script,
305 bool is_eval) {
306#ifdef DEBUG
307 bool print_source = false;
308 bool print_ast = false;
309 bool print_code = false;
310 const char* ftype;
311
312 if (Bootstrapper::IsActive()) {
313 print_source = FLAG_print_builtin_source;
314 print_ast = FLAG_print_builtin_ast;
315 print_code = FLAG_print_builtin_code;
316 ftype = "builtin";
317 } else {
318 print_source = FLAG_print_source;
319 print_ast = FLAG_print_ast;
320 print_code = FLAG_print_code;
321 ftype = "user-defined";
322 }
323
324 if (FLAG_trace_codegen || print_source || print_ast) {
325 PrintF("*** Generate code for %s function: ", ftype);
326 flit->name()->ShortPrint();
327 PrintF(" ***\n");
328 }
329
330 if (print_source) {
331 PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit));
332 }
333
334 if (print_ast) {
335 PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit));
336 }
337#endif // DEBUG
338
339 // Generate code.
340 const int initial_buffer_size = 4 * KB;
341 ArmCodeGenerator cgen(initial_buffer_size, script, is_eval);
342 cgen.GenCode(flit);
343 if (cgen.HasStackOverflow()) {
kasper.lund212ac232008-07-16 07:07:30 +0000344 ASSERT(!Top::has_pending_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 return Handle<Code>::null();
346 }
347
348 // Process any deferred code.
349 cgen.ProcessDeferred();
350
351 // Allocate and install the code.
352 CodeDesc desc;
353 cgen.masm()->GetCode(&desc);
354 ScopeInfo<> sinfo(flit->scope());
355 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION);
356 Handle<Code> code = Factory::NewCode(desc, &sinfo, flags);
357
358 // Add unresolved entries in the code to the fixup list.
359 Bootstrapper::AddFixup(*code, cgen.masm());
360
361#ifdef DEBUG
362 if (print_code) {
363 // Print the source code if available.
364 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
365 PrintF("--- Raw source ---\n");
366 StringInputBuffer stream(String::cast(script->source()));
367 stream.Seek(flit->start_position());
368 // flit->end_position() points to the last character in the stream. We
369 // need to compensate by adding one to calculate the length.
370 int source_len = flit->end_position() - flit->start_position() + 1;
371 for (int i = 0; i < source_len; i++) {
372 if (stream.has_more()) PrintF("%c", stream.GetNext());
373 }
374 PrintF("\n\n");
375 }
376 PrintF("--- Code ---\n");
377 code->Print();
378 }
379#endif // DEBUG
380
381 return code;
382}
383
384
385ArmCodeGenerator::ArmCodeGenerator(int buffer_size,
386 Handle<Script> script,
387 bool is_eval)
388 : CodeGenerator(is_eval, script),
389 masm_(new MacroAssembler(NULL, buffer_size)),
390 scope_(NULL),
391 cc_reg_(al),
392 state_(NULL),
393 break_stack_height_(0) {
394}
395
396
397// Calling conventions:
398
399// r0: always contains top-of-stack (TOS), but in case of a call it's
400// the number of arguments
401// fp: frame pointer
402// sp: stack pointer
403// pp: caller's parameter pointer
404// cp: callee's context
405
406void ArmCodeGenerator::GenCode(FunctionLiteral* fun) {
407 Scope* scope = fun->scope();
408 ZoneList<Statement*>* body = fun->body();
409
410 // Initialize state.
411 { CodeGenState state;
412 state_ = &state;
413 scope_ = scope;
414 cc_reg_ = al;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415
416 // Entry
417 // stack: function, receiver, arguments, return address
418 // r0: number of arguments
419 // sp: stack pointer
420 // fp: frame pointer
421 // pp: caller's parameter pointer
422 // cp: callee's context
423
424 { Comment cmnt(masm_, "[ enter JS frame");
kasper.lund7276f142008-07-30 08:49:36 +0000425 EnterJSFrame(scope->num_parameters());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 }
427 // tos: code slot
428#ifdef DEBUG
429 if (strlen(FLAG_stop_at) > 0 &&
430 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000431 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 }
433#endif
434
435 // Allocate space for locals and initialize them.
kasper.lund7276f142008-07-30 08:49:36 +0000436 if (scope->num_stack_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 Comment cmnt(masm_, "[ allocate space for locals");
438 // Pushing the first local materializes the code slot on the stack
439 // (formerly stored in tos register r0).
440 __ Push(Operand(Factory::undefined_value()));
441 // The remaining locals are pushed using the fact that r0 (tos)
442 // already contains the undefined value.
kasper.lund7276f142008-07-30 08:49:36 +0000443 for (int i = 1; i < scope->num_stack_slots(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 __ push(r0);
445 }
446 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447
448 if (scope->num_heap_slots() > 0) {
449 // Allocate local context.
450 // Get outer context and create a new context based on it.
451 __ Push(FunctionOperand());
kasper.lund7276f142008-07-30 08:49:36 +0000452 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
453
454 if (kDebug) {
455 Label verified_true;
456 __ cmp(r0, Operand(cp));
457 __ b(eq, &verified_true);
458 __ stop("NewContext: r0 is expected to be the same as cp");
459 __ bind(&verified_true);
460 }
461 __ pop(r0); // restore TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 // Update context local.
463 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
464 }
465
466 // TODO(1241774): Improve this code!!!
467 // 1) only needed if we have a context
468 // 2) no need to recompute context ptr every single time
469 // 3) don't copy parameter operand code from SlotOperand!
470 {
471 Comment cmnt2(masm_, "[ copy context parameters into .context");
472
473 // Note that iteration order is relevant here! If we have the same
474 // parameter twice (e.g., function (x, y, x)), and that parameter
475 // needs to be copied into the context, it must be the last argument
476 // passed to the parameter that needs to be copied. This is a rare
477 // case so we don't check for it, instead we rely on the copying
478 // order: such a parameter is copied repeatedly into the same
479 // context location and thus the last value is what is seen inside
480 // the function.
481 for (int i = 0; i < scope->num_parameters(); i++) {
482 Variable* par = scope->parameter(i);
483 Slot* slot = par->slot();
484 if (slot != NULL && slot->type() == Slot::CONTEXT) {
485 ASSERT(!scope->is_global_scope()); // no parameters in global scope
486 int parameter_offset =
487 JavaScriptFrameConstants::kParam0Offset - i * kPointerSize;
488 __ ldr(r1, MemOperand(pp, parameter_offset));
489 // Loads r2 with context; used below in RecordWrite.
490 __ str(r1, SlotOperand(slot, r2));
491 // Load the offset into r3.
492 int slot_offset =
493 FixedArray::kHeaderSize + slot->index() * kPointerSize;
494 __ mov(r3, Operand(slot_offset));
495 __ RecordWrite(r2, r3, r1);
496 }
497 }
498 }
499
500 // Store the arguments object.
501 // This must happen after context initialization because
502 // the arguments array may be stored in the context!
503 if (scope->arguments() != NULL) {
504 ASSERT(scope->arguments_shadow() != NULL);
505 Comment cmnt(masm_, "[ allocate arguments object");
506 {
507 Reference target(this, scope->arguments());
508 __ Push(FunctionOperand());
509 __ CallRuntime(Runtime::kNewArguments, 1);
510 SetValue(&target);
511 }
512 // The value of arguments must also be stored in .arguments.
513 // TODO(1241813): This code can probably be improved by fusing it with
514 // the code that stores the arguments object above.
515 {
516 Reference target(this, scope->arguments_shadow());
517 Load(scope->arguments());
518 SetValue(&target);
519 }
520 }
521
522 // Generate code to 'execute' declarations and initialize
523 // functions (source elements). In case of an illegal
524 // redeclaration we need to handle that instead of processing the
525 // declarations.
526 if (scope->HasIllegalRedeclaration()) {
527 Comment cmnt(masm_, "[ illegal redeclarations");
528 scope->VisitIllegalRedeclaration(this);
529 } else {
530 Comment cmnt(masm_, "[ declarations");
531 ProcessDeclarations(scope->declarations());
kasper.lund212ac232008-07-16 07:07:30 +0000532 // Bail out if a stack-overflow exception occured when
533 // processing declarations.
534 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 }
536
537 if (FLAG_trace) __ CallRuntime(Runtime::kTraceEnter, 1);
538 CheckStack();
539
540 // Compile the body of the function in a vanilla state. Don't
541 // bother compiling all the code if the scope has an illegal
542 // redeclaration.
543 if (!scope->HasIllegalRedeclaration()) {
544 Comment cmnt(masm_, "[ function body");
545#ifdef DEBUG
546 bool is_builtin = Bootstrapper::IsActive();
547 bool should_trace =
548 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
549 if (should_trace) __ CallRuntime(Runtime::kDebugTrace, 1);
550#endif
551 VisitStatements(body);
552 }
553
554 state_ = NULL;
555 }
556
557 // exit
558 // r0: result
559 // sp: stack pointer
560 // fp: frame pointer
561 // pp: parameter pointer
562 // cp: callee's context
563 __ Push(Operand(Factory::undefined_value()));
564 __ bind(&function_return_);
565 if (FLAG_trace) __ CallRuntime(Runtime::kTraceExit, 1);
kasper.lund7276f142008-07-30 08:49:36 +0000566 ExitJSFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
568 // Code generation state must be reset.
569 scope_ = NULL;
570 ASSERT(!has_cc());
571 ASSERT(state_ == NULL);
572}
573
574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575MemOperand ArmCodeGenerator::SlotOperand(Slot* slot, Register tmp) {
576 // Currently, this assertion will fail if we try to assign to
577 // a constant variable that is constant because it is read-only
578 // (such as the variable referring to a named function expression).
579 // We need to implement assignments to read-only variables.
580 // Ideally, we should do this during AST generation (by converting
581 // such assignments into expression statements); however, in general
582 // we may not be able to make the decision until past AST generation,
583 // that is when the entire program is known.
584 ASSERT(slot != NULL);
585 int index = slot->index();
586 switch (slot->type()) {
587 case Slot::PARAMETER:
588 return ParameterOperand(index);
589
590 case Slot::LOCAL: {
591 ASSERT(0 <= index &&
592 index < scope_->num_stack_slots() &&
kasper.lund7276f142008-07-30 08:49:36 +0000593 index >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 int local_offset = JavaScriptFrameConstants::kLocal0Offset -
kasper.lund7276f142008-07-30 08:49:36 +0000595 index * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 return MemOperand(fp, local_offset);
597 }
598
599 case Slot::CONTEXT: {
600 // Follow the context chain if necessary.
601 ASSERT(!tmp.is(cp)); // do not overwrite context register
602 Register context = cp;
603 int chain_length = scope_->ContextChainLength(slot->var()->scope());
604 for (int i = chain_length; i-- > 0;) {
605 // Load the closure.
606 // (All contexts, even 'with' contexts, have a closure,
607 // and it is the same for all contexts inside a function.
608 // There is no need to go to the function context first.)
609 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
610 // Load the function context (which is the incoming, outer context).
611 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
612 context = tmp;
613 }
614 // We may have a 'with' context now. Get the function context.
615 // (In fact this mov may never be the needed, since the scope analysis
616 // may not permit a direct context access in this case and thus we are
617 // always at a function context. However it is safe to dereference be-
618 // cause the function context of a function context is itself. Before
619 // deleting this mov we should try to create a counter-example first,
620 // though...)
621 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
622 return ContextOperand(tmp, index);
623 }
624
625 default:
626 UNREACHABLE();
627 return MemOperand(r0, 0);
628 }
629}
630
631
632// Loads a value on TOS. If it is a boolean value, the result may have been
633// (partially) translated into branches, or it may have set the condition code
634// register. If force_cc is set, the value is forced to set the condition code
635// register and no value is pushed. If the condition code register was set,
636// has_cc() is true and cc_reg_ contains the condition to test for 'true'.
637void ArmCodeGenerator::LoadCondition(Expression* x,
638 CodeGenState::AccessType access,
639 Label* true_target,
640 Label* false_target,
641 bool force_cc) {
642 ASSERT(access == CodeGenState::LOAD ||
643 access == CodeGenState::LOAD_TYPEOF_EXPR);
644 ASSERT(!has_cc() && !is_referenced());
645
646 CodeGenState* old_state = state_;
647 CodeGenState new_state(access, NULL, true_target, false_target);
648 state_ = &new_state;
649 Visit(x);
650 state_ = old_state;
651 if (force_cc && !has_cc()) {
652 // Pop the TOS from the stack and convert it to a boolean in the
653 // condition code register.
654 __ mov(r1, Operand(r0));
655 __ pop(r0);
656 ToBoolean(r1, true_target, false_target);
657 }
658 ASSERT(has_cc() || !force_cc);
659}
660
661
662void ArmCodeGenerator::Load(Expression* x, CodeGenState::AccessType access) {
663 ASSERT(access == CodeGenState::LOAD ||
664 access == CodeGenState::LOAD_TYPEOF_EXPR);
665
666 Label true_target;
667 Label false_target;
668 LoadCondition(x, access, &true_target, &false_target, false);
669
670 if (has_cc()) {
671 // convert cc_reg_ into a bool
672 Label loaded, materialize_true;
673 __ b(cc_reg_, &materialize_true);
674 __ Push(Operand(Factory::false_value()));
675 __ b(&loaded);
676 __ bind(&materialize_true);
677 __ Push(Operand(Factory::true_value()));
678 __ bind(&loaded);
679 cc_reg_ = al;
680 }
681
682 if (true_target.is_linked() || false_target.is_linked()) {
683 // we have at least one condition value
684 // that has been "translated" into a branch,
685 // thus it needs to be loaded explicitly again
686 Label loaded;
687 __ b(&loaded); // don't lose current TOS
688 bool both = true_target.is_linked() && false_target.is_linked();
689 // reincarnate "true", if necessary
690 if (true_target.is_linked()) {
691 __ bind(&true_target);
692 __ Push(Operand(Factory::true_value()));
693 }
694 // if both "true" and "false" need to be reincarnated,
695 // jump across code for "false"
696 if (both)
697 __ b(&loaded);
698 // reincarnate "false", if necessary
699 if (false_target.is_linked()) {
700 __ bind(&false_target);
701 __ Push(Operand(Factory::false_value()));
702 }
703 // everything is loaded at this point
704 __ bind(&loaded);
705 }
706 ASSERT(!has_cc());
707}
708
709
710void ArmCodeGenerator::LoadGlobal() {
711 __ Push(GlobalObject());
712}
713
714
715// TODO(1241834): Get rid of this function in favor of just using Load, now
716// that we have the LOAD_TYPEOF_EXPR access type. => Need to handle
717// global variables w/o reference errors elsewhere.
718void ArmCodeGenerator::LoadTypeofExpression(Expression* x) {
719 Variable* variable = x->AsVariableProxy()->AsVariable();
720 if (variable != NULL && !variable->is_this() && variable->is_global()) {
721 // NOTE: This is somewhat nasty. We force the compiler to load
722 // the variable as if through '<global>.<variable>' to make sure we
723 // do not get reference errors.
724 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
725 Literal key(variable->name());
726 // TODO(1241834): Fetch the position from the variable instead of using
727 // no position.
728 Property property(&global, &key, kNoPosition);
729 Load(&property);
730 } else {
731 Load(x, CodeGenState::LOAD_TYPEOF_EXPR);
732 }
733}
734
735
736Reference::Reference(ArmCodeGenerator* cgen, Expression* expression)
737 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
738 cgen->LoadReference(this);
739}
740
741
742Reference::~Reference() {
743 cgen_->UnloadReference(this);
744}
745
746
747void ArmCodeGenerator::LoadReference(Reference* ref) {
748 Expression* e = ref->expression();
749 Property* property = e->AsProperty();
750 Variable* var = e->AsVariableProxy()->AsVariable();
751
752 if (property != NULL) {
753 Load(property->obj());
754 // Used a named reference if the key is a literal symbol.
755 // We don't use a named reference if they key is a string that can be
756 // legally parsed as an integer. This is because, otherwise we don't
757 // get into the slow case code that handles [] on String objects.
758 Literal* literal = property->key()->AsLiteral();
759 uint32_t dummy;
760 if (literal != NULL && literal->handle()->IsSymbol() &&
761 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
762 ref->set_type(Reference::NAMED);
763 } else {
764 Load(property->key());
765 ref->set_type(Reference::KEYED);
766 }
767 } else if (var != NULL) {
768 if (var->is_global()) {
769 // global variable
770 LoadGlobal();
771 ref->set_type(Reference::NAMED);
772 } else {
773 // local variable
774 ref->set_type(Reference::EMPTY);
775 }
776 } else {
777 Load(e);
778 __ CallRuntime(Runtime::kThrowReferenceError, 1);
779 }
780}
781
782
783void ArmCodeGenerator::UnloadReference(Reference* ref) {
784 int size = ref->size();
785 if (size <= 0) {
786 // Do nothing. No popping is necessary.
787 } else {
788 __ add(sp, sp, Operand(size * kPointerSize));
789 }
790}
791
792
793void ArmCodeGenerator::AccessReference(Reference* ref,
794 CodeGenState::AccessType access) {
795 ASSERT(!has_cc());
796 ASSERT(ref->type() != Reference::ILLEGAL);
797 CodeGenState* old_state = state_;
798 CodeGenState new_state(access, ref, true_target(), false_target());
799 state_ = &new_state;
800 Visit(ref->expression());
801 state_ = old_state;
802}
803
804
805// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
806// register to a boolean in the condition code register. The code
807// may jump to 'false_target' in case the register converts to 'false'.
808void ArmCodeGenerator::ToBoolean(Register reg,
809 Label* true_target,
810 Label* false_target) {
811 // Note: The generated code snippet cannot change 'reg'.
812 // Only the condition code should be set.
813
814 // Fast case checks
815
816 // Check if reg is 'false'.
817 __ cmp(reg, Operand(Factory::false_value()));
818 __ b(eq, false_target);
819
820 // Check if reg is 'true'.
821 __ cmp(reg, Operand(Factory::true_value()));
822 __ b(eq, true_target);
823
824 // Check if reg is 'undefined'.
825 __ cmp(reg, Operand(Factory::undefined_value()));
826 __ b(eq, false_target);
827
828 // Check if reg is a smi.
829 __ cmp(reg, Operand(Smi::FromInt(0)));
830 __ b(eq, false_target);
831 __ tst(reg, Operand(kSmiTagMask));
832 __ b(eq, true_target);
833
834 // Slow case: call the runtime.
835 __ push(r0);
836 if (r0.is(reg)) {
837 __ CallRuntime(Runtime::kToBool, 1);
838 } else {
839 __ mov(r0, Operand(reg));
840 __ CallRuntime(Runtime::kToBool, 1);
841 }
842 // Convert result (r0) to condition code
843 __ cmp(r0, Operand(Factory::false_value()));
844 __ pop(r0);
845
846 cc_reg_ = ne;
847}
848
849
850#undef __
851#define __ masm->
852
853
854class GetPropertyStub : public CodeStub {
855 public:
856 GetPropertyStub() { }
857
858 private:
859 Major MajorKey() { return GetProperty; }
860 int MinorKey() { return 0; }
861 void Generate(MacroAssembler* masm);
862
863 const char* GetName() { return "GetPropertyStub"; }
864};
865
866
867void GetPropertyStub::Generate(MacroAssembler* masm) {
868 Label slow, fast;
869 // Get the object from the stack.
870 __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // 1 ~ key
871 // Check that the key is a smi.
872 __ tst(r0, Operand(kSmiTagMask));
873 __ b(ne, &slow);
874 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
875 // Check that the object isn't a smi.
876 __ tst(r1, Operand(kSmiTagMask));
877 __ b(eq, &slow);
878 // Check that the object is some kind of JS object.
879 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
880 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
881 __ cmp(r2, Operand(JS_OBJECT_TYPE));
882 __ b(lt, &slow);
883
884 // Check if the object is a value-wrapper object. In that case we
885 // enter the runtime system to make sure that indexing into string
886 // objects work as intended.
887 __ cmp(r2, Operand(JS_VALUE_TYPE));
888 __ b(eq, &slow);
889
890 // Get the elements array of the object.
891 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
892 // Check that the object is in fast mode (not dictionary).
893 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
894 __ cmp(r3, Operand(Factory::hash_table_map()));
895 __ b(eq, &slow);
896 // Check that the key (index) is within bounds.
897 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
898 __ cmp(r0, Operand(r3));
899 __ b(lo, &fast);
900
901 // Slow case: Push extra copies of the arguments (2).
902 __ bind(&slow);
903 __ ldm(ia, sp, r0.bit() | r1.bit());
904 __ stm(db_w, sp, r0.bit() | r1.bit());
905 // Do tail-call to runtime routine.
906 __ mov(r0, Operand(1)); // not counting receiver
907 __ JumpToBuiltin(ExternalReference(Runtime::kGetProperty));
908
909 // Fast case: Do the load.
910 __ bind(&fast);
911 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
912 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
913 __ cmp(r0, Operand(Factory::the_hole_value()));
914 // In case the loaded value is the_hole we have to consult GetProperty
915 // to ensure the prototype chain is searched.
916 __ b(eq, &slow);
917
918 masm->StubReturn(1);
919}
920
921
922class SetPropertyStub : public CodeStub {
923 public:
924 SetPropertyStub() { }
925
926 private:
927 Major MajorKey() { return SetProperty; }
928 int MinorKey() { return 0; }
929 void Generate(MacroAssembler* masm);
930
931 const char* GetName() { return "GetPropertyStub"; }
932};
933
934
935void SetPropertyStub::Generate(MacroAssembler* masm) {
936 Label slow, fast, array, extra, exit;
937 // Get the key and the object from the stack.
938 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
939 // Check that the key is a smi.
940 __ tst(r1, Operand(kSmiTagMask));
941 __ b(ne, &slow);
942 // Check that the object isn't a smi.
943 __ tst(r3, Operand(kSmiTagMask));
944 __ b(eq, &slow);
945 // Get the type of the object from its map.
946 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
947 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
948 // Check if the object is a JS array or not.
949 __ cmp(r2, Operand(JS_ARRAY_TYPE));
950 __ b(eq, &array);
951 // Check that the object is some kind of JS object.
952 __ cmp(r2, Operand(JS_OBJECT_TYPE));
953 __ b(lt, &slow);
954
955
956 // Object case: Check key against length in the elements array.
957 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
958 // Check that the object is in fast mode (not dictionary).
959 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
960 __ cmp(r2, Operand(Factory::hash_table_map()));
961 __ b(eq, &slow);
962 // Untag the key (for checking against untagged length in the fixed array).
963 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
964 // Compute address to store into and check array bounds.
965 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
966 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
967 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
968 __ cmp(r1, Operand(ip));
969 __ b(lo, &fast);
970
971
972 // Slow case: Push extra copies of the arguments (3).
973 // r0 == value
974 __ bind(&slow);
975 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
976 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
977 // Do tail-call to runtime routine.
978 __ mov(r0, Operand(2)); // not counting receiver
979 __ JumpToBuiltin(ExternalReference(Runtime::kSetProperty));
980
981
982 // Extra capacity case: Check if there is extra capacity to
983 // perform the store and update the length. Used for adding one
984 // element to the array by writing to array[array.length].
985 // r0 == value, r1 == key, r2 == elements, r3 == object
986 __ bind(&extra);
987 __ b(ne, &slow); // do not leave holes in the array
988 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
989 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
990 __ cmp(r1, Operand(ip));
991 __ b(hs, &slow);
992 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
993 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
994 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
995 __ mov(r3, Operand(r2));
996 // NOTE: Computing the address to store into must take the fact
997 // that the key has been incremented into account.
998 int displacement = Array::kHeaderSize - kHeapObjectTag -
999 ((1 << kSmiTagSize) * 2);
1000 __ add(r2, r2, Operand(displacement));
1001 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1002 __ b(&fast);
1003
1004
1005 // Array case: Get the length and the elements array from the JS
1006 // array. Check that the array is in fast mode; if it is the
1007 // length is always a smi.
1008 // r0 == value, r3 == object
1009 __ bind(&array);
1010 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
1011 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1012 __ cmp(r1, Operand(Factory::hash_table_map()));
1013 __ b(eq, &slow);
1014
1015 // Check the key against the length in the array, compute the
1016 // address to store into and fall through to fast case.
1017 __ ldr(r1, MemOperand(sp));
1018 // r0 == value, r1 == key, r2 == elements, r3 == object.
1019 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
1020 __ cmp(r1, Operand(ip));
1021 __ b(hs, &extra);
1022 __ mov(r3, Operand(r2));
1023 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
1024 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1025
1026
1027 // Fast case: Do the store.
1028 // r0 == value, r2 == address to store into, r3 == elements
1029 __ bind(&fast);
1030 __ str(r0, MemOperand(r2));
1031 // Skip write barrier if the written value is a smi.
1032 __ tst(r0, Operand(kSmiTagMask));
1033 __ b(eq, &exit);
1034 // Update write barrier for the elements array address.
1035 __ sub(r1, r2, Operand(r3));
1036 __ RecordWrite(r3, r1, r2);
1037 __ bind(&exit);
1038 masm->StubReturn(1);
1039}
1040
1041
kasper.lund7276f142008-07-30 08:49:36 +00001042class GenericBinaryOpStub : public CodeStub {
1043 public:
1044 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
1045
1046 private:
1047 Token::Value op_;
1048
1049 Major MajorKey() { return GenericBinaryOp; }
1050 int MinorKey() { return static_cast<int>(op_); }
1051 void Generate(MacroAssembler* masm);
1052
1053 const char* GetName() {
1054 switch (op_) {
1055 case Token::ADD: return "GenericBinaryOpStub_ADD";
1056 case Token::SUB: return "GenericBinaryOpStub_SUB";
1057 case Token::MUL: return "GenericBinaryOpStub_MUL";
1058 case Token::DIV: return "GenericBinaryOpStub_DIV";
1059 default: return "GenericBinaryOpStub";
1060 }
1061 }
1062
1063#ifdef DEBUG
1064 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
1065#endif
1066};
1067
1068
1069void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 switch (op_) {
1071 case Token::ADD: {
1072 Label slow, exit;
1073 // fast path
1074 // Get x (y is on TOS, i.e., r0).
1075 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
1076 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1077 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
1078 // go slow-path in case of overflow
1079 __ b(vs, &slow);
1080 // go slow-path in case of non-smi operands
1081 ASSERT(kSmiTag == 0); // adjust code below
1082 __ tst(r2, Operand(kSmiTagMask));
1083 __ b(eq, &exit);
1084 // slow path
1085 __ bind(&slow);
1086 __ sub(r0, r0, Operand(r1)); // revert optimistic add
1087 __ push(r0);
1088 __ mov(r0, Operand(1)); // set number of arguments
1089 __ InvokeBuiltin("ADD", 1, JUMP_JS);
1090 // done
1091 __ bind(&exit);
1092 break;
1093 }
1094
1095 case Token::SUB: {
1096 Label slow, exit;
1097 // fast path
1098 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // get x
1099 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1100 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
1101 // go slow-path in case of overflow
1102 __ b(vs, &slow);
1103 // go slow-path in case of non-smi operands
1104 ASSERT(kSmiTag == 0); // adjust code below
1105 __ tst(r2, Operand(kSmiTagMask));
1106 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
1107 __ b(eq, &exit);
1108 // slow path
1109 __ bind(&slow);
1110 __ push(r0);
1111 __ mov(r0, Operand(1)); // set number of arguments
1112 __ InvokeBuiltin("SUB", 1, JUMP_JS);
1113 // done
1114 __ bind(&exit);
1115 break;
1116 }
1117
1118 case Token::MUL: {
1119 Label slow, exit;
1120 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // get x
1121 // tag check
1122 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1123 ASSERT(kSmiTag == 0); // adjust code below
1124 __ tst(r2, Operand(kSmiTagMask));
1125 __ b(ne, &slow);
1126 // remove tag from one operand (but keep sign), so that result is smi
1127 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
1128 // do multiplication
1129 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
1130 // go slow on overflows (overflow bit is not set)
1131 __ mov(ip, Operand(r3, ASR, 31));
1132 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
1133 __ b(ne, &slow);
1134 // go slow on zero result to handle -0
1135 __ tst(r3, Operand(r3));
1136 __ mov(r0, Operand(r3), LeaveCC, ne);
1137 __ b(ne, &exit);
1138 // slow case
1139 __ bind(&slow);
1140 __ push(r0);
1141 __ mov(r0, Operand(1)); // set number of arguments
1142 __ InvokeBuiltin("MUL", 1, JUMP_JS);
1143 // done
1144 __ bind(&exit);
1145 break;
1146 }
1147 default: UNREACHABLE();
1148 }
1149 masm->StubReturn(2);
1150}
1151
1152
1153class SmiOpStub : public CodeStub {
1154 public:
1155 SmiOpStub(Token::Value op, bool reversed)
1156 : op_(op), reversed_(reversed) {}
1157
1158 private:
1159 Token::Value op_;
1160 bool reversed_;
1161
1162 Major MajorKey() { return SmiOp; }
1163 int MinorKey() {
1164 return (op_ == Token::ADD ? 2 : 0) | (reversed_ ? 1 : 0);
1165 }
1166 void Generate(MacroAssembler* masm);
1167 void GenerateShared(MacroAssembler* masm);
1168
1169 const char* GetName() { return "SmiOpStub"; }
1170
1171#ifdef DEBUG
1172 void Print() {
1173 PrintF("SmiOpStub (token %s), (reversed %s)\n",
1174 Token::String(op_), reversed_ ? "true" : "false");
1175 }
1176#endif
1177};
1178
1179
1180void SmiOpStub::Generate(MacroAssembler* masm) {
1181 switch (op_) {
1182 case Token::ADD: {
1183 if (!reversed_) {
1184 __ sub(r0, r0, Operand(r1)); // revert optimistic add
1185 __ push(r0);
1186 __ push(r1);
1187 __ mov(r0, Operand(1)); // set number of arguments
1188 __ InvokeBuiltin("ADD", 1, JUMP_JS);
1189 } else {
1190 __ sub(r0, r0, Operand(r1)); // revert optimistic add
1191 __ push(r1); // reversed
1192 __ push(r0);
1193 __ mov(r0, Operand(1)); // set number of arguments
1194 __ InvokeBuiltin("ADD", 1, JUMP_JS);
1195 }
1196 break;
1197 }
1198 case Token::SUB: {
1199 if (!reversed_) {
1200 __ push(r0);
1201 __ push(r1);
1202 __ mov(r0, Operand(1)); // set number of arguments
1203 __ InvokeBuiltin("SUB", 1, JUMP_JS);
1204 } else {
1205 __ push(r1);
1206 __ push(r0);
1207 __ mov(r0, Operand(1)); // set number of arguments
1208 __ InvokeBuiltin("SUB", 1, JUMP_JS);
1209 }
1210 break;
1211 }
1212 default: UNREACHABLE();
1213 }
1214}
1215
1216void StackCheckStub::Generate(MacroAssembler* masm) {
1217 Label within_limit;
1218 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
1219 __ ldr(ip, MemOperand(ip));
1220 __ cmp(sp, Operand(ip));
1221 __ b(hs, &within_limit);
1222 // Do tail-call to runtime routine.
1223 __ push(r0);
1224 __ mov(r0, Operand(0)); // not counting receiver (i.e. flushed TOS)
1225 __ JumpToBuiltin(ExternalReference(Runtime::kStackGuard));
1226 __ bind(&within_limit);
1227
1228 masm->StubReturn(1);
1229}
1230
1231
1232void UnarySubStub::Generate(MacroAssembler* masm) {
1233 Label undo;
1234 Label slow;
1235 Label done;
1236
1237 // Enter runtime system if the value is not a smi.
1238 __ tst(r0, Operand(kSmiTagMask));
1239 __ b(ne, &slow);
1240
1241 // Enter runtime system if the value of the expression is zero
1242 // to make sure that we switch between 0 and -0.
1243 __ cmp(r0, Operand(0));
1244 __ b(eq, &slow);
1245
1246 // The value of the expression is a smi that is not zero. Try
1247 // optimistic subtraction '0 - value'.
1248 __ rsb(r1, r0, Operand(0), SetCC);
1249 __ b(vs, &slow);
1250
1251 // If result is a smi we are done.
1252 __ tst(r1, Operand(kSmiTagMask));
1253 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
1254 __ b(eq, &done);
1255
1256 // Enter runtime system.
1257 __ bind(&slow);
1258 __ push(r0);
1259 __ mov(r0, Operand(0)); // set number of arguments
1260 __ InvokeBuiltin("UNARY_MINUS", 0, JUMP_JS);
1261
1262 __ bind(&done);
1263 masm->StubReturn(1);
1264}
1265
1266
1267class InvokeBuiltinStub : public CodeStub {
1268 public:
1269 enum Kind { Inc, Dec, ToNumber };
1270 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
1271
1272 private:
1273 Kind kind_;
1274 int argc_;
1275
1276 Major MajorKey() { return InvokeBuiltin; }
1277 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
1278 void Generate(MacroAssembler* masm);
1279
1280 const char* GetName() { return "InvokeBuiltinStub"; }
1281
1282#ifdef DEBUG
1283 void Print() {
1284 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
1285 static_cast<int>(kind_),
1286 argc_);
1287 }
1288#endif
1289};
1290
1291
1292void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
1293 __ push(r0);
1294 __ mov(r0, Operand(0)); // set number of arguments
1295 switch (kind_) {
1296 case ToNumber: __ InvokeBuiltin("TO_NUMBER", 0, JUMP_JS); break;
1297 case Inc: __ InvokeBuiltin("INC", 0, JUMP_JS); break;
1298 case Dec: __ InvokeBuiltin("DEC", 0, JUMP_JS); break;
1299 default: UNREACHABLE();
1300 }
1301 masm->StubReturn(argc_);
1302}
1303
1304
1305class JSExitStub : public CodeStub {
1306 public:
1307 enum Kind { Inc, Dec, ToNumber };
1308
kasper.lund7276f142008-07-30 08:49:36 +00001309 explicit JSExitStub(ExitJSFlag flag) : flag_(flag) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
1311 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 ExitJSFlag flag_;
1313
1314 Major MajorKey() { return JSExit; }
kasper.lund7276f142008-07-30 08:49:36 +00001315 int MinorKey() { return static_cast<int>(flag_); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 void Generate(MacroAssembler* masm);
1317
1318 const char* GetName() { return "JSExitStub"; }
1319
1320#ifdef DEBUG
1321 void Print() {
kasper.lund7276f142008-07-30 08:49:36 +00001322 PrintF("JSExitStub flag %d)\n", static_cast<int>(flag_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 }
1324#endif
1325};
1326
1327
1328void JSExitStub::Generate(MacroAssembler* masm) {
kasper.lund7276f142008-07-30 08:49:36 +00001329 __ ExitJSFrame(flag_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 masm->StubReturn(1);
1331}
1332
1333
1334
1335void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
1336 // r0 holds exception
1337 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1339 __ ldr(sp, MemOperand(r3));
1340 __ pop(r2); // pop next in chain
1341 __ str(r2, MemOperand(r3));
1342 // restore parameter- and frame-pointer and pop state.
1343 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
1344 // Before returning we restore the context from the frame pointer if not NULL.
1345 // The frame pointer is NULL in the exception handler of a JS entry frame.
1346 __ cmp(fp, Operand(0));
1347 // Set cp to NULL if fp is NULL.
1348 __ mov(cp, Operand(0), LeaveCC, eq);
1349 // Restore cp otherwise.
1350 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1351 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
1352 __ pop(pc);
1353}
1354
1355
1356void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
1357 // Fetch top stack handler.
1358 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1359 __ ldr(r3, MemOperand(r3));
1360
1361 // Unwind the handlers until the ENTRY handler is found.
1362 Label loop, done;
1363 __ bind(&loop);
1364 // Load the type of the current stack handler.
1365 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
1366 StackHandlerConstants::kStateOffset;
1367 __ ldr(r2, MemOperand(r3, kStateOffset));
1368 __ cmp(r2, Operand(StackHandler::ENTRY));
1369 __ b(eq, &done);
1370 // Fetch the next handler in the list.
1371 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
1372 StackHandlerConstants::kNextOffset;
1373 __ ldr(r3, MemOperand(r3, kNextOffset));
1374 __ jmp(&loop);
1375 __ bind(&done);
1376
1377 // Set the top handler address to next handler past the current ENTRY handler.
1378 __ ldr(r0, MemOperand(r3, kNextOffset));
1379 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
1380 __ str(r0, MemOperand(r2));
1381
1382 // Set external caught exception to false.
1383 __ mov(r0, Operand(false));
1384 ExternalReference external_caught(Top::k_external_caught_exception_address);
1385 __ mov(r2, Operand(external_caught));
1386 __ str(r0, MemOperand(r2));
1387
1388 // Set pending exception and TOS to out of memory exception.
1389 Failure* out_of_memory = Failure::OutOfMemoryException();
1390 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1391 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
1392 __ str(r0, MemOperand(r2));
1393
1394 // Restore the stack to the address of the ENTRY handler
1395 __ mov(sp, Operand(r3));
1396
1397 // restore parameter- and frame-pointer and pop state.
1398 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
1399 // Before returning we restore the context from the frame pointer if not NULL.
1400 // The frame pointer is NULL in the exception handler of a JS entry frame.
1401 __ cmp(fp, Operand(0));
1402 // Set cp to NULL if fp is NULL.
1403 __ mov(cp, Operand(0), LeaveCC, eq);
1404 // Restore cp otherwise.
1405 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
1406 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
1407 __ pop(pc);
1408}
1409
1410
1411void CEntryStub::GenerateCore(MacroAssembler* masm,
1412 Label* throw_normal_exception,
1413 Label* throw_out_of_memory_exception,
1414 bool do_gc,
1415 bool do_restore) {
1416 // r0: result parameter for PerformGC, if any
1417 // r4: number of arguments (C callee-saved)
1418 // r5: pointer to builtin function (C callee-saved)
1419
1420 if (do_gc) {
1421 __ Call(FUNCTION_ADDR(Runtime::PerformGC), runtime_entry); // passing r0
1422 }
1423
1424 // call C built-in
1425 __ mov(r0, Operand(r4)); // a0 = argc
1426 __ add(r1, fp, Operand(r4, LSL, kPointerSizeLog2));
1427 __ add(r1, r1, Operand(ExitFrameConstants::kPPDisplacement)); // a1 = argv
1428
1429 // TODO(1242173): To let the GC traverse the return address of the exit
1430 // frames, we need to know where the return address is. Right now,
1431 // we push it on the stack to be able to find it again, but we never
1432 // restore from it in case of changes, which makes it impossible to
1433 // support moving the C entry code stub. This should be fixed, but currently
1434 // this is OK because the CEntryStub gets generated so early in the V8 boot
1435 // sequence that it is not moving ever.
1436 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
1437 __ push(lr);
1438#if !defined(__arm__)
1439 // Notify the simulator of the transition to C code.
1440 __ swi(assembler::arm::call_rt_r5);
1441#else /* !defined(__arm__) */
1442 __ mov(pc, Operand(r5));
1443#endif /* !defined(__arm__) */
1444 // result is in r0 or r0:r1 - do not destroy these registers!
1445
1446 // check for failure result
1447 Label failure_returned;
1448 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
1449 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
1450 __ add(r2, r0, Operand(1));
1451 __ tst(r2, Operand(kFailureTagMask));
1452 __ b(eq, &failure_returned);
1453
1454 // clear top frame
1455 __ mov(r3, Operand(0));
1456 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1457 __ str(r3, MemOperand(ip));
1458
1459 // Restore the memory copy of the registers by digging them out from
1460 // the stack.
1461 if (do_restore) {
1462 // Ok to clobber r2 and r3.
1463 const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize;
1464 const int kOffset = ExitFrameConstants::kDebugMarkOffset - kCallerSavedSize;
1465 __ add(r3, fp, Operand(kOffset));
1466 __ CopyRegistersFromStackToMemory(r3, r2, kJSCallerSaved);
1467 }
1468
1469 // Exit C frame and return
1470 // r0:r1: result
1471 // sp: stack pointer
1472 // fp: frame pointer
1473 // pp: caller's parameter pointer pp (restored as C callee-saved)
1474
1475 // Restore current context from top and clear it in debug mode.
1476 __ mov(r3, Operand(Top::context_address()));
1477 __ ldr(cp, MemOperand(r3));
1478 __ mov(sp, Operand(fp)); // respect ABI stack constraint
kasper.lund7276f142008-07-30 08:49:36 +00001479 __ ldm(ia, sp, pp.bit() | fp.bit() | sp.bit() | pc.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480
1481 // check if we should retry or throw exception
1482 Label retry;
1483 __ bind(&failure_returned);
1484 ASSERT(Failure::RETRY_AFTER_GC == 0);
1485 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
1486 __ b(eq, &retry);
1487
1488 Label continue_exception;
1489 // If the returned failure is EXCEPTION then promote Top::pending_exception().
1490 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
1491 __ b(ne, &continue_exception);
1492
1493 // Retrieve the pending exception and clear the variable.
1494 __ mov(ip, Operand(Factory::the_hole_value().location()));
1495 __ ldr(r3, MemOperand(ip));
1496 __ mov(ip, Operand(Top::pending_exception_address()));
1497 __ ldr(r0, MemOperand(ip));
1498 __ str(r3, MemOperand(ip));
1499
1500 __ bind(&continue_exception);
1501 // Special handling of out of memory exception.
1502 Failure* out_of_memory = Failure::OutOfMemoryException();
1503 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
1504 __ b(eq, throw_out_of_memory_exception);
1505
1506 // Handle normal exception.
1507 __ jmp(throw_normal_exception);
1508
1509 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
1510}
1511
1512
1513void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
1514 // Called from JavaScript; parameters are on stack as if calling JS function
1515 // r0: number of arguments
1516 // r1: pointer to builtin function
1517 // fp: frame pointer (restored after C call)
1518 // sp: stack pointer (restored as callee's pp after C call)
1519 // cp: current context (C callee-saved)
1520 // pp: caller's parameter pointer pp (C callee-saved)
1521
1522 // NOTE: Invocations of builtins may return failure objects
1523 // instead of a proper result. The builtin entry handles
1524 // this by performing a garbage collection and retrying the
1525 // builtin once.
1526
1527 // Enter C frame
1528 // Compute parameter pointer before making changes and save it as ip register
1529 // so that it is restored as sp register on exit, thereby popping the args.
1530 // ip = sp + kPointerSize*(args_len+1); // +1 for receiver
1531 __ add(ip, sp, Operand(r0, LSL, kPointerSizeLog2));
1532 __ add(ip, ip, Operand(kPointerSize));
1533
1534 // all JS callee-saved are saved and traversed by GC; push in reverse order:
1535 // JS callee-saved, caller_pp, caller_fp, sp_on_exit (ip==pp), caller_pc
kasper.lund7276f142008-07-30 08:49:36 +00001536 __ stm(db_w, sp, pp.bit() | fp.bit() | ip.bit() | lr.bit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 __ mov(fp, Operand(sp)); // setup new frame pointer
1538
1539 // Store the current context in top.
1540 __ mov(ip, Operand(Top::context_address()));
1541 __ str(cp, MemOperand(ip));
1542
1543 // remember top frame
1544 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1545 __ str(fp, MemOperand(ip));
1546
1547 // Push debug marker.
1548 __ mov(ip, Operand(is_debug_break ? 1 : 0));
1549 __ push(ip);
1550
1551 if (is_debug_break) {
1552 // Save the state of all registers to the stack from the memory location.
1553 // Use sp as base to push.
1554 __ CopyRegistersFromMemoryToStack(sp, kJSCallerSaved);
1555 }
1556
1557 // move number of arguments (argc) into callee-saved register
1558 __ mov(r4, Operand(r0));
1559
1560 // move pointer to builtin function into callee-saved register
1561 __ mov(r5, Operand(r1));
1562
1563 // r0: result parameter for PerformGC, if any (setup below)
1564 // r4: number of arguments
1565 // r5: pointer to builtin function (C callee-saved)
1566
1567 Label entry;
1568 __ bind(&entry);
1569
1570 Label throw_out_of_memory_exception;
1571 Label throw_normal_exception;
1572
1573#ifdef DEBUG
1574 if (FLAG_gc_greedy) {
1575 Failure* failure = Failure::RetryAfterGC(0, NEW_SPACE);
1576 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
1577 }
1578 GenerateCore(masm,
1579 &throw_normal_exception,
1580 &throw_out_of_memory_exception,
1581 FLAG_gc_greedy,
1582 is_debug_break);
1583#else
1584 GenerateCore(masm,
1585 &throw_normal_exception,
1586 &throw_out_of_memory_exception,
1587 false,
1588 is_debug_break);
1589#endif
1590 GenerateCore(masm,
1591 &throw_normal_exception,
1592 &throw_out_of_memory_exception,
1593 true,
1594 is_debug_break);
1595
1596 __ bind(&throw_out_of_memory_exception);
1597 GenerateThrowOutOfMemory(masm);
1598 // control flow for generated will not return.
1599
1600 __ bind(&throw_normal_exception);
1601 GenerateThrowTOS(masm);
1602}
1603
1604
1605void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
1606 // r0: code entry
1607 // r1: function
1608 // r2: receiver
1609 // r3: argc
1610 // [sp+0]: argv
1611
1612 Label invoke, exit;
1613
1614 // Called from C, so do not pop argc and args on exit (preserve sp)
1615 // No need to save register-passed args
1616 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
1617 __ mov(ip, Operand(sp));
1618 __ stm(db_w, sp, kCalleeSaved | ip.bit() | lr.bit());
1619
1620 // Setup frame pointer
1621 __ mov(fp, Operand(sp));
1622
1623 // Add constructor mark.
1624 __ mov(ip, Operand(is_construct ? 1 : 0));
1625 __ push(ip);
1626
1627 // Move arguments into registers expected by Builtins::JSEntryTrampoline
1628 // preserve r0-r3, set r4, r5-r7 may be clobbered
1629
1630 // Get address of argv, see stm above.
1631 __ add(r4, sp, Operand((kNumCalleeSaved + 3)*kPointerSize));
1632 __ ldr(r4, MemOperand(r4)); // argv
1633
1634 // Save copies of the top frame descriptors on the stack.
1635 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1636 __ ldr(r6, MemOperand(ip));
1637 __ stm(db_w, sp, r6.bit());
1638
1639 // Call a faked try-block that does the invoke.
1640 __ bl(&invoke);
1641
1642 // Caught exception: Store result (exception) in the pending
1643 // exception field in the JSEnv and return a failure sentinel.
1644 __ mov(ip, Operand(Top::pending_exception_address()));
1645 __ str(r0, MemOperand(ip));
1646 __ mov(r0, Operand(Handle<Failure>(Failure::Exception())));
1647 __ b(&exit);
1648
1649 // Invoke: Link this frame into the handler chain.
1650 __ bind(&invoke);
1651 // Must preserve r0-r3, r5-r7 are available.
1652 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
1653 // If an exception not caught by another handler occurs, this handler returns
1654 // control to the code after the bl(&invoke) above, which restores all
1655 // kCalleeSaved registers (including cp, pp and fp) to their saved values
1656 // before returning a failure to C.
1657
1658 // Clear any pending exceptions.
1659 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
1660 __ ldr(r5, MemOperand(ip));
1661 __ mov(ip, Operand(Top::pending_exception_address()));
1662 __ str(r5, MemOperand(ip));
1663
1664 // Invoke the function by calling through JS entry trampoline builtin.
1665 // Notice that we cannot store a reference to the trampoline code directly in
1666 // this stub, because runtime stubs are not traversed when doing GC.
1667
1668 // Expected registers by Builtins::JSEntryTrampoline
1669 // r0: code entry
1670 // r1: function
1671 // r2: receiver
1672 // r3: argc
1673 // r4: argv
1674 if (is_construct) {
1675 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
1676 __ mov(ip, Operand(construct_entry));
1677 } else {
1678 ExternalReference entry(Builtins::JSEntryTrampoline);
1679 __ mov(ip, Operand(entry));
1680 }
1681 __ ldr(ip, MemOperand(ip)); // deref address
1682
1683 // Branch and link to JSEntryTrampoline
1684 __ mov(lr, Operand(pc));
1685 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
1686
1687 // Unlink this frame from the handler chain. When reading the
1688 // address of the next handler, there is no need to use the address
1689 // displacement since the current stack pointer (sp) points directly
1690 // to the stack handler.
1691 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
1692 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
1693 __ str(r3, MemOperand(ip));
1694 // No need to restore registers
1695 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
1696
1697 __ bind(&exit); // r0 holds result
1698 // Restore the top frame descriptors from the stack.
1699 __ ldm(ia_w, sp, r3.bit());
1700 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
1701 __ str(r3, MemOperand(ip));
1702
1703 // Remove constructor mark.
1704 __ add(sp, sp, Operand(kPointerSize));
1705
1706 // Restore callee-saved registers, sp, and return.
1707#ifdef DEBUG
1708 if (FLAG_debug_code) __ mov(lr, Operand(pc));
1709#endif
1710 __ ldm(ia, sp, kCalleeSaved | sp.bit() | pc.bit());
1711}
1712
1713
1714class ArgumentsAccessStub: public CodeStub {
1715 public:
1716 explicit ArgumentsAccessStub(bool is_length) : is_length_(is_length) { }
1717
1718 private:
1719 bool is_length_;
1720
1721 Major MajorKey() { return ArgumentsAccess; }
1722 int MinorKey() { return is_length_ ? 1 : 0; }
1723 void Generate(MacroAssembler* masm);
1724
1725 const char* GetName() { return "ArgumentsAccessStub"; }
1726
1727#ifdef DEBUG
1728 void Print() {
1729 PrintF("ArgumentsAccessStub (is_length %s)\n",
1730 is_length_ ? "true" : "false");
1731 }
1732#endif
1733};
1734
1735
1736void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
1737 if (is_length_) {
1738 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kArgsLengthOffset));
1739 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
1740 __ Ret();
1741 } else {
1742 // Check that the key is a smi.
1743 Label slow;
1744 __ tst(r0, Operand(kSmiTagMask));
1745 __ b(ne, &slow);
1746
1747 // Get the actual number of arguments passed and do bounds
1748 // check. Use unsigned comparison to get negative check for free.
1749 __ ldr(r1, MemOperand(fp, JavaScriptFrameConstants::kArgsLengthOffset));
1750 __ cmp(r0, Operand(r1, LSL, kSmiTagSize));
1751 __ b(hs, &slow);
1752
1753 // Load the argument directly from the stack and return.
1754 __ sub(r1, pp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1755 __ ldr(r0, MemOperand(r1, JavaScriptFrameConstants::kParam0Offset));
1756 __ Ret();
1757
1758 // Slow-case: Handle non-smi or out-of-bounds access to arguments
1759 // by calling the runtime system.
1760 __ bind(&slow);
1761 __ push(r0);
1762 __ mov(r0, Operand(0)); // not counting receiver
1763 __ JumpToBuiltin(ExternalReference(Runtime::kGetArgumentsProperty));
1764 }
1765}
1766
1767
1768#undef __
1769#define __ masm_->
1770
1771
1772void ArmCodeGenerator::AccessReferenceProperty(
1773 Expression* key,
1774 CodeGenState::AccessType access) {
1775 Reference::Type type = ref()->type();
1776 ASSERT(type != Reference::ILLEGAL);
1777
1778 // TODO(1241834): Make sure that this is sufficient. If there is a chance
1779 // that reference errors can be thrown below, we must distinguish
1780 // between the 2 kinds of loads (typeof expression loads must not
1781 // throw a reference errror).
1782 bool is_load = (access == CodeGenState::LOAD ||
1783 access == CodeGenState::LOAD_TYPEOF_EXPR);
1784
1785 if (type == Reference::NAMED) {
1786 // Compute the name of the property.
1787 Literal* literal = key->AsLiteral();
1788 Handle<String> name(String::cast(*literal->handle()));
1789
1790 // Loading adds a value to the stack; push the TOS to prepare.
1791 if (is_load) __ push(r0);
1792
1793 // Setup the name register.
1794 __ mov(r2, Operand(name));
1795
1796 // Call the appropriate IC code.
1797 if (is_load) {
1798 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
1799 Variable* var = ref()->expression()->AsVariableProxy()->AsVariable();
1800 if (var != NULL) {
1801 ASSERT(var->is_global());
1802 __ Call(ic, code_target_context);
1803 } else {
1804 __ Call(ic, code_target);
1805 }
1806 } else {
1807 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
1808 __ Call(ic, code_target);
1809 }
1810 return;
1811 }
1812
1813 // Access keyed property.
1814 ASSERT(type == Reference::KEYED);
1815
1816 if (is_load) {
1817 __ push(r0); // empty tos
1818 // TODO(1224671): Implement inline caching for keyed loads as on ia32.
1819 GetPropertyStub stub;
1820 __ CallStub(&stub);
1821 } else {
1822 SetPropertyStub stub;
1823 __ CallStub(&stub);
1824 }
1825}
1826
1827
kasper.lund7276f142008-07-30 08:49:36 +00001828void ArmCodeGenerator::GenericBinaryOperation(Token::Value op) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 // Stub is entered with a call: 'return address' is in lr.
1830 switch (op) {
1831 case Token::ADD: // fall through.
1832 case Token::SUB: // fall through.
1833 case Token::MUL: {
kasper.lund7276f142008-07-30 08:49:36 +00001834 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 __ CallStub(&stub);
1836 break;
1837 }
1838
1839 case Token::DIV: {
1840 __ push(r0);
1841 __ mov(r0, Operand(1)); // set number of arguments
1842 __ InvokeBuiltin("DIV", 1, CALL_JS);
1843 break;
1844 }
1845
1846 case Token::MOD: {
1847 __ push(r0);
1848 __ mov(r0, Operand(1)); // set number of arguments
1849 __ InvokeBuiltin("MOD", 1, CALL_JS);
1850 break;
1851 }
1852
1853 case Token::BIT_OR:
1854 case Token::BIT_AND:
1855 case Token::BIT_XOR: {
1856 Label slow, exit;
1857 __ pop(r1); // get x
1858 // tag check
1859 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1860 ASSERT(kSmiTag == 0); // adjust code below
1861 __ tst(r2, Operand(kSmiTagMask));
1862 __ b(ne, &slow);
1863 switch (op) {
1864 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
1865 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
1866 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
1867 default: UNREACHABLE();
1868 }
1869 __ b(&exit);
1870 __ bind(&slow);
1871 __ push(r1); // restore stack
1872 __ push(r0);
1873 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
1874 switch (op) {
1875 case Token::BIT_OR: __ InvokeBuiltin("BIT_OR", 1, CALL_JS); break;
1876 case Token::BIT_AND: __ InvokeBuiltin("BIT_AND", 1, CALL_JS); break;
1877 case Token::BIT_XOR: __ InvokeBuiltin("BIT_XOR", 1, CALL_JS); break;
1878 default: UNREACHABLE();
1879 }
1880 __ bind(&exit);
1881 break;
1882 }
1883
1884 case Token::SHL:
1885 case Token::SHR:
1886 case Token::SAR: {
1887 Label slow, exit;
1888 __ mov(r1, Operand(r0)); // get y
1889 __ pop(r0); // get x
1890 // tag check
1891 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
1892 ASSERT(kSmiTag == 0); // adjust code below
1893 __ tst(r2, Operand(kSmiTagMask));
1894 __ b(ne, &slow);
1895 // get copies of operands
1896 __ mov(r3, Operand(r0));
1897 __ mov(r2, Operand(r1));
1898 // remove tags from operands (but keep sign)
1899 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
1900 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
1901 // use only the 5 least significant bits of the shift count
1902 __ and_(r2, r2, Operand(0x1f));
1903 // perform operation
1904 switch (op) {
1905 case Token::SAR:
1906 __ mov(r3, Operand(r3, ASR, r2));
1907 // no checks of result necessary
1908 break;
1909
1910 case Token::SHR:
1911 __ mov(r3, Operand(r3, LSR, r2));
1912 // check that the *unsigned* result fits in a smi
1913 // neither of the two high-order bits can be set:
1914 // - 0x80000000: high bit would be lost when smi tagging
1915 // - 0x40000000: this number would convert to negative when
1916 // smi tagging these two cases can only happen with shifts
1917 // by 0 or 1 when handed a valid smi
1918 __ and_(r2, r3, Operand(0xc0000000), SetCC);
1919 __ b(ne, &slow);
1920 break;
1921
1922 case Token::SHL:
1923 __ mov(r3, Operand(r3, LSL, r2));
1924 // check that the *signed* result fits in a smi
1925 __ add(r2, r3, Operand(0x40000000), SetCC);
1926 __ b(mi, &slow);
1927 break;
1928
1929 default: UNREACHABLE();
1930 }
1931 // tag result and store it in TOS (r0)
1932 ASSERT(kSmiTag == 0); // adjust code below
1933 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
1934 __ b(&exit);
1935 // slow case
1936 __ bind(&slow);
1937 __ push(r0); // restore stack
1938 __ mov(r0, Operand(r1));
1939 __ Push(Operand(1)); // 1 argument (not counting receiver).
1940 switch (op) {
1941 case Token::SAR: __ InvokeBuiltin("SAR", 1, CALL_JS); break;
1942 case Token::SHR: __ InvokeBuiltin("SHR", 1, CALL_JS); break;
1943 case Token::SHL: __ InvokeBuiltin("SHL", 1, CALL_JS); break;
1944 default: UNREACHABLE();
1945 }
1946 __ bind(&exit);
1947 break;
1948 }
1949
1950 case Token::COMMA:
1951 // simply discard left value
1952 __ add(sp, sp, Operand(kPointerSize));
1953 break;
1954
1955 default:
1956 // Other cases should have been handled before this point.
1957 UNREACHABLE();
1958 break;
1959 }
1960}
1961
1962
1963
1964
1965void ArmCodeGenerator::SmiOperation(Token::Value op,
1966 Handle<Object> value,
1967 bool reversed) {
1968 // NOTE: This is an attempt to inline (a bit) more of the code for
1969 // some possible smi operations (like + and -) when (at least) one
1970 // of the operands is a literal smi. With this optimization, the
1971 // performance of the system is increased by ~15%, and the generated
1972 // code size is increased by ~1% (measured on a combination of
1973 // different benchmarks).
1974
1975 ASSERT(value->IsSmi());
1976
1977 Label exit;
1978
1979 switch (op) {
1980 case Token::ADD: {
1981 Label slow;
1982
1983 __ mov(r1, Operand(value));
1984 __ add(r0, r0, Operand(r1), SetCC);
1985 __ b(vs, &slow);
1986 __ tst(r0, Operand(kSmiTagMask));
1987 __ b(eq, &exit);
1988 __ bind(&slow);
1989
1990 SmiOpStub stub(Token::ADD, reversed);
1991 __ CallStub(&stub);
1992 break;
1993 }
1994
1995 case Token::SUB: {
1996 Label slow;
1997
1998 __ mov(r1, Operand(value));
1999 if (!reversed) {
2000 __ sub(r2, r0, Operand(r1), SetCC);
2001 } else {
2002 __ rsb(r2, r0, Operand(r1), SetCC);
2003 }
2004 __ b(vs, &slow);
2005 __ tst(r2, Operand(kSmiTagMask));
2006 __ mov(r0, Operand(r2), LeaveCC, eq); // conditionally set r0 to result
2007 __ b(eq, &exit);
2008
2009 __ bind(&slow);
2010
2011 SmiOpStub stub(Token::SUB, reversed);
2012 __ CallStub(&stub);
2013 break;
2014 }
2015
2016 default:
2017 if (!reversed) {
2018 __ Push(Operand(value));
2019 } else {
2020 __ mov(ip, Operand(value));
2021 __ push(ip);
2022 }
kasper.lund7276f142008-07-30 08:49:36 +00002023 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 break;
2025 }
2026
2027 __ bind(&exit);
2028}
2029
2030
2031void ArmCodeGenerator::Comparison(Condition cc, bool strict) {
2032 // Strict only makes sense for equality comparisons.
2033 ASSERT(!strict || cc == eq);
2034
2035 Label exit, smi;
2036 __ pop(r1);
2037 __ orr(r2, r0, Operand(r1));
2038 __ tst(r2, Operand(kSmiTagMask));
2039 __ b(eq, &smi);
2040
2041 // Perform non-smi comparison by runtime call.
2042 __ push(r1);
2043
2044 // Figure out which native to call and setup the arguments.
2045 const char* native;
2046 int argc;
2047 if (cc == eq) {
2048 native = strict ? "STRICT_EQUALS" : "EQUALS";
2049 argc = 1;
2050 } else {
2051 native = "COMPARE";
2052 int ncr; // NaN compare result
2053 if (cc == lt || cc == le) {
2054 ncr = GREATER;
2055 } else {
2056 ASSERT(cc == gt || cc == ge); // remaining cases
2057 ncr = LESS;
2058 }
2059 __ Push(Operand(Smi::FromInt(ncr)));
2060 argc = 2;
2061 }
2062
2063 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2064 // tagged as a small integer.
2065 __ Push(Operand(argc));
2066 __ InvokeBuiltin(native, argc, CALL_JS);
2067 __ cmp(r0, Operand(0));
2068 __ b(&exit);
2069
2070 // test smi equality by pointer comparison.
2071 __ bind(&smi);
2072 __ cmp(r1, Operand(r0));
2073
2074 __ bind(&exit);
2075 __ pop(r0); // be careful not to destroy the cc register
2076 cc_reg_ = cc;
2077}
2078
2079
kasper.lund7276f142008-07-30 08:49:36 +00002080class CallFunctionStub: public CodeStub {
2081 public:
2082 explicit CallFunctionStub(int argc) : argc_(argc) {}
2083
2084 void Generate(MacroAssembler* masm);
2085
2086 private:
2087 int argc_;
2088
2089 const char* GetName() { return "CallFuntionStub"; }
2090
2091#if defined(DEBUG)
2092 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
2093#endif // defined(DEBUG)
2094
2095 Major MajorKey() { return CallFunction; }
2096 int MinorKey() { return argc_; }
2097};
2098
2099
2100void CallFunctionStub::Generate(MacroAssembler* masm) {
2101 Label slow;
2102
2103 // Push the number of arguments.
2104 masm->Push(Operand(argc_));
2105
2106 // Get the function to call from the stack.
2107 // function, receiver [, arguments], argc_
2108 masm->ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
2109
2110 // Check that the function is really a JavaScript function.
2111 masm->tst(r1, Operand(kSmiTagMask));
2112 masm->b(eq, &slow);
2113 // Get the map of the function object.
2114 masm->ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2115 masm->ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2116 masm->cmp(r2, Operand(JS_FUNCTION_TYPE));
2117 masm->b(ne, &slow);
2118
2119 // Fast-case: Invoke the function now.
2120 masm->ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
2121 masm->ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
2122 masm->ldr(r1,
2123 MemOperand(r1, SharedFunctionInfo::kCodeOffset - kHeapObjectTag));
2124 masm->add(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
2125 masm->Jump(r1); // Callee will return to the original call site directly.
2126
2127 // Slow-case: Non-function called.
2128 masm->bind(&slow);
2129 masm->InvokeBuiltin("CALL_NON_FUNCTION", 0, JUMP_JS);
2130}
2131
2132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133// Call the function just below TOS on the stack with the given
2134// arguments. The receiver is the TOS.
2135void ArmCodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
2136 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 // Push the arguments ("left-to-right") on the stack.
2138 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2139
kasper.lund7276f142008-07-30 08:49:36 +00002140 // Record the position for debugging purposes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141 __ RecordPosition(position);
2142
kasper.lund7276f142008-07-30 08:49:36 +00002143 // Use the shared code stub to call the function.
2144 CallFunctionStub call_function(args->length());
2145 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146
2147 // Restore context and pop function from the stack.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2149 __ add(sp, sp, Operand(kPointerSize)); // discard
2150}
2151
2152
2153void ArmCodeGenerator::Branch(bool if_true, Label* L) {
2154 ASSERT(has_cc());
2155 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
2156 __ b(cc, L);
2157 cc_reg_ = al;
2158}
2159
2160
2161void ArmCodeGenerator::CheckStack() {
2162 if (FLAG_check_stack) {
2163 Comment cmnt(masm_, "[ check stack");
2164 StackCheckStub stub;
2165 __ CallStub(&stub);
2166 }
2167}
2168
2169
2170void ArmCodeGenerator::VisitBlock(Block* node) {
2171 Comment cmnt(masm_, "[ Block");
2172 if (FLAG_debug_info) RecordStatementPosition(node);
2173 node->set_break_stack_height(break_stack_height_);
2174 VisitStatements(node->statements());
2175 __ bind(node->break_target());
2176}
2177
2178
2179void ArmCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
2180 __ Push(Operand(pairs));
2181 __ Push(Operand(cp));
2182 __ Push(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
2183 __ CallRuntime(Runtime::kDeclareGlobals, 3);
2184
2185 // Get rid of return value.
2186 __ pop(r0);
2187}
2188
2189
2190void ArmCodeGenerator::VisitDeclaration(Declaration* node) {
2191 Comment cmnt(masm_, "[ Declaration");
2192 Variable* var = node->proxy()->var();
2193 ASSERT(var != NULL); // must have been resolved
2194 Slot* slot = var->slot();
2195
2196 // If it was not possible to allocate the variable at compile time,
2197 // we need to "declare" it at runtime to make sure it actually
2198 // exists in the local context.
2199 if (slot != NULL && slot->type() == Slot::LOOKUP) {
2200 // Variables with a "LOOKUP" slot were introduced as non-locals
2201 // during variable resolution and must have mode DYNAMIC.
2202 ASSERT(var->mode() == Variable::DYNAMIC);
2203 // For now, just do a runtime call.
2204 __ Push(Operand(cp));
2205 __ Push(Operand(var->name()));
2206 // Declaration nodes are always declared in only two modes.
2207 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
2208 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
2209 __ Push(Operand(Smi::FromInt(attr)));
2210 // Push initial value, if any.
2211 // Note: For variables we must not push an initial value (such as
2212 // 'undefined') because we may have a (legal) redeclaration and we
2213 // must not destroy the current value.
2214 if (node->mode() == Variable::CONST) {
2215 __ Push(Operand(Factory::the_hole_value()));
2216 } else if (node->fun() != NULL) {
2217 Load(node->fun());
2218 } else {
2219 __ Push(Operand(0)); // no initial value!
2220 }
2221 __ CallRuntime(Runtime::kDeclareContextSlot, 5);
2222 // DeclareContextSlot pops the assigned value by accepting an
2223 // extra argument and returning the TOS; no need to explicitly pop
2224 // here.
2225 return;
2226 }
2227
2228 ASSERT(!var->is_global());
2229
2230 // If we have a function or a constant, we need to initialize the variable.
2231 Expression* val = NULL;
2232 if (node->mode() == Variable::CONST) {
2233 val = new Literal(Factory::the_hole_value());
2234 } else {
2235 val = node->fun(); // NULL if we don't have a function
2236 }
2237
2238 if (val != NULL) {
2239 // Set initial value.
2240 Reference target(this, node->proxy());
2241 Load(val);
2242 SetValue(&target);
2243 // Get rid of the assigned value (declarations are statements).
2244 __ pop(r0); // Pop(no_reg);
2245 }
2246}
2247
2248
2249void ArmCodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
2250 Comment cmnt(masm_, "[ ExpressionStatement");
2251 if (FLAG_debug_info) RecordStatementPosition(node);
2252 Expression* expression = node->expression();
2253 expression->MarkAsStatement();
2254 Load(expression);
2255 __ pop(r0); // __ Pop(no_reg)
2256}
2257
2258
2259void ArmCodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
2260 Comment cmnt(masm_, "// EmptyStatement");
2261 // nothing to do
2262}
2263
2264
2265void ArmCodeGenerator::VisitIfStatement(IfStatement* node) {
2266 Comment cmnt(masm_, "[ IfStatement");
2267 // Generate different code depending on which
2268 // parts of the if statement are present or not.
2269 bool has_then_stm = node->HasThenStatement();
2270 bool has_else_stm = node->HasElseStatement();
2271
2272 if (FLAG_debug_info) RecordStatementPosition(node);
2273
2274 Label exit;
2275 if (has_then_stm && has_else_stm) {
2276 Label then;
2277 Label else_;
2278 // if (cond)
2279 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
2280 Branch(false, &else_);
2281 // then
2282 __ bind(&then);
2283 Visit(node->then_statement());
2284 __ b(&exit);
2285 // else
2286 __ bind(&else_);
2287 Visit(node->else_statement());
2288
2289 } else if (has_then_stm) {
2290 ASSERT(!has_else_stm);
2291 Label then;
2292 // if (cond)
2293 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &exit, true);
2294 Branch(false, &exit);
2295 // then
2296 __ bind(&then);
2297 Visit(node->then_statement());
2298
2299 } else if (has_else_stm) {
2300 ASSERT(!has_then_stm);
2301 Label else_;
2302 // if (!cond)
2303 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &else_, true);
2304 Branch(true, &exit);
2305 // else
2306 __ bind(&else_);
2307 Visit(node->else_statement());
2308
2309 } else {
2310 ASSERT(!has_then_stm && !has_else_stm);
2311 // if (cond)
2312 LoadCondition(node->condition(), CodeGenState::LOAD, &exit, &exit, false);
2313 if (has_cc()) {
2314 cc_reg_ = al;
2315 } else {
2316 __ pop(r0); // __ Pop(no_reg)
2317 }
2318 }
2319
2320 // end
2321 __ bind(&exit);
2322}
2323
2324
2325void ArmCodeGenerator::CleanStack(int num_bytes) {
2326 ASSERT(num_bytes >= 0);
2327 if (num_bytes > 0) {
2328 __ add(sp, sp, Operand(num_bytes - kPointerSize));
2329 __ pop(r0);
2330 }
2331}
2332
2333
2334void ArmCodeGenerator::VisitContinueStatement(ContinueStatement* node) {
2335 Comment cmnt(masm_, "[ ContinueStatement");
2336 if (FLAG_debug_info) RecordStatementPosition(node);
2337 CleanStack(break_stack_height_ - node->target()->break_stack_height());
2338 __ b(node->target()->continue_target());
2339}
2340
2341
2342void ArmCodeGenerator::VisitBreakStatement(BreakStatement* node) {
2343 Comment cmnt(masm_, "[ BreakStatement");
2344 if (FLAG_debug_info) RecordStatementPosition(node);
2345 CleanStack(break_stack_height_ - node->target()->break_stack_height());
2346 __ b(node->target()->break_target());
2347}
2348
2349
2350void ArmCodeGenerator::VisitReturnStatement(ReturnStatement* node) {
2351 Comment cmnt(masm_, "[ ReturnStatement");
2352 if (FLAG_debug_info) RecordStatementPosition(node);
2353 Load(node->expression());
2354 __ b(&function_return_);
2355}
2356
2357
2358void ArmCodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
2359 Comment cmnt(masm_, "[ WithEnterStatement");
2360 if (FLAG_debug_info) RecordStatementPosition(node);
2361 Load(node->expression());
kasper.lund7276f142008-07-30 08:49:36 +00002362 __ CallRuntime(Runtime::kPushContext, 1);
2363 if (kDebug) {
2364 Label verified_true;
2365 __ cmp(r0, Operand(cp));
2366 __ b(eq, &verified_true);
2367 __ stop("PushContext: r0 is expected to be the same as cp");
2368 __ bind(&verified_true);
2369 }
2370 __ pop(r0); // restore TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371 // Update context local.
2372 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2373}
2374
2375
2376void ArmCodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
2377 Comment cmnt(masm_, "[ WithExitStatement");
2378 // Pop context.
2379 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2380 // Update context local.
2381 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2382}
2383
2384
2385void ArmCodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
2386 Comment cmnt(masm_, "[ SwitchStatement");
2387 if (FLAG_debug_info) RecordStatementPosition(node);
2388 node->set_break_stack_height(break_stack_height_);
2389
2390 Load(node->tag());
2391
2392 Label next, fall_through, default_case;
2393 ZoneList<CaseClause*>* cases = node->cases();
2394 int length = cases->length();
2395
2396 for (int i = 0; i < length; i++) {
2397 CaseClause* clause = cases->at(i);
2398
2399 Comment cmnt(masm_, "[ case clause");
2400
2401 if (clause->is_default()) {
2402 // Bind the default case label, so we can branch to it when we
2403 // have compared against all other cases.
2404 ASSERT(default_case.is_unused()); // at most one default clause
2405
2406 // If the default case is the first (but not only) case, we have
2407 // to jump past it for now. Once we're done with the remaining
2408 // clauses, we'll branch back here. If it isn't the first case,
2409 // we jump past it by avoiding to chain it into the next chain.
2410 if (length > 1) {
2411 if (i == 0) __ b(&next);
2412 __ bind(&default_case);
2413 }
2414
2415 } else {
2416 __ bind(&next);
2417 next.Unuse();
2418 __ push(r0); // duplicate TOS
2419 Load(clause->label());
2420 Comparison(eq, true);
2421 Branch(false, &next);
2422 __ pop(r0); // __ Pop(no_reg)
2423 }
2424
2425 // Generate code for the body.
2426 __ bind(&fall_through);
2427 fall_through.Unuse();
2428 VisitStatements(clause->statements());
2429 __ b(&fall_through);
2430 }
2431
2432 __ bind(&next);
2433 __ pop(r0); // __ Pop(no_reg)
2434 if (default_case.is_bound()) __ b(&default_case);
2435
2436 __ bind(&fall_through);
2437 __ bind(node->break_target());
2438}
2439
2440
2441void ArmCodeGenerator::VisitLoopStatement(LoopStatement* node) {
2442 Comment cmnt(masm_, "[ LoopStatement");
2443 if (FLAG_debug_info) RecordStatementPosition(node);
2444 node->set_break_stack_height(break_stack_height_);
2445
2446 // simple condition analysis
2447 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
2448 if (node->cond() == NULL) {
2449 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2450 info = ALWAYS_TRUE;
2451 } else {
2452 Literal* lit = node->cond()->AsLiteral();
2453 if (lit != NULL) {
2454 if (lit->IsTrue()) {
2455 info = ALWAYS_TRUE;
2456 } else if (lit->IsFalse()) {
2457 info = ALWAYS_FALSE;
2458 }
2459 }
2460 }
2461
2462 Label loop, entry;
2463
2464 // init
2465 if (node->init() != NULL) {
2466 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2467 Visit(node->init());
2468 }
2469 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
2470 __ b(&entry);
2471 }
2472
2473 // body
2474 __ bind(&loop);
2475 Visit(node->body());
2476
2477 // next
2478 __ bind(node->continue_target());
2479 if (node->next() != NULL) {
2480 // Record source position of the statement as this code which is after the
2481 // code for the body actually belongs to the loop statement and not the
2482 // body.
2483 if (FLAG_debug_info) __ RecordPosition(node->statement_pos());
2484 ASSERT(node->type() == LoopStatement::FOR_LOOP);
2485 Visit(node->next());
2486 }
2487
2488 // cond
2489 __ bind(&entry);
2490 switch (info) {
2491 case ALWAYS_TRUE:
2492 CheckStack(); // TODO(1222600): ignore if body contains calls.
2493 __ b(&loop);
2494 break;
2495 case ALWAYS_FALSE:
2496 break;
2497 case DONT_KNOW:
2498 CheckStack(); // TODO(1222600): ignore if body contains calls.
2499 LoadCondition(node->cond(),
2500 CodeGenState::LOAD,
2501 &loop,
2502 node->break_target(),
2503 true);
2504 Branch(true, &loop);
2505 break;
2506 }
2507
2508 // exit
2509 __ bind(node->break_target());
2510}
2511
2512
2513void ArmCodeGenerator::VisitForInStatement(ForInStatement* node) {
2514 Comment cmnt(masm_, "[ ForInStatement");
2515 if (FLAG_debug_info) RecordStatementPosition(node);
2516
2517 // We keep stuff on the stack while the body is executing.
2518 // Record it, so that a break/continue crossing this statement
2519 // can restore the stack.
2520 const int kForInStackSize = 5 * kPointerSize;
2521 break_stack_height_ += kForInStackSize;
2522 node->set_break_stack_height(break_stack_height_);
2523
2524 Label loop, next, entry, cleanup, exit, primitive, jsobject;
2525 Label filter_key, end_del_check, fixed_array, non_string;
2526
2527 // Get the object to enumerate over (converted to JSObject).
2528 Load(node->enumerable());
2529
2530 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2531 // to the specification. 12.6.4 mandates a call to ToObject.
2532 __ cmp(r0, Operand(Factory::undefined_value()));
2533 __ b(eq, &exit);
2534 __ cmp(r0, Operand(Factory::null_value()));
2535 __ b(eq, &exit);
2536
2537 // Stack layout in body:
2538 // [iteration counter (Smi)]
2539 // [length of array]
2540 // [FixedArray]
2541 // [Map or 0]
2542 // [Object]
2543
2544 // Check if enumerable is already a JSObject
2545 __ tst(r0, Operand(kSmiTagMask));
2546 __ b(eq, &primitive);
2547 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2548 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2549 __ cmp(r1, Operand(JS_OBJECT_TYPE));
2550 __ b(hs, &jsobject);
2551
2552 __ bind(&primitive);
2553 __ Push(Operand(0));
2554 __ InvokeBuiltin("TO_OBJECT", 0, CALL_JS);
2555
2556
2557 __ bind(&jsobject);
2558
2559 // Get the set of properties (as a FixedArray or Map).
2560 __ push(r0); // duplicate the object being enumerated
2561 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
2562
2563 // If we got a Map, we can do a fast modification check.
2564 // Otherwise, we got a FixedArray, and we have to do a slow check.
2565 __ mov(r2, Operand(r0));
2566 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
2567 __ cmp(r1, Operand(Factory::meta_map()));
2568 __ b(ne, &fixed_array);
2569
2570 // Get enum cache
2571 __ mov(r1, Operand(r0));
2572 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2573 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2574 __ ldr(r2,
2575 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2576
2577 __ Push(Operand(r2));
2578 __ Push(FieldMemOperand(r2, FixedArray::kLengthOffset));
2579 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
2580 __ Push(Operand(Smi::FromInt(0)));
2581 __ b(&entry);
2582
2583
2584 __ bind(&fixed_array);
2585
2586 __ mov(r1, Operand(Smi::FromInt(0)));
2587 __ push(r1); // insert 0 in place of Map
2588
2589 // Push the length of the array and the initial index onto the stack.
2590 __ Push(FieldMemOperand(r0, FixedArray::kLengthOffset));
2591 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
2592 __ Push(Operand(Smi::FromInt(0)));
2593 __ b(&entry);
2594
2595 // Body.
2596 __ bind(&loop);
2597 Visit(node->body());
2598
2599 // Next.
2600 __ bind(node->continue_target());
2601 __ bind(&next);
2602 __ add(r0, r0, Operand(Smi::FromInt(1)));
2603
2604 // Condition.
2605 __ bind(&entry);
2606
2607 __ ldr(ip, MemOperand(sp, 0));
2608 __ cmp(r0, Operand(ip));
2609 __ b(hs, &cleanup);
2610
2611 // Get the i'th entry of the array.
2612 __ ldr(r2, MemOperand(sp, kPointerSize));
2613 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2614 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2615
2616 // Get Map or 0.
2617 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
2618 // Check if this (still) matches the map of the enumerable.
2619 // If not, we have to filter the key.
2620 __ ldr(r1, MemOperand(sp, 3 * kPointerSize));
2621 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2622 __ cmp(r1, Operand(r2));
2623 __ b(eq, &end_del_check);
2624
2625 // Convert the entry to a string (or null if it isn't a property anymore).
2626 __ Push(MemOperand(sp, 4 * kPointerSize)); // push enumerable
2627 __ Push(Operand(r3)); // push entry
2628 __ Push(Operand(1));
2629 __ InvokeBuiltin("FILTER_KEY", 1, CALL_JS);
2630 __ mov(r3, Operand(r0));
2631 __ pop(r0);
2632
2633 // If the property has been removed while iterating, we just skip it.
2634 __ cmp(r3, Operand(Factory::null_value()));
2635 __ b(eq, &next);
2636
2637
2638 __ bind(&end_del_check);
2639
2640 // Store the entry in the 'each' expression and take another spin in the loop.
2641 __ Push(Operand(r3));
2642 { Reference each(this, node->each());
2643 if (!each.is_illegal()) {
2644 if (each.size() > 0) __ Push(MemOperand(sp, kPointerSize * each.size()));
2645 SetValue(&each);
2646 if (each.size() > 0) __ pop(r0);
2647 }
2648 }
2649 __ pop(r0);
2650 CheckStack(); // TODO(1222600): ignore if body contains calls.
2651 __ jmp(&loop);
2652
2653 // Cleanup.
2654 __ bind(&cleanup);
2655 __ bind(node->break_target());
2656 __ add(sp, sp, Operand(4 * kPointerSize));
2657
2658 // Exit.
2659 __ bind(&exit);
2660 __ pop(r0);
2661
2662 break_stack_height_ -= kForInStackSize;
2663}
2664
2665
2666void ArmCodeGenerator::VisitTryCatch(TryCatch* node) {
2667 Comment cmnt(masm_, "[ TryCatch");
2668
2669 Label try_block, exit;
2670
2671 __ push(r0);
2672 __ bl(&try_block);
2673
2674
2675 // --- Catch block ---
2676
2677 // Store the caught exception in the catch variable.
2678 { Reference ref(this, node->catch_var());
2679 // Load the exception to the top of the stack.
2680 __ Push(MemOperand(sp, ref.size() * kPointerSize));
2681 SetValue(&ref);
2682 }
2683
2684 // Remove the exception from the stack.
2685 __ add(sp, sp, Operand(kPointerSize));
2686
2687 // Restore TOS register caching.
2688 __ pop(r0);
2689
2690 VisitStatements(node->catch_block()->statements());
2691 __ b(&exit);
2692
2693
2694 // --- Try block ---
2695 __ bind(&try_block);
2696
2697 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
2698
2699 // Introduce shadow labels for all escapes from the try block,
2700 // including returns. We should probably try to unify the escaping
2701 // labels and the return label.
2702 int nof_escapes = node->escaping_labels()->length();
2703 List<LabelShadow*> shadows(1 + nof_escapes);
2704 shadows.Add(new LabelShadow(&function_return_));
2705 for (int i = 0; i < nof_escapes; i++) {
2706 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
2707 }
2708
2709 // Generate code for the statements in the try block.
2710 VisitStatements(node->try_block()->statements());
2711
2712 // Stop the introduced shadowing and count the number of required unlinks.
2713 int nof_unlinks = 0;
2714 for (int i = 0; i <= nof_escapes; i++) {
2715 shadows[i]->StopShadowing();
2716 if (shadows[i]->is_linked()) nof_unlinks++;
2717 }
2718
2719 // Unlink from try chain.
2720 // TOS contains code slot
2721 const int kNextOffset = StackHandlerConstants::kNextOffset +
2722 StackHandlerConstants::kAddressDisplacement;
2723 __ ldr(r1, MemOperand(sp, kNextOffset)); // read next_sp
2724 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2725 __ str(r1, MemOperand(r3));
2726 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
2727 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2728 // Code slot popped.
2729 __ pop(r0); // restore TOS
2730 if (nof_unlinks > 0) __ b(&exit);
2731
2732 // Generate unlink code for all used shadow labels.
2733 for (int i = 0; i <= nof_escapes; i++) {
2734 if (shadows[i]->is_linked()) {
2735 // Unlink from try chain; be careful not to destroy the TOS.
2736 __ bind(shadows[i]);
2737
2738 bool is_return = (shadows[i]->shadowed() == &function_return_);
2739 if (!is_return) {
2740 // Break/continue case. TOS is the code slot of the handler.
2741 __ push(r0); // flush TOS
2742 }
2743
2744 // Reload sp from the top handler, because some statements that we
2745 // break from (eg, for...in) may have left stuff on the stack.
2746 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2747 __ ldr(sp, MemOperand(r3));
2748
2749 __ ldr(r1, MemOperand(sp, kNextOffset));
2750 __ str(r1, MemOperand(r3));
2751 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
2752 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2753 // Code slot popped.
2754
2755 if (!is_return) {
2756 __ pop(r0); // restore TOS
2757 }
2758
2759 __ b(shadows[i]->shadowed());
2760 }
2761 }
2762
2763 __ bind(&exit);
2764}
2765
2766
2767void ArmCodeGenerator::VisitTryFinally(TryFinally* node) {
2768 Comment cmnt(masm_, "[ TryFinally");
2769
2770 // State: Used to keep track of reason for entering the finally
2771 // block. Should probably be extended to hold information for
2772 // break/continue from within the try block.
2773 enum { FALLING, THROWING, JUMPING };
2774
2775 Label exit, unlink, try_block, finally_block;
2776
2777 __ push(r0);
2778 __ bl(&try_block);
2779
2780 // In case of thrown exceptions, this is where we continue.
2781 __ mov(r2, Operand(Smi::FromInt(THROWING)));
2782 __ b(&finally_block);
2783
2784
2785 // --- Try block ---
2786 __ bind(&try_block);
2787
2788 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
2789
2790 // Introduce shadow labels for all escapes from the try block,
2791 // including returns. We should probably try to unify the escaping
2792 // labels and the return label.
2793 int nof_escapes = node->escaping_labels()->length();
2794 List<LabelShadow*> shadows(1 + nof_escapes);
2795 shadows.Add(new LabelShadow(&function_return_));
2796 for (int i = 0; i < nof_escapes; i++) {
2797 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
2798 }
2799
2800 // Generate code for the statements in the try block.
2801 VisitStatements(node->try_block()->statements());
2802
2803 // Stop the introduced shadowing and count the number of required
2804 // unlinks.
2805 int nof_unlinks = 0;
2806 for (int i = 0; i <= nof_escapes; i++) {
2807 shadows[i]->StopShadowing();
2808 if (shadows[i]->is_linked()) nof_unlinks++;
2809 }
2810
2811 // Set the state on the stack to FALLING.
2812 __ Push(Operand(Factory::undefined_value())); // fake TOS
2813 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2814 if (nof_unlinks > 0) __ b(&unlink);
2815
2816 // Generate code that sets the state for all used shadow labels.
2817 for (int i = 0; i <= nof_escapes; i++) {
2818 if (shadows[i]->is_linked()) {
2819 __ bind(shadows[i]);
2820 if (shadows[i]->shadowed() != &function_return_) {
2821 // Fake TOS for break and continue (not return).
2822 __ Push(Operand(Factory::undefined_value()));
2823 }
2824 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
2825 __ b(&unlink);
2826 }
2827 }
2828
2829 // Unlink from try chain; be careful not to destroy the TOS.
2830 __ bind(&unlink);
2831
2832 // Reload sp from the top handler, because some statements that we
2833 // break from (eg, for...in) may have left stuff on the stack.
2834 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2835 __ ldr(sp, MemOperand(r3));
2836 const int kNextOffset = StackHandlerConstants::kNextOffset +
2837 StackHandlerConstants::kAddressDisplacement;
2838 __ ldr(r1, MemOperand(sp, kNextOffset));
2839 __ str(r1, MemOperand(r3));
2840 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
2841 __ add(sp, sp, Operand(StackHandlerConstants::kSize - kPointerSize));
2842 // Code slot popped.
2843
2844
2845 // --- Finally block ---
2846 __ bind(&finally_block);
2847
2848 // Push the state on the stack. If necessary move the state to a
2849 // local variable to avoid having extra values on the stack while
2850 // evaluating the finally block.
2851 __ Push(Operand(r2));
2852 if (node->finally_var() != NULL) {
2853 Reference target(this, node->finally_var());
2854 SetValue(&target);
2855 ASSERT(target.size() == 0); // no extra stuff on the stack
2856 __ pop(r0);
2857 }
2858
2859 // Generate code for the statements in the finally block.
2860 VisitStatements(node->finally_block()->statements());
2861
2862 // Get the state from the stack - or the local variable - and
2863 // restore the TOS register.
2864 if (node->finally_var() != NULL) {
2865 Reference target(this, node->finally_var());
2866 GetValue(&target);
2867 }
2868 __ Pop(r2);
2869
2870 // Generate code that jumps to the right destination for all used
2871 // shadow labels.
2872 for (int i = 0; i <= nof_escapes; i++) {
2873 if (shadows[i]->is_bound()) {
2874 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2875 if (shadows[i]->shadowed() != &function_return_) {
2876 Label next;
2877 __ b(ne, &next);
2878 __ pop(r0); // pop faked TOS
2879 __ b(shadows[i]->shadowed());
2880 __ bind(&next);
2881 } else {
2882 __ b(eq, shadows[i]->shadowed());
2883 }
2884 }
2885 }
2886
2887 // Check if we need to rethrow the exception.
2888 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2889 __ b(ne, &exit);
2890
2891 // Rethrow exception.
2892 __ CallRuntime(Runtime::kReThrow, 1);
2893
2894 // Done.
2895 __ bind(&exit);
2896 __ pop(r0); // restore TOS caching.
2897}
2898
2899
2900void ArmCodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
2901 Comment cmnt(masm_, "[ DebuggerStatament");
2902 if (FLAG_debug_info) RecordStatementPosition(node);
2903 __ CallRuntime(Runtime::kDebugBreak, 1);
2904}
2905
2906
2907void ArmCodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
2908 ASSERT(boilerplate->IsBoilerplate());
2909
2910 // Push the boilerplate on the stack.
2911 __ Push(Operand(boilerplate));
2912
2913 // Create a new closure.
2914 __ Push(Operand(cp));
2915 __ CallRuntime(Runtime::kNewClosure, 2);
2916}
2917
2918
2919void ArmCodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
2920 Comment cmnt(masm_, "[ FunctionLiteral");
2921
2922 // Build the function boilerplate and instantiate it.
2923 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002924 // Check for stack-overflow exception.
2925 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926 InstantiateBoilerplate(boilerplate);
2927}
2928
2929
2930void ArmCodeGenerator::VisitFunctionBoilerplateLiteral(
2931 FunctionBoilerplateLiteral* node) {
2932 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2933 InstantiateBoilerplate(node->boilerplate());
2934}
2935
2936
2937void ArmCodeGenerator::VisitConditional(Conditional* node) {
2938 Comment cmnt(masm_, "[ Conditional");
2939 Label then, else_, exit;
2940 LoadCondition(node->condition(), CodeGenState::LOAD, &then, &else_, true);
2941 Branch(false, &else_);
2942 __ bind(&then);
2943 Load(node->then_expression(), access());
2944 __ b(&exit);
2945 __ bind(&else_);
2946 Load(node->else_expression(), access());
2947 __ bind(&exit);
2948}
2949
2950
2951void ArmCodeGenerator::VisitSlot(Slot* node) {
2952 Comment cmnt(masm_, "[ Slot");
2953
2954 if (node->type() == Slot::LOOKUP) {
2955 ASSERT(node->var()->mode() == Variable::DYNAMIC);
2956
2957 // For now, just do a runtime call.
2958 __ Push(Operand(cp));
2959 __ Push(Operand(node->var()->name()));
2960
2961 switch (access()) {
2962 case CodeGenState::UNDEFINED:
2963 UNREACHABLE();
2964 break;
2965
2966 case CodeGenState::LOAD:
2967 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2968 // result (TOS) is the value that was loaded
2969 break;
2970
2971 case CodeGenState::LOAD_TYPEOF_EXPR:
2972 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
2973 // result (TOS) is the value that was loaded
2974 break;
2975
2976 case CodeGenState::STORE:
2977 // Storing a variable must keep the (new) value on the stack. This
2978 // is necessary for compiling assignment expressions.
2979 __ CallRuntime(Runtime::kStoreContextSlot, 3);
2980 // result (TOS) is the value that was stored
2981 break;
2982
2983 case CodeGenState::INIT_CONST:
2984 // Same as STORE but ignores attribute (e.g. READ_ONLY) of
2985 // context slot so that we can initialize const properties
2986 // (introduced via eval("const foo = (some expr);")). Also,
2987 // uses the current function context instead of the top
2988 // context.
2989 //
2990 // Note that we must declare the foo upon entry of eval(),
2991 // via a context slot declaration, but we cannot initialize
2992 // it at the same time, because the const declaration may
2993 // be at the end of the eval code (sigh...) and the const
2994 // variable may have been used before (where its value is
2995 // 'undefined'). Thus, we can only do the initialization
2996 // when we actually encounter the expression and when the
2997 // expression operands are defined and valid, and thus we
2998 // need the split into 2 operations: declaration of the
2999 // context slot followed by initialization.
3000 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3001 break;
3002 }
3003
3004 } else {
3005 // Note: We would like to keep the assert below, but it fires because
3006 // of some nasty code in LoadTypeofExpression() which should be removed...
3007 // ASSERT(node->var()->mode() != Variable::DYNAMIC);
3008
3009 switch (access()) {
3010 case CodeGenState::UNDEFINED:
3011 UNREACHABLE();
3012 break;
3013
3014 case CodeGenState::LOAD: // fall through
3015 case CodeGenState::LOAD_TYPEOF_EXPR:
3016 // Special handling for locals allocated in registers.
kasper.lund7276f142008-07-30 08:49:36 +00003017 __ Push(SlotOperand(node, r2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003018 if (node->var()->mode() == Variable::CONST) {
3019 // Const slots may contain 'the hole' value (the constant hasn't
3020 // been initialized yet) which needs to be converted into the
3021 // 'undefined' value.
3022 Comment cmnt(masm_, "[ Unhole const");
3023 __ cmp(r0, Operand(Factory::the_hole_value()));
3024 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
3025 }
3026 break;
3027
3028 case CodeGenState::INIT_CONST: {
3029 ASSERT(node->var()->mode() == Variable::CONST);
3030 // Only the first const initialization must be executed (the slot
3031 // still contains 'the hole' value). When the assignment is executed,
3032 // the code is identical to a normal store (see below).
3033 { Comment cmnt(masm_, "[ Init const");
3034 Label L;
kasper.lund7276f142008-07-30 08:49:36 +00003035 __ ldr(r2, SlotOperand(node, r2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036 __ cmp(r2, Operand(Factory::the_hole_value()));
3037 __ b(ne, &L);
3038 // We must execute the store.
kasper.lund7276f142008-07-30 08:49:36 +00003039 // r2 may be loaded with context; used below in RecordWrite.
3040 __ str(r0, SlotOperand(node, r2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003041 if (node->type() == Slot::CONTEXT) {
3042 // Skip write barrier if the written value is a smi.
3043 Label exit;
3044 __ tst(r0, Operand(kSmiTagMask));
3045 __ b(eq, &exit);
3046 // r2 is loaded with context when calling SlotOperand above.
3047 int offset = FixedArray::kHeaderSize + node->index() * kPointerSize;
3048 __ mov(r3, Operand(offset));
3049 __ RecordWrite(r2, r3, r1);
3050 __ bind(&exit);
3051 }
3052 __ bind(&L);
3053 }
3054 break;
3055 }
3056
3057 case CodeGenState::STORE: {
3058 // Storing a variable must keep the (new) value on the stack. This
3059 // is necessary for compiling assignment expressions.
3060 // Special handling for locals allocated in registers.
3061 //
3062 // Note: We will reach here even with node->var()->mode() ==
3063 // Variable::CONST because of const declarations which will
3064 // initialize consts to 'the hole' value and by doing so, end
3065 // up calling this code.
kasper.lund7276f142008-07-30 08:49:36 +00003066 // r2 may be loaded with context; used below in RecordWrite.
3067 __ str(r0, SlotOperand(node, r2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 if (node->type() == Slot::CONTEXT) {
3069 // Skip write barrier if the written value is a smi.
3070 Label exit;
3071 __ tst(r0, Operand(kSmiTagMask));
3072 __ b(eq, &exit);
3073 // r2 is loaded with context when calling SlotOperand above.
3074 int offset = FixedArray::kHeaderSize + node->index() * kPointerSize;
3075 __ mov(r3, Operand(offset));
3076 __ RecordWrite(r2, r3, r1);
3077 __ bind(&exit);
3078 }
3079 break;
3080 }
3081 }
3082 }
3083}
3084
3085
3086void ArmCodeGenerator::VisitVariableProxy(VariableProxy* proxy_node) {
3087 Comment cmnt(masm_, "[ VariableProxy");
3088 Variable* node = proxy_node->var();
3089
3090 Expression* x = node->rewrite();
3091 if (x != NULL) {
3092 Visit(x);
3093 return;
3094 }
3095
3096 ASSERT(node->is_global());
3097 if (is_referenced()) {
3098 if (node->AsProperty() != NULL) {
3099 __ RecordPosition(node->AsProperty()->position());
3100 }
3101 AccessReferenceProperty(new Literal(node->name()), access());
3102
3103 } else {
3104 // All stores are through references.
3105 ASSERT(access() != CodeGenState::STORE);
3106 Reference property(this, proxy_node);
3107 GetValue(&property);
3108 }
3109}
3110
3111
3112void ArmCodeGenerator::VisitLiteral(Literal* node) {
3113 Comment cmnt(masm_, "[ Literal");
3114 __ Push(Operand(node->handle()));
3115}
3116
3117
3118void ArmCodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
3119 Comment cmnt(masm_, "[ RexExp Literal");
3120
3121 // Retrieve the literal array and check the allocated entry.
3122
3123 // Load the function of this activation.
3124 __ ldr(r1, MemOperand(pp, 0));
3125
3126 // Load the literals array of the function.
3127 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3128
3129 // Load the literal at the ast saved index.
3130 int literal_offset =
3131 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3132 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3133
3134 Label done;
3135 __ cmp(r2, Operand(Factory::undefined_value()));
3136 __ b(ne, &done);
3137
3138 // If the entry is undefined we call the runtime system to computed
3139 // the literal.
3140 __ Push(Operand(r1)); // literal array (0)
3141 __ Push(Operand(Smi::FromInt(node->literal_index()))); // literal index (1)
3142 __ Push(Operand(node->pattern())); // RegExp pattern (2)
3143 __ Push(Operand(node->flags())); // RegExp flags (3)
3144 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
3145 __ Pop(r2);
3146 __ bind(&done);
3147
3148 // Push the literal.
3149 __ Push(Operand(r2));
3150}
3151
3152
3153// This deferred code stub will be used for creating the boilerplate
3154// by calling Runtime_CreateObjectLiteral.
3155// Each created boilerplate is stored in the JSFunction and they are
3156// therefore context dependent.
3157class ObjectLiteralDeferred: public DeferredCode {
3158 public:
3159 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
3160 : DeferredCode(generator), node_(node) {
3161 set_comment("[ ObjectLiteralDeferred");
3162 }
3163 virtual void Generate();
3164 private:
3165 ObjectLiteral* node_;
3166};
3167
3168
3169void ObjectLiteralDeferred::Generate() {
3170 // If the entry is undefined we call the runtime system to computed
3171 // the literal.
3172
3173 // Literal array (0).
3174 __ Push(Operand(r1));
3175 // Literal index (1).
3176 __ Push(Operand(Smi::FromInt(node_->literal_index())));
3177 // Constant properties (2).
3178 __ Push(Operand(node_->constant_properties()));
3179 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
3180 __ Pop(r2);
3181}
3182
3183
3184void ArmCodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
3185 Comment cmnt(masm_, "[ ObjectLiteral");
3186
3187 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
3188
3189 // Retrieve the literal array and check the allocated entry.
3190
3191 // Load the function of this activation.
3192 __ ldr(r1, MemOperand(pp, 0));
3193
3194 // Load the literals array of the function.
3195 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3196
3197 // Load the literal at the ast saved index.
3198 int literal_offset =
3199 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3200 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3201
3202 // Check whether we need to materialize the object literal boilerplate.
3203 // If so, jump to the deferred code.
3204 __ cmp(r2, Operand(Factory::undefined_value()));
3205 __ b(eq, deferred->enter());
3206 __ bind(deferred->exit());
3207
3208 // Push the object literal boilerplate.
3209 __ Push(Operand(r2));
3210 // Clone the boilerplate object.
3211 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
3212
3213 for (int i = 0; i < node->properties()->length(); i++) {
3214 ObjectLiteral::Property* property = node->properties()->at(i);
3215 Literal* key = property->key();
3216 Expression* value = property->value();
3217 switch (property->kind()) {
3218 case ObjectLiteral::Property::CONSTANT: break;
3219 case ObjectLiteral::Property::COMPUTED: // fall through
3220 case ObjectLiteral::Property::PROTOTYPE: {
3221 // Save a copy of the resulting object on the stack.
3222 __ push(r0);
3223 Load(key);
3224 Load(value);
3225 __ CallRuntime(Runtime::kSetProperty, 3);
3226 // Restore the result object from the stack.
3227 __ pop(r0);
3228 break;
3229 }
3230 case ObjectLiteral::Property::SETTER: {
3231 __ push(r0);
3232 Load(key);
3233 __ Push(Operand(Smi::FromInt(1)));
3234 Load(value);
3235 __ CallRuntime(Runtime::kDefineAccessor, 4);
3236 __ pop(r0);
3237 break;
3238 }
3239 case ObjectLiteral::Property::GETTER: {
3240 __ push(r0);
3241 Load(key);
3242 __ Push(Operand(Smi::FromInt(0)));
3243 Load(value);
3244 __ CallRuntime(Runtime::kDefineAccessor, 4);
3245 __ pop(r0);
3246 break;
3247 }
3248 }
3249 }
3250}
3251
3252
3253void ArmCodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
3254 Comment cmnt(masm_, "[ ArrayLiteral");
3255 // Load the resulting object.
3256 Load(node->result());
3257 for (int i = 0; i < node->values()->length(); i++) {
3258 Expression* value = node->values()->at(i);
3259
3260 // If value is literal the property value is already
3261 // set in the boilerplate object.
3262 if (value->AsLiteral() == NULL) {
3263 // The property must be set by generated code.
3264 Load(value);
3265
3266 // Fetch the object literal
3267 __ ldr(r1, MemOperand(sp, 0));
3268 // Get the elements array.
3269 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3270
3271 // Write to the indexed properties array.
3272 int offset = i * kPointerSize + Array::kHeaderSize;
3273 __ str(r0, FieldMemOperand(r1, offset));
3274
3275 // Update the write barrier for the array address.
3276 __ mov(r3, Operand(offset));
3277 __ RecordWrite(r1, r3, r2);
3278
3279 __ pop(r0);
3280 }
3281 }
3282}
3283
3284
3285void ArmCodeGenerator::VisitAssignment(Assignment* node) {
3286 Comment cmnt(masm_, "[ Assignment");
3287
3288 if (FLAG_debug_info) RecordStatementPosition(node);
3289 Reference target(this, node->target());
3290 if (target.is_illegal()) return;
3291
3292 if (node->op() == Token::ASSIGN ||
3293 node->op() == Token::INIT_VAR ||
3294 node->op() == Token::INIT_CONST) {
3295 Load(node->value());
3296
3297 } else {
3298 GetValue(&target);
3299 Literal* literal = node->value()->AsLiteral();
3300 if (literal != NULL && literal->handle()->IsSmi()) {
3301 SmiOperation(node->binary_op(), literal->handle(), false);
3302 } else {
3303 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00003304 GenericBinaryOperation(node->binary_op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305 }
3306 }
3307
3308 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3309 if (var != NULL &&
3310 (var->mode() == Variable::CONST) &&
3311 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3312 // Assignment ignored - leave the value on the stack.
3313 } else {
3314 __ RecordPosition(node->position());
3315 if (node->op() == Token::INIT_CONST) {
3316 // Dynamic constant initializations must use the function context
3317 // and initialize the actual constant declared. Dynamic variable
3318 // initializations are simply assignments and use SetValue.
3319 InitConst(&target);
3320 } else {
3321 SetValue(&target);
3322 }
3323 }
3324}
3325
3326
3327void ArmCodeGenerator::VisitThrow(Throw* node) {
3328 Comment cmnt(masm_, "[ Throw");
3329
3330 Load(node->exception());
3331 __ RecordPosition(node->position());
3332 __ CallRuntime(Runtime::kThrow, 1);
3333}
3334
3335
3336void ArmCodeGenerator::VisitProperty(Property* node) {
3337 Comment cmnt(masm_, "[ Property");
3338 if (is_referenced()) {
3339 __ RecordPosition(node->position());
3340 AccessReferenceProperty(node->key(), access());
3341 } else {
3342 // All stores are through references.
3343 ASSERT(access() != CodeGenState::STORE);
3344 Reference property(this, node);
3345 __ RecordPosition(node->position());
3346 GetValue(&property);
3347 }
3348}
3349
3350
3351void ArmCodeGenerator::VisitCall(Call* node) {
3352 Comment cmnt(masm_, "[ Call");
3353
3354 ZoneList<Expression*>* args = node->arguments();
3355
3356 if (FLAG_debug_info) RecordStatementPosition(node);
3357 // Standard function call.
3358
3359 // Check if the function is a variable or a property.
3360 Expression* function = node->expression();
3361 Variable* var = function->AsVariableProxy()->AsVariable();
3362 Property* property = function->AsProperty();
3363
3364 // ------------------------------------------------------------------------
3365 // Fast-case: Use inline caching.
3366 // ---
3367 // According to ECMA-262, section 11.2.3, page 44, the function to call
3368 // must be resolved after the arguments have been evaluated. The IC code
3369 // automatically handles this by loading the arguments before the function
3370 // is resolved in cache misses (this also holds for megamorphic calls).
3371 // ------------------------------------------------------------------------
3372
3373 if (var != NULL && !var->is_this() && var->is_global()) {
3374 // ----------------------------------
3375 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3376 // ----------------------------------
3377
3378 // Push the name of the function and the receiver onto the stack.
3379 __ Push(Operand(var->name()));
3380 LoadGlobal();
3381
3382 // Load the arguments.
3383 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3384 __ Push(Operand(args->length()));
3385
3386 // Setup the receiver register and call the IC initialization code.
3387 Handle<Code> stub = ComputeCallInitialize(args->length());
3388 __ ldr(r1, GlobalObject());
3389 __ RecordPosition(node->position());
3390 __ Call(stub, code_target_context);
3391 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3392
3393 // Remove the function from the stack.
3394 __ add(sp, sp, Operand(kPointerSize));
3395
3396 } else if (var != NULL && var->slot() != NULL &&
3397 var->slot()->type() == Slot::LOOKUP) {
3398 // ----------------------------------
3399 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3400 // ----------------------------------
3401
3402 // Load the function
3403 __ Push(Operand(cp));
3404 __ Push(Operand(var->name()));
3405 __ CallRuntime(Runtime::kLoadContextSlot, 2);
3406 // r0: slot value; r1: receiver
3407
3408 // Load the receiver.
3409 __ push(r0);
3410 __ mov(r0, Operand(r1));
3411
3412 // Call the function.
3413 CallWithArguments(args, node->position());
3414
3415 } else if (property != NULL) {
3416 // Check if the key is a literal string.
3417 Literal* literal = property->key()->AsLiteral();
3418
3419 if (literal != NULL && literal->handle()->IsSymbol()) {
3420 // ------------------------------------------------------------------
3421 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3422 // ------------------------------------------------------------------
3423
3424 // Push the name of the function and the receiver onto the stack.
3425 __ Push(Operand(literal->handle()));
3426 Load(property->obj());
3427
3428 // Load the arguments.
3429 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3430 __ Push(Operand(args->length()));
3431
3432 // Set the receiver register and call the IC initialization code.
3433 Handle<Code> stub = ComputeCallInitialize(args->length());
3434 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize));
3435 __ RecordPosition(node->position());
3436 __ Call(stub, code_target);
3437 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3438
3439 // Remove the function from the stack.
3440 __ add(sp, sp, Operand(kPointerSize));
3441
3442 } else {
3443 // -------------------------------------------
3444 // JavaScript example: 'array[index](1, 2, 3)'
3445 // -------------------------------------------
3446
3447 // Load the function to call from the property through a reference.
3448 Reference ref(this, property);
3449 GetValue(&ref);
3450
3451 // Pass receiver to called function.
3452 __ Push(MemOperand(sp, ref.size() * kPointerSize));
3453
3454 // Call the function.
3455 CallWithArguments(args, node->position());
3456 }
3457
3458 } else {
3459 // ----------------------------------
3460 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3461 // ----------------------------------
3462
3463 // Load the function.
3464 Load(function);
3465
3466 // Pass the global object as the receiver.
3467 LoadGlobal();
3468
3469 // Call the function.
3470 CallWithArguments(args, node->position());
3471 }
3472}
3473
3474
3475void ArmCodeGenerator::VisitCallNew(CallNew* node) {
3476 Comment cmnt(masm_, "[ CallNew");
3477
3478 // According to ECMA-262, section 11.2.2, page 44, the function
3479 // expression in new calls must be evaluated before the
3480 // arguments. This is different from ordinary calls, where the
3481 // actual function to call is resolved after the arguments have been
3482 // evaluated.
3483
3484 // Compute function to call and use the global object as the
3485 // receiver.
3486 Load(node->expression());
3487 LoadGlobal();
3488
3489 // Push the arguments ("left-to-right") on the stack.
3490 ZoneList<Expression*>* args = node->arguments();
3491 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3492
3493 // Push the number of arguments.
3494 __ Push(Operand(args->length()));
3495
3496 // Call the construct call builtin that handles allocation and
3497 // constructor invocation.
3498 __ RecordPosition(position);
3499 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
3500 js_construct_call);
3501 __ add(sp, sp, Operand(kPointerSize)); // discard
3502}
3503
3504
3505void ArmCodeGenerator::GenerateSetThisFunction(ZoneList<Expression*>* args) {
3506 ASSERT(args->length() == 1);
3507 Load(args->at(0));
3508 __ str(r0, MemOperand(pp, JavaScriptFrameConstants::kFunctionOffset));
3509}
3510
3511
3512void ArmCodeGenerator::GenerateGetThisFunction(ZoneList<Expression*>* args) {
3513 ASSERT(args->length() == 0);
3514 __ Push(MemOperand(pp, JavaScriptFrameConstants::kFunctionOffset));
3515}
3516
3517
3518void ArmCodeGenerator::GenerateSetThis(ZoneList<Expression*>* args) {
3519 ASSERT(args->length() == 1);
3520 Load(args->at(0));
3521 __ str(r0, MemOperand(pp, JavaScriptFrameConstants::kReceiverOffset));
3522}
3523
3524
3525void ArmCodeGenerator::GenerateSetArgumentsLength(ZoneList<Expression*>* args) {
3526 ASSERT(args->length() == 1);
3527 Load(args->at(0));
3528 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
3529 __ str(r0, MemOperand(fp, JavaScriptFrameConstants::kArgsLengthOffset));
3530 __ mov(r0, Operand(Smi::FromInt(0)));
3531}
3532
3533
3534void ArmCodeGenerator::GenerateGetArgumentsLength(ZoneList<Expression*>* args) {
3535 ASSERT(args->length() == 1);
3536 __ push(r0);
3537 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kArgsLengthOffset));
3538 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3539}
3540
3541
3542void ArmCodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
3543 ASSERT(args->length() == 1);
3544 Label leave;
3545 Load(args->at(0));
3546 // r0 contains object.
3547 // if (object->IsSmi()) return TOS.
3548 __ tst(r0, Operand(kSmiTagMask));
3549 __ b(eq, &leave);
3550 // It is a heap object - get map.
3551 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3552 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3553 // if (!object->IsJSValue()) return TOS.
3554 __ cmp(r1, Operand(JS_VALUE_TYPE));
3555 __ b(ne, &leave);
3556 // Load the value.
3557 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
3558 __ bind(&leave);
3559}
3560
3561
3562void ArmCodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
3563 ASSERT(args->length() == 2);
3564 Label leave;
3565 Load(args->at(0)); // Load the object.
3566 Load(args->at(1)); // Load the value.
3567 __ pop(r1);
3568 // r0 contains value.
3569 // r1 contains object.
3570 // if (object->IsSmi()) return object.
3571 __ tst(r1, Operand(kSmiTagMask));
3572 __ b(eq, &leave);
3573 // It is a heap object - get map.
3574 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3575 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3576 // if (!object->IsJSValue()) return object.
3577 __ cmp(r2, Operand(JS_VALUE_TYPE));
3578 __ b(ne, &leave);
3579 // Store the value.
3580 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3581 // Update the write barrier.
3582 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3583 __ RecordWrite(r1, r2, r3);
3584 // Leave.
3585 __ bind(&leave);
3586}
3587
3588
3589void ArmCodeGenerator::GenerateTailCallWithArguments(
3590 ZoneList<Expression*>* args) {
3591 // r0 = number of arguments (smi)
3592 ASSERT(args->length() == 1);
3593 Load(args->at(0));
3594 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
3595
3596 // r1 = new function (previously written to stack)
3597 __ ldr(r1, MemOperand(pp, JavaScriptFrameConstants::kFunctionOffset));
3598
3599 // Reset parameter pointer and frame pointer to previous frame
kasper.lund7276f142008-07-30 08:49:36 +00003600 ExitJSFrame(DO_NOT_RETURN);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003601
3602 // Jump (tail-call) to the function in register r1.
3603 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
3604 __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
3605 __ ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
3606 __ add(pc, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607}
3608
3609
3610void ArmCodeGenerator::GenerateSetArgument(ZoneList<Expression*>* args) {
3611 ASSERT(args->length() == 3);
3612 // r1 = args[i]
3613 Comment cmnt(masm_, "[ GenerateSetArgument");
3614 Load(args->at(1));
3615 __ mov(r1, Operand(r0));
3616 // r0 = i
3617 Load(args->at(0));
3618#if defined(DEBUG)
3619 { Label L;
3620 __ tst(r0, Operand(kSmiTagMask));
3621 __ b(eq, &L);
3622 __ stop("SMI expected");
3623 __ bind(&L);
3624 }
3625#endif // defined(DEBUG)
3626 __ add(r2, pp, Operand(JavaScriptFrameConstants::kParam0Offset));
3627 __ str(r1,
3628 MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize, NegOffset));
3629 __ pop(r0);
3630}
3631
3632
3633void ArmCodeGenerator::GenerateSquashFrame(ZoneList<Expression*>* args) {
3634 ASSERT(args->length() == 2);
3635 // Load r1 with old number of arguments, r0 with new number, r1 > r0.
3636 Load(args->at(0));
3637 __ mov(r1, Operand(r0, LSR, kSmiTagSize));
3638 Load(args->at(1));
3639 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
3640 // r1 = number of words to move stack.
3641 __ sub(r1, r1, Operand(r0));
3642 // r2 is source.
3643 __ add(r2, fp, Operand(StandardFrameConstants::kCallerPCOffset));
3644 // Move down frame pointer fp.
3645 __ add(fp, fp, Operand(r1, LSL, kPointerSizeLog2));
3646 // r1 is destination.
3647 __ add(r1, fp, Operand(StandardFrameConstants::kCallerPCOffset));
3648
3649 Label move;
3650 __ bind(&move);
3651 __ ldr(r3, MemOperand(r2, -kPointerSize, PostIndex));
3652 __ str(r3, MemOperand(r1, -kPointerSize, PostIndex));
3653 __ cmp(r2, Operand(sp));
3654 __ b(ne, &move);
3655 __ ldr(r3, MemOperand(r2));
3656 __ str(r3, MemOperand(r1));
3657
3658 // Move down stack pointer esp.
3659 __ mov(sp, Operand(r1));
3660 // Balance stack and put something GC-able in r0.
3661 __ pop(r0);
3662}
3663
3664
3665void ArmCodeGenerator::GenerateExpandFrame(ZoneList<Expression*>* args) {
3666 ASSERT(args->length() == 2);
3667 // Load r1 with new number of arguments, r0 with old number (as Smi), r1 > r0.
3668 Load(args->at(1));
3669 __ mov(r1, Operand(r0, LSR, kSmiTagSize));
3670 Load(args->at(0));
3671 // r1 = number of words to move stack.
3672 __ sub(r1, r1, Operand(r0, LSR, kSmiTagSize));
3673 Label end_of_expand_frame;
3674 if (FLAG_check_stack) {
3675 Label not_too_big;
3676 __ sub(r2, sp, Operand(r1, LSL, kPointerSizeLog2));
3677 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3678 __ ldr(ip, MemOperand(ip));
3679 __ cmp(r2, Operand(ip));
3680 __ b(gt, &not_too_big);
3681 __ pop(r0);
3682 __ mov(r0, Operand(Factory::false_value()));
3683 __ b(&end_of_expand_frame);
3684 __ bind(&not_too_big);
3685 }
3686 // r3 is source.
3687 __ mov(r3, Operand(sp));
3688 // r0 is copy limit + 1 word
3689 __ add(r0, fp,
3690 Operand(StandardFrameConstants::kCallerPCOffset + kPointerSize));
3691 // Move up frame pointer fp.
3692 __ sub(fp, fp, Operand(r1, LSL, kPointerSizeLog2));
3693 // Move up stack pointer sp.
3694 __ sub(sp, sp, Operand(r1, LSL, kPointerSizeLog2));
3695 // r1 is destination (r1 = source - r1).
3696 __ mov(r2, Operand(0));
3697 __ sub(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3698 __ add(r1, r3, Operand(r2));
3699
3700 Label move;
3701 __ bind(&move);
3702 __ ldr(r2, MemOperand(r3, kPointerSize, PostIndex));
3703 __ str(r2, MemOperand(r1, kPointerSize, PostIndex));
3704 __ cmp(r3, Operand(r0));
3705 __ b(ne, &move);
3706
3707 // Balance stack and put success value in top of stack
3708 __ pop(r0);
3709 __ mov(r0, Operand(Factory::true_value()));
3710 __ bind(&end_of_expand_frame);
3711}
3712
3713
3714void ArmCodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
3715 ASSERT(args->length() == 1);
3716 Load(args->at(0));
3717 __ tst(r0, Operand(kSmiTagMask));
3718 __ pop(r0);
3719 cc_reg_ = eq;
3720}
3721
3722
kasper.lund7276f142008-07-30 08:49:36 +00003723// This should generate code that performs a charCodeAt() call or returns
3724// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3725// It is not yet implemented on ARM, so it always goes to the slow case.
3726void ArmCodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
3727 ASSERT(args->length() == 2);
3728 __ push(r0);
3729 __ mov(r0, Operand(Factory::undefined_value()));
3730}
3731
3732
3733
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003734// This is used in the implementation of apply on ia32 but it is not
3735// used on ARM yet.
3736void ArmCodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00003737 __ stop("ArmCodeGenerator::GenerateIsArray");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003738 cc_reg_ = eq;
3739}
3740
3741
3742void ArmCodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
3743 ASSERT(args->length() == 0);
3744
3745 // Flush the TOS cache and seed the result with the formal
3746 // parameters count, which will be used in case no arguments adaptor
3747 // frame is found below the current frame.
3748 __ push(r0);
3749 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3750
3751 // Call the shared stub to get to the arguments.length.
3752 ArgumentsAccessStub stub(true);
3753 __ CallStub(&stub);
3754}
3755
3756
3757void ArmCodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
3758 ASSERT(args->length() == 1);
3759
3760 // Load the key onto the stack and set register r1 to the formal
3761 // parameters count for the currently executing function.
3762 Load(args->at(0));
3763 __ mov(r1, Operand(Smi::FromInt(scope_->num_parameters())));
3764
3765 // Call the shared stub to get to arguments[key].
3766 ArgumentsAccessStub stub(false);
3767 __ CallStub(&stub);
3768}
3769
3770
3771void ArmCodeGenerator::GenerateShiftDownAndTailCall(
3772 ZoneList<Expression*>* args) {
3773 // r0 = number of arguments
3774 ASSERT(args->length() == 1);
3775 Load(args->at(0));
3776 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
3777
3778 // Get the 'this' function and exit the frame without returning.
3779 __ ldr(r1, MemOperand(pp, JavaScriptFrameConstants::kFunctionOffset));
kasper.lund7276f142008-07-30 08:49:36 +00003780 ExitJSFrame(DO_NOT_RETURN);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781 // return address in lr
3782
3783 // Move arguments one element down the stack.
3784 Label move;
3785 Label moved;
3786 __ sub(r2, r0, Operand(0), SetCC);
3787 __ b(eq, &moved);
3788 __ bind(&move);
3789 __ sub(ip, r2, Operand(1));
3790 __ ldr(r3, MemOperand(sp, ip, LSL, kPointerSizeLog2));
3791 __ str(r3, MemOperand(sp, r2, LSL, kPointerSizeLog2));
3792 __ sub(r2, r2, Operand(1), SetCC);
3793 __ b(ne, &move);
3794 __ bind(&moved);
3795
3796 // Remove the TOS (copy of last argument)
3797 __ add(sp, sp, Operand(kPointerSize));
3798
3799 // Jump (tail-call) to the function in register r1.
3800 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
3801 __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
3802 __ ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCodeOffset));
3803 __ add(pc, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
3804 return;
3805}
3806
3807
3808void ArmCodeGenerator::VisitCallRuntime(CallRuntime* node) {
3809 if (CheckForInlineRuntimeCall(node))
3810 return;
3811
3812 ZoneList<Expression*>* args = node->arguments();
3813 Comment cmnt(masm_, "[ CallRuntime");
3814 Runtime::Function* function = node->function();
3815
3816 if (function == NULL) {
3817 // Prepare stack for calling JS runtime function.
3818 __ Push(Operand(node->name()));
3819 // Push the builtins object found in the current global object.
3820 __ ldr(r1, GlobalObject());
3821 __ Push(FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
3822 }
3823
3824 // Push the arguments ("left-to-right").
3825 for (int i = 0; i < args->length(); i++) Load(args->at(i));
3826
3827 if (function != NULL) {
3828 // Call the C runtime function.
3829 __ CallRuntime(function, args->length());
3830 } else {
3831 // Call the JS runtime function.
3832 __ Push(Operand(args->length()));
3833 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize));
3834 Handle<Code> stub = ComputeCallInitialize(args->length());
3835 __ Call(stub, code_target);
3836 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3837 __ add(sp, sp, Operand(kPointerSize));
3838 }
3839}
3840
3841
3842void ArmCodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
3843 Comment cmnt(masm_, "[ UnaryOperation");
3844
3845 Token::Value op = node->op();
3846
3847 if (op == Token::NOT) {
3848 LoadCondition(node->expression(),
3849 CodeGenState::LOAD,
3850 false_target(),
3851 true_target(),
3852 true);
3853 cc_reg_ = NegateCondition(cc_reg_);
3854
3855 } else if (op == Token::DELETE) {
3856 Property* property = node->expression()->AsProperty();
3857 if (property != NULL) {
3858 Load(property->obj());
3859 Load(property->key());
3860 __ Push(Operand(1)); // not counting receiver
3861 __ InvokeBuiltin("DELETE", 1, CALL_JS);
3862 return;
3863 }
3864
3865 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
3866 if (variable != NULL) {
3867 Slot* slot = variable->slot();
3868 if (variable->is_global()) {
3869 LoadGlobal();
3870 __ Push(Operand(variable->name()));
3871 __ Push(Operand(1)); // not counting receiver
3872 __ InvokeBuiltin("DELETE", 1, CALL_JS);
3873 return;
3874
3875 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3876 // lookup the context holding the named variable
3877 __ Push(Operand(cp));
3878 __ Push(Operand(variable->name()));
3879 __ CallRuntime(Runtime::kLookupContext, 2);
3880 // r0: context
3881 __ Push(Operand(variable->name()));
3882 __ Push(Operand(1)); // not counting receiver
3883 __ InvokeBuiltin("DELETE", 1, CALL_JS);
3884 return;
3885 }
3886
3887 // Default: Result of deleting non-global, not dynamically
3888 // introduced variables is false.
3889 __ Push(Operand(Factory::false_value()));
3890
3891 } else {
3892 // Default: Result of deleting expressions is true.
3893 Load(node->expression()); // may have side-effects
3894 __ mov(r0, Operand(Factory::true_value()));
3895 }
3896
3897 } else if (op == Token::TYPEOF) {
3898 // Special case for loading the typeof expression; see comment on
3899 // LoadTypeofExpression().
3900 LoadTypeofExpression(node->expression());
3901 __ CallRuntime(Runtime::kTypeof, 1);
3902
3903 } else {
3904 Load(node->expression());
3905 switch (op) {
3906 case Token::NOT:
3907 case Token::DELETE:
3908 case Token::TYPEOF:
3909 UNREACHABLE(); // handled above
3910 break;
3911
3912 case Token::SUB: {
3913 UnarySubStub stub;
3914 __ CallStub(&stub);
3915 break;
3916 }
3917
3918 case Token::BIT_NOT: {
3919 // smi check
3920 Label smi_label;
3921 Label continue_label;
3922 __ tst(r0, Operand(kSmiTagMask));
3923 __ b(eq, &smi_label);
3924
3925 __ Push(Operand(0)); // not counting receiver
3926 __ InvokeBuiltin("BIT_NOT", 0, CALL_JS);
3927
3928 __ b(&continue_label);
3929 __ bind(&smi_label);
3930 __ mvn(r0, Operand(r0));
3931 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
3932 __ bind(&continue_label);
3933 break;
3934 }
3935
3936 case Token::VOID:
3937 // since the stack top is cached in r0, popping and then
3938 // pushing a value can be done by just writing to r0.
3939 __ mov(r0, Operand(Factory::undefined_value()));
3940 break;
3941
3942 case Token::ADD:
3943 __ Push(Operand(0)); // not counting receiver
3944 __ InvokeBuiltin("TO_NUMBER", 0, CALL_JS);
3945 break;
3946
3947 default:
3948 UNREACHABLE();
3949 }
3950 }
3951}
3952
3953
3954void ArmCodeGenerator::VisitCountOperation(CountOperation* node) {
3955 Comment cmnt(masm_, "[ CountOperation");
3956
3957 bool is_postfix = node->is_postfix();
3958 bool is_increment = node->op() == Token::INC;
3959
3960 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3961 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3962
3963 // Postfix: Make room for the result.
3964 if (is_postfix) __ Push(Operand(0));
3965
3966 { Reference target(this, node->expression());
3967 if (target.is_illegal()) return;
3968 GetValue(&target);
3969
3970 Label slow, exit;
3971
3972 // Load the value (1) into register r1.
3973 __ mov(r1, Operand(Smi::FromInt(1)));
3974
3975 // Check for smi operand.
3976 __ tst(r0, Operand(kSmiTagMask));
3977 __ b(ne, &slow);
3978
3979 // Postfix: Store the old value as the result.
3980 if (is_postfix) __ str(r0, MemOperand(sp, target.size() * kPointerSize));
3981
3982 // Perform optimistic increment/decrement.
3983 if (is_increment) {
3984 __ add(r0, r0, Operand(r1), SetCC);
3985 } else {
3986 __ sub(r0, r0, Operand(r1), SetCC);
3987 }
3988
3989 // If the increment/decrement didn't overflow, we're done.
3990 __ b(vc, &exit);
3991
3992 // Revert optimistic increment/decrement.
3993 if (is_increment) {
3994 __ sub(r0, r0, Operand(r1));
3995 } else {
3996 __ add(r0, r0, Operand(r1));
3997 }
3998
3999 // Slow case: Convert to number.
4000 __ bind(&slow);
4001
4002 // Postfix: Convert the operand to a number and store it as the result.
4003 if (is_postfix) {
4004 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
4005 __ CallStub(&stub);
4006 // Store to result (on the stack).
4007 __ str(r0, MemOperand(sp, target.size() * kPointerSize));
4008 }
4009
4010 // Compute the new value by calling the right JavaScript native.
4011 if (is_increment) {
4012 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
4013 __ CallStub(&stub);
4014 } else {
4015 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
4016 __ CallStub(&stub);
4017 }
4018
4019 // Store the new value in the target if not const.
4020 __ bind(&exit);
4021 if (!is_const) SetValue(&target);
4022 }
4023
4024 // Postfix: Discard the new value and use the old.
4025 if (is_postfix) __ pop(r0);
4026}
4027
4028
4029void ArmCodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4030 Comment cmnt(masm_, "[ BinaryOperation");
4031 Token::Value op = node->op();
4032
4033 // According to ECMA-262 section 11.11, page 58, the binary logical
4034 // operators must yield the result of one of the two expressions
4035 // before any ToBoolean() conversions. This means that the value
4036 // produced by a && or || operator is not necessarily a boolean.
4037
4038 // NOTE: If the left hand side produces a materialized value (not in
4039 // the CC register), we force the right hand side to do the
4040 // same. This is necessary because we may have to branch to the exit
4041 // after evaluating the left hand side (due to the shortcut
4042 // semantics), but the compiler must (statically) know if the result
4043 // of compiling the binary operation is materialized or not.
4044
4045 if (op == Token::AND) {
4046 Label is_true;
4047 LoadCondition(node->left(),
4048 CodeGenState::LOAD,
4049 &is_true,
4050 false_target(),
4051 false);
4052 if (has_cc()) {
4053 Branch(false, false_target());
4054
4055 // Evaluate right side expression.
4056 __ bind(&is_true);
4057 LoadCondition(node->right(),
4058 CodeGenState::LOAD,
4059 true_target(),
4060 false_target(),
4061 false);
4062
4063 } else {
4064 Label pop_and_continue, exit;
4065
4066 // Avoid popping the result if it converts to 'false' using the
4067 // standard ToBoolean() conversion as described in ECMA-262,
4068 // section 9.2, page 30.
4069 ToBoolean(r0, &pop_and_continue, &exit);
4070 Branch(false, &exit);
4071
4072 // Pop the result of evaluating the first part.
4073 __ bind(&pop_and_continue);
4074 __ pop(r0);
4075
4076 // Evaluate right side expression.
4077 __ bind(&is_true);
4078 Load(node->right());
4079
4080 // Exit (always with a materialized value).
4081 __ bind(&exit);
4082 }
4083
4084 } else if (op == Token::OR) {
4085 Label is_false;
4086 LoadCondition(node->left(),
4087 CodeGenState::LOAD,
4088 true_target(),
4089 &is_false,
4090 false);
4091 if (has_cc()) {
4092 Branch(true, true_target());
4093
4094 // Evaluate right side expression.
4095 __ bind(&is_false);
4096 LoadCondition(node->right(),
4097 CodeGenState::LOAD,
4098 true_target(),
4099 false_target(),
4100 false);
4101
4102 } else {
4103 Label pop_and_continue, exit;
4104
4105 // Avoid popping the result if it converts to 'true' using the
4106 // standard ToBoolean() conversion as described in ECMA-262,
4107 // section 9.2, page 30.
4108 ToBoolean(r0, &exit, &pop_and_continue);
4109 Branch(true, &exit);
4110
4111 // Pop the result of evaluating the first part.
4112 __ bind(&pop_and_continue);
4113 __ pop(r0);
4114
4115 // Evaluate right side expression.
4116 __ bind(&is_false);
4117 Load(node->right());
4118
4119 // Exit (always with a materialized value).
4120 __ bind(&exit);
4121 }
4122
4123 } else {
4124 // Optimize for the case where (at least) one of the expressions
4125 // is a literal small integer.
4126 Literal* lliteral = node->left()->AsLiteral();
4127 Literal* rliteral = node->right()->AsLiteral();
4128
4129 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
4130 Load(node->left());
4131 SmiOperation(node->op(), rliteral->handle(), false);
4132
4133 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
4134 Load(node->right());
4135 SmiOperation(node->op(), lliteral->handle(), true);
4136
4137 } else {
4138 Load(node->left());
4139 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00004140 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004141 }
4142 }
4143}
4144
4145
4146void ArmCodeGenerator::VisitThisFunction(ThisFunction* node) {
4147 __ Push(FunctionOperand());
4148}
4149
4150
4151void ArmCodeGenerator::VisitCompareOperation(CompareOperation* node) {
4152 Comment cmnt(masm_, "[ CompareOperation");
4153
4154 // Get the expressions from the node.
4155 Expression* left = node->left();
4156 Expression* right = node->right();
4157 Token::Value op = node->op();
4158
4159 // NOTE: To make null checks efficient, we check if either left or
4160 // right is the literal 'null'. If so, we optimize the code by
4161 // inlining a null check instead of calling the (very) general
4162 // runtime routine for checking equality.
4163
4164 bool left_is_null =
4165 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
4166 bool right_is_null =
4167 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4168
4169 if (op == Token::EQ || op == Token::EQ_STRICT) {
4170 // The 'null' value is only equal to 'null' or 'undefined'.
4171 if (left_is_null || right_is_null) {
4172 Load(left_is_null ? right : left);
4173 Label exit, undetectable;
4174 __ cmp(r0, Operand(Factory::null_value()));
4175
4176 // The 'null' value is only equal to 'undefined' if using
4177 // non-strict comparisons.
4178 if (op != Token::EQ_STRICT) {
4179 __ b(eq, &exit);
4180 __ cmp(r0, Operand(Factory::undefined_value()));
4181
4182 // NOTE: it can be undetectable object.
4183 __ b(eq, &exit);
4184 __ tst(r0, Operand(kSmiTagMask));
4185
4186 __ b(ne, &undetectable);
4187 __ pop(r0);
4188 __ b(false_target());
4189
4190 __ bind(&undetectable);
4191 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4192 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4193 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4194 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4195 }
4196
4197 __ bind(&exit);
4198 __ pop(r0);
4199
4200 cc_reg_ = eq;
4201 return;
4202 }
4203 }
4204
4205
4206 // NOTE: To make typeof testing for natives implemented in
4207 // JavaScript really efficient, we generate special code for
4208 // expressions of the form: 'typeof <expression> == <string>'.
4209
4210 UnaryOperation* operation = left->AsUnaryOperation();
4211 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4212 (operation != NULL && operation->op() == Token::TYPEOF) &&
4213 (right->AsLiteral() != NULL &&
4214 right->AsLiteral()->handle()->IsString())) {
4215 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4216
4217 // Load the operand, move it to register r1, and restore TOS.
4218 LoadTypeofExpression(operation->expression());
4219 __ mov(r1, Operand(r0));
4220 __ pop(r0);
4221
4222 if (check->Equals(Heap::number_symbol())) {
4223 __ tst(r1, Operand(kSmiTagMask));
4224 __ b(eq, true_target());
4225 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4226 __ cmp(r1, Operand(Factory::heap_number_map()));
4227 cc_reg_ = eq;
4228
4229 } else if (check->Equals(Heap::string_symbol())) {
4230 __ tst(r1, Operand(kSmiTagMask));
4231 __ b(eq, false_target());
4232
4233 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4234
4235 // NOTE: it might be an undetectable string object
4236 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4237 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4238 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4239 __ b(eq, false_target());
4240
4241 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4242 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4243 cc_reg_ = lt;
4244
4245 } else if (check->Equals(Heap::boolean_symbol())) {
4246 __ cmp(r1, Operand(Factory::true_value()));
4247 __ b(eq, true_target());
4248 __ cmp(r1, Operand(Factory::false_value()));
4249 cc_reg_ = eq;
4250
4251 } else if (check->Equals(Heap::undefined_symbol())) {
4252 __ cmp(r1, Operand(Factory::undefined_value()));
4253 __ b(eq, true_target());
4254
4255 __ tst(r1, Operand(kSmiTagMask));
4256 __ b(eq, false_target());
4257
4258 // NOTE: it can be undetectable object.
4259 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4260 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4261 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4262 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4263
4264 cc_reg_ = eq;
4265
4266 } else if (check->Equals(Heap::function_symbol())) {
4267 __ tst(r1, Operand(kSmiTagMask));
4268 __ b(eq, false_target());
4269 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4270 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4271 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4272 cc_reg_ = eq;
4273
4274 } else if (check->Equals(Heap::object_symbol())) {
4275 __ tst(r1, Operand(kSmiTagMask));
4276 __ b(eq, false_target());
4277
4278 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4279 __ cmp(r1, Operand(Factory::null_value()));
4280 __ b(eq, true_target());
4281
4282 // NOTE: it might be an undetectable object.
4283 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4284 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4285 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
4286 __ b(eq, false_target());
4287
4288 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4289 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
4290 __ b(lt, false_target());
4291 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4292 cc_reg_ = le;
4293
4294 } else {
4295 // Uncommon case: Typeof testing against a string literal that
4296 // is never returned from the typeof operator.
4297 __ b(false_target());
4298 }
4299 return;
4300 }
4301
4302 Load(left);
4303 Load(right);
4304 switch (op) {
4305 case Token::EQ:
4306 Comparison(eq, false);
4307 break;
4308
4309 case Token::LT:
4310 Comparison(lt);
4311 break;
4312
4313 case Token::GT:
4314 Comparison(gt);
4315 break;
4316
4317 case Token::LTE:
4318 Comparison(le);
4319 break;
4320
4321 case Token::GTE:
4322 Comparison(ge);
4323 break;
4324
4325 case Token::EQ_STRICT:
4326 Comparison(eq, true);
4327 break;
4328
4329 case Token::IN:
4330 __ Push(Operand(1)); // not counting receiver
4331 __ InvokeBuiltin("IN", 1, CALL_JS);
4332 break;
4333
4334 case Token::INSTANCEOF:
4335 __ Push(Operand(1)); // not counting receiver
4336 __ InvokeBuiltin("INSTANCE_OF", 1, CALL_JS);
4337 break;
4338
4339 default:
4340 UNREACHABLE();
4341 }
4342}
4343
4344
4345void ArmCodeGenerator::RecordStatementPosition(Node* node) {
4346 if (FLAG_debug_info) {
4347 int statement_pos = node->statement_pos();
4348 if (statement_pos == kNoPosition) return;
4349 __ RecordStatementPosition(statement_pos);
4350 }
4351}
4352
4353
kasper.lund7276f142008-07-30 08:49:36 +00004354void ArmCodeGenerator::EnterJSFrame(int argc) {
4355 __ EnterJSFrame(argc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004356}
4357
4358
kasper.lund7276f142008-07-30 08:49:36 +00004359void ArmCodeGenerator::ExitJSFrame(ExitJSFlag flag) {
4360 JSExitStub stub(flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004361 __ CallJSExitStub(&stub);
4362}
4363
4364
4365#undef __
4366
4367
4368// -----------------------------------------------------------------------------
4369// CodeGenerator interface
4370
4371// MakeCode() is just a wrapper for CodeGenerator::MakeCode()
4372// so we don't have to expose the entire CodeGenerator class in
4373// the .h file.
4374Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* fun,
4375 Handle<Script> script,
4376 bool is_eval) {
4377 Handle<Code> code = ArmCodeGenerator::MakeCode(fun, script, is_eval);
4378 if (!code.is_null()) {
4379 Counters::total_compiled_code_size.Increment(code->instruction_size());
4380 }
4381 return code;
4382}
4383
4384
4385} } // namespace v8::internal