Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceGlobalInits.h - Global declarations -------*- C++ -*-===// |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// This file declares the representation of function declarations, |
| 12 | /// global variable declarations, and the corresponding variable |
| 13 | /// initializers in Subzero. Global variable initializers are |
| 14 | /// represented as a sequence of simple initializers. |
| 15 | /// |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef SUBZERO_SRC_ICEGLOBALINITS_H |
| 19 | #define SUBZERO_SRC_ICEGLOBALINITS_H |
| 20 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 21 | #include "IceDefs.h" |
| 22 | #include "IceGlobalContext.h" |
| 23 | #include "IceTypes.h" |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 24 | |
| 25 | #pragma clang diagnostic push |
| 26 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Andrew Scull | 2c688f6 | 2015-09-09 11:56:10 -0700 | [diff] [blame] | 27 | #pragma clang diagnostic ignored "-Wredundant-move" |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 28 | #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" // for NaClBitcodeRecord. |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 29 | #include "llvm/IR/CallingConv.h" |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 30 | #include "llvm/IR/GlobalValue.h" // for GlobalValue::LinkageTypes. |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 31 | #pragma clang diagnostic pop |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 32 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 33 | #include <memory> |
| 34 | #include <utility> |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 35 | |
| 36 | // TODO(kschimpf): Remove ourselves from using LLVM representation for calling |
| 37 | // conventions and linkage types. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 38 | |
| 39 | namespace Ice { |
| 40 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 41 | /// Base class for global variable and function declarations. |
| 42 | class GlobalDeclaration { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 43 | GlobalDeclaration() = delete; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 44 | GlobalDeclaration(const GlobalDeclaration &) = delete; |
| 45 | GlobalDeclaration &operator=(const GlobalDeclaration &) = delete; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 46 | |
| 47 | public: |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 48 | /// Discriminator for LLVM-style RTTI. |
| 49 | enum GlobalDeclarationKind { |
| 50 | FunctionDeclarationKind, |
| 51 | VariableDeclarationKind |
| 52 | }; |
| 53 | GlobalDeclarationKind getKind() const { return Kind; } |
| 54 | const IceString &getName() const { return Name; } |
| 55 | void setName(const IceString &NewName) { Name = NewName; } |
| 56 | bool hasName() const { return !Name.empty(); } |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 57 | bool isInternal() const { |
| 58 | return Linkage == llvm::GlobalValue::InternalLinkage; |
| 59 | } |
| 60 | llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; } |
| 61 | bool isExternal() const { |
| 62 | return Linkage == llvm::GlobalValue::ExternalLinkage; |
| 63 | } |
| 64 | void setLinkage(llvm::GlobalValue::LinkageTypes NewLinkage) { |
| 65 | Linkage = NewLinkage; |
| 66 | } |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 67 | virtual ~GlobalDeclaration() = default; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 68 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 69 | /// Prints out type of the global declaration. |
| 70 | virtual void dumpType(Ostream &Stream) const = 0; |
| 71 | |
| 72 | /// Prints out the global declaration. |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 73 | virtual void dump(GlobalContext *Ctx, Ostream &Stream) const = 0; |
| 74 | void dump(Ostream &Stream) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 75 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 76 | return; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 77 | GlobalContext *const Ctx = nullptr; |
| 78 | dump(Ctx, Stream); |
| 79 | } |
| 80 | |
| 81 | /// Returns true if when emitting names, we should suppress mangling. |
| 82 | virtual bool getSuppressMangling() const = 0; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 83 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 84 | /// Mangles name for cross tests, unless external and not defined locally |
| 85 | /// (so that relocations accross pnacl-sz and pnacl-llc will work). |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 86 | virtual IceString mangleName(GlobalContext *Ctx) const { |
| 87 | return getSuppressMangling() ? Name : Ctx->mangleName(Name); |
| 88 | } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 89 | |
| 90 | protected: |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 91 | GlobalDeclaration(GlobalDeclarationKind Kind, |
| 92 | llvm::GlobalValue::LinkageTypes Linkage) |
| 93 | : Kind(Kind), Linkage(Linkage) {} |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 94 | |
| 95 | const GlobalDeclarationKind Kind; |
| 96 | IceString Name; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 97 | llvm::GlobalValue::LinkageTypes Linkage; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 100 | /// Models a function declaration. This includes the type signature of |
| 101 | /// the function, its calling conventions, and its linkage. |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 102 | class FunctionDeclaration : public GlobalDeclaration { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 103 | FunctionDeclaration() = delete; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 104 | FunctionDeclaration(const FunctionDeclaration &) = delete; |
| 105 | FunctionDeclaration &operator=(const FunctionDeclaration &) = delete; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 106 | |
| 107 | public: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 108 | static FunctionDeclaration *create(GlobalContext *Context, |
| 109 | const FuncSigType &Signature, |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 110 | llvm::CallingConv::ID CallingConv, |
| 111 | llvm::GlobalValue::LinkageTypes Linkage, |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 112 | bool IsProto) { |
| 113 | return new (Context->allocate<FunctionDeclaration>()) |
| 114 | FunctionDeclaration(Signature, CallingConv, Linkage, IsProto); |
| 115 | } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 116 | const FuncSigType &getSignature() const { return Signature; } |
| 117 | llvm::CallingConv::ID getCallingConv() const { return CallingConv; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 118 | /// isProto implies that there isn't a (local) definition for the function. |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 119 | bool isProto() const { return IsProto; } |
| 120 | static bool classof(const GlobalDeclaration *Addr) { |
| 121 | return Addr->getKind() == FunctionDeclarationKind; |
| 122 | } |
| 123 | void dumpType(Ostream &Stream) const final; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 124 | void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 125 | bool getSuppressMangling() const final { return isExternal() && IsProto; } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 126 | |
| 127 | private: |
| 128 | const Ice::FuncSigType Signature; |
| 129 | llvm::CallingConv::ID CallingConv; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 130 | bool IsProto; |
| 131 | |
| 132 | FunctionDeclaration(const FuncSigType &Signature, |
| 133 | llvm::CallingConv::ID CallingConv, |
| 134 | llvm::GlobalValue::LinkageTypes Linkage, bool IsProto) |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 135 | : GlobalDeclaration(FunctionDeclarationKind, Linkage), |
| 136 | Signature(Signature), CallingConv(CallingConv), IsProto(IsProto) {} |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /// Models a global variable declaration, and its initializers. |
| 140 | class VariableDeclaration : public GlobalDeclaration { |
| 141 | VariableDeclaration(const VariableDeclaration &) = delete; |
| 142 | VariableDeclaration &operator=(const VariableDeclaration &) = delete; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 143 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 144 | public: |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 145 | /// Base class for a global variable initializer. |
| 146 | class Initializer { |
| 147 | Initializer(const Initializer &) = delete; |
| 148 | Initializer &operator=(const Initializer &) = delete; |
| 149 | |
| 150 | public: |
| 151 | /// Discriminator for LLVM-style RTTI. |
| 152 | enum InitializerKind { |
| 153 | DataInitializerKind, |
| 154 | ZeroInitializerKind, |
| 155 | RelocInitializerKind |
| 156 | }; |
| 157 | InitializerKind getKind() const { return Kind; } |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 158 | virtual ~Initializer() = default; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 159 | virtual SizeT getNumBytes() const = 0; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 160 | virtual void dump(GlobalContext *Ctx, Ostream &Stream) const = 0; |
| 161 | void dump(Ostream &Stream) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 162 | if (BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 163 | dump(nullptr, Stream); |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 164 | } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 165 | virtual void dumpType(Ostream &Stream) const; |
| 166 | |
| 167 | protected: |
| 168 | explicit Initializer(InitializerKind Kind) : Kind(Kind) {} |
| 169 | |
| 170 | private: |
| 171 | const InitializerKind Kind; |
| 172 | }; |
| 173 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 174 | /// Models the data in a data initializer. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 175 | using DataVecType = std::vector<char>; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 176 | |
| 177 | /// Defines a sequence of byte values as a data initializer. |
| 178 | class DataInitializer : public Initializer { |
| 179 | DataInitializer(const DataInitializer &) = delete; |
| 180 | DataInitializer &operator=(const DataInitializer &) = delete; |
| 181 | |
| 182 | public: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 183 | template <class... Args> |
| 184 | static std::unique_ptr<DataInitializer> create(Args &&... TheArgs) { |
| 185 | return makeUnique<DataInitializer>(std::forward<Args>(TheArgs)...); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 186 | } |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 187 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 188 | const DataVecType &getContents() const { return Contents; } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 189 | SizeT getNumBytes() const final { return Contents.size(); } |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 190 | void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 191 | static bool classof(const Initializer *D) { |
| 192 | return D->getKind() == DataInitializerKind; |
| 193 | } |
| 194 | |
| 195 | private: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 196 | ENABLE_MAKE_UNIQUE; |
| 197 | |
| 198 | DataInitializer(const llvm::NaClBitcodeRecord::RecordVector &Values) |
| 199 | : Initializer(DataInitializerKind), Contents(Values.size()) { |
| 200 | for (SizeT I = 0; I < Values.size(); ++I) |
| 201 | Contents[I] = static_cast<int8_t>(Values[I]); |
| 202 | } |
| 203 | |
| 204 | DataInitializer(const char *Str, size_t StrLen) |
| 205 | : Initializer(DataInitializerKind), Contents(StrLen) { |
| 206 | for (size_t i = 0; i < StrLen; ++i) |
| 207 | Contents[i] = Str[i]; |
| 208 | } |
| 209 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 210 | /// The byte contents of the data initializer. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 211 | DataVecType Contents; |
| 212 | }; |
| 213 | |
| 214 | /// Defines a sequence of bytes initialized to zero. |
| 215 | class ZeroInitializer : public Initializer { |
| 216 | ZeroInitializer(const ZeroInitializer &) = delete; |
| 217 | ZeroInitializer &operator=(const ZeroInitializer &) = delete; |
| 218 | |
| 219 | public: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 220 | static std::unique_ptr<ZeroInitializer> create(SizeT Size) { |
| 221 | return makeUnique<ZeroInitializer>(Size); |
| 222 | } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 223 | SizeT getNumBytes() const final { return Size; } |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 224 | void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 225 | static bool classof(const Initializer *Z) { |
| 226 | return Z->getKind() == ZeroInitializerKind; |
| 227 | } |
| 228 | |
| 229 | private: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 230 | ENABLE_MAKE_UNIQUE; |
| 231 | |
| 232 | explicit ZeroInitializer(SizeT Size) |
| 233 | : Initializer(ZeroInitializerKind), Size(Size) {} |
| 234 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 235 | /// The number of bytes to be zero initialized. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 236 | SizeT Size; |
| 237 | }; |
| 238 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 239 | /// Defines the relocation value of another global declaration. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 240 | class RelocInitializer : public Initializer { |
| 241 | RelocInitializer(const RelocInitializer &) = delete; |
| 242 | RelocInitializer &operator=(const RelocInitializer &) = delete; |
| 243 | |
| 244 | public: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 245 | static std::unique_ptr<RelocInitializer> |
| 246 | create(const GlobalDeclaration *Declaration, RelocOffsetT Offset) { |
| 247 | return makeUnique<RelocInitializer>(Declaration, Offset); |
| 248 | } |
| 249 | |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 250 | RelocOffsetT getOffset() const { return Offset; } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 251 | const GlobalDeclaration *getDeclaration() const { return Declaration; } |
| 252 | SizeT getNumBytes() const final { return RelocAddrSize; } |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 253 | void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 254 | void dumpType(Ostream &Stream) const final; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 255 | static bool classof(const Initializer *R) { |
| 256 | return R->getKind() == RelocInitializerKind; |
| 257 | } |
| 258 | |
| 259 | private: |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 260 | ENABLE_MAKE_UNIQUE; |
| 261 | |
| 262 | RelocInitializer(const GlobalDeclaration *Declaration, RelocOffsetT Offset) |
| 263 | : Initializer(RelocInitializerKind), Declaration(Declaration), |
| 264 | Offset(Offset) {} // The global declaration used in the relocation. |
| 265 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 266 | const GlobalDeclaration *Declaration; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 267 | /// The offset to add to the relocation. |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 268 | const RelocOffsetT Offset; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | /// Models the list of initializers. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 272 | using InitializerListType = std::vector<std::unique_ptr<Initializer>>; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 273 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 274 | static VariableDeclaration *create(GlobalContext *Context) { |
| 275 | return new (Context->allocate<VariableDeclaration>()) VariableDeclaration(); |
| 276 | } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 277 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 278 | const InitializerListType &getInitializers() const { return *Initializers; } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 279 | bool getIsConstant() const { return IsConstant; } |
| 280 | void setIsConstant(bool NewValue) { IsConstant = NewValue; } |
| 281 | uint32_t getAlignment() const { return Alignment; } |
| 282 | void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; } |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 283 | bool hasInitializer() const { return HasInitializer; } |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 284 | bool hasNonzeroInitializer() const { |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 285 | return !(Initializers->size() == 1 && |
| 286 | llvm::isa<ZeroInitializer>((*Initializers)[0].get())); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 287 | } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 288 | |
| 289 | /// Returns the number of bytes for the initializer of the global |
| 290 | /// address. |
| 291 | SizeT getNumBytes() const { |
| 292 | SizeT Count = 0; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 293 | for (const std::unique_ptr<Initializer> &Init : *Initializers) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 294 | Count += Init->getNumBytes(); |
| 295 | } |
| 296 | return Count; |
| 297 | } |
| 298 | |
| 299 | /// Adds Initializer to the list of initializers. Takes ownership of |
| 300 | /// the initializer. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 301 | void addInitializer(std::unique_ptr<Initializer> Initializer) { |
| 302 | Initializers->emplace_back(std::move(Initializer)); |
| 303 | HasInitializer = true; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 306 | /// Prints out type for initializer associated with the declaration |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 307 | /// to Stream. |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 308 | void dumpType(Ostream &Stream) const final; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 309 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 310 | /// Prints out the definition of the global variable declaration |
| 311 | /// (including initialization). |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 312 | void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 313 | |
| 314 | static bool classof(const GlobalDeclaration *Addr) { |
| 315 | return Addr->getKind() == VariableDeclarationKind; |
| 316 | } |
| 317 | |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 318 | bool getSuppressMangling() const final { |
Jim Stichnoth | d9f1f9f | 2015-06-11 10:19:32 -0700 | [diff] [blame] | 319 | if (ForceSuppressMangling) |
| 320 | return true; |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 321 | return isExternal() && !hasInitializer(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Jim Stichnoth | 0933c0c | 2015-06-12 10:41:16 -0700 | [diff] [blame] | 324 | void setSuppressMangling() { ForceSuppressMangling = true; } |
Jim Stichnoth | d9f1f9f | 2015-06-11 10:19:32 -0700 | [diff] [blame] | 325 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 326 | void discardInitializers() { Initializers = nullptr; } |
| 327 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 328 | private: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 329 | /// List of initializers for the declared variable. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 330 | std::unique_ptr<InitializerListType> Initializers; |
| 331 | bool HasInitializer; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 332 | /// The alignment of the declared variable. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 333 | uint32_t Alignment; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 334 | /// True if a declared (global) constant. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 335 | bool IsConstant; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 336 | /// If set to true, force getSuppressMangling() to return true. |
Jim Stichnoth | d9f1f9f | 2015-06-11 10:19:32 -0700 | [diff] [blame] | 337 | bool ForceSuppressMangling; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 338 | |
| 339 | VariableDeclaration() |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 340 | : GlobalDeclaration(VariableDeclarationKind, |
| 341 | llvm::GlobalValue::InternalLinkage), |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 342 | Initializers(new InitializerListType), HasInitializer(false), |
Jim Stichnoth | d9f1f9f | 2015-06-11 10:19:32 -0700 | [diff] [blame] | 343 | Alignment(0), IsConstant(false), ForceSuppressMangling(false) {} |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | template <class StreamType> |
| 347 | inline StreamType &operator<<(StreamType &Stream, |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 348 | const VariableDeclaration::Initializer &Init) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 349 | Init.dump(Stream); |
| 350 | return Stream; |
| 351 | } |
| 352 | |
| 353 | template <class StreamType> |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 354 | inline StreamType &operator<<(StreamType &Stream, |
| 355 | const GlobalDeclaration &Addr) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 356 | Addr.dump(Stream); |
| 357 | return Stream; |
| 358 | } |
| 359 | |
| 360 | } // end of namespace Ice |
| 361 | |
| 362 | #endif // SUBZERO_SRC_ICEGLOBALINITS_H |