MCAssembler.h revision f1d0f7781e766df878bec4e7977fa3204374f394
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 MCContext;
29class MCCodeEmitter;
30class MCExpr;
31class MCFragment;
32class MCObjectWriter;
33class MCSection;
34class MCSectionData;
35class MCSymbol;
36class MCSymbolData;
37class MCValue;
38class MCAsmBackend;
39
40class 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
46public:
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
58private:
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
83protected:
84  MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
85
86public:
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
107class 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
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  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
174public:
175  typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
176  typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
177
178public:
179  MCInstFragment(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(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
219class 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 \arg 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
240public:
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
269class 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 \arg 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
282public:
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
308class 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
317public:
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
337class 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;
347public:
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
370class 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
383public:
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
407class 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
416public:
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.
440class MCSectionData : public ilist_node<MCSectionData> {
441  friend class MCAsmLayout;
442
443  MCSectionData(const MCSectionData&);  // DO NOT IMPLEMENT
444  void operator=(const MCSectionData&); // DO NOT IMPLEMENT
445
446public:
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
455private:
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
479public:
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.
526class MCSymbolData : public ilist_node<MCSymbolData> {
527public:
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
566public:
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.
649struct IndirectSymbolData {
650  MCSymbol *Symbol;
651  MCSectionData *SectionData;
652};
653
654class MCAssembler {
655  friend class MCAsmLayout;
656
657public:
658  typedef iplist<MCSectionData> SectionDataListType;
659  typedef iplist<MCSymbolData> SymbolDataListType;
660
661  typedef SectionDataListType::const_iterator const_iterator;
662  typedef SectionDataListType::iterator iterator;
663
664  typedef SymbolDataListType::const_iterator const_symbol_iterator;
665  typedef SymbolDataListType::iterator symbol_iterator;
666
667  typedef std::vector<IndirectSymbolData>::const_iterator
668    const_indirect_symbol_iterator;
669  typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
670
671private:
672  MCAssembler(const MCAssembler&);    // DO NOT IMPLEMENT
673  void operator=(const MCAssembler&); // DO NOT IMPLEMENT
674
675  MCContext &Context;
676
677  MCAsmBackend &Backend;
678
679  MCCodeEmitter &Emitter;
680
681  MCObjectWriter &Writer;
682
683  raw_ostream &OS;
684
685  iplist<MCSectionData> Sections;
686
687  iplist<MCSymbolData> Symbols;
688
689  /// The map of sections to their associated assembler backend data.
690  //
691  // FIXME: Avoid this indirection?
692  DenseMap<const MCSection*, MCSectionData*> SectionMap;
693
694  /// The map of symbols to their associated assembler backend data.
695  //
696  // FIXME: Avoid this indirection?
697  DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
698
699  std::vector<IndirectSymbolData> IndirectSymbols;
700
701  /// The set of function symbols for which a .thumb_func directive has
702  /// been seen.
703  //
704  // FIXME: We really would like this in target specific code rather than
705  // here. Maybe when the relocation stuff moves to target specific,
706  // this can go with it? The streamer would need some target specific
707  // refactoring too.
708  SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
709
710  unsigned RelaxAll : 1;
711  unsigned NoExecStack : 1;
712  unsigned SubsectionsViaSymbols : 1;
713
714private:
715  /// Evaluate a fixup to a relocatable expression and the value which should be
716  /// placed into the fixup.
717  ///
718  /// \param Layout The layout to use for evaluation.
719  /// \param Fixup The fixup to evaluate.
720  /// \param DF The fragment the fixup is inside.
721  /// \param Target [out] On return, the relocatable expression the fixup
722  /// evaluates to.
723  /// \param Value [out] On return, the value of the fixup as currently laid
724  /// out.
725  /// \return Whether the fixup value was fully resolved. This is true if the
726  /// \arg Value result is fixed, otherwise the value may change due to
727  /// relocation.
728  bool evaluateFixup(const MCAsmLayout &Layout,
729                     const MCFixup &Fixup, const MCFragment *DF,
730                     MCValue &Target, uint64_t &Value) const;
731
732  /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
733  /// (increased in size, in order to hold its value correctly).
734  bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCInstFragment *DF,
735                            const MCAsmLayout &Layout) const;
736
737  /// Check whether the given fragment needs relaxation.
738  bool fragmentNeedsRelaxation(const MCInstFragment *IF,
739                               const MCAsmLayout &Layout) const;
740
741  /// layoutOnce - Perform one layout iteration and return true if any offsets
742  /// were adjusted.
743  bool layoutOnce(MCAsmLayout &Layout);
744
745  bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
746
747  bool relaxInstruction(MCAsmLayout &Layout, MCInstFragment &IF);
748
749  bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
750
751  bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
752  bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
753                                   MCDwarfCallFrameFragment &DF);
754
755  /// finishLayout - Finalize a layout, including fragment lowering.
756  void finishLayout(MCAsmLayout &Layout);
757
758  uint64_t handleFixup(const MCAsmLayout &Layout,
759                       MCFragment &F, const MCFixup &Fixup);
760
761public:
762  /// Compute the effective fragment size assuming it is laid out at the given
763  /// \arg SectionAddress and \arg FragmentOffset.
764  uint64_t computeFragmentSize(const MCAsmLayout &Layout,
765                               const MCFragment &F) const;
766
767  /// Find the symbol which defines the atom containing the given symbol, or
768  /// null if there is no such symbol.
769  const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
770
771  /// Check whether a particular symbol is visible to the linker and is required
772  /// in the symbol table, or whether it can be discarded by the assembler. This
773  /// also effects whether the assembler treats the label as potentially
774  /// defining a separate atom.
775  bool isSymbolLinkerVisible(const MCSymbol &SD) const;
776
777  /// Emit the section contents using the given object writer.
778  void writeSectionData(const MCSectionData *Section,
779                        const MCAsmLayout &Layout) const;
780
781  /// Check whether a given symbol has been flagged with .thumb_func.
782  bool isThumbFunc(const MCSymbol *Func) const {
783    return ThumbFuncs.count(Func);
784  }
785
786  /// Flag a function symbol as the target of a .thumb_func directive.
787  void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
788
789public:
790  /// Construct a new assembler instance.
791  ///
792  /// \arg OS - The stream to output to.
793  //
794  // FIXME: How are we going to parameterize this? Two obvious options are stay
795  // concrete and require clients to pass in a target like object. The other
796  // option is to make this abstract, and have targets provide concrete
797  // implementations as we do with AsmParser.
798  MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
799              MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
800              raw_ostream &OS);
801  ~MCAssembler();
802
803  MCContext &getContext() const { return Context; }
804
805  MCAsmBackend &getBackend() const { return Backend; }
806
807  MCCodeEmitter &getEmitter() const { return Emitter; }
808
809  MCObjectWriter &getWriter() const { return Writer; }
810
811  /// Finish - Do final processing and write the object to the output stream.
812  /// \arg Writer is used for custom object writer (as the MCJIT does),
813  /// if not specified it is automatically created from backend.
814  void Finish();
815
816  // FIXME: This does not belong here.
817  bool getSubsectionsViaSymbols() const {
818    return SubsectionsViaSymbols;
819  }
820  void setSubsectionsViaSymbols(bool Value) {
821    SubsectionsViaSymbols = Value;
822  }
823
824  bool getRelaxAll() const { return RelaxAll; }
825  void setRelaxAll(bool Value) { RelaxAll = Value; }
826
827  bool getNoExecStack() const { return NoExecStack; }
828  void setNoExecStack(bool Value) { NoExecStack = Value; }
829
830  /// @name Section List Access
831  /// @{
832
833  const SectionDataListType &getSectionList() const { return Sections; }
834  SectionDataListType &getSectionList() { return Sections; }
835
836  iterator begin() { return Sections.begin(); }
837  const_iterator begin() const { return Sections.begin(); }
838
839  iterator end() { return Sections.end(); }
840  const_iterator end() const { return Sections.end(); }
841
842  size_t size() const { return Sections.size(); }
843
844  /// @}
845  /// @name Symbol List Access
846  /// @{
847
848  const SymbolDataListType &getSymbolList() const { return Symbols; }
849  SymbolDataListType &getSymbolList() { return Symbols; }
850
851  symbol_iterator symbol_begin() { return Symbols.begin(); }
852  const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
853
854  symbol_iterator symbol_end() { return Symbols.end(); }
855  const_symbol_iterator symbol_end() const { return Symbols.end(); }
856
857  size_t symbol_size() const { return Symbols.size(); }
858
859  /// @}
860  /// @name Indirect Symbol List Access
861  /// @{
862
863  // FIXME: This is a total hack, this should not be here. Once things are
864  // factored so that the streamer has direct access to the .o writer, it can
865  // disappear.
866  std::vector<IndirectSymbolData> &getIndirectSymbols() {
867    return IndirectSymbols;
868  }
869
870  indirect_symbol_iterator indirect_symbol_begin() {
871    return IndirectSymbols.begin();
872  }
873  const_indirect_symbol_iterator indirect_symbol_begin() const {
874    return IndirectSymbols.begin();
875  }
876
877  indirect_symbol_iterator indirect_symbol_end() {
878    return IndirectSymbols.end();
879  }
880  const_indirect_symbol_iterator indirect_symbol_end() const {
881    return IndirectSymbols.end();
882  }
883
884  size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
885
886  /// @}
887  /// @name Backend Data Access
888  /// @{
889
890  MCSectionData &getSectionData(const MCSection &Section) const {
891    MCSectionData *Entry = SectionMap.lookup(&Section);
892    assert(Entry && "Missing section data!");
893    return *Entry;
894  }
895
896  MCSectionData &getOrCreateSectionData(const MCSection &Section,
897                                        bool *Created = 0) {
898    MCSectionData *&Entry = SectionMap[&Section];
899
900    if (Created) *Created = !Entry;
901    if (!Entry)
902      Entry = new MCSectionData(Section, this);
903
904    return *Entry;
905  }
906
907  MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
908    MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
909    assert(Entry && "Missing symbol data!");
910    return *Entry;
911  }
912
913  MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
914                                      bool *Created = 0) {
915    MCSymbolData *&Entry = SymbolMap[&Symbol];
916
917    if (Created) *Created = !Entry;
918    if (!Entry)
919      Entry = new MCSymbolData(Symbol, 0, 0, this);
920
921    return *Entry;
922  }
923
924  /// @}
925
926  void dump();
927};
928
929} // end namespace llvm
930
931#endif
932