aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/MC/MCSection.h
blob: 4f7642781a52f2eb5040a040d9cb1d9ee6fc4792 (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
// 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