From 787f3a7a20958bfa6f231abccc617f4d8de25c4d Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 1 Aug 2026 17:36:40 +0800 Subject: [CodeGen] Add MIR function and block attributes --- .../lib/CodeGen/MachineBasicBlockAttributes.cpp | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 backend/lib/CodeGen/MachineBasicBlockAttributes.cpp (limited to 'backend/lib/CodeGen/MachineBasicBlockAttributes.cpp') diff --git a/backend/lib/CodeGen/MachineBasicBlockAttributes.cpp b/backend/lib/CodeGen/MachineBasicBlockAttributes.cpp new file mode 100644 index 0000000..8cb6d48 --- /dev/null +++ b/backend/lib/CodeGen/MachineBasicBlockAttributes.cpp @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "rcc/CodeGen/MachineBasicBlockAttributes.h" + +#include +#include +#include +#include + +namespace rcc { + +std::string_view getMachineBasicBlockAttributeName(MachineBasicBlockAttribute Attribute) noexcept { + switch (Attribute) { + case MachineBasicBlockAttribute::MachineBlockAddressTaken: + return "MachineBlockAddressTaken"; + case MachineBasicBlockAttribute::IRBlockAddressTaken: + return "IRBlockAddressTaken"; + case MachineBasicBlockAttribute::LandingPad: + return "LandingPad"; + case MachineBasicBlockAttribute::InlineAsmBrIndirectTarget: + return "InlineAsmBrIndirectTarget"; + case MachineBasicBlockAttribute::EHFuncletEntry: + return "EHFuncletEntry"; + case MachineBasicBlockAttribute::EHScopeEntry: + return "EHScopeEntry"; + case MachineBasicBlockAttribute::Count: + return "Invalid"; + } + return "Invalid"; +} + +Expected +MachineBasicBlockAttributes::fromRawFlags(std::uint8_t RawFlags) { + if ((RawFlags & static_cast(~ValidFlags)) != 0) { + return makeUnexpected(ErrorCode::InvalidArgument, + "machine basic block attributes contain unknown flag bits"); + } + MachineBasicBlockAttributes Result; + Result.Flags = RawFlags; + return Result; +} + +Expected MachineBasicBlockAttributes::setName(std::string NewName) { + if (NewName.empty()) { + return makeUnexpected(ErrorCode::InvalidArgument, "machine basic block name cannot be empty"); + } + Name = std::move(NewName); + return {}; +} + +Expected MachineBasicBlockAttributes::setIRBlockName(std::string NewName) { + if (NewName.empty()) { + return makeUnexpected(ErrorCode::InvalidArgument, + "machine basic block IR name cannot be empty"); + } + IRBlockName = std::move(NewName); + return {}; +} + +Expected MachineBasicBlockAttributes::verify() const { + if ((Flags & static_cast(~ValidFlags)) != 0) { + return makeUnexpected(ErrorCode::VerificationError, + "machine basic block attributes contain unknown flag bits"); + } + if (Name && Name->empty()) { + return makeUnexpected(ErrorCode::VerificationError, "machine basic block name cannot be empty"); + } + if (IRBlockName && IRBlockName->empty()) { + return makeUnexpected(ErrorCode::VerificationError, + "machine basic block IR name cannot be empty"); + } + if (has(MachineBasicBlockAttribute::IRBlockAddressTaken) && !IRBlockName) { + return makeUnexpected(ErrorCode::VerificationError, + "IR-block-address-taken block requires an IR block reference"); + } + if (BlockAlignment.bytes() == 0) { + return makeUnexpected(ErrorCode::VerificationError, + "machine basic block alignment must be nonzero"); + } + return {}; +} + +} // namespace rcc -- cgit v1.2.3