8727aefc6e63b796fd1112e2afaa9c20a0d9e066
[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 #include "llvm/MC/SectionKind.h"
21
22 namespace llvm {
23   class MCSection;
24   class MCContext;
25   class GlobalValue;
26   class Mangler;
27   class TargetMachine;
28   
29   
30 /// SectionInfo - This class is a target-independent classification of a global
31 /// which is used to simplify target-specific code by exposing common
32 /// predicates.
33 class SectionInfo : public SectionKind {
34   /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
35   /// weak, weak_odr, etc).  This is orthogonal from the categorization.
36   bool Weak : 1;
37   
38 public:
39   
40   /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
41   /// weak, weak_odr, etc).  This is orthogonal from the categorization.
42   bool isWeak() const { return Weak; }
43   
44   static SectionInfo get(Kind K, bool isWeak = false) {
45     SectionInfo Res;
46     Res.K = K;
47     Res.Weak = isWeak;
48     return Res;
49   }
50   static SectionInfo get(SectionKind K, bool isWeak = false) {
51     SectionInfo Res;
52     *(SectionKind*)&Res = K;
53     Res.Weak = isWeak;
54     return Res;
55   }
56 };
57   
58 class TargetLoweringObjectFile {
59   MCContext *Ctx;
60 protected:
61   
62   TargetLoweringObjectFile();
63   
64   /// TextSection - Section directive for standard text.
65   ///
66   const MCSection *TextSection;           // Defaults to ".text".
67   
68   /// DataSection - Section directive for standard data.
69   ///
70   const MCSection *DataSection;           // Defaults to ".data".
71   
72   
73   
74   // FIXME: SINK THESE.
75   const MCSection *BSSSection_;
76
77   /// ReadOnlySection - This is the directive that is emitted to switch to a
78   /// read-only section for constant data (e.g. data declared const,
79   /// jump tables).
80   const MCSection *ReadOnlySection;       // Defaults to NULL
81   
82   /// TLSDataSection - Section directive for Thread Local data.
83   ///
84   const MCSection *TLSDataSection;        // Defaults to ".tdata".
85   
86   /// TLSBSSSection - Section directive for Thread Local uninitialized data.
87   /// Null if this target doesn't support a BSS section.
88   ///
89   const MCSection *TLSBSSSection;         // Defaults to ".tbss".
90   
91   const MCSection *CStringSection_;
92   
93 public:
94   // FIXME: NONPUB.
95   const MCSection *getOrCreateSection(const char *Name,
96                                       bool isDirective,
97                                       SectionKind K) const;
98 public:
99   
100   virtual ~TargetLoweringObjectFile();
101   
102   /// Initialize - this method must be called before any actual lowering is
103   /// done.  This specifies the current context for codegen, and gives the
104   /// lowering implementations a chance to set up their default sections.
105   virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
106     Ctx = &ctx;
107   }
108   
109   
110   const MCSection *getTextSection() const { return TextSection; }
111   const MCSection *getDataSection() const { return DataSection; }
112   
113   /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
114   /// decide not to emit the UsedDirective for some symbols in llvm.used.
115   /// FIXME: REMOVE this (rdar://7071300)
116   virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
117                                           Mangler *) const {
118     return (GV!=0);
119   }
120   
121   /// getSectionForMergeableConstant - Given a mergeable constant with the
122   /// specified size and relocation information, return a section that it
123   /// should be placed in.
124   virtual const MCSection *
125   getSectionForMergeableConstant(SectionKind Kind) const;
126   
127   /// getKindForNamedSection - If this target wants to be able to override
128   /// section flags based on the name of the section specified for a global
129   /// variable, it can implement this.  This is used on ELF systems so that
130   /// ".tbss" gets the TLS bit set etc.
131   virtual SectionKind getKindForNamedSection(const char *Section,
132                                              SectionKind K) const {
133     return K;
134   }
135   
136   /// SectionForGlobal - This method computes the appropriate section to emit
137   /// the specified global variable or function definition.  This should not
138   /// be passed external (or available externally) globals.
139   const MCSection *SectionForGlobal(const GlobalValue *GV,
140                                     Mangler *Mang,
141                                     const TargetMachine &TM) const;
142   
143   /// getSpecialCasedSectionGlobals - Allow the target to completely override
144   /// section assignment of a global.
145   /// FIXME: ELIMINATE this by making PIC16 implement ADDRESS with
146   /// getFlagsForNamedSection.
147   virtual const MCSection *
148   getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
149                                 SectionInfo Kind) const {
150     return 0;
151   }
152   
153   /// getSectionFlagsAsString - Turn the flags in the specified SectionKind
154   /// into a string that can be printed to the assembly file after the
155   /// ".section foo" part of a section directive.
156   virtual void getSectionFlagsAsString(SectionKind Kind,
157                                        SmallVectorImpl<char> &Str) const {
158   }
159   
160 protected:
161   virtual const MCSection *
162   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
163                          Mangler *Mang, const TargetMachine &TM) const;
164 };
165   
166   
167   
168
169 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
170   bool AtIsCommentChar;  // True if @ is the comment character on this target.
171   bool HasCrazyBSS;
172 public:
173   /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
174   /// is "@".
175   TargetLoweringObjectFileELF(bool atIsCommentChar = false,
176                               // FIXME: REMOVE AFTER UNIQUING IS FIXED.
177                               bool hasCrazyBSS = false)
178     : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
179     
180   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
181   
182   
183   /// getSectionForMergeableConstant - Given a mergeable constant with the
184   /// specified size and relocation information, return a section that it
185   /// should be placed in.
186   virtual const MCSection *
187   getSectionForMergeableConstant(SectionKind Kind) const;
188   
189   virtual SectionKind getKindForNamedSection(const char *Section,
190                                              SectionKind K) const;
191   void getSectionFlagsAsString(SectionKind Kind,
192                                SmallVectorImpl<char> &Str) const;
193   
194   virtual const MCSection *
195   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
196                          Mangler *Mang, const TargetMachine &TM) const;
197 protected:
198   const MCSection *DataRelSection;
199   const MCSection *DataRelLocalSection;
200   const MCSection *DataRelROSection;
201   const MCSection *DataRelROLocalSection;
202   
203   const MCSection *MergeableConst4Section;
204   const MCSection *MergeableConst8Section;
205   const MCSection *MergeableConst16Section;
206 };
207
208   
209   
210 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
211   const MCSection *TextCoalSection;
212   const MCSection *ConstTextCoalSection;
213   const MCSection *ConstDataCoalSection;
214   const MCSection *ConstDataSection;
215   const MCSection *DataCoalSection;
216   const MCSection *FourByteConstantSection;
217   const MCSection *EightByteConstantSection;
218   const MCSection *SixteenByteConstantSection;
219 public:
220   
221   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
222
223   virtual const MCSection *
224   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
225                          Mangler *Mang, const TargetMachine &TM) const;
226   
227   virtual const MCSection *
228   getSectionForMergeableConstant(SectionKind Kind) const;
229   
230   /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
231   /// decide not to emit the UsedDirective for some symbols in llvm.used.
232   /// FIXME: REMOVE this (rdar://7071300)
233   virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
234                                           Mangler *) const;
235 };
236
237
238
239 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
240 public:
241   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
242   
243   virtual void getSectionFlagsAsString(SectionKind Kind,
244                                        SmallVectorImpl<char> &Str) const;
245   
246   virtual const MCSection *
247   SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
248                          Mangler *Mang, const TargetMachine &TM) const;
249 };
250
251 } // end namespace llvm
252
253 #endif