| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace interpreter { | 21 namespace interpreter { |
| 22 | 22 |
| 23 class BytecodeLabel; | 23 class BytecodeLabel; |
| 24 class Register; | 24 class Register; |
| 25 | 25 |
| 26 class BytecodeArrayBuilder { | 26 class BytecodeArrayBuilder { |
| 27 public: | 27 public: |
| 28 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); | 28 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); |
| 29 Handle<BytecodeArray> ToBytecodeArray(); | 29 Handle<BytecodeArray> ToBytecodeArray(); |
| 30 | 30 |
| 31 // Set number of parameters expected by function. | 31 // Set the number of parameters expected by function. |
| 32 void set_parameter_count(int number_of_params); | 32 void set_parameter_count(int number_of_params); |
| 33 int parameter_count() const { | 33 int parameter_count() const { |
| 34 DCHECK_GE(parameter_count_, 0); | 34 DCHECK_GE(parameter_count_, 0); |
| 35 return parameter_count_; | 35 return parameter_count_; |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Set number of locals required for bytecode array. | 38 // Set the number of locals required for bytecode array. |
| 39 void set_locals_count(int number_of_locals); | 39 void set_locals_count(int number_of_locals); |
| 40 int locals_count() const { | 40 int locals_count() const { |
| 41 DCHECK_GE(local_register_count_, 0); | 41 DCHECK_GE(local_register_count_, 0); |
| 42 return local_register_count_; | 42 return local_register_count_; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Set number of contexts required for bytecode array. | 45 // Set number of contexts required for bytecode array. |
| 46 void set_context_count(int number_of_contexts); | 46 void set_context_count(int number_of_contexts); |
| 47 int context_count() const { | 47 int context_count() const { |
| 48 DCHECK_GE(context_register_count_, 0); | 48 DCHECK_GE(context_register_count_, 0); |
| 49 return context_register_count_; | 49 return context_register_count_; |
| 50 } | 50 } |
| 51 | 51 |
| 52 Register first_context_register() const; | 52 Register first_context_register() const; |
| 53 Register last_context_register() const; | 53 Register last_context_register() const; |
| 54 | 54 |
| 55 // Returns the number of fixed (non-temporary) registers. | 55 // Returns the number of fixed (non-temporary) registers. |
| 56 int fixed_register_count() const { return context_count() + locals_count(); } | 56 int fixed_register_count() const { return context_count() + locals_count(); } |
| 57 | 57 |
| 58 Register Parameter(int parameter_index) const; | 58 Register Parameter(int parameter_index) const; |
| 59 | 59 |
| 60 // Constant loads to the accumulator. | 60 // Return true if the register |reg| represents a parameter or a |
| 61 // local. |
| 62 bool RegisterIsParameterOrLocal(Register reg) const; |
| 63 |
| 64 // Return true if the register |reg| represents a temporary register. |
| 65 bool RegisterIsTemporary(Register reg) const; |
| 66 |
| 67 // Constant loads to accumulator. |
| 61 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); | 68 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); |
| 62 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); | 69 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); |
| 63 BytecodeArrayBuilder& LoadUndefined(); | 70 BytecodeArrayBuilder& LoadUndefined(); |
| 64 BytecodeArrayBuilder& LoadNull(); | 71 BytecodeArrayBuilder& LoadNull(); |
| 65 BytecodeArrayBuilder& LoadTheHole(); | 72 BytecodeArrayBuilder& LoadTheHole(); |
| 66 BytecodeArrayBuilder& LoadTrue(); | 73 BytecodeArrayBuilder& LoadTrue(); |
| 67 BytecodeArrayBuilder& LoadFalse(); | 74 BytecodeArrayBuilder& LoadFalse(); |
| 68 | 75 |
| 69 // Global loads to accumulator and stores from the accumulator. | 76 // Global loads to the accumulator and stores from the accumulator. |
| 70 BytecodeArrayBuilder& LoadGlobal(int slot_index); | 77 BytecodeArrayBuilder& LoadGlobal(int slot_index); |
| 71 BytecodeArrayBuilder& StoreGlobal(int slot_index, LanguageMode language_mode); | 78 BytecodeArrayBuilder& StoreGlobal(int slot_index, LanguageMode language_mode); |
| 72 | 79 |
| 73 // Load the object at |slot_index| in |context| into the accumulator. | 80 // Load the object at |slot_index| in |context| into the accumulator. |
| 74 BytecodeArrayBuilder& LoadContextSlot(Register context, int slot_index); | 81 BytecodeArrayBuilder& LoadContextSlot(Register context, int slot_index); |
| 75 | 82 |
| 76 // Stores the object in the accumulator into |slot_index| of |context|. | 83 // Stores the object in the accumulator into |slot_index| of |context|. |
| 77 BytecodeArrayBuilder& StoreContextSlot(Register context, int slot_index); | 84 BytecodeArrayBuilder& StoreContextSlot(Register context, int slot_index); |
| 78 | 85 |
| 79 // Register-accumulator transfers. | 86 // Register-accumulator transfers. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 Strength strength); | 140 Strength strength); |
| 134 | 141 |
| 135 // Unary Operators. | 142 // Unary Operators. |
| 136 BytecodeArrayBuilder& LogicalNot(); | 143 BytecodeArrayBuilder& LogicalNot(); |
| 137 BytecodeArrayBuilder& TypeOf(); | 144 BytecodeArrayBuilder& TypeOf(); |
| 138 | 145 |
| 139 // Tests. | 146 // Tests. |
| 140 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg, | 147 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg, |
| 141 Strength strength); | 148 Strength strength); |
| 142 | 149 |
| 143 // Casts | 150 // Casts. |
| 144 BytecodeArrayBuilder& CastAccumulatorToBoolean(); | 151 BytecodeArrayBuilder& CastAccumulatorToBoolean(); |
| 145 BytecodeArrayBuilder& CastAccumulatorToName(); | 152 BytecodeArrayBuilder& CastAccumulatorToName(); |
| 146 | 153 |
| 147 // Flow Control. | 154 // Flow Control. |
| 148 BytecodeArrayBuilder& Bind(BytecodeLabel* label); | 155 BytecodeArrayBuilder& Bind(BytecodeLabel* label); |
| 149 BytecodeArrayBuilder& Bind(const BytecodeLabel& target, BytecodeLabel* label); | 156 BytecodeArrayBuilder& Bind(const BytecodeLabel& target, BytecodeLabel* label); |
| 150 | 157 |
| 151 BytecodeArrayBuilder& Jump(BytecodeLabel* label); | 158 BytecodeArrayBuilder& Jump(BytecodeLabel* label); |
| 152 BytecodeArrayBuilder& JumpIfTrue(BytecodeLabel* label); | 159 BytecodeArrayBuilder& JumpIfTrue(BytecodeLabel* label); |
| 153 BytecodeArrayBuilder& JumpIfFalse(BytecodeLabel* label); | 160 BytecodeArrayBuilder& JumpIfFalse(BytecodeLabel* label); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 ZoneVector<uint8_t>::iterator jump_location); | 207 ZoneVector<uint8_t>::iterator jump_location); |
| 201 | 208 |
| 202 void EnsureReturn(); | 209 void EnsureReturn(); |
| 203 | 210 |
| 204 bool OperandIsValid(Bytecode bytecode, int operand_index, | 211 bool OperandIsValid(Bytecode bytecode, int operand_index, |
| 205 uint32_t operand_value) const; | 212 uint32_t operand_value) const; |
| 206 bool LastBytecodeInSameBlock() const; | 213 bool LastBytecodeInSameBlock() const; |
| 207 | 214 |
| 208 size_t GetConstantPoolEntry(Handle<Object> object); | 215 size_t GetConstantPoolEntry(Handle<Object> object); |
| 209 | 216 |
| 210 // Scope helpers used by TemporaryRegisterScope | |
| 211 int BorrowTemporaryRegister(); | 217 int BorrowTemporaryRegister(); |
| 212 void ReturnTemporaryRegister(int reg_index); | 218 void ReturnTemporaryRegister(int reg_index); |
| 219 int PrepareForConsecutiveTemporaryRegisters(size_t count); |
| 220 void BorrowConsecutiveTemporaryRegister(int reg_index); |
| 221 bool TemporaryRegisterIsLive(Register reg) const; |
| 222 |
| 223 Register first_temporary_register() const; |
| 224 Register last_temporary_register() const; |
| 213 | 225 |
| 214 Isolate* isolate_; | 226 Isolate* isolate_; |
| 215 Zone* zone_; | 227 Zone* zone_; |
| 216 ZoneVector<uint8_t> bytecodes_; | 228 ZoneVector<uint8_t> bytecodes_; |
| 217 bool bytecode_generated_; | 229 bool bytecode_generated_; |
| 218 size_t last_block_end_; | 230 size_t last_block_end_; |
| 219 size_t last_bytecode_start_; | 231 size_t last_bytecode_start_; |
| 220 bool exit_seen_in_block_; | 232 bool exit_seen_in_block_; |
| 221 | 233 |
| 222 IdentityMap<size_t> constants_map_; | 234 IdentityMap<size_t> constants_map_; |
| 223 ZoneVector<Handle<Object>> constants_; | 235 ZoneVector<Handle<Object>> constants_; |
| 224 | 236 |
| 225 int parameter_count_; | 237 int parameter_count_; |
| 226 int local_register_count_; | 238 int local_register_count_; |
| 227 int context_register_count_; | 239 int context_register_count_; |
| 228 int temporary_register_count_; | 240 int temporary_register_count_; |
| 229 int temporary_register_next_; | 241 |
| 242 ZoneSet<int> free_temporaries_; |
| 230 | 243 |
| 231 friend class TemporaryRegisterScope; | 244 friend class TemporaryRegisterScope; |
| 232 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); | 245 DISALLOW_COPY_AND_ASSIGN(BytecodeArrayBuilder); |
| 233 }; | 246 }; |
| 234 | 247 |
| 235 | 248 |
| 236 // A label representing a branch target in a bytecode array. When a | 249 // A label representing a branch target in a bytecode array. When a |
| 237 // label is bound, it represents a known position in the bytecode | 250 // label is bound, it represents a known position in the bytecode |
| 238 // array. For labels that are forward references there can be at most | 251 // array. For labels that are forward references there can be at most |
| 239 // one reference whilst it is unbound. | 252 // one reference whilst it is unbound. |
| 240 class BytecodeLabel final { | 253 class BytecodeLabel final { |
| 241 public: | 254 public: |
| 242 BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {} | 255 BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {} |
| (...skipping 23 matching lines...) Expand all Loading... |
| 266 // BACKWARD_TARGET true Offset of label in bytecode array when bound | 279 // BACKWARD_TARGET true Offset of label in bytecode array when bound |
| 267 bool bound_; | 280 bool bound_; |
| 268 size_t offset_; | 281 size_t offset_; |
| 269 | 282 |
| 270 friend class BytecodeArrayBuilder; | 283 friend class BytecodeArrayBuilder; |
| 271 }; | 284 }; |
| 272 | 285 |
| 273 | 286 |
| 274 // A stack-allocated class than allows the instantiator to allocate | 287 // A stack-allocated class than allows the instantiator to allocate |
| 275 // temporary registers that are cleaned up when scope is closed. | 288 // temporary registers that are cleaned up when scope is closed. |
| 289 // TODO(oth): Deprecate TemporaryRegisterScope use. Code should be |
| 290 // using result scopes as far as possible. |
| 276 class TemporaryRegisterScope { | 291 class TemporaryRegisterScope { |
| 277 public: | 292 public: |
| 278 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); | 293 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
| 279 ~TemporaryRegisterScope(); | 294 ~TemporaryRegisterScope(); |
| 280 Register NewRegister(); | 295 Register NewRegister(); |
| 281 | 296 |
| 297 void PrepareForConsecutiveAllocations(size_t count); |
| 298 Register NextConsecutiveRegister(); |
| 299 |
| 282 private: | 300 private: |
| 283 void* operator new(size_t size); | 301 void* operator new(size_t size); |
| 284 void operator delete(void* p); | 302 void operator delete(void* p); |
| 285 | 303 |
| 286 BytecodeArrayBuilder* builder_; | 304 BytecodeArrayBuilder* builder_; |
| 287 int count_; | 305 const TemporaryRegisterScope* outer_; |
| 288 int last_register_index_; | 306 ZoneVector<int> allocated_; |
| 307 int next_consecutive_register_; |
| 308 int next_consecutive_count_; |
| 289 | 309 |
| 290 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); | 310 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
| 291 }; | 311 }; |
| 292 | 312 |
| 293 | 313 |
| 294 } // namespace interpreter | 314 } // namespace interpreter |
| 295 } // namespace internal | 315 } // namespace internal |
| 296 } // namespace v8 | 316 } // namespace v8 |
| 297 | 317 |
| 298 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 318 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| OLD | NEW |