MCAssembler.h revision 94edc64ba23f71c31d5dee0b6942584a14cedcb5
1a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com//===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
2a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com//
3a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com//                     The LLVM Compiler Infrastructure
4a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com//
5a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com// This file is distributed under the University of Illinois Open Source
6a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com// License. See LICENSE.TXT for details.
7a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com//
8a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com//===----------------------------------------------------------------------===//
9d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com
10cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#ifndef LLVM_MC_MCASSEMBLER_H
112a48c3adb7cf4fc754f99a41352210b4a99edf04bsalomon@google.com#define LLVM_MC_MCASSEMBLER_H
122a48c3adb7cf4fc754f99a41352210b4a99edf04bsalomon@google.com
132a48c3adb7cf4fc754f99a41352210b4a99edf04bsalomon@google.com#include "llvm/MC/MCFixup.h"
142a48c3adb7cf4fc754f99a41352210b4a99edf04bsalomon@google.com#include "llvm/MC/MCInst.h"
15396e61fe440590744345e0c56970b26ab464591dbsalomon@google.com#include "llvm/ADT/DenseMap.h"
1667b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com#include "llvm/ADT/SmallPtrSet.h"
17c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com#include "llvm/ADT/SmallString.h"
18a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com#include "llvm/ADT/ilist.h"
198f6884aab8aecd7657cf3f9cdbc682f0deca29c5tfarina@chromium.org#include "llvm/ADT/ilist_node.h"
20a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com#include "llvm/Support/Casting.h"
212db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com#include "llvm/Support/DataTypes.h"
22223137f49d1a4e805f5c1b1c20b7fd68719ac54btfarina@chromium.org#include <vector> // FIXME: Shouldn't be needed.
23c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
24c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.comnamespace llvm {
25e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.orgclass raw_ostream;
2631ec7985f2b52a0cab4aa714a613b918cf663c08bsalomon@google.comclass MCAsmLayout;
272db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCAssembler;
282db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCContext;
292db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCCodeEmitter;
302db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCExpr;
312db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCFragment;
32054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.comclass MCObjectWriter;
332db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCSection;
342db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCSectionData;
352db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCSymbol;
362db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCSymbolData;
372db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCValue;
38054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.comclass MCAsmBackend;
392db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
402db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCFragment : public ilist_node<MCFragment> {
412db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  friend class MCAsmLayout;
422db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
43054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.com  MCFragment(const MCFragment&);     // DO NOT IMPLEMENT
4405a2ee052c9ef4c781b7b590b00b3d2da3b3449askia.committer@gmail.com  void operator=(const MCFragment&); // DO NOT IMPLEMENT
45054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.com
46054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.compublic:
47949eef0af2f5b47000e637347801cf2970092a38commit-bot@chromium.org  enum FragmentType {
48949eef0af2f5b47000e637347801cf2970092a38commit-bot@chromium.org    FT_Align,
4905a2ee052c9ef4c781b7b590b00b3d2da3b3449askia.committer@gmail.com    FT_Data,
502db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com    FT_Fill,
512db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com    FT_Inst,
522db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com    FT_Org,
532db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com    FT_Dwarf,
54054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.com    FT_DwarfFrame,
55054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.com    FT_LEB
56949eef0af2f5b47000e637347801cf2970092a38commit-bot@chromium.org  };
57949eef0af2f5b47000e637347801cf2970092a38commit-bot@chromium.org
58054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.comprivate:
592db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  FragmentType Kind;
602db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
612db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// Parent - The data for the section this fragment is in.
622db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  MCSectionData *Parent;
6391207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com
6491207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  /// Atom - The atom this fragment is in, as represented by it's defining
652db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// symbol. Atom's are only used by backends which set
6691207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  /// \see MCAsmBackend::hasReliableSymbolDifference().
6791207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  MCSymbolData *Atom;
68054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.com
692db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @name Assembler Backend Data
702db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @{
712db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  //
722db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  // FIXME: This could all be kept private to the assembler implementation.
73c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
74b79d865f03456b67273b568fa5046b8ba01e365absalomon@google.com  /// Offset - The offset of this fragment in its section. This is ~0 until
752db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// initialized.
766b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org  uint64_t Offset;
772db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
782db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// LayoutOrder - The layout order of this fragment.
792db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  unsigned LayoutOrder;
802db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
812db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @}
822db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
832db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comprotected:
842db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
852db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
862db3ded335fdb6697623bece61cabc307a414770bsalomon@google.compublic:
8791207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  // Only for sentinel.
886b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org  MCFragment();
896b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org  virtual ~MCFragment();
906b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org
9191207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  FragmentType getKind() const { return Kind; }
929b855c7c95ce9fff7a447e4a6bdf8a469c1f3097jvanverth@google.com
93b79d865f03456b67273b568fa5046b8ba01e365absalomon@google.com  MCSectionData *getParent() const { return Parent; }
942db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  void setParent(MCSectionData *Value) { Parent = Value; }
952db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
962db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  MCSymbolData *getAtom() const { return Atom; }
972db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  void setAtom(MCSymbolData *Value) { Atom = Value; }
982db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
992db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  unsigned getLayoutOrder() const { return LayoutOrder; }
1002db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
1012db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
1022db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  static bool classof(const MCFragment *O) { return true; }
103b79d865f03456b67273b568fa5046b8ba01e365absalomon@google.com
1045920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com  void dump();
1056b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org};
1066b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org
1076b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.orgclass MCDataFragment : public MCFragment {
1086b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org  virtual void anchor();
1096b30e457409f37c91c301cd82040e733e2930286commit-bot@chromium.org  SmallString<32> Contents;
1105920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com
1115920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com  /// Fixups - The list of fixups in this fragment.
1125920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com  std::vector<MCFixup> Fixups;
1135920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com
1145920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.compublic:
1155920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com  typedef std::vector<MCFixup>::const_iterator const_fixup_iterator;
11672993ab54276f8202d49b1e132e01c2a584d1dbebsalomon@google.com  typedef std::vector<MCFixup>::iterator fixup_iterator;
1175920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.com
1185920ac276877b36624e07baf97c7768e80a07f98bsalomon@google.compublic:
1192db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
1202db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
1212db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @name Accessors
1222db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @{
1232db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
12491207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  SmallString<32> &getContents() { return Contents; }
12591207482c9398944fc997aeb99ed5f8674be58cbbsalomon@google.com  const SmallString<32> &getContents() const { return Contents; }
126042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com
127042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com  /// @}
128d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com  /// @name Fixup Access
1292db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @{
130fec0bc3fc13481f5bcb341ab2d2d695911f39bd4bsalomon@google.com
131d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com  void addFixup(MCFixup Fixup) {
132d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    // Enforce invariant that fixups are in offset order.
133d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    assert((Fixups.empty() || Fixup.getOffset() >= Fixups.back().getOffset()) &&
1342db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com           "Fixups must be added in order!");
135d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    Fixups.push_back(Fixup);
136d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com  }
137d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com
138d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com  std::vector<MCFixup> &getFixups() { return Fixups; }
139d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com  const std::vector<MCFixup> &getFixups() const { return Fixups; }
140c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
141c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  fixup_iterator fixup_begin() { return Fixups.begin(); }
142e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.org  const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
143c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
144c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  fixup_iterator fixup_end() {return Fixups.end();}
145c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  const_fixup_iterator fixup_end() const {return Fixups.end();}
146c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
147c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  size_t fixup_size() const { return Fixups.size(); }
148c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
149c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  /// @}
150c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
151c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  static bool classof(const MCFragment *F) {
152c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com    return F->getKind() == MCFragment::FT_Data;
15331ec7985f2b52a0cab4aa714a613b918cf663c08bsalomon@google.com  }
154c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  static bool classof(const MCDataFragment *) { return true; }
155054ae99d93711c26e40682a0e3a03a47ea605c53jvanverth@google.com};
1568e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1574324c3ba707a567d45628b80073c2e44d2d0e4e4commit-bot@chromium.org// FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
158b79d865f03456b67273b568fa5046b8ba01e365absalomon@google.com// it is almost entirely a duplicate of MCDataFragment. If we decide to stick
1592db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com// with this approach (as opposed to making MCInstFragment a very light weight
1602db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com// object with just the MCInst and a code size, then we should just change
1612db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com// MCDataFragment to have an optional MCInst at its end.
1622db3ded335fdb6697623bece61cabc307a414770bsalomon@google.comclass MCInstFragment : public MCFragment {
1632db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  virtual void anchor();
1642db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
1652db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// Inst - The instruction this is a fragment for.
1668e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  MCInst Inst;
1678e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1688e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  /// Code - Binary data for the currently encoded instruction.
1692db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  SmallString<8> Code;
1702db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
1712db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// Fixups - The list of fixups in this fragment.
1722db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  SmallVector<MCFixup, 1> Fixups;
1732db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
17465ee5f424cb4dabd453268902c00086605d77c1dcommit-bot@chromium.orgpublic:
1752db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
1762db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
1772db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
1782db3ded335fdb6697623bece61cabc307a414770bsalomon@google.compublic:
1792db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
1802db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com    : MCFragment(FT_Inst, SD), Inst(_Inst) {
1812db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  }
1828e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1838e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  /// @name Accessors
1848e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  /// @{
1858e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1868e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  SmallVectorImpl<char> &getCode() { return Code; }
1878e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  const SmallVectorImpl<char> &getCode() const { return Code; }
1888e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1898e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  unsigned getInstSize() const { return Code.size(); }
1908e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1918e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  MCInst &getInst() { return Inst; }
1928e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  const MCInst &getInst() const { return Inst; }
1938e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1948e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org  void setInst(MCInst Value) { Inst = Value; }
1958e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
1962db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @}
1972db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @name Fixup Access
198c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  /// @{
1992d3b49201302f3f82e405a750724eae8ef82e5a0commit-bot@chromium.org
2002d3b49201302f3f82e405a750724eae8ef82e5a0commit-bot@chromium.org  SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
2012db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
2028e919add406c5d20918a7f0ca811317312e6ce67commit-bot@chromium.org
203c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  fixup_iterator fixup_begin() { return Fixups.begin(); }
204b79d865f03456b67273b568fa5046b8ba01e365absalomon@google.com  const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
2052db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
2062db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  fixup_iterator fixup_end() {return Fixups.end();}
2072db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  const_fixup_iterator fixup_end() const {return Fixups.end();}
2082db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
2092db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  size_t fixup_size() const { return Fixups.size(); }
2102db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
2112db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com  /// @}
2122db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
21305a2ee052c9ef4c781b7b590b00b3d2da3b3449askia.committer@gmail.com  static bool classof(const MCFragment *F) {
2149188a15f846ae79892c332aed2a72ee38116bdc6commit-bot@chromium.org    return F->getKind() == MCFragment::FT_Inst;
215c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  }
2162c84aa35988c661b3e5513c8ba9b3959832ff288bsalomon@google.com  static bool classof(const MCInstFragment *) { return true; }
2172c84aa35988c661b3e5513c8ba9b3959832ff288bsalomon@google.com};
2182db3ded335fdb6697623bece61cabc307a414770bsalomon@google.com
219504976ef6f1b969c2ac13ff1140ea1067f085ffabsalomon@google.comclass MCAlignFragment : public MCFragment {
220504976ef6f1b969c2ac13ff1140ea1067f085ffabsalomon@google.com  virtual void anchor();
221c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
222c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  /// Alignment - The alignment to ensure, in bytes.
223c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  unsigned Alignment;
224c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com
225c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  /// Value - Value to use for filling padding bytes.
226c3841b927ba32237a540949c408f78ad7682aa60bsalomon@google.com  int64_t Value;
227a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com
2284ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.org  /// ValueSize - The size of the integer (in bytes) of \arg Value.
22967b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com  unsigned ValueSize;
23067b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com
23167b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com  /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
232042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com  /// cannot be satisfied in this width then this fragment is ignored.
233eb6879f50a5564eeb981ec5616b55bf685eb76fcbsalomon@google.com  unsigned MaxBytesToEmit;
234042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com
235042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com  /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
236042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com  /// of using the provided value. The exact interpretation of this flag is
237042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com  /// target dependent.
238042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com  bool EmitNops : 1;
239042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.com
240042a2861b2aa7a7a9fff12503a4b297d6ee1d335bsalomon@google.compublic:
24167b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com  MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
24267b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com                  unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
243a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com    : MCFragment(FT_Align, SD), Alignment(_Alignment),
244a8e686eb6cadb74039d3b624ece0d3ccb0684dccbsalomon@google.com      Value(_Value),ValueSize(_ValueSize),
245d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com      MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false) {}
24667e78c9e47c38a51816412a24a10f4fe2db142a3bsalomon@google.com
247a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com  /// @name Accessors
248a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com  /// @{
249a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com
25040eb3c100095c8573ddfdc553794b3182d7b18d1commit-bot@chromium.org  unsigned getAlignment() const { return Alignment; }
2518f6884aab8aecd7657cf3f9cdbc682f0deca29c5tfarina@chromium.org
252a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com  int64_t getValue() const { return Value; }
25382aa7482cbf55ce6d42c692550cadee5e23146e4bsalomon@google.com
254a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com  unsigned getValueSize() const { return ValueSize; }
255a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com
256a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com  unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
257a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com
258a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com  bool hasEmitNops() const { return EmitNops; }
2599109e188c77abfd2832767530d33fdec35290a84commit-bot@chromium.org  void setEmitNops(bool Value) { EmitNops = Value; }
260cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org
261cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org  /// @}
262adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com
263adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com  static bool classof(const MCFragment *F) {
264adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    return F->getKind() == MCFragment::FT_Align;
265adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com  }
26667e78c9e47c38a51816412a24a10f4fe2db142a3bsalomon@google.com  static bool classof(const MCAlignFragment *) { return true; }
267727a352f7412753d2a3e4d30eab6500a1a4de135commit-bot@chromium.org};
268a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.com
269a1bf0fffff821d9c11809c89bd98d4ced480421absalomon@google.comclass MCFillFragment : public MCFragment {
270cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com  virtual void anchor();
271
272  /// Value - Value to use for filling bytes.
273  int64_t Value;
274
275  /// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
276  /// this is a virtual fill fragment.
277  unsigned ValueSize;
278
279  /// Size - The number of bytes to insert.
280  uint64_t Size;
281
282public:
283  MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
284                 MCSectionData *SD = 0)
285    : MCFragment(FT_Fill, SD),
286      Value(_Value), ValueSize(_ValueSize), Size(_Size) {
287    assert((!ValueSize || (Size % ValueSize) == 0) &&
288           "Fill size must be a multiple of the value size!");
289  }
290
291  /// @name Accessors
292  /// @{
293
294  int64_t getValue() const { return Value; }
295
296  unsigned getValueSize() const { return ValueSize; }
297
298  uint64_t getSize() const { return Size; }
299
300  /// @}
301
302  static bool classof(const MCFragment *F) {
303    return F->getKind() == MCFragment::FT_Fill;
304  }
305  static bool classof(const MCFillFragment *) { return true; }
306};
307
308class MCOrgFragment : public MCFragment {
309  virtual void anchor();
310
311  /// Offset - The offset this fragment should start at.
312  const MCExpr *Offset;
313
314  /// Value - Value to use for filling bytes.
315  int8_t Value;
316
317public:
318  MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
319    : MCFragment(FT_Org, SD),
320      Offset(&_Offset), Value(_Value) {}
321
322  /// @name Accessors
323  /// @{
324
325  const MCExpr &getOffset() const { return *Offset; }
326
327  uint8_t getValue() const { return Value; }
328
329  /// @}
330
331  static bool classof(const MCFragment *F) {
332    return F->getKind() == MCFragment::FT_Org;
333  }
334  static bool classof(const MCOrgFragment *) { return true; }
335};
336
337class MCLEBFragment : public MCFragment {
338  virtual void anchor();
339
340  /// Value - The value this fragment should contain.
341  const MCExpr *Value;
342
343  /// IsSigned - True if this is a sleb128, false if uleb128.
344  bool IsSigned;
345
346  SmallString<8> Contents;
347public:
348  MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSectionData *SD)
349    : MCFragment(FT_LEB, SD),
350      Value(&Value_), IsSigned(IsSigned_) { Contents.push_back(0); }
351
352  /// @name Accessors
353  /// @{
354
355  const MCExpr &getValue() const { return *Value; }
356
357  bool isSigned() const { return IsSigned; }
358
359  SmallString<8> &getContents() { return Contents; }
360  const SmallString<8> &getContents() const { return Contents; }
361
362  /// @}
363
364  static bool classof(const MCFragment *F) {
365    return F->getKind() == MCFragment::FT_LEB;
366  }
367  static bool classof(const MCLEBFragment *) { return true; }
368};
369
370class MCDwarfLineAddrFragment : public MCFragment {
371  virtual void anchor();
372
373  /// LineDelta - the value of the difference between the two line numbers
374  /// between two .loc dwarf directives.
375  int64_t LineDelta;
376
377  /// AddrDelta - The expression for the difference of the two symbols that
378  /// make up the address delta between two .loc dwarf directives.
379  const MCExpr *AddrDelta;
380
381  SmallString<8> Contents;
382
383public:
384  MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
385                      MCSectionData *SD)
386    : MCFragment(FT_Dwarf, SD),
387      LineDelta(_LineDelta), AddrDelta(&_AddrDelta) { Contents.push_back(0); }
388
389  /// @name Accessors
390  /// @{
391
392  int64_t getLineDelta() const { return LineDelta; }
393
394  const MCExpr &getAddrDelta() const { return *AddrDelta; }
395
396  SmallString<8> &getContents() { return Contents; }
397  const SmallString<8> &getContents() const { return Contents; }
398
399  /// @}
400
401  static bool classof(const MCFragment *F) {
402    return F->getKind() == MCFragment::FT_Dwarf;
403  }
404  static bool classof(const MCDwarfLineAddrFragment *) { return true; }
405};
406
407class MCDwarfCallFrameFragment : public MCFragment {
408  virtual void anchor();
409
410  /// AddrDelta - The expression for the difference of the two symbols that
411  /// make up the address delta between two .cfi_* dwarf directives.
412  const MCExpr *AddrDelta;
413
414  SmallString<8> Contents;
415
416public:
417  MCDwarfCallFrameFragment(const MCExpr &_AddrDelta,  MCSectionData *SD)
418    : MCFragment(FT_DwarfFrame, SD),
419      AddrDelta(&_AddrDelta) { Contents.push_back(0); }
420
421  /// @name Accessors
422  /// @{
423
424  const MCExpr &getAddrDelta() const { return *AddrDelta; }
425
426  SmallString<8> &getContents() { return Contents; }
427  const SmallString<8> &getContents() const { return Contents; }
428
429  /// @}
430
431  static bool classof(const MCFragment *F) {
432    return F->getKind() == MCFragment::FT_DwarfFrame;
433  }
434  static bool classof(const MCDwarfCallFrameFragment *) { return true; }
435};
436
437// FIXME: Should this be a separate class, or just merged into MCSection? Since
438// we anticipate the fast path being through an MCAssembler, the only reason to
439// keep it out is for API abstraction.
440class MCSectionData : public ilist_node<MCSectionData> {
441  friend class MCAsmLayout;
442
443  MCSectionData(const MCSectionData&);  // DO NOT IMPLEMENT
444  void operator=(const MCSectionData&); // DO NOT IMPLEMENT
445
446public:
447  typedef iplist<MCFragment> FragmentListType;
448
449  typedef FragmentListType::const_iterator const_iterator;
450  typedef FragmentListType::iterator iterator;
451
452  typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
453  typedef FragmentListType::reverse_iterator reverse_iterator;
454
455private:
456  FragmentListType Fragments;
457  const MCSection *Section;
458
459  /// Ordinal - The section index in the assemblers section list.
460  unsigned Ordinal;
461
462  /// LayoutOrder - The index of this section in the layout order.
463  unsigned LayoutOrder;
464
465  /// Alignment - The maximum alignment seen in this section.
466  unsigned Alignment;
467
468  /// @name Assembler Backend Data
469  /// @{
470  //
471  // FIXME: This could all be kept private to the assembler implementation.
472
473  /// HasInstructions - Whether this section has had instructions emitted into
474  /// it.
475  unsigned HasInstructions : 1;
476
477  /// @}
478
479public:
480  // Only for use as sentinel.
481  MCSectionData();
482  MCSectionData(const MCSection &Section, MCAssembler *A = 0);
483
484  const MCSection &getSection() const { return *Section; }
485
486  unsigned getAlignment() const { return Alignment; }
487  void setAlignment(unsigned Value) { Alignment = Value; }
488
489  bool hasInstructions() const { return HasInstructions; }
490  void setHasInstructions(bool Value) { HasInstructions = Value; }
491
492  unsigned getOrdinal() const { return Ordinal; }
493  void setOrdinal(unsigned Value) { Ordinal = Value; }
494
495  unsigned getLayoutOrder() const { return LayoutOrder; }
496  void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
497
498  /// @name Fragment Access
499  /// @{
500
501  const FragmentListType &getFragmentList() const { return Fragments; }
502  FragmentListType &getFragmentList() { return Fragments; }
503
504  iterator begin() { return Fragments.begin(); }
505  const_iterator begin() const { return Fragments.begin(); }
506
507  iterator end() { return Fragments.end(); }
508  const_iterator end() const { return Fragments.end(); }
509
510  reverse_iterator rbegin() { return Fragments.rbegin(); }
511  const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
512
513  reverse_iterator rend() { return Fragments.rend(); }
514  const_reverse_iterator rend() const { return Fragments.rend(); }
515
516  size_t size() const { return Fragments.size(); }
517
518  bool empty() const { return Fragments.empty(); }
519
520  void dump();
521
522  /// @}
523};
524
525// FIXME: Same concerns as with SectionData.
526class MCSymbolData : public ilist_node<MCSymbolData> {
527public:
528  const MCSymbol *Symbol;
529
530  /// Fragment - The fragment this symbol's value is relative to, if any.
531  MCFragment *Fragment;
532
533  /// Offset - The offset to apply to the fragment address to form this symbol's
534  /// value.
535  uint64_t Offset;
536
537  /// IsExternal - True if this symbol is visible outside this translation
538  /// unit.
539  unsigned IsExternal : 1;
540
541  /// IsPrivateExtern - True if this symbol is private extern.
542  unsigned IsPrivateExtern : 1;
543
544  /// CommonSize - The size of the symbol, if it is 'common', or 0.
545  //
546  // FIXME: Pack this in with other fields? We could put it in offset, since a
547  // common symbol can never get a definition.
548  uint64_t CommonSize;
549
550  /// SymbolSize - An expression describing how to calculate the size of
551  /// a symbol. If a symbol has no size this field will be NULL.
552  const MCExpr *SymbolSize;
553
554  /// CommonAlign - The alignment of the symbol, if it is 'common'.
555  //
556  // FIXME: Pack this in with other fields?
557  unsigned CommonAlign;
558
559  /// Flags - The Flags field is used by object file implementations to store
560  /// additional per symbol information which is not easily classified.
561  uint32_t Flags;
562
563  /// Index - Index field, for use by the object file implementation.
564  uint64_t Index;
565
566public:
567  // Only for use as sentinel.
568  MCSymbolData();
569  MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
570               MCAssembler *A = 0);
571
572  /// @name Accessors
573  /// @{
574
575  const MCSymbol &getSymbol() const { return *Symbol; }
576
577  MCFragment *getFragment() const { return Fragment; }
578  void setFragment(MCFragment *Value) { Fragment = Value; }
579
580  uint64_t getOffset() const { return Offset; }
581  void setOffset(uint64_t Value) { Offset = Value; }
582
583  /// @}
584  /// @name Symbol Attributes
585  /// @{
586
587  bool isExternal() const { return IsExternal; }
588  void setExternal(bool Value) { IsExternal = Value; }
589
590  bool isPrivateExtern() const { return IsPrivateExtern; }
591  void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
592
593  /// isCommon - Is this a 'common' symbol.
594  bool isCommon() const { return CommonSize != 0; }
595
596  /// setCommon - Mark this symbol as being 'common'.
597  ///
598  /// \param Size - The size of the symbol.
599  /// \param Align - The alignment of the symbol.
600  void setCommon(uint64_t Size, unsigned Align) {
601    CommonSize = Size;
602    CommonAlign = Align;
603  }
604
605  /// getCommonSize - Return the size of a 'common' symbol.
606  uint64_t getCommonSize() const {
607    assert(isCommon() && "Not a 'common' symbol!");
608    return CommonSize;
609  }
610
611  void setSize(const MCExpr *SS) {
612    SymbolSize = SS;
613  }
614
615  const MCExpr *getSize() const {
616    return SymbolSize;
617  }
618
619
620  /// getCommonAlignment - Return the alignment of a 'common' symbol.
621  unsigned getCommonAlignment() const {
622    assert(isCommon() && "Not a 'common' symbol!");
623    return CommonAlign;
624  }
625
626  /// getFlags - Get the (implementation defined) symbol flags.
627  uint32_t getFlags() const { return Flags; }
628
629  /// setFlags - Set the (implementation defined) symbol flags.
630  void setFlags(uint32_t Value) { Flags = Value; }
631
632  /// modifyFlags - Modify the flags via a mask
633  void modifyFlags(uint32_t Value, uint32_t Mask) {
634    Flags = (Flags & ~Mask) | Value;
635  }
636
637  /// getIndex - Get the (implementation defined) index.
638  uint64_t getIndex() const { return Index; }
639
640  /// setIndex - Set the (implementation defined) index.
641  void setIndex(uint64_t Value) { Index = Value; }
642
643  /// @}
644
645  void dump();
646};
647
648// FIXME: This really doesn't belong here. See comments below.
649struct IndirectSymbolData {
650  MCSymbol *Symbol;
651  MCSectionData *SectionData;
652};
653
654// FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
655// to one another.
656struct DataRegionData {
657  // This enum should be kept in sync w/ the mach-o definition in
658  // llvm/Object/MachOFormat.h.
659  enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
660  MCSymbol *Start;
661  MCSymbol *End;
662};
663
664class MCAssembler {
665  friend class MCAsmLayout;
666
667public:
668  typedef iplist<MCSectionData> SectionDataListType;
669  typedef iplist<MCSymbolData> SymbolDataListType;
670
671  typedef SectionDataListType::const_iterator const_iterator;
672  typedef SectionDataListType::iterator iterator;
673
674  typedef SymbolDataListType::const_iterator const_symbol_iterator;
675  typedef SymbolDataListType::iterator symbol_iterator;
676
677  typedef std::vector<IndirectSymbolData>::const_iterator
678    const_indirect_symbol_iterator;
679  typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
680
681  typedef std::vector<DataRegionData>::const_iterator
682    const_data_region_iterator;
683  typedef std::vector<DataRegionData>::iterator data_region_iterator;
684
685private:
686  MCAssembler(const MCAssembler&);    // DO NOT IMPLEMENT
687  void operator=(const MCAssembler&); // DO NOT IMPLEMENT
688
689  MCContext &Context;
690
691  MCAsmBackend &Backend;
692
693  MCCodeEmitter &Emitter;
694
695  MCObjectWriter &Writer;
696
697  raw_ostream &OS;
698
699  iplist<MCSectionData> Sections;
700
701  iplist<MCSymbolData> Symbols;
702
703  /// The map of sections to their associated assembler backend data.
704  //
705  // FIXME: Avoid this indirection?
706  DenseMap<const MCSection*, MCSectionData*> SectionMap;
707
708  /// The map of symbols to their associated assembler backend data.
709  //
710  // FIXME: Avoid this indirection?
711  DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
712
713  std::vector<IndirectSymbolData> IndirectSymbols;
714
715  std::vector<DataRegionData> DataRegions;
716  /// The set of function symbols for which a .thumb_func directive has
717  /// been seen.
718  //
719  // FIXME: We really would like this in target specific code rather than
720  // here. Maybe when the relocation stuff moves to target specific,
721  // this can go with it? The streamer would need some target specific
722  // refactoring too.
723  SmallPtrSet<const MCSymbol*, 64> ThumbFuncs;
724
725  unsigned RelaxAll : 1;
726  unsigned NoExecStack : 1;
727  unsigned SubsectionsViaSymbols : 1;
728
729private:
730  /// Evaluate a fixup to a relocatable expression and the value which should be
731  /// placed into the fixup.
732  ///
733  /// \param Layout The layout to use for evaluation.
734  /// \param Fixup The fixup to evaluate.
735  /// \param DF The fragment the fixup is inside.
736  /// \param Target [out] On return, the relocatable expression the fixup
737  /// evaluates to.
738  /// \param Value [out] On return, the value of the fixup as currently laid
739  /// out.
740  /// \return Whether the fixup value was fully resolved. This is true if the
741  /// \arg Value result is fixed, otherwise the value may change due to
742  /// relocation.
743  bool evaluateFixup(const MCAsmLayout &Layout,
744                     const MCFixup &Fixup, const MCFragment *DF,
745                     MCValue &Target, uint64_t &Value) const;
746
747  /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
748  /// (increased in size, in order to hold its value correctly).
749  bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCInstFragment *DF,
750                            const MCAsmLayout &Layout) const;
751
752  /// Check whether the given fragment needs relaxation.
753  bool fragmentNeedsRelaxation(const MCInstFragment *IF,
754                               const MCAsmLayout &Layout) const;
755
756  /// layoutOnce - Perform one layout iteration and return true if any offsets
757  /// were adjusted.
758  bool layoutOnce(MCAsmLayout &Layout);
759
760  bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
761
762  bool relaxInstruction(MCAsmLayout &Layout, MCInstFragment &IF);
763
764  bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
765
766  bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
767  bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
768                                   MCDwarfCallFrameFragment &DF);
769
770  /// finishLayout - Finalize a layout, including fragment lowering.
771  void finishLayout(MCAsmLayout &Layout);
772
773  uint64_t handleFixup(const MCAsmLayout &Layout,
774                       MCFragment &F, const MCFixup &Fixup);
775
776public:
777  /// Compute the effective fragment size assuming it is laid out at the given
778  /// \arg SectionAddress and \arg FragmentOffset.
779  uint64_t computeFragmentSize(const MCAsmLayout &Layout,
780                               const MCFragment &F) const;
781
782  /// Find the symbol which defines the atom containing the given symbol, or
783  /// null if there is no such symbol.
784  const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
785
786  /// Check whether a particular symbol is visible to the linker and is required
787  /// in the symbol table, or whether it can be discarded by the assembler. This
788  /// also effects whether the assembler treats the label as potentially
789  /// defining a separate atom.
790  bool isSymbolLinkerVisible(const MCSymbol &SD) const;
791
792  /// Emit the section contents using the given object writer.
793  void writeSectionData(const MCSectionData *Section,
794                        const MCAsmLayout &Layout) const;
795
796  /// Check whether a given symbol has been flagged with .thumb_func.
797  bool isThumbFunc(const MCSymbol *Func) const {
798    return ThumbFuncs.count(Func);
799  }
800
801  /// Flag a function symbol as the target of a .thumb_func directive.
802  void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
803
804public:
805  /// Construct a new assembler instance.
806  ///
807  /// \arg OS - The stream to output to.
808  //
809  // FIXME: How are we going to parameterize this? Two obvious options are stay
810  // concrete and require clients to pass in a target like object. The other
811  // option is to make this abstract, and have targets provide concrete
812  // implementations as we do with AsmParser.
813  MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
814              MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
815              raw_ostream &OS);
816  ~MCAssembler();
817
818  MCContext &getContext() const { return Context; }
819
820  MCAsmBackend &getBackend() const { return Backend; }
821
822  MCCodeEmitter &getEmitter() const { return Emitter; }
823
824  MCObjectWriter &getWriter() const { return Writer; }
825
826  /// Finish - Do final processing and write the object to the output stream.
827  /// \arg Writer is used for custom object writer (as the MCJIT does),
828  /// if not specified it is automatically created from backend.
829  void Finish();
830
831  // FIXME: This does not belong here.
832  bool getSubsectionsViaSymbols() const {
833    return SubsectionsViaSymbols;
834  }
835  void setSubsectionsViaSymbols(bool Value) {
836    SubsectionsViaSymbols = Value;
837  }
838
839  bool getRelaxAll() const { return RelaxAll; }
840  void setRelaxAll(bool Value) { RelaxAll = Value; }
841
842  bool getNoExecStack() const { return NoExecStack; }
843  void setNoExecStack(bool Value) { NoExecStack = Value; }
844
845  /// @name Section List Access
846  /// @{
847
848  const SectionDataListType &getSectionList() const { return Sections; }
849  SectionDataListType &getSectionList() { return Sections; }
850
851  iterator begin() { return Sections.begin(); }
852  const_iterator begin() const { return Sections.begin(); }
853
854  iterator end() { return Sections.end(); }
855  const_iterator end() const { return Sections.end(); }
856
857  size_t size() const { return Sections.size(); }
858
859  /// @}
860  /// @name Symbol List Access
861  /// @{
862
863  const SymbolDataListType &getSymbolList() const { return Symbols; }
864  SymbolDataListType &getSymbolList() { return Symbols; }
865
866  symbol_iterator symbol_begin() { return Symbols.begin(); }
867  const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
868
869  symbol_iterator symbol_end() { return Symbols.end(); }
870  const_symbol_iterator symbol_end() const { return Symbols.end(); }
871
872  size_t symbol_size() const { return Symbols.size(); }
873
874  /// @}
875  /// @name Indirect Symbol List Access
876  /// @{
877
878  // FIXME: This is a total hack, this should not be here. Once things are
879  // factored so that the streamer has direct access to the .o writer, it can
880  // disappear.
881  std::vector<IndirectSymbolData> &getIndirectSymbols() {
882    return IndirectSymbols;
883  }
884
885  indirect_symbol_iterator indirect_symbol_begin() {
886    return IndirectSymbols.begin();
887  }
888  const_indirect_symbol_iterator indirect_symbol_begin() const {
889    return IndirectSymbols.begin();
890  }
891
892  indirect_symbol_iterator indirect_symbol_end() {
893    return IndirectSymbols.end();
894  }
895  const_indirect_symbol_iterator indirect_symbol_end() const {
896    return IndirectSymbols.end();
897  }
898
899  size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
900
901  /// @}
902  /// @name Data Region List Access
903  /// @{
904
905  // FIXME: This is a total hack, this should not be here. Once things are
906  // factored so that the streamer has direct access to the .o writer, it can
907  // disappear.
908  std::vector<DataRegionData> &getDataRegions() {
909    return DataRegions;
910  }
911
912  data_region_iterator data_region_begin() {
913    return DataRegions.begin();
914  }
915  const_data_region_iterator data_region_begin() const {
916    return DataRegions.begin();
917  }
918
919  data_region_iterator data_region_end() {
920    return DataRegions.end();
921  }
922  const_data_region_iterator data_region_end() const {
923    return DataRegions.end();
924  }
925
926  size_t data_region_size() const { return DataRegions.size(); }
927
928  /// @}
929  /// @name Backend Data Access
930  /// @{
931
932  MCSectionData &getSectionData(const MCSection &Section) const {
933    MCSectionData *Entry = SectionMap.lookup(&Section);
934    assert(Entry && "Missing section data!");
935    return *Entry;
936  }
937
938  MCSectionData &getOrCreateSectionData(const MCSection &Section,
939                                        bool *Created = 0) {
940    MCSectionData *&Entry = SectionMap[&Section];
941
942    if (Created) *Created = !Entry;
943    if (!Entry)
944      Entry = new MCSectionData(Section, this);
945
946    return *Entry;
947  }
948
949  MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
950    MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
951    assert(Entry && "Missing symbol data!");
952    return *Entry;
953  }
954
955  MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
956                                      bool *Created = 0) {
957    MCSymbolData *&Entry = SymbolMap[&Symbol];
958
959    if (Created) *Created = !Entry;
960    if (!Entry)
961      Entry = new MCSymbolData(Symbol, 0, 0, this);
962
963    return *Entry;
964  }
965
966  /// @}
967
968  void dump();
969};
970
971} // end namespace llvm
972
973#endif
974