deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / full-codegen.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_FULL_CODEGEN_H_
6 #define V8_FULL_CODEGEN_H_
7
8 #include "src/v8.h"
9
10 #include "src/allocation.h"
11 #include "src/assert-scope.h"
12 #include "src/ast.h"
13 #include "src/bit-vector.h"
14 #include "src/code-stubs.h"
15 #include "src/codegen.h"
16 #include "src/compiler.h"
17 #include "src/globals.h"
18 #include "src/objects.h"
19
20 namespace v8 {
21 namespace internal {
22
23 // Forward declarations.
24 class JumpPatchSite;
25
26 // AST node visitor which can tell whether a given statement will be breakable
27 // when the code is compiled by the full compiler in the debugger. This means
28 // that there will be an IC (load/store/call) in the code generated for the
29 // debugger to piggybag on.
30 class BreakableStatementChecker: public AstVisitor {
31  public:
32   BreakableStatementChecker(Isolate* isolate, Zone* zone)
33       : is_breakable_(false) {
34     InitializeAstVisitor(isolate, zone);
35   }
36
37   void Check(Statement* stmt);
38   void Check(Expression* stmt);
39
40   bool is_breakable() { return is_breakable_; }
41
42  private:
43   // AST node visit functions.
44 #define DECLARE_VISIT(type) virtual void Visit##type(type* node) OVERRIDE;
45   AST_NODE_LIST(DECLARE_VISIT)
46 #undef DECLARE_VISIT
47
48   bool is_breakable_;
49
50   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
51   DISALLOW_COPY_AND_ASSIGN(BreakableStatementChecker);
52 };
53
54
55 // -----------------------------------------------------------------------------
56 // Full code generator.
57
58 class FullCodeGenerator: public AstVisitor {
59  public:
60   enum State {
61     NO_REGISTERS,
62     TOS_REG
63   };
64
65   FullCodeGenerator(MacroAssembler* masm, CompilationInfo* info)
66       : masm_(masm),
67         info_(info),
68         scope_(info->scope()),
69         nesting_stack_(NULL),
70         loop_depth_(0),
71         globals_(NULL),
72         context_(NULL),
73         bailout_entries_(info->HasDeoptimizationSupport()
74                          ? info->function()->ast_node_count() : 0,
75                          info->zone()),
76         back_edges_(2, info->zone()),
77         ic_total_count_(0) {
78     DCHECK(!info->IsStub());
79     Initialize();
80   }
81
82   void Initialize();
83
84   static bool MakeCode(CompilationInfo* info);
85
86   // Encode state and pc-offset as a BitField<type, start, size>.
87   // Only use 30 bits because we encode the result as a smi.
88   class StateField : public BitField<State, 0, 1> { };
89   class PcField    : public BitField<unsigned, 1, 30-1> { };
90
91   static const char* State2String(State state) {
92     switch (state) {
93       case NO_REGISTERS: return "NO_REGISTERS";
94       case TOS_REG: return "TOS_REG";
95     }
96     UNREACHABLE();
97     return NULL;
98   }
99
100   static const int kMaxBackEdgeWeight = 127;
101
102   // Platform-specific code size multiplier.
103 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
104   static const int kCodeSizeMultiplier = 105;
105 #elif V8_TARGET_ARCH_X64
106   static const int kCodeSizeMultiplier = 170;
107 #elif V8_TARGET_ARCH_ARM
108   static const int kCodeSizeMultiplier = 149;
109 #elif V8_TARGET_ARCH_ARM64
110 // TODO(all): Copied ARM value. Check this is sensible for ARM64.
111   static const int kCodeSizeMultiplier = 149;
112 #elif V8_TARGET_ARCH_PPC64
113   static const int kCodeSizeMultiplier = 200;
114 #elif V8_TARGET_ARCH_PPC
115   static const int kCodeSizeMultiplier = 200;
116 #elif V8_TARGET_ARCH_MIPS
117   static const int kCodeSizeMultiplier = 149;
118 #elif V8_TARGET_ARCH_MIPS64
119   static const int kCodeSizeMultiplier = 149;
120 #else
121 #error Unsupported target architecture.
122 #endif
123
124  private:
125   class Breakable;
126   class Iteration;
127
128   class TestContext;
129
130   class NestedStatement BASE_EMBEDDED {
131    public:
132     explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
133       // Link into codegen's nesting stack.
134       previous_ = codegen->nesting_stack_;
135       codegen->nesting_stack_ = this;
136     }
137     virtual ~NestedStatement() {
138       // Unlink from codegen's nesting stack.
139       DCHECK_EQ(this, codegen_->nesting_stack_);
140       codegen_->nesting_stack_ = previous_;
141     }
142
143     virtual Breakable* AsBreakable() { return NULL; }
144     virtual Iteration* AsIteration() { return NULL; }
145
146     virtual bool IsContinueTarget(Statement* target) { return false; }
147     virtual bool IsBreakTarget(Statement* target) { return false; }
148
149     // Notify the statement that we are exiting it via break, continue, or
150     // return and give it a chance to generate cleanup code.  Return the
151     // next outer statement in the nesting stack.  We accumulate in
152     // *stack_depth the amount to drop the stack and in *context_length the
153     // number of context chain links to unwind as we traverse the nesting
154     // stack from an exit to its target.
155     virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
156       return previous_;
157     }
158
159     // Like the Exit() method above, but limited to accumulating stack depth.
160     virtual NestedStatement* AccumulateDepth(int* stack_depth) {
161       return previous_;
162     }
163
164    protected:
165     MacroAssembler* masm() { return codegen_->masm(); }
166
167     FullCodeGenerator* codegen_;
168     NestedStatement* previous_;
169
170    private:
171     DISALLOW_COPY_AND_ASSIGN(NestedStatement);
172   };
173
174   // A breakable statement such as a block.
175   class Breakable : public NestedStatement {
176    public:
177     Breakable(FullCodeGenerator* codegen, BreakableStatement* statement)
178         : NestedStatement(codegen), statement_(statement) {
179     }
180     virtual ~Breakable() {}
181
182     virtual Breakable* AsBreakable() { return this; }
183     virtual bool IsBreakTarget(Statement* target) {
184       return statement() == target;
185     }
186
187     BreakableStatement* statement() { return statement_; }
188     Label* break_label() { return &break_label_; }
189
190    private:
191     BreakableStatement* statement_;
192     Label break_label_;
193   };
194
195   // An iteration statement such as a while, for, or do loop.
196   class Iteration : public Breakable {
197    public:
198     Iteration(FullCodeGenerator* codegen, IterationStatement* statement)
199         : Breakable(codegen, statement) {
200     }
201     virtual ~Iteration() {}
202
203     virtual Iteration* AsIteration() { return this; }
204     virtual bool IsContinueTarget(Statement* target) {
205       return statement() == target;
206     }
207
208     Label* continue_label() { return &continue_label_; }
209
210    private:
211     Label continue_label_;
212   };
213
214   // A nested block statement.
215   class NestedBlock : public Breakable {
216    public:
217     NestedBlock(FullCodeGenerator* codegen, Block* block)
218         : Breakable(codegen, block) {
219     }
220     virtual ~NestedBlock() {}
221
222     virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
223       if (statement()->AsBlock()->scope() != NULL) {
224         ++(*context_length);
225       }
226       return previous_;
227     }
228   };
229
230   // The try block of a try/catch statement.
231   class TryCatch : public NestedStatement {
232    public:
233     static const int kElementCount = TryBlockConstant::kElementCount;
234
235     explicit TryCatch(FullCodeGenerator* codegen) : NestedStatement(codegen) {}
236     virtual ~TryCatch() {}
237
238     virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
239       *stack_depth += kElementCount;
240       return previous_;
241     }
242     virtual NestedStatement* AccumulateDepth(int* stack_depth) {
243       *stack_depth += kElementCount;
244       return previous_;
245     }
246   };
247
248   // The try block of a try/finally statement.
249   class TryFinally : public NestedStatement {
250    public:
251     static const int kElementCount = TryBlockConstant::kElementCount;
252
253     TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
254         : NestedStatement(codegen), finally_entry_(finally_entry) {
255     }
256     virtual ~TryFinally() {}
257
258     virtual NestedStatement* Exit(int* stack_depth, int* context_length);
259     virtual NestedStatement* AccumulateDepth(int* stack_depth) {
260       *stack_depth += kElementCount;
261       return previous_;
262     }
263
264    private:
265     Label* finally_entry_;
266   };
267
268   // The finally block of a try/finally statement.
269   class Finally : public NestedStatement {
270    public:
271     static const int kElementCount = 3;
272
273     explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) {}
274     virtual ~Finally() {}
275
276     virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
277       *stack_depth += kElementCount;
278       return previous_;
279     }
280     virtual NestedStatement* AccumulateDepth(int* stack_depth) {
281       *stack_depth += kElementCount;
282       return previous_;
283     }
284   };
285
286   // The body of a for/in loop.
287   class ForIn : public Iteration {
288    public:
289     static const int kElementCount = 5;
290
291     ForIn(FullCodeGenerator* codegen, ForInStatement* statement)
292         : Iteration(codegen, statement) {
293     }
294     virtual ~ForIn() {}
295
296     virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
297       *stack_depth += kElementCount;
298       return previous_;
299     }
300     virtual NestedStatement* AccumulateDepth(int* stack_depth) {
301       *stack_depth += kElementCount;
302       return previous_;
303     }
304   };
305
306
307   // The body of a with or catch.
308   class WithOrCatch : public NestedStatement {
309    public:
310     explicit WithOrCatch(FullCodeGenerator* codegen)
311         : NestedStatement(codegen) {
312     }
313     virtual ~WithOrCatch() {}
314
315     virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
316       ++(*context_length);
317       return previous_;
318     }
319   };
320
321   // A platform-specific utility to overwrite the accumulator register
322   // with a GC-safe value.
323   void ClearAccumulator();
324
325   // Determine whether or not to inline the smi case for the given
326   // operation.
327   bool ShouldInlineSmiCase(Token::Value op);
328
329   // Helper function to convert a pure value into a test context.  The value
330   // is expected on the stack or the accumulator, depending on the platform.
331   // See the platform-specific implementation for details.
332   void DoTest(Expression* condition,
333               Label* if_true,
334               Label* if_false,
335               Label* fall_through);
336   void DoTest(const TestContext* context);
337
338   // Helper function to split control flow and avoid a branch to the
339   // fall-through label if it is set up.
340 #if V8_TARGET_ARCH_MIPS
341   void Split(Condition cc,
342              Register lhs,
343              const Operand&  rhs,
344              Label* if_true,
345              Label* if_false,
346              Label* fall_through);
347 #elif V8_TARGET_ARCH_MIPS64
348   void Split(Condition cc,
349              Register lhs,
350              const Operand&  rhs,
351              Label* if_true,
352              Label* if_false,
353              Label* fall_through);
354 #elif V8_TARGET_ARCH_PPC
355   void Split(Condition cc, Label* if_true, Label* if_false, Label* fall_through,
356              CRegister cr = cr7);
357 #else  // All other arch.
358   void Split(Condition cc,
359              Label* if_true,
360              Label* if_false,
361              Label* fall_through);
362 #endif
363
364   // Load the value of a known (PARAMETER, LOCAL, or CONTEXT) variable into
365   // a register.  Emits a context chain walk if if necessary (so does
366   // SetVar) so avoid calling both on the same variable.
367   void GetVar(Register destination, Variable* var);
368
369   // Assign to a known (PARAMETER, LOCAL, or CONTEXT) variable.  If it's in
370   // the context, the write barrier will be emitted and source, scratch0,
371   // scratch1 will be clobbered.  Emits a context chain walk if if necessary
372   // (so does GetVar) so avoid calling both on the same variable.
373   void SetVar(Variable* var,
374               Register source,
375               Register scratch0,
376               Register scratch1);
377
378   // An operand used to read/write a stack-allocated (PARAMETER or LOCAL)
379   // variable.  Writing does not need the write barrier.
380   MemOperand StackOperand(Variable* var);
381
382   // An operand used to read/write a known (PARAMETER, LOCAL, or CONTEXT)
383   // variable.  May emit code to traverse the context chain, loading the
384   // found context into the scratch register.  Writing to this operand will
385   // need the write barrier if location is CONTEXT.
386   MemOperand VarOperand(Variable* var, Register scratch);
387
388   void VisitForEffect(Expression* expr) {
389     EffectContext context(this);
390     Visit(expr);
391     PrepareForBailout(expr, NO_REGISTERS);
392   }
393
394   void VisitForAccumulatorValue(Expression* expr) {
395     AccumulatorValueContext context(this);
396     Visit(expr);
397     PrepareForBailout(expr, TOS_REG);
398   }
399
400   void VisitForStackValue(Expression* expr) {
401     StackValueContext context(this);
402     Visit(expr);
403     PrepareForBailout(expr, NO_REGISTERS);
404   }
405
406   void VisitForControl(Expression* expr,
407                        Label* if_true,
408                        Label* if_false,
409                        Label* fall_through) {
410     TestContext context(this, expr, if_true, if_false, fall_through);
411     Visit(expr);
412     // For test contexts, we prepare for bailout before branching, not at
413     // the end of the entire expression.  This happens as part of visiting
414     // the expression.
415   }
416
417   void VisitInDuplicateContext(Expression* expr);
418
419   void VisitDeclarations(ZoneList<Declaration*>* declarations) OVERRIDE;
420   void DeclareModules(Handle<FixedArray> descriptions);
421   void DeclareGlobals(Handle<FixedArray> pairs);
422   int DeclareGlobalsFlags();
423
424   // Generate code to allocate all (including nested) modules and contexts.
425   // Because of recursive linking and the presence of module alias declarations,
426   // this has to be a separate pass _before_ populating or executing any module.
427   void AllocateModules(ZoneList<Declaration*>* declarations);
428
429   // Generate code to create an iterator result object.  The "value" property is
430   // set to a value popped from the stack, and "done" is set according to the
431   // argument.  The result object is left in the result register.
432   void EmitCreateIteratorResult(bool done);
433
434   // Try to perform a comparison as a fast inlined literal compare if
435   // the operands allow it.  Returns true if the compare operations
436   // has been matched and all code generated; false otherwise.
437   bool TryLiteralCompare(CompareOperation* compare);
438
439   // Platform-specific code for comparing the type of a value with
440   // a given literal string.
441   void EmitLiteralCompareTypeof(Expression* expr,
442                                 Expression* sub_expr,
443                                 Handle<String> check);
444
445   // Platform-specific code for equality comparison with a nil-like value.
446   void EmitLiteralCompareNil(CompareOperation* expr,
447                              Expression* sub_expr,
448                              NilValue nil);
449
450   // Bailout support.
451   void PrepareForBailout(Expression* node, State state);
452   void PrepareForBailoutForId(BailoutId id, State state);
453
454   // Feedback slot support. The feedback vector will be cleared during gc and
455   // collected by the type-feedback oracle.
456   Handle<TypeFeedbackVector> FeedbackVector() const {
457     return info_->feedback_vector();
458   }
459   void EnsureSlotContainsAllocationSite(FeedbackVectorSlot slot);
460   void EnsureSlotContainsAllocationSite(FeedbackVectorICSlot slot);
461
462   // Returns a smi for the index into the FixedArray that backs the feedback
463   // vector
464   Smi* SmiFromSlot(FeedbackVectorSlot slot) const {
465     return Smi::FromInt(FeedbackVector()->GetIndex(slot));
466   }
467
468   Smi* SmiFromSlot(FeedbackVectorICSlot slot) const {
469     return Smi::FromInt(FeedbackVector()->GetIndex(slot));
470   }
471
472   // Record a call's return site offset, used to rebuild the frame if the
473   // called function was inlined at the site.
474   void RecordJSReturnSite(Call* call);
475
476   // Prepare for bailout before a test (or compare) and branch.  If
477   // should_normalize, then the following comparison will not handle the
478   // canonical JS true value so we will insert a (dead) test against true at
479   // the actual bailout target from the optimized code. If not
480   // should_normalize, the true and false labels are ignored.
481   void PrepareForBailoutBeforeSplit(Expression* expr,
482                                     bool should_normalize,
483                                     Label* if_true,
484                                     Label* if_false);
485
486   // If enabled, emit debug code for checking that the current context is
487   // neither a with nor a catch context.
488   void EmitDebugCheckDeclarationContext(Variable* variable);
489
490   // This is meant to be called at loop back edges, |back_edge_target| is
491   // the jump target of the back edge and is used to approximate the amount
492   // of code inside the loop.
493   void EmitBackEdgeBookkeeping(IterationStatement* stmt,
494                                Label* back_edge_target);
495   // Record the OSR AST id corresponding to a back edge in the code.
496   void RecordBackEdge(BailoutId osr_ast_id);
497   // Emit a table of back edge ids, pcs and loop depths into the code stream.
498   // Return the offset of the start of the table.
499   unsigned EmitBackEdgeTable();
500
501   void EmitProfilingCounterDecrement(int delta);
502   void EmitProfilingCounterReset();
503
504   // Emit code to pop values from the stack associated with nested statements
505   // like try/catch, try/finally, etc, running the finallies and unwinding the
506   // handlers as needed.
507   void EmitUnwindBeforeReturn();
508
509   // Platform-specific return sequence
510   void EmitReturnSequence();
511
512   // Platform-specific code sequences for calls
513   void EmitCall(Call* expr, CallICState::CallType = CallICState::FUNCTION);
514   void EmitSuperConstructorCall(Call* expr);
515   void EmitCallWithLoadIC(Call* expr);
516   void EmitSuperCallWithLoadIC(Call* expr);
517   void EmitKeyedCallWithLoadIC(Call* expr, Expression* key);
518   void EmitKeyedSuperCallWithLoadIC(Call* expr);
519
520 #define FOR_EACH_FULL_CODE_INTRINSIC(F)   \
521   F(IsSmi)                                \
522   F(IsNonNegativeSmi)                     \
523   F(IsArray)                              \
524   F(IsRegExp)                             \
525   F(IsJSProxy)                            \
526   F(IsConstructCall)                      \
527   F(CallFunction)                         \
528   F(DefaultConstructorCallSuper)          \
529   F(ArgumentsLength)                      \
530   F(Arguments)                            \
531   F(ValueOf)                              \
532   F(SetValueOf)                           \
533   F(DateField)                            \
534   F(StringCharFromCode)                   \
535   F(StringCharAt)                         \
536   F(OneByteSeqStringSetChar)              \
537   F(TwoByteSeqStringSetChar)              \
538   F(ObjectEquals)                         \
539   F(IsObject)                             \
540   F(IsFunction)                           \
541   F(IsUndetectableObject)                 \
542   F(IsSpecObject)                         \
543   F(IsStringWrapperSafeForDefaultValueOf) \
544   F(MathPow)                              \
545   F(IsMinusZero)                          \
546   F(HasCachedArrayIndex)                  \
547   F(GetCachedArrayIndex)                  \
548   F(FastOneByteArrayJoin)                 \
549   F(GeneratorNext)                        \
550   F(GeneratorThrow)                       \
551   F(DebugBreakInOptimizedCode)            \
552   F(ClassOf)                              \
553   F(StringCharCodeAt)                     \
554   F(StringAdd)                            \
555   F(SubString)                            \
556   F(StringCompare)                        \
557   F(RegExpExec)                           \
558   F(RegExpConstructResult)                \
559   F(GetFromCache)                         \
560   F(NumberToString)                       \
561   F(DebugIsActive)
562
563 #define GENERATOR_DECLARATION(Name) void Emit##Name(CallRuntime* call);
564   FOR_EACH_FULL_CODE_INTRINSIC(GENERATOR_DECLARATION)
565 #undef GENERATOR_DECLARATION
566
567   // Platform-specific code for resuming generators.
568   void EmitGeneratorResume(Expression *generator,
569                            Expression *value,
570                            JSGeneratorObject::ResumeMode resume_mode);
571
572   // Platform-specific code for loading variables.
573   void EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
574                                      TypeofState typeof_state,
575                                      Label* slow);
576   MemOperand ContextSlotOperandCheckExtensions(Variable* var, Label* slow);
577   void EmitDynamicLookupFastCase(VariableProxy* proxy,
578                                  TypeofState typeof_state,
579                                  Label* slow,
580                                  Label* done);
581   void EmitVariableLoad(VariableProxy* proxy);
582
583   void EmitAccessor(Expression* expression);
584
585   // Expects the arguments and the function already pushed.
586   void EmitResolvePossiblyDirectEval(int arg_count);
587
588   // Platform-specific support for allocating a new closure based on
589   // the given function info.
590   void EmitNewClosure(Handle<SharedFunctionInfo> info, bool pretenure);
591
592   // Platform-specific support for compiling assignments.
593
594   // Left-hand side can only be a property, a global or a (parameter or local)
595   // slot.
596   enum LhsKind {
597     VARIABLE,
598     NAMED_PROPERTY,
599     KEYED_PROPERTY,
600     NAMED_SUPER_PROPERTY,
601     KEYED_SUPER_PROPERTY
602   };
603
604   static LhsKind GetAssignType(Property* property) {
605     if (property == NULL) return VARIABLE;
606     bool super_access = property->IsSuperAccess();
607     return (property->key()->IsPropertyName())
608                ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
609                : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
610   }
611
612   // Load a value from a named property.
613   // The receiver is left on the stack by the IC.
614   void EmitNamedPropertyLoad(Property* expr);
615
616   // Load a value from super.named property.
617   // Expect receiver ('this' value) and home_object on the stack.
618   void EmitNamedSuperPropertyLoad(Property* expr);
619
620   // Load a value from super[keyed] property.
621   // Expect receiver ('this' value), home_object and key on the stack.
622   void EmitKeyedSuperPropertyLoad(Property* expr);
623
624   // Load a value from a keyed property.
625   // The receiver and the key is left on the stack by the IC.
626   void EmitKeyedPropertyLoad(Property* expr);
627
628   // Adds the properties to the class (function) object and to its prototype.
629   // Expects the class (function) in the accumulator. The class (function) is
630   // in the accumulator after installing all the properties.
631   void EmitClassDefineProperties(ClassLiteral* lit);
632
633   // Pushes the property key as a Name on the stack.
634   void EmitPropertyKey(ObjectLiteralProperty* property, BailoutId bailout_id);
635
636   // Apply the compound assignment operator. Expects the left operand on top
637   // of the stack and the right one in the accumulator.
638   void EmitBinaryOp(BinaryOperation* expr, Token::Value op);
639
640   // Helper functions for generating inlined smi code for certain
641   // binary operations.
642   void EmitInlineSmiBinaryOp(BinaryOperation* expr,
643                              Token::Value op,
644                              Expression* left,
645                              Expression* right);
646
647   // Assign to the given expression as if via '='. The right-hand-side value
648   // is expected in the accumulator.
649   void EmitAssignment(Expression* expr);
650
651   // Complete a variable assignment.  The right-hand-side value is expected
652   // in the accumulator.
653   void EmitVariableAssignment(Variable* var,
654                               Token::Value op);
655
656   // Helper functions to EmitVariableAssignment
657   void EmitStoreToStackLocalOrContextSlot(Variable* var,
658                                           MemOperand location);
659
660   // Complete a named property assignment.  The receiver is expected on top
661   // of the stack and the right-hand-side value in the accumulator.
662   void EmitNamedPropertyAssignment(Assignment* expr);
663
664   // Complete a super named property assignment. The right-hand-side value
665   // is expected in accumulator.
666   void EmitNamedSuperPropertyStore(Property* prop);
667
668   // Complete a super named property assignment. The right-hand-side value
669   // is expected in accumulator.
670   void EmitKeyedSuperPropertyStore(Property* prop);
671
672   // Complete a keyed property assignment.  The receiver and key are
673   // expected on top of the stack and the right-hand-side value in the
674   // accumulator.
675   void EmitKeyedPropertyAssignment(Assignment* expr);
676
677   void EmitLoadHomeObject(SuperReference* expr);
678
679   static bool NeedsHomeObject(Expression* expr) {
680     return FunctionLiteral::NeedsHomeObject(expr);
681   }
682
683   // Adds the [[HomeObject]] to |initializer| if it is a FunctionLiteral.
684   // The value of the initializer is expected to be at the top of the stack.
685   // |offset| is the offset in the stack where the home object can be found.
686   void EmitSetHomeObjectIfNeeded(Expression* initializer, int offset);
687
688   void EmitLoadSuperConstructor();
689
690   void CallIC(Handle<Code> code,
691               TypeFeedbackId id = TypeFeedbackId::None());
692
693   void CallLoadIC(ContextualMode mode,
694                   TypeFeedbackId id = TypeFeedbackId::None());
695   void CallGlobalLoadIC(Handle<String> name);
696   void CallStoreIC(TypeFeedbackId id = TypeFeedbackId::None());
697
698   void SetFunctionPosition(FunctionLiteral* fun);
699   void SetReturnPosition(FunctionLiteral* fun);
700   void SetStatementPosition(Statement* stmt);
701   void SetExpressionPosition(Expression* expr);
702   void SetSourcePosition(int pos);
703
704   // Non-local control flow support.
705   void EnterTryBlock(int handler_index, Label* handler);
706   void ExitTryBlock(int handler_index);
707   void EnterFinallyBlock();
708   void ExitFinallyBlock();
709
710   // Loop nesting counter.
711   int loop_depth() { return loop_depth_; }
712   void increment_loop_depth() { loop_depth_++; }
713   void decrement_loop_depth() {
714     DCHECK(loop_depth_ > 0);
715     loop_depth_--;
716   }
717
718   MacroAssembler* masm() const { return masm_; }
719
720   class ExpressionContext;
721   const ExpressionContext* context() { return context_; }
722   void set_new_context(const ExpressionContext* context) { context_ = context; }
723
724   Handle<Script> script() { return info_->script(); }
725   bool is_eval() { return info_->is_eval(); }
726   bool is_native() { return info_->is_native(); }
727   LanguageMode language_mode() { return function()->language_mode(); }
728   bool is_simple_parameter_list() { return info_->is_simple_parameter_list(); }
729   FunctionLiteral* function() { return info_->function(); }
730   Scope* scope() { return scope_; }
731
732   static Register result_register();
733   static Register context_register();
734
735   // Set fields in the stack frame. Offsets are the frame pointer relative
736   // offsets defined in, e.g., StandardFrameConstants.
737   void StoreToFrameField(int frame_offset, Register value);
738
739   // Load a value from the current context. Indices are defined as an enum
740   // in v8::internal::Context.
741   void LoadContextField(Register dst, int context_index);
742
743   // Push the function argument for the runtime functions PushWithContext
744   // and PushCatchContext.
745   void PushFunctionArgumentForContextAllocation();
746
747   // AST node visit functions.
748 #define DECLARE_VISIT(type) virtual void Visit##type(type* node) OVERRIDE;
749   AST_NODE_LIST(DECLARE_VISIT)
750 #undef DECLARE_VISIT
751
752   void VisitComma(BinaryOperation* expr);
753   void VisitLogicalExpression(BinaryOperation* expr);
754   void VisitArithmeticExpression(BinaryOperation* expr);
755
756   void VisitForTypeofValue(Expression* expr);
757
758   void Generate();
759   void PopulateDeoptimizationData(Handle<Code> code);
760   void PopulateTypeFeedbackInfo(Handle<Code> code);
761
762   Handle<HandlerTable> handler_table() { return handler_table_; }
763
764   struct BailoutEntry {
765     BailoutId id;
766     unsigned pc_and_state;
767   };
768
769   struct BackEdgeEntry {
770     BailoutId id;
771     unsigned pc;
772     uint32_t loop_depth;
773   };
774
775   class ExpressionContext BASE_EMBEDDED {
776    public:
777     explicit ExpressionContext(FullCodeGenerator* codegen)
778         : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
779       codegen->set_new_context(this);
780     }
781
782     virtual ~ExpressionContext() {
783       codegen_->set_new_context(old_);
784     }
785
786     Isolate* isolate() const { return codegen_->isolate(); }
787
788     // Convert constant control flow (true or false) to the result expected for
789     // this expression context.
790     virtual void Plug(bool flag) const = 0;
791
792     // Emit code to convert a pure value (in a register, known variable
793     // location, as a literal, or on top of the stack) into the result
794     // expected according to this expression context.
795     virtual void Plug(Register reg) const = 0;
796     virtual void Plug(Variable* var) const = 0;
797     virtual void Plug(Handle<Object> lit) const = 0;
798     virtual void Plug(Heap::RootListIndex index) const = 0;
799     virtual void PlugTOS() const = 0;
800
801     // Emit code to convert pure control flow to a pair of unbound labels into
802     // the result expected according to this expression context.  The
803     // implementation will bind both labels unless it's a TestContext, which
804     // won't bind them at this point.
805     virtual void Plug(Label* materialize_true,
806                       Label* materialize_false) const = 0;
807
808     // Emit code to discard count elements from the top of stack, then convert
809     // a pure value into the result expected according to this expression
810     // context.
811     virtual void DropAndPlug(int count, Register reg) const = 0;
812
813     // Set up branch labels for a test expression.  The three Label** parameters
814     // are output parameters.
815     virtual void PrepareTest(Label* materialize_true,
816                              Label* materialize_false,
817                              Label** if_true,
818                              Label** if_false,
819                              Label** fall_through) const = 0;
820
821     // Returns true if we are evaluating only for side effects (i.e. if the
822     // result will be discarded).
823     virtual bool IsEffect() const { return false; }
824
825     // Returns true if we are evaluating for the value (in accu/on stack).
826     virtual bool IsAccumulatorValue() const { return false; }
827     virtual bool IsStackValue() const { return false; }
828
829     // Returns true if we are branching on the value rather than materializing
830     // it.  Only used for asserts.
831     virtual bool IsTest() const { return false; }
832
833    protected:
834     FullCodeGenerator* codegen() const { return codegen_; }
835     MacroAssembler* masm() const { return masm_; }
836     MacroAssembler* masm_;
837
838    private:
839     const ExpressionContext* old_;
840     FullCodeGenerator* codegen_;
841   };
842
843   class AccumulatorValueContext : public ExpressionContext {
844    public:
845     explicit AccumulatorValueContext(FullCodeGenerator* codegen)
846         : ExpressionContext(codegen) { }
847
848     virtual void Plug(bool flag) const;
849     virtual void Plug(Register reg) const;
850     virtual void Plug(Label* materialize_true, Label* materialize_false) const;
851     virtual void Plug(Variable* var) const;
852     virtual void Plug(Handle<Object> lit) const;
853     virtual void Plug(Heap::RootListIndex) const;
854     virtual void PlugTOS() const;
855     virtual void DropAndPlug(int count, Register reg) const;
856     virtual void PrepareTest(Label* materialize_true,
857                              Label* materialize_false,
858                              Label** if_true,
859                              Label** if_false,
860                              Label** fall_through) const;
861     virtual bool IsAccumulatorValue() const { return true; }
862   };
863
864   class StackValueContext : public ExpressionContext {
865    public:
866     explicit StackValueContext(FullCodeGenerator* codegen)
867         : ExpressionContext(codegen) { }
868
869     virtual void Plug(bool flag) const;
870     virtual void Plug(Register reg) const;
871     virtual void Plug(Label* materialize_true, Label* materialize_false) const;
872     virtual void Plug(Variable* var) const;
873     virtual void Plug(Handle<Object> lit) const;
874     virtual void Plug(Heap::RootListIndex) const;
875     virtual void PlugTOS() const;
876     virtual void DropAndPlug(int count, Register reg) const;
877     virtual void PrepareTest(Label* materialize_true,
878                              Label* materialize_false,
879                              Label** if_true,
880                              Label** if_false,
881                              Label** fall_through) const;
882     virtual bool IsStackValue() const { return true; }
883   };
884
885   class TestContext : public ExpressionContext {
886    public:
887     TestContext(FullCodeGenerator* codegen,
888                 Expression* condition,
889                 Label* true_label,
890                 Label* false_label,
891                 Label* fall_through)
892         : ExpressionContext(codegen),
893           condition_(condition),
894           true_label_(true_label),
895           false_label_(false_label),
896           fall_through_(fall_through) { }
897
898     static const TestContext* cast(const ExpressionContext* context) {
899       DCHECK(context->IsTest());
900       return reinterpret_cast<const TestContext*>(context);
901     }
902
903     Expression* condition() const { return condition_; }
904     Label* true_label() const { return true_label_; }
905     Label* false_label() const { return false_label_; }
906     Label* fall_through() const { return fall_through_; }
907
908     virtual void Plug(bool flag) const;
909     virtual void Plug(Register reg) const;
910     virtual void Plug(Label* materialize_true, Label* materialize_false) const;
911     virtual void Plug(Variable* var) const;
912     virtual void Plug(Handle<Object> lit) const;
913     virtual void Plug(Heap::RootListIndex) const;
914     virtual void PlugTOS() const;
915     virtual void DropAndPlug(int count, Register reg) const;
916     virtual void PrepareTest(Label* materialize_true,
917                              Label* materialize_false,
918                              Label** if_true,
919                              Label** if_false,
920                              Label** fall_through) const;
921     virtual bool IsTest() const { return true; }
922
923    private:
924     Expression* condition_;
925     Label* true_label_;
926     Label* false_label_;
927     Label* fall_through_;
928   };
929
930   class EffectContext : public ExpressionContext {
931    public:
932     explicit EffectContext(FullCodeGenerator* codegen)
933         : ExpressionContext(codegen) { }
934
935     virtual void Plug(bool flag) const;
936     virtual void Plug(Register reg) const;
937     virtual void Plug(Label* materialize_true, Label* materialize_false) const;
938     virtual void Plug(Variable* var) const;
939     virtual void Plug(Handle<Object> lit) const;
940     virtual void Plug(Heap::RootListIndex) const;
941     virtual void PlugTOS() const;
942     virtual void DropAndPlug(int count, Register reg) const;
943     virtual void PrepareTest(Label* materialize_true,
944                              Label* materialize_false,
945                              Label** if_true,
946                              Label** if_false,
947                              Label** fall_through) const;
948     virtual bool IsEffect() const { return true; }
949   };
950
951   class EnterBlockScopeIfNeeded {
952    public:
953     EnterBlockScopeIfNeeded(FullCodeGenerator* codegen, Scope* scope,
954                             BailoutId entry_id, BailoutId declarations_id,
955                             BailoutId exit_id);
956     ~EnterBlockScopeIfNeeded();
957
958    private:
959     MacroAssembler* masm() const { return codegen_->masm(); }
960
961     FullCodeGenerator* codegen_;
962     Scope* scope_;
963     Scope* saved_scope_;
964     BailoutId exit_id_;
965   };
966
967   MacroAssembler* masm_;
968   CompilationInfo* info_;
969   Scope* scope_;
970   Label return_label_;
971   NestedStatement* nesting_stack_;
972   int loop_depth_;
973   ZoneList<Handle<Object> >* globals_;
974   Handle<FixedArray> modules_;
975   int module_index_;
976   const ExpressionContext* context_;
977   ZoneList<BailoutEntry> bailout_entries_;
978   ZoneList<BackEdgeEntry> back_edges_;
979   int ic_total_count_;
980   Handle<HandlerTable> handler_table_;
981   Handle<Cell> profiling_counter_;
982   bool generate_debug_code_;
983
984   friend class NestedStatement;
985
986   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
987   DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
988 };
989
990
991 // A map from property names to getter/setter pairs allocated in the zone.
992 class AccessorTable: public TemplateHashMap<Literal,
993                                             ObjectLiteral::Accessors,
994                                             ZoneAllocationPolicy> {
995  public:
996   explicit AccessorTable(Zone* zone) :
997       TemplateHashMap<Literal, ObjectLiteral::Accessors,
998                       ZoneAllocationPolicy>(Literal::Match,
999                                             ZoneAllocationPolicy(zone)),
1000       zone_(zone) { }
1001
1002   Iterator lookup(Literal* literal) {
1003     Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
1004     if (it->second == NULL) it->second = new(zone_) ObjectLiteral::Accessors();
1005     return it;
1006   }
1007
1008  private:
1009   Zone* zone_;
1010 };
1011
1012
1013 class BackEdgeTable {
1014  public:
1015   BackEdgeTable(Code* code, DisallowHeapAllocation* required) {
1016     DCHECK(code->kind() == Code::FUNCTION);
1017     instruction_start_ = code->instruction_start();
1018     Address table_address = instruction_start_ + code->back_edge_table_offset();
1019     length_ = Memory::uint32_at(table_address);
1020     start_ = table_address + kTableLengthSize;
1021   }
1022
1023   uint32_t length() { return length_; }
1024
1025   BailoutId ast_id(uint32_t index) {
1026     return BailoutId(static_cast<int>(
1027         Memory::uint32_at(entry_at(index) + kAstIdOffset)));
1028   }
1029
1030   uint32_t loop_depth(uint32_t index) {
1031     return Memory::uint32_at(entry_at(index) + kLoopDepthOffset);
1032   }
1033
1034   uint32_t pc_offset(uint32_t index) {
1035     return Memory::uint32_at(entry_at(index) + kPcOffsetOffset);
1036   }
1037
1038   Address pc(uint32_t index) {
1039     return instruction_start_ + pc_offset(index);
1040   }
1041
1042   enum BackEdgeState {
1043     INTERRUPT,
1044     ON_STACK_REPLACEMENT,
1045     OSR_AFTER_STACK_CHECK
1046   };
1047
1048   // Increase allowed loop nesting level by one and patch those matching loops.
1049   static void Patch(Isolate* isolate, Code* unoptimized_code);
1050
1051   // Patch the back edge to the target state, provided the correct callee.
1052   static void PatchAt(Code* unoptimized_code,
1053                       Address pc,
1054                       BackEdgeState target_state,
1055                       Code* replacement_code);
1056
1057   // Change all patched back edges back to normal interrupts.
1058   static void Revert(Isolate* isolate,
1059                      Code* unoptimized_code);
1060
1061   // Change a back edge patched for on-stack replacement to perform a
1062   // stack check first.
1063   static void AddStackCheck(Handle<Code> code, uint32_t pc_offset);
1064
1065   // Revert the patch by AddStackCheck.
1066   static void RemoveStackCheck(Handle<Code> code, uint32_t pc_offset);
1067
1068   // Return the current patch state of the back edge.
1069   static BackEdgeState GetBackEdgeState(Isolate* isolate,
1070                                         Code* unoptimized_code,
1071                                         Address pc_after);
1072
1073 #ifdef DEBUG
1074   // Verify that all back edges of a certain loop depth are patched.
1075   static bool Verify(Isolate* isolate, Code* unoptimized_code);
1076 #endif  // DEBUG
1077
1078  private:
1079   Address entry_at(uint32_t index) {
1080     DCHECK(index < length_);
1081     return start_ + index * kEntrySize;
1082   }
1083
1084   static const int kTableLengthSize = kIntSize;
1085   static const int kAstIdOffset = 0 * kIntSize;
1086   static const int kPcOffsetOffset = 1 * kIntSize;
1087   static const int kLoopDepthOffset = 2 * kIntSize;
1088   static const int kEntrySize = 3 * kIntSize;
1089
1090   Address start_;
1091   Address instruction_start_;
1092   uint32_t length_;
1093 };
1094
1095
1096 } }  // namespace v8::internal
1097
1098 #endif  // V8_FULL_CODEGEN_H_