Unweaken vtables as per http://llvm.org/docs/CodingStandards.html#ll_virtual_anch
[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/MC/MCFixup.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <vector> // FIXME: Shouldn't be needed.
23
24 namespace llvm {
25 class raw_ostream;
26 class MCAsmLayout;
27 class MCAssembler;
28 class MCBinaryExpr;
29 class MCContext;
30 class MCCodeEmitter;
31 class MCExpr;
32 class MCFragment;
33 class MCObjectWriter;
34 class MCSection;
35 class MCSectionData;
36 class MCSymbol;
37 class MCSymbolData;
38 class MCValue;
39 class MCAsmBackend;
40
41 class MCFragment : public ilist_node<MCFragment> {
42   friend class MCAsmLayout;
43
44   MCFragment(const MCFragment&);     // DO NOT IMPLEMENT
45   void operator=(const MCFragment&); // DO NOT IMPLEMENT
46
47 public:
48   enum FragmentType {
49     FT_Align,
50     FT_Data,
51     FT_Fill,
52     FT_Inst,
53     FT_Org,
54     FT_Dwarf,
55     FT_DwarfFrame,
56     FT_LEB
57   };
58
59 private:
60   FragmentType Kind;
61
62   /// Parent - The data for the section this fragment is in.
63   MCSectionData *Parent;
64
65   /// Atom - The atom this fragment is in, as represented by it's defining
66   /// symbol. Atom's are only used by backends which set
67   /// \see MCAsmBackend::hasReliableSymbolDifference().
68   MCSymbolData *Atom;
69
70   /// @name Assembler Backend Data
71   /// @{
72   //
73   // FIXME: This could all be kept private to the assembler implementation.
74
75   /// Offset - The offset of this fragment in its section. This is ~0 until
76   /// initialized.
77   uint64_t Offset;
78
79   /// LayoutOrder - The layout order of this fragment.
80   unsigned LayoutOrder;
81
82   /// @}
83
84 protected:
85   MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
86
87 public:
88   // Only for sentinel.
89   MCFragment();
90   virtual ~MCFragment();
91
92   FragmentType getKind() const { return Kind; }
93
94   MCSectionData *getParent() const { return Parent; }
95   void setParent(MCSectionData *Value) { Parent = Value; }
96
97   MCSymbolData *getAtom() const { return Atom; }
98   void setAtom(MCSymbolData *Value) { Atom = Value; }
99
100   unsigned getLayoutOrder() const { return LayoutOrder; }
101   void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
102
103   static bool classof(const MCFragment *O) { return true; }
104
105   void dump();
106 };
107
108 class MCDataFragment : public MCFragment {
109   virtual void anchor();
110   SmallString<32> Contents;
111
112   /// Fixups - The list of fixups in this fragment.
113   std::vector<MCFixup> Fixups;
114
115 public:
116   typedef std::vector<MCFixup>::const_iterator const_fixup_iterator;
117   typedef std::vector<MCFixup>::iterator fixup_iterator;
118
119 public:
120   MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
121
122   /// @name Accessors
123   /// @{
124
125   SmallString<32> &getContents() { return Contents; }
126   const SmallString<32> &getContents() const { return Contents; }
127
128   /// @}
129   /// @name Fixup Access
130   /// @{
131
132   void addFixup(MCFixup Fixup) {
133     // Enforce invariant that fixups are in offset order.
134     assert((Fixups.empty() || Fixup.getOffset() > Fixups.back().getOffset()) &&
135            "Fixups must be added in order!");
136     Fixups.push_back(Fixup);
137   }
138
139   std::vector<MCFixup> &getFixups() { return Fixups; }
140   const std::vector<MCFixup> &getFixups() const { return Fixups; }
141
142   fixup_iterator fixup_begin() { return Fixups.begin(); }
143   const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
144
145   fixup_iterator fixup_end() {return Fixups.end();}
146   const_fixup_iterator fixup_end() const {return Fixups.end();}
147
148   size_t fixup_size() const { return Fixups.size(); }
149
150   /// @}
151
152   static bool classof(const MCFragment *F) {
153     return F->getKind() == MCFragment::FT_Data;
154   }
155   static bool classof(const MCDataFragment *) { return true; }
156 };
157
158 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
159 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
160 // with this approach (as opposed to making MCInstFragment a very light weight
161 // object with just the MCInst and a code size, then we should just change
162 // MCDataFragment to have an optional MCInst at its end.
163 class MCInstFragment : public MCFragment {
164   virtual void anchor();
165
166   /// Inst - The instruction this is a fragment for.
167   MCInst Inst;
168
169   /// Code - Binary data for the currently encoded instruction.
170   SmallString<8> Code;
171
172   /// Fixups - The list of fixups in this fragment.
173   SmallVector<MCFixup, 1> Fixups;
174
175 public:
176   typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
177   typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
178
179 public:
180   MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
181     : MCFragment(FT_Inst, SD), Inst(_Inst) {
182   }
183
184   /// @name Accessors
185   /// @{
186
187   SmallVectorImpl<char> &getCode() { return Code; }
188   const SmallVectorImpl<char> &getCode() const { return Code; }
189
190   unsigned getInstSize() const { return Code.size(); }
191
192   MCInst &getInst() { return Inst; }
193   const MCInst &getInst() const { return Inst; }
194
195   void setInst(MCInst Value) { Inst = Value; }
196
197   /// @}
198   /// @name Fixup Access
199   /// @{
200
201   SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
202   const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
203
204   fixup_iterator fixup_begin() { return Fixups.begin(); }
205   const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
206
207   fixup_iterator fixup_end() {return Fixups.end();}
208   const_fixup_iterator fixup_end() const {return Fixups.end();}
209
210   size_t fixup_size() const { return Fixups.size(); }
211
212   /// @}
213
214   static bool classof(const MCFragment *F) {
215     return F->getKind() == MCFragment::FT_Inst;
216   }
217   static bool classof(const MCInstFragment *) { return true; }
218 };
219
220 class MCAlignFragment : public MCFragment {
221   virtual void anchor();
222
223   /// Alignment - The alignment to ensure, in bytes.
224   unsigned Alignment;
225
226   /// Value - Value to use for filling padding bytes.
227   int64_t Value;
228
229   /// ValueSize - The size of the integer (in bytes) of \arg Value.
230   unsigned ValueSize;
231
232   /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
233   /// cannot be satisfied in this width then this fragment is ignored.
234   unsigned MaxBytesToEmit;
235
236   /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
237   /// of using the provided value. The exact interpretation of this flag is
238   /// target dependent.
239   bool EmitNops : 1;
240
241 public:
242   MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
243                   unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
244     : MCFragment(FT_Align, SD), Alignment(_Alignment),
245       Value(_Value),ValueSize(_ValueSize),
246       MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
247
248   /// @name Accessors
249   /// @{
250
251   unsigned getAlignment() const { return Alignment; }
252
253   int64_t getValue() const { return Value; }
254
255   unsigned getValueSize() const { return ValueSize; }
256
257   unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
258
259   bool hasEmitNops() const { return EmitNops; }
260   void setEmitNops(bool Value) { EmitNops = Value; }
261
262   /// @}
263
264   static bool classof(const MCFragment *F) {
265     return F->getKind() == MCFragment::FT_Align;
266   }
267   static bool classof(const MCAlignFragment *) { return true; }
268 };
269
270 class MCFillFragment : public MCFragment {
271   virtual void anchor();
272
273   /// Value - Value to use for filling bytes.
274   int64_t Value;
275
276   /// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
277   /// this is a virtual fill fragment.
278   unsigned ValueSize;
279
280   /// Size - The number of bytes to insert.
281   uint64_t Size;
282
283 public:
284   MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
285                  MCSectionData *SD = 0)
286     : MCFragment(FT_Fill, SD),
287       Value(_Value), ValueSize(_ValueSize), Size(_Size) {
288     assert((!ValueSize || (Size % ValueSize) == 0) &&
289            "Fill size must be a multiple of the value size!");
290   }
291
292   /// @name Accessors
293   /// @{
294
295   int64_t getValue() const { return Value; }
296
297   unsigned getValueSize() const { return ValueSize; }
298
299   uint64_t getSize() const { return Size; }
300
301   /// @}
302
303   static bool classof(const MCFragment *F) {
304     return F->getKind() == MCFragment::FT_Fill;
305   }
306   static bool classof(const MCFillFragment *) { return true; }
307 };
308
309 class MCOrgFragment : public MCFragment {
310   virtual void anchor();
311
312   /// Offset - The offset this fragment should start at.
313   const MCExpr *Offset;
314
315   /// Value - Value to use for filling bytes.
316   int8_t Value;
317
318 public:
319   MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
320     : MCFragment(FT_Org, SD),
321       Offset(&_Offset), Value(_Value) {}
322
323   /// @name Accessors
324   /// @{
325
326   const MCExpr &getOffset() const { return *Offset; }
327
328   uint8_t getValue() const { return Value; }
329
330   /// @}
331
332   static bool classof(const MCFragment *F) {
333     return F->getKind() == MCFragment::FT_Org;
334   }
335   static bool classof(const MCOrgFragment *) { return true; }
336 };
337
338 class MCLEBFragment : public MCFragment {
339   virtual void anchor();
340
341   /// Value - The value this fragment should contain.
342   const MCExpr *Value;
343
344   /// IsSigned - True if this is a sleb128, false if uleb128.
345   bool IsSigned;
346
347   SmallString<8> Contents;
348 public:
349   MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD)
350     : MCFragment(FT_LEB, SD),
351       Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
352
353   /// @name Accessors
354   /// @{
355
356   const MCExpr &getValue() const { return *Value; }
357
358   bool isSigned() const { return IsSigned; }
359
360   SmallString<8> &getContents() { return Contents; }
361   const SmallString<8> &getContents() const { return Contents; }
362
363   /// @}
364
365   static bool classof(const MCFragment *F) {
366     return F->getKind() == MCFragment::FT_LEB;
367   }
368   static bool classof(const MCLEBFragment *) { return true; }
369 };
370
371 class MCDwarfLineAddrFragment : public MCFragment {
372   virtual void anchor();
373
374   /// LineDelta - the value of the difference between the two line numbers
375   /// between two .loc dwarf directives.
376   int64_t LineDelta;
377
378   /// AddrDelta - The expression for the difference of the two symbols that
379   /// make up the address delta between two .loc dwarf directives.
380   const MCExpr *AddrDelta;
381
382   SmallString<8> Contents;
383
384 public:
385   MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
386                       MCSectionData *SD)
387     : MCFragment(FT_Dwarf, SD),
388       LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
389
390   /// @name Accessors
391   /// @{
392
393   int64_t getLineDelta() const { return LineDelta; }
394
395   const MCExpr &getAddrDelta() const { return *AddrDelta; }
396
397   SmallString<8> &getContents() { return Contents; }
398   const SmallString<8> &getContents() const { return Contents; }
399
400   /// @}
401
402   static bool classof(const MCFragment *F) {
403     return F->getKind() == MCFragment::FT_Dwarf;
404   }
405   static bool classof(const MCDwarfLineAddrFragment *) { return true; }
406 };
407
408 class MCDwarfCallFrameFragment : public MCFragment {
409   virtual void anchor();
410
411   /// AddrDelta - The expression for the difference of the two symbols that
412   /// make up the address delta between two .cfi_* dwarf directives.
413   const MCExpr *AddrDelta;
414
415   SmallString<8> Contents;
416
417 public:
418   MCDwarfCallFrameFragment(const MCExpr &_AddrDelta,  MCSectionData *SD)
419     : MCFragment(FT_DwarfFrame, SD),
420       AddrDelta(&_AddrDelta) { Contents.push_back(0); }
421
422   /// @name Accessors
423   /// @{
424
425   const MCExpr &getAddrDelta() const { return *AddrDelta; }
426
427   SmallString<8> &getContents() { return Contents; }
428   const SmallString<8> &getContents() const { return Contents; }
429
430   /// @}
431
432   static bool classof(const MCFragment *F) {
433     return F->getKind() == MCFragment::FT_DwarfFrame;
434   }
435   static bool classof(const MCDwarfCallFrameFragment *) { return true; }
436 };
437
438 // FIXME: Should this be a separate class, or just merged into MCSection? Since
439 // we anticipate the fast path being through an MCAssembler, the only reason to
440 // keep it out is for API abstraction.
441 class MCSectionData : public ilist_node<MCSectionData> {
442   friend class MCAsmLayout;
443
444   MCSectionData(const MCSectionData&);  // DO NOT IMPLEMENT
445   void operator=(const MCSectionData&); // DO NOT IMPLEMENT
446
447 public:
448   typedef iplist<MCFragment> FragmentListType;
449
450   typedef FragmentListType::const_iterator const_iterator;
451   typedef FragmentListType::iterator iterator;
452
453   typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
454   typedef FragmentListType::reverse_iterator reverse_iterator;
455
456 private:
457   FragmentListType Fragments;
458   const MCSection *Section;
459
460   /// Ordinal - The section index in the assemblers section list.
461   unsigned Ordinal;
462
463   /// LayoutOrder - The index of this section in the layout order.
464   unsigned LayoutOrder;
465
466   /// Alignment - The maximum alignment seen in this section.
467   unsigned Alignment;
468
469   /// @name Assembler Backend Data
470   /// @{
471   //
472   // FIXME: This could all be kept private to the assembler implementation.
473
474   /// HasInstructions - Whether this section has had instructions emitted into
475   /// it.
476   unsigned HasInstructions : 1;
477
478   /// @}
479
480 public:
481   // Only for use as sentinel.
482   MCSectionData();
483   MCSectionData(const MCSection &Section, MCAssembler *A = 0);
484
485   const MCSection &getSection() const { return *Section; }
486
487   unsigned getAlignment() const { return Alignment; }
488   void setAlignment(unsigned Value) { Alignment = Value; }
489
490   bool hasInstructions() const { return HasInstructions; }
491   void setHasInstructions(bool Value) { HasInstructions = Value; }
492
493   unsigned getOrdinal() const { return Ordinal; }
494   void setOrdinal(unsigned Value) { Ordinal = Value; }
495
496   unsigned getLayoutOrder() const { return LayoutOrder; }
497   void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
498
499   /// @name Fragment Access
500   /// @{
501
502   const FragmentListType &getFragmentList() const { return Fragments; }
503   FragmentListType &getFragmentList() { return Fragments; }
504
505   iterator begin() { return Fragments.begin(); }
506   const_iterator begin() const { return Fragments.begin(); }
507
508   iterator end() { return Fragments.end(); }
509   const_iterator end() const { return Fragments.end(); }
510
511   reverse_iterator rbegin() { return Fragments.rbegin(); }
512   const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
513
514   reverse_iterator rend() { return Fragments.rend(); }
515   const_reverse_iterator rend() const { return Fragments.rend(); }
516
517   size_t size() const { return Fragments.size(); }
518
519   bool empty() const { return Fragments.empty(); }
520
521   void dump();
522
523   /// @}
524 };
525
526 // FIXME: Same concerns as with SectionData.
527 class MCSymbolData : public ilist_node<MCSymbolData> {
528 public:
529   const MCSymbol *Symbol;
530
531   /// Fragment - The fragment this symbol's value is relative to, if any.
532   MCFragment *Fragment;
533
534   /// Offset - The offset to apply to the fragment address to form this symbol's
535   /// value.
536   uint64_t Offset;
537
538   /// IsExternal - True if this symbol is visible outside this translation
539   /// unit.
540   unsigned IsExternal : 1;
541
542   /// IsPrivateExtern - True if this symbol is private extern.
543   unsigned IsPrivateExtern : 1;
544
545   /// CommonSize - The size of the symbol, if it is 'common', or 0.
546   //
547   // FIXME: Pack this in with other fields? We could put it in offset, since a
548   // common symbol can never get a definition.
549   uint64_t CommonSize;
550
551   /// SymbolSize - An expression describing how to calculate the size of
552   /// a symbol. If a symbol has no size this field will be NULL.
553   const MCExpr *SymbolSize;
554
555   /// CommonAlign - The alignment of the symbol, if it is 'common'.
556   //
557   // FIXME: Pack this in with other fields?
558   unsigned CommonAlign;
559
560   /// Flags - The Flags field is used by object file implementations to store
561   /// additional per symbol information which is not easily classified.
562   uint32_t Flags;
563
564   /// Index - Index field, for use by the object file implementation.
565   uint64_t Index;
566
567 public:
568   // Only for use as sentinel.
569   MCSymbolData();
570   MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
571                MCAssembler *A = 0);
572
573   /// @name Accessors
574   /// @{
575
576   const MCSymbol &getSymbol() const { return *Symbol; }
577
578   MCFragment *getFragment() const { return Fragment; }
579   void setFragment(MCFragment *Value) { Fragment = Value; }
580
581   uint64_t getOffset() const { return Offset; }
582   void setOffset(uint64_t Value) { Offset = Value; }
583
584   /// @}
585   /// @name Symbol Attributes
586   /// @{
587
588   bool isExternal() const { return IsExternal; }
589   void setExternal(bool Value) { IsExternal = Value; }
590
591   bool isPrivateExtern() const { return IsPrivateExtern; }
592   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
593
594   /// isCommon - Is this a 'common' symbol.
595   bool isCommon() const { return CommonSize != 0; }
596
597   /// setCommon - Mark this symbol as being 'common'.
598   ///
599   /// \param Size - The size of the symbol.
600   /// \param Align - The alignment of the symbol.
601   void setCommon(uint64_t Size, unsigned Align) {
602     CommonSize = Size;
603     CommonAlign = Align;
604   }
605
606   /// getCommonSize - Return the size of a 'common' symbol.
607   uint64_t getCommonSize() const {
608     assert(isCommon() && "Not a 'common' symbol!");
609     return CommonSize;
610   }
611
612   void setSize(const MCExpr *SS) {
613     SymbolSize = SS;
614   }
615
616   const MCExpr *getSize() const {
617     return SymbolSize;
618   }
619
620
621   /// getCommonAlignment - Return the alignment of a 'common' symbol.
622   unsigned getCommonAlignment() const {
623     assert(isCommon() && "Not a 'common' symbol!");
624     return CommonAlign;
625   }
626
627   /// getFlags - Get the (implementation defined) symbol flags.
628   uint32_t getFlags() const { return Flags; }
629
630   /// setFlags - Set the (implementation defined) symbol flags.
631   void setFlags(uint32_t Value) { Flags = Value; }
632
633   /// modifyFlags - Modify the flags via a mask
634   void modifyFlags(uint32_t Value, uint32_t Mask) {
635     Flags = (Flags & ~Mask) | Value;
636   }
637
638   /// getIndex - Get the (implementation defined) index.
639   uint64_t getIndex() const { return Index; }
640
641   /// setIndex - Set the (implementation defined) index.
642   void setIndex(uint64_t Value) { Index = Value; }
643
644   /// @}
645
646   void dump();
647 };
648
649 // FIXME: This really doesn't belong here. See comments below.
650 struct IndirectSymbolData {
651   MCSymbol *Symbol;
652   MCSectionData *SectionData;
653 };
654
655 class MCAssembler {
656   friend class MCAsmLayout;
657
658 public:
659   typedef iplist<MCSectionData> SectionDataListType;
660   typedef iplist<MCSymbolData> SymbolDataListType;
661
662   typedef SectionDataListType::const_iterator const_iterator;
663   typedef SectionDataListType::iterator iterator;
664
665   typedef SymbolDataListType::const_iterator const_symbol_iterator;
666   typedef SymbolDataListType::iterator symbol_iterator;
667
668   typedef std::vector<IndirectSymbolData>::const_iterator
669     const_indirect_symbol_iterator;
670   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
671
672 private:
673   MCAssembler(const MCAssembler&);    // DO NOT IMPLEMENT
674   void operator=(const MCAssembler&); // DO NOT IMPLEMENT
675
676   MCContext &Context;
677
678   MCAsmBackend &Backend;
679
680   MCCodeEmitter &Emitter;
681
682   MCObjectWriter &Writer;
683
684   raw_ostream &OS;
685
686   iplist<MCSectionData> Sections;
687
688   iplist<MCSymbolData> Symbols;
689
690   /// The map of sections to their associated assembler backend data.
691   //
692   // FIXME: Avoid this indirection?
693   DenseMap<const MCSection*, MCSectionData*> SectionMap;
694
695   /// The map of symbols to their associated assembler backend data.
696   //
697   // FIXME: Avoid this indirection?
698   DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
699
700   std::vector<IndirectSymbolData> IndirectSymbols;
701
702   /// The set of function symbols for which a .thumb_func directive has
703   /// been seen.
704   //
705   // FIXME: We really would like this in target specific code rather than
706   // here. Maybe when the relocation stuff moves to target specific,
707   // this can go with it? The streamer would need some target specific
708   // refactoring too.
709   SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
710
711   unsigned RelaxAll : 1;
712   unsigned NoExecStack : 1;
713   unsigned SubsectionsViaSymbols : 1;
714
715 private:
716   /// Evaluate a fixup to a relocatable expression and the value which should be
717   /// placed into the fixup.
718   ///
719   /// \param Layout The layout to use for evaluation.
720   /// \param Fixup The fixup to evaluate.
721   /// \param DF The fragment the fixup is inside.
722   /// \param Target [out] On return, the relocatable expression the fixup
723   /// evaluates to.
724   /// \param Value [out] On return, the value of the fixup as currently laid
725   /// out.
726   /// \return Whether the fixup value was fully resolved. This is true if the
727   /// \arg Value result is fixed, otherwise the value may change due to
728   /// relocation.
729   bool evaluateFixup(const MCAsmLayout &Layout,
730                      const MCFixup &Fixup, const MCFragment *DF,
731                      MCValue &Target, uint64_t &Value) const;
732
733   /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
734   /// (increased in size, in order to hold its value correctly).
735   bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCInstFragment *DF,
736                             const MCAsmLayout &Layout) const;
737
738   /// Check whether the given fragment needs relaxation.
739   bool fragmentNeedsRelaxation(const MCInstFragment *IF,
740                                const MCAsmLayout &Layout) const;
741
742   /// layoutOnce - Perform one layout iteration and return true if any offsets
743   /// were adjusted.
744   bool layoutOnce(MCAsmLayout &Layout);
745
746   bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
747
748   bool relaxInstruction(MCAsmLayout &Layout, MCInstFragment &IF);
749
750   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
751
752   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
753   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
754                                    MCDwarfCallFrameFragment &DF);
755
756   /// finishLayout - Finalize a layout, including fragment lowering.
757   void finishLayout(MCAsmLayout &Layout);
758
759   uint64_t handleFixup(const MCAsmLayout &Layout,
760                        MCFragment &F, const MCFixup &Fixup);
761
762 public:
763   /// Compute the effective fragment size assuming it is laid out at the given
764   /// \arg SectionAddress and \arg FragmentOffset.
765   uint64_t computeFragmentSize(const MCAsmLayout &Layout,
766                                const MCFragment &F) const;
767
768   /// Find the symbol which defines the atom containing the given symbol, or
769   /// null if there is no such symbol.
770   const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
771
772   /// Check whether a particular symbol is visible to the linker and is required
773   /// in the symbol table, or whether it can be discarded by the assembler. This
774   /// also effects whether the assembler treats the label as potentially
775   /// defining a separate atom.
776   bool isSymbolLinkerVisible(const MCSymbol &SD) const;
777
778   /// Emit the section contents using the given object writer.
779   void writeSectionData(const MCSectionData *Section,
780                         const MCAsmLayout &Layout) const;
781
782   /// Check whether a given symbol has been flagged with .thumb_func.
783   bool isThumbFunc(const MCSymbol *Func) const {
784     return ThumbFuncs.count(Func);
785   }
786
787   /// Flag a function symbol as the target of a .thumb_func directive.
788   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
789
790 public:
791   /// Construct a new assembler instance.
792   ///
793   /// \arg OS - The stream to output to.
794   //
795   // FIXME: How are we going to parameterize this? Two obvious options are stay
796   // concrete and require clients to pass in a target like object. The other
797   // option is to make this abstract, and have targets provide concrete
798   // implementations as we do with AsmParser.
799   MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
800               MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
801               raw_ostream &OS);
802   ~MCAssembler();
803
804   MCContext &getContext() const { return Context; }
805
806   MCAsmBackend &getBackend() const { return Backend; }
807
808   MCCodeEmitter &getEmitter() const { return Emitter; }
809
810   MCObjectWriter &getWriter() const { return Writer; }
811
812   /// Finish - Do final processing and write the object to the output stream.
813   /// \arg Writer is used for custom object writer (as the MCJIT does),
814   /// if not specified it is automatically created from backend.
815   void Finish();
816
817   // FIXME: This does not belong here.
818   bool getSubsectionsViaSymbols() const {
819     return SubsectionsViaSymbols;
820   }
821   void setSubsectionsViaSymbols(bool Value) {
822     SubsectionsViaSymbols = Value;
823   }
824
825   bool getRelaxAll() const { return RelaxAll; }
826   void setRelaxAll(bool Value) { RelaxAll = Value; }
827
828   bool getNoExecStack() const { return NoExecStack; }
829   void setNoExecStack(bool Value) { NoExecStack = Value; }
830
831   /// @name Section List Access
832   /// @{
833
834   const SectionDataListType &getSectionList() const { return Sections; }
835   SectionDataListType &getSectionList() { return Sections; }
836
837   iterator begin() { return Sections.begin(); }
838   const_iterator begin() const { return Sections.begin(); }
839
840   iterator end() { return Sections.end(); }
841   const_iterator end() const { return Sections.end(); }
842
843   size_t size() const { return Sections.size(); }
844
845   /// @}
846   /// @name Symbol List Access
847   /// @{
848
849   const SymbolDataListType &getSymbolList() const { return Symbols; }
850   SymbolDataListType &getSymbolList() { return Symbols; }
851
852   symbol_iterator symbol_begin() { return Symbols.begin(); }
853   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
854
855   symbol_iterator symbol_end() { return Symbols.end(); }
856   const_symbol_iterator symbol_end() const { return Symbols.end(); }
857
858   size_t symbol_size() const { return Symbols.size(); }
859
860   /// @}
861   /// @name Indirect Symbol List Access
862   /// @{
863
864   // FIXME: This is a total hack, this should not be here. Once things are
865   // factored so that the streamer has direct access to the .o writer, it can
866   // disappear.
867   std::vector<IndirectSymbolData> &getIndirectSymbols() {
868     return IndirectSymbols;
869   }
870
871   indirect_symbol_iterator indirect_symbol_begin() {
872     return IndirectSymbols.begin();
873   }
874   const_indirect_symbol_iterator indirect_symbol_begin() const {
875     return IndirectSymbols.begin();
876   }
877
878   indirect_symbol_iterator indirect_symbol_end() {
879     return IndirectSymbols.end();
880   }
881   const_indirect_symbol_iterator indirect_symbol_end() const {
882     return IndirectSymbols.end();
883   }
884
885   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
886
887   /// @}
888   /// @name Backend Data Access
889   /// @{
890
891   MCSectionData &getSectionData(const MCSection &Section) const {
892     MCSectionData *Entry = SectionMap.lookup(&Section);
893     assert(Entry && "Missing section data!");
894     return *Entry;
895   }
896
897   MCSectionData &getOrCreateSectionData(const MCSection &Section,
898                                         bool *Created = 0) {
899     MCSectionData *&Entry = SectionMap[&Section];
900
901     if (Created) *Created = !Entry;
902     if (!Entry)
903       Entry = new MCSectionData(Section, this);
904
905     return *Entry;
906   }
907
908   MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
909     MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
910     assert(Entry && "Missing symbol data!");
911     return *Entry;
912   }
913
914   MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
915                                       bool *Created = 0) {
916     MCSymbolData *&Entry = SymbolMap[&Symbol];
917
918     if (Created) *Created = !Entry;
919     if (!Entry)
920       Entry = new MCSymbolData(Symbol, 0, 0, this);
921
922     return *Entry;
923   }
924
925   /// @}
926
927   void dump();
928 };
929
930 } // end namespace llvm
931
932 #endif