blob: 1a2a92e3293b24cb52c96868eeb5f43c8224382e [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:
Evan Cheng78c10ee2011-07-25 23:24:55 +000031 MCPureStreamer(MCContext &Context, MCAsmBackend &TAB,
Daniel Dunbarabc75622010-11-17 16:06:47 +000032 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola85f2ecc2010-12-07 00:27:36 +000033 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbarabc75622010-11-17 16:06:47 +000034
35 /// @name MCStreamer Interface
36 /// @{
37
38 virtual void InitSections();
39 virtual void EmitLabel(MCSymbol *Symbol);
40 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
41 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
42 unsigned Size = 0, unsigned ByteAlignment = 0);
43 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Daniel Dunbarabc75622010-11-17 16:06:47 +000044 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 void EmitValueToOffset(const MCExpr *Offset,
50 unsigned char Value = 0);
51 virtual void Finish();
52
53
54 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
55 report_fatal_error("unsupported directive in pure streamer");
56 }
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
58 report_fatal_error("unsupported directive in pure streamer");
59 }
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");
63 }
64 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
65 report_fatal_error("unsupported directive in pure streamer");
66 }
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment) {
69 report_fatal_error("unsupported directive in pure streamer");
70 }
71 virtual void EmitThumbFunc(MCSymbol *Func) {
72 report_fatal_error("unsupported directive in pure streamer");
73 }
74 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
75 report_fatal_error("unsupported directive in pure streamer");
76 }
77 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
78 report_fatal_error("unsupported directive in pure streamer");
79 }
80 virtual void EmitCOFFSymbolType(int Type) {
81 report_fatal_error("unsupported directive in pure streamer");
82 }
83 virtual void EndCOFFSymbolDef() {
84 report_fatal_error("unsupported directive in pure streamer");
85 }
86 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
87 report_fatal_error("unsupported directive in pure streamer");
88 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000089 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
90 unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000091 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 }
Nick Lewycky44d798d2011-10-17 23:05:28 +000096 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
97 StringRef Filename) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000098 report_fatal_error("unsupported directive in pure streamer");
99 return false;
100 }
101
102 /// @}
103};
104
105} // end anonymous namespace.
106
107void MCPureStreamer::InitSections() {
108 // FIMXE: To what!?
109 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
110 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
111 0, SectionKind::getText()));
112
113}
114
115void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
116 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
117 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000118 assert(getCurrentSection() && "Cannot emit before setting section!");
Daniel Dunbarabc75622010-11-17 16:06:47 +0000119
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000120 Symbol->setSection(*getCurrentSection());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000121
122 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
123
124 // We have to create a new fragment if this is an atom defining symbol,
125 // fragments cannot span atoms.
126 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
127 new MCDataFragment(getCurrentSectionData());
128
129 // FIXME: This is wasteful, we don't necessarily need to create a data
130 // fragment. Instead, we should mark the symbol as pointing into the data
131 // fragment if it exists, otherwise we should just queue the label and set its
132 // fragment pointer when we emit the next fragment.
133 MCDataFragment *F = getOrCreateDataFragment();
134 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
135 SD.setFragment(F);
136 SD.setOffset(F->getContents().size());
137}
138
139void MCPureStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
140 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
141 // MCObjectStreamer.
142 // FIXME: Lift context changes into super class.
143 getAssembler().getOrCreateSymbolData(*Symbol);
144 Symbol->setVariableValue(AddValueSymbols(Value));
145}
146
147void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
148 unsigned Size, unsigned ByteAlignment) {
149 report_fatal_error("not yet implemented in pure streamer");
150}
151
152void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
153 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
154 // MCObjectStreamer.
155 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
156}
157
Daniel Dunbarabc75622010-11-17 16:06:47 +0000158void 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
162 // MCObjectStreamer.
163 if (MaxBytesToEmit == 0)
164 MaxBytesToEmit = ByteAlignment;
165 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
166 getCurrentSectionData());
167
168 // Update the maximum alignment on the current section if necessary.
169 if (ByteAlignment > getCurrentSectionData()->getAlignment())
170 getCurrentSectionData()->setAlignment(ByteAlignment);
171}
172
173void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
174 unsigned MaxBytesToEmit) {
175 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
176 // MCObjectStreamer.
177 if (MaxBytesToEmit == 0)
178 MaxBytesToEmit = ByteAlignment;
179 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
180 getCurrentSectionData());
181 F->setEmitNops(true);
182
183 // Update the maximum alignment on the current section if necessary.
184 if (ByteAlignment > getCurrentSectionData()->getAlignment())
185 getCurrentSectionData()->setAlignment(ByteAlignment);
186}
187
188void MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
189 unsigned char Value) {
190 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
191}
192
193void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
194 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
195
196 // Add the fixups and data.
197 //
198 // FIXME: Revisit this design decision when relaxation is done, we may be
199 // able to get away with not storing any extra data in the MCInst.
200 SmallVector<MCFixup, 4> Fixups;
201 SmallString<256> Code;
202 raw_svector_ostream VecOS(Code);
203 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
204 VecOS.flush();
205
206 IF->getCode() = Code;
207 IF->getFixups() = Fixups;
208}
209
210void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
211 MCDataFragment *DF = getOrCreateDataFragment();
212
213 SmallVector<MCFixup, 4> Fixups;
214 SmallString<256> Code;
215 raw_svector_ostream VecOS(Code);
216 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
217 VecOS.flush();
218
219 // Add the fixups and data.
220 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
221 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
222 DF->addFixup(Fixups[i]);
223 }
224 DF->getContents().append(Code.begin(), Code.end());
225}
226
227void MCPureStreamer::Finish() {
228 // FIXME: Handle DWARF tables?
229
230 this->MCObjectStreamer::Finish();
231}
232
Evan Cheng78c10ee2011-07-25 23:24:55 +0000233MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000234 raw_ostream &OS, MCCodeEmitter *CE) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000235 return new MCPureStreamer(Context, MAB, OS, CE);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000236}