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