blob: 8ae724facb0c21bb94987c1559b2c22a799d5600 [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);
Daniel Dunbarabc75622010-11-17 16:06:47 +000043 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
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
54 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
55 report_fatal_error("unsupported directive in pure streamer");
56 }
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
58 report_fatal_error("unsupported directive in pure streamer");
59 }
60 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
61 uint64_t Size, unsigned ByteAlignment = 0) {
62 report_fatal_error("unsupported directive in pure streamer");
63 }
64 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
65 report_fatal_error("unsupported directive in pure streamer");
66 }
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment) {
69 report_fatal_error("unsupported directive in pure streamer");
70 }
71 virtual void EmitThumbFunc(MCSymbol *Func) {
72 report_fatal_error("unsupported directive in pure streamer");
73 }
74 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
75 report_fatal_error("unsupported directive in pure streamer");
76 }
77 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
78 report_fatal_error("unsupported directive in pure streamer");
79 }
80 virtual void EmitCOFFSymbolType(int Type) {
81 report_fatal_error("unsupported directive in pure streamer");
82 }
83 virtual void EndCOFFSymbolDef() {
84 report_fatal_error("unsupported directive in pure streamer");
85 }
86 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
87 report_fatal_error("unsupported directive in pure streamer");
88 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000089 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
90 unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000091 report_fatal_error("unsupported directive in pure streamer");
92 }
Daniel Dunbarabc75622010-11-17 16:06:47 +000093 virtual void EmitFileDirective(StringRef Filename) {
94 report_fatal_error("unsupported directive in pure streamer");
95 }
Nick Lewycky44d798d2011-10-17 23:05:28 +000096 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
Manman Ren3de61b42013-03-07 01:42:00 +000097 StringRef Filename, unsigned CUID = 0) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000098 report_fatal_error("unsupported directive in pure streamer");
Daniel Dunbarabc75622010-11-17 16:06:47 +000099 }
100
101 /// @}
Chandler Carruth5da36652013-01-31 23:29:57 +0000102
103 static bool classof(const MCStreamer *S) {
104 return S->getKind() == SK_PureStreamer;
105 }
Daniel Dunbarabc75622010-11-17 16:06:47 +0000106};
107
108} // end anonymous namespace.
109
110void MCPureStreamer::InitSections() {
Eli Bendersky030f63a2013-01-14 19:04:57 +0000111 InitToTextSection();
112}
113
114void MCPureStreamer::InitToTextSection() {
Nico Rieckef1762b2013-04-14 21:18:36 +0000115 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000116}
117
118void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
119 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
120 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000121 assert(getCurrentSection().first && "Cannot emit before setting section!");
Daniel Dunbarabc75622010-11-17 16:06:47 +0000122
Peter Collingbournedf39be62013-04-17 21:18:16 +0000123 Symbol->setSection(*getCurrentSection().first);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000124
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()))
Peter Collingbournedf39be62013-04-17 21:18:16 +0000130 insert(new MCDataFragment());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000131
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
Reed Kotler2c3a4642012-12-16 04:00:45 +0000142
Reed Kotler082b7e62012-12-17 23:41:45 +0000143void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
144 EmitLabel(Symbol);
145}
146
Daniel Dunbarabc75622010-11-17 16:06:47 +0000147void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000148 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000149 report_fatal_error("not yet implemented in pure streamer");
150}
151
152void 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
Daniel Dunbarabc75622010-11-17 16:06:47 +0000158void 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;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000165 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
Daniel Dunbarabc75622010-11-17 16:06:47 +0000166
167 // Update the maximum alignment on the current section if necessary.
168 if (ByteAlignment > getCurrentSectionData()->getAlignment())
169 getCurrentSectionData()->setAlignment(ByteAlignment);
170}
171
172void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
173 unsigned MaxBytesToEmit) {
174 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
175 // MCObjectStreamer.
176 if (MaxBytesToEmit == 0)
177 MaxBytesToEmit = ByteAlignment;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000178 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
179 insert(F);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000180 F->setEmitNops(true);
181
182 // Update the maximum alignment on the current section if necessary.
183 if (ByteAlignment > getCurrentSectionData()->getAlignment())
184 getCurrentSectionData()->setAlignment(ByteAlignment);
185}
186
Jim Grosbachebd4c052012-01-27 00:37:08 +0000187bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000188 unsigned char Value) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000189 insert(new MCOrgFragment(*Offset, Value));
Jim Grosbachebd4c052012-01-27 00:37:08 +0000190 return false;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000191}
192
193void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000194 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
195 insert(IF);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000196
197 // Add the fixups and data.
198 //
199 // FIXME: Revisit this design decision when relaxation is done, we may be
200 // able to get away with not storing any extra data in the MCInst.
201 SmallVector<MCFixup, 4> Fixups;
202 SmallString<256> Code;
203 raw_svector_ostream VecOS(Code);
204 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
205 VecOS.flush();
206
Eli Bendersky64d9a322012-12-07 19:13:57 +0000207 IF->getContents() = Code;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000208 IF->getFixups() = Fixups;
209}
210
211void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
212 MCDataFragment *DF = getOrCreateDataFragment();
213
214 SmallVector<MCFixup, 4> Fixups;
215 SmallString<256> Code;
216 raw_svector_ostream VecOS(Code);
217 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
218 VecOS.flush();
219
220 // Add the fixups and data.
221 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
222 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000223 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000224 }
225 DF->getContents().append(Code.begin(), Code.end());
226}
227
Rafael Espindola99b42372012-01-07 03:13:18 +0000228void MCPureStreamer::FinishImpl() {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000229 // FIXME: Handle DWARF tables?
230
Rafael Espindola99b42372012-01-07 03:13:18 +0000231 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +0000232}
233
Evan Cheng78c10ee2011-07-25 23:24:55 +0000234MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000235 raw_ostream &OS, MCCodeEmitter *CE) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000236 return new MCPureStreamer(Context, MAB, OS, CE);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000237}