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.
5 #ifndef V8_FULL_CODEGEN_H_
6 #define V8_FULL_CODEGEN_H_
10 #include "src/allocation.h"
11 #include "src/assert-scope.h"
13 #include "src/code-stubs.h"
14 #include "src/codegen.h"
15 #include "src/compiler.h"
16 #include "src/data-flow.h"
17 #include "src/globals.h"
18 #include "src/objects.h"
23 // Forward declarations.
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 {
32 explicit BreakableStatementChecker(Zone* zone) : is_breakable_(false) {
33 InitializeAstVisitor(zone);
36 void Check(Statement* stmt);
37 void Check(Expression* stmt);
39 bool is_breakable() { return is_breakable_; }
42 // AST node visit functions.
43 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
44 AST_NODE_LIST(DECLARE_VISIT)
49 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
50 DISALLOW_COPY_AND_ASSIGN(BreakableStatementChecker);
54 // -----------------------------------------------------------------------------
55 // Full code generator.
57 class FullCodeGenerator: public AstVisitor {
64 FullCodeGenerator(MacroAssembler* masm, CompilationInfo* info)
67 scope_(info->scope()),
72 bailout_entries_(info->HasDeoptimizationSupport()
73 ? info->function()->ast_node_count() : 0,
75 back_edges_(2, info->zone()),
77 DCHECK(!info->IsStub());
83 static bool MakeCode(CompilationInfo* info);
85 // Encode state and pc-offset as a BitField<type, start, size>.
86 // Only use 30 bits because we encode the result as a smi.
87 class StateField : public BitField<State, 0, 1> { };
88 class PcField : public BitField<unsigned, 1, 30-1> { };
90 static const char* State2String(State state) {
92 case NO_REGISTERS: return "NO_REGISTERS";
93 case TOS_REG: return "TOS_REG";
99 static const int kMaxBackEdgeWeight = 127;
101 // Platform-specific code size multiplier.
102 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
103 static const int kCodeSizeMultiplier = 105;
104 static const int kBootCodeSizeMultiplier = 100;
105 #elif V8_TARGET_ARCH_X64
106 static const int kCodeSizeMultiplier = 170;
107 static const int kBootCodeSizeMultiplier = 140;
108 #elif V8_TARGET_ARCH_ARM
109 static const int kCodeSizeMultiplier = 149;
110 static const int kBootCodeSizeMultiplier = 110;
111 #elif V8_TARGET_ARCH_ARM64
112 // TODO(all): Copied ARM value. Check this is sensible for ARM64.
113 static const int kCodeSizeMultiplier = 149;
114 static const int kBootCodeSizeMultiplier = 110;
115 #elif V8_TARGET_ARCH_MIPS
116 static const int kCodeSizeMultiplier = 149;
117 static const int kBootCodeSizeMultiplier = 120;
118 #elif V8_TARGET_ARCH_MIPS64
119 static const int kCodeSizeMultiplier = 149;
120 static const int kBootCodeSizeMultiplier = 120;
122 #error Unsupported target architecture.
131 class NestedStatement BASE_EMBEDDED {
133 explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
134 // Link into codegen's nesting stack.
135 previous_ = codegen->nesting_stack_;
136 codegen->nesting_stack_ = this;
138 virtual ~NestedStatement() {
139 // Unlink from codegen's nesting stack.
140 DCHECK_EQ(this, codegen_->nesting_stack_);
141 codegen_->nesting_stack_ = previous_;
144 virtual Breakable* AsBreakable() { return NULL; }
145 virtual Iteration* AsIteration() { return NULL; }
147 virtual bool IsContinueTarget(Statement* target) { return false; }
148 virtual bool IsBreakTarget(Statement* target) { return false; }
150 // Notify the statement that we are exiting it via break, continue, or
151 // return and give it a chance to generate cleanup code. Return the
152 // next outer statement in the nesting stack. We accumulate in
153 // *stack_depth the amount to drop the stack and in *context_length the
154 // number of context chain links to unwind as we traverse the nesting
155 // stack from an exit to its target.
156 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
161 MacroAssembler* masm() { return codegen_->masm(); }
163 FullCodeGenerator* codegen_;
164 NestedStatement* previous_;
167 DISALLOW_COPY_AND_ASSIGN(NestedStatement);
170 // A breakable statement such as a block.
171 class Breakable : public NestedStatement {
173 Breakable(FullCodeGenerator* codegen, BreakableStatement* statement)
174 : NestedStatement(codegen), statement_(statement) {
176 virtual ~Breakable() {}
178 virtual Breakable* AsBreakable() { return this; }
179 virtual bool IsBreakTarget(Statement* target) {
180 return statement() == target;
183 BreakableStatement* statement() { return statement_; }
184 Label* break_label() { return &break_label_; }
187 BreakableStatement* statement_;
191 // An iteration statement such as a while, for, or do loop.
192 class Iteration : public Breakable {
194 Iteration(FullCodeGenerator* codegen, IterationStatement* statement)
195 : Breakable(codegen, statement) {
197 virtual ~Iteration() {}
199 virtual Iteration* AsIteration() { return this; }
200 virtual bool IsContinueTarget(Statement* target) {
201 return statement() == target;
204 Label* continue_label() { return &continue_label_; }
207 Label continue_label_;
210 // A nested block statement.
211 class NestedBlock : public Breakable {
213 NestedBlock(FullCodeGenerator* codegen, Block* block)
214 : Breakable(codegen, block) {
216 virtual ~NestedBlock() {}
218 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
219 if (statement()->AsBlock()->scope() != NULL) {
226 // The try block of a try/catch statement.
227 class TryCatch : public NestedStatement {
229 explicit TryCatch(FullCodeGenerator* codegen) : NestedStatement(codegen) {
231 virtual ~TryCatch() {}
233 virtual NestedStatement* Exit(int* stack_depth, int* context_length);
236 // The try block of a try/finally statement.
237 class TryFinally : public NestedStatement {
239 TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
240 : NestedStatement(codegen), finally_entry_(finally_entry) {
242 virtual ~TryFinally() {}
244 virtual NestedStatement* Exit(int* stack_depth, int* context_length);
247 Label* finally_entry_;
250 // The finally block of a try/finally statement.
251 class Finally : public NestedStatement {
253 static const int kElementCount = 5;
255 explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { }
256 virtual ~Finally() {}
258 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
259 *stack_depth += kElementCount;
264 // The body of a for/in loop.
265 class ForIn : public Iteration {
267 static const int kElementCount = 5;
269 ForIn(FullCodeGenerator* codegen, ForInStatement* statement)
270 : Iteration(codegen, statement) {
274 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
275 *stack_depth += kElementCount;
281 // The body of a with or catch.
282 class WithOrCatch : public NestedStatement {
284 explicit WithOrCatch(FullCodeGenerator* codegen)
285 : NestedStatement(codegen) {
287 virtual ~WithOrCatch() {}
289 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
295 // Type of a member function that generates inline code for a native function.
296 typedef void (FullCodeGenerator::*InlineFunctionGenerator)(CallRuntime* expr);
298 static const InlineFunctionGenerator kInlineFunctionGenerators[];
300 // A platform-specific utility to overwrite the accumulator register
301 // with a GC-safe value.
302 void ClearAccumulator();
304 // Determine whether or not to inline the smi case for the given
306 bool ShouldInlineSmiCase(Token::Value op);
308 // Helper function to convert a pure value into a test context. The value
309 // is expected on the stack or the accumulator, depending on the platform.
310 // See the platform-specific implementation for details.
311 void DoTest(Expression* condition,
314 Label* fall_through);
315 void DoTest(const TestContext* context);
317 // Helper function to split control flow and avoid a branch to the
318 // fall-through label if it is set up.
319 #if V8_TARGET_ARCH_MIPS
320 void Split(Condition cc,
325 Label* fall_through);
326 #elif V8_TARGET_ARCH_MIPS64
327 void Split(Condition cc,
332 Label* fall_through);
333 #else // All non-mips arch.
334 void Split(Condition cc,
337 Label* fall_through);
338 #endif // V8_TARGET_ARCH_MIPS
340 // Load the value of a known (PARAMETER, LOCAL, or CONTEXT) variable into
341 // a register. Emits a context chain walk if if necessary (so does
342 // SetVar) so avoid calling both on the same variable.
343 void GetVar(Register destination, Variable* var);
345 // Assign to a known (PARAMETER, LOCAL, or CONTEXT) variable. If it's in
346 // the context, the write barrier will be emitted and source, scratch0,
347 // scratch1 will be clobbered. Emits a context chain walk if if necessary
348 // (so does GetVar) so avoid calling both on the same variable.
349 void SetVar(Variable* var,
354 // An operand used to read/write a stack-allocated (PARAMETER or LOCAL)
355 // variable. Writing does not need the write barrier.
356 MemOperand StackOperand(Variable* var);
358 // An operand used to read/write a known (PARAMETER, LOCAL, or CONTEXT)
359 // variable. May emit code to traverse the context chain, loading the
360 // found context into the scratch register. Writing to this operand will
361 // need the write barrier if location is CONTEXT.
362 MemOperand VarOperand(Variable* var, Register scratch);
364 void VisitForEffect(Expression* expr) {
365 EffectContext context(this);
367 PrepareForBailout(expr, NO_REGISTERS);
370 void VisitForAccumulatorValue(Expression* expr) {
371 AccumulatorValueContext context(this);
373 PrepareForBailout(expr, TOS_REG);
376 void VisitForStackValue(Expression* expr) {
377 StackValueContext context(this);
379 PrepareForBailout(expr, NO_REGISTERS);
382 void VisitForControl(Expression* expr,
385 Label* fall_through) {
386 TestContext context(this, expr, if_true, if_false, fall_through);
388 // For test contexts, we prepare for bailout before branching, not at
389 // the end of the entire expression. This happens as part of visiting
393 void VisitInDuplicateContext(Expression* expr);
395 void VisitDeclarations(ZoneList<Declaration*>* declarations);
396 void DeclareModules(Handle<FixedArray> descriptions);
397 void DeclareGlobals(Handle<FixedArray> pairs);
398 int DeclareGlobalsFlags();
400 // Generate code to allocate all (including nested) modules and contexts.
401 // Because of recursive linking and the presence of module alias declarations,
402 // this has to be a separate pass _before_ populating or executing any module.
403 void AllocateModules(ZoneList<Declaration*>* declarations);
405 // Generate code to create an iterator result object. The "value" property is
406 // set to a value popped from the stack, and "done" is set according to the
407 // argument. The result object is left in the result register.
408 void EmitCreateIteratorResult(bool done);
410 // Try to perform a comparison as a fast inlined literal compare if
411 // the operands allow it. Returns true if the compare operations
412 // has been matched and all code generated; false otherwise.
413 bool TryLiteralCompare(CompareOperation* compare);
415 // Platform-specific code for comparing the type of a value with
416 // a given literal string.
417 void EmitLiteralCompareTypeof(Expression* expr,
418 Expression* sub_expr,
419 Handle<String> check);
421 // Platform-specific code for equality comparison with a nil-like value.
422 void EmitLiteralCompareNil(CompareOperation* expr,
423 Expression* sub_expr,
427 void PrepareForBailout(Expression* node, State state);
428 void PrepareForBailoutForId(BailoutId id, State state);
430 // Feedback slot support. The feedback vector will be cleared during gc and
431 // collected by the type-feedback oracle.
432 Handle<FixedArray> FeedbackVector() {
433 return info_->feedback_vector();
435 void EnsureSlotContainsAllocationSite(int slot);
437 // Record a call's return site offset, used to rebuild the frame if the
438 // called function was inlined at the site.
439 void RecordJSReturnSite(Call* call);
441 // Prepare for bailout before a test (or compare) and branch. If
442 // should_normalize, then the following comparison will not handle the
443 // canonical JS true value so we will insert a (dead) test against true at
444 // the actual bailout target from the optimized code. If not
445 // should_normalize, the true and false labels are ignored.
446 void PrepareForBailoutBeforeSplit(Expression* expr,
447 bool should_normalize,
451 // If enabled, emit debug code for checking that the current context is
452 // neither a with nor a catch context.
453 void EmitDebugCheckDeclarationContext(Variable* variable);
455 // This is meant to be called at loop back edges, |back_edge_target| is
456 // the jump target of the back edge and is used to approximate the amount
457 // of code inside the loop.
458 void EmitBackEdgeBookkeeping(IterationStatement* stmt,
459 Label* back_edge_target);
460 // Record the OSR AST id corresponding to a back edge in the code.
461 void RecordBackEdge(BailoutId osr_ast_id);
462 // Emit a table of back edge ids, pcs and loop depths into the code stream.
463 // Return the offset of the start of the table.
464 unsigned EmitBackEdgeTable();
466 void EmitProfilingCounterDecrement(int delta);
467 void EmitProfilingCounterReset();
469 // Emit code to pop values from the stack associated with nested statements
470 // like try/catch, try/finally, etc, running the finallies and unwinding the
471 // handlers as needed.
472 void EmitUnwindBeforeReturn();
474 // Platform-specific return sequence
475 void EmitReturnSequence();
477 // Platform-specific code sequences for calls
478 void EmitCall(Call* expr, CallIC::CallType = CallIC::FUNCTION);
479 void EmitCallWithLoadIC(Call* expr);
480 void EmitKeyedCallWithLoadIC(Call* expr, Expression* key);
482 // Platform-specific code for inline runtime calls.
483 InlineFunctionGenerator FindInlineFunctionGenerator(Runtime::FunctionId id);
485 void EmitInlineRuntimeCall(CallRuntime* expr);
487 #define EMIT_INLINE_RUNTIME_CALL(name, x, y) \
488 void Emit##name(CallRuntime* expr);
489 INLINE_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
490 #undef EMIT_INLINE_RUNTIME_CALL
492 // Platform-specific code for resuming generators.
493 void EmitGeneratorResume(Expression *generator,
495 JSGeneratorObject::ResumeMode resume_mode);
497 // Platform-specific code for loading variables.
498 void EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
499 TypeofState typeof_state,
501 MemOperand ContextSlotOperandCheckExtensions(Variable* var, Label* slow);
502 void EmitDynamicLookupFastCase(VariableProxy* proxy,
503 TypeofState typeof_state,
506 void EmitVariableLoad(VariableProxy* proxy);
508 void EmitAccessor(Expression* expression);
510 // Expects the arguments and the function already pushed.
511 void EmitResolvePossiblyDirectEval(int arg_count);
513 // Platform-specific support for allocating a new closure based on
514 // the given function info.
515 void EmitNewClosure(Handle<SharedFunctionInfo> info, bool pretenure);
517 // Platform-specific support for compiling assignments.
519 // Load a value from a named property.
520 // The receiver is left on the stack by the IC.
521 void EmitNamedPropertyLoad(Property* expr);
523 // Load a value from a keyed property.
524 // The receiver and the key is left on the stack by the IC.
525 void EmitKeyedPropertyLoad(Property* expr);
527 // Apply the compound assignment operator. Expects the left operand on top
528 // of the stack and the right one in the accumulator.
529 void EmitBinaryOp(BinaryOperation* expr,
533 // Helper functions for generating inlined smi code for certain
534 // binary operations.
535 void EmitInlineSmiBinaryOp(BinaryOperation* expr,
541 // Assign to the given expression as if via '='. The right-hand-side value
542 // is expected in the accumulator.
543 void EmitAssignment(Expression* expr);
545 // Complete a variable assignment. The right-hand-side value is expected
546 // in the accumulator.
547 void EmitVariableAssignment(Variable* var,
550 // Helper functions to EmitVariableAssignment
551 void EmitStoreToStackLocalOrContextSlot(Variable* var,
552 MemOperand location);
554 // Complete a named property assignment. The receiver is expected on top
555 // of the stack and the right-hand-side value in the accumulator.
556 void EmitNamedPropertyAssignment(Assignment* expr);
558 // Complete a keyed property assignment. The receiver and key are
559 // expected on top of the stack and the right-hand-side value in the
561 void EmitKeyedPropertyAssignment(Assignment* expr);
563 void CallIC(Handle<Code> code,
564 TypeFeedbackId id = TypeFeedbackId::None());
566 void CallLoadIC(ContextualMode mode,
567 TypeFeedbackId id = TypeFeedbackId::None());
568 void CallStoreIC(TypeFeedbackId id = TypeFeedbackId::None());
570 void SetFunctionPosition(FunctionLiteral* fun);
571 void SetReturnPosition(FunctionLiteral* fun);
572 void SetStatementPosition(Statement* stmt);
573 void SetExpressionPosition(Expression* expr);
574 void SetSourcePosition(int pos);
576 // Non-local control flow support.
577 void EnterFinallyBlock();
578 void ExitFinallyBlock();
580 // Loop nesting counter.
581 int loop_depth() { return loop_depth_; }
582 void increment_loop_depth() { loop_depth_++; }
583 void decrement_loop_depth() {
584 DCHECK(loop_depth_ > 0);
588 MacroAssembler* masm() { return masm_; }
590 class ExpressionContext;
591 const ExpressionContext* context() { return context_; }
592 void set_new_context(const ExpressionContext* context) { context_ = context; }
594 Handle<Script> script() { return info_->script(); }
595 bool is_eval() { return info_->is_eval(); }
596 bool is_native() { return info_->is_native(); }
597 StrictMode strict_mode() { return function()->strict_mode(); }
598 FunctionLiteral* function() { return info_->function(); }
599 Scope* scope() { return scope_; }
601 static Register result_register();
602 static Register context_register();
604 // Set fields in the stack frame. Offsets are the frame pointer relative
605 // offsets defined in, e.g., StandardFrameConstants.
606 void StoreToFrameField(int frame_offset, Register value);
608 // Load a value from the current context. Indices are defined as an enum
609 // in v8::internal::Context.
610 void LoadContextField(Register dst, int context_index);
612 // Push the function argument for the runtime functions PushWithContext
613 // and PushCatchContext.
614 void PushFunctionArgumentForContextAllocation();
616 // AST node visit functions.
617 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
618 AST_NODE_LIST(DECLARE_VISIT)
621 void VisitComma(BinaryOperation* expr);
622 void VisitLogicalExpression(BinaryOperation* expr);
623 void VisitArithmeticExpression(BinaryOperation* expr);
625 void VisitForTypeofValue(Expression* expr);
628 void PopulateDeoptimizationData(Handle<Code> code);
629 void PopulateTypeFeedbackInfo(Handle<Code> code);
631 Handle<FixedArray> handler_table() { return handler_table_; }
633 struct BailoutEntry {
635 unsigned pc_and_state;
638 struct BackEdgeEntry {
644 class ExpressionContext BASE_EMBEDDED {
646 explicit ExpressionContext(FullCodeGenerator* codegen)
647 : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
648 codegen->set_new_context(this);
651 virtual ~ExpressionContext() {
652 codegen_->set_new_context(old_);
655 Isolate* isolate() const { return codegen_->isolate(); }
657 // Convert constant control flow (true or false) to the result expected for
658 // this expression context.
659 virtual void Plug(bool flag) const = 0;
661 // Emit code to convert a pure value (in a register, known variable
662 // location, as a literal, or on top of the stack) into the result
663 // expected according to this expression context.
664 virtual void Plug(Register reg) const = 0;
665 virtual void Plug(Variable* var) const = 0;
666 virtual void Plug(Handle<Object> lit) const = 0;
667 virtual void Plug(Heap::RootListIndex index) const = 0;
668 virtual void PlugTOS() const = 0;
670 // Emit code to convert pure control flow to a pair of unbound labels into
671 // the result expected according to this expression context. The
672 // implementation will bind both labels unless it's a TestContext, which
673 // won't bind them at this point.
674 virtual void Plug(Label* materialize_true,
675 Label* materialize_false) const = 0;
677 // Emit code to discard count elements from the top of stack, then convert
678 // a pure value into the result expected according to this expression
680 virtual void DropAndPlug(int count, Register reg) const = 0;
682 // Set up branch labels for a test expression. The three Label** parameters
683 // are output parameters.
684 virtual void PrepareTest(Label* materialize_true,
685 Label* materialize_false,
688 Label** fall_through) const = 0;
690 // Returns true if we are evaluating only for side effects (i.e. if the
691 // result will be discarded).
692 virtual bool IsEffect() const { return false; }
694 // Returns true if we are evaluating for the value (in accu/on stack).
695 virtual bool IsAccumulatorValue() const { return false; }
696 virtual bool IsStackValue() const { return false; }
698 // Returns true if we are branching on the value rather than materializing
699 // it. Only used for asserts.
700 virtual bool IsTest() const { return false; }
703 FullCodeGenerator* codegen() const { return codegen_; }
704 MacroAssembler* masm() const { return masm_; }
705 MacroAssembler* masm_;
708 const ExpressionContext* old_;
709 FullCodeGenerator* codegen_;
712 class AccumulatorValueContext : public ExpressionContext {
714 explicit AccumulatorValueContext(FullCodeGenerator* codegen)
715 : ExpressionContext(codegen) { }
717 virtual void Plug(bool flag) const;
718 virtual void Plug(Register reg) const;
719 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
720 virtual void Plug(Variable* var) const;
721 virtual void Plug(Handle<Object> lit) const;
722 virtual void Plug(Heap::RootListIndex) const;
723 virtual void PlugTOS() const;
724 virtual void DropAndPlug(int count, Register reg) const;
725 virtual void PrepareTest(Label* materialize_true,
726 Label* materialize_false,
729 Label** fall_through) const;
730 virtual bool IsAccumulatorValue() const { return true; }
733 class StackValueContext : public ExpressionContext {
735 explicit StackValueContext(FullCodeGenerator* codegen)
736 : ExpressionContext(codegen) { }
738 virtual void Plug(bool flag) const;
739 virtual void Plug(Register reg) const;
740 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
741 virtual void Plug(Variable* var) const;
742 virtual void Plug(Handle<Object> lit) const;
743 virtual void Plug(Heap::RootListIndex) const;
744 virtual void PlugTOS() const;
745 virtual void DropAndPlug(int count, Register reg) const;
746 virtual void PrepareTest(Label* materialize_true,
747 Label* materialize_false,
750 Label** fall_through) const;
751 virtual bool IsStackValue() const { return true; }
754 class TestContext : public ExpressionContext {
756 TestContext(FullCodeGenerator* codegen,
757 Expression* condition,
761 : ExpressionContext(codegen),
762 condition_(condition),
763 true_label_(true_label),
764 false_label_(false_label),
765 fall_through_(fall_through) { }
767 static const TestContext* cast(const ExpressionContext* context) {
768 DCHECK(context->IsTest());
769 return reinterpret_cast<const TestContext*>(context);
772 Expression* condition() const { return condition_; }
773 Label* true_label() const { return true_label_; }
774 Label* false_label() const { return false_label_; }
775 Label* fall_through() const { return fall_through_; }
777 virtual void Plug(bool flag) const;
778 virtual void Plug(Register reg) const;
779 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
780 virtual void Plug(Variable* var) const;
781 virtual void Plug(Handle<Object> lit) const;
782 virtual void Plug(Heap::RootListIndex) const;
783 virtual void PlugTOS() const;
784 virtual void DropAndPlug(int count, Register reg) const;
785 virtual void PrepareTest(Label* materialize_true,
786 Label* materialize_false,
789 Label** fall_through) const;
790 virtual bool IsTest() const { return true; }
793 Expression* condition_;
796 Label* fall_through_;
799 class EffectContext : public ExpressionContext {
801 explicit EffectContext(FullCodeGenerator* codegen)
802 : ExpressionContext(codegen) { }
804 virtual void Plug(bool flag) const;
805 virtual void Plug(Register reg) const;
806 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
807 virtual void Plug(Variable* var) const;
808 virtual void Plug(Handle<Object> lit) const;
809 virtual void Plug(Heap::RootListIndex) const;
810 virtual void PlugTOS() const;
811 virtual void DropAndPlug(int count, Register reg) const;
812 virtual void PrepareTest(Label* materialize_true,
813 Label* materialize_false,
816 Label** fall_through) const;
817 virtual bool IsEffect() const { return true; }
820 MacroAssembler* masm_;
821 CompilationInfo* info_;
824 NestedStatement* nesting_stack_;
826 ZoneList<Handle<Object> >* globals_;
827 Handle<FixedArray> modules_;
829 const ExpressionContext* context_;
830 ZoneList<BailoutEntry> bailout_entries_;
831 ZoneList<BackEdgeEntry> back_edges_;
833 Handle<FixedArray> handler_table_;
834 Handle<Cell> profiling_counter_;
835 bool generate_debug_code_;
837 friend class NestedStatement;
839 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
840 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
844 // A map from property names to getter/setter pairs allocated in the zone.
845 class AccessorTable: public TemplateHashMap<Literal,
846 ObjectLiteral::Accessors,
847 ZoneAllocationPolicy> {
849 explicit AccessorTable(Zone* zone) :
850 TemplateHashMap<Literal, ObjectLiteral::Accessors,
851 ZoneAllocationPolicy>(Literal::Match,
852 ZoneAllocationPolicy(zone)),
855 Iterator lookup(Literal* literal) {
856 Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
857 if (it->second == NULL) it->second = new(zone_) ObjectLiteral::Accessors();
866 class BackEdgeTable {
868 BackEdgeTable(Code* code, DisallowHeapAllocation* required) {
869 DCHECK(code->kind() == Code::FUNCTION);
870 instruction_start_ = code->instruction_start();
871 Address table_address = instruction_start_ + code->back_edge_table_offset();
872 length_ = Memory::uint32_at(table_address);
873 start_ = table_address + kTableLengthSize;
876 uint32_t length() { return length_; }
878 BailoutId ast_id(uint32_t index) {
879 return BailoutId(static_cast<int>(
880 Memory::uint32_at(entry_at(index) + kAstIdOffset)));
883 uint32_t loop_depth(uint32_t index) {
884 return Memory::uint32_at(entry_at(index) + kLoopDepthOffset);
887 uint32_t pc_offset(uint32_t index) {
888 return Memory::uint32_at(entry_at(index) + kPcOffsetOffset);
891 Address pc(uint32_t index) {
892 return instruction_start_ + pc_offset(index);
897 ON_STACK_REPLACEMENT,
898 OSR_AFTER_STACK_CHECK
901 // Increase allowed loop nesting level by one and patch those matching loops.
902 static void Patch(Isolate* isolate, Code* unoptimized_code);
904 // Patch the back edge to the target state, provided the correct callee.
905 static void PatchAt(Code* unoptimized_code,
907 BackEdgeState target_state,
908 Code* replacement_code);
910 // Change all patched back edges back to normal interrupts.
911 static void Revert(Isolate* isolate,
912 Code* unoptimized_code);
914 // Change a back edge patched for on-stack replacement to perform a
915 // stack check first.
916 static void AddStackCheck(Handle<Code> code, uint32_t pc_offset);
918 // Revert the patch by AddStackCheck.
919 static void RemoveStackCheck(Handle<Code> code, uint32_t pc_offset);
921 // Return the current patch state of the back edge.
922 static BackEdgeState GetBackEdgeState(Isolate* isolate,
923 Code* unoptimized_code,
927 // Verify that all back edges of a certain loop depth are patched.
928 static bool Verify(Isolate* isolate, Code* unoptimized_code);
932 Address entry_at(uint32_t index) {
933 DCHECK(index < length_);
934 return start_ + index * kEntrySize;
937 static const int kTableLengthSize = kIntSize;
938 static const int kAstIdOffset = 0 * kIntSize;
939 static const int kPcOffsetOffset = 1 * kIntSize;
940 static const int kLoopDepthOffset = 2 * kIntSize;
941 static const int kEntrySize = 3 * kIntSize;
944 Address instruction_start_;
949 } } // namespace v8::internal
951 #endif // V8_FULL_CODEGEN_H_