blob: af23c91211749ba254e28a999246d2d30f346e6d [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:
31 MCPureStreamer(MCContext &Context, TargetAsmBackend &TAB,
32 raw_ostream &OS, MCCodeEmitter *Emitter)
33 : MCObjectStreamer(Context, TAB, OS, Emitter,
34 /*PadSectionToAlignment=*/true) {}
35
36 /// @name MCStreamer Interface
37 /// @{
38
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);
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);
50 virtual void EmitValueToOffset(const MCExpr *Offset,
51 unsigned char Value = 0);
52 virtual void Finish();
53
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 }
90 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
91 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 }
96 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
97 report_fatal_error("unsupported directive in pure streamer");
98 return false;
99 }
100
101 /// @}
102};
103
104} // end anonymous namespace.
105
106void MCPureStreamer::InitSections() {
107 // FIMXE: To what!?
108 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
109 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
110 0, SectionKind::getText()));
111
112}
113
114void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
115 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
116 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
117 assert(CurSection && "Cannot emit before setting section!");
118
119 Symbol->setSection(*CurSection);
120
121 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
122
123 // We have to create a new fragment if this is an atom defining symbol,
124 // fragments cannot span atoms.
125 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
126 new MCDataFragment(getCurrentSectionData());
127
128 // FIXME: This is wasteful, we don't necessarily need to create a data
129 // fragment. Instead, we should mark the symbol as pointing into the data
130 // fragment if it exists, otherwise we should just queue the label and set its
131 // fragment pointer when we emit the next fragment.
132 MCDataFragment *F = getOrCreateDataFragment();
133 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
134 SD.setFragment(F);
135 SD.setOffset(F->getContents().size());
136}
137
138void MCPureStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
139 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
140 // MCObjectStreamer.
141 // FIXME: Lift context changes into super class.
142 getAssembler().getOrCreateSymbolData(*Symbol);
143 Symbol->setVariableValue(AddValueSymbols(Value));
144}
145
146void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
147 unsigned Size, unsigned ByteAlignment) {
148 report_fatal_error("not yet implemented in pure streamer");
149}
150
151void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
152 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
153 // MCObjectStreamer.
154 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
155}
156
Daniel Dunbarabc75622010-11-17 16:06:47 +0000157void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
158 int64_t Value, unsigned ValueSize,
159 unsigned MaxBytesToEmit) {
160 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
161 // MCObjectStreamer.
162 if (MaxBytesToEmit == 0)
163 MaxBytesToEmit = ByteAlignment;
164 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
165 getCurrentSectionData());
166
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;
178 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
179 getCurrentSectionData());
180 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
187void MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
188 unsigned char Value) {
189 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
190}
191
192void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
193 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
194
195 // Add the fixups and data.
196 //
197 // FIXME: Revisit this design decision when relaxation is done, we may be
198 // able to get away with not storing any extra data in the MCInst.
199 SmallVector<MCFixup, 4> Fixups;
200 SmallString<256> Code;
201 raw_svector_ostream VecOS(Code);
202 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
203 VecOS.flush();
204
205 IF->getCode() = Code;
206 IF->getFixups() = Fixups;
207}
208
209void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
210 MCDataFragment *DF = getOrCreateDataFragment();
211
212 SmallVector<MCFixup, 4> Fixups;
213 SmallString<256> Code;
214 raw_svector_ostream VecOS(Code);
215 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
216 VecOS.flush();
217
218 // Add the fixups and data.
219 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
220 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
221 DF->addFixup(Fixups[i]);
222 }
223 DF->getContents().append(Code.begin(), Code.end());
224}
225
226void MCPureStreamer::Finish() {
227 // FIXME: Handle DWARF tables?
228
229 this->MCObjectStreamer::Finish();
230}
231
232MCStreamer *llvm::createPureStreamer(MCContext &Context, TargetAsmBackend &TAB,
233 raw_ostream &OS, MCCodeEmitter *CE) {
234 return new MCPureStreamer(Context, TAB, OS, CE);
235}