1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_INTERPRETER_BYTECODE_GENERATOR_H_ 6 #define V8_INTERPRETER_BYTECODE_GENERATOR_H_ 7 8 #include "src/ast/ast.h" 9 #include "src/interpreter/bytecode-array-builder.h" 10 #include "src/interpreter/bytecodes.h" 11 12 namespace v8 { 13 namespace internal { 14 namespace interpreter { 15 16 class BytecodeGenerator final : public AstVisitor { 17 public: 18 BytecodeGenerator(Isolate* isolate, Zone* zone); 19 20 Handle<BytecodeArray> MakeBytecode(CompilationInfo* info); 21 22 #define DECLARE_VISIT(type) void Visit##type(type* node) override; 23 AST_NODE_LIST(DECLARE_VISIT) 24 #undef DECLARE_VISIT 25 26 // Visiting function for declarations list and statements are overridden. 27 void VisitDeclarations(ZoneList<Declaration*>* declarations) override; 28 void VisitStatements(ZoneList<Statement*>* statments) override; 29 30 private: 31 class ContextScope; 32 class ControlScope; 33 class ControlScopeForBreakable; 34 class ControlScopeForIteration; 35 class ExpressionResultScope; 36 class EffectResultScope; 37 class AccumulatorResultScope; 38 class RegisterResultScope; 39 class RegisterAllocationScope; 40 41 void MakeBytecodeBody(); 42 Register NextContextRegister() const; 43 44 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 45 46 // Dispatched from VisitBinaryOperation. 47 void VisitArithmeticExpression(BinaryOperation* binop); 48 void VisitCommaExpression(BinaryOperation* binop); 49 void VisitLogicalOrExpression(BinaryOperation* binop); 50 void VisitLogicalAndExpression(BinaryOperation* binop); 51 52 // Dispatched from VisitUnaryOperation. 53 void VisitVoid(UnaryOperation* expr); 54 void VisitTypeOf(UnaryOperation* expr); 55 void VisitNot(UnaryOperation* expr); 56 void VisitDelete(UnaryOperation* expr); 57 58 // Used by flow control routines to evaluate loop condition. 59 void VisitCondition(Expression* expr); 60 61 // Helper visitors which perform common operations. 62 Register VisitArguments(ZoneList<Expression*>* arguments); 63 64 void VisitPropertyLoad(Register obj, Property* expr); 65 void VisitPropertyLoadForAccumulator(Register obj, Property* expr); 66 67 void VisitVariableLoad(Variable* variable, FeedbackVectorSlot slot, 68 TypeofMode typeof_mode = NOT_INSIDE_TYPEOF); 69 void VisitVariableLoadForAccumulatorValue( 70 Variable* variable, FeedbackVectorSlot slot, 71 TypeofMode typeof_mode = NOT_INSIDE_TYPEOF); 72 MUST_USE_RESULT Register 73 VisitVariableLoadForRegisterValue(Variable* variable, FeedbackVectorSlot slot, 74 TypeofMode typeof_mode = NOT_INSIDE_TYPEOF); 75 void VisitVariableAssignment(Variable* variable, FeedbackVectorSlot slot); 76 77 void VisitArgumentsObject(Variable* variable); 78 void VisitThisFunctionVariable(Variable* variable); 79 void VisitNewTargetVariable(Variable* variable); 80 void VisitNewLocalFunctionContext(); 81 void VisitBuildLocalActivationContext(); 82 void VisitNewLocalBlockContext(Scope* scope); 83 void VisitFunctionClosureForContext(); 84 void VisitSetHomeObject(Register value, Register home_object, 85 ObjectLiteralProperty* property, int slot_number = 0); 86 void VisitObjectLiteralAccessor(Register home_object, 87 ObjectLiteralProperty* property, 88 Register value_out); 89 void VisitForInAssignment(Expression* expr, FeedbackVectorSlot slot); 90 91 // Visitors for obtaining expression result in the accumulator, in a 92 // register, or just getting the effect. 93 void VisitForAccumulatorValue(Expression* expression); 94 MUST_USE_RESULT Register VisitForRegisterValue(Expression* expression); 95 void VisitForEffect(Expression* node); 96 97 // Methods for tracking and remapping register. 98 void RecordStoreToRegister(Register reg); 99 Register LoadFromAliasedRegister(Register reg); 100 101 inline BytecodeArrayBuilder* builder() { return &builder_; } 102 103 inline Isolate* isolate() const { return isolate_; } 104 inline Zone* zone() const { return zone_; } 105 106 inline Scope* scope() const { return scope_; } 107 inline void set_scope(Scope* scope) { scope_ = scope; } 108 inline CompilationInfo* info() const { return info_; } 109 inline void set_info(CompilationInfo* info) { info_ = info; } 110 111 inline ControlScope* execution_control() const { return execution_control_; } 112 inline void set_execution_control(ControlScope* scope) { 113 execution_control_ = scope; 114 } 115 inline ContextScope* execution_context() const { return execution_context_; } 116 inline void set_execution_context(ContextScope* context) { 117 execution_context_ = context; 118 } 119 inline void set_execution_result(ExpressionResultScope* execution_result) { 120 execution_result_ = execution_result; 121 } 122 ExpressionResultScope* execution_result() const { return execution_result_; } 123 inline void set_register_allocator( 124 RegisterAllocationScope* register_allocator) { 125 register_allocator_ = register_allocator; 126 } 127 RegisterAllocationScope* register_allocator() const { 128 return register_allocator_; 129 } 130 131 ZoneVector<Handle<Object>>* globals() { return &globals_; } 132 inline LanguageMode language_mode() const; 133 Strength language_mode_strength() const; 134 int feedback_index(FeedbackVectorSlot slot) const; 135 136 Isolate* isolate_; 137 Zone* zone_; 138 BytecodeArrayBuilder builder_; 139 CompilationInfo* info_; 140 Scope* scope_; 141 ZoneVector<Handle<Object>> globals_; 142 ControlScope* execution_control_; 143 ContextScope* execution_context_; 144 ExpressionResultScope* execution_result_; 145 RegisterAllocationScope* register_allocator_; 146 }; 147 148 } // namespace interpreter 149 } // namespace internal 150 } // namespace v8 151 152 #endif // V8_INTERPRETER_BYTECODE_GENERATOR_H_ 153