with the previous refactoring, fixme fixed!
[oota-llvm.git] / include / llvm / Target / TargetLoweringObjectFile.h
1 //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
16 #define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
17
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20
21 namespace llvm {
22   class MCSection;
23   class MCContext;
24   class GlobalValue;
25   class Mangler;
26   class TargetMachine;
27   
28 /// SectionKind - This is a simple POD value that classifies the properties of
29 /// a section.  A global variable is classified into the deepest possible
30 /// classification, and then the target maps them onto their sections based on
31 /// what capabilities they have.
32 ///
33 /// The comments below describe these as if they were an inheritance hierarchy
34 /// in order to explain the predicates below.
35 class SectionKind {
36 public:
37   enum Kind {
38     /// Metadata - Debug info sections or other metadata.
39     Metadata,
40     
41     /// Text - Text section, used for functions and other executable code.
42     Text,
43     
44     /// ReadOnly - Data that is never written to at program runtime by the
45     /// program or the dynamic linker.  Things in the top-level readonly
46     /// SectionKind are not mergeable.
47     ReadOnly,
48
49         /// MergeableCString - This is a special section for nul-terminated
50         /// strings.  The linker can unique the C strings, knowing their
51         /// semantics.  Because it uniques based on the nul terminators, the
52         /// compiler can't put strings in this section that have embeded nuls
53         /// in them.
54         MergeableCString,
55     
56         /// MergeableConst - These are sections for merging fixed-length
57         /// constants together.  For example, this can be used to unique
58         /// constant pool entries etc.
59         MergeableConst,
60     
61             /// MergeableConst4 - This is a section used by 4-byte constants,
62             /// for example, floats.
63             MergeableConst4,
64     
65             /// MergeableConst8 - This is a section used by 8-byte constants,
66             /// for example, doubles.
67             MergeableConst8,
68
69             /// MergeableConst16 - This is a section used by 16-byte constants,
70             /// for example, vectors.
71             MergeableConst16,
72     
73     /// Writeable - This is the base of all segments that need to be written
74     /// to during program runtime.
75     
76        /// ThreadLocal - This is the base of all TLS segments.  All TLS
77        /// objects must be writeable, otherwise there is no reason for them to
78        /// be thread local!
79     
80            /// ThreadBSS - Zero-initialized TLS data objects.
81            ThreadBSS,
82     
83            /// ThreadData - Initialized TLS data objects.
84            ThreadData,
85     
86        /// GlobalWriteableData - Writeable data that is global (not thread
87        /// local).
88     
89            /// BSS - Zero initialized writeable data.
90            BSS,
91
92            /// DataRel - This is the most general form of data that is written
93            /// to by the program, it can have random relocations to arbitrary
94            /// globals.
95            DataRel,
96
97                /// DataRelLocal - This is writeable data that has a non-zero
98                /// initializer and has relocations in it, but all of the
99                /// relocations are known to be within the final linked image
100                /// the global is linked into.
101                DataRelLocal,
102
103                    /// DataNoRel - This is writeable data that has a non-zero
104                    /// initializer, but whose initializer is known to have no
105                    /// relocations.
106                    DataNoRel,
107
108            /// ReadOnlyWithRel - These are global variables that are never
109            /// written to by the program, but that have relocations, so they
110            /// must be stuck in a writeable section so that the dynamic linker
111            /// can write to them.  If it chooses to, the dynamic linker can
112            /// mark the pages these globals end up on as read-only after it is
113            /// done with its relocation phase.
114            ReadOnlyWithRel,
115     
116                /// ReadOnlyWithRelLocal - This is data that is readonly by the
117                /// program, but must be writeable so that the dynamic linker
118                /// can perform relocations in it.  This is used when we know
119                /// that all the relocations are to globals in this final
120                /// linked image.
121                ReadOnlyWithRelLocal
122     
123   };
124   
125 protected:
126   Kind K : 6;
127   
128 public:
129   
130   bool isMetadata() const { return K == Metadata; }
131   bool isText() const { return K == Text; }
132   
133   bool isReadOnly() const {
134     return K == ReadOnly || K == MergeableCString || isMergeableConst();
135   }
136
137   bool isMergeableCString() const { return K == MergeableCString; }
138   bool isMergeableConst() const {
139     return K == MergeableConst || K == MergeableConst4 ||
140            K == MergeableConst8 || K == MergeableConst16;
141   }
142   
143   bool isMergeableConst4() const { return K == MergeableConst4; }
144   bool isMergeableConst8() const { return K == MergeableConst8; }
145   bool isMergeableConst16() const { return K == MergeableConst16; }
146   
147   bool isWriteable() const {
148     return isThreadLocal() || isGlobalWriteableData();
149   }
150   
151   bool isThreadLocal() const {
152     return K == ThreadData || K == ThreadBSS;
153   }
154   
155   bool isThreadBSS() const { return K == ThreadBSS; } 
156   bool isThreadData() const { return K == ThreadData; } 
157
158   bool isGlobalWriteableData() const {
159     return isBSS() || isDataRel() || isReadOnlyWithRel();
160   }
161   
162   bool isBSS() const { return K == BSS; }
163   
164   bool isDataRel() const {
165     return K == DataRel || K == DataRelLocal || K == DataNoRel;
166   }
167   
168   bool isDataRelLocal() const {
169     return K == DataRelLocal || K == DataNoRel;
170   }
171
172   bool isDataNoRel() const { return K == DataNoRel; }
173   
174   bool isReadOnlyWithRel() const {
175     return K == ReadOnlyWithRel || K == ReadOnlyWithRelLocal;
176   }
177
178   bool isReadOnlyWithRelLocal() const {
179     return K == ReadOnlyWithRelLocal;
180   }
181   
182   static SectionKind get(Kind K) {
183     SectionKind Res;
184     Res.K = K;
185     return Res;
186   }
187 };
188
189   
190 /// SectionInfo - This class is a target-independent classification of a global
191 /// which is used to simplify target-specific code by exposing common
192 /// predicates.
193 class SectionInfo : public SectionKind {
194   /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
195   /// weak, weak_odr, etc).  This is orthogonal from the categorization.
196   bool Weak : 1;
197   
198 public:
199   
200   /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
201   /// weak, weak_odr, etc).  This is orthogonal from the categorization.
202   bool isWeak() const { return Weak; }
203   
204   static SectionInfo get(Kind K, bool isWeak = false) {
205     SectionInfo Res;
206     Res.K = K;
207     Res.Weak = isWeak;
208     return Res;
209   }
210   static SectionInfo get(SectionKind K, bool isWeak = false) {
211     SectionInfo Res;
212     *(SectionKind*)&Res = K;
213     Res.Weak = isWeak;
214     return Res;
215   }
216 };
217   
218 class TargetLoweringObjectFile {
219   MCContext *Ctx;
220 protected:
221   
222   TargetLoweringObjectFile();
223   
224   /// TextSection - Section directive for standard text.
225   ///
226   const MCSection *TextSection;           // Defaults to ".text".
227   
228   /// DataSection - Section directive for standard data.
229   ///
230   const MCSection *DataSection;           // Defaults to ".data".
231   
232   
233   
234   // FIXME: SINK THESE.
235   const MCSection *BSSSection_;
236
237   /// ReadOnlySection - This is the directive that is emitted to switch to a
238   /// read-only section for constant data (e.g. data declared const,
239   /// jump tables).
240   const MCSection *ReadOnlySection;       // Defaults to NULL
241   
242   /// TLSDataSection - Section directive for Thread Local data.
243   ///
244   const MCSection *TLSDataSection;        // Defaults to ".tdata".
245   
246   /// TLSBSSSection - Section directive for Thread Local uninitialized data.
247   /// Null if this target doesn't support a BSS section.
248   ///
249   const MCSection *TLSBSSSection;         // Defaults to ".tbss".
250   
251   const MCSection *CStringSection_;
252   
253 public:
254   // FIXME: NONPUB.
255   const MCSection *getOrCreateSection(const char *Name,
256                                       bool isDirective,
257                                       SectionKind K) const;
258 public:
259   
260   virtual ~TargetLoweringObjectFile();
261   
262   /// Initialize - this method must be called before any actual lowering is
263   /// done.  This specifies the current context for codegen, and gives the
264   /// lowering implementations a chance to set up their default sections.
265   virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
266     Ctx = &ctx;
267   }
268   
269   
270   const MCSection *getTextSection() const { return TextSection; }
271   const MCSection *getDataSection() const { return DataSection; }
272   
273   /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
274   /// decide not to emit the UsedDirective for some symbols in llvm.used.
275   /// FIXME: REMOVE this (rdar://7071300)
276   virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
277                                           Mangler *) const {
278     return (GV!=0);
279   }
280   
281   /// getSectionForMergeableConstant - Given a mergeable constant with the
282   /// specified size and relocation information, return a section that it
283   /// should be placed in.
284   virtual const MCSection *
285   getSectionForMergeableConstant(SectionKind Kind) const;
286   
287   /// getKindForNamedSection - If this target wants to be able to override
288   /// section flags based on the name of the section specified for a global
289   /// variable, it can implement this.  This is used on ELF systems so that
290   /// ".tbss" gets the TLS bit set etc.
291   virtual SectionKind getKindForNamedSection(const char *Section,
292                                              SectionKind K) const {
293     return K;
294   }
295   
296   /// SectionForGlobal - This method computes the appropriate section to emit
297   /// the specified global variable or function definition.  This should not
298   /// be passed external (or available externally) globals.
299   const MCSection *SectionForGlobal(const GlobalValue *GV,
300                                     Mangler *Mang,
301                                     const TargetMachine &TM) const;
302   
303   /// getSpecialCasedSectionGlobals - Allow the target to completely override
304   /// section assignment of a global.
305   /// FIXME: ELIMINATE this by making PIC16 implement ADDRESS with
306   /// getFlagsForNamedSection.
307   virtual const MCSection *
308   getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
309                                 SectionInfo Kind) const {
310     return 0;
311   }
312   
313   /// getSectionFlagsAsString - Turn the flags in the specified SectionKind
314   /// into a string that can be printed to the assembly file after the
315   /// ".section foo" part of a section directive.
316   virtual void getSectionFlagsAsString(SectionKind Kind,
317                                        SmallVectorImpl<char> &Str) const {
318   }
319   
320 protected:
321   virtual const MCSection *
322   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
323                          Mangler *Mang, const TargetMachine &TM) const;
324 };
325   
326   
327   
328
329 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
330   bool AtIsCommentChar;  // True if @ is the comment character on this target.
331   bool HasCrazyBSS;
332 public:
333   /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
334   /// is "@".
335   TargetLoweringObjectFileELF(bool atIsCommentChar = false,
336                               // FIXME: REMOVE AFTER UNIQUING IS FIXED.
337                               bool hasCrazyBSS = false)
338     : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
339     
340   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
341   
342   
343   /// getSectionForMergeableConstant - Given a mergeable constant with the
344   /// specified size and relocation information, return a section that it
345   /// should be placed in.
346   virtual const MCSection *
347   getSectionForMergeableConstant(SectionKind Kind) const;
348   
349   virtual SectionKind getKindForNamedSection(const char *Section,
350                                              SectionKind K) const;
351   void getSectionFlagsAsString(SectionKind Kind,
352                                SmallVectorImpl<char> &Str) const;
353   
354   virtual const MCSection *
355   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
356                          Mangler *Mang, const TargetMachine &TM) const;
357 protected:
358   const MCSection *DataRelSection;
359   const MCSection *DataRelLocalSection;
360   const MCSection *DataRelROSection;
361   const MCSection *DataRelROLocalSection;
362   
363   const MCSection *MergeableConst4Section;
364   const MCSection *MergeableConst8Section;
365   const MCSection *MergeableConst16Section;
366 };
367
368   
369   
370 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
371   const MCSection *TextCoalSection;
372   const MCSection *ConstTextCoalSection;
373   const MCSection *ConstDataCoalSection;
374   const MCSection *ConstDataSection;
375   const MCSection *DataCoalSection;
376   const MCSection *FourByteConstantSection;
377   const MCSection *EightByteConstantSection;
378   const MCSection *SixteenByteConstantSection;
379 public:
380   
381   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
382
383   virtual const MCSection *
384   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
385                          Mangler *Mang, const TargetMachine &TM) const;
386   
387   virtual const MCSection *
388   getSectionForMergeableConstant(SectionKind Kind) const;
389   
390   /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
391   /// decide not to emit the UsedDirective for some symbols in llvm.used.
392   /// FIXME: REMOVE this (rdar://7071300)
393   virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
394                                           Mangler *) const;
395 };
396
397
398
399 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
400 public:
401   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
402   
403   virtual void getSectionFlagsAsString(SectionKind Kind,
404                                        SmallVectorImpl<char> &Str) const;
405   
406   virtual const MCSection *
407   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
408                          Mangler *Mang, const TargetMachine &TM) const;
409 };
410
411 } // end namespace llvm
412
413 #endif