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
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.
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.
28 #ifndef V8_FULL_CODEGEN_H_
29 #define V8_FULL_CODEGEN_H_
33 #include "allocation.h"
34 #include "assert-scope.h"
36 #include "code-stubs.h"
39 #include "data-flow.h"
46 // Forward declarations.
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 {
55 explicit BreakableStatementChecker(Zone* zone) : is_breakable_(false) {
56 InitializeAstVisitor(zone);
59 void Check(Statement* stmt);
60 void Check(Expression* stmt);
62 bool is_breakable() { return is_breakable_; }
65 // AST node visit functions.
66 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
67 AST_NODE_LIST(DECLARE_VISIT)
72 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
73 DISALLOW_COPY_AND_ASSIGN(BreakableStatementChecker);
77 // -----------------------------------------------------------------------------
78 // Full code generator.
80 class FullCodeGenerator: public AstVisitor {
87 FullCodeGenerator(MacroAssembler* masm, CompilationInfo* info)
90 scope_(info->scope()),
95 bailout_entries_(info->HasDeoptimizationSupport()
96 ? info->function()->ast_node_count() : 0,
98 back_edges_(2, info->zone()),
105 static bool MakeCode(CompilationInfo* info);
107 // Encode state and pc-offset as a BitField<type, start, size>.
108 // Only use 30 bits because we encode the result as a smi.
109 class StateField : public BitField<State, 0, 1> { };
110 class PcField : public BitField<unsigned, 1, 30-1> { };
112 static const char* State2String(State state) {
114 case NO_REGISTERS: return "NO_REGISTERS";
115 case TOS_REG: return "TOS_REG";
121 static const int kMaxBackEdgeWeight = 127;
123 // Platform-specific code size multiplier.
124 #if V8_TARGET_ARCH_IA32
125 static const int kCodeSizeMultiplier = 100;
126 #elif V8_TARGET_ARCH_X64
127 static const int kCodeSizeMultiplier = 162;
128 #elif V8_TARGET_ARCH_ARM
129 static const int kCodeSizeMultiplier = 142;
130 #elif V8_TARGET_ARCH_ARM64
131 // TODO(all): Copied ARM value. Check this is sensible for ARM64.
132 static const int kCodeSizeMultiplier = 142;
133 #elif V8_TARGET_ARCH_MIPS
134 static const int kCodeSizeMultiplier = 142;
136 #error Unsupported target architecture.
145 class NestedStatement BASE_EMBEDDED {
147 explicit NestedStatement(FullCodeGenerator* codegen) : codegen_(codegen) {
148 // Link into codegen's nesting stack.
149 previous_ = codegen->nesting_stack_;
150 codegen->nesting_stack_ = this;
152 virtual ~NestedStatement() {
153 // Unlink from codegen's nesting stack.
154 ASSERT_EQ(this, codegen_->nesting_stack_);
155 codegen_->nesting_stack_ = previous_;
158 virtual Breakable* AsBreakable() { return NULL; }
159 virtual Iteration* AsIteration() { return NULL; }
161 virtual bool IsContinueTarget(Statement* target) { return false; }
162 virtual bool IsBreakTarget(Statement* target) { return false; }
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) {
175 MacroAssembler* masm() { return codegen_->masm(); }
177 FullCodeGenerator* codegen_;
178 NestedStatement* previous_;
181 DISALLOW_COPY_AND_ASSIGN(NestedStatement);
184 // A breakable statement such as a block.
185 class Breakable : public NestedStatement {
187 Breakable(FullCodeGenerator* codegen, BreakableStatement* statement)
188 : NestedStatement(codegen), statement_(statement) {
190 virtual ~Breakable() {}
192 virtual Breakable* AsBreakable() { return this; }
193 virtual bool IsBreakTarget(Statement* target) {
194 return statement() == target;
197 BreakableStatement* statement() { return statement_; }
198 Label* break_label() { return &break_label_; }
201 BreakableStatement* statement_;
205 // An iteration statement such as a while, for, or do loop.
206 class Iteration : public Breakable {
208 Iteration(FullCodeGenerator* codegen, IterationStatement* statement)
209 : Breakable(codegen, statement) {
211 virtual ~Iteration() {}
213 virtual Iteration* AsIteration() { return this; }
214 virtual bool IsContinueTarget(Statement* target) {
215 return statement() == target;
218 Label* continue_label() { return &continue_label_; }
221 Label continue_label_;
224 // A nested block statement.
225 class NestedBlock : public Breakable {
227 NestedBlock(FullCodeGenerator* codegen, Block* block)
228 : Breakable(codegen, block) {
230 virtual ~NestedBlock() {}
232 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
233 if (statement()->AsBlock()->scope() != NULL) {
240 // The try block of a try/catch statement.
241 class TryCatch : public NestedStatement {
243 explicit TryCatch(FullCodeGenerator* codegen) : NestedStatement(codegen) {
245 virtual ~TryCatch() {}
247 virtual NestedStatement* Exit(int* stack_depth, int* context_length);
250 // The try block of a try/finally statement.
251 class TryFinally : public NestedStatement {
253 TryFinally(FullCodeGenerator* codegen, Label* finally_entry)
254 : NestedStatement(codegen), finally_entry_(finally_entry) {
256 virtual ~TryFinally() {}
258 virtual NestedStatement* Exit(int* stack_depth, int* context_length);
261 Label* finally_entry_;
264 // The finally block of a try/finally statement.
265 class Finally : public NestedStatement {
267 static const int kElementCount = 5;
269 explicit Finally(FullCodeGenerator* codegen) : NestedStatement(codegen) { }
270 virtual ~Finally() {}
272 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
273 *stack_depth += kElementCount;
278 // The body of a for/in loop.
279 class ForIn : public Iteration {
281 static const int kElementCount = 5;
283 ForIn(FullCodeGenerator* codegen, ForInStatement* statement)
284 : Iteration(codegen, statement) {
288 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
289 *stack_depth += kElementCount;
295 // The body of a with or catch.
296 class WithOrCatch : public NestedStatement {
298 explicit WithOrCatch(FullCodeGenerator* codegen)
299 : NestedStatement(codegen) {
301 virtual ~WithOrCatch() {}
303 virtual NestedStatement* Exit(int* stack_depth, int* context_length) {
309 // Type of a member function that generates inline code for a native function.
310 typedef void (FullCodeGenerator::*InlineFunctionGenerator)(CallRuntime* expr);
312 static const InlineFunctionGenerator kInlineFunctionGenerators[];
314 // A platform-specific utility to overwrite the accumulator register
315 // with a GC-safe value.
316 void ClearAccumulator();
318 // Determine whether or not to inline the smi case for the given
320 bool ShouldInlineSmiCase(Token::Value op);
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,
328 Label* fall_through);
329 void DoTest(const TestContext* context);
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,
339 Label* fall_through);
340 #else // All non-mips arch.
341 void Split(Condition cc,
344 Label* fall_through);
345 #endif // V8_TARGET_ARCH_MIPS
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);
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,
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);
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);
371 void VisitForEffect(Expression* expr) {
372 EffectContext context(this);
374 PrepareForBailout(expr, NO_REGISTERS);
377 void VisitForAccumulatorValue(Expression* expr) {
378 AccumulatorValueContext context(this);
380 PrepareForBailout(expr, TOS_REG);
383 void VisitForStackValue(Expression* expr) {
384 StackValueContext context(this);
386 PrepareForBailout(expr, NO_REGISTERS);
389 void VisitForControl(Expression* expr,
392 Label* fall_through) {
393 TestContext context(this, expr, if_true, if_false, fall_through);
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
400 void VisitInDuplicateContext(Expression* expr);
402 void VisitDeclarations(ZoneList<Declaration*>* declarations);
403 void DeclareModules(Handle<FixedArray> descriptions);
404 void DeclareGlobals(Handle<FixedArray> pairs);
405 int DeclareGlobalsFlags();
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);
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);
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);
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);
428 // Platform-specific code for equality comparison with a nil-like value.
429 void EmitLiteralCompareNil(CompareOperation* expr,
430 Expression* sub_expr,
434 void PrepareForBailout(Expression* node, State state);
435 void PrepareForBailoutForId(BailoutId id, State state);
437 // Feedback slot support. The feedback vector will be cleared during gc and
438 // collected by the type-feedback oracle.
439 Handle<FixedArray> FeedbackVector() {
440 return feedback_vector_;
442 void StoreFeedbackVectorSlot(int slot, Handle<Object> object) {
443 feedback_vector_->set(slot, *object);
445 void InitializeFeedbackVector();
447 // Record a call's return site offset, used to rebuild the frame if the
448 // called function was inlined at the site.
449 void RecordJSReturnSite(Call* call);
451 // Prepare for bailout before a test (or compare) and branch. If
452 // should_normalize, then the following comparison will not handle the
453 // canonical JS true value so we will insert a (dead) test against true at
454 // the actual bailout target from the optimized code. If not
455 // should_normalize, the true and false labels are ignored.
456 void PrepareForBailoutBeforeSplit(Expression* expr,
457 bool should_normalize,
461 // If enabled, emit debug code for checking that the current context is
462 // neither a with nor a catch context.
463 void EmitDebugCheckDeclarationContext(Variable* variable);
465 // This is meant to be called at loop back edges, |back_edge_target| is
466 // the jump target of the back edge and is used to approximate the amount
467 // of code inside the loop.
468 void EmitBackEdgeBookkeeping(IterationStatement* stmt,
469 Label* back_edge_target);
470 // Record the OSR AST id corresponding to a back edge in the code.
471 void RecordBackEdge(BailoutId osr_ast_id);
472 // Emit a table of back edge ids, pcs and loop depths into the code stream.
473 // Return the offset of the start of the table.
474 unsigned EmitBackEdgeTable();
476 void EmitProfilingCounterDecrement(int delta);
477 void EmitProfilingCounterReset();
479 // Emit code to pop values from the stack associated with nested statements
480 // like try/catch, try/finally, etc, running the finallies and unwinding the
481 // handlers as needed.
482 void EmitUnwindBeforeReturn();
484 // Platform-specific return sequence
485 void EmitReturnSequence();
487 // Platform-specific code sequences for calls
488 void EmitCallWithStub(Call* expr);
489 void EmitCallWithIC(Call* expr);
490 void EmitKeyedCallWithIC(Call* expr, Expression* key);
492 // Platform-specific code for inline runtime calls.
493 InlineFunctionGenerator FindInlineFunctionGenerator(Runtime::FunctionId id);
495 void EmitInlineRuntimeCall(CallRuntime* expr);
497 #define EMIT_INLINE_RUNTIME_CALL(name, x, y) \
498 void Emit##name(CallRuntime* expr);
499 INLINE_FUNCTION_LIST(EMIT_INLINE_RUNTIME_CALL)
500 #undef EMIT_INLINE_RUNTIME_CALL
502 // Platform-specific code for resuming generators.
503 void EmitGeneratorResume(Expression *generator,
505 JSGeneratorObject::ResumeMode resume_mode);
507 // Platform-specific code for loading variables.
508 void EmitLoadGlobalCheckExtensions(Variable* var,
509 TypeofState typeof_state,
511 MemOperand ContextSlotOperandCheckExtensions(Variable* var, Label* slow);
512 void EmitDynamicLookupFastCase(Variable* var,
513 TypeofState typeof_state,
516 void EmitVariableLoad(VariableProxy* proxy);
518 void EmitAccessor(Expression* expression);
520 // Expects the arguments and the function already pushed.
521 void EmitResolvePossiblyDirectEval(int arg_count);
523 // Platform-specific support for allocating a new closure based on
524 // the given function info.
525 void EmitNewClosure(Handle<SharedFunctionInfo> info, bool pretenure);
527 // Platform-specific support for compiling assignments.
529 // Load a value from a named property.
530 // The receiver is left on the stack by the IC.
531 void EmitNamedPropertyLoad(Property* expr);
533 // Load a value from a keyed property.
534 // The receiver and the key is left on the stack by the IC.
535 void EmitKeyedPropertyLoad(Property* expr);
537 // Apply the compound assignment operator. Expects the left operand on top
538 // of the stack and the right one in the accumulator.
539 void EmitBinaryOp(BinaryOperation* expr,
543 // Helper functions for generating inlined smi code for certain
544 // binary operations.
545 void EmitInlineSmiBinaryOp(BinaryOperation* expr,
551 // Assign to the given expression as if via '='. The right-hand-side value
552 // is expected in the accumulator.
553 void EmitAssignment(Expression* expr);
555 // Complete a variable assignment. The right-hand-side value is expected
556 // in the accumulator.
557 void EmitVariableAssignment(Variable* var,
560 // Helper functions to EmitVariableAssignment
561 void EmitStoreToStackLocalOrContextSlot(Variable* var,
562 MemOperand location);
563 void EmitCallStoreContextSlot(Handle<String> name, StrictMode strict_mode);
565 // Complete a named property assignment. The receiver is expected on top
566 // of the stack and the right-hand-side value in the accumulator.
567 void EmitNamedPropertyAssignment(Assignment* expr);
569 // Complete a keyed property assignment. The receiver and key are
570 // expected on top of the stack and the right-hand-side value in the
572 void EmitKeyedPropertyAssignment(Assignment* expr);
574 void CallIC(Handle<Code> code,
575 TypeFeedbackId id = TypeFeedbackId::None());
577 void CallLoadIC(ContextualMode mode,
578 TypeFeedbackId id = TypeFeedbackId::None());
579 void CallStoreIC(TypeFeedbackId id = TypeFeedbackId::None());
581 void SetFunctionPosition(FunctionLiteral* fun);
582 void SetReturnPosition(FunctionLiteral* fun);
583 void SetStatementPosition(Statement* stmt);
584 void SetExpressionPosition(Expression* expr);
585 void SetStatementPosition(int pos);
586 void SetSourcePosition(int pos);
588 // Non-local control flow support.
589 void EnterFinallyBlock();
590 void ExitFinallyBlock();
592 // Loop nesting counter.
593 int loop_depth() { return loop_depth_; }
594 void increment_loop_depth() { loop_depth_++; }
595 void decrement_loop_depth() {
596 ASSERT(loop_depth_ > 0);
600 MacroAssembler* masm() { return masm_; }
602 class ExpressionContext;
603 const ExpressionContext* context() { return context_; }
604 void set_new_context(const ExpressionContext* context) { context_ = context; }
606 Handle<Script> script() { return info_->script(); }
607 bool is_eval() { return info_->is_eval(); }
608 bool is_native() { return info_->is_native(); }
609 StrictMode strict_mode() { return function()->strict_mode(); }
610 FunctionLiteral* function() { return info_->function(); }
611 Scope* scope() { return scope_; }
613 static Register result_register();
614 static Register context_register();
616 // Set fields in the stack frame. Offsets are the frame pointer relative
617 // offsets defined in, e.g., StandardFrameConstants.
618 void StoreToFrameField(int frame_offset, Register value);
620 // Load a value from the current context. Indices are defined as an enum
621 // in v8::internal::Context.
622 void LoadContextField(Register dst, int context_index);
624 // Push the function argument for the runtime functions PushWithContext
625 // and PushCatchContext.
626 void PushFunctionArgumentForContextAllocation();
628 // AST node visit functions.
629 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
630 AST_NODE_LIST(DECLARE_VISIT)
633 void VisitComma(BinaryOperation* expr);
634 void VisitLogicalExpression(BinaryOperation* expr);
635 void VisitArithmeticExpression(BinaryOperation* expr);
637 void VisitForTypeofValue(Expression* expr);
640 void PopulateDeoptimizationData(Handle<Code> code);
641 void PopulateTypeFeedbackInfo(Handle<Code> code);
643 Handle<FixedArray> handler_table() { return handler_table_; }
645 struct BailoutEntry {
647 unsigned pc_and_state;
650 struct BackEdgeEntry {
656 class ExpressionContext BASE_EMBEDDED {
658 explicit ExpressionContext(FullCodeGenerator* codegen)
659 : masm_(codegen->masm()), old_(codegen->context()), codegen_(codegen) {
660 codegen->set_new_context(this);
663 virtual ~ExpressionContext() {
664 codegen_->set_new_context(old_);
667 Isolate* isolate() const { return codegen_->isolate(); }
669 // Convert constant control flow (true or false) to the result expected for
670 // this expression context.
671 virtual void Plug(bool flag) const = 0;
673 // Emit code to convert a pure value (in a register, known variable
674 // location, as a literal, or on top of the stack) into the result
675 // expected according to this expression context.
676 virtual void Plug(Register reg) const = 0;
677 virtual void Plug(Variable* var) const = 0;
678 virtual void Plug(Handle<Object> lit) const = 0;
679 virtual void Plug(Heap::RootListIndex index) const = 0;
680 virtual void PlugTOS() const = 0;
682 // Emit code to convert pure control flow to a pair of unbound labels into
683 // the result expected according to this expression context. The
684 // implementation will bind both labels unless it's a TestContext, which
685 // won't bind them at this point.
686 virtual void Plug(Label* materialize_true,
687 Label* materialize_false) const = 0;
689 // Emit code to discard count elements from the top of stack, then convert
690 // a pure value into the result expected according to this expression
692 virtual void DropAndPlug(int count, Register reg) const = 0;
694 // Set up branch labels for a test expression. The three Label** parameters
695 // are output parameters.
696 virtual void PrepareTest(Label* materialize_true,
697 Label* materialize_false,
700 Label** fall_through) const = 0;
702 // Returns true if we are evaluating only for side effects (i.e. if the
703 // result will be discarded).
704 virtual bool IsEffect() const { return false; }
706 // Returns true if we are evaluating for the value (in accu/on stack).
707 virtual bool IsAccumulatorValue() const { return false; }
708 virtual bool IsStackValue() const { return false; }
710 // Returns true if we are branching on the value rather than materializing
711 // it. Only used for asserts.
712 virtual bool IsTest() const { return false; }
715 FullCodeGenerator* codegen() const { return codegen_; }
716 MacroAssembler* masm() const { return masm_; }
717 MacroAssembler* masm_;
720 const ExpressionContext* old_;
721 FullCodeGenerator* codegen_;
724 class AccumulatorValueContext : public ExpressionContext {
726 explicit AccumulatorValueContext(FullCodeGenerator* codegen)
727 : ExpressionContext(codegen) { }
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,
741 Label** fall_through) const;
742 virtual bool IsAccumulatorValue() const { return true; }
745 class StackValueContext : public ExpressionContext {
747 explicit StackValueContext(FullCodeGenerator* codegen)
748 : ExpressionContext(codegen) { }
750 virtual void Plug(bool flag) const;
751 virtual void Plug(Register reg) const;
752 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
753 virtual void Plug(Variable* var) const;
754 virtual void Plug(Handle<Object> lit) const;
755 virtual void Plug(Heap::RootListIndex) const;
756 virtual void PlugTOS() const;
757 virtual void DropAndPlug(int count, Register reg) const;
758 virtual void PrepareTest(Label* materialize_true,
759 Label* materialize_false,
762 Label** fall_through) const;
763 virtual bool IsStackValue() const { return true; }
766 class TestContext : public ExpressionContext {
768 TestContext(FullCodeGenerator* codegen,
769 Expression* condition,
773 : ExpressionContext(codegen),
774 condition_(condition),
775 true_label_(true_label),
776 false_label_(false_label),
777 fall_through_(fall_through) { }
779 static const TestContext* cast(const ExpressionContext* context) {
780 ASSERT(context->IsTest());
781 return reinterpret_cast<const TestContext*>(context);
784 Expression* condition() const { return condition_; }
785 Label* true_label() const { return true_label_; }
786 Label* false_label() const { return false_label_; }
787 Label* fall_through() const { return fall_through_; }
789 virtual void Plug(bool flag) const;
790 virtual void Plug(Register reg) const;
791 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
792 virtual void Plug(Variable* var) const;
793 virtual void Plug(Handle<Object> lit) const;
794 virtual void Plug(Heap::RootListIndex) const;
795 virtual void PlugTOS() const;
796 virtual void DropAndPlug(int count, Register reg) const;
797 virtual void PrepareTest(Label* materialize_true,
798 Label* materialize_false,
801 Label** fall_through) const;
802 virtual bool IsTest() const { return true; }
805 Expression* condition_;
808 Label* fall_through_;
811 class EffectContext : public ExpressionContext {
813 explicit EffectContext(FullCodeGenerator* codegen)
814 : ExpressionContext(codegen) { }
816 virtual void Plug(bool flag) const;
817 virtual void Plug(Register reg) const;
818 virtual void Plug(Label* materialize_true, Label* materialize_false) const;
819 virtual void Plug(Variable* var) const;
820 virtual void Plug(Handle<Object> lit) const;
821 virtual void Plug(Heap::RootListIndex) const;
822 virtual void PlugTOS() const;
823 virtual void DropAndPlug(int count, Register reg) const;
824 virtual void PrepareTest(Label* materialize_true,
825 Label* materialize_false,
828 Label** fall_through) const;
829 virtual bool IsEffect() const { return true; }
832 MacroAssembler* masm_;
833 CompilationInfo* info_;
836 NestedStatement* nesting_stack_;
838 ZoneList<Handle<Object> >* globals_;
839 Handle<FixedArray> modules_;
841 const ExpressionContext* context_;
842 ZoneList<BailoutEntry> bailout_entries_;
843 GrowableBitVector prepared_bailout_ids_;
844 ZoneList<BackEdgeEntry> back_edges_;
846 Handle<FixedArray> handler_table_;
847 Handle<FixedArray> feedback_vector_;
848 Handle<Cell> profiling_counter_;
849 bool generate_debug_code_;
851 friend class NestedStatement;
853 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
854 DISALLOW_COPY_AND_ASSIGN(FullCodeGenerator);
858 // A map from property names to getter/setter pairs allocated in the zone.
859 class AccessorTable: public TemplateHashMap<Literal,
860 ObjectLiteral::Accessors,
861 ZoneAllocationPolicy> {
863 explicit AccessorTable(Zone* zone) :
864 TemplateHashMap<Literal, ObjectLiteral::Accessors,
865 ZoneAllocationPolicy>(Literal::Match,
866 ZoneAllocationPolicy(zone)),
869 Iterator lookup(Literal* literal) {
870 Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
871 if (it->second == NULL) it->second = new(zone_) ObjectLiteral::Accessors();
880 class BackEdgeTable {
882 BackEdgeTable(Code* code, DisallowHeapAllocation* required) {
883 ASSERT(code->kind() == Code::FUNCTION);
884 instruction_start_ = code->instruction_start();
885 Address table_address = instruction_start_ + code->back_edge_table_offset();
886 length_ = Memory::uint32_at(table_address);
887 start_ = table_address + kTableLengthSize;
890 uint32_t length() { return length_; }
892 BailoutId ast_id(uint32_t index) {
893 return BailoutId(static_cast<int>(
894 Memory::uint32_at(entry_at(index) + kAstIdOffset)));
897 uint32_t loop_depth(uint32_t index) {
898 return Memory::uint32_at(entry_at(index) + kLoopDepthOffset);
901 uint32_t pc_offset(uint32_t index) {
902 return Memory::uint32_at(entry_at(index) + kPcOffsetOffset);
905 Address pc(uint32_t index) {
906 return instruction_start_ + pc_offset(index);
911 ON_STACK_REPLACEMENT,
912 OSR_AFTER_STACK_CHECK
915 // Patch all interrupts with allowed loop depth in the unoptimized code to
916 // unconditionally call replacement_code.
917 static void Patch(Isolate* isolate,
918 Code* unoptimized_code);
920 // Patch the back edge to the target state, provided the correct callee.
921 static void PatchAt(Code* unoptimized_code,
923 BackEdgeState target_state,
924 Code* replacement_code);
926 // Change all patched back edges back to normal interrupts.
927 static void Revert(Isolate* isolate,
928 Code* unoptimized_code);
930 // Change a back edge patched for on-stack replacement to perform a
931 // stack check first.
932 static void AddStackCheck(Handle<Code> code, uint32_t pc_offset);
934 // Revert the patch by AddStackCheck.
935 static void RemoveStackCheck(Handle<Code> code, uint32_t pc_offset);
937 // Return the current patch state of the back edge.
938 static BackEdgeState GetBackEdgeState(Isolate* isolate,
939 Code* unoptimized_code,
943 // Verify that all back edges of a certain loop depth are patched.
944 static bool Verify(Isolate* isolate,
945 Code* unoptimized_code,
946 int loop_nesting_level);
950 Address entry_at(uint32_t index) {
951 ASSERT(index < length_);
952 return start_ + index * kEntrySize;
955 static const int kTableLengthSize = kIntSize;
956 static const int kAstIdOffset = 0 * kIntSize;
957 static const int kPcOffsetOffset = 1 * kIntSize;
958 static const int kLoopDepthOffset = 2 * kIntSize;
959 static const int kEntrySize = 3 * kIntSize;
962 Address instruction_start_;
967 } } // namespace v8::internal
969 #endif // V8_FULL_CODEGEN_H_