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