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/MCObjectStreamer.h"
16 // FIXME: Remove this.
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/ErrorHandling.h"
25 class MCPureStreamer : public MCObjectStreamer {
27 virtual void EmitInstToFragment(const MCInst &Inst);
28 virtual void EmitInstToData(const MCInst &Inst);
31 MCPureStreamer(MCContext &Context, TargetAsmBackend &TAB,
32 raw_ostream &OS, MCCodeEmitter *Emitter)
33 : MCObjectStreamer(Context, TAB, OS, Emitter,
34 /*PadSectionToAlignment=*/true) {}
36 /// @name MCStreamer Interface
39 virtual void InitSections();
40 virtual void EmitLabel(MCSymbol *Symbol);
41 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
42 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
43 unsigned Size = 0, unsigned ByteAlignment = 0);
44 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
45 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
46 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
47 unsigned ValueSize = 1,
48 unsigned MaxBytesToEmit = 0);
49 virtual void EmitCodeAlignment(unsigned ByteAlignment,
50 unsigned MaxBytesToEmit = 0);
51 virtual void EmitValueToOffset(const MCExpr *Offset,
52 unsigned char Value = 0);
53 virtual void Finish();
56 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
57 report_fatal_error("unsupported directive in pure streamer");
59 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
60 report_fatal_error("unsupported directive in pure streamer");
62 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
63 uint64_t Size, unsigned ByteAlignment = 0) {
64 report_fatal_error("unsupported directive in pure streamer");
66 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
67 report_fatal_error("unsupported directive in pure streamer");
69 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
70 unsigned ByteAlignment) {
71 report_fatal_error("unsupported directive in pure streamer");
73 virtual void EmitThumbFunc(MCSymbol *Func) {
74 report_fatal_error("unsupported directive in pure streamer");
76 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
77 report_fatal_error("unsupported directive in pure streamer");
79 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
80 report_fatal_error("unsupported directive in pure streamer");
82 virtual void EmitCOFFSymbolType(int Type) {
83 report_fatal_error("unsupported directive in pure streamer");
85 virtual void EndCOFFSymbolDef() {
86 report_fatal_error("unsupported directive in pure streamer");
88 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
89 report_fatal_error("unsupported directive in pure streamer");
91 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
92 report_fatal_error("unsupported directive in pure streamer");
94 virtual void EmitGPRel32Value(const MCExpr *Value) {
95 report_fatal_error("unsupported directive in pure streamer");
97 virtual void EmitFileDirective(StringRef Filename) {
98 report_fatal_error("unsupported directive in pure streamer");
100 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
101 report_fatal_error("unsupported directive in pure streamer");
108 } // end anonymous namespace.
110 void MCPureStreamer::InitSections() {
112 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
114 0, SectionKind::getText()));
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(CurSection && "Cannot emit before setting section!");
123 Symbol->setSection(*CurSection);
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 new MCDataFragment(getCurrentSectionData());
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());
142 void MCPureStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
143 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
145 // FIXME: Lift context changes into super class.
146 getAssembler().getOrCreateSymbolData(*Symbol);
147 Symbol->setVariableValue(AddValueSymbols(Value));
150 void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
151 unsigned Size, unsigned ByteAlignment) {
152 report_fatal_error("not yet implemented in pure streamer");
155 void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
156 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
158 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
161 void MCPureStreamer::EmitValue(const MCExpr *Value, unsigned Size,
162 unsigned AddrSpace) {
163 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
165 MCDataFragment *DF = getOrCreateDataFragment();
167 // Avoid fixups when possible.
169 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
170 // FIXME: Endianness assumption.
171 for (unsigned i = 0; i != Size; ++i)
172 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
174 DF->addFixup(MCFixup::Create(DF->getContents().size(),
175 AddValueSymbols(Value),
176 MCFixup::getKindForSize(Size)));
177 DF->getContents().resize(DF->getContents().size() + Size, 0);
181 void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
182 int64_t Value, unsigned ValueSize,
183 unsigned MaxBytesToEmit) {
184 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
186 if (MaxBytesToEmit == 0)
187 MaxBytesToEmit = ByteAlignment;
188 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
189 getCurrentSectionData());
191 // Update the maximum alignment on the current section if necessary.
192 if (ByteAlignment > getCurrentSectionData()->getAlignment())
193 getCurrentSectionData()->setAlignment(ByteAlignment);
196 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
197 unsigned MaxBytesToEmit) {
198 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
200 if (MaxBytesToEmit == 0)
201 MaxBytesToEmit = ByteAlignment;
202 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
203 getCurrentSectionData());
204 F->setEmitNops(true);
206 // Update the maximum alignment on the current section if necessary.
207 if (ByteAlignment > getCurrentSectionData()->getAlignment())
208 getCurrentSectionData()->setAlignment(ByteAlignment);
211 void MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
212 unsigned char Value) {
213 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
216 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
217 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
219 // Add the fixups and data.
221 // FIXME: Revisit this design decision when relaxation is done, we may be
222 // able to get away with not storing any extra data in the MCInst.
223 SmallVector<MCFixup, 4> Fixups;
224 SmallString<256> Code;
225 raw_svector_ostream VecOS(Code);
226 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
229 IF->getCode() = Code;
230 IF->getFixups() = Fixups;
233 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
234 MCDataFragment *DF = getOrCreateDataFragment();
236 SmallVector<MCFixup, 4> Fixups;
237 SmallString<256> Code;
238 raw_svector_ostream VecOS(Code);
239 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
242 // Add the fixups and data.
243 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
244 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
245 DF->addFixup(Fixups[i]);
247 DF->getContents().append(Code.begin(), Code.end());
250 void MCPureStreamer::Finish() {
251 // FIXME: Handle DWARF tables?
253 this->MCObjectStreamer::Finish();
256 MCStreamer *llvm::createPureStreamer(MCContext &Context, TargetAsmBackend &TAB,
257 raw_ostream &OS, MCCodeEmitter *CE) {
258 return new MCPureStreamer(Context, TAB, OS, CE);