| OLD | NEW |
| 1 //===- subzero/src/IceGlobalInits.h - Global declarations -------*- C++ -*-===// | 1 //===- subzero/src/IceGlobalInits.h - Global declarations -------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| 11 /// \brief Declares the representation of function declarations, global variable | 11 /// \brief Declares the representation of function declarations, global variable |
| 12 /// declarations, and the corresponding variable initializers in Subzero. | 12 /// declarations, and the corresponding variable initializers in Subzero. |
| 13 /// | 13 /// |
| 14 /// Global variable initializers are represented as a sequence of simple | 14 /// Global variable initializers are represented as a sequence of simple |
| 15 /// initializers. | 15 /// initializers. |
| 16 /// | 16 /// |
| 17 //===----------------------------------------------------------------------===// | 17 //===----------------------------------------------------------------------===// |
| 18 | 18 |
| 19 #ifndef SUBZERO_SRC_ICEGLOBALINITS_H | 19 #ifndef SUBZERO_SRC_ICEGLOBALINITS_H |
| 20 #define SUBZERO_SRC_ICEGLOBALINITS_H | 20 #define SUBZERO_SRC_ICEGLOBALINITS_H |
| 21 | 21 |
| 22 #include "IceDefs.h" | 22 #include "IceDefs.h" |
| 23 #include "IceFixups.h" | 23 #include "IceFixups.h" |
| 24 #include "IceGlobalContext.h" | 24 #include "IceGlobalContext.h" |
| 25 #include "IceIntrinsics.h" | 25 #include "IceIntrinsics.h" |
| 26 #include "IceMangling.h" |
| 26 #include "IceOperand.h" | 27 #include "IceOperand.h" |
| 27 #include "IceTypes.h" | 28 #include "IceTypes.h" |
| 28 | 29 |
| 29 #ifdef __clang__ | 30 #ifdef __clang__ |
| 30 #pragma clang diagnostic push | 31 #pragma clang diagnostic push |
| 31 #pragma clang diagnostic ignored "-Wunused-parameter" | 32 #pragma clang diagnostic ignored "-Wunused-parameter" |
| 32 #pragma clang diagnostic ignored "-Wredundant-move" | 33 #pragma clang diagnostic ignored "-Wredundant-move" |
| 33 #endif // __clang__ | 34 #endif // __clang__ |
| 34 | 35 |
| 35 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" // for NaClBitcodeRecord. | 36 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" // for NaClBitcodeRecord. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 55 GlobalDeclaration &operator=(const GlobalDeclaration &) = delete; | 56 GlobalDeclaration &operator=(const GlobalDeclaration &) = delete; |
| 56 | 57 |
| 57 public: | 58 public: |
| 58 /// Discriminator for LLVM-style RTTI. | 59 /// Discriminator for LLVM-style RTTI. |
| 59 enum GlobalDeclarationKind { | 60 enum GlobalDeclarationKind { |
| 60 FunctionDeclarationKind, | 61 FunctionDeclarationKind, |
| 61 VariableDeclarationKind | 62 VariableDeclarationKind |
| 62 }; | 63 }; |
| 63 GlobalDeclarationKind getKind() const { return Kind; } | 64 GlobalDeclarationKind getKind() const { return Kind; } |
| 64 const IceString &getName() const { return Name; } | 65 const IceString &getName() const { return Name; } |
| 65 void setName(const IceString &NewName) { Name = NewName; } | 66 void setName(const IceString &NewName) { |
| 67 Name = getSuppressMangling() ? NewName : mangleName(NewName); |
| 68 } |
| 66 bool hasName() const { return !Name.empty(); } | 69 bool hasName() const { return !Name.empty(); } |
| 67 bool isInternal() const { | 70 bool isInternal() const { |
| 68 return Linkage == llvm::GlobalValue::InternalLinkage; | 71 return Linkage == llvm::GlobalValue::InternalLinkage; |
| 69 } | 72 } |
| 70 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; } | 73 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; } |
| 71 bool isExternal() const { | 74 bool isExternal() const { |
| 72 return Linkage == llvm::GlobalValue::ExternalLinkage; | 75 return Linkage == llvm::GlobalValue::ExternalLinkage; |
| 73 } | 76 } |
| 74 void setLinkage(llvm::GlobalValue::LinkageTypes NewLinkage) { | |
| 75 Linkage = NewLinkage; | |
| 76 } | |
| 77 virtual ~GlobalDeclaration() = default; | 77 virtual ~GlobalDeclaration() = default; |
| 78 | 78 |
| 79 /// Prints out type of the global declaration. | 79 /// Prints out type of the global declaration. |
| 80 virtual void dumpType(Ostream &Stream) const = 0; | 80 virtual void dumpType(Ostream &Stream) const = 0; |
| 81 | 81 |
| 82 /// Prints out the global declaration. | 82 /// Prints out the global declaration. |
| 83 virtual void dump(GlobalContext *Ctx, Ostream &Stream) const = 0; | 83 virtual void dump(Ostream &Stream) const = 0; |
| 84 void dump(Ostream &Stream) const { | |
| 85 if (!BuildDefs::dump()) | |
| 86 return; | |
| 87 GlobalContext *const Ctx = nullptr; | |
| 88 dump(Ctx, Stream); | |
| 89 } | |
| 90 | 84 |
| 91 /// Returns true if when emitting names, we should suppress mangling. | 85 /// Returns true if when emitting names, we should suppress mangling. |
| 92 virtual bool getSuppressMangling() const = 0; | 86 virtual bool getSuppressMangling() const = 0; |
| 93 | 87 |
| 94 /// Mangles name for cross tests, unless external and not defined locally (so | |
| 95 /// that relocations across pnacl-sz and pnacl-llc will work). | |
| 96 virtual IceString mangleName(GlobalContext *Ctx) const { | |
| 97 return getSuppressMangling() ? Name : Ctx->mangleName(Name); | |
| 98 } | |
| 99 | |
| 100 /// Returns textual name of linkage. | 88 /// Returns textual name of linkage. |
| 101 const char *getLinkageName() const { | 89 const char *getLinkageName() const { |
| 102 return isInternal() ? "internal" : "external"; | 90 return isInternal() ? "internal" : "external"; |
| 103 } | 91 } |
| 104 | 92 |
| 105 protected: | 93 protected: |
| 106 GlobalDeclaration(GlobalDeclarationKind Kind, | 94 GlobalDeclaration(GlobalDeclarationKind Kind, |
| 107 llvm::GlobalValue::LinkageTypes Linkage) | 95 llvm::GlobalValue::LinkageTypes Linkage) |
| 108 : Kind(Kind), Linkage(Linkage) {} | 96 : Kind(Kind), Linkage(Linkage) {} |
| 109 | 97 |
| 110 /// Returns true if linkage is defined correctly for the global declaration, | 98 /// Returns true if linkage is defined correctly for the global declaration, |
| 111 /// based on default rules. | 99 /// based on default rules. |
| 112 bool verifyLinkageDefault(const GlobalContext *Ctx) const { | 100 bool verifyLinkageDefault(const GlobalContext *Ctx) const { |
| 113 switch (Linkage) { | 101 switch (Linkage) { |
| 114 default: | 102 default: |
| 115 return false; | 103 return false; |
| 116 case llvm::GlobalValue::InternalLinkage: | 104 case llvm::GlobalValue::InternalLinkage: |
| 117 return true; | 105 return true; |
| 118 case llvm::GlobalValue::ExternalLinkage: | 106 case llvm::GlobalValue::ExternalLinkage: |
| 119 return Ctx->getFlags().getAllowExternDefinedSymbols(); | 107 return Ctx->getFlags().getAllowExternDefinedSymbols(); |
| 120 } | 108 } |
| 121 } | 109 } |
| 122 | 110 |
| 123 const GlobalDeclarationKind Kind; | 111 const GlobalDeclarationKind Kind; |
| 112 const llvm::GlobalValue::LinkageTypes Linkage; |
| 124 IceString Name; | 113 IceString Name; |
| 125 llvm::GlobalValue::LinkageTypes Linkage; | |
| 126 }; | 114 }; |
| 127 | 115 |
| 128 /// Models a function declaration. This includes the type signature of the | 116 /// Models a function declaration. This includes the type signature of the |
| 129 /// function, its calling conventions, and its linkage. | 117 /// function, its calling conventions, and its linkage. |
| 130 class FunctionDeclaration : public GlobalDeclaration { | 118 class FunctionDeclaration : public GlobalDeclaration { |
| 131 FunctionDeclaration() = delete; | 119 FunctionDeclaration() = delete; |
| 132 FunctionDeclaration(const FunctionDeclaration &) = delete; | 120 FunctionDeclaration(const FunctionDeclaration &) = delete; |
| 133 FunctionDeclaration &operator=(const FunctionDeclaration &) = delete; | 121 FunctionDeclaration &operator=(const FunctionDeclaration &) = delete; |
| 134 | 122 |
| 135 public: | 123 public: |
| 136 static FunctionDeclaration *create(GlobalContext *Context, | 124 static FunctionDeclaration *create(GlobalContext *Context, |
| 137 const FuncSigType &Signature, | 125 const FuncSigType &Signature, |
| 138 llvm::CallingConv::ID CallingConv, | 126 llvm::CallingConv::ID CallingConv, |
| 139 llvm::GlobalValue::LinkageTypes Linkage, | 127 llvm::GlobalValue::LinkageTypes Linkage, |
| 140 bool IsProto) { | 128 bool IsProto) { |
| 141 return new (Context->allocate<FunctionDeclaration>()) | 129 return new (Context->allocate<FunctionDeclaration>()) |
| 142 FunctionDeclaration(Signature, CallingConv, Linkage, IsProto); | 130 FunctionDeclaration(Signature, CallingConv, Linkage, IsProto); |
| 143 } | 131 } |
| 144 const FuncSigType &getSignature() const { return Signature; } | 132 const FuncSigType &getSignature() const { return Signature; } |
| 145 llvm::CallingConv::ID getCallingConv() const { return CallingConv; } | 133 llvm::CallingConv::ID getCallingConv() const { return CallingConv; } |
| 146 /// isProto implies that there isn't a (local) definition for the function. | 134 /// isProto implies that there isn't a (local) definition for the function. |
| 147 bool isProto() const { return IsProto; } | 135 bool isProto() const { return IsProto; } |
| 148 static bool classof(const GlobalDeclaration *Addr) { | 136 static bool classof(const GlobalDeclaration *Addr) { |
| 149 return Addr->getKind() == FunctionDeclarationKind; | 137 return Addr->getKind() == FunctionDeclarationKind; |
| 150 } | 138 } |
| 151 void dumpType(Ostream &Stream) const final; | 139 void dumpType(Ostream &Stream) const final; |
| 152 void dump(GlobalContext *Ctx, Ostream &Stream) const final; | 140 void dump(Ostream &Stream) const final; |
| 153 bool getSuppressMangling() const final { return isExternal() && IsProto; } | 141 bool getSuppressMangling() const final { return isExternal() && IsProto; } |
| 154 | 142 |
| 155 /// Returns true if linkage is correct for the function declaration. | 143 /// Returns true if linkage is correct for the function declaration. |
| 156 bool verifyLinkageCorrect(const GlobalContext *Ctx) const { | 144 bool verifyLinkageCorrect(const GlobalContext *Ctx) const { |
| 157 if (isPNaClABIExternalName() || isIntrinsicName(Ctx)) | 145 if (isPNaClABIExternalName() || isIntrinsicName(Ctx)) |
| 158 return Linkage == llvm::GlobalValue::ExternalLinkage; | 146 return Linkage == llvm::GlobalValue::ExternalLinkage; |
| 159 return verifyLinkageDefault(Ctx); | 147 return verifyLinkageDefault(Ctx); |
| 160 } | 148 } |
| 161 | 149 |
| 162 /// Validates that the type signature of the function is correct. Returns true | 150 /// Validates that the type signature of the function is correct. Returns true |
| (...skipping 18 matching lines...) Expand all Loading... |
| 181 } | 169 } |
| 182 | 170 |
| 183 /// Same as above, except IsIntrinsic is true if the function is intrinsic | 171 /// Same as above, except IsIntrinsic is true if the function is intrinsic |
| 184 /// (even if not a PNaCl intrinsic). | 172 /// (even if not a PNaCl intrinsic). |
| 185 const Intrinsics::FullIntrinsicInfo * | 173 const Intrinsics::FullIntrinsicInfo * |
| 186 getIntrinsicInfo(const GlobalContext *Ctx, bool *IsIntrinsic) const; | 174 getIntrinsicInfo(const GlobalContext *Ctx, bool *IsIntrinsic) const; |
| 187 | 175 |
| 188 private: | 176 private: |
| 189 const Ice::FuncSigType Signature; | 177 const Ice::FuncSigType Signature; |
| 190 llvm::CallingConv::ID CallingConv; | 178 llvm::CallingConv::ID CallingConv; |
| 191 bool IsProto; | 179 const bool IsProto; |
| 192 | 180 |
| 193 FunctionDeclaration(const FuncSigType &Signature, | 181 FunctionDeclaration(const FuncSigType &Signature, |
| 194 llvm::CallingConv::ID CallingConv, | 182 llvm::CallingConv::ID CallingConv, |
| 195 llvm::GlobalValue::LinkageTypes Linkage, bool IsProto) | 183 llvm::GlobalValue::LinkageTypes Linkage, bool IsProto) |
| 196 : GlobalDeclaration(FunctionDeclarationKind, Linkage), | 184 : GlobalDeclaration(FunctionDeclarationKind, Linkage), |
| 197 Signature(Signature), CallingConv(CallingConv), IsProto(IsProto) {} | 185 Signature(Signature), CallingConv(CallingConv), IsProto(IsProto) {} |
| 198 | 186 |
| 199 bool isPNaClABIExternalName() const { | 187 bool isPNaClABIExternalName() const { |
| 200 const char *Name = getName().c_str(); | 188 const char *Name = getName().c_str(); |
| 201 return strcmp(Name, "_start") == 0 || strcmp(Name, "__pnacl_pso_root") == 0; | 189 return strcmp(Name, "_start") == 0 || strcmp(Name, "__pnacl_pso_root") == 0; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 227 public: | 215 public: |
| 228 /// Discriminator for LLVM-style RTTI. | 216 /// Discriminator for LLVM-style RTTI. |
| 229 enum InitializerKind { | 217 enum InitializerKind { |
| 230 DataInitializerKind, | 218 DataInitializerKind, |
| 231 ZeroInitializerKind, | 219 ZeroInitializerKind, |
| 232 RelocInitializerKind | 220 RelocInitializerKind |
| 233 }; | 221 }; |
| 234 InitializerKind getKind() const { return Kind; } | 222 InitializerKind getKind() const { return Kind; } |
| 235 virtual ~Initializer() = default; | 223 virtual ~Initializer() = default; |
| 236 virtual SizeT getNumBytes() const = 0; | 224 virtual SizeT getNumBytes() const = 0; |
| 237 virtual void dump(GlobalContext *Ctx, Ostream &Stream) const = 0; | 225 virtual void dump(Ostream &Stream) const = 0; |
| 238 void dump(Ostream &Stream) const { | |
| 239 if (BuildDefs::dump()) | |
| 240 dump(nullptr, Stream); | |
| 241 } | |
| 242 virtual void dumpType(Ostream &Stream) const; | 226 virtual void dumpType(Ostream &Stream) const; |
| 243 | 227 |
| 244 protected: | 228 protected: |
| 245 explicit Initializer(InitializerKind Kind) : Kind(Kind) {} | 229 explicit Initializer(InitializerKind Kind) : Kind(Kind) {} |
| 246 | 230 |
| 247 private: | 231 private: |
| 248 const InitializerKind Kind; | 232 const InitializerKind Kind; |
| 249 }; | 233 }; |
| 250 | 234 |
| 251 /// Models the data in a data initializer. | 235 /// Models the data in a data initializer. |
| 252 using DataVecType = std::vector<char>; | 236 using DataVecType = std::vector<char>; |
| 253 | 237 |
| 254 /// Defines a sequence of byte values as a data initializer. | 238 /// Defines a sequence of byte values as a data initializer. |
| 255 class DataInitializer : public Initializer { | 239 class DataInitializer : public Initializer { |
| 256 DataInitializer(const DataInitializer &) = delete; | 240 DataInitializer(const DataInitializer &) = delete; |
| 257 DataInitializer &operator=(const DataInitializer &) = delete; | 241 DataInitializer &operator=(const DataInitializer &) = delete; |
| 258 | 242 |
| 259 public: | 243 public: |
| 260 template <class... Args> | 244 template <class... Args> |
| 261 static std::unique_ptr<DataInitializer> create(Args &&... TheArgs) { | 245 static std::unique_ptr<DataInitializer> create(Args &&... TheArgs) { |
| 262 return makeUnique<DataInitializer>(std::forward<Args>(TheArgs)...); | 246 return makeUnique<DataInitializer>(std::forward<Args>(TheArgs)...); |
| 263 } | 247 } |
| 264 | 248 |
| 265 const DataVecType &getContents() const { return Contents; } | 249 const DataVecType &getContents() const { return Contents; } |
| 266 SizeT getNumBytes() const final { return Contents.size(); } | 250 SizeT getNumBytes() const final { return Contents.size(); } |
| 267 void dump(GlobalContext *Ctx, Ostream &Stream) const final; | 251 void dump(Ostream &Stream) const final; |
| 268 static bool classof(const Initializer *D) { | 252 static bool classof(const Initializer *D) { |
| 269 return D->getKind() == DataInitializerKind; | 253 return D->getKind() == DataInitializerKind; |
| 270 } | 254 } |
| 271 | 255 |
| 272 private: | 256 private: |
| 273 ENABLE_MAKE_UNIQUE; | 257 ENABLE_MAKE_UNIQUE; |
| 274 | 258 |
| 275 DataInitializer(const llvm::NaClBitcodeRecord::RecordVector &Values) | 259 DataInitializer(const llvm::NaClBitcodeRecord::RecordVector &Values) |
| 276 : Initializer(DataInitializerKind), Contents(Values.size()) { | 260 : Initializer(DataInitializerKind), Contents(Values.size()) { |
| 277 for (SizeT I = 0; I < Values.size(); ++I) | 261 for (SizeT I = 0; I < Values.size(); ++I) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 291 /// Defines a sequence of bytes initialized to zero. | 275 /// Defines a sequence of bytes initialized to zero. |
| 292 class ZeroInitializer : public Initializer { | 276 class ZeroInitializer : public Initializer { |
| 293 ZeroInitializer(const ZeroInitializer &) = delete; | 277 ZeroInitializer(const ZeroInitializer &) = delete; |
| 294 ZeroInitializer &operator=(const ZeroInitializer &) = delete; | 278 ZeroInitializer &operator=(const ZeroInitializer &) = delete; |
| 295 | 279 |
| 296 public: | 280 public: |
| 297 static std::unique_ptr<ZeroInitializer> create(SizeT Size) { | 281 static std::unique_ptr<ZeroInitializer> create(SizeT Size) { |
| 298 return makeUnique<ZeroInitializer>(Size); | 282 return makeUnique<ZeroInitializer>(Size); |
| 299 } | 283 } |
| 300 SizeT getNumBytes() const final { return Size; } | 284 SizeT getNumBytes() const final { return Size; } |
| 301 void dump(GlobalContext *Ctx, Ostream &Stream) const final; | 285 void dump(Ostream &Stream) const final; |
| 302 static bool classof(const Initializer *Z) { | 286 static bool classof(const Initializer *Z) { |
| 303 return Z->getKind() == ZeroInitializerKind; | 287 return Z->getKind() == ZeroInitializerKind; |
| 304 } | 288 } |
| 305 | 289 |
| 306 private: | 290 private: |
| 307 ENABLE_MAKE_UNIQUE; | 291 ENABLE_MAKE_UNIQUE; |
| 308 | 292 |
| 309 explicit ZeroInitializer(SizeT Size) | 293 explicit ZeroInitializer(SizeT Size) |
| 310 : Initializer(ZeroInitializerKind), Size(Size) {} | 294 : Initializer(ZeroInitializerKind), Size(Size) {} |
| 311 | 295 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 } | 327 } |
| 344 | 328 |
| 345 bool hasFixup() const { return HasFixup; } | 329 bool hasFixup() const { return HasFixup; } |
| 346 FixupKind getFixup() const { | 330 FixupKind getFixup() const { |
| 347 assert(HasFixup); | 331 assert(HasFixup); |
| 348 return Fixup; | 332 return Fixup; |
| 349 } | 333 } |
| 350 | 334 |
| 351 const GlobalDeclaration *getDeclaration() const { return Declaration; } | 335 const GlobalDeclaration *getDeclaration() const { return Declaration; } |
| 352 SizeT getNumBytes() const final { return RelocAddrSize; } | 336 SizeT getNumBytes() const final { return RelocAddrSize; } |
| 353 void dump(GlobalContext *Ctx, Ostream &Stream) const final; | 337 void dump(Ostream &Stream) const final; |
| 354 void dumpType(Ostream &Stream) const final; | 338 void dumpType(Ostream &Stream) const final; |
| 355 static bool classof(const Initializer *R) { | 339 static bool classof(const Initializer *R) { |
| 356 return R->getKind() == RelocInitializerKind; | 340 return R->getKind() == RelocInitializerKind; |
| 357 } | 341 } |
| 358 | 342 |
| 359 private: | 343 private: |
| 360 ENABLE_MAKE_UNIQUE; | 344 ENABLE_MAKE_UNIQUE; |
| 361 | 345 |
| 362 RelocInitializer(const GlobalDeclaration *Declaration, | 346 RelocInitializer(const GlobalDeclaration *Declaration, |
| 363 const RelocOffsetArray &OffsetExpr, bool HasFixup, | 347 const RelocOffsetArray &OffsetExpr, bool HasFixup, |
| 364 FixupKind Fixup = 0) | 348 FixupKind Fixup = 0) |
| 365 : Initializer(RelocInitializerKind), | 349 : Initializer(RelocInitializerKind), |
| 366 Declaration(Declaration), // The global declaration used in the reloc. | 350 Declaration(Declaration), // The global declaration used in the reloc. |
| 367 OffsetExpr(OffsetExpr), HasFixup(HasFixup), Fixup(Fixup) {} | 351 OffsetExpr(OffsetExpr), HasFixup(HasFixup), Fixup(Fixup) {} |
| 368 | 352 |
| 369 const GlobalDeclaration *Declaration; | 353 const GlobalDeclaration *Declaration; |
| 370 /// The offset to add to the relocation. | 354 /// The offset to add to the relocation. |
| 371 const RelocOffsetArray OffsetExpr; | 355 const RelocOffsetArray OffsetExpr; |
| 372 const bool HasFixup = false; | 356 const bool HasFixup = false; |
| 373 const FixupKind Fixup = 0; | 357 const FixupKind Fixup = 0; |
| 374 }; | 358 }; |
| 375 | 359 |
| 376 /// Models the list of initializers. | 360 /// Models the list of initializers. |
| 377 using InitializerListType = std::vector<std::unique_ptr<Initializer>>; | 361 using InitializerListType = std::vector<std::unique_ptr<Initializer>>; |
| 378 | 362 |
| 379 static VariableDeclaration *create(GlobalContext *Context) { | 363 static VariableDeclaration *create(GlobalContext *Context, |
| 380 return new (Context->allocate<VariableDeclaration>()) VariableDeclaration(); | 364 bool SuppressMangling = false, |
| 365 llvm::GlobalValue::LinkageTypes Linkage = |
| 366 llvm::GlobalValue::InternalLinkage) { |
| 367 return new (Context->allocate<VariableDeclaration>()) |
| 368 VariableDeclaration(Linkage, SuppressMangling); |
| 369 } |
| 370 static VariableDeclaration *createExternal(GlobalContext *Context) { |
| 371 constexpr bool SuppressMangling = true; |
| 372 constexpr llvm::GlobalValue::LinkageTypes Linkage = |
| 373 llvm::GlobalValue::ExternalLinkage; |
| 374 return create(Context, SuppressMangling, Linkage); |
| 381 } | 375 } |
| 382 | 376 |
| 383 const InitializerListType &getInitializers() const { return *Initializers; } | 377 const InitializerListType &getInitializers() const { return *Initializers; } |
| 384 bool getIsConstant() const { return IsConstant; } | 378 bool getIsConstant() const { return IsConstant; } |
| 385 void setIsConstant(bool NewValue) { IsConstant = NewValue; } | 379 void setIsConstant(bool NewValue) { IsConstant = NewValue; } |
| 386 uint32_t getAlignment() const { return Alignment; } | 380 uint32_t getAlignment() const { return Alignment; } |
| 387 void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; } | 381 void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; } |
| 388 bool hasInitializer() const { return HasInitializer; } | 382 bool hasInitializer() const { return HasInitializer; } |
| 389 bool hasNonzeroInitializer() const { | 383 bool hasNonzeroInitializer() const { |
| 390 return !(Initializers->size() == 1 && | 384 return !(Initializers->size() == 1 && |
| 391 llvm::isa<ZeroInitializer>((*Initializers)[0].get())); | 385 llvm::isa<ZeroInitializer>((*Initializers)[0].get())); |
| 392 } | 386 } |
| 393 | 387 |
| 394 /// Returns the number of bytes for the initializer of the global address. | 388 /// Returns the number of bytes for the initializer of the global address. |
| 395 SizeT getNumBytes() const { | 389 SizeT getNumBytes() const { |
| 396 SizeT Count = 0; | 390 SizeT Count = 0; |
| 397 for (const std::unique_ptr<Initializer> &Init : *Initializers) { | 391 for (const std::unique_ptr<Initializer> &Init : *Initializers) { |
| 398 Count += Init->getNumBytes(); | 392 Count += Init->getNumBytes(); |
| 399 } | 393 } |
| 400 return Count; | 394 return Count; |
| 401 } | 395 } |
| 402 | 396 |
| 403 /// Adds Initializer to the list of initializers. Takes ownership of the | 397 /// Adds Initializer to the list of initializers. Takes ownership of the |
| 404 /// initializer. | 398 /// initializer. |
| 405 void addInitializer(std::unique_ptr<Initializer> Initializer) { | 399 void addInitializer(std::unique_ptr<Initializer> Initializer) { |
| 400 const bool OldSuppressMangling = getSuppressMangling(); |
| 406 Initializers->emplace_back(std::move(Initializer)); | 401 Initializers->emplace_back(std::move(Initializer)); |
| 407 HasInitializer = true; | 402 HasInitializer = true; |
| 403 // The getSuppressMangling() logic depends on whether the global variable |
| 404 // has initializers. If its value changed as a result of adding an |
| 405 // initializer, then make sure we haven't previously set the name based on |
| 406 // faulty SuppressMangling logic. |
| 407 const bool SameMangling = (OldSuppressMangling == getSuppressMangling()); |
| 408 (void)SameMangling; |
| 409 assert(!Name.empty() || SameMangling); |
| 408 } | 410 } |
| 409 | 411 |
| 410 /// Prints out type for initializer associated with the declaration to Stream. | 412 /// Prints out type for initializer associated with the declaration to Stream. |
| 411 void dumpType(Ostream &Stream) const final; | 413 void dumpType(Ostream &Stream) const final; |
| 412 | 414 |
| 413 /// Prints out the definition of the global variable declaration (including | 415 /// Prints out the definition of the global variable declaration (including |
| 414 /// initialization). | 416 /// initialization). |
| 415 void dump(GlobalContext *Ctx, Ostream &Stream) const final; | 417 virtual void dump(Ostream &Stream) const; |
| 416 | 418 |
| 417 /// Returns true if linkage is correct for the variable declaration. | 419 /// Returns true if linkage is correct for the variable declaration. |
| 418 bool verifyLinkageCorrect(const GlobalContext *Ctx) const { | 420 bool verifyLinkageCorrect(const GlobalContext *Ctx) const { |
| 419 return verifyLinkageDefault(Ctx); | 421 return verifyLinkageDefault(Ctx); |
| 420 } | 422 } |
| 421 | 423 |
| 422 static bool classof(const GlobalDeclaration *Addr) { | 424 static bool classof(const GlobalDeclaration *Addr) { |
| 423 return Addr->getKind() == VariableDeclarationKind; | 425 return Addr->getKind() == VariableDeclarationKind; |
| 424 } | 426 } |
| 425 | 427 |
| 426 bool getSuppressMangling() const final { | 428 bool getSuppressMangling() const final { |
| 427 if (ForceSuppressMangling) | 429 if (ForceSuppressMangling) |
| 428 return true; | 430 return true; |
| 429 return isExternal() && !hasInitializer(); | 431 return isExternal() && !hasInitializer(); |
| 430 } | 432 } |
| 431 | 433 |
| 432 void setSuppressMangling() { ForceSuppressMangling = true; } | |
| 433 | |
| 434 void discardInitializers() { Initializers = nullptr; } | 434 void discardInitializers() { Initializers = nullptr; } |
| 435 | 435 |
| 436 private: | 436 private: |
| 437 /// List of initializers for the declared variable. | 437 /// List of initializers for the declared variable. |
| 438 std::unique_ptr<InitializerListType> Initializers; | 438 std::unique_ptr<InitializerListType> Initializers; |
| 439 bool HasInitializer; | 439 bool HasInitializer = false; |
| 440 /// The alignment of the declared variable. | 440 /// The alignment of the declared variable. |
| 441 uint32_t Alignment; | 441 uint32_t Alignment = 0; |
| 442 /// True if a declared (global) constant. | 442 /// True if a declared (global) constant. |
| 443 bool IsConstant; | 443 bool IsConstant = false; |
| 444 /// If set to true, force getSuppressMangling() to return true. | 444 /// If set to true, force getSuppressMangling() to return true. |
| 445 bool ForceSuppressMangling; | 445 const bool ForceSuppressMangling; |
| 446 | 446 |
| 447 VariableDeclaration() | 447 VariableDeclaration(llvm::GlobalValue::LinkageTypes Linkage, |
| 448 : GlobalDeclaration(VariableDeclarationKind, | 448 bool SuppressMangling) |
| 449 llvm::GlobalValue::InternalLinkage), | 449 : GlobalDeclaration(VariableDeclarationKind, Linkage), |
| 450 Initializers(new InitializerListType), HasInitializer(false), | 450 Initializers(new InitializerListType), |
| 451 Alignment(0), IsConstant(false), ForceSuppressMangling(false) {} | 451 ForceSuppressMangling(SuppressMangling) {} |
| 452 }; | 452 }; |
| 453 | 453 |
| 454 template <class StreamType> | 454 template <class StreamType> |
| 455 inline StreamType &operator<<(StreamType &Stream, | 455 inline StreamType &operator<<(StreamType &Stream, |
| 456 const VariableDeclaration::Initializer &Init) { | 456 const VariableDeclaration::Initializer &Init) { |
| 457 Init.dump(Stream); | 457 Init.dump(Stream); |
| 458 return Stream; | 458 return Stream; |
| 459 } | 459 } |
| 460 | 460 |
| 461 template <class StreamType> | 461 template <class StreamType> |
| 462 inline StreamType &operator<<(StreamType &Stream, | 462 inline StreamType &operator<<(StreamType &Stream, |
| 463 const GlobalDeclaration &Addr) { | 463 const GlobalDeclaration &Addr) { |
| 464 Addr.dump(Stream); | 464 Addr.dump(Stream); |
| 465 return Stream; | 465 return Stream; |
| 466 } | 466 } |
| 467 | 467 |
| 468 } // end of namespace Ice | 468 } // end of namespace Ice |
| 469 | 469 |
| 470 #endif // SUBZERO_SRC_ICEGLOBALINITS_H | 470 #endif // SUBZERO_SRC_ICEGLOBALINITS_H |
| OLD | NEW |