Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: src/IceGlobalInits.h

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

Powered by Google App Engine
This is Rietveld 408576698