MC: Simplify main section layout process by moving alignment into LayoutSection.
[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 public:
584   typedef iplist<MCSectionData> SectionDataListType;
585   typedef iplist<MCSymbolData> SymbolDataListType;
586
587   typedef SectionDataListType::const_iterator const_iterator;
588   typedef SectionDataListType::iterator iterator;
589
590   typedef SymbolDataListType::const_iterator const_symbol_iterator;
591   typedef SymbolDataListType::iterator symbol_iterator;
592
593   typedef std::vector<IndirectSymbolData>::const_iterator
594     const_indirect_symbol_iterator;
595   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
596
597 private:
598   MCAssembler(const MCAssembler&);    // DO NOT IMPLEMENT
599   void operator=(const MCAssembler&); // DO NOT IMPLEMENT
600
601   MCContext &Context;
602
603   TargetAsmBackend &Backend;
604
605   MCCodeEmitter &Emitter;
606
607   raw_ostream &OS;
608
609   iplist<MCSectionData> Sections;
610
611   iplist<MCSymbolData> Symbols;
612
613   /// The map of sections to their associated assembler backend data.
614   //
615   // FIXME: Avoid this indirection?
616   DenseMap<const MCSection*, MCSectionData*> SectionMap;
617
618   /// The map of symbols to their associated assembler backend data.
619   //
620   // FIXME: Avoid this indirection?
621   DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
622
623   std::vector<IndirectSymbolData> IndirectSymbols;
624
625   unsigned SubsectionsViaSymbols : 1;
626
627 private:
628   /// Evaluate a fixup to a relocatable expression and the value which should be
629   /// placed into the fixup.
630   ///
631   /// \param Layout The layout to use for evaluation.
632   /// \param Fixup The fixup to evaluate.
633   /// \param DF The fragment the fixup is inside.
634   /// \param Target [out] On return, the relocatable expression the fixup
635   /// evaluates to.
636   /// \param Value [out] On return, the value of the fixup as currently layed
637   /// out.
638   /// \return Whether the fixup value was fully resolved. This is true if the
639   /// \arg Value result is fixed, otherwise the value may change due to
640   /// relocation.
641   bool EvaluateFixup(const MCAsmLayout &Layout,
642                      const MCAsmFixup &Fixup, const MCFragment *DF,
643                      MCValue &Target, uint64_t &Value) const;
644
645   /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
646   /// (increased in size, in order to hold its value correctly).
647   bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
648                             const MCAsmLayout &Layout) const;
649
650   /// Check whether the given fragment needs relaxation.
651   bool FragmentNeedsRelaxation(const MCInstFragment *IF,
652                                const MCAsmLayout &Layout) const;
653
654   /// LayoutSection - Assign the section the given \arg StartAddress, and then
655   /// assign offsets and sizes to the fragments in the section \arg SD, and
656   /// update the section size.
657   ///
658   /// \return The address at the end of the section, for use in laying out the
659   /// succeeding section.
660   uint64_t LayoutSection(MCSectionData &SD, MCAsmLayout &Layout,
661                          uint64_t StartAddress);
662
663   /// LayoutOnce - Perform one layout iteration and return true if any offsets
664   /// were adjusted.
665   bool LayoutOnce(MCAsmLayout &Layout);
666
667   /// FinishLayout - Finalize a layout, including fragment lowering.
668   void FinishLayout(MCAsmLayout &Layout);
669
670 public:
671   /// Find the symbol which defines the atom containing given address, inside
672   /// the given section, or null if there is no such symbol.
673   //
674   // FIXME-PERF: Eliminate this, it is very slow.
675   const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout,
676                                         const MCSectionData *Section,
677                                         uint64_t Address) const;
678
679   /// Find the symbol which defines the atom containing the given symbol, or
680   /// null if there is no such symbol.
681   //
682   // FIXME-PERF: Eliminate this, it is very slow.
683   const MCSymbolData *getAtom(const MCAsmLayout &Layout,
684                               const MCSymbolData *Symbol) const;
685
686   /// Check whether a particular symbol is visible to the linker and is required
687   /// in the symbol table, or whether it can be discarded by the assembler. This
688   /// also effects whether the assembler treats the label as potentially
689   /// defining a separate atom.
690   bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
691
692   /// Emit the section contents using the given object writer.
693   //
694   // FIXME: Should MCAssembler always have a reference to the object writer?
695   void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
696                         MCObjectWriter *OW) const;
697
698 public:
699   /// Construct a new assembler instance.
700   ///
701   /// \arg OS - The stream to output to.
702   //
703   // FIXME: How are we going to parameterize this? Two obvious options are stay
704   // concrete and require clients to pass in a target like object. The other
705   // option is to make this abstract, and have targets provide concrete
706   // implementations as we do with AsmParser.
707   MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
708               MCCodeEmitter &_Emitter, raw_ostream &OS);
709   ~MCAssembler();
710
711   MCContext &getContext() const { return Context; }
712
713   TargetAsmBackend &getBackend() const { return Backend; }
714
715   MCCodeEmitter &getEmitter() const { return Emitter; }
716
717   /// Finish - Do final processing and write the object to the output stream.
718   void Finish();
719
720   // FIXME: This does not belong here.
721   bool getSubsectionsViaSymbols() const {
722     return SubsectionsViaSymbols;
723   }
724   void setSubsectionsViaSymbols(bool Value) {
725     SubsectionsViaSymbols = Value;
726   }
727
728   /// @name Section List Access
729   /// @{
730
731   const SectionDataListType &getSectionList() const { return Sections; }
732   SectionDataListType &getSectionList() { return Sections; }
733
734   iterator begin() { return Sections.begin(); }
735   const_iterator begin() const { return Sections.begin(); }
736
737   iterator end() { return Sections.end(); }
738   const_iterator end() const { return Sections.end(); }
739
740   size_t size() const { return Sections.size(); }
741
742   /// @}
743   /// @name Symbol List Access
744   /// @{
745
746   const SymbolDataListType &getSymbolList() const { return Symbols; }
747   SymbolDataListType &getSymbolList() { return Symbols; }
748
749   symbol_iterator symbol_begin() { return Symbols.begin(); }
750   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
751
752   symbol_iterator symbol_end() { return Symbols.end(); }
753   const_symbol_iterator symbol_end() const { return Symbols.end(); }
754
755   size_t symbol_size() const { return Symbols.size(); }
756
757   /// @}
758   /// @name Indirect Symbol List Access
759   /// @{
760
761   // FIXME: This is a total hack, this should not be here. Once things are
762   // factored so that the streamer has direct access to the .o writer, it can
763   // disappear.
764   std::vector<IndirectSymbolData> &getIndirectSymbols() {
765     return IndirectSymbols;
766   }
767
768   indirect_symbol_iterator indirect_symbol_begin() {
769     return IndirectSymbols.begin();
770   }
771   const_indirect_symbol_iterator indirect_symbol_begin() const {
772     return IndirectSymbols.begin();
773   }
774
775   indirect_symbol_iterator indirect_symbol_end() {
776     return IndirectSymbols.end();
777   }
778   const_indirect_symbol_iterator indirect_symbol_end() const {
779     return IndirectSymbols.end();
780   }
781
782   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
783
784   /// @}
785   /// @name Backend Data Access
786   /// @{
787
788   MCSectionData &getSectionData(const MCSection &Section) const {
789     MCSectionData *Entry = SectionMap.lookup(&Section);
790     assert(Entry && "Missing section data!");
791     return *Entry;
792   }
793
794   MCSectionData &getOrCreateSectionData(const MCSection &Section,
795                                         bool *Created = 0) {
796     MCSectionData *&Entry = SectionMap[&Section];
797
798     if (Created) *Created = !Entry;
799     if (!Entry)
800       Entry = new MCSectionData(Section, this);
801
802     return *Entry;
803   }
804
805   MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
806     MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
807     assert(Entry && "Missing symbol data!");
808     return *Entry;
809   }
810
811   MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
812                                       bool *Created = 0) {
813     MCSymbolData *&Entry = SymbolMap[&Symbol];
814
815     if (Created) *Created = !Entry;
816     if (!Entry)
817       Entry = new MCSymbolData(Symbol, 0, 0, this);
818
819     return *Entry;
820   }
821
822   /// @}
823
824   void dump();
825 };
826
827 } // end namespace llvm
828
829 #endif