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