Preliminary patch to improve dwarf EH generation - Hooks to return Personality /...
[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/StringRef.h"
19 #include "llvm/MC/SectionKind.h"
20
21 namespace llvm {
22   class MachineModuleInfo;
23   class Mangler;
24   class MCAsmInfo;
25   class MCExpr;
26   class MCSection;
27   class MCSectionMachO;
28   class MCSymbol;
29   class MCContext;
30   class GlobalValue;
31   class TargetMachine;
32   
33 class TargetLoweringObjectFile {
34   MCContext *Ctx;
35   
36   TargetLoweringObjectFile(const TargetLoweringObjectFile&); // DO NOT IMPLEMENT
37   void operator=(const TargetLoweringObjectFile&);           // DO NOT IMPLEMENT
38 protected:
39   
40   TargetLoweringObjectFile();
41   
42   /// TextSection - Section directive for standard text.
43   ///
44   const MCSection *TextSection;
45   
46   /// DataSection - Section directive for standard data.
47   ///
48   const MCSection *DataSection;
49   
50   /// BSSSection - Section that is default initialized to zero.
51   const MCSection *BSSSection;
52   
53   /// ReadOnlySection - Section that is readonly and can contain arbitrary
54   /// initialized data.  Targets are not required to have a readonly section.
55   /// If they don't, various bits of code will fall back to using the data
56   /// section for constants.
57   const MCSection *ReadOnlySection;
58   
59   /// StaticCtorSection - This section contains the static constructor pointer
60   /// list.
61   const MCSection *StaticCtorSection;
62
63   /// StaticDtorSection - This section contains the static destructor pointer
64   /// list.
65   const MCSection *StaticDtorSection;
66   
67   /// LSDASection - If exception handling is supported by the target, this is
68   /// the section the Language Specific Data Area information is emitted to.
69   const MCSection *LSDASection;
70   
71   /// EHFrameSection - If exception handling is supported by the target, this is
72   /// the section the EH Frame is emitted to.
73   const MCSection *EHFrameSection;
74   
75   // Dwarf sections for debug info.  If a target supports debug info, these must
76   // be set.
77   const MCSection *DwarfAbbrevSection;
78   const MCSection *DwarfInfoSection;
79   const MCSection *DwarfLineSection;
80   const MCSection *DwarfFrameSection;
81   const MCSection *DwarfPubNamesSection;
82   const MCSection *DwarfPubTypesSection;
83   const MCSection *DwarfDebugInlineSection;
84   const MCSection *DwarfStrSection;
85   const MCSection *DwarfLocSection;
86   const MCSection *DwarfARangesSection;
87   const MCSection *DwarfRangesSection;
88   const MCSection *DwarfMacroInfoSection;
89   
90 public:
91   
92   MCContext &getContext() const { return *Ctx; }
93   
94
95   virtual ~TargetLoweringObjectFile();
96   
97   /// Initialize - this method must be called before any actual lowering is
98   /// done.  This specifies the current context for codegen, and gives the
99   /// lowering implementations a chance to set up their default sections.
100   virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
101     Ctx = &ctx;
102   }
103   
104   
105   const MCSection *getTextSection() const { return TextSection; }
106   const MCSection *getDataSection() const { return DataSection; }
107   const MCSection *getBSSSection() const { return BSSSection; }
108   const MCSection *getStaticCtorSection() const { return StaticCtorSection; }
109   const MCSection *getStaticDtorSection() const { return StaticDtorSection; }
110   const MCSection *getLSDASection() const { return LSDASection; }
111   const MCSection *getEHFrameSection() const { return EHFrameSection; }
112   const MCSection *getDwarfAbbrevSection() const { return DwarfAbbrevSection; }
113   const MCSection *getDwarfInfoSection() const { return DwarfInfoSection; }
114   const MCSection *getDwarfLineSection() const { return DwarfLineSection; }
115   const MCSection *getDwarfFrameSection() const { return DwarfFrameSection; }
116   const MCSection *getDwarfPubNamesSection() const{return DwarfPubNamesSection;}
117   const MCSection *getDwarfPubTypesSection() const{return DwarfPubTypesSection;}
118   const MCSection *getDwarfDebugInlineSection() const {
119     return DwarfDebugInlineSection;
120   }
121   const MCSection *getDwarfStrSection() const { return DwarfStrSection; }
122   const MCSection *getDwarfLocSection() const { return DwarfLocSection; }
123   const MCSection *getDwarfARangesSection() const { return DwarfARangesSection;}
124   const MCSection *getDwarfRangesSection() const { return DwarfRangesSection; }
125   const MCSection *getDwarfMacroInfoSection() const {
126     return DwarfMacroInfoSection;
127   }
128   
129   /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
130   /// decide not to emit the UsedDirective for some symbols in llvm.used.
131   /// FIXME: REMOVE this (rdar://7071300)
132   virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
133                                           Mangler *) const {
134     return GV != 0;
135   }
136   
137   /// getSectionForConstant - Given a constant with the SectionKind, return a
138   /// section that it should be placed in.
139   virtual const MCSection *getSectionForConstant(SectionKind Kind) const;
140   
141   /// getKindForGlobal - Classify the specified global variable into a set of
142   /// target independent categories embodied in SectionKind.
143   static SectionKind getKindForGlobal(const GlobalValue *GV,
144                                       const TargetMachine &TM);
145   
146   /// SectionForGlobal - This method computes the appropriate section to emit
147   /// the specified global variable or function definition.  This should not
148   /// be passed external (or available externally) globals.
149   const MCSection *SectionForGlobal(const GlobalValue *GV,
150                                     SectionKind Kind, Mangler *Mang,
151                                     const TargetMachine &TM) const;
152   
153   /// SectionForGlobal - This method computes the appropriate section to emit
154   /// the specified global variable or function definition.  This should not
155   /// be passed external (or available externally) globals.
156   const MCSection *SectionForGlobal(const GlobalValue *GV,
157                                     Mangler *Mang,
158                                     const TargetMachine &TM) const {
159     return SectionForGlobal(GV, getKindForGlobal(GV, TM), Mang, TM);
160   }
161   
162   
163   
164   /// getExplicitSectionGlobal - Targets should implement this method to assign
165   /// a section to globals with an explicit section specfied.  The
166   /// implementation of this method can assume that GV->hasSection() is true.
167   virtual const MCSection *
168   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
169                            Mangler *Mang, const TargetMachine &TM) const = 0;
170   
171   /// getSpecialCasedSectionGlobals - Allow the target to completely override
172   /// section assignment of a global.
173   virtual const MCSection *
174   getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
175                                 SectionKind Kind) const {
176     return 0;
177   }
178   
179   /// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a reference
180   /// to the specified global variable from exception handling information.
181   ///
182   virtual const MCExpr *
183   getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
184                               MachineModuleInfo *MMI, unsigned Encoding) const;
185
186   virtual const MCExpr *
187   getSymbolForDwarfReference(const MCSymbol *Sym, MachineModuleInfo *MMI,
188                              unsigned Encoding) const;
189
190   virtual unsigned getPersonalityEncoding() const;
191   virtual unsigned getLSDAEncoding() const;
192   virtual unsigned getFDEEncoding() const;
193   virtual unsigned getTTypeEncoding() const;
194
195 protected:
196   virtual const MCSection *
197   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
198                          Mangler *Mang, const TargetMachine &TM) const;
199 };
200   
201   
202   
203
204 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
205   mutable void *UniquingMap;
206 protected:
207   /// TLSDataSection - Section directive for Thread Local data.
208   ///
209   const MCSection *TLSDataSection;        // Defaults to ".tdata".
210   
211   /// TLSBSSSection - Section directive for Thread Local uninitialized data.
212   /// Null if this target doesn't support a BSS section.
213   ///
214   const MCSection *TLSBSSSection;         // Defaults to ".tbss".
215   
216   const MCSection *DataRelSection;
217   const MCSection *DataRelLocalSection;
218   const MCSection *DataRelROSection;
219   const MCSection *DataRelROLocalSection;
220   
221   const MCSection *MergeableConst4Section;
222   const MCSection *MergeableConst8Section;
223   const MCSection *MergeableConst16Section;
224   
225 protected:
226   const MCSection *getELFSection(StringRef Section, unsigned Type, 
227                                  unsigned Flags, SectionKind Kind,
228                                  bool IsExplicit = false) const;
229 public:
230   TargetLoweringObjectFileELF() : UniquingMap(0) {}
231   ~TargetLoweringObjectFileELF();
232   
233   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
234
235   const MCSection *getDataRelSection() const { return DataRelSection; }
236
237   /// getSectionForConstant - Given a constant with the SectionKind, return a
238   /// section that it should be placed in.
239   virtual const MCSection *getSectionForConstant(SectionKind Kind) const;
240   
241   
242   virtual const MCSection *
243   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
244                            Mangler *Mang, const TargetMachine &TM) const;
245   
246   virtual const MCSection *
247   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
248                          Mangler *Mang, const TargetMachine &TM) const;
249
250   /// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a reference
251   /// to the specified global variable from exception handling information.
252   ///
253   virtual const MCExpr *
254   getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
255                               MachineModuleInfo *MMI, unsigned Encoding) const;
256 };
257
258   
259   
260 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
261   mutable void *UniquingMap;
262   
263   const MCSection *CStringSection;
264   const MCSection *UStringSection;
265   const MCSection *TextCoalSection;
266   const MCSection *ConstTextCoalSection;
267   const MCSection *ConstDataCoalSection;
268   const MCSection *ConstDataSection;
269   const MCSection *DataCoalSection;
270   const MCSection *DataCommonSection;
271   const MCSection *DataBSSSection;
272   const MCSection *FourByteConstantSection;
273   const MCSection *EightByteConstantSection;
274   const MCSection *SixteenByteConstantSection;
275   
276   const MCSection *LazySymbolPointerSection;
277   const MCSection *NonLazySymbolPointerSection;
278 public:
279   TargetLoweringObjectFileMachO() : UniquingMap(0) {}
280   ~TargetLoweringObjectFileMachO();
281   
282   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
283
284   virtual const MCSection *
285   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
286                          Mangler *Mang, const TargetMachine &TM) const;
287   
288   virtual const MCSection *
289   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
290                            Mangler *Mang, const TargetMachine &TM) const;
291   
292   virtual const MCSection *getSectionForConstant(SectionKind Kind) const;
293   
294   /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
295   /// decide not to emit the UsedDirective for some symbols in llvm.used.
296   /// FIXME: REMOVE this (rdar://7071300)
297   virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
298                                           Mangler *) const;
299
300   /// getMachOSection - Return the MCSection for the specified mach-o section.
301   /// This requires the operands to be valid.
302   const MCSectionMachO *getMachOSection(StringRef Segment,
303                                         StringRef Section,
304                                         unsigned TypeAndAttributes,
305                                         SectionKind K) const {
306     return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
307   }
308   const MCSectionMachO *getMachOSection(StringRef Segment,
309                                         StringRef Section,
310                                         unsigned TypeAndAttributes,
311                                         unsigned Reserved2,
312                                         SectionKind K) const;
313
314   /// getTextCoalSection - Return the "__TEXT,__textcoal_nt" section we put weak
315   /// text symbols into.
316   const MCSection *getTextCoalSection() const {
317     return TextCoalSection;
318   }
319   
320   /// getConstTextCoalSection - Return the "__TEXT,__const_coal" section
321   /// we put weak read-only symbols into.
322   const MCSection *getConstTextCoalSection() const {
323     return ConstTextCoalSection;
324   }
325   
326   /// getLazySymbolPointerSection - Return the section corresponding to
327   /// the .lazy_symbol_pointer directive.
328   const MCSection *getLazySymbolPointerSection() const {
329     return LazySymbolPointerSection;
330   }
331   
332   /// getNonLazySymbolPointerSection - Return the section corresponding to
333   /// the .non_lazy_symbol_pointer directive.
334   const MCSection *getNonLazySymbolPointerSection() const {
335     return NonLazySymbolPointerSection;
336   }
337   
338   /// getSymbolForDwarfGlobalReference - The mach-o version of this method
339   /// defaults to returning a stub reference.
340   virtual const MCExpr *
341   getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
342                               MachineModuleInfo *MMI, unsigned Encoding) const;
343 };
344
345
346
347 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
348   mutable void *UniquingMap;
349 public:
350   TargetLoweringObjectFileCOFF() : UniquingMap(0) {}
351   ~TargetLoweringObjectFileCOFF();
352   
353   virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
354   
355   virtual const MCSection *
356   getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
357                            Mangler *Mang, const TargetMachine &TM) const;
358   
359   virtual const MCSection *
360   SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
361                          Mangler *Mang, const TargetMachine &TM) const;
362
363   /// getCOFFSection - Return the MCSection for the specified COFF section.
364   /// FIXME: Switch this to a semantic view eventually.
365   const MCSection *getCOFFSection(StringRef Name, bool isDirective,
366                                   SectionKind K) const;
367 };
368
369 } // end namespace llvm
370
371 #endif