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