Annotate BumpPtrAllocator for MemorySanitizer.
[oota-llvm.git] / lib / MC / MCPureStreamer.cpp
1 //===- lib/MC/MCPureStreamer.cpp - MC "Pure" Object Output ----------------===//
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 #include "llvm/MC/MCStreamer.h"
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectStreamer.h"
16 // FIXME: Remove this.
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class MCPureStreamer : public MCObjectStreamer {
26 private:
27   virtual void EmitInstToFragment(const MCInst &Inst);
28   virtual void EmitInstToData(const MCInst &Inst);
29
30 public:
31   MCPureStreamer(MCContext &Context, MCAsmBackend &TAB,
32                  raw_ostream &OS, MCCodeEmitter *Emitter)
33     : MCObjectStreamer(Context, TAB, OS, Emitter) {}
34
35   /// @name MCStreamer Interface
36   /// @{
37
38   virtual void InitSections();
39   virtual void InitToTextSection();
40   virtual void EmitLabel(MCSymbol *Symbol);
41   virtual void EmitDebugLabel(MCSymbol *Symbol);
42   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
43                             uint64_t Size = 0, unsigned ByteAlignment = 0);
44   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
45   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
46                                     unsigned ValueSize = 1,
47                                     unsigned MaxBytesToEmit = 0);
48   virtual void EmitCodeAlignment(unsigned ByteAlignment,
49                                  unsigned MaxBytesToEmit = 0);
50   virtual bool EmitValueToOffset(const MCExpr *Offset,
51                                  unsigned char Value = 0);
52   virtual void FinishImpl();
53
54
55   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
56     report_fatal_error("unsupported directive in pure streamer");
57   }
58   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
59     report_fatal_error("unsupported directive in pure streamer");
60   }
61   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
62                               uint64_t Size, unsigned ByteAlignment = 0) {
63     report_fatal_error("unsupported directive in pure streamer");
64   }
65   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
66     report_fatal_error("unsupported directive in pure streamer");
67   }
68   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69                                 unsigned ByteAlignment) {
70     report_fatal_error("unsupported directive in pure streamer");
71   }
72   virtual void EmitThumbFunc(MCSymbol *Func) {
73     report_fatal_error("unsupported directive in pure streamer");
74   }
75   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
76     report_fatal_error("unsupported directive in pure streamer");
77   }
78   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
79     report_fatal_error("unsupported directive in pure streamer");
80   }
81   virtual void EmitCOFFSymbolType(int Type) {
82     report_fatal_error("unsupported directive in pure streamer");
83   }
84   virtual void EndCOFFSymbolDef() {
85     report_fatal_error("unsupported directive in pure streamer");
86   }
87   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
88     report_fatal_error("unsupported directive in pure streamer");
89   }
90   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
91                                      unsigned ByteAlignment) {
92     report_fatal_error("unsupported directive in pure streamer");
93   }
94   virtual void EmitFileDirective(StringRef Filename) {
95     report_fatal_error("unsupported directive in pure streamer");
96   }
97   virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
98                                       StringRef Filename) {
99     report_fatal_error("unsupported directive in pure streamer");
100   }
101
102   /// @}
103 };
104
105 } // end anonymous namespace.
106
107 void MCPureStreamer::InitSections() {
108   InitToTextSection();
109 }
110
111 void MCPureStreamer::InitToTextSection() {
112   // FIMXE: To what!?
113   SwitchSection(getContext().getMachOSection("__TEXT", "__text",
114                                     MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
115                                     0, SectionKind::getText()));
116 }
117
118 void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
119   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
120   assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
121   assert(getCurrentSection() && "Cannot emit before setting section!");
122
123   Symbol->setSection(*getCurrentSection());
124
125   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
126
127   // We have to create a new fragment if this is an atom defining symbol,
128   // fragments cannot span atoms.
129   if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
130     new MCDataFragment(getCurrentSectionData());
131
132   // FIXME: This is wasteful, we don't necessarily need to create a data
133   // fragment. Instead, we should mark the symbol as pointing into the data
134   // fragment if it exists, otherwise we should just queue the label and set its
135   // fragment pointer when we emit the next fragment.
136   MCDataFragment *F = getOrCreateDataFragment();
137   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
138   SD.setFragment(F);
139   SD.setOffset(F->getContents().size());
140 }
141
142
143 void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
144   EmitLabel(Symbol);
145 }
146
147 void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
148                                   uint64_t Size, unsigned ByteAlignment) {
149   report_fatal_error("not yet implemented in pure streamer");
150 }
151
152 void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
153   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
154   // MCObjectStreamer.
155   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
156 }
157
158 void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
159                                           int64_t Value, unsigned ValueSize,
160                                           unsigned MaxBytesToEmit) {
161   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
162   // MCObjectStreamer.
163   if (MaxBytesToEmit == 0)
164     MaxBytesToEmit = ByteAlignment;
165   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
166                       getCurrentSectionData());
167
168   // Update the maximum alignment on the current section if necessary.
169   if (ByteAlignment > getCurrentSectionData()->getAlignment())
170     getCurrentSectionData()->setAlignment(ByteAlignment);
171 }
172
173 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
174                                        unsigned MaxBytesToEmit) {
175   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
176   // MCObjectStreamer.
177   if (MaxBytesToEmit == 0)
178     MaxBytesToEmit = ByteAlignment;
179   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
180                                            getCurrentSectionData());
181   F->setEmitNops(true);
182
183   // Update the maximum alignment on the current section if necessary.
184   if (ByteAlignment > getCurrentSectionData()->getAlignment())
185     getCurrentSectionData()->setAlignment(ByteAlignment);
186 }
187
188 bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
189                                        unsigned char Value) {
190   new MCOrgFragment(*Offset, Value, getCurrentSectionData());
191   return false;
192 }
193
194 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
195   MCRelaxableFragment *IF =
196     new MCRelaxableFragment(Inst, getCurrentSectionData());
197
198   // Add the fixups and data.
199   //
200   // FIXME: Revisit this design decision when relaxation is done, we may be
201   // able to get away with not storing any extra data in the MCInst.
202   SmallVector<MCFixup, 4> Fixups;
203   SmallString<256> Code;
204   raw_svector_ostream VecOS(Code);
205   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
206   VecOS.flush();
207
208   IF->getContents() = Code;
209   IF->getFixups() = Fixups;
210 }
211
212 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
213   MCDataFragment *DF = getOrCreateDataFragment();
214
215   SmallVector<MCFixup, 4> Fixups;
216   SmallString<256> Code;
217   raw_svector_ostream VecOS(Code);
218   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
219   VecOS.flush();
220
221   // Add the fixups and data.
222   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
223     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
224     DF->getFixups().push_back(Fixups[i]);
225   }
226   DF->getContents().append(Code.begin(), Code.end());
227 }
228
229 void MCPureStreamer::FinishImpl() {
230   // FIXME: Handle DWARF tables?
231
232   this->MCObjectStreamer::FinishImpl();
233 }
234
235 MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
236                                      raw_ostream &OS, MCCodeEmitter *CE) {
237   return new MCPureStreamer(Context, MAB, OS, CE);
238 }