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