[Orc] Add explicit move construction/assignment to RCMemoryManager.
[oota-llvm.git] / include / llvm / MC / MCAssembler.h
1 //===- MCAssembler.h - Object File Generation -------------------*- 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 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
12
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/ADT/ilist.h"
15 #include "llvm/ADT/ilist_node.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/MC/MCDirectives.h"
18 #include "llvm/MC/MCDwarf.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCFragment.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCLinkerOptimizationHint.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
25
26 namespace llvm {
27 class raw_ostream;
28 class MCAsmLayout;
29 class MCAssembler;
30 class MCContext;
31 class MCCodeEmitter;
32 class MCExpr;
33 class MCFragment;
34 class MCObjectWriter;
35 class MCSection;
36 class MCSubtargetInfo;
37 class MCValue;
38 class MCAsmBackend;
39
40 // FIXME: This really doesn't belong here. See comments below.
41 struct IndirectSymbolData {
42   MCSymbol *Symbol;
43   MCSection *Section;
44 };
45
46 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
47 // to one another.
48 struct DataRegionData {
49   // This enum should be kept in sync w/ the mach-o definition in
50   // llvm/Object/MachOFormat.h.
51   enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
52   MCSymbol *Start;
53   MCSymbol *End;
54 };
55
56 class MCAssembler {
57   friend class MCAsmLayout;
58
59 public:
60   typedef std::vector<MCSection *> SectionListType;
61   typedef std::vector<const MCSymbol *> SymbolDataListType;
62
63   typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
64   typedef pointee_iterator<SectionListType::iterator> iterator;
65
66   typedef pointee_iterator<SymbolDataListType::const_iterator>
67   const_symbol_iterator;
68   typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
69
70   typedef iterator_range<symbol_iterator> symbol_range;
71   typedef iterator_range<const_symbol_iterator> const_symbol_range;
72
73   typedef std::vector<IndirectSymbolData>::const_iterator
74       const_indirect_symbol_iterator;
75   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
76
77   typedef std::vector<DataRegionData>::const_iterator
78       const_data_region_iterator;
79   typedef std::vector<DataRegionData>::iterator data_region_iterator;
80
81   /// MachO specific deployment target version info.
82   // A Major version of 0 indicates that no version information was supplied
83   // and so the corresponding load command should not be emitted.
84   typedef struct {
85     MCVersionMinType Kind;
86     unsigned Major;
87     unsigned Minor;
88     unsigned Update;
89   } VersionMinInfoType;
90
91 private:
92   MCAssembler(const MCAssembler &) = delete;
93   void operator=(const MCAssembler &) = delete;
94
95   MCContext &Context;
96
97   MCAsmBackend &Backend;
98
99   MCCodeEmitter &Emitter;
100
101   MCObjectWriter &Writer;
102
103   SectionListType Sections;
104
105   SymbolDataListType Symbols;
106
107   std::vector<IndirectSymbolData> IndirectSymbols;
108
109   std::vector<DataRegionData> DataRegions;
110
111   /// The list of linker options to propagate into the object file.
112   std::vector<std::vector<std::string>> LinkerOptions;
113
114   /// List of declared file names
115   std::vector<std::string> FileNames;
116
117   MCDwarfLineTableParams LTParams;
118
119   /// The set of function symbols for which a .thumb_func directive has
120   /// been seen.
121   //
122   // FIXME: We really would like this in target specific code rather than
123   // here. Maybe when the relocation stuff moves to target specific,
124   // this can go with it? The streamer would need some target specific
125   // refactoring too.
126   mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
127
128   /// \brief The bundle alignment size currently set in the assembler.
129   ///
130   /// By default it's 0, which means bundling is disabled.
131   unsigned BundleAlignSize;
132
133   unsigned RelaxAll : 1;
134   unsigned SubsectionsViaSymbols : 1;
135   unsigned IncrementalLinkerCompatible : 1;
136
137   /// ELF specific e_header flags
138   // It would be good if there were an MCELFAssembler class to hold this.
139   // ELF header flags are used both by the integrated and standalone assemblers.
140   // Access to the flags is necessary in cases where assembler directives affect
141   // which flags to be set.
142   unsigned ELFHeaderEFlags;
143
144   /// Used to communicate Linker Optimization Hint information between
145   /// the Streamer and the .o writer
146   MCLOHContainer LOHContainer;
147
148   VersionMinInfoType VersionMinInfo;
149
150 private:
151   /// Evaluate a fixup to a relocatable expression and the value which should be
152   /// placed into the fixup.
153   ///
154   /// \param Layout The layout to use for evaluation.
155   /// \param Fixup The fixup to evaluate.
156   /// \param DF The fragment the fixup is inside.
157   /// \param Target [out] On return, the relocatable expression the fixup
158   /// evaluates to.
159   /// \param Value [out] On return, the value of the fixup as currently laid
160   /// out.
161   /// \return Whether the fixup value was fully resolved. This is true if the
162   /// \p Value result is fixed, otherwise the value may change due to
163   /// relocation.
164   bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
165                      const MCFragment *DF, MCValue &Target,
166                      uint64_t &Value) const;
167
168   /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
169   /// (increased in size, in order to hold its value correctly).
170   bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
171                             const MCAsmLayout &Layout) const;
172
173   /// Check whether the given fragment needs relaxation.
174   bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
175                                const MCAsmLayout &Layout) const;
176
177   /// \brief Perform one layout iteration and return true if any offsets
178   /// were adjusted.
179   bool layoutOnce(MCAsmLayout &Layout);
180
181   /// \brief Perform one layout iteration of the given section and return true
182   /// if any offsets were adjusted.
183   bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
184
185   bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
186
187   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
188
189   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
190   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
191                                    MCDwarfCallFrameFragment &DF);
192
193   /// finishLayout - Finalize a layout, including fragment lowering.
194   void finishLayout(MCAsmLayout &Layout);
195
196   std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
197                                         MCFragment &F, const MCFixup &Fixup);
198
199 public:
200   /// Compute the effective fragment size assuming it is laid out at the given
201   /// \p SectionAddress and \p FragmentOffset.
202   uint64_t computeFragmentSize(const MCAsmLayout &Layout,
203                                const MCFragment &F) const;
204
205   /// Find the symbol which defines the atom containing the given symbol, or
206   /// null if there is no such symbol.
207   const MCSymbol *getAtom(const MCSymbol &S) const;
208
209   /// Check whether a particular symbol is visible to the linker and is required
210   /// in the symbol table, or whether it can be discarded by the assembler. This
211   /// also effects whether the assembler treats the label as potentially
212   /// defining a separate atom.
213   bool isSymbolLinkerVisible(const MCSymbol &SD) const;
214
215   /// Emit the section contents using the given object writer.
216   void writeSectionData(const MCSection *Section,
217                         const MCAsmLayout &Layout) const;
218
219   /// Check whether a given symbol has been flagged with .thumb_func.
220   bool isThumbFunc(const MCSymbol *Func) const;
221
222   /// Flag a function symbol as the target of a .thumb_func directive.
223   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
224
225   /// ELF e_header flags
226   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
227   void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
228
229   /// MachO deployment target version information.
230   const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
231   void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
232                          unsigned Update) {
233     VersionMinInfo.Kind = Kind;
234     VersionMinInfo.Major = Major;
235     VersionMinInfo.Minor = Minor;
236     VersionMinInfo.Update = Update;
237   }
238
239 public:
240   /// Construct a new assembler instance.
241   //
242   // FIXME: How are we going to parameterize this? Two obvious options are stay
243   // concrete and require clients to pass in a target like object. The other
244   // option is to make this abstract, and have targets provide concrete
245   // implementations as we do with AsmParser.
246   MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
247               MCCodeEmitter &Emitter_, MCObjectWriter &Writer_);
248   ~MCAssembler();
249
250   /// Reuse an assembler instance
251   ///
252   void reset();
253
254   MCContext &getContext() const { return Context; }
255
256   MCAsmBackend &getBackend() const { return Backend; }
257
258   MCCodeEmitter &getEmitter() const { return Emitter; }
259
260   MCObjectWriter &getWriter() const { return Writer; }
261
262   MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
263   void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
264
265   /// Finish - Do final processing and write the object to the output stream.
266   /// \p Writer is used for custom object writer (as the MCJIT does),
267   /// if not specified it is automatically created from backend.
268   void Finish();
269
270   // Layout all section and prepare them for emission.
271   void layout(MCAsmLayout &Layout);
272
273   // FIXME: This does not belong here.
274   bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
275   void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
276
277   bool isIncrementalLinkerCompatible() const {
278     return IncrementalLinkerCompatible;
279   }
280   void setIncrementalLinkerCompatible(bool Value) {
281     IncrementalLinkerCompatible = Value;
282   }
283
284   bool getRelaxAll() const { return RelaxAll; }
285   void setRelaxAll(bool Value) { RelaxAll = Value; }
286
287   bool isBundlingEnabled() const { return BundleAlignSize != 0; }
288
289   unsigned getBundleAlignSize() const { return BundleAlignSize; }
290
291   void setBundleAlignSize(unsigned Size) {
292     assert((Size == 0 || !(Size & (Size - 1))) &&
293            "Expect a power-of-two bundle align size");
294     BundleAlignSize = Size;
295   }
296
297   /// \name Section List Access
298   /// @{
299
300   iterator begin() { return Sections.begin(); }
301   const_iterator begin() const { return Sections.begin(); }
302
303   iterator end() { return Sections.end(); }
304   const_iterator end() const { return Sections.end(); }
305
306   size_t size() const { return Sections.size(); }
307
308   /// @}
309   /// \name Symbol List Access
310   /// @{
311   symbol_iterator symbol_begin() { return Symbols.begin(); }
312   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
313
314   symbol_iterator symbol_end() { return Symbols.end(); }
315   const_symbol_iterator symbol_end() const { return Symbols.end(); }
316
317   symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
318   const_symbol_range symbols() const {
319     return make_range(symbol_begin(), symbol_end());
320   }
321
322   size_t symbol_size() const { return Symbols.size(); }
323
324   /// @}
325   /// \name Indirect Symbol List Access
326   /// @{
327
328   // FIXME: This is a total hack, this should not be here. Once things are
329   // factored so that the streamer has direct access to the .o writer, it can
330   // disappear.
331   std::vector<IndirectSymbolData> &getIndirectSymbols() {
332     return IndirectSymbols;
333   }
334
335   indirect_symbol_iterator indirect_symbol_begin() {
336     return IndirectSymbols.begin();
337   }
338   const_indirect_symbol_iterator indirect_symbol_begin() const {
339     return IndirectSymbols.begin();
340   }
341
342   indirect_symbol_iterator indirect_symbol_end() {
343     return IndirectSymbols.end();
344   }
345   const_indirect_symbol_iterator indirect_symbol_end() const {
346     return IndirectSymbols.end();
347   }
348
349   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
350
351   /// @}
352   /// \name Linker Option List Access
353   /// @{
354
355   std::vector<std::vector<std::string>> &getLinkerOptions() {
356     return LinkerOptions;
357   }
358
359   /// @}
360   /// \name Data Region List Access
361   /// @{
362
363   // FIXME: This is a total hack, this should not be here. Once things are
364   // factored so that the streamer has direct access to the .o writer, it can
365   // disappear.
366   std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
367
368   data_region_iterator data_region_begin() { return DataRegions.begin(); }
369   const_data_region_iterator data_region_begin() const {
370     return DataRegions.begin();
371   }
372
373   data_region_iterator data_region_end() { return DataRegions.end(); }
374   const_data_region_iterator data_region_end() const {
375     return DataRegions.end();
376   }
377
378   size_t data_region_size() const { return DataRegions.size(); }
379
380   /// @}
381   /// \name Data Region List Access
382   /// @{
383
384   // FIXME: This is a total hack, this should not be here. Once things are
385   // factored so that the streamer has direct access to the .o writer, it can
386   // disappear.
387   MCLOHContainer &getLOHContainer() { return LOHContainer; }
388   const MCLOHContainer &getLOHContainer() const {
389     return const_cast<MCAssembler *>(this)->getLOHContainer();
390   }
391   /// @}
392   /// \name Backend Data Access
393   /// @{
394
395   bool registerSection(MCSection &Section);
396
397   void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
398
399   ArrayRef<std::string> getFileNames() { return FileNames; }
400
401   void addFileName(StringRef FileName) {
402     if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
403         FileNames.end())
404       FileNames.push_back(FileName);
405   }
406
407   /// \brief Write the necessary bundle padding to the given object writer.
408   /// Expects a fragment \p F containing instructions and its size \p FSize.
409   void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
410                             MCObjectWriter *OW) const;
411
412   /// @}
413
414   void dump();
415 };
416
417 /// \brief Compute the amount of padding required before the fragment \p F to
418 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
419 /// its section and \p FSize is the fragment's size.
420 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
421                               uint64_t FOffset, uint64_t FSize);
422
423 } // end namespace llvm
424
425 #endif