aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineBasicBlockAttributes.cpp
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-01 17:36:40 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-01 17:36:40 +0800
commit787f3a7a20958bfa6f231abccc617f4d8de25c4d (patch)
tree775d2913f48fc5f3a6e44bc6da986da9e52754ef /backend/lib/CodeGen/MachineBasicBlockAttributes.cpp
parent77d76b73a614820bf45024b053db376f35b06d10 (diff)
downloadrcc-project-787f3a7a20958bfa6f231abccc617f4d8de25c4d.tar.gz
rcc-project-787f3a7a20958bfa6f231abccc617f4d8de25c4d.tar.xz
[CodeGen] Add MIR function and block attributes
Diffstat (limited to 'backend/lib/CodeGen/MachineBasicBlockAttributes.cpp')
-rw-r--r--backend/lib/CodeGen/MachineBasicBlockAttributes.cpp83
1 files changed, 83 insertions, 0 deletions
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 <cstdint>
+#include <string>
+#include <string_view>
+#include <utility>
+
+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>
+MachineBasicBlockAttributes::fromRawFlags(std::uint8_t RawFlags) {
+ if ((RawFlags & static_cast<std::uint8_t>(~ValidFlags)) != 0) {
+ return makeUnexpected(ErrorCode::InvalidArgument,
+ "machine basic block attributes contain unknown flag bits");
+ }
+ MachineBasicBlockAttributes Result;
+ Result.Flags = RawFlags;
+ return Result;
+}
+
+Expected<void> 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<void> 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<void> MachineBasicBlockAttributes::verify() const {
+ if ((Flags & static_cast<std::uint8_t>(~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