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/bytecode-register.h" |
8 #include "src/interpreter/bytecodes.h" | 9 #include "src/interpreter/bytecodes.h" |
9 #include "src/zone/zone-containers.h" | 10 #include "src/zone/zone-containers.h" |
10 | 11 |
11 namespace v8 { | 12 namespace v8 { |
12 namespace internal { | 13 namespace internal { |
13 namespace interpreter { | 14 namespace interpreter { |
14 | 15 |
15 class BytecodeArrayBuilder; | 16 class RegisterList { |
16 class Register; | 17 public: |
17 class TemporaryRegisterObserver; | 18 RegisterList() : first_reg_index_(Register().index()), register_count_(0) {} |
| 19 RegisterList(int first_reg_index, int register_count) |
| 20 : first_reg_index_(first_reg_index), register_count_(register_count) {} |
18 | 21 |
19 class TemporaryRegisterAllocator final { | 22 // Returns a new RegisterList which is a truncated version of this list, with |
20 public: | 23 // |count| registers. |
21 TemporaryRegisterAllocator(Zone* zone, int start_index); | 24 const RegisterList Truncate(int new_count) { |
| 25 DCHECK_GE(new_count, 0); |
| 26 DCHECK_LT(new_count, register_count_); |
| 27 return RegisterList(first_reg_index_, new_count); |
| 28 } |
22 | 29 |
23 // Borrow a temporary register. | 30 const Register operator[](size_t i) const { |
24 int BorrowTemporaryRegister(); | 31 DCHECK_LT(static_cast<int>(i), register_count_); |
| 32 return Register(first_reg_index_ + static_cast<int>(i)); |
| 33 } |
25 | 34 |
26 // Borrow a temporary register from the register range outside of | 35 const Register first_register() const { |
27 // |start_index| to |end_index|. | 36 return (register_count() == 0) ? Register(0) : (*this)[0]; |
28 int BorrowTemporaryRegisterNotInRange(int start_index, int end_index); | 37 } |
29 | 38 |
30 // Return a temporary register when no longer used. | 39 int register_count() const { return register_count_; } |
31 void ReturnTemporaryRegister(int reg_index); | |
32 | |
33 // Ensure a run of consecutive registers is available. Each register in | |
34 // the range should be borrowed with BorrowConsecutiveTemporaryRegister(). | |
35 // Returns the start index of the run. | |
36 int PrepareForConsecutiveTemporaryRegisters(size_t count); | |
37 | |
38 // Borrow a register from a range prepared with | |
39 // PrepareForConsecutiveTemporaryRegisters(). | |
40 void BorrowConsecutiveTemporaryRegister(int reg_index); | |
41 | |
42 // Returns true if |reg| is a temporary register and is currently | |
43 // borrowed. | |
44 bool RegisterIsLive(Register reg) const; | |
45 | |
46 // Returns the first register in the range of temporary registers. | |
47 Register first_temporary_register() const; | |
48 | |
49 // Returns the last register in the range of temporary registers. | |
50 Register last_temporary_register() const; | |
51 | |
52 // Returns the start index of temporary register allocations. | |
53 int allocation_base() const { return allocation_base_; } | |
54 | |
55 // Returns the number of temporary register allocations made. | |
56 int allocation_count() const { return allocation_count_; } | |
57 | |
58 // Sets an observer for temporary register events. | |
59 void set_observer(TemporaryRegisterObserver* observer); | |
60 | 40 |
61 private: | 41 private: |
62 // Allocate a temporary register. | 42 int first_reg_index_; |
63 int AllocateTemporaryRegister(); | 43 int register_count_; |
64 | |
65 ZoneSet<int> free_temporaries_; | |
66 int allocation_base_; | |
67 int allocation_count_; | |
68 TemporaryRegisterObserver* observer_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterAllocator); | |
71 }; | 44 }; |
72 | 45 |
73 class TemporaryRegisterObserver { | 46 // A class that allows the allocation of contiguous temporary registers. |
74 public: | |
75 virtual ~TemporaryRegisterObserver() {} | |
76 virtual void TemporaryRegisterFreeEvent(Register reg) = 0; | |
77 }; | |
78 | |
79 // A class that allows the instantiator to allocate temporary registers that are | |
80 // cleaned up when scope is closed. | |
81 class BytecodeRegisterAllocator final { | 47 class BytecodeRegisterAllocator final { |
82 public: | 48 public: |
83 explicit BytecodeRegisterAllocator(Zone* zone, | 49 // Enables observation of register allocation and free events. |
84 TemporaryRegisterAllocator* allocator); | 50 class Observer { |
85 ~BytecodeRegisterAllocator(); | 51 public: |
86 Register NewRegister(); | 52 virtual ~Observer() {} |
| 53 virtual void RegisterAllocateEvent(Register reg) = 0; |
| 54 virtual void RegisterListAllocateEvent(RegisterList reg_list) = 0; |
| 55 virtual void RegisterListFreeEvent(RegisterList reg_list) = 0; |
| 56 }; |
87 | 57 |
88 // Ensure |count| consecutive allocations are available. | 58 explicit BytecodeRegisterAllocator(int start_index) |
89 void PrepareForConsecutiveAllocations(size_t count); | 59 : next_register_index_(start_index), |
| 60 max_register_count_(start_index), |
| 61 observer_(nullptr) {} |
| 62 ~BytecodeRegisterAllocator() {} |
90 | 63 |
91 // Get the next consecutive allocation after calling | 64 // Returns a new register. |
92 // PrepareForConsecutiveAllocations. | 65 Register NewRegister() { |
93 Register NextConsecutiveRegister(); | 66 Register reg(next_register_index_++); |
| 67 max_register_count_ = std::max(next_register_index_, max_register_count_); |
| 68 if (observer_) { |
| 69 observer_->RegisterAllocateEvent(reg); |
| 70 } |
| 71 return reg; |
| 72 } |
94 | 73 |
95 // Returns true if |reg| is allocated in this allocator. | 74 // Returns a consecutive list of |count| new registers. |
96 bool RegisterIsAllocatedInThisScope(Register reg) const; | 75 RegisterList NewRegisterList(int count) { |
| 76 RegisterList reg_list(next_register_index_, count); |
| 77 next_register_index_ += count; |
| 78 max_register_count_ = std::max(next_register_index_, max_register_count_); |
| 79 if (observer_) { |
| 80 observer_->RegisterListAllocateEvent(reg_list); |
| 81 } |
| 82 return reg_list; |
| 83 } |
97 | 84 |
98 // Returns true if unused consecutive allocations remain. | 85 // Release all registers above |register_index|. |
99 bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; } | 86 void ReleaseRegisters(int register_index) { |
| 87 if (observer_) { |
| 88 observer_->RegisterListFreeEvent( |
| 89 RegisterList(register_index, next_register_index_ - register_index)); |
| 90 } |
| 91 next_register_index_ = register_index; |
| 92 } |
| 93 |
| 94 // Returns true if the register |reg| is a live register. |
| 95 bool RegisterIsLive(Register reg) const { |
| 96 return reg.index() < next_register_index_; |
| 97 } |
| 98 |
| 99 void set_observer(Observer* observer) { observer_ = observer; } |
| 100 |
| 101 int next_register_index() const { return next_register_index_; } |
| 102 int maximum_register_count() const { return max_register_count_; } |
100 | 103 |
101 private: | 104 private: |
102 TemporaryRegisterAllocator* base_allocator() const { return base_allocator_; } | 105 int next_register_index_; |
103 | 106 int max_register_count_; |
104 TemporaryRegisterAllocator* base_allocator_; | 107 Observer* observer_; |
105 ZoneVector<int> allocated_; | |
106 int next_consecutive_register_; | |
107 int next_consecutive_count_; | |
108 | 108 |
109 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); | 109 DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); |
110 }; | 110 }; |
111 | 111 |
112 } // namespace interpreter | 112 } // namespace interpreter |
113 } // namespace internal | 113 } // namespace internal |
114 } // namespace v8 | 114 } // namespace v8 |
115 | 115 |
116 | 116 |
117 #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ | 117 #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
OLD | NEW |