Demote a single-use named function object to a lambda
[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/EndianStream.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cassert>
19
20 namespace llvm {
21 class MCAsmLayout;
22 class MCAssembler;
23 class MCFixup;
24 class MCFragment;
25 class MCSymbolRefExpr;
26 class MCValue;
27
28 /// Defines the object file and target independent interfaces used by the
29 /// 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   raw_pwrite_stream *OS;
44
45 protected:
46   unsigned IsLittleEndian : 1;
47
48 protected: // Can only create subclasses.
49   MCObjectWriter(raw_pwrite_stream &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_pwrite_stream &getStream() { return *OS; }
61   void setStream(raw_pwrite_stream &NewOS) { OS = &NewOS; }
62
63   /// \name High-Level API
64   /// @{
65
66   /// Perform any late binding of symbols (for example, to assign symbol
67   /// indices for use when generating relocations).
68   ///
69   /// This routine is called by the assembler after layout and relaxation is
70   /// complete.
71   virtual void executePostLayoutBinding(MCAssembler &Asm,
72                                         const MCAsmLayout &Layout) = 0;
73
74   /// Record a relocation entry.
75   ///
76   /// This routine is called by the assembler after layout and relaxation, and
77   /// post layout binding. The implementation is responsible for storing
78   /// information about the relocation so that it can be emitted during
79   /// writeObject().
80   virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
81                                 const MCFragment *Fragment,
82                                 const MCFixup &Fixup, MCValue Target,
83                                 bool &IsPCRel, uint64_t &FixedValue) = 0;
84
85   /// Check whether the difference (A - B) between two symbol references is
86   /// fully resolved.
87   ///
88   /// Clients are not required to answer precisely and may conservatively return
89   /// false, even when a difference is fully resolved.
90   bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
91                                           const MCSymbolRefExpr *A,
92                                           const MCSymbolRefExpr *B,
93                                           bool InSet) const;
94
95   virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
96                                                       const MCSymbol &A,
97                                                       const MCSymbol &B,
98                                                       bool InSet) const;
99
100   virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
101                                                       const MCSymbol &SymA,
102                                                       const MCFragment &FB,
103                                                       bool InSet,
104                                                       bool IsPCRel) const;
105
106   /// True if this symbol (which is a variable) is weak. This is not
107   /// just STB_WEAK, but more generally whether or not we can evaluate
108   /// past it.
109   virtual bool isWeak(const MCSymbol &Sym) const;
110
111   /// Write the object file.
112   ///
113   /// This routine is called by the assembler after layout and relaxation is
114   /// complete, fixups have been evaluated and applied, and relocations
115   /// generated.
116   virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
117
118   /// @}
119   /// \name Binary Output
120   /// @{
121
122   void write8(uint8_t Value) { *OS << char(Value); }
123
124   void writeLE16(uint16_t Value) {
125     support::endian::Writer<support::little>(*OS).write(Value);
126   }
127
128   void writeLE32(uint32_t Value) {
129     support::endian::Writer<support::little>(*OS).write(Value);
130   }
131
132   void writeLE64(uint64_t Value) {
133     support::endian::Writer<support::little>(*OS).write(Value);
134   }
135
136   void writeBE16(uint16_t Value) {
137     support::endian::Writer<support::big>(*OS).write(Value);
138   }
139
140   void writeBE32(uint32_t Value) {
141     support::endian::Writer<support::big>(*OS).write(Value);
142   }
143
144   void writeBE64(uint64_t Value) {
145     support::endian::Writer<support::big>(*OS).write(Value);
146   }
147
148   void write16(uint16_t Value) {
149     if (IsLittleEndian)
150       writeLE16(Value);
151     else
152       writeBE16(Value);
153   }
154
155   void write32(uint32_t Value) {
156     if (IsLittleEndian)
157       writeLE32(Value);
158     else
159       writeBE32(Value);
160   }
161
162   void write64(uint64_t Value) {
163     if (IsLittleEndian)
164       writeLE64(Value);
165     else
166       writeBE64(Value);
167   }
168
169   void WriteZeros(unsigned N) {
170     const char Zeros[16] = {0};
171
172     for (unsigned i = 0, e = N / 16; i != e; ++i)
173       *OS << StringRef(Zeros, 16);
174
175     *OS << StringRef(Zeros, N % 16);
176   }
177
178   void writeBytes(const SmallVectorImpl<char> &ByteVec,
179                   unsigned ZeroFillSize = 0) {
180     writeBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
181   }
182
183   void writeBytes(StringRef Str, unsigned ZeroFillSize = 0) {
184     // TODO: this version may need to go away once all fragment contents are
185     // converted to SmallVector<char, N>
186     assert(
187         (ZeroFillSize == 0 || Str.size() <= ZeroFillSize) &&
188         "data size greater than fill size, unexpected large write will occur");
189     *OS << Str;
190     if (ZeroFillSize)
191       WriteZeros(ZeroFillSize - Str.size());
192   }
193
194   /// @}
195 };
196
197 } // End llvm namespace
198
199 #endif