aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/GlobalObject.h
blob: 798dcd854d2407de5c4220ab0b6063189fc5bd81 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_GLOBALOBJECT_H
#define RCC_CODEGEN_GLOBALOBJECT_H

#include "rcc/CodeGen/GenericConstant.h"
#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/TargetConstant.h"
#include "rcc/Support/Error.h"

#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>

namespace rcc {

class GlobalInitializer;
using GlobalInitializerRef = std::shared_ptr<const GlobalInitializer>;

struct GlobalZeroFill final {
  std::uint64_t Size = 0;
};

struct GlobalRepeatedInitializer final {
  std::uint64_t Count = 0;
  GlobalInitializerRef Value;
};

struct GlobalAggregateInitializer final {
  std::vector<GlobalInitializerRef> Elements;
};

struct GlobalSymbolAddress final {
  std::string Symbol;
  std::int64_t Addend = 0;
  std::uint8_t Size = 8;
  std::uint32_t TargetFlags = 0;
};

struct GlobalSymbolDifference final {
  std::string Minuend;
  std::string Subtrahend;
  std::int64_t Addend = 0;
  std::uint8_t Size = 8;
};

struct GlobalAlignmentPadding final {
  std::uint64_t Size = 0;
  std::uint8_t Fill = 0;
};

struct GlobalUndefinedStorage final {
  std::uint64_t Size = 0;
};

class GlobalInitializer final {
public:
  enum class Kind : std::uint8_t {
    Invalid,
    Integer,
    Floating,
    Bytes,
    ZeroFill,
    Repeated,
    Aggregate,
    SymbolAddress,
    SymbolDifference,
    Target,
    AlignmentPadding,
    UndefinedStorage,
  };

  GlobalInitializer() = default;
  GlobalInitializer(const GlobalInitializer &) = delete;
  GlobalInitializer &operator=(const GlobalInitializer &) = delete;
  GlobalInitializer(GlobalInitializer &&) noexcept = default;
  GlobalInitializer &operator=(GlobalInitializer &&) noexcept = default;
  ~GlobalInitializer() = default;

  [[nodiscard]] static GlobalInitializer integer(APInt Value);
  [[nodiscard]] static GlobalInitializer floating(APFloat Value);
  [[nodiscard]] static Expected<GlobalInitializer> bytes(std::vector<std::byte> Value);
  [[nodiscard]] static Expected<GlobalInitializer> zeroFill(std::uint64_t Size);
  [[nodiscard]] static Expected<GlobalInitializer> repeated(std::uint64_t Count,
                                                            GlobalInitializer Value);
  [[nodiscard]] static Expected<GlobalInitializer>
  aggregate(std::vector<GlobalInitializer> Elements);
  [[nodiscard]] static Expected<GlobalInitializer> symbolAddress(GlobalSymbolAddress Value);
  [[nodiscard]] static Expected<GlobalInitializer> symbolDifference(GlobalSymbolDifference Value);
  [[nodiscard]] static Expected<GlobalInitializer> target(TargetConstant Value);
  [[nodiscard]] static Expected<GlobalInitializer> alignmentPadding(std::uint64_t Size,
                                                                    std::uint8_t Fill = 0);
  [[nodiscard]] static Expected<GlobalInitializer> undefinedStorage(std::uint64_t Size);

  [[nodiscard]] Kind kind() const noexcept;

  template <typename ValueT> [[nodiscard]] const ValueT *getIf() const noexcept {
    return std::get_if<ValueT>(&Storage);
  }

  [[nodiscard]] Expected<void> verify(std::size_t MaximumDepth = 1024) const;
  [[nodiscard]] Expected<std::uint64_t> storageSize(std::size_t MaximumDepth = 1024) const;

private:
  using StorageT =
      std::variant<std::monostate, APInt, APFloat, std::vector<std::byte>, GlobalZeroFill,
                   GlobalRepeatedInitializer, GlobalAggregateInitializer, GlobalSymbolAddress,
                   GlobalSymbolDifference, TargetConstant, GlobalAlignmentPadding,
                   GlobalUndefinedStorage>;

  explicit GlobalInitializer(StorageT Storage) : Storage(std::move(Storage)) {}

  StorageT Storage;
};

enum class GlobalLinkage : std::uint8_t {
  External,
  AvailableExternally,
  LinkOnceAny,
  LinkOnceODR,
  WeakAny,
  WeakODR,
  Appending,
  Internal,
  Private,
  ExternalWeak,
  Common,
  Count,
};

enum class GlobalVisibility : std::uint8_t {
  Default,
  Hidden,
  Protected,
  Count,
};

enum class GlobalTLSModel : std::uint8_t {
  None,
  GeneralDynamic,
  LocalDynamic,
  InitialExec,
  LocalExec,
  Count,
};

enum class GlobalUnnamedAddress : std::uint8_t {
  None,
  Local,
  Global,
  Count,
};

struct GlobalObjectDesc final {
  std::string Symbol;
  GenericConstantType ObjectType;
  GlobalLinkage Linkage = GlobalLinkage::External;
  GlobalVisibility Visibility = GlobalVisibility::Default;
  bool DSOLocal = false;
  std::optional<std::string> Section;
  Alignment Align;
  std::uint64_t Size = 0;
  std::uint32_t AddressSpace = 0;
  GlobalTLSModel TLSModel = GlobalTLSModel::None;
  bool ExternallyInitialized = false;
  std::optional<std::string> COMDAT;
  bool IsConstant = false;
  GlobalUnnamedAddress UnnamedAddress = GlobalUnnamedAddress::None;
  std::optional<GlobalInitializer> Initializer;
  std::vector<std::string> DebugExpressions;
  std::optional<OriginID> StableOrigin;
  std::uint64_t TargetFlags = 0;
  std::vector<std::pair<std::string, std::string>> TargetAttributes;

  GlobalObjectDesc() = default;
  GlobalObjectDesc(const GlobalObjectDesc &) = delete;
  GlobalObjectDesc &operator=(const GlobalObjectDesc &) = delete;
  GlobalObjectDesc(GlobalObjectDesc &&) noexcept = default;
  GlobalObjectDesc &operator=(GlobalObjectDesc &&) noexcept = default;
  ~GlobalObjectDesc() = default;

  [[nodiscard]] bool isDeclaration() const noexcept {
    return !Initializer.has_value() && Linkage != GlobalLinkage::Common;
  }

  [[nodiscard]] Expected<void> verify() const;
};

} // namespace rcc

#endif // RCC_CODEGEN_GLOBALOBJECT_H