blob: 55297596370ec84f518b186ae3739119f611a19c [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);
Daniel Dunbarabc75622010-11-17 16:06:47 +000040 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000041 uint64_t Size = 0, unsigned ByteAlignment = 0);
Daniel Dunbarabc75622010-11-17 16:06:47 +000042 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Daniel Dunbarabc75622010-11-17 16:06:47 +000043 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
44 unsigned ValueSize = 1,
45 unsigned MaxBytesToEmit = 0);
46 virtual void EmitCodeAlignment(unsigned ByteAlignment,
47 unsigned MaxBytesToEmit = 0);
Jim Grosbachebd4c052012-01-27 00:37:08 +000048 virtual bool EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +000049 unsigned char Value = 0);
Rafael Espindola99b42372012-01-07 03:13:18 +000050 virtual void FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +000051
52
53 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
54 report_fatal_error("unsupported directive in pure streamer");
55 }
56 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
57 report_fatal_error("unsupported directive in pure streamer");
58 }
59 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
60 uint64_t Size, unsigned ByteAlignment = 0) {
61 report_fatal_error("unsupported directive in pure streamer");
62 }
63 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
64 report_fatal_error("unsupported directive in pure streamer");
65 }
66 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
67 unsigned ByteAlignment) {
68 report_fatal_error("unsupported directive in pure streamer");
69 }
70 virtual void EmitThumbFunc(MCSymbol *Func) {
71 report_fatal_error("unsupported directive in pure streamer");
72 }
73 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
74 report_fatal_error("unsupported directive in pure streamer");
75 }
76 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
77 report_fatal_error("unsupported directive in pure streamer");
78 }
79 virtual void EmitCOFFSymbolType(int Type) {
80 report_fatal_error("unsupported directive in pure streamer");
81 }
82 virtual void EndCOFFSymbolDef() {
83 report_fatal_error("unsupported directive in pure streamer");
84 }
85 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
86 report_fatal_error("unsupported directive in pure streamer");
87 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000088 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
89 unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000090 report_fatal_error("unsupported directive in pure streamer");
91 }
Daniel Dunbarabc75622010-11-17 16:06:47 +000092 virtual void EmitFileDirective(StringRef Filename) {
93 report_fatal_error("unsupported directive in pure streamer");
94 }
Nick Lewycky44d798d2011-10-17 23:05:28 +000095 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
96 StringRef Filename) {
Daniel Dunbarabc75622010-11-17 16:06:47 +000097 report_fatal_error("unsupported directive in pure streamer");
Daniel Dunbarabc75622010-11-17 16:06:47 +000098 }
99
100 /// @}
101};
102
103} // end anonymous namespace.
104
105void MCPureStreamer::InitSections() {
106 // FIMXE: To what!?
107 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109 0, SectionKind::getText()));
110
111}
112
113void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
114 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
115 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000116 assert(getCurrentSection() && "Cannot emit before setting section!");
Daniel Dunbarabc75622010-11-17 16:06:47 +0000117
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000118 Symbol->setSection(*getCurrentSection());
Daniel Dunbarabc75622010-11-17 16:06:47 +0000119
120 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
121
122 // We have to create a new fragment if this is an atom defining symbol,
123 // fragments cannot span atoms.
124 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
125 new MCDataFragment(getCurrentSectionData());
126
127 // FIXME: This is wasteful, we don't necessarily need to create a data
128 // fragment. Instead, we should mark the symbol as pointing into the data
129 // fragment if it exists, otherwise we should just queue the label and set its
130 // fragment pointer when we emit the next fragment.
131 MCDataFragment *F = getOrCreateDataFragment();
132 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
133 SD.setFragment(F);
134 SD.setOffset(F->getContents().size());
135}
136
Reed Kotler2c3a4642012-12-16 04:00:45 +0000137
Daniel Dunbarabc75622010-11-17 16:06:47 +0000138void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000139 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000140 report_fatal_error("not yet implemented in pure streamer");
141}
142
143void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
144 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
145 // MCObjectStreamer.
146 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
147}
148
Daniel Dunbarabc75622010-11-17 16:06:47 +0000149void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
150 int64_t Value, unsigned ValueSize,
151 unsigned MaxBytesToEmit) {
152 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
153 // MCObjectStreamer.
154 if (MaxBytesToEmit == 0)
155 MaxBytesToEmit = ByteAlignment;
156 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
157 getCurrentSectionData());
158
159 // Update the maximum alignment on the current section if necessary.
160 if (ByteAlignment > getCurrentSectionData()->getAlignment())
161 getCurrentSectionData()->setAlignment(ByteAlignment);
162}
163
164void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
165 unsigned MaxBytesToEmit) {
166 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
167 // MCObjectStreamer.
168 if (MaxBytesToEmit == 0)
169 MaxBytesToEmit = ByteAlignment;
170 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
171 getCurrentSectionData());
172 F->setEmitNops(true);
173
174 // Update the maximum alignment on the current section if necessary.
175 if (ByteAlignment > getCurrentSectionData()->getAlignment())
176 getCurrentSectionData()->setAlignment(ByteAlignment);
177}
178
Jim Grosbachebd4c052012-01-27 00:37:08 +0000179bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000180 unsigned char Value) {
181 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
Jim Grosbachebd4c052012-01-27 00:37:08 +0000182 return false;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000183}
184
185void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
186 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
187
188 // Add the fixups and data.
189 //
190 // FIXME: Revisit this design decision when relaxation is done, we may be
191 // able to get away with not storing any extra data in the MCInst.
192 SmallVector<MCFixup, 4> Fixups;
193 SmallString<256> Code;
194 raw_svector_ostream VecOS(Code);
195 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
196 VecOS.flush();
197
Eli Bendersky64d9a322012-12-07 19:13:57 +0000198 IF->getContents() = Code;
Daniel Dunbarabc75622010-11-17 16:06:47 +0000199 IF->getFixups() = Fixups;
200}
201
202void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
203 MCDataFragment *DF = getOrCreateDataFragment();
204
205 SmallVector<MCFixup, 4> Fixups;
206 SmallString<256> Code;
207 raw_svector_ostream VecOS(Code);
208 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
209 VecOS.flush();
210
211 // Add the fixups and data.
212 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
213 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000214 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000215 }
216 DF->getContents().append(Code.begin(), Code.end());
217}
218
Rafael Espindola99b42372012-01-07 03:13:18 +0000219void MCPureStreamer::FinishImpl() {
Daniel Dunbarabc75622010-11-17 16:06:47 +0000220 // FIXME: Handle DWARF tables?
221
Rafael Espindola99b42372012-01-07 03:13:18 +0000222 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarabc75622010-11-17 16:06:47 +0000223}
224
Evan Cheng78c10ee2011-07-25 23:24:55 +0000225MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarabc75622010-11-17 16:06:47 +0000226 raw_ostream &OS, MCCodeEmitter *CE) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000227 return new MCPureStreamer(Context, MAB, OS, CE);
Daniel Dunbarabc75622010-11-17 16:06:47 +0000228}