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