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