MC: Move the backend section and symbol data maps to MCAssembler.
[oota-llvm.git] / include / llvm / MC / MCAssembler.h
index 6b609d88c19087150a02877ed31e6d618ad4db90..0a296cd297407863f186289b855f52d30d84857d 100644 (file)
 #ifndef LLVM_MC_MCASSEMBLER_H
 #define LLVM_MC_MCASSEMBLER_H
 
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/ilist.h"
 #include "llvm/ADT/ilist_node.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/MC/MCFixup.h"
 #include "llvm/System/DataTypes.h"
 #include <vector> // FIXME: Shouldn't be needed.
 
@@ -37,8 +39,8 @@ struct MCAsmFixup {
   /// Value - The expression to eventually write into the fragment.
   const MCExpr *Value;
 
-  /// Size - The fixup size.
-  unsigned Size;
+  /// Kind - The fixup kind.
+  MCFixupKind Kind;
 
   /// FixedValue - The value to replace the fix up by.
   //
@@ -46,18 +48,14 @@ struct MCAsmFixup {
   uint64_t FixedValue;
 
 public:
-  MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, unsigned _Size)
-    : Offset(_Offset), Value(&_Value), Size(_Size), FixedValue(0) {}
+  MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
+    : Offset(_Offset), Value(&_Value), Kind(_Kind), FixedValue(0) {}
 };
 
 class MCFragment : public ilist_node<MCFragment> {
   MCFragment(const MCFragment&);     // DO NOT IMPLEMENT
   void operator=(const MCFragment&); // DO NOT IMPLEMENT
 
-public:
-  typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
-  typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
-
 public:
   enum FragmentType {
     FT_Data,
@@ -85,11 +83,6 @@ private:
   /// FileSize - The file size of this section. This is ~0 until initialized.
   uint64_t FileSize;
 
-  /// Fixups - The list of fixups in this fragment.
-  //
-  // FIXME: This should be sunk into MCDataFragment.
-  std::vector<MCAsmFixup> Fixups;
-
   /// @}
 
 protected:
@@ -111,36 +104,6 @@ public:
     return 0;
   }
 
-  /// @name Fixup Access
-  /// @{
-
-  /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
-  /// Offset.
-  ///
-  /// If multiple fixups exist for the same fragment and offset it is undefined
-  /// which one is returned.
-  //
-  // FIXME: This isn't horribly slow in practice, but there are much nicer
-  // solutions to applying the fixups. This will be fixed by sinking fixups into
-  // data fragments exclusively.
-  const MCAsmFixup *LookupFixup(uint64_t Offset) const {
-    for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
-      if (Fixups[i].Offset == Offset)
-        return &Fixups[i];
-    return 0;
-  }
-
-  std::vector<MCAsmFixup> &getFixups() { return Fixups; }
-  const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
-
-  fixup_iterator fixup_begin() { return Fixups.begin(); }
-  const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
-
-  fixup_iterator fixup_end() {return Fixups.end();}
-  const_fixup_iterator fixup_end() const {return Fixups.end();}
-
-  size_t fixup_size() const { return Fixups.size(); }
-
   /// @name Assembler Backend Support
   /// @{
   //
@@ -173,6 +136,13 @@ public:
 class MCDataFragment : public MCFragment {
   SmallString<32> Contents;
 
+  /// Fixups - The list of fixups in this fragment.
+  std::vector<MCAsmFixup> Fixups;
+
+public:
+  typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
+  typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
+
 public:
   MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
 
@@ -188,6 +158,22 @@ public:
 
   /// @}
 
+  /// @name Fixup Access
+  /// @{
+
+  std::vector<MCAsmFixup> &getFixups() { return Fixups; }
+  const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
+
+  fixup_iterator fixup_begin() { return Fixups.begin(); }
+  const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
+
+  fixup_iterator fixup_end() {return Fixups.end();}
+  const_fixup_iterator fixup_end() const {return Fixups.end();}
+
+  size_t fixup_size() const { return Fixups.size(); }
+
+  /// @}
+
   static bool classof(const MCFragment *F) {
     return F->getKind() == MCFragment::FT_Data;
   }
@@ -210,12 +196,16 @@ class MCAlignFragment : public MCFragment {
   /// cannot be satisfied in this width then this fragment is ignored.
   unsigned MaxBytesToEmit;
 
+  /// EmitNops - true when aligning code and optimal nops to be used for filling
+  bool EmitNops;
+
 public:
   MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
-                  unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
+                  unsigned _MaxBytesToEmit, bool _EmitNops,
+                 MCSectionData *SD = 0)
     : MCFragment(FT_Align, SD), Alignment(_Alignment),
       Value(_Value),ValueSize(_ValueSize),
-      MaxBytesToEmit(_MaxBytesToEmit) {}
+      MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
 
   /// @name Accessors
   /// @{
@@ -232,6 +222,8 @@ public:
 
   unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
 
+  unsigned getEmitNops() const { return EmitNops; }
+
   /// @}
 
   static bool classof(const MCFragment *F) {
@@ -596,6 +588,16 @@ private:
 
   iplist<MCSymbolData> Symbols;
 
+  /// The map of sections to their associated assembler backend data.
+  //
+  // FIXME: Avoid this indirection?
+  DenseMap<const MCSection*, MCSectionData*> SectionMap;
+
+  /// The map of symbols to their associated assembler backend data.
+  //
+  // FIXME: Avoid this indirection?
+  DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
+
   std::vector<IndirectSymbolData> IndirectSymbols;
 
   unsigned SubsectionsViaSymbols : 1;
@@ -681,6 +683,44 @@ public:
 
   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
 
+  /// @}
+  /// @name Backend Data Access
+  /// @{
+
+  MCSectionData &getSectionData(const MCSection &Section) {
+    MCSectionData *&Entry = SectionMap[&Section];
+    assert(Entry && "Missing section data!");
+    return *Entry;
+  }
+
+  MCSectionData &getOrCreateSectionData(const MCSection &Section,
+                                        bool *Created = 0) {
+    MCSectionData *&Entry = SectionMap[&Section];
+
+    if (Created) *Created = !Entry;
+    if (!Entry)
+      Entry = new MCSectionData(Section, this);
+
+    return *Entry;
+  }
+
+  MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
+    MCSymbolData *&Entry = SymbolMap[&Symbol];
+    assert(Entry && "Missing symbol data!");
+    return *Entry;
+  }
+
+  MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
+                                      bool *Created = 0) {
+    MCSymbolData *&Entry = SymbolMap[&Symbol];
+
+    if (Created) *Created = !Entry;
+    if (!Entry)
+      Entry = new MCSymbolData(Symbol, 0, 0, this);
+
+    return *Entry;
+  }
+
   /// @}
 
   void dump();