MCAssembler.h revision 0c7f116bb6950ef819323d855415b2f2b0aad987
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/DenseSet.h"
15#include "llvm/ADT/PointerIntPair.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/MC/MCDirectives.h"
21#include "llvm/MC/MCFixup.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCLinkerOptimizationHint.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/DataTypes.h"
27#include <algorithm>
28#include <vector> // FIXME: Shouldn't be needed.
29
30namespace llvm {
31class raw_ostream;
32class MCAsmLayout;
33class MCAssembler;
34class MCContext;
35class MCCodeEmitter;
36class MCExpr;
37class MCFragment;
38class MCObjectWriter;
39class MCSection;
40class MCSectionData;
41class MCSubtargetInfo;
42class MCSymbol;
43class MCSymbolData;
44class MCValue;
45class MCAsmBackend;
46
47class MCFragment : public ilist_node<MCFragment> {
48  friend class MCAsmLayout;
49
50  MCFragment(const MCFragment&) = delete;
51  void operator=(const MCFragment&) = delete;
52
53public:
54  enum FragmentType {
55    FT_Align,
56    FT_Data,
57    FT_CompactEncodedInst,
58    FT_Fill,
59    FT_Relaxable,
60    FT_Org,
61    FT_Dwarf,
62    FT_DwarfFrame,
63    FT_LEB
64  };
65
66private:
67  FragmentType Kind;
68
69  /// Parent - The data for the section this fragment is in.
70  MCSectionData *Parent;
71
72  /// Atom - The atom this fragment is in, as represented by it's defining
73  /// symbol.
74  MCSymbolData *Atom;
75
76  /// @name Assembler Backend Data
77  /// @{
78  //
79  // FIXME: This could all be kept private to the assembler implementation.
80
81  /// Offset - The offset of this fragment in its section. This is ~0 until
82  /// initialized.
83  uint64_t Offset;
84
85  /// LayoutOrder - The layout order of this fragment.
86  unsigned LayoutOrder;
87
88  /// @}
89
90protected:
91  MCFragment(FragmentType Kind, MCSectionData *Parent = nullptr);
92
93public:
94  // Only for sentinel.
95  MCFragment();
96  virtual ~MCFragment();
97
98  FragmentType getKind() const { return Kind; }
99
100  MCSectionData *getParent() const { return Parent; }
101  void setParent(MCSectionData *Value) { Parent = Value; }
102
103  MCSymbolData *getAtom() const { return Atom; }
104  void setAtom(MCSymbolData *Value) { Atom = Value; }
105
106  unsigned getLayoutOrder() const { return LayoutOrder; }
107  void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
108
109  /// \brief Does this fragment have instructions emitted into it? By default
110  /// this is false, but specific fragment types may set it to true.
111  virtual bool hasInstructions() const { return false; }
112
113  /// \brief Should this fragment be placed at the end of an aligned bundle?
114  virtual bool alignToBundleEnd() const { return false; }
115  virtual void setAlignToBundleEnd(bool V) { }
116
117  /// \brief Get the padding size that must be inserted before this fragment.
118  /// Used for bundling. By default, no padding is inserted.
119  /// Note that padding size is restricted to 8 bits. This is an optimization
120  /// to reduce the amount of space used for each fragment. In practice, larger
121  /// padding should never be required.
122  virtual uint8_t getBundlePadding() const {
123    return 0;
124  }
125
126  /// \brief Set the padding size for this fragment. By default it's a no-op,
127  /// and only some fragments have a meaningful implementation.
128  virtual void setBundlePadding(uint8_t N) {
129  }
130
131  void dump();
132};
133
134/// Interface implemented by fragments that contain encoded instructions and/or
135/// data.
136///
137class MCEncodedFragment : public MCFragment {
138  virtual void anchor();
139
140  uint8_t BundlePadding;
141public:
142  MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = nullptr)
143    : MCFragment(FType, SD), BundlePadding(0)
144  {
145  }
146  ~MCEncodedFragment() override;
147
148  virtual SmallVectorImpl<char> &getContents() = 0;
149  virtual const SmallVectorImpl<char> &getContents() const = 0;
150
151  uint8_t getBundlePadding() const override {
152    return BundlePadding;
153  }
154
155  void setBundlePadding(uint8_t N) override {
156    BundlePadding = N;
157  }
158
159  static bool classof(const MCFragment *F) {
160    MCFragment::FragmentType Kind = F->getKind();
161    switch (Kind) {
162      default:
163        return false;
164      case MCFragment::FT_Relaxable:
165      case MCFragment::FT_CompactEncodedInst:
166      case MCFragment::FT_Data:
167        return true;
168    }
169  }
170};
171
172/// Interface implemented by fragments that contain encoded instructions and/or
173/// data and also have fixups registered.
174///
175class MCEncodedFragmentWithFixups : public MCEncodedFragment {
176  void anchor() override;
177
178public:
179  MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
180                              MCSectionData *SD = nullptr)
181    : MCEncodedFragment(FType, SD)
182  {
183  }
184
185  ~MCEncodedFragmentWithFixups() override;
186
187  typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
188  typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
189
190  virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
191  virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
192
193  virtual fixup_iterator fixup_begin() = 0;
194  virtual const_fixup_iterator fixup_begin() const  = 0;
195  virtual fixup_iterator fixup_end() = 0;
196  virtual const_fixup_iterator fixup_end() const = 0;
197
198  static bool classof(const MCFragment *F) {
199    MCFragment::FragmentType Kind = F->getKind();
200    return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
201  }
202};
203
204/// Fragment for data and encoded instructions.
205///
206class MCDataFragment : public MCEncodedFragmentWithFixups {
207  void anchor() override;
208
209  /// \brief Does this fragment contain encoded instructions anywhere in it?
210  bool HasInstructions;
211
212  /// \brief Should this fragment be aligned to the end of a bundle?
213  bool AlignToBundleEnd;
214
215  SmallVector<char, 32> Contents;
216
217  /// Fixups - The list of fixups in this fragment.
218  SmallVector<MCFixup, 4> Fixups;
219public:
220  MCDataFragment(MCSectionData *SD = nullptr)
221    : MCEncodedFragmentWithFixups(FT_Data, SD),
222      HasInstructions(false), AlignToBundleEnd(false)
223  {
224  }
225
226  SmallVectorImpl<char> &getContents() override { return Contents; }
227  const SmallVectorImpl<char> &getContents() const override {
228    return Contents;
229  }
230
231  SmallVectorImpl<MCFixup> &getFixups() override {
232    return Fixups;
233  }
234
235  const SmallVectorImpl<MCFixup> &getFixups() const override {
236    return Fixups;
237  }
238
239  bool hasInstructions() const override { return HasInstructions; }
240  virtual void setHasInstructions(bool V) { HasInstructions = V; }
241
242  bool alignToBundleEnd() const override { return AlignToBundleEnd; }
243  void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
244
245  fixup_iterator fixup_begin() override { return Fixups.begin(); }
246  const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
247
248  fixup_iterator fixup_end() override {return Fixups.end();}
249  const_fixup_iterator fixup_end() const override {return Fixups.end();}
250
251  static bool classof(const MCFragment *F) {
252    return F->getKind() == MCFragment::FT_Data;
253  }
254};
255
256/// This is a compact (memory-size-wise) fragment for holding an encoded
257/// instruction (non-relaxable) that has no fixups registered. When applicable,
258/// it can be used instead of MCDataFragment and lead to lower memory
259/// consumption.
260///
261class MCCompactEncodedInstFragment : public MCEncodedFragment {
262  void anchor() override;
263
264  /// \brief Should this fragment be aligned to the end of a bundle?
265  bool AlignToBundleEnd;
266
267  SmallVector<char, 4> Contents;
268public:
269  MCCompactEncodedInstFragment(MCSectionData *SD = nullptr)
270    : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false)
271  {
272  }
273
274  bool hasInstructions() const override {
275    return true;
276  }
277
278  SmallVectorImpl<char> &getContents() override { return Contents; }
279  const SmallVectorImpl<char> &getContents() const override { return Contents; }
280
281  bool alignToBundleEnd() const override { return AlignToBundleEnd; }
282  void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
283
284  static bool classof(const MCFragment *F) {
285    return F->getKind() == MCFragment::FT_CompactEncodedInst;
286  }
287};
288
289/// A relaxable fragment holds on to its MCInst, since it may need to be
290/// relaxed during the assembler layout and relaxation stage.
291///
292class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
293  void anchor() override;
294
295  /// Inst - The instruction this is a fragment for.
296  MCInst Inst;
297
298  /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
299  /// Keep a copy instead of a reference to make sure that updates to STI
300  /// in the assembler are not seen here.
301  const MCSubtargetInfo STI;
302
303  /// Contents - Binary data for the currently encoded instruction.
304  SmallVector<char, 8> Contents;
305
306  /// Fixups - The list of fixups in this fragment.
307  SmallVector<MCFixup, 1> Fixups;
308
309public:
310  MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
311                      MCSectionData *SD = nullptr)
312      : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(Inst), STI(STI) {}
313
314  SmallVectorImpl<char> &getContents() override { return Contents; }
315  const SmallVectorImpl<char> &getContents() const override { return Contents; }
316
317  const MCInst &getInst() const { return Inst; }
318  void setInst(const MCInst& Value) { Inst = Value; }
319
320  const MCSubtargetInfo &getSubtargetInfo() { return STI; }
321
322  SmallVectorImpl<MCFixup> &getFixups() override {
323    return Fixups;
324  }
325
326  const SmallVectorImpl<MCFixup> &getFixups() const override {
327    return Fixups;
328  }
329
330  bool hasInstructions() const override { return true; }
331
332  fixup_iterator fixup_begin() override { return Fixups.begin(); }
333  const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
334
335  fixup_iterator fixup_end() override {return Fixups.end();}
336  const_fixup_iterator fixup_end() const override {return Fixups.end();}
337
338  static bool classof(const MCFragment *F) {
339    return F->getKind() == MCFragment::FT_Relaxable;
340  }
341};
342
343class MCAlignFragment : public MCFragment {
344  virtual void anchor();
345
346  /// Alignment - The alignment to ensure, in bytes.
347  unsigned Alignment;
348
349  /// Value - Value to use for filling padding bytes.
350  int64_t Value;
351
352  /// ValueSize - The size of the integer (in bytes) of \p Value.
353  unsigned ValueSize;
354
355  /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
356  /// cannot be satisfied in this width then this fragment is ignored.
357  unsigned MaxBytesToEmit;
358
359  /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
360  /// of using the provided value. The exact interpretation of this flag is
361  /// target dependent.
362  bool EmitNops : 1;
363
364public:
365  MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
366                  unsigned MaxBytesToEmit, MCSectionData *SD = nullptr)
367      : MCFragment(FT_Align, SD), Alignment(Alignment), Value(Value),
368        ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit), EmitNops(false) {}
369
370  /// @name Accessors
371  /// @{
372
373  unsigned getAlignment() const { return Alignment; }
374
375  int64_t getValue() const { return Value; }
376
377  unsigned getValueSize() const { return ValueSize; }
378
379  unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
380
381  bool hasEmitNops() const { return EmitNops; }
382  void setEmitNops(bool Value) { EmitNops = Value; }
383
384  /// @}
385
386  static bool classof(const MCFragment *F) {
387    return F->getKind() == MCFragment::FT_Align;
388  }
389};
390
391class MCFillFragment : public MCFragment {
392  virtual void anchor();
393
394  /// Value - Value to use for filling bytes.
395  int64_t Value;
396
397  /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
398  /// this is a virtual fill fragment.
399  unsigned ValueSize;
400
401  /// Size - The number of bytes to insert.
402  uint64_t Size;
403
404public:
405  MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
406                 MCSectionData *SD = nullptr)
407      : MCFragment(FT_Fill, SD), Value(Value), ValueSize(ValueSize),
408        Size(Size) {
409    assert((!ValueSize || (Size % ValueSize) == 0) &&
410           "Fill size must be a multiple of the value size!");
411  }
412
413  /// @name Accessors
414  /// @{
415
416  int64_t getValue() const { return Value; }
417
418  unsigned getValueSize() const { return ValueSize; }
419
420  uint64_t getSize() const { return Size; }
421
422  /// @}
423
424  static bool classof(const MCFragment *F) {
425    return F->getKind() == MCFragment::FT_Fill;
426  }
427};
428
429class MCOrgFragment : public MCFragment {
430  virtual void anchor();
431
432  /// Offset - The offset this fragment should start at.
433  const MCExpr *Offset;
434
435  /// Value - Value to use for filling bytes.
436  int8_t Value;
437
438public:
439  MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSectionData *SD = nullptr)
440      : MCFragment(FT_Org, SD), Offset(&Offset), Value(Value) {}
441
442  /// @name Accessors
443  /// @{
444
445  const MCExpr &getOffset() const { return *Offset; }
446
447  uint8_t getValue() const { return Value; }
448
449  /// @}
450
451  static bool classof(const MCFragment *F) {
452    return F->getKind() == MCFragment::FT_Org;
453  }
454};
455
456class MCLEBFragment : public MCFragment {
457  virtual void anchor();
458
459  /// Value - The value this fragment should contain.
460  const MCExpr *Value;
461
462  /// IsSigned - True if this is a sleb128, false if uleb128.
463  bool IsSigned;
464
465  SmallString<8> Contents;
466public:
467  MCLEBFragment(const MCExpr &Value_, bool IsSigned_,
468                MCSectionData *SD = nullptr)
469    : MCFragment(FT_LEB, SD),
470      Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
471
472  /// @name Accessors
473  /// @{
474
475  const MCExpr &getValue() const { return *Value; }
476
477  bool isSigned() const { return IsSigned; }
478
479  SmallString<8> &getContents() { return Contents; }
480  const SmallString<8> &getContents() const { return Contents; }
481
482  /// @}
483
484  static bool classof(const MCFragment *F) {
485    return F->getKind() == MCFragment::FT_LEB;
486  }
487};
488
489class MCDwarfLineAddrFragment : public MCFragment {
490  virtual void anchor();
491
492  /// LineDelta - the value of the difference between the two line numbers
493  /// between two .loc dwarf directives.
494  int64_t LineDelta;
495
496  /// AddrDelta - The expression for the difference of the two symbols that
497  /// make up the address delta between two .loc dwarf directives.
498  const MCExpr *AddrDelta;
499
500  SmallString<8> Contents;
501
502public:
503  MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
504                          MCSectionData *SD = nullptr)
505      : MCFragment(FT_Dwarf, SD), LineDelta(LineDelta), AddrDelta(&AddrDelta) {
506    Contents.push_back(0);
507  }
508
509  /// @name Accessors
510  /// @{
511
512  int64_t getLineDelta() const { return LineDelta; }
513
514  const MCExpr &getAddrDelta() const { return *AddrDelta; }
515
516  SmallString<8> &getContents() { return Contents; }
517  const SmallString<8> &getContents() const { return Contents; }
518
519  /// @}
520
521  static bool classof(const MCFragment *F) {
522    return F->getKind() == MCFragment::FT_Dwarf;
523  }
524};
525
526class MCDwarfCallFrameFragment : public MCFragment {
527  virtual void anchor();
528
529  /// AddrDelta - The expression for the difference of the two symbols that
530  /// make up the address delta between two .cfi_* dwarf directives.
531  const MCExpr *AddrDelta;
532
533  SmallString<8> Contents;
534
535public:
536  MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSectionData *SD = nullptr)
537      : MCFragment(FT_DwarfFrame, SD), AddrDelta(&AddrDelta) {
538    Contents.push_back(0);
539  }
540
541  /// @name Accessors
542  /// @{
543
544  const MCExpr &getAddrDelta() const { return *AddrDelta; }
545
546  SmallString<8> &getContents() { return Contents; }
547  const SmallString<8> &getContents() const { return Contents; }
548
549  /// @}
550
551  static bool classof(const MCFragment *F) {
552    return F->getKind() == MCFragment::FT_DwarfFrame;
553  }
554};
555
556// FIXME: Should this be a separate class, or just merged into MCSection? Since
557// we anticipate the fast path being through an MCAssembler, the only reason to
558// keep it out is for API abstraction.
559class MCSectionData : public ilist_node<MCSectionData> {
560  friend class MCAsmLayout;
561
562  MCSectionData(const MCSectionData&) = delete;
563  void operator=(const MCSectionData&) = delete;
564
565public:
566  typedef iplist<MCFragment> FragmentListType;
567
568  typedef FragmentListType::const_iterator const_iterator;
569  typedef FragmentListType::iterator iterator;
570
571  typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
572  typedef FragmentListType::reverse_iterator reverse_iterator;
573
574  /// \brief Express the state of bundle locked groups while emitting code.
575  enum BundleLockStateType {
576    NotBundleLocked,
577    BundleLocked,
578    BundleLockedAlignToEnd
579  };
580private:
581  FragmentListType Fragments;
582  const MCSection *Section;
583
584  /// Ordinal - The section index in the assemblers section list.
585  unsigned Ordinal;
586
587  /// LayoutOrder - The index of this section in the layout order.
588  unsigned LayoutOrder;
589
590  /// Alignment - The maximum alignment seen in this section.
591  unsigned Alignment;
592
593  /// \brief Keeping track of bundle-locked state.
594  BundleLockStateType BundleLockState;
595
596  /// \brief Current nesting depth of bundle_lock directives.
597  unsigned BundleLockNestingDepth;
598
599  /// \brief We've seen a bundle_lock directive but not its first instruction
600  /// yet.
601  bool BundleGroupBeforeFirstInst;
602
603  /// @name Assembler Backend Data
604  /// @{
605  //
606  // FIXME: This could all be kept private to the assembler implementation.
607
608  /// HasInstructions - Whether this section has had instructions emitted into
609  /// it.
610  unsigned HasInstructions : 1;
611
612  /// Mapping from subsection number to insertion point for subsection numbers
613  /// below that number.
614  SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
615
616  /// @}
617
618public:
619  // Only for use as sentinel.
620  MCSectionData();
621  MCSectionData(const MCSection &Section, MCAssembler *A = nullptr);
622
623  const MCSection &getSection() const { return *Section; }
624
625  unsigned getAlignment() const { return Alignment; }
626  void setAlignment(unsigned Value) { Alignment = Value; }
627
628  bool hasInstructions() const { return HasInstructions; }
629  void setHasInstructions(bool Value) { HasInstructions = Value; }
630
631  unsigned getOrdinal() const { return Ordinal; }
632  void setOrdinal(unsigned Value) { Ordinal = Value; }
633
634  unsigned getLayoutOrder() const { return LayoutOrder; }
635  void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
636
637  /// @name Fragment Access
638  /// @{
639
640  const FragmentListType &getFragmentList() const { return Fragments; }
641  FragmentListType &getFragmentList() { return Fragments; }
642
643  iterator begin() { return Fragments.begin(); }
644  const_iterator begin() const { return Fragments.begin(); }
645
646  iterator end() { return Fragments.end(); }
647  const_iterator end() const { return Fragments.end(); }
648
649  reverse_iterator rbegin() { return Fragments.rbegin(); }
650  const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
651
652  reverse_iterator rend() { return Fragments.rend(); }
653  const_reverse_iterator rend() const { return Fragments.rend(); }
654
655  size_t size() const { return Fragments.size(); }
656
657  bool empty() const { return Fragments.empty(); }
658
659  iterator getSubsectionInsertionPoint(unsigned Subsection);
660
661  bool isBundleLocked() const {
662    return BundleLockState != NotBundleLocked;
663  }
664
665  BundleLockStateType getBundleLockState() const {
666    return BundleLockState;
667  }
668
669  void setBundleLockState(BundleLockStateType NewState);
670
671  bool isBundleGroupBeforeFirstInst() const {
672    return BundleGroupBeforeFirstInst;
673  }
674
675  void setBundleGroupBeforeFirstInst(bool IsFirst) {
676    BundleGroupBeforeFirstInst = IsFirst;
677  }
678
679  void dump();
680
681  /// @}
682};
683
684// FIXME: Same concerns as with SectionData.
685class MCSymbolData : public ilist_node<MCSymbolData> {
686  const MCSymbol *Symbol;
687
688  /// Fragment - The fragment this symbol's value is relative to, if any. Also
689  /// stores if this symbol is visible outside this translation unit (bit 0) or
690  /// if it is private extern (bit 1).
691  PointerIntPair<MCFragment *, 2> Fragment;
692
693  union {
694    /// Offset - The offset to apply to the fragment address to form this
695    /// symbol's value.
696    uint64_t Offset;
697
698    /// CommonSize - The size of the symbol, if it is 'common'.
699    uint64_t CommonSize;
700  };
701
702  /// SymbolSize - An expression describing how to calculate the size of
703  /// a symbol. If a symbol has no size this field will be NULL.
704  const MCExpr *SymbolSize;
705
706  /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
707  //
708  // FIXME: Pack this in with other fields?
709  unsigned CommonAlign;
710
711  /// Flags - The Flags field is used by object file implementations to store
712  /// additional per symbol information which is not easily classified.
713  uint32_t Flags;
714
715  /// Index - Index field, for use by the object file implementation.
716  uint64_t Index;
717
718public:
719  // Only for use as sentinel.
720  MCSymbolData();
721  MCSymbolData(const MCSymbol &Symbol, MCFragment *Fragment, uint64_t Offset,
722               MCAssembler *A = nullptr);
723
724  /// @name Accessors
725  /// @{
726
727  const MCSymbol &getSymbol() const { return *Symbol; }
728
729  MCFragment *getFragment() const { return Fragment.getPointer(); }
730  void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
731
732  uint64_t getOffset() const {
733    assert(!isCommon());
734    return Offset;
735  }
736  void setOffset(uint64_t Value) {
737    assert(!isCommon());
738    Offset = Value;
739  }
740
741  /// @}
742  /// @name Symbol Attributes
743  /// @{
744
745  bool isExternal() const { return Fragment.getInt() & 1; }
746  void setExternal(bool Value) {
747    Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
748  }
749
750  bool isPrivateExtern() const { return Fragment.getInt() & 2; }
751  void setPrivateExtern(bool Value) {
752    Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
753  }
754
755  /// isCommon - Is this a 'common' symbol.
756  bool isCommon() const { return CommonAlign != -1U; }
757
758  /// setCommon - Mark this symbol as being 'common'.
759  ///
760  /// \param Size - The size of the symbol.
761  /// \param Align - The alignment of the symbol.
762  void setCommon(uint64_t Size, unsigned Align) {
763    assert(getOffset() == 0);
764    CommonSize = Size;
765    CommonAlign = Align;
766  }
767
768  /// getCommonSize - Return the size of a 'common' symbol.
769  uint64_t getCommonSize() const {
770    assert(isCommon() && "Not a 'common' symbol!");
771    return CommonSize;
772  }
773
774  void setSize(const MCExpr *SS) {
775    SymbolSize = SS;
776  }
777
778  const MCExpr *getSize() const {
779    return SymbolSize;
780  }
781
782
783  /// getCommonAlignment - Return the alignment of a 'common' symbol.
784  unsigned getCommonAlignment() const {
785    assert(isCommon() && "Not a 'common' symbol!");
786    return CommonAlign;
787  }
788
789  /// getFlags - Get the (implementation defined) symbol flags.
790  uint32_t getFlags() const { return Flags; }
791
792  /// setFlags - Set the (implementation defined) symbol flags.
793  void setFlags(uint32_t Value) { Flags = Value; }
794
795  /// modifyFlags - Modify the flags via a mask
796  void modifyFlags(uint32_t Value, uint32_t Mask) {
797    Flags = (Flags & ~Mask) | Value;
798  }
799
800  /// getIndex - Get the (implementation defined) index.
801  uint64_t getIndex() const { return Index; }
802
803  /// setIndex - Set the (implementation defined) index.
804  void setIndex(uint64_t Value) { Index = Value; }
805
806  /// @}
807
808  void dump() const;
809};
810
811// FIXME: This really doesn't belong here. See comments below.
812struct IndirectSymbolData {
813  MCSymbol *Symbol;
814  MCSectionData *SectionData;
815};
816
817// FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
818// to one another.
819struct DataRegionData {
820  // This enum should be kept in sync w/ the mach-o definition in
821  // llvm/Object/MachOFormat.h.
822  enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
823  MCSymbol *Start;
824  MCSymbol *End;
825};
826
827class MCAssembler {
828  friend class MCAsmLayout;
829
830public:
831  typedef iplist<MCSectionData> SectionDataListType;
832  typedef iplist<MCSymbolData> SymbolDataListType;
833
834  typedef SectionDataListType::const_iterator const_iterator;
835  typedef SectionDataListType::iterator iterator;
836
837  typedef SymbolDataListType::const_iterator const_symbol_iterator;
838  typedef SymbolDataListType::iterator symbol_iterator;
839
840  typedef iterator_range<symbol_iterator> symbol_range;
841  typedef iterator_range<const_symbol_iterator> const_symbol_range;
842
843  typedef std::vector<std::string> FileNameVectorType;
844  typedef FileNameVectorType::const_iterator const_file_name_iterator;
845
846  typedef std::vector<IndirectSymbolData>::const_iterator
847    const_indirect_symbol_iterator;
848  typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
849
850  typedef std::vector<DataRegionData>::const_iterator
851    const_data_region_iterator;
852  typedef std::vector<DataRegionData>::iterator data_region_iterator;
853
854  /// MachO specific deployment target version info.
855  // A Major version of 0 indicates that no version information was supplied
856  // and so the corresponding load command should not be emitted.
857  typedef struct {
858    MCVersionMinType Kind;
859    unsigned Major;
860    unsigned Minor;
861    unsigned Update;
862  } VersionMinInfoType;
863private:
864  MCAssembler(const MCAssembler&) = delete;
865  void operator=(const MCAssembler&) = delete;
866
867  MCContext &Context;
868
869  MCAsmBackend &Backend;
870
871  MCCodeEmitter &Emitter;
872
873  MCObjectWriter &Writer;
874
875  raw_ostream &OS;
876
877  iplist<MCSectionData> Sections;
878
879  iplist<MCSymbolData> Symbols;
880
881  DenseSet<const MCSymbol *> LocalsUsedInReloc;
882
883  /// The map of sections to their associated assembler backend data.
884  //
885  // FIXME: Avoid this indirection?
886  DenseMap<const MCSection*, MCSectionData*> SectionMap;
887
888  /// The map of symbols to their associated assembler backend data.
889  //
890  // FIXME: Avoid this indirection?
891  DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
892
893  std::vector<IndirectSymbolData> IndirectSymbols;
894
895  std::vector<DataRegionData> DataRegions;
896
897  /// The list of linker options to propagate into the object file.
898  std::vector<std::vector<std::string> > LinkerOptions;
899
900  /// List of declared file names
901  FileNameVectorType FileNames;
902
903  /// The set of function symbols for which a .thumb_func directive has
904  /// been seen.
905  //
906  // FIXME: We really would like this in target specific code rather than
907  // here. Maybe when the relocation stuff moves to target specific,
908  // this can go with it? The streamer would need some target specific
909  // refactoring too.
910  mutable SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
911
912  /// \brief The bundle alignment size currently set in the assembler.
913  ///
914  /// By default it's 0, which means bundling is disabled.
915  unsigned BundleAlignSize;
916
917  unsigned RelaxAll : 1;
918  unsigned SubsectionsViaSymbols : 1;
919
920  /// ELF specific e_header flags
921  // It would be good if there were an MCELFAssembler class to hold this.
922  // ELF header flags are used both by the integrated and standalone assemblers.
923  // Access to the flags is necessary in cases where assembler directives affect
924  // which flags to be set.
925  unsigned ELFHeaderEFlags;
926
927  /// Used to communicate Linker Optimization Hint information between
928  /// the Streamer and the .o writer
929  MCLOHContainer LOHContainer;
930
931  VersionMinInfoType VersionMinInfo;
932private:
933  /// Evaluate a fixup to a relocatable expression and the value which should be
934  /// placed into the fixup.
935  ///
936  /// \param Layout The layout to use for evaluation.
937  /// \param Fixup The fixup to evaluate.
938  /// \param DF The fragment the fixup is inside.
939  /// \param Target [out] On return, the relocatable expression the fixup
940  /// evaluates to.
941  /// \param Value [out] On return, the value of the fixup as currently laid
942  /// out.
943  /// \return Whether the fixup value was fully resolved. This is true if the
944  /// \p Value result is fixed, otherwise the value may change due to
945  /// relocation.
946  bool evaluateFixup(const MCAsmLayout &Layout,
947                     const MCFixup &Fixup, const MCFragment *DF,
948                     MCValue &Target, uint64_t &Value) const;
949
950  /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
951  /// (increased in size, in order to hold its value correctly).
952  bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
953                            const MCAsmLayout &Layout) const;
954
955  /// Check whether the given fragment needs relaxation.
956  bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
957                               const MCAsmLayout &Layout) const;
958
959  /// \brief Perform one layout iteration and return true if any offsets
960  /// were adjusted.
961  bool layoutOnce(MCAsmLayout &Layout);
962
963  /// \brief Perform one layout iteration of the given section and return true
964  /// if any offsets were adjusted.
965  bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
966
967  bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
968
969  bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
970
971  bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
972  bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
973                                   MCDwarfCallFrameFragment &DF);
974
975  /// finishLayout - Finalize a layout, including fragment lowering.
976  void finishLayout(MCAsmLayout &Layout);
977
978  std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
979                                        MCFragment &F, const MCFixup &Fixup);
980
981public:
982  void addLocalUsedInReloc(const MCSymbol &Sym);
983  bool isLocalUsedInReloc(const MCSymbol &Sym) const;
984
985  /// Compute the effective fragment size assuming it is laid out at the given
986  /// \p SectionAddress and \p FragmentOffset.
987  uint64_t computeFragmentSize(const MCAsmLayout &Layout,
988                               const MCFragment &F) const;
989
990  /// Find the symbol which defines the atom containing the given symbol, or
991  /// null if there is no such symbol.
992  const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
993
994  /// Check whether a particular symbol is visible to the linker and is required
995  /// in the symbol table, or whether it can be discarded by the assembler. This
996  /// also effects whether the assembler treats the label as potentially
997  /// defining a separate atom.
998  bool isSymbolLinkerVisible(const MCSymbol &SD) const;
999
1000  /// Emit the section contents using the given object writer.
1001  void writeSectionData(const MCSectionData *Section,
1002                        const MCAsmLayout &Layout) const;
1003
1004  /// Check whether a given symbol has been flagged with .thumb_func.
1005  bool isThumbFunc(const MCSymbol *Func) const;
1006
1007  /// Flag a function symbol as the target of a .thumb_func directive.
1008  void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
1009
1010  /// ELF e_header flags
1011  unsigned getELFHeaderEFlags() const {return ELFHeaderEFlags;}
1012  void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags;}
1013
1014  /// MachO deployment target version information.
1015  const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
1016  void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
1017                         unsigned Update) {
1018    VersionMinInfo.Kind = Kind;
1019    VersionMinInfo.Major = Major;
1020    VersionMinInfo.Minor = Minor;
1021    VersionMinInfo.Update = Update;
1022  }
1023
1024public:
1025  /// Construct a new assembler instance.
1026  ///
1027  /// \param OS The stream to output to.
1028  //
1029  // FIXME: How are we going to parameterize this? Two obvious options are stay
1030  // concrete and require clients to pass in a target like object. The other
1031  // option is to make this abstract, and have targets provide concrete
1032  // implementations as we do with AsmParser.
1033  MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
1034              MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
1035              raw_ostream &OS);
1036  ~MCAssembler();
1037
1038  /// Reuse an assembler instance
1039  ///
1040  void reset();
1041
1042  MCContext &getContext() const { return Context; }
1043
1044  MCAsmBackend &getBackend() const { return Backend; }
1045
1046  MCCodeEmitter &getEmitter() const { return Emitter; }
1047
1048  MCObjectWriter &getWriter() const { return Writer; }
1049
1050  /// Finish - Do final processing and write the object to the output stream.
1051  /// \p Writer is used for custom object writer (as the MCJIT does),
1052  /// if not specified it is automatically created from backend.
1053  void Finish();
1054
1055  // FIXME: This does not belong here.
1056  bool getSubsectionsViaSymbols() const {
1057    return SubsectionsViaSymbols;
1058  }
1059  void setSubsectionsViaSymbols(bool Value) {
1060    SubsectionsViaSymbols = Value;
1061  }
1062
1063  bool getRelaxAll() const { return RelaxAll; }
1064  void setRelaxAll(bool Value) { RelaxAll = Value; }
1065
1066  bool isBundlingEnabled() const {
1067    return BundleAlignSize != 0;
1068  }
1069
1070  unsigned getBundleAlignSize() const {
1071    return BundleAlignSize;
1072  }
1073
1074  void setBundleAlignSize(unsigned Size) {
1075    assert((Size == 0 || !(Size & (Size - 1))) &&
1076           "Expect a power-of-two bundle align size");
1077    BundleAlignSize = Size;
1078  }
1079
1080  /// @name Section List Access
1081  /// @{
1082
1083  const SectionDataListType &getSectionList() const { return Sections; }
1084  SectionDataListType &getSectionList() { return Sections; }
1085
1086  iterator begin() { return Sections.begin(); }
1087  const_iterator begin() const { return Sections.begin(); }
1088
1089  iterator end() { return Sections.end(); }
1090  const_iterator end() const { return Sections.end(); }
1091
1092  size_t size() const { return Sections.size(); }
1093
1094  /// @}
1095  /// @name Symbol List Access
1096  /// @{
1097
1098  const SymbolDataListType &getSymbolList() const { return Symbols; }
1099  SymbolDataListType &getSymbolList() { return Symbols; }
1100
1101  symbol_iterator symbol_begin() { return Symbols.begin(); }
1102  const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
1103
1104  symbol_iterator symbol_end() { return Symbols.end(); }
1105  const_symbol_iterator symbol_end() const { return Symbols.end(); }
1106
1107  symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
1108  const_symbol_range symbols() const { return make_range(symbol_begin(), symbol_end()); }
1109
1110  size_t symbol_size() const { return Symbols.size(); }
1111
1112  /// @}
1113  /// @name Indirect Symbol List Access
1114  /// @{
1115
1116  // FIXME: This is a total hack, this should not be here. Once things are
1117  // factored so that the streamer has direct access to the .o writer, it can
1118  // disappear.
1119  std::vector<IndirectSymbolData> &getIndirectSymbols() {
1120    return IndirectSymbols;
1121  }
1122
1123  indirect_symbol_iterator indirect_symbol_begin() {
1124    return IndirectSymbols.begin();
1125  }
1126  const_indirect_symbol_iterator indirect_symbol_begin() const {
1127    return IndirectSymbols.begin();
1128  }
1129
1130  indirect_symbol_iterator indirect_symbol_end() {
1131    return IndirectSymbols.end();
1132  }
1133  const_indirect_symbol_iterator indirect_symbol_end() const {
1134    return IndirectSymbols.end();
1135  }
1136
1137  size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
1138
1139  /// @}
1140  /// @name Linker Option List Access
1141  /// @{
1142
1143  std::vector<std::vector<std::string> > &getLinkerOptions() {
1144    return LinkerOptions;
1145  }
1146
1147  /// @}
1148  /// @name Data Region List Access
1149  /// @{
1150
1151  // FIXME: This is a total hack, this should not be here. Once things are
1152  // factored so that the streamer has direct access to the .o writer, it can
1153  // disappear.
1154  std::vector<DataRegionData> &getDataRegions() {
1155    return DataRegions;
1156  }
1157
1158  data_region_iterator data_region_begin() {
1159    return DataRegions.begin();
1160  }
1161  const_data_region_iterator data_region_begin() const {
1162    return DataRegions.begin();
1163  }
1164
1165  data_region_iterator data_region_end() {
1166    return DataRegions.end();
1167  }
1168  const_data_region_iterator data_region_end() const {
1169    return DataRegions.end();
1170  }
1171
1172  size_t data_region_size() const { return DataRegions.size(); }
1173
1174  /// @}
1175  /// @name Data Region List Access
1176  /// @{
1177
1178  // FIXME: This is a total hack, this should not be here. Once things are
1179  // factored so that the streamer has direct access to the .o writer, it can
1180  // disappear.
1181  MCLOHContainer & getLOHContainer() {
1182    return LOHContainer;
1183  }
1184  const MCLOHContainer & getLOHContainer() const {
1185    return const_cast<MCAssembler *>(this)->getLOHContainer();
1186  }
1187  /// @}
1188  /// @name Backend Data Access
1189  /// @{
1190
1191  MCSectionData &getSectionData(const MCSection &Section) const {
1192    MCSectionData *Entry = SectionMap.lookup(&Section);
1193    assert(Entry && "Missing section data!");
1194    return *Entry;
1195  }
1196
1197  MCSectionData &getOrCreateSectionData(const MCSection &Section,
1198                                        bool *Created = nullptr) {
1199    MCSectionData *&Entry = SectionMap[&Section];
1200
1201    if (Created) *Created = !Entry;
1202    if (!Entry)
1203      Entry = new MCSectionData(Section, this);
1204
1205    return *Entry;
1206  }
1207
1208  bool hasSymbolData(const MCSymbol &Symbol) const {
1209    return SymbolMap.lookup(&Symbol) != nullptr;
1210  }
1211
1212  MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1213    return const_cast<MCSymbolData &>(
1214        static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1215  }
1216
1217  const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1218    MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
1219    assert(Entry && "Missing symbol data!");
1220    return *Entry;
1221  }
1222
1223  MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1224                                      bool *Created = nullptr) {
1225    MCSymbolData *&Entry = SymbolMap[&Symbol];
1226
1227    if (Created) *Created = !Entry;
1228    if (!Entry)
1229      Entry = new MCSymbolData(Symbol, nullptr, 0, this);
1230
1231    return *Entry;
1232  }
1233
1234  const_file_name_iterator file_names_begin() const {
1235    return FileNames.begin();
1236  }
1237
1238  const_file_name_iterator file_names_end() const {
1239    return FileNames.end();
1240  }
1241
1242  void addFileName(StringRef FileName) {
1243    if (std::find(file_names_begin(), file_names_end(), FileName) ==
1244        file_names_end())
1245      FileNames.push_back(FileName);
1246  }
1247
1248  /// \brief Write the necessary bundle padding to the given object writer.
1249  /// Expects a fragment \p F containing instructions and its size \p FSize.
1250  void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
1251                            MCObjectWriter *OW) const;
1252
1253  /// @}
1254
1255  void dump();
1256};
1257
1258/// \brief Compute the amount of padding required before the fragment \p F to
1259/// obey bundling restrictions, where \p FOffset is the fragment's offset in
1260/// its section and \p FSize is the fragment's size.
1261uint64_t computeBundlePadding(const MCAssembler &Assembler,
1262                              const MCFragment *F,
1263                              uint64_t FOffset, uint64_t FSize);
1264
1265} // end namespace llvm
1266
1267#endif
1268