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