blob: a4f68459cbc4696e8501394dbf812ee0de773b73 [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#ifndef V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_
6#define V8_INTERPRETER_BYTECODE_REGISTER_ALLOCATOR_H_
7
Ben Murdoch097c5b22016-05-18 11:27:45 +01008#include "src/interpreter/bytecodes.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/zone-containers.h"
10
11namespace v8 {
12namespace internal {
13namespace interpreter {
14
15class BytecodeArrayBuilder;
16class Register;
17
Ben Murdoch097c5b22016-05-18 11:27:45 +010018class 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 Murdochc5610432016-08-08 18:44:38 +010068// A class that allows the instantiator to allocate temporary registers that are
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069// cleaned up when scope is closed.
Ben Murdoch097c5b22016-05-18 11:27:45 +010070class BytecodeRegisterAllocator final {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +010072 explicit BytecodeRegisterAllocator(Zone* zone,
73 TemporaryRegisterAllocator* allocator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 ~BytecodeRegisterAllocator();
75 Register NewRegister();
76
Ben Murdoch097c5b22016-05-18 11:27:45 +010077 // Ensure |count| consecutive allocations are available.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 void PrepareForConsecutiveAllocations(size_t count);
Ben Murdoch097c5b22016-05-18 11:27:45 +010079
80 // Get the next consecutive allocation after calling
81 // PrepareForConsecutiveAllocations.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 Register NextConsecutiveRegister();
83
Ben Murdoch097c5b22016-05-18 11:27:45 +010084 // Returns true if |reg| is allocated in this allocator.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 bool RegisterIsAllocatedInThisScope(Register reg) const;
86
Ben Murdoch097c5b22016-05-18 11:27:45 +010087 // Returns true if unused consecutive allocations remain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 bool HasConsecutiveAllocations() const { return next_consecutive_count_ > 0; }
89
90 private:
Ben Murdoch097c5b22016-05-18 11:27:45 +010091 TemporaryRegisterAllocator* base_allocator() const { return base_allocator_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092
Ben Murdoch097c5b22016-05-18 11:27:45 +010093 TemporaryRegisterAllocator* base_allocator_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 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_