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 | #include "src/interpreter/bytecode-register-allocator.h" |
| 6 | |
| 7 | #include "src/interpreter/bytecode-array-builder.h" |
| 8 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | namespace interpreter { |
| 12 | |
| 13 | BytecodeRegisterAllocator::BytecodeRegisterAllocator( |
| 14 | BytecodeArrayBuilder* builder) |
| 15 | : builder_(builder), |
| 16 | allocated_(builder->zone()), |
| 17 | next_consecutive_register_(-1), |
| 18 | next_consecutive_count_(-1) {} |
| 19 | |
| 20 | |
| 21 | BytecodeRegisterAllocator::~BytecodeRegisterAllocator() { |
| 22 | for (auto i = allocated_.rbegin(); i != allocated_.rend(); i++) { |
| 23 | builder_->ReturnTemporaryRegister(*i); |
| 24 | } |
| 25 | allocated_.clear(); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | Register BytecodeRegisterAllocator::NewRegister() { |
| 30 | int allocated = -1; |
| 31 | if (next_consecutive_count_ <= 0) { |
| 32 | allocated = builder_->BorrowTemporaryRegister(); |
| 33 | } else { |
| 34 | allocated = builder_->BorrowTemporaryRegisterNotInRange( |
| 35 | next_consecutive_register_, |
| 36 | next_consecutive_register_ + next_consecutive_count_ - 1); |
| 37 | } |
| 38 | allocated_.push_back(allocated); |
| 39 | return Register(allocated); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | bool BytecodeRegisterAllocator::RegisterIsAllocatedInThisScope( |
| 44 | Register reg) const { |
| 45 | for (auto i = allocated_.begin(); i != allocated_.end(); i++) { |
| 46 | if (*i == reg.index()) return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void BytecodeRegisterAllocator::PrepareForConsecutiveAllocations(size_t count) { |
| 53 | if (static_cast<int>(count) > next_consecutive_count_) { |
| 54 | next_consecutive_register_ = |
| 55 | builder_->PrepareForConsecutiveTemporaryRegisters(count); |
| 56 | next_consecutive_count_ = static_cast<int>(count); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | Register BytecodeRegisterAllocator::NextConsecutiveRegister() { |
| 62 | DCHECK_GE(next_consecutive_register_, 0); |
| 63 | DCHECK_GT(next_consecutive_count_, 0); |
| 64 | builder_->BorrowConsecutiveTemporaryRegister(next_consecutive_register_); |
| 65 | allocated_.push_back(next_consecutive_register_); |
| 66 | next_consecutive_count_--; |
| 67 | return Register(next_consecutive_register_++); |
| 68 | } |
| 69 | |
| 70 | } // namespace interpreter |
| 71 | } // namespace internal |
| 72 | } // namespace v8 |