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