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
) {}
35 /// @name MCStreamer Interface
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
);
44 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();
54 virtual void EmitSymbolAttribute(MCSymbol
*Symbol
, MCSymbolAttr Attribute
) {
55 report_fatal_error("unsupported directive in pure streamer");
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag
) {
58 report_fatal_error("unsupported directive in pure streamer");
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");
64 virtual void EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
65 report_fatal_error("unsupported directive in pure streamer");
67 virtual void EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
68 unsigned ByteAlignment
) {
69 report_fatal_error("unsupported directive in pure streamer");
71 virtual void EmitThumbFunc(MCSymbol
*Func
) {
72 report_fatal_error("unsupported directive in pure streamer");
74 virtual void BeginCOFFSymbolDef(const MCSymbol
*Symbol
) {
75 report_fatal_error("unsupported directive in pure streamer");
77 virtual void EmitCOFFSymbolStorageClass(int StorageClass
) {
78 report_fatal_error("unsupported directive in pure streamer");
80 virtual void EmitCOFFSymbolType(int Type
) {
81 report_fatal_error("unsupported directive in pure streamer");
83 virtual void EndCOFFSymbolDef() {
84 report_fatal_error("unsupported directive in pure streamer");
86 virtual void EmitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) {
87 report_fatal_error("unsupported directive in pure streamer");
89 virtual void EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
) {
90 report_fatal_error("unsupported directive in pure streamer");
92 virtual void EmitFileDirective(StringRef Filename
) {
93 report_fatal_error("unsupported directive in pure streamer");
95 virtual bool EmitDwarfFileDirective(unsigned FileNo
, StringRef Filename
) {
96 report_fatal_error("unsupported directive in pure streamer");
103 } // end anonymous namespace.
105 void MCPureStreamer::InitSections() {
107 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS
,
109 0, SectionKind::getText()));
113 void MCPureStreamer::EmitLabel(MCSymbol
*Symbol
) {
114 assert(Symbol
->isUndefined() && "Cannot define a symbol twice!");
115 assert(!Symbol
->isVariable() && "Cannot emit a variable symbol!");
116 assert(getCurrentSection() && "Cannot emit before setting section!");
118 Symbol
->setSection(*getCurrentSection());
120 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
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());
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!");
134 SD
.setOffset(F
->getContents().size());
137 void MCPureStreamer::EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
138 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
140 // FIXME: Lift context changes into super class.
141 getAssembler().getOrCreateSymbolData(*Symbol
);
142 Symbol
->setVariableValue(AddValueSymbols(Value
));
145 void MCPureStreamer::EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
,
146 unsigned Size
, unsigned ByteAlignment
) {
147 report_fatal_error("not yet implemented in pure streamer");
150 void MCPureStreamer::EmitBytes(StringRef Data
, unsigned AddrSpace
) {
151 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
153 getOrCreateDataFragment()->getContents().append(Data
.begin(), Data
.end());
156 void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment
,
157 int64_t Value
, unsigned ValueSize
,
158 unsigned MaxBytesToEmit
) {
159 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
161 if (MaxBytesToEmit
== 0)
162 MaxBytesToEmit
= ByteAlignment
;
163 new MCAlignFragment(ByteAlignment
, Value
, ValueSize
, MaxBytesToEmit
,
164 getCurrentSectionData());
166 // Update the maximum alignment on the current section if necessary.
167 if (ByteAlignment
> getCurrentSectionData()->getAlignment())
168 getCurrentSectionData()->setAlignment(ByteAlignment
);
171 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment
,
172 unsigned MaxBytesToEmit
) {
173 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
175 if (MaxBytesToEmit
== 0)
176 MaxBytesToEmit
= ByteAlignment
;
177 MCAlignFragment
*F
= new MCAlignFragment(ByteAlignment
, 0, 1, MaxBytesToEmit
,
178 getCurrentSectionData());
179 F
->setEmitNops(true);
181 // Update the maximum alignment on the current section if necessary.
182 if (ByteAlignment
> getCurrentSectionData()->getAlignment())
183 getCurrentSectionData()->setAlignment(ByteAlignment
);
186 void MCPureStreamer::EmitValueToOffset(const MCExpr
*Offset
,
187 unsigned char Value
) {
188 new MCOrgFragment(*Offset
, Value
, getCurrentSectionData());
191 void MCPureStreamer::EmitInstToFragment(const MCInst
&Inst
) {
192 MCInstFragment
*IF
= new MCInstFragment(Inst
, getCurrentSectionData());
194 // Add the fixups and data.
196 // FIXME: Revisit this design decision when relaxation is done, we may be
197 // able to get away with not storing any extra data in the MCInst.
198 SmallVector
<MCFixup
, 4> Fixups
;
199 SmallString
<256> Code
;
200 raw_svector_ostream
VecOS(Code
);
201 getAssembler().getEmitter().EncodeInstruction(Inst
, VecOS
, Fixups
);
204 IF
->getCode() = Code
;
205 IF
->getFixups() = Fixups
;
208 void MCPureStreamer::EmitInstToData(const MCInst
&Inst
) {
209 MCDataFragment
*DF
= getOrCreateDataFragment();
211 SmallVector
<MCFixup
, 4> Fixups
;
212 SmallString
<256> Code
;
213 raw_svector_ostream
VecOS(Code
);
214 getAssembler().getEmitter().EncodeInstruction(Inst
, VecOS
, Fixups
);
217 // Add the fixups and data.
218 for (unsigned i
= 0, e
= Fixups
.size(); i
!= e
; ++i
) {
219 Fixups
[i
].setOffset(Fixups
[i
].getOffset() + DF
->getContents().size());
220 DF
->addFixup(Fixups
[i
]);
222 DF
->getContents().append(Code
.begin(), Code
.end());
225 void MCPureStreamer::Finish() {
226 // FIXME: Handle DWARF tables?
228 this->MCObjectStreamer::Finish();
231 MCStreamer
*llvm::createPureStreamer(MCContext
&Context
, TargetAsmBackend
&TAB
,
232 raw_ostream
&OS
, MCCodeEmitter
*CE
) {
233 return new MCPureStreamer(Context
, TAB
, OS
, CE
);