aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineFunction.h
blob: 38d88f9eda7ac7f8574e4c5384696e6bf23f2f0a (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEFUNCTION_H
#define RCC_CODEGEN_MACHINEFUNCTION_H

#include "rcc/CodeGen/MachineConstantPool.h"
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachineFunctionProperties.h"
#include "rcc/CodeGen/MachineJumpTableInfo.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/StableID.h"
#include "rcc/Support/Error.h"

#include <cstddef>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <vector>

namespace rcc {

class MachineArena;
class MachineBasicBlock;
class MachineCFGUpdater;
class MachineInstr;
class MachineMemOperand;
struct MachineMemOperandDesc;
class Opcode;

class MachineFunction final {
public:
  [[nodiscard]] static Expected<std::unique_ptr<MachineFunction>>
  create(std::string Name, MachineFunctionStableID StableID);

  MachineFunction(const MachineFunction &) = delete;
  MachineFunction &operator=(const MachineFunction &) = delete;
  MachineFunction(MachineFunction &&) = delete;
  MachineFunction &operator=(MachineFunction &&) = delete;
  ~MachineFunction();

  [[nodiscard]] std::string_view name() const noexcept {
    return Name;
  }

  [[nodiscard]] MachineFunctionStableID stableID() const noexcept {
    return StableID;
  }

  [[nodiscard]] MachineRegisterInfo &registerInfo() noexcept {
    return RegInfo;
  }

  [[nodiscard]] const MachineRegisterInfo &registerInfo() const noexcept {
    return RegInfo;
  }

  [[nodiscard]] MachineFrameInfo &frameInfo() noexcept {
    return FrameInfo;
  }

  [[nodiscard]] const MachineFrameInfo &frameInfo() const noexcept {
    return FrameInfo;
  }

  [[nodiscard]] MachineConstantPool &constantPool() noexcept {
    return ConstantPool;
  }

  [[nodiscard]] const MachineConstantPool &constantPool() const noexcept {
    return ConstantPool;
  }

  [[nodiscard]] MachineJumpTableInfo &jumpTableInfo() noexcept {
    return JumpTables;
  }

  [[nodiscard]] const MachineJumpTableInfo &jumpTableInfo() const noexcept {
    return JumpTables;
  }

  [[nodiscard]] MachineFunctionProperties &properties() noexcept {
    return Properties;
  }

  [[nodiscard]] const MachineFunctionProperties &properties() const noexcept {
    return Properties;
  }

  [[nodiscard]] Expected<MachineBasicBlock *> createBasicBlock();
  [[nodiscard]] Expected<MachineInstr *> createInstruction(Opcode InstructionOpcode);
  [[nodiscard]] Expected<const MachineMemOperand *>
  createMemOperand(MachineMemOperandDesc Description);

  [[nodiscard]] Expected<void> appendBlock(MachineBasicBlock &Block);
  [[nodiscard]] Expected<void> insertBlockBefore(MachineBasicBlock &Position,
                                                 MachineBasicBlock &Block);
  [[nodiscard]] Expected<void> removeBlock(MachineBasicBlock &Block);

  [[nodiscard]] MachineBasicBlock *firstBlock() noexcept {
    return FirstBlock;
  }

  [[nodiscard]] const MachineBasicBlock *firstBlock() const noexcept {
    return FirstBlock;
  }

  [[nodiscard]] MachineBasicBlock *lastBlock() noexcept {
    return LastBlock;
  }

  [[nodiscard]] const MachineBasicBlock *lastBlock() const noexcept {
    return LastBlock;
  }

  [[nodiscard]] std::size_t blockCount() const noexcept;
  [[nodiscard]] std::span<MachineBasicBlock *const> allBlocks() const noexcept {
    return AllBlocks;
  }

  [[nodiscard]] std::span<MachineInstr *const> allInstructions() const noexcept {
    return AllInstructions;
  }

  [[nodiscard]] std::span<const MachineMemOperand *const> allMemOperands() const noexcept {
    return AllMemOperands;
  }

  [[nodiscard]] bool ownsMemOperand(const MachineMemOperand &MemOperand) const noexcept;

  [[nodiscard]] MachineCFGUpdater cfgUpdater() noexcept;
  void renumberBlocks() noexcept;

private:
  MachineFunction(std::string Name, MachineFunctionStableID StableID);

  std::string Name;
  MachineFunctionStableID StableID;
  MachineRegisterInfo RegInfo;
  MachineFrameInfo FrameInfo;
  MachineConstantPool ConstantPool;
  MachineJumpTableInfo JumpTables;
  MachineFunctionProperties Properties;
  MachineBasicBlock *FirstBlock = nullptr;
  MachineBasicBlock *LastBlock = nullptr;
  std::vector<MachineBasicBlock *> AllBlocks;
  std::vector<MachineInstr *> AllInstructions;
  std::vector<const MachineMemOperand *> AllMemOperands;
  StableIDGenerator<MachineBlockStableID> BlockIDGenerator;
  StableIDGenerator<MachineInstrStableID> InstrIDGenerator;
  std::unique_ptr<MachineArena> Arena;

  friend class MachineBasicBlock;
  friend class MachineCFGUpdater;
  friend class MachineInstr;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEFUNCTION_H