blob: a83caf6080b6952b71234a63d9612b0d0e11530f [file] [log] [blame]
Daniel Dunbarabc75622010-11-17 16:06:47 +00001//===- 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"
Nico Rieckef1762b2013-04-14 21:18:36 +000015#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbarabc75622010-11-17 16:06:47 +000016#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbarabc75622010-11-17 16:06:47 +000017#include "llvm/MC/MCSymbol.h"
18#include "llvm/Support/ErrorHandling.h"
19
20using namespace llvm;
21
22namespace {
23
24class MCPureStreamer : public MCObjectStreamer {
25private:
26 virtual void EmitInstToFragment(const MCInst &Inst);
27 virtual void EmitInstToData(const MCInst &Inst);
28
29public:
Chandler Carruth5da36652013-01-31 23:29:57 +000030 MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
31 MCCodeEmitter *Emitter)
32 : MCObjectStreamer(SK_PureStreamer, Context, TAB, OS, Emitter) {}
Daniel Dunbarabc75622010-11-17 16:06:47 +000033
34 /// @name MCStreamer Interface
35 /// @{
36
37 virtual void InitSections();
Eli Bendersky030f63a2013-01-14 19:04:57 +000038 virtual void InitToTextSection();
Daniel Dunbarabc75622010-11-17 16:06:47 +000039 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotler082b7e62012-12-17 23:41:45 +000040 virtual void EmitDebugLabel(MCSymbol *Symbol);
Daniel Dunbarabc75622010-11-17 16:06:47 +000041 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000042 uint64_t Size = 0, unsigned ByteAlignment = 0);
Rafael Espindolaa3863ea2013-07-02 15:49:13 +000043 virtual void EmitBytes(StringRef Data);
Daniel Dunbarabc75622010-11-17 16:06:47 +000044 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
45 unsigned ValueSize = 1,
46 unsigned MaxBytesToEmit = 0);
47 virtual void EmitCodeAlignment(unsigned ByteAlignment,
48 unsigned MaxBytesToEmit = 0);
Jim Grosbachebd4c052012-01-27 00:37:08 +000049 virtual bool EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +000050 unsigned char Value = 0);
Rafael Espindola99b42372012-01-07 03:13:18 +000051 virtual void FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +000052
53
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +000054 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000055 report_fatal_error("unsupported directive in pure streamer");
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +000056 return false;
Daniel Dunbarabc75622010-11-17 16:06:47 +000057 }
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 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000090 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
91 unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000092 report_fatal_error("unsupported directive in pure streamer");
93 }
Daniel Dunbarabc75622010-11-17 16:06:47 +000094 virtual void EmitFileDirective(StringRef Filename) {
95 report_fatal_error("unsupported directive in pure streamer");
96 }
Nick Lewycky44d798d2011-10-17 23:05:28 +000097 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
Manman Ren3de61b42013-03-07 01:42:00 +000098 StringRef Filename, unsigned CUID = 0) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000099 report_fatal_error("unsupported directive in pure streamer");
Daniel Dunbarabc75622010-11-17 16:06:47 +0000100 }
101
102 /// @}
Chandler Carruth5da36652013-01-31 23:29:57 +0000103
104 static bool classof(const MCStreamer *S) {
105 return S->getKind() == SK_PureStreamer;
106 }
Daniel Dunbarabc75622010-11-17 16:06:47 +0000107};
108
109} // end anonymous namespace.
110
111void MCPureStreamer::InitSections() {
Eli Bendersky030f63a2013-01-14 19:04:57 +0000112 InitToTextSection();
113}
114
115void MCPureStreamer::InitToTextSection() {
Nico Rieckef1762b2013-04-14 21:18:36 +0000116 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000117}
118
119void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
120 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
121 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000122 assert(getCurrentSection().first && "Cannot emit before setting section!");
Daniel Dunbarabc75622010-11-17 16:06:47 +0000123
Richard Mitton5cc319a2013-09-19 23:21:01 +0000124 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000125
126 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
127
128 // We have to create a new fragment if this is an atom defining symbol,
129 // fragments cannot span atoms.
130 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
Peter Collingbournedf39be62013-04-17 21:18:16 +0000131 insert(new MCDataFragment());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000132
133 // FIXME: This is wasteful, we don't necessarily need to create a data
134 // fragment. Instead, we should mark the symbol as pointing into the data
135 // fragment if it exists, otherwise we should just queue the label and set its
136 // fragment pointer when we emit the next fragment.
137 MCDataFragment *F = getOrCreateDataFragment();
138 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
139 SD.setFragment(F);
140 SD.setOffset(F->getContents().size());
141}
142
Reed Kotler2c3a4642012-12-16 04:00:45 +0000143
Reed Kotler082b7e62012-12-17 23:41:45 +0000144void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
145 EmitLabel(Symbol);
146}
147
Daniel Dunbarabc75622010-11-17 16:06:47 +0000148void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000149 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000150 report_fatal_error("not yet implemented in pure streamer");
151}
152
Rafael Espindolaa3863ea2013-07-02 15:49:13 +0000153void MCPureStreamer::EmitBytes(StringRef Data) {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000154 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
155 // MCObjectStreamer.
156 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
157}
158
Daniel Dunbarabc75622010-11-17 16:06:47 +0000159void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
160 int64_t Value, unsigned ValueSize,
161 unsigned MaxBytesToEmit) {
162 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
163 // MCObjectStreamer.
164 if (MaxBytesToEmit == 0)
165 MaxBytesToEmit = ByteAlignment;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000166 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
Daniel Dunbarabc75622010-11-17 16:06:47 +0000167
168 // Update the maximum alignment on the current section if necessary.
169 if (ByteAlignment > getCurrentSectionData()->getAlignment())
170 getCurrentSectionData()->setAlignment(ByteAlignment);
171}
172
173void 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;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000179 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
180 insert(F);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000181 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
Jim Grosbachebd4c052012-01-27 00:37:08 +0000188bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000189 unsigned char Value) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000190 insert(new MCOrgFragment(*Offset, Value));
Jim Grosbachebd4c052012-01-27 00:37:08 +0000191 return false;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000192}
193
194void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000195 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
196 insert(IF);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000197
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
Eli Bendersky64d9a322012-12-07 19:13:57 +0000208 IF->getContents() = Code;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000209 IF->getFixups() = Fixups;
210}
211
212void 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());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000224 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000225 }
226 DF->getContents().append(Code.begin(), Code.end());
227}
228
Rafael Espindola99b42372012-01-07 03:13:18 +0000229void MCPureStreamer::FinishImpl() {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000230 // FIXME: Handle DWARF tables?
231
Rafael Espindola99b42372012-01-07 03:13:18 +0000232 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +0000233}
234
Evan Cheng78c10ee2011-07-25 23:24:55 +0000235MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000236 raw_ostream &OS, MCCodeEmitter *CE) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000237 return new MCPureStreamer(Context, MAB, OS, CE);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000238}