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