blob: 573308abb85d0d074fbbebc36639d4da25006995 [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"
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
21using namespace llvm;
22
23namespace {
24
25class MCPureStreamer : public MCObjectStreamer {
26private:
27 virtual void EmitInstToFragment(const MCInst &Inst);
28 virtual void EmitInstToData(const MCInst &Inst);
29
30public:
Chandler Carruth5da36652013-01-31 23:29:57 +000031 MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
32 MCCodeEmitter *Emitter)
33 : MCObjectStreamer(SK_PureStreamer, Context, TAB, OS, Emitter) {}
Daniel Dunbarabc75622010-11-17 16:06:47 +000034
35 /// @name MCStreamer Interface
36 /// @{
37
38 virtual void InitSections();
Eli Bendersky030f63a2013-01-14 19:04:57 +000039 virtual void InitToTextSection();
Daniel Dunbarabc75622010-11-17 16:06:47 +000040 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotler082b7e62012-12-17 23:41:45 +000041 virtual void EmitDebugLabel(MCSymbol *Symbol);
Daniel Dunbarabc75622010-11-17 16:06:47 +000042 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000043 uint64_t Size = 0, unsigned ByteAlignment = 0);
Daniel Dunbarabc75622010-11-17 16:06:47 +000044 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Daniel Dunbarabc75622010-11-17 16:06:47 +000045 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);
Jim Grosbachebd4c052012-01-27 00:37:08 +000050 virtual bool EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +000051 unsigned char Value = 0);
Rafael Espindola99b42372012-01-07 03:13:18 +000052 virtual void FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +000053
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 }
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,
98 StringRef Filename) {
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() {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000116 // FIMXE: To what!?
117 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
118 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
119 0, SectionKind::getText()));
Daniel Dunbarabc75622010-11-17 16:06:47 +0000120}
121
122void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
123 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
124 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000125 assert(getCurrentSection() && "Cannot emit before setting section!");
Daniel Dunbarabc75622010-11-17 16:06:47 +0000126
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000127 Symbol->setSection(*getCurrentSection());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000128
129 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
130
131 // We have to create a new fragment if this is an atom defining symbol,
132 // fragments cannot span atoms.
133 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
134 new MCDataFragment(getCurrentSectionData());
135
136 // FIXME: This is wasteful, we don't necessarily need to create a data
137 // fragment. Instead, we should mark the symbol as pointing into the data
138 // fragment if it exists, otherwise we should just queue the label and set its
139 // fragment pointer when we emit the next fragment.
140 MCDataFragment *F = getOrCreateDataFragment();
141 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
142 SD.setFragment(F);
143 SD.setOffset(F->getContents().size());
144}
145
Reed Kotler2c3a4642012-12-16 04:00:45 +0000146
Reed Kotler082b7e62012-12-17 23:41:45 +0000147void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
148 EmitLabel(Symbol);
149}
150
Daniel Dunbarabc75622010-11-17 16:06:47 +0000151void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000152 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000153 report_fatal_error("not yet implemented in pure streamer");
154}
155
156void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
157 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
158 // MCObjectStreamer.
159 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
160}
161
Daniel Dunbarabc75622010-11-17 16:06:47 +0000162void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
163 int64_t Value, unsigned ValueSize,
164 unsigned MaxBytesToEmit) {
165 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
166 // MCObjectStreamer.
167 if (MaxBytesToEmit == 0)
168 MaxBytesToEmit = ByteAlignment;
169 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
170 getCurrentSectionData());
171
172 // Update the maximum alignment on the current section if necessary.
173 if (ByteAlignment > getCurrentSectionData()->getAlignment())
174 getCurrentSectionData()->setAlignment(ByteAlignment);
175}
176
177void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
178 unsigned MaxBytesToEmit) {
179 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
180 // MCObjectStreamer.
181 if (MaxBytesToEmit == 0)
182 MaxBytesToEmit = ByteAlignment;
183 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
184 getCurrentSectionData());
185 F->setEmitNops(true);
186
187 // Update the maximum alignment on the current section if necessary.
188 if (ByteAlignment > getCurrentSectionData()->getAlignment())
189 getCurrentSectionData()->setAlignment(ByteAlignment);
190}
191
Jim Grosbachebd4c052012-01-27 00:37:08 +0000192bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000193 unsigned char Value) {
194 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Jim Grosbachebd4c052012-01-27 00:37:08 +0000195 return false;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000196}
197
198void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
Eli Bendersky251040b2013-01-08 00:22:56 +0000199 MCRelaxableFragment *IF =
200 new MCRelaxableFragment(Inst, getCurrentSectionData());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000201
202 // Add the fixups and data.
203 //
204 // FIXME: Revisit this design decision when relaxation is done, we may be
205 // able to get away with not storing any extra data in the MCInst.
206 SmallVector<MCFixup, 4> Fixups;
207 SmallString<256> Code;
208 raw_svector_ostream VecOS(Code);
209 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
210 VecOS.flush();
211
Eli Bendersky64d9a322012-12-07 19:13:57 +0000212 IF->getContents() = Code;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000213 IF->getFixups() = Fixups;
214}
215
216void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
217 MCDataFragment *DF = getOrCreateDataFragment();
218
219 SmallVector<MCFixup, 4> Fixups;
220 SmallString<256> Code;
221 raw_svector_ostream VecOS(Code);
222 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
223 VecOS.flush();
224
225 // Add the fixups and data.
226 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
227 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000228 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000229 }
230 DF->getContents().append(Code.begin(), Code.end());
231}
232
Rafael Espindola99b42372012-01-07 03:13:18 +0000233void MCPureStreamer::FinishImpl() {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000234 // FIXME: Handle DWARF tables?
235
Rafael Espindola99b42372012-01-07 03:13:18 +0000236 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +0000237}
238
Evan Cheng78c10ee2011-07-25 23:24:55 +0000239MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000240 raw_ostream &OS, MCCodeEmitter *CE) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000241 return new MCPureStreamer(Context, MAB, OS, CE);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000242}