| 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; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int feedback_slot, | 68 int feedback_slot, |
| 68 LanguageMode language_mode); | 69 LanguageMode language_mode); |
| 69 | 70 |
| 70 // 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 |
| 71 // |callable|, the receiver should be in |receiver| and all subsequent | 72 // |callable|, the receiver should be in |receiver| and all subsequent |
| 72 // arguments should be in registers <receiver + 1> to | 73 // arguments should be in registers <receiver + 1> to |
| 73 // <receiver + 1 + arg_count>. | 74 // <receiver + 1 + arg_count>. |
| 74 BytecodeArrayBuilder& Call(Register callable, Register receiver, | 75 BytecodeArrayBuilder& Call(Register callable, Register receiver, |
| 75 size_t arg_count); | 76 size_t arg_count); |
| 76 | 77 |
| 77 // Operators. | 78 // Operators (register == lhs, accumulator = rhs). |
| 78 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); | 79 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); |
| 79 | 80 |
| 81 // Tests. |
| 82 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg, |
| 83 LanguageMode language_mode); |
| 84 |
| 85 // Casts |
| 86 BytecodeArrayBuilder& CastAccumulatorToBoolean(); |
| 87 |
| 80 // 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); |
| 81 BytecodeArrayBuilder& Return(); | 93 BytecodeArrayBuilder& Return(); |
| 82 | 94 |
| 83 private: | 95 private: |
| 84 static Bytecode BytecodeForBinaryOperation(Token::Value op); | 96 static Bytecode BytecodeForBinaryOperation(Token::Value op); |
| 85 static bool FitsInByteOperand(int value); | 97 static Bytecode BytecodeForCompareOperation(Token::Value op); |
| 86 static bool FitsInByteOperand(size_t value); | 98 static bool FitsInIdxOperand(int value); |
| 99 static bool FitsInIdxOperand(size_t value); |
| 100 static bool FitsInImm8Operand(int value); |
| 101 static bool IsJumpWithSmi8Operand(Bytecode jump_bytecode); |
| 102 static Bytecode GetJumpWithConstantOperand(Bytecode jump_with_smi8_operand); |
| 87 | 103 |
| 88 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); | 104 ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; } |
| 89 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); | 105 const ZoneVector<uint8_t>* bytecodes() const { return &bytecodes_; } |
| 90 void Output(Bytecode bytecode, uint8_t r0); | 106 Isolate* isolate() const { return isolate_; } |
| 107 |
| 108 template <size_t N> |
| 109 void Output(uint8_t(&bytes)[N]); |
| 110 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1, |
| 111 uint8_t operand2); |
| 112 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1); |
| 113 void Output(Bytecode bytecode, uint8_t operand0); |
| 91 void Output(Bytecode bytecode); | 114 void Output(Bytecode bytecode); |
| 115 void PatchJump(const ZoneVector<uint8_t>::iterator& jump_target, |
| 116 ZoneVector<uint8_t>::iterator jump_location); |
| 117 void OutputJump(Bytecode jump_bytecode, |
| 118 const ZoneVector<uint8_t>::iterator& jump_target); |
| 119 BytecodeArrayBuilder& Jump(Bytecode jump_bytecode, BytecodeLabel* label); |
| 92 | 120 |
| 93 bool OperandIsValid(Bytecode bytecode, int operand_index, | 121 bool OperandIsValid(Bytecode bytecode, int operand_index, |
| 94 uint8_t operand_value) const; | 122 uint8_t operand_value) const; |
| 95 | 123 |
| 96 size_t GetConstantPoolEntry(Handle<Object> object); | 124 size_t GetConstantPoolEntry(Handle<Object> object); |
| 97 | 125 |
| 126 // Scope helpers used by TemporaryRegisterScope |
| 98 int BorrowTemporaryRegister(); | 127 int BorrowTemporaryRegister(); |
| 99 void ReturnTemporaryRegister(int reg_index); | 128 void ReturnTemporaryRegister(int reg_index); |
| 100 | 129 |
| 101 Isolate* isolate_; | 130 Isolate* isolate_; |
| 102 ZoneVector<uint8_t> bytecodes_; | 131 ZoneVector<uint8_t> bytecodes_; |
| 103 bool bytecode_generated_; | 132 bool bytecode_generated_; |
| 104 | 133 |
| 105 IdentityMap<size_t> constants_map_; | 134 IdentityMap<size_t> constants_map_; |
| 106 ZoneVector<Handle<Object>> constants_; | 135 ZoneVector<Handle<Object>> constants_; |
| 107 | 136 |
| 108 int parameter_count_; | 137 int parameter_count_; |
| 109 int local_register_count_; | 138 int local_register_count_; |
| 110 int temporary_register_count_; | 139 int temporary_register_count_; |
| 111 int temporary_register_next_; | 140 int temporary_register_next_; |
| 112 | 141 |
| 113 friend class TemporaryRegisterScope; | 142 friend class TemporaryRegisterScope; |
| 114 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); | 143 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); |
| 115 }; | 144 }; |
| 116 | 145 |
| 146 |
| 147 // A label representing a branch target in a bytecode array. When a |
| 148 // label is bound, it represents a known position in the bytecode |
| 149 // array. For labels that are forward references there can be at most |
| 150 // one reference whilst it is unbound. |
| 151 class BytecodeLabel final { |
| 152 public: |
| 153 BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {} |
| 154 ~BytecodeLabel() { DCHECK(bound_ && offset_ != kInvalidOffset); } |
| 155 |
| 156 private: |
| 157 static const size_t kInvalidOffset = static_cast<size_t>(-1); |
| 158 |
| 159 INLINE(void bind_to(size_t offset)) { |
| 160 DCHECK(!bound_ && offset != kInvalidOffset); |
| 161 offset_ = offset; |
| 162 bound_ = true; |
| 163 } |
| 164 INLINE(void set_referrer(size_t offset)) { |
| 165 DCHECK(!bound_ && offset != kInvalidOffset); |
| 166 offset_ = offset; |
| 167 } |
| 168 INLINE(size_t offset() const) { return offset_; } |
| 169 INLINE(bool is_bound() const) { return bound_; } |
| 170 INLINE(bool is_forward_target() const) { |
| 171 return offset() != kInvalidOffset && !is_bound(); |
| 172 } |
| 173 |
| 174 // There are three states for a label: |
| 175 // bound_ offset_ |
| 176 // UNSET false kInvalidOffset |
| 177 // FORWARD_TARGET false Offset of referring jump |
| 178 // BACKWARD_TARGET true Offset of label in bytecode array when bound |
| 179 bool bound_; |
| 180 size_t offset_; |
| 181 |
| 182 friend class BytecodeArrayBuilder; |
| 183 DISALLOW_COPY_AND_ASSIGN(BytecodeLabel); |
| 184 }; |
| 185 |
| 186 |
| 117 // A stack-allocated class than allows the instantiator to allocate | 187 // A stack-allocated class than allows the instantiator to allocate |
| 118 // temporary registers that are cleaned up when scope is closed. | 188 // temporary registers that are cleaned up when scope is closed. |
| 119 class TemporaryRegisterScope { | 189 class TemporaryRegisterScope { |
| 120 public: | 190 public: |
| 121 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); | 191 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
| 122 ~TemporaryRegisterScope(); | 192 ~TemporaryRegisterScope(); |
| 123 Register NewRegister(); | 193 Register NewRegister(); |
| 124 | 194 |
| 125 private: | 195 private: |
| 126 void* operator new(size_t size); | 196 void* operator new(size_t size); |
| 127 void operator delete(void* p); | 197 void operator delete(void* p); |
| 128 | 198 |
| 129 BytecodeArrayBuilder* builder_; | 199 BytecodeArrayBuilder* builder_; |
| 130 int count_; | 200 int count_; |
| 131 int last_register_index_; | 201 int last_register_index_; |
| 132 | 202 |
| 133 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); | 203 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
| 134 }; | 204 }; |
| 135 | 205 |
| 136 | 206 |
| 137 } // namespace interpreter | 207 } // namespace interpreter |
| 138 } // namespace internal | 208 } // namespace internal |
| 139 } // namespace v8 | 209 } // namespace v8 |
| 140 | 210 |
| 141 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 211 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| OLD | NEW |