[MCInstPrinter] Enable MCInstPrinter to change its behavior based on the
[oota-llvm.git] / include / llvm / MC / MCObjectWriter.h
1 //===-- llvm/MC/MCObjectWriter.h - Object File Writer Interface -*- 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_MCOBJECTWRITER_H
11 #define LLVM_MC_MCOBJECTWRITER_H
12
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cassert>
18
19 namespace llvm {
20 class MCAsmLayout;
21 class MCAssembler;
22 class MCFixup;
23 class MCFragment;
24 class MCSymbolData;
25 class MCSymbolRefExpr;
26 class MCValue;
27
28 /// MCObjectWriter - Defines the object file and target independent interfaces
29 /// used by the assembler backend to write native file format object files.
30 ///
31 /// The object writer contains a few callbacks used by the assembler to allow
32 /// the object writer to modify the assembler data structures at appropriate
33 /// points. Once assembly is complete, the object writer is given the
34 /// MCAssembler instance, which contains all the symbol and section data which
35 /// should be emitted as part of WriteObject().
36 ///
37 /// The object writer also contains a number of helper methods for writing
38 /// binary data to the output stream.
39 class MCObjectWriter {
40   MCObjectWriter(const MCObjectWriter &) = delete;
41   void operator=(const MCObjectWriter &) = delete;
42
43 protected:
44   raw_ostream &OS;
45
46   unsigned IsLittleEndian : 1;
47
48 protected: // Can only create subclasses.
49   MCObjectWriter(raw_ostream &OS, bool IsLittleEndian)
50       : OS(OS), IsLittleEndian(IsLittleEndian) {}
51
52 public:
53   virtual ~MCObjectWriter();
54
55   /// lifetime management
56   virtual void reset() { }
57
58   bool isLittleEndian() const { return IsLittleEndian; }
59
60   raw_ostream &getStream() { return OS; }
61
62   /// @name High-Level API
63   /// @{
64
65   /// \brief Perform any late binding of symbols (for example, to assign symbol
66   /// indices for use when generating relocations).
67   ///
68   /// This routine is called by the assembler after layout and relaxation is
69   /// complete.
70   virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
71                                         const MCAsmLayout &Layout) = 0;
72
73   /// \brief Record a relocation entry.
74   ///
75   /// This routine is called by the assembler after layout and relaxation, and
76   /// post layout binding. The implementation is responsible for storing
77   /// information about the relocation so that it can be emitted during
78   /// WriteObject().
79   virtual void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
80                                 const MCFragment *Fragment,
81                                 const MCFixup &Fixup, MCValue Target,
82                                 bool &IsPCRel, uint64_t &FixedValue) = 0;
83
84   /// \brief Check whether the difference (A - B) between two symbol
85   /// references is fully resolved.
86   ///
87   /// Clients are not required to answer precisely and may conservatively return
88   /// false, even when a difference is fully resolved.
89   bool IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
90                                           const MCSymbolRefExpr *A,
91                                           const MCSymbolRefExpr *B,
92                                           bool InSet) const;
93
94   virtual bool
95   IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
96                                          const MCSymbolData &DataA,
97                                          const MCFragment &FB,
98                                          bool InSet,
99                                          bool IsPCRel) const;
100
101   /// \brief True if this symbol (which is a variable) is weak. This is not
102   /// just STB_WEAK, but more generally whether or not we can evaluate
103   /// past it.
104   virtual bool isWeak(const MCSymbolData &SD) const;
105
106   /// \brief Write the object file.
107   ///
108   /// This routine is called by the assembler after layout and relaxation is
109   /// complete, fixups have been evaluated and applied, and relocations
110   /// generated.
111   virtual void WriteObject(MCAssembler &Asm,
112                            const MCAsmLayout &Layout) = 0;
113
114   /// @}
115   /// @name Binary Output
116   /// @{
117
118   void Write8(uint8_t Value) {
119     OS << char(Value);
120   }
121
122   void WriteLE16(uint16_t Value) {
123     Write8(uint8_t(Value >> 0));
124     Write8(uint8_t(Value >> 8));
125   }
126
127   void WriteLE32(uint32_t Value) {
128     WriteLE16(uint16_t(Value >> 0));
129     WriteLE16(uint16_t(Value >> 16));
130   }
131
132   void WriteLE64(uint64_t Value) {
133     WriteLE32(uint32_t(Value >> 0));
134     WriteLE32(uint32_t(Value >> 32));
135   }
136
137   void WriteBE16(uint16_t Value) {
138     Write8(uint8_t(Value >> 8));
139     Write8(uint8_t(Value >> 0));
140   }
141
142   void WriteBE32(uint32_t Value) {
143     WriteBE16(uint16_t(Value >> 16));
144     WriteBE16(uint16_t(Value >> 0));
145   }
146
147   void WriteBE64(uint64_t Value) {
148     WriteBE32(uint32_t(Value >> 32));
149     WriteBE32(uint32_t(Value >> 0));
150   }
151
152   void Write16(uint16_t Value) {
153     if (IsLittleEndian)
154       WriteLE16(Value);
155     else
156       WriteBE16(Value);
157   }
158
159   void Write32(uint32_t Value) {
160     if (IsLittleEndian)
161       WriteLE32(Value);
162     else
163       WriteBE32(Value);
164   }
165
166   void Write64(uint64_t Value) {
167     if (IsLittleEndian)
168       WriteLE64(Value);
169     else
170       WriteBE64(Value);
171   }
172
173   void WriteZeros(unsigned N) {
174     const char Zeros[16] = { 0 };
175
176     for (unsigned i = 0, e = N / 16; i != e; ++i)
177       OS << StringRef(Zeros, 16);
178
179     OS << StringRef(Zeros, N % 16);
180   }
181
182   void WriteBytes(const SmallVectorImpl<char> &ByteVec, unsigned ZeroFillSize = 0) {
183     WriteBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
184   }
185
186   void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
187     // TODO: this version may need to go away once all fragment contents are
188     // converted to SmallVector<char, N>
189     assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
190       "data size greater than fill size, unexpected large write will occur");
191     OS << Str;
192     if (ZeroFillSize)
193       WriteZeros(ZeroFillSize - Str.size());
194   }
195
196   /// @}
197
198 };
199
200 } // End llvm namespace
201
202 #endif