| 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_REGISTER_ALLOCATOR_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
| 6 #define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ | 6 #define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
| 7 | 7 |
| 8 #include "src/interpreter/bytecodes.h" |
| 8 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
| 9 | 10 |
| 10 namespace v8 { | 11 namespace v8 { |
| 11 namespace internal { | 12 namespace internal { |
| 12 namespace interpreter { | 13 namespace interpreter { |
| 13 | 14 |
| 14 class BytecodeArrayBuilder; | 15 class BytecodeArrayBuilder; |
| 15 class Register; | 16 class Register; |
| 16 | 17 |
| 18 class TemporaryRegisterAllocator final { |
| 19 public: |
| 20 TemporaryRegisterAllocator(Zone* zone, int start_index); |
| 21 |
| 22 // Borrow a temporary register. |
| 23 int BorrowTemporaryRegister(); |
| 24 |
| 25 // Borrow a temporary register from the register range outside of |
| 26 // |start_index| to |end_index|. |
| 27 int BorrowTemporaryRegisterNotInRange(int start_index, int end_index); |
| 28 |
| 29 // Return a temporary register when no longer used. |
| 30 void ReturnTemporaryRegister(int reg_index); |
| 31 |
| 32 // Ensure a run of consecutive registers is available. Each register in |
| 33 // the range should be borrowed with BorrowConsecutiveTemporaryRegister(). |
| 34 // Returns the start index of the run. |
| 35 int PrepareForConsecutiveTemporaryRegisters(size_t count); |
| 36 |
| 37 // Borrow a register from a range prepared with |
| 38 // PrepareForConsecutiveTemporaryRegisters(). |
| 39 void BorrowConsecutiveTemporaryRegister(int reg_index); |
| 40 |
| 41 // Returns true if |reg| is a temporary register and is currently |
| 42 // borrowed. |
| 43 bool RegisterIsLive(Register reg) const; |
| 44 |
| 45 // Returns the first register in the range of temporary registers. |
| 46 Register first_temporary_register() const; |
| 47 |
| 48 // Returns the last register in the range of temporary registers. |
| 49 Register last_temporary_register() const; |
| 50 |
| 51 // Returns the start index of temporary register allocations. |
| 52 int allocation_base() const { return allocation_base_; } |
| 53 |
| 54 // Returns the number of temporary register allocations made. |
| 55 int allocation_count() const { return allocation_count_; } |
| 56 |
| 57 private: |
| 58 // Allocate a temporary register. |
| 59 int AllocateTemporaryRegister(); |
| 60 |
| 61 ZoneSet<int> free_temporaries_; |
| 62 int allocation_base_; |
| 63 int allocation_count_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterAllocator); |
| 66 }; |
| 67 |
| 17 // A class than allows the instantiator to allocate temporary registers that are | 68 // A class than allows the instantiator to allocate temporary registers that are |
| 18 // cleaned up when scope is closed. | 69 // cleaned up when scope is closed. |
| 19 class BytecodeRegisterAllocator { | 70 class BytecodeRegisterAllocator final { |
| 20 public: | 71 public: |
| 21 explicit BytecodeRegisterAllocator(BytecodeArrayBuilder* builder); | 72 explicit BytecodeRegisterAllocator(Zone* zone, |
| 73 TemporaryRegisterAllocator* allocator); |
| 22 ~BytecodeRegisterAllocator(); | 74 ~BytecodeRegisterAllocator(); |
| 23 Register NewRegister(); | 75 Register NewRegister(); |
| 24 | 76 |
| 25 void PrepareForConsecutiveAllocations(size_t count); | 77 void PrepareForConsecutiveAllocations(size_t count); |
| 26 Register NextConsecutiveRegister(); | 78 Register NextConsecutiveRegister(); |
| 27 | 79 |
| 28 bool RegisterIsAllocatedInThisScope(Register reg) const; | 80 bool RegisterIsAllocatedInThisScope(Register reg) const; |
| 29 | 81 |
| 30 bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; } | 82 bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; } |
| 31 | 83 |
| 32 private: | 84 private: |
| 33 void* operator new(size_t size); | 85 TemporaryRegisterAllocator* base_allocator() const { return base_allocator_; } |
| 34 void operator delete(void* p); | |
| 35 | 86 |
| 36 BytecodeArrayBuilder* builder_; | 87 TemporaryRegisterAllocator* base_allocator_; |
| 37 ZoneVector<int> allocated_; | 88 ZoneVector<int> allocated_; |
| 38 int next_consecutive_register_; | 89 int next_consecutive_register_; |
| 39 int next_consecutive_count_; | 90 int next_consecutive_count_; |
| 40 | 91 |
| 41 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); | 92 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); |
| 42 }; | 93 }; |
| 43 | 94 |
| 44 } // namespace interpreter | 95 } // namespace interpreter |
| 45 } // namespace internal | 96 } // namespace internal |
| 46 } // namespace v8 | 97 } // namespace v8 |
| 47 | 98 |
| 48 | 99 |
| 49 #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ | 100 #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
| OLD | NEW |