1 //===- lib/MC/MCPureStreamer.cpp - MC "Pure" Object Output ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/MCObjectFileInfo.h"
16 #include "llvm/MC/MCObjectStreamer.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/ErrorHandling.h"
24 class MCPureStreamer : public MCObjectStreamer {
26 virtual void EmitInstToFragment(const MCInst &Inst);
27 virtual void EmitInstToData(const MCInst &Inst);
30 MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
31 MCCodeEmitter *Emitter)
32 : MCObjectStreamer(SK_PureStreamer, Context, TAB, OS, Emitter) {}
34 /// @name MCStreamer Interface
37 virtual void InitSections();
38 virtual void InitToTextSection();
39 virtual void EmitLabel(MCSymbol *Symbol);
40 virtual void EmitDebugLabel(MCSymbol *Symbol);
41 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
42 uint64_t Size = 0, unsigned ByteAlignment = 0);
43 virtual void EmitBytes(StringRef Data);
44 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);
49 virtual bool EmitValueToOffset(const MCExpr *Offset,
50 unsigned char Value = 0);
51 virtual void FinishImpl();
54 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
55 report_fatal_error("unsupported directive in pure streamer");
58 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
59 report_fatal_error("unsupported directive in pure streamer");
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");
65 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
66 report_fatal_error("unsupported directive in pure streamer");
68 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69 unsigned ByteAlignment) {
70 report_fatal_error("unsupported directive in pure streamer");
72 virtual void EmitThumbFunc(MCSymbol *Func) {
73 report_fatal_error("unsupported directive in pure streamer");
75 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
76 report_fatal_error("unsupported directive in pure streamer");
78 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
79 report_fatal_error("unsupported directive in pure streamer");
81 virtual void EmitCOFFSymbolType(int Type) {
82 report_fatal_error("unsupported directive in pure streamer");
84 virtual void EndCOFFSymbolDef() {
85 report_fatal_error("unsupported directive in pure streamer");
87 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
88 report_fatal_error("unsupported directive in pure streamer");
90 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
91 unsigned ByteAlignment) {
92 report_fatal_error("unsupported directive in pure streamer");
94 virtual void EmitFileDirective(StringRef Filename) {
95 report_fatal_error("unsupported directive in pure streamer");
97 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
98 StringRef Filename, unsigned CUID = 0) {
99 report_fatal_error("unsupported directive in pure streamer");
104 static bool classof(const MCStreamer *S) {
105 return S->getKind() == SK_PureStreamer;
109 } // end anonymous namespace.
111 void MCPureStreamer::InitSections() {
115 void MCPureStreamer::InitToTextSection() {
116 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
119 void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
120 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
121 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
122 assert(getCurrentSection().first && "Cannot emit before setting section!");
124 Symbol->setSection(*getCurrentSection().first);
126 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
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()))
131 insert(new MCDataFragment());
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!");
140 SD.setOffset(F->getContents().size());
144 void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
148 void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
149 uint64_t Size, unsigned ByteAlignment) {
150 report_fatal_error("not yet implemented in pure streamer");
153 void MCPureStreamer::EmitBytes(StringRef Data) {
154 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
156 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
159 void 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
164 if (MaxBytesToEmit == 0)
165 MaxBytesToEmit = ByteAlignment;
166 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
168 // Update the maximum alignment on the current section if necessary.
169 if (ByteAlignment > getCurrentSectionData()->getAlignment())
170 getCurrentSectionData()->setAlignment(ByteAlignment);
173 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
174 unsigned MaxBytesToEmit) {
175 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
177 if (MaxBytesToEmit == 0)
178 MaxBytesToEmit = ByteAlignment;
179 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
181 F->setEmitNops(true);
183 // Update the maximum alignment on the current section if necessary.
184 if (ByteAlignment > getCurrentSectionData()->getAlignment())
185 getCurrentSectionData()->setAlignment(ByteAlignment);
188 bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
189 unsigned char Value) {
190 insert(new MCOrgFragment(*Offset, Value));
194 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
195 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
198 // Add the fixups and data.
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);
208 IF->getContents() = Code;
209 IF->getFixups() = Fixups;
212 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
213 MCDataFragment *DF = getOrCreateDataFragment();
215 SmallVector<MCFixup, 4> Fixups;
216 SmallString<256> Code;
217 raw_svector_ostream VecOS(Code);
218 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
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]);
226 DF->getContents().append(Code.begin(), Code.end());
229 void MCPureStreamer::FinishImpl() {
230 // FIXME: Handle DWARF tables?
232 this->MCObjectStreamer::FinishImpl();
235 MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
236 raw_ostream &OS, MCCodeEmitter *CE) {
237 return new MCPureStreamer(Context, MAB, OS, CE);