aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/MC/MCSection.h
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-09 18:31:49 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-09 18:31:49 +0800
commit379ff50dcc7ccf977379aee68fa9d10f69065d23 (patch)
treee0fac3952d46d0bbcaea32bcfc6087c95fff2992 /backend/include/rcc/MC/MCSection.h
parent495c675771f43cfa12ab7a426a538be72fec1805 (diff)
downloadrcc-project-379ff50dcc7ccf977379aee68fa9d10f69065d23.tar.gz
rcc-project-379ff50dcc7ccf977379aee68fa9d10f69065d23.tar.xz
[MC] Implement RV64GC assembly emission
Diffstat (limited to 'backend/include/rcc/MC/MCSection.h')
-rw-r--r--backend/include/rcc/MC/MCSection.h108
1 files changed, 108 insertions, 0 deletions
diff --git a/backend/include/rcc/MC/MCSection.h b/backend/include/rcc/MC/MCSection.h
new file mode 100644
index 0000000..4f76427
--- /dev/null
+++ b/backend/include/rcc/MC/MCSection.h
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#ifndef RCC_MC_MCSECTION_H
+#define RCC_MC_MCSECTION_H
+
+#include "rcc/MC/MCSymbol.h"
+#include "rcc/Support/Error.h"
+
+#include <cstdint>
+#include <initializer_list>
+#include <memory>
+#include <optional>
+#include <string>
+#include <string_view>
+
+namespace rcc::mc {
+
+enum class MCSectionKind : std::uint8_t {
+ Text,
+ ReadOnly,
+ Data,
+ BSS,
+ TLSData,
+ TLSBSS,
+ EHFrame,
+ Custom,
+};
+
+enum class MCSectionFlag : std::uint8_t {
+ Alloc,
+ Executable,
+ Writable,
+ Merge,
+ Strings,
+ TLS,
+ Group,
+ Retain,
+ Count,
+};
+
+class MCSectionFlags final {
+public:
+ constexpr MCSectionFlags() noexcept = default;
+
+ constexpr MCSectionFlags(std::initializer_list<MCSectionFlag> Flags) noexcept {
+ for (const MCSectionFlag Flag : Flags)
+ set(Flag);
+ }
+
+ constexpr void set(MCSectionFlag Flag) noexcept {
+ const auto Index = static_cast<std::uint8_t>(Flag);
+ if (Index < static_cast<std::uint8_t>(MCSectionFlag::Count))
+ Mask |= std::uint16_t{1} << Index;
+ }
+
+ [[nodiscard]] constexpr bool has(MCSectionFlag Flag) const noexcept {
+ const auto Index = static_cast<std::uint8_t>(Flag);
+ return Index < static_cast<std::uint8_t>(MCSectionFlag::Count) &&
+ (Mask & (std::uint16_t{1} << Index)) != 0;
+ }
+
+ [[nodiscard]] constexpr std::uint16_t rawValue() const noexcept {
+ return Mask;
+ }
+
+ friend constexpr bool operator==(MCSectionFlags Left, MCSectionFlags Right) noexcept = default;
+
+private:
+ std::uint16_t Mask = 0;
+};
+
+struct MCSectionDesc final {
+ std::string Name;
+ MCSectionKind Kind = MCSectionKind::Custom;
+ MCSectionFlags Flags;
+ std::uint64_t Alignment = 1;
+ std::uint64_t EntrySize = 0;
+ MCSymbolRef Group;
+ std::optional<std::uint64_t> UniqueID;
+
+ [[nodiscard]] Expected<void> verify() const;
+
+ friend bool operator==(const MCSectionDesc &Left, const MCSectionDesc &Right) = default;
+};
+
+class MCSection final {
+public:
+ [[nodiscard]] std::string_view name() const noexcept {
+ return Description.Name;
+ }
+
+ [[nodiscard]] const MCSectionDesc &description() const noexcept {
+ return Description;
+ }
+
+private:
+ explicit MCSection(MCSectionDesc Description) : Description(std::move(Description)) {}
+
+ MCSectionDesc Description;
+
+ friend class MCContext;
+};
+
+using MCSectionRef = std::shared_ptr<const MCSection>;
+
+} // namespace rcc::mc
+
+#endif // RCC_MC_MCSECTION_H