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