summaryrefslogtreecommitdiff
path: root/deps/v8/src/interpreter/bytecode-register-allocator.h
blob: 696a3b174a8858446699111a0464bc7658b1f078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_
#define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_

#include "src/interpreter/bytecodes.h"
#include "src/zone-containers.h"

namespace v8 {
namespace internal {
namespace interpreter {

class BytecodeArrayBuilder;
class Register;

class TemporaryRegisterAllocator final {
 public:
  TemporaryRegisterAllocator(Zone* zone, int start_index);

  // Borrow a temporary register.
  int BorrowTemporaryRegister();

  // Borrow a temporary register from the register range outside of
  // |start_index| to |end_index|.
  int BorrowTemporaryRegisterNotInRange(int start_index, int end_index);

  // Return a temporary register when no longer used.
  void ReturnTemporaryRegister(int reg_index);

  // Ensure a run of consecutive registers is available. Each register in
  // the range should be borrowed with BorrowConsecutiveTemporaryRegister().
  // Returns the start index of the run.
  int PrepareForConsecutiveTemporaryRegisters(size_t count);

  // Borrow a register from a range prepared with
  // PrepareForConsecutiveTemporaryRegisters().
  void BorrowConsecutiveTemporaryRegister(int reg_index);

  // Returns true if |reg| is a temporary register and is currently
  // borrowed.
  bool RegisterIsLive(Register reg) const;

  // Returns the first register in the range of temporary registers.
  Register first_temporary_register() const;

  // Returns the last register in the range of temporary registers.
  Register last_temporary_register() const;

  // Returns the start index of temporary register allocations.
  int allocation_base() const { return allocation_base_; }

  // Returns the number of temporary register allocations made.
  int allocation_count() const { return allocation_count_; }

 private:
  // Allocate a temporary register.
  int AllocateTemporaryRegister();

  ZoneSet<int> free_temporaries_;
  int allocation_base_;
  int allocation_count_;

  DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterAllocator);
};

// A class than allows the instantiator to allocate temporary registers that are
// cleaned up when scope is closed.
class BytecodeRegisterAllocator final {
 public:
  explicit BytecodeRegisterAllocator(Zone* zone,
                                     TemporaryRegisterAllocator* allocator);
  ~BytecodeRegisterAllocator();
  Register NewRegister();

  // Ensure |count| consecutive allocations are available.
  void PrepareForConsecutiveAllocations(size_t count);

  // Get the next consecutive allocation after calling
  // PrepareForConsecutiveAllocations.
  Register NextConsecutiveRegister();

  // Returns true if |reg| is allocated in this allocator.
  bool RegisterIsAllocatedInThisScope(Register reg) const;

  // Returns true if unused consecutive allocations remain.
  bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; }

 private:
  TemporaryRegisterAllocator* base_allocator() const { return base_allocator_; }

  TemporaryRegisterAllocator* base_allocator_;
  ZoneVector<int> allocated_;
  int next_consecutive_register_;
  int next_consecutive_count_;

  DISALLOW_COPY_AND_ASSIGN(BytecodeRegisterAllocator);
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8


#endif  // V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_