| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| 6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "src/ast.h" | 10 #include "src/ast.h" |
| 11 #include "src/identity-map.h" | 11 #include "src/identity-map.h" |
| 12 #include "src/interpreter/bytecodes.h" | 12 #include "src/interpreter/bytecodes.h" |
| 13 #include "src/zone.h" | 13 #include "src/zone.h" |
| 14 #include "src/zone-containers.h" | 14 #include "src/zone-containers.h" |
| 15 | 15 |
| 16 namespace v8 { | 16 namespace v8 { |
| 17 namespace internal { | 17 namespace internal { |
| 18 | 18 |
| 19 class Isolate; | 19 class Isolate; |
| 20 | 20 |
| 21 namespace interpreter { | 21 namespace interpreter { |
| 22 | 22 |
| 23 class BytecodeLabel; |
| 23 class Register; | 24 class Register; |
| 24 | 25 |
| 25 class BytecodeArrayBuilder { | 26 class BytecodeArrayBuilder { |
| 26 public: | 27 public: |
| 27 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); | 28 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); |
| 28 Handle<BytecodeArray> ToBytecodeArray(); | 29 Handle<BytecodeArray> ToBytecodeArray(); |
| 29 | 30 |
| 30 // Set number of parameters expected by function. | 31 // Set number of parameters expected by function. |
| 31 void set_parameter_count(int number_of_params); | 32 void set_parameter_count(int number_of_params); |
| 32 int parameter_count() const; | 33 int parameter_count() const; |
| 33 | 34 |
| 34 // Set number of locals required for bytecode array. | 35 // Set number of locals required for bytecode array. |
| 35 void set_locals_count(int number_of_locals); | 36 void set_locals_count(int number_of_locals); |
| 36 int locals_count() const; | 37 int locals_count() const; |
| 37 | 38 |
| 38 // Returns true if the bytecode has an explicit return at the end. | |
| 39 bool HasExplicitReturn(); | |
| 40 | |
| 41 Register Parameter(int parameter_index); | 39 Register Parameter(int parameter_index); |
| 42 | 40 |
| 43 // Constant loads to accumulator. | 41 // Constant loads to accumulator. |
| 44 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); | 42 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); |
| 45 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); | 43 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); |
| 46 BytecodeArrayBuilder& LoadUndefined(); | 44 BytecodeArrayBuilder& LoadUndefined(); |
| 47 BytecodeArrayBuilder& LoadNull(); | 45 BytecodeArrayBuilder& LoadNull(); |
| 48 BytecodeArrayBuilder& LoadTheHole(); | 46 BytecodeArrayBuilder& LoadTheHole(); |
| 49 BytecodeArrayBuilder& LoadTrue(); | 47 BytecodeArrayBuilder& LoadTrue(); |
| 50 BytecodeArrayBuilder& LoadFalse(); | 48 BytecodeArrayBuilder& LoadFalse(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 70 int feedback_slot, | 68 int feedback_slot, |
| 71 LanguageMode language_mode); | 69 LanguageMode language_mode); |
| 72 | 70 |
| 73 // Call a JS function. The JSFunction or Callable to be called should be in | 71 // Call a JS function. The JSFunction or Callable to be called should be in |
| 74 // |callable|, the receiver should be in |receiver| and all subsequent | 72 // |callable|, the receiver should be in |receiver| and all subsequent |
| 75 // arguments should be in registers <receiver + 1> to | 73 // arguments should be in registers <receiver + 1> to |
| 76 // <receiver + 1 + arg_count>. | 74 // <receiver + 1 + arg_count>. |
| 77 BytecodeArrayBuilder& Call(Register callable, Register receiver, | 75 BytecodeArrayBuilder& Call(Register callable, Register receiver, |
| 78 size_t arg_count); | 76 size_t arg_count); |
| 79 | 77 |
| 80 // Operators. | 78 // Operators (register == lhs, accumulator = rhs). |
| 81 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); | 79 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); |
| 82 | 80 |
| 81 // Tests. |
| 82 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg, |
| 83 LanguageMode language_mode); |
| 84 |
| 85 // Casts |
| 86 BytecodeArrayBuilder& CastAccumulatorToBoolean(); |
| 87 |
| 83 // Flow Control. | 88 // Flow Control. |
| 89 BytecodeArrayBuilder& Bind(BytecodeLabel* label); |
| 90 BytecodeArrayBuilder& Jump(BytecodeLabel* label); |
| 91 BytecodeArrayBuilder& JumpIfTrue(BytecodeLabel* label); |
| 92 BytecodeArrayBuilder& JumpIfFalse(BytecodeLabel* label); |
| 84 BytecodeArrayBuilder& Return(); | 93 BytecodeArrayBuilder& Return(); |
| 85 | 94 |
| 95 BytecodeArrayBuilder& EnterBlock(); |
| 96 BytecodeArrayBuilder& LeaveBlock(); |
| 97 |
| 86 private: | 98 private: |
| 99 ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; } |
| 100 const ZoneVector<uint8_t>* bytecodes() const { return &bytecodes_; } |
| 101 Isolate* isolate() const { return isolate_; } |
| 102 |
| 87 static Bytecode BytecodeForBinaryOperation(Token::Value op); | 103 static Bytecode BytecodeForBinaryOperation(Token::Value op); |
| 88 static bool FitsInByteOperand(int value); | 104 static Bytecode BytecodeForCompareOperation(Token::Value op); |
| 89 static bool FitsInByteOperand(size_t value); | 105 static bool FitsInIdxOperand(int value); |
| 106 static bool FitsInIdxOperand(size_t value); |
| 107 static bool FitsInImm8Operand(int value); |
| 108 static bool IsJumpWithImm8Operand(Bytecode jump_bytecode); |
| 109 static Bytecode GetJumpWithConstantOperand(Bytecode jump_with_smi8_operand); |
| 90 | 110 |
| 91 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); | 111 template <size_t N> |
| 92 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); | 112 INLINE(void Output(uint8_t(&bytes)[N])); |
| 93 void Output(Bytecode bytecode, uint8_t r0); | 113 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1, |
| 114 uint8_t operand2); |
| 115 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1); |
| 116 void Output(Bytecode bytecode, uint8_t operand0); |
| 94 void Output(Bytecode bytecode); | 117 void Output(Bytecode bytecode); |
| 118 void PatchJump(const ZoneVector<uint8_t>::iterator& jump_target, |
| 119 ZoneVector<uint8_t>::iterator jump_location); |
| 120 BytecodeArrayBuilder& OutputJump(Bytecode jump_bytecode, |
| 121 BytecodeLabel* label); |
| 122 |
| 123 void EnsureReturn(); |
| 95 | 124 |
| 96 bool OperandIsValid(Bytecode bytecode, int operand_index, | 125 bool OperandIsValid(Bytecode bytecode, int operand_index, |
| 97 uint8_t operand_value) const; | 126 uint8_t operand_value) const; |
| 127 bool LastBytecodeInSameBlock() const; |
| 98 | 128 |
| 99 size_t GetConstantPoolEntry(Handle<Object> object); | 129 size_t GetConstantPoolEntry(Handle<Object> object); |
| 100 | 130 |
| 131 // Scope helpers used by TemporaryRegisterScope |
| 101 int BorrowTemporaryRegister(); | 132 int BorrowTemporaryRegister(); |
| 102 void ReturnTemporaryRegister(int reg_index); | 133 void ReturnTemporaryRegister(int reg_index); |
| 103 | 134 |
| 104 Isolate* isolate_; | 135 Isolate* isolate_; |
| 105 ZoneVector<uint8_t> bytecodes_; | 136 ZoneVector<uint8_t> bytecodes_; |
| 106 bool bytecode_generated_; | 137 bool bytecode_generated_; |
| 138 size_t last_block_end_; |
| 139 size_t last_bytecode_start_; |
| 140 bool return_seen_in_block_; |
| 107 | 141 |
| 108 IdentityMap<size_t> constants_map_; | 142 IdentityMap<size_t> constants_map_; |
| 109 ZoneVector<Handle<Object>> constants_; | 143 ZoneVector<Handle<Object>> constants_; |
| 110 | 144 |
| 111 int parameter_count_; | 145 int parameter_count_; |
| 112 int local_register_count_; | 146 int local_register_count_; |
| 113 int temporary_register_count_; | 147 int temporary_register_count_; |
| 114 int temporary_register_next_; | 148 int temporary_register_next_; |
| 115 | 149 |
| 116 friend class TemporaryRegisterScope; | 150 friend class TemporaryRegisterScope; |
| 117 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); | 151 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); |
| 118 }; | 152 }; |
| 119 | 153 |
| 154 |
| 155 // A label representing a branch target in a bytecode array. When a |
| 156 // label is bound, it represents a known position in the bytecode |
| 157 // array. For labels that are forward references there can be at most |
| 158 // one reference whilst it is unbound. |
| 159 class BytecodeLabel final { |
| 160 public: |
| 161 BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {} |
| 162 ~BytecodeLabel() { DCHECK(bound_ && offset_ != kInvalidOffset); } |
| 163 |
| 164 private: |
| 165 static const size_t kInvalidOffset = static_cast<size_t>(-1); |
| 166 |
| 167 INLINE(void bind_to(size_t offset)) { |
| 168 DCHECK(!bound_ && offset != kInvalidOffset); |
| 169 offset_ = offset; |
| 170 bound_ = true; |
| 171 } |
| 172 INLINE(void set_referrer(size_t offset)) { |
| 173 DCHECK(!bound_ && offset != kInvalidOffset); |
| 174 offset_ = offset; |
| 175 } |
| 176 INLINE(size_t offset() const) { return offset_; } |
| 177 INLINE(bool is_bound() const) { return bound_; } |
| 178 INLINE(bool is_forward_target() const) { |
| 179 return offset() != kInvalidOffset && !is_bound(); |
| 180 } |
| 181 |
| 182 // There are three states for a label: |
| 183 // bound_ offset_ |
| 184 // UNSET false kInvalidOffset |
| 185 // FORWARD_TARGET false Offset of referring jump |
| 186 // BACKWARD_TARGET true Offset of label in bytecode array when bound |
| 187 bool bound_; |
| 188 size_t offset_; |
| 189 |
| 190 friend class BytecodeArrayBuilder; |
| 191 DISALLOW_COPY_AND_ASSIGN(BytecodeLabel); |
| 192 }; |
| 193 |
| 194 |
| 120 // A stack-allocated class than allows the instantiator to allocate | 195 // A stack-allocated class than allows the instantiator to allocate |
| 121 // temporary registers that are cleaned up when scope is closed. | 196 // temporary registers that are cleaned up when scope is closed. |
| 122 class TemporaryRegisterScope { | 197 class TemporaryRegisterScope { |
| 123 public: | 198 public: |
| 124 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); | 199 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
| 125 ~TemporaryRegisterScope(); | 200 ~TemporaryRegisterScope(); |
| 126 Register NewRegister(); | 201 Register NewRegister(); |
| 127 | 202 |
| 128 private: | 203 private: |
| 129 void* operator new(size_t size); | 204 void* operator new(size_t size); |
| 130 void operator delete(void* p); | 205 void operator delete(void* p); |
| 131 | 206 |
| 132 BytecodeArrayBuilder* builder_; | 207 BytecodeArrayBuilder* builder_; |
| 133 int count_; | 208 int count_; |
| 134 int last_register_index_; | 209 int last_register_index_; |
| 135 | 210 |
| 136 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); | 211 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
| 137 }; | 212 }; |
| 138 | 213 |
| 139 | 214 |
| 140 } // namespace interpreter | 215 } // namespace interpreter |
| 141 } // namespace internal | 216 } // namespace internal |
| 142 } // namespace v8 | 217 } // namespace v8 |
| 143 | 218 |
| 144 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 219 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| OLD | NEW |