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, unsigned AddrSpace);
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 void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
55 report_fatal_error("unsupported directive in pure streamer");
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
58 report_fatal_error("unsupported directive in pure streamer");
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");
64 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
65 report_fatal_error("unsupported directive in pure streamer");
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment) {
69 report_fatal_error("unsupported directive in pure streamer");
71 virtual void EmitThumbFunc(MCSymbol *Func) {
72 report_fatal_error("unsupported directive in pure streamer");
74 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
75 report_fatal_error("unsupported directive in pure streamer");
77 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
78 report_fatal_error("unsupported directive in pure streamer");
80 virtual void EmitCOFFSymbolType(int Type) {
81 report_fatal_error("unsupported directive in pure streamer");
83 virtual void EndCOFFSymbolDef() {
84 report_fatal_error("unsupported directive in pure streamer");
86 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
87 report_fatal_error("unsupported directive in pure streamer");
89 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
90 unsigned ByteAlignment) {
91 report_fatal_error("unsupported directive in pure streamer");
93 virtual void EmitFileDirective(StringRef Filename) {
94 report_fatal_error("unsupported directive in pure streamer");
96 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
97 StringRef Filename, unsigned CUID = 0) {
98 report_fatal_error("unsupported directive in pure streamer");
103 static bool classof(const MCStreamer *S) {
104 return S->getKind() == SK_PureStreamer;
108 } // end anonymous namespace.
110 void MCPureStreamer::InitSections() {
114 void MCPureStreamer::InitToTextSection() {
115 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
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().first && "Cannot emit before setting section!");
123 Symbol->setSection(*getCurrentSection().first);
125 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
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 insert(new MCDataFragment());
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!");
139 SD.setOffset(F->getContents().size());
143 void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
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");
152 void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
153 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
155 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
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
163 if (MaxBytesToEmit == 0)
164 MaxBytesToEmit = ByteAlignment;
165 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
167 // Update the maximum alignment on the current section if necessary.
168 if (ByteAlignment > getCurrentSectionData()->getAlignment())
169 getCurrentSectionData()->setAlignment(ByteAlignment);
172 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
173 unsigned MaxBytesToEmit) {
174 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
176 if (MaxBytesToEmit == 0)
177 MaxBytesToEmit = ByteAlignment;
178 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
180 F->setEmitNops(true);
182 // Update the maximum alignment on the current section if necessary.
183 if (ByteAlignment > getCurrentSectionData()->getAlignment())
184 getCurrentSectionData()->setAlignment(ByteAlignment);
187 bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
188 unsigned char Value) {
189 insert(new MCOrgFragment(*Offset, Value));
193 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
194 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
197 // Add the fixups and data.
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);
207 IF->getContents() = Code;
208 IF->getFixups() = Fixups;
211 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
212 MCDataFragment *DF = getOrCreateDataFragment();
214 SmallVector<MCFixup, 4> Fixups;
215 SmallString<256> Code;
216 raw_svector_ostream VecOS(Code);
217 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
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());
223 DF->getFixups().push_back(Fixups[i]);
225 DF->getContents().append(Code.begin(), Code.end());
228 void MCPureStreamer::FinishImpl() {
229 // FIXME: Handle DWARF tables?
231 this->MCObjectStreamer::FinishImpl();
234 MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
235 raw_ostream &OS, MCCodeEmitter *CE) {
236 return new MCPureStreamer(Context, MAB, OS, CE);