diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-01 05:27:13 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-01 05:27:13 +0800 |
| commit | c759cebef15bdac2ef160065efd116fe6ed540a7 (patch) | |
| tree | 28fe46f32bebc7b0ba67f38d04e01e9e6cd272e7 /backend/include/rcc/CodeGen/MachineFunction.h | |
| parent | e2505772ab260c79d0075728082c907a90dab7ca (diff) | |
| download | rcc-project-c759cebef15bdac2ef160065efd116fe6ed540a7.tar.gz rcc-project-c759cebef15bdac2ef160065efd116fe6ed540a7.tar.xz | |
[CodeGen] Build the core MachineIR object model
Add arena-owned, address-stable MachineFunction, MachineBasicBlock, and MachineInstr nodes with intrusive layout and instruction lists. Model every required MachineOperand category, reciprocal ties, source state, and immutable per-instruction memory references.
Implement physical and virtual register def-use chains whose back-links survive operand vector moves, plus vreg properties, tombstones, live-ins, reserved registers, and atomic CFG updates. Exercise ownership, reallocation, unlink/relink, cross-function rejection, and sanitizer paths.
Diffstat (limited to 'backend/include/rcc/CodeGen/MachineFunction.h')
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineFunction.h | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/backend/include/rcc/CodeGen/MachineFunction.h b/backend/include/rcc/CodeGen/MachineFunction.h new file mode 100644 index 0000000..487fb5b --- /dev/null +++ b/backend/include/rcc/CodeGen/MachineFunction.h @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MACHINEFUNCTION_H +#define RCC_CODEGEN_MACHINEFUNCTION_H + +#include "rcc/CodeGen/MachineFunctionProperties.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 ®isterInfo() noexcept { + return RegInfo; + } + + [[nodiscard]] const MachineRegisterInfo ®isterInfo() const noexcept { + return RegInfo; + } + + [[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; + 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 |
