blob: 4efb612db52b7566464d58a199f15e397b820350 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
9namespace v8 {
10namespace internal {
11namespace interpreter {
12
13BytecodeRegisterAllocator::BytecodeRegisterAllocator(
14 BytecodeArrayBuilder* builder)
15 : builder_(builder),
16 allocated_(builder->zone()),
17 next_consecutive_register_(-1),
18 next_consecutive_count_(-1) {}
19
20
21BytecodeRegisterAllocator::~BytecodeRegisterAllocator() {
22 for (auto i = allocated_.rbegin(); i != allocated_.rend(); i++) {
23 builder_->ReturnTemporaryRegister(*i);
24 }
25 allocated_.clear();
26}
27
28
29Register 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
43bool 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
52void 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
61Register 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