Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
| 6 | #define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |
| 7 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8 | #include "src/interpreter/bytecodes.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | #include "src/zone-containers.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | namespace interpreter { |
| 14 | |
| 15 | class BytecodeArrayBuilder; |
| 16 | class Register; |
| 17 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 68 | // A class that allows the instantiator to allocate temporary registers that are |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | // cleaned up when scope is closed. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 70 | class BytecodeRegisterAllocator final { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 71 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 72 | explicit BytecodeRegisterAllocator(Zone* zone, |
| 73 | TemporaryRegisterAllocator* allocator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | ~BytecodeRegisterAllocator(); |
| 75 | Register NewRegister(); |
| 76 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 77 | // Ensure |count| consecutive allocations are available. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 78 | void PrepareForConsecutiveAllocations(size_t count); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 79 | |
| 80 | // Get the next consecutive allocation after calling |
| 81 | // PrepareForConsecutiveAllocations. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 82 | Register NextConsecutiveRegister(); |
| 83 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 84 | // Returns true if |reg| is allocated in this allocator. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | bool RegisterIsAllocatedInThisScope(Register reg) const; |
| 86 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 87 | // Returns true if unused consecutive allocations remain. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; } |
| 89 | |
| 90 | private: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 91 | TemporaryRegisterAllocator* base_allocator() const { return base_allocator_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 93 | TemporaryRegisterAllocator* base_allocator_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 94 | ZoneVector<int> allocated_; |
| 95 | int next_consecutive_register_; |
| 96 | int next_consecutive_count_; |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator); |
| 99 | }; |
| 100 | |
| 101 | } // namespace interpreter |
| 102 | } // namespace internal |
| 103 | } // namespace v8 |
| 104 | |
| 105 | |
| 106 | #endif // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_ |