| 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 // This file declares the representation of function declarations, | 10 // This file declares the representation of function declarations, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 public: | 36 public: |
| 37 /// Discriminator for LLVM-style RTTI. | 37 /// Discriminator for LLVM-style RTTI. |
| 38 enum GlobalDeclarationKind { | 38 enum GlobalDeclarationKind { |
| 39 FunctionDeclarationKind, | 39 FunctionDeclarationKind, |
| 40 VariableDeclarationKind | 40 VariableDeclarationKind |
| 41 }; | 41 }; |
| 42 GlobalDeclarationKind getKind() const { return Kind; } | 42 GlobalDeclarationKind getKind() const { return Kind; } |
| 43 const IceString &getName() const { return Name; } | 43 const IceString &getName() const { return Name; } |
| 44 void setName(const IceString &NewName) { Name = NewName; } | 44 void setName(const IceString &NewName) { Name = NewName; } |
| 45 bool hasName() const { return !Name.empty(); } | 45 bool hasName() const { return !Name.empty(); } |
| 46 bool isInternal() const { |
| 47 return Linkage == llvm::GlobalValue::InternalLinkage; |
| 48 } |
| 49 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; } |
| 50 bool isExternal() const { |
| 51 return Linkage == llvm::GlobalValue::ExternalLinkage; |
| 52 } |
| 53 void setLinkage(llvm::GlobalValue::LinkageTypes NewLinkage) { |
| 54 Linkage = NewLinkage; |
| 55 } |
| 46 virtual ~GlobalDeclaration() {} | 56 virtual ~GlobalDeclaration() {} |
| 47 | 57 |
| 48 /// Returns true if the declaration is external. | |
| 49 virtual bool getIsExternal() const = 0; | |
| 50 | |
| 51 /// Prints out type of the global declaration. | 58 /// Prints out type of the global declaration. |
| 52 virtual void dumpType(Ostream &Stream) const = 0; | 59 virtual void dumpType(Ostream &Stream) const = 0; |
| 53 | 60 |
| 54 /// Prints out the global declaration. | 61 /// Prints out the global declaration. |
| 55 virtual void dump(Ostream &Stream) const = 0; | 62 virtual void dump(GlobalContext *Ctx, Ostream &Stream) const = 0; |
| 63 void dump(Ostream &Stream) const { |
| 64 GlobalContext *const Ctx = nullptr; |
| 65 dump(Ctx, Stream); |
| 66 } |
| 67 |
| 68 /// Returns true if when emitting names, we should suppress mangling. |
| 69 virtual bool getSuppressMangling() const = 0; |
| 56 | 70 |
| 57 // Mangles name for cross tests, unless external and not defined locally | 71 // Mangles name for cross tests, unless external and not defined locally |
| 58 // (so that relocations accross llvm2ice and pnacl-llc will work). | 72 // (so that relocations accross llvm2ice and pnacl-llc will work). |
| 59 virtual IceString mangleName(GlobalContext *Ctx) const = 0; | 73 virtual IceString mangleName(GlobalContext *Ctx) const { |
| 74 return getSuppressMangling() ? Name : Ctx->mangleName(Name); |
| 75 } |
| 60 | 76 |
| 61 protected: | 77 protected: |
| 62 GlobalDeclaration(GlobalDeclarationKind Kind) : Kind(Kind) {} | 78 GlobalDeclaration(GlobalDeclarationKind Kind, |
| 79 llvm::GlobalValue::LinkageTypes Linkage) |
| 80 : Kind(Kind), Linkage(Linkage) {} |
| 63 | 81 |
| 64 const GlobalDeclarationKind Kind; | 82 const GlobalDeclarationKind Kind; |
| 65 IceString Name; | 83 IceString Name; |
| 84 llvm::GlobalValue::LinkageTypes Linkage; |
| 66 }; | 85 }; |
| 67 | 86 |
| 68 // Models a function declaration. This includes the type signature of | 87 // Models a function declaration. This includes the type signature of |
| 69 // the function, its calling conventions, and its linkage. | 88 // the function, its calling conventions, and its linkage. |
| 70 class FunctionDeclaration : public GlobalDeclaration { | 89 class FunctionDeclaration : public GlobalDeclaration { |
| 71 FunctionDeclaration(const FunctionDeclaration &) = delete; | 90 FunctionDeclaration(const FunctionDeclaration &) = delete; |
| 72 FunctionDeclaration &operator=(const FunctionDeclaration &) = delete; | 91 FunctionDeclaration &operator=(const FunctionDeclaration &) = delete; |
| 73 friend class GlobalContext; | 92 friend class GlobalContext; |
| 74 | 93 |
| 75 public: | 94 public: |
| 76 static FunctionDeclaration *create(GlobalContext *Ctx, | 95 static FunctionDeclaration *create(GlobalContext *Ctx, |
| 77 const FuncSigType &Signature, | 96 const FuncSigType &Signature, |
| 78 llvm::CallingConv::ID CallingConv, | 97 llvm::CallingConv::ID CallingConv, |
| 79 llvm::GlobalValue::LinkageTypes Linkage, | 98 llvm::GlobalValue::LinkageTypes Linkage, |
| 80 bool IsProto); | 99 bool IsProto); |
| 81 ~FunctionDeclaration() final {} | 100 ~FunctionDeclaration() final {} |
| 82 const FuncSigType &getSignature() const { return Signature; } | 101 const FuncSigType &getSignature() const { return Signature; } |
| 83 llvm::CallingConv::ID getCallingConv() const { return CallingConv; } | 102 llvm::CallingConv::ID getCallingConv() const { return CallingConv; } |
| 84 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; } | |
| 85 // isProto implies that there isn't a (local) definition for the function. | 103 // isProto implies that there isn't a (local) definition for the function. |
| 86 bool isProto() const { return IsProto; } | 104 bool isProto() const { return IsProto; } |
| 87 static bool classof(const GlobalDeclaration *Addr) { | 105 static bool classof(const GlobalDeclaration *Addr) { |
| 88 return Addr->getKind() == FunctionDeclarationKind; | 106 return Addr->getKind() == FunctionDeclarationKind; |
| 89 } | 107 } |
| 90 void dumpType(Ostream &Stream) const final; | 108 void dumpType(Ostream &Stream) const final; |
| 91 void dump(Ostream &Stream) const final; | 109 void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
| 92 bool getIsExternal() const final { | 110 bool getSuppressMangling() const final { |
| 93 return Linkage == llvm::GlobalValue::ExternalLinkage; | 111 return isExternal() && IsProto; |
| 94 } | |
| 95 | |
| 96 virtual IceString mangleName(GlobalContext *Ctx) const final { | |
| 97 return (getIsExternal() && IsProto) ? Name : Ctx->mangleName(Name); | |
| 98 } | 112 } |
| 99 | 113 |
| 100 private: | 114 private: |
| 101 const Ice::FuncSigType Signature; | 115 const Ice::FuncSigType Signature; |
| 102 llvm::CallingConv::ID CallingConv; | 116 llvm::CallingConv::ID CallingConv; |
| 103 llvm::GlobalValue::LinkageTypes Linkage; | |
| 104 bool IsProto; | 117 bool IsProto; |
| 105 | 118 |
| 106 FunctionDeclaration(const FuncSigType &Signature, | 119 FunctionDeclaration(const FuncSigType &Signature, |
| 107 llvm::CallingConv::ID CallingConv, | 120 llvm::CallingConv::ID CallingConv, |
| 108 llvm::GlobalValue::LinkageTypes Linkage, bool IsProto) | 121 llvm::GlobalValue::LinkageTypes Linkage, bool IsProto) |
| 109 : GlobalDeclaration(FunctionDeclarationKind), Signature(Signature), | 122 : GlobalDeclaration(FunctionDeclarationKind, Linkage), |
| 110 CallingConv(CallingConv), Linkage(Linkage), IsProto(IsProto) {} | 123 Signature(Signature), CallingConv(CallingConv), IsProto(IsProto) {} |
| 111 }; | 124 }; |
| 112 | 125 |
| 113 /// Models a global variable declaration, and its initializers. | 126 /// Models a global variable declaration, and its initializers. |
| 114 class VariableDeclaration : public GlobalDeclaration { | 127 class VariableDeclaration : public GlobalDeclaration { |
| 115 VariableDeclaration(const VariableDeclaration &) = delete; | 128 VariableDeclaration(const VariableDeclaration &) = delete; |
| 116 VariableDeclaration &operator=(const VariableDeclaration &) = delete; | 129 VariableDeclaration &operator=(const VariableDeclaration &) = delete; |
| 117 friend class GlobalContext; | 130 friend class GlobalContext; |
| 118 // TODO(kschimpf) Factor out allocation of initializers into the | 131 // TODO(kschimpf) Factor out allocation of initializers into the |
| 119 // global context, so that memory allocation/collection can be | 132 // global context, so that memory allocation/collection can be |
| 120 // optimized. | 133 // optimized. |
| 121 public: | 134 public: |
| 122 /// Base class for a global variable initializer. | 135 /// Base class for a global variable initializer. |
| 123 class Initializer { | 136 class Initializer { |
| 124 Initializer(const Initializer &) = delete; | 137 Initializer(const Initializer &) = delete; |
| 125 Initializer &operator=(const Initializer &) = delete; | 138 Initializer &operator=(const Initializer &) = delete; |
| 126 | 139 |
| 127 public: | 140 public: |
| 128 /// Discriminator for LLVM-style RTTI. | 141 /// Discriminator for LLVM-style RTTI. |
| 129 enum InitializerKind { | 142 enum InitializerKind { |
| 130 DataInitializerKind, | 143 DataInitializerKind, |
| 131 ZeroInitializerKind, | 144 ZeroInitializerKind, |
| 132 RelocInitializerKind | 145 RelocInitializerKind |
| 133 }; | 146 }; |
| 134 InitializerKind getKind() const { return Kind; } | 147 InitializerKind getKind() const { return Kind; } |
| 135 virtual ~Initializer() {} | 148 virtual ~Initializer() {} |
| 136 virtual SizeT getNumBytes() const = 0; | 149 virtual SizeT getNumBytes() const = 0; |
| 137 virtual void dump(Ostream &Stream) const = 0; | 150 virtual void dump(GlobalContext *Ctx, Ostream &Stream) const = 0; |
| 151 void dump(Ostream &Stream) const { |
| 152 dump(nullptr, Stream); |
| 153 } |
| 138 virtual void dumpType(Ostream &Stream) const; | 154 virtual void dumpType(Ostream &Stream) const; |
| 139 | 155 |
| 140 protected: | 156 protected: |
| 141 explicit Initializer(InitializerKind Kind) : Kind(Kind) {} | 157 explicit Initializer(InitializerKind Kind) : Kind(Kind) {} |
| 142 | 158 |
| 143 private: | 159 private: |
| 144 const InitializerKind Kind; | 160 const InitializerKind Kind; |
| 145 }; | 161 }; |
| 146 | 162 |
| 147 // Models the data in a data initializer. | 163 // Models the data in a data initializer. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 163 } | 179 } |
| 164 } | 180 } |
| 165 DataInitializer(const char *Str, size_t StrLen) | 181 DataInitializer(const char *Str, size_t StrLen) |
| 166 : Initializer(DataInitializerKind), Contents(StrLen) { | 182 : Initializer(DataInitializerKind), Contents(StrLen) { |
| 167 for (size_t i = 0; i < StrLen; ++i) | 183 for (size_t i = 0; i < StrLen; ++i) |
| 168 Contents[i] = static_cast<uint8_t>(Str[i]); | 184 Contents[i] = static_cast<uint8_t>(Str[i]); |
| 169 } | 185 } |
| 170 ~DataInitializer() override {} | 186 ~DataInitializer() override {} |
| 171 const DataVecType &getContents() const { return Contents; } | 187 const DataVecType &getContents() const { return Contents; } |
| 172 SizeT getNumBytes() const final { return Contents.size(); } | 188 SizeT getNumBytes() const final { return Contents.size(); } |
| 173 void dump(Ostream &Stream) const final; | 189 void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
| 174 static bool classof(const Initializer *D) { | 190 static bool classof(const Initializer *D) { |
| 175 return D->getKind() == DataInitializerKind; | 191 return D->getKind() == DataInitializerKind; |
| 176 } | 192 } |
| 177 | 193 |
| 178 private: | 194 private: |
| 179 // The byte contents of the data initializer. | 195 // The byte contents of the data initializer. |
| 180 DataVecType Contents; | 196 DataVecType Contents; |
| 181 }; | 197 }; |
| 182 | 198 |
| 183 /// Defines a sequence of bytes initialized to zero. | 199 /// Defines a sequence of bytes initialized to zero. |
| 184 class ZeroInitializer : public Initializer { | 200 class ZeroInitializer : public Initializer { |
| 185 ZeroInitializer(const ZeroInitializer &) = delete; | 201 ZeroInitializer(const ZeroInitializer &) = delete; |
| 186 ZeroInitializer &operator=(const ZeroInitializer &) = delete; | 202 ZeroInitializer &operator=(const ZeroInitializer &) = delete; |
| 187 | 203 |
| 188 public: | 204 public: |
| 189 explicit ZeroInitializer(SizeT Size) | 205 explicit ZeroInitializer(SizeT Size) |
| 190 : Initializer(ZeroInitializerKind), Size(Size) {} | 206 : Initializer(ZeroInitializerKind), Size(Size) {} |
| 191 ~ZeroInitializer() override {} | 207 ~ZeroInitializer() override {} |
| 192 SizeT getNumBytes() const final { return Size; } | 208 SizeT getNumBytes() const final { return Size; } |
| 193 void dump(Ostream &Stream) const final; | 209 void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
| 194 static bool classof(const Initializer *Z) { | 210 static bool classof(const Initializer *Z) { |
| 195 return Z->getKind() == ZeroInitializerKind; | 211 return Z->getKind() == ZeroInitializerKind; |
| 196 } | 212 } |
| 197 | 213 |
| 198 private: | 214 private: |
| 199 // The number of bytes to be zero initialized. | 215 // The number of bytes to be zero initialized. |
| 200 SizeT Size; | 216 SizeT Size; |
| 201 }; | 217 }; |
| 202 | 218 |
| 203 // Relocation address offsets must be 32 bit values. | 219 // Relocation address offsets must be 32 bit values. |
| 204 typedef int32_t RelocOffsetType; | 220 typedef int32_t RelocOffsetType; |
| 205 static const SizeT RelocAddrSize = 4; | 221 static const SizeT RelocAddrSize = 4; |
| 206 | 222 |
| 207 /// Defines the relocation value of another global declaration. | 223 /// Defines the relocation value of another global declaration. |
| 208 class RelocInitializer : public Initializer { | 224 class RelocInitializer : public Initializer { |
| 209 RelocInitializer(const RelocInitializer &) = delete; | 225 RelocInitializer(const RelocInitializer &) = delete; |
| 210 RelocInitializer &operator=(const RelocInitializer &) = delete; | 226 RelocInitializer &operator=(const RelocInitializer &) = delete; |
| 211 | 227 |
| 212 public: | 228 public: |
| 213 RelocInitializer(const GlobalDeclaration *Declaration, | 229 RelocInitializer(const GlobalDeclaration *Declaration, |
| 214 RelocOffsetType Offset) | 230 RelocOffsetType Offset) |
| 215 : Initializer(RelocInitializerKind), Declaration(Declaration), | 231 : Initializer(RelocInitializerKind), Declaration(Declaration), |
| 216 Offset(Offset) {} | 232 Offset(Offset) {} |
| 217 ~RelocInitializer() override {} | 233 ~RelocInitializer() override {} |
| 218 RelocOffsetType getOffset() const { return Offset; } | 234 RelocOffsetType getOffset() const { return Offset; } |
| 219 const GlobalDeclaration *getDeclaration() const { return Declaration; } | 235 const GlobalDeclaration *getDeclaration() const { return Declaration; } |
| 220 SizeT getNumBytes() const final { return RelocAddrSize; } | 236 SizeT getNumBytes() const final { return RelocAddrSize; } |
| 221 void dump(Ostream &Stream) const final; | 237 void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
| 222 void dumpType(Ostream &Stream) const final; | 238 void dumpType(Ostream &Stream) const final; |
| 223 static bool classof(const Initializer *R) { | 239 static bool classof(const Initializer *R) { |
| 224 return R->getKind() == RelocInitializerKind; | 240 return R->getKind() == RelocInitializerKind; |
| 225 } | 241 } |
| 226 | 242 |
| 227 private: | 243 private: |
| 228 // The global declaration used in the relocation. | 244 // The global declaration used in the relocation. |
| 229 const GlobalDeclaration *Declaration; | 245 const GlobalDeclaration *Declaration; |
| 230 // The offset to add to the relocation. | 246 // The offset to add to the relocation. |
| 231 const RelocOffsetType Offset; | 247 const RelocOffsetType Offset; |
| 232 }; | 248 }; |
| 233 | 249 |
| 234 /// Models the list of initializers. | 250 /// Models the list of initializers. |
| 235 typedef std::vector<Initializer *> InitializerListType; | 251 typedef std::vector<Initializer *> InitializerListType; |
| 236 | 252 |
| 237 static VariableDeclaration *create(GlobalContext *Ctx); | 253 static VariableDeclaration *create(GlobalContext *Ctx); |
| 238 ~VariableDeclaration() final; | 254 ~VariableDeclaration() final; |
| 239 | 255 |
| 240 const InitializerListType &getInitializers() const { return Initializers; } | 256 const InitializerListType &getInitializers() const { return Initializers; } |
| 241 bool getIsConstant() const { return IsConstant; } | 257 bool getIsConstant() const { return IsConstant; } |
| 242 void setIsConstant(bool NewValue) { IsConstant = NewValue; } | 258 void setIsConstant(bool NewValue) { IsConstant = NewValue; } |
| 243 uint32_t getAlignment() const { return Alignment; } | 259 uint32_t getAlignment() const { return Alignment; } |
| 244 void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; } | 260 void setAlignment(uint32_t NewAlignment) { Alignment = NewAlignment; } |
| 245 bool getIsInternal() const { return IsInternal; } | 261 bool hasInitializer() const { return !Initializers.empty(); } |
| 246 void setIsInternal(bool NewValue) { IsInternal = NewValue; } | 262 bool hasNonzeroInitializer() const { |
| 247 bool getIsExternal() const final { return !getIsInternal(); } | |
| 248 bool hasInitializer() const { | |
| 249 return !(Initializers.size() == 1 && | 263 return !(Initializers.size() == 1 && |
| 250 llvm::isa<ZeroInitializer>(Initializers[0])); | 264 llvm::isa<ZeroInitializer>(Initializers[0])); |
| 251 } | 265 } |
| 252 | 266 |
| 253 /// Returns the number of bytes for the initializer of the global | 267 /// Returns the number of bytes for the initializer of the global |
| 254 /// address. | 268 /// address. |
| 255 SizeT getNumBytes() const { | 269 SizeT getNumBytes() const { |
| 256 SizeT Count = 0; | 270 SizeT Count = 0; |
| 257 for (Initializer *Init : Initializers) { | 271 for (Initializer *Init : Initializers) { |
| 258 Count += Init->getNumBytes(); | 272 Count += Init->getNumBytes(); |
| 259 } | 273 } |
| 260 return Count; | 274 return Count; |
| 261 } | 275 } |
| 262 | 276 |
| 263 /// Adds Initializer to the list of initializers. Takes ownership of | 277 /// Adds Initializer to the list of initializers. Takes ownership of |
| 264 /// the initializer. | 278 /// the initializer. |
| 265 void addInitializer(Initializer *Initializer) { | 279 void addInitializer(Initializer *Initializer) { |
| 266 Initializers.push_back(Initializer); | 280 Initializers.push_back(Initializer); |
| 267 } | 281 } |
| 268 | 282 |
| 269 /// Prints out type for initializer associated with the declaration | 283 /// Prints out type for initializer associated with the declaration |
| 270 /// to Stream. | 284 /// to Stream. |
| 271 void dumpType(Ostream &Stream) const final; | 285 void dumpType(Ostream &Stream) const final; |
| 272 | 286 |
| 273 /// Prints out the definition of the global variable declaration | 287 /// Prints out the definition of the global variable declaration |
| 274 /// (including initialization). | 288 /// (including initialization). |
| 275 void dump(Ostream &Stream) const final; | 289 void dump(GlobalContext *Ctx, Ostream &Stream) const final; |
| 276 | 290 |
| 277 static bool classof(const GlobalDeclaration *Addr) { | 291 static bool classof(const GlobalDeclaration *Addr) { |
| 278 return Addr->getKind() == VariableDeclarationKind; | 292 return Addr->getKind() == VariableDeclarationKind; |
| 279 } | 293 } |
| 280 | 294 |
| 281 IceString mangleName(GlobalContext *Ctx) const final { | 295 bool getSuppressMangling() const final { |
| 282 return (getIsExternal() && !hasInitializer()) | 296 return isExternal() && !hasInitializer(); |
| 283 ? Name : Ctx->mangleName(Name); | |
| 284 } | 297 } |
| 285 | 298 |
| 286 | |
| 287 private: | 299 private: |
| 288 // list of initializers for the declared variable. | 300 // list of initializers for the declared variable. |
| 289 InitializerListType Initializers; | 301 InitializerListType Initializers; |
| 290 // The alignment of the declared variable. | 302 // The alignment of the declared variable. |
| 291 uint32_t Alignment; | 303 uint32_t Alignment; |
| 292 // True if a declared (global) constant. | 304 // True if a declared (global) constant. |
| 293 bool IsConstant; | 305 bool IsConstant; |
| 294 // True if the declaration is internal. | |
| 295 bool IsInternal; | |
| 296 | 306 |
| 297 VariableDeclaration() | 307 VariableDeclaration() |
| 298 : GlobalDeclaration(VariableDeclarationKind), Alignment(0), | 308 : GlobalDeclaration(VariableDeclarationKind, |
| 299 IsConstant(false), IsInternal(true) {} | 309 llvm::GlobalValue::InternalLinkage), |
| 310 Alignment(0), IsConstant(false) {} |
| 300 }; | 311 }; |
| 301 | 312 |
| 302 template <class StreamType> | 313 template <class StreamType> |
| 303 inline StreamType &operator<<(StreamType &Stream, | 314 inline StreamType &operator<<(StreamType &Stream, |
| 304 const VariableDeclaration::Initializer &Init) { | 315 const VariableDeclaration::Initializer &Init) { |
| 305 Init.dump(Stream); | 316 Init.dump(Stream); |
| 306 return Stream; | 317 return Stream; |
| 307 } | 318 } |
| 308 | 319 |
| 309 template <class StreamType> | 320 template <class StreamType> |
| 310 inline StreamType &operator<<(StreamType &Stream, | 321 inline StreamType &operator<<(StreamType &Stream, |
| 311 const GlobalDeclaration &Addr) { | 322 const GlobalDeclaration &Addr) { |
| 312 Addr.dump(Stream); | 323 Addr.dump(Stream); |
| 313 return Stream; | 324 return Stream; |
| 314 } | 325 } |
| 315 | 326 |
| 316 } // end of namespace Ice | 327 } // end of namespace Ice |
| 317 | 328 |
| 318 #endif // SUBZERO_SRC_ICEGLOBALINITS_H | 329 #endif // SUBZERO_SRC_ICEGLOBALINITS_H |
| OLD | NEW |