1 //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
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/MCAsmInfo.h"
11 #include "llvm/MC/MCObjectStreamer.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Target/TargetAsmBackend.h"
21 #include "llvm/Target/TargetAsmInfo.h"
24 MCObjectStreamer::MCObjectStreamer(MCContext
&Context
, TargetAsmBackend
&TAB
,
25 raw_ostream
&OS
, MCCodeEmitter
*Emitter_
)
26 : MCStreamer(Context
),
27 Assembler(new MCAssembler(Context
, TAB
,
28 *Emitter_
, *TAB
.createObjectWriter(OS
),
34 MCObjectStreamer::MCObjectStreamer(MCContext
&Context
, TargetAsmBackend
&TAB
,
35 raw_ostream
&OS
, MCCodeEmitter
*Emitter_
,
36 MCAssembler
*_Assembler
)
37 : MCStreamer(Context
), Assembler(_Assembler
), CurSectionData(0)
41 MCObjectStreamer::~MCObjectStreamer() {
42 delete &Assembler
->getBackend();
43 delete &Assembler
->getEmitter();
44 delete &Assembler
->getWriter();
48 MCFragment
*MCObjectStreamer::getCurrentFragment() const {
49 assert(getCurrentSectionData() && "No current section!");
51 if (!getCurrentSectionData()->empty())
52 return &getCurrentSectionData()->getFragmentList().back();
57 MCDataFragment
*MCObjectStreamer::getOrCreateDataFragment() const {
58 MCDataFragment
*F
= dyn_cast_or_null
<MCDataFragment
>(getCurrentFragment());
60 F
= new MCDataFragment(getCurrentSectionData());
64 const MCExpr
*MCObjectStreamer::AddValueSymbols(const MCExpr
*Value
) {
65 switch (Value
->getKind()) {
67 cast
<MCTargetExpr
>(Value
)->AddValueSymbols(Assembler
);
70 case MCExpr::Constant
:
73 case MCExpr::Binary
: {
74 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(Value
);
75 AddValueSymbols(BE
->getLHS());
76 AddValueSymbols(BE
->getRHS());
80 case MCExpr::SymbolRef
:
81 Assembler
->getOrCreateSymbolData(cast
<MCSymbolRefExpr
>(Value
)->getSymbol());
85 AddValueSymbols(cast
<MCUnaryExpr
>(Value
)->getSubExpr());
92 void MCObjectStreamer::EmitValueImpl(const MCExpr
*Value
, unsigned Size
,
94 assert(AddrSpace
== 0 && "Address space must be 0!");
95 MCDataFragment
*DF
= getOrCreateDataFragment();
97 // Avoid fixups when possible.
99 if (AddValueSymbols(Value
)->EvaluateAsAbsolute(AbsValue
, getAssembler())) {
100 EmitIntValue(AbsValue
, Size
, AddrSpace
);
103 DF
->addFixup(MCFixup::Create(DF
->getContents().size(),
105 MCFixup::getKindForSize(Size
, false)));
106 DF
->getContents().resize(DF
->getContents().size() + Size
, 0);
109 void MCObjectStreamer::EmitLabel(MCSymbol
*Symbol
) {
110 MCStreamer::EmitLabel(Symbol
);
112 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
114 // FIXME: This is wasteful, we don't necessarily need to create a data
115 // fragment. Instead, we should mark the symbol as pointing into the data
116 // fragment if it exists, otherwise we should just queue the label and set its
117 // fragment pointer when we emit the next fragment.
118 MCDataFragment
*F
= getOrCreateDataFragment();
119 assert(!SD
.getFragment() && "Unexpected fragment on symbol data!");
121 SD
.setOffset(F
->getContents().size());
124 void MCObjectStreamer::EmitULEB128Value(const MCExpr
*Value
) {
126 if (Value
->EvaluateAsAbsolute(IntValue
, getAssembler())) {
127 EmitULEB128IntValue(IntValue
);
130 Value
= ForceExpAbs(Value
);
131 new MCLEBFragment(*Value
, false, getCurrentSectionData());
134 void MCObjectStreamer::EmitSLEB128Value(const MCExpr
*Value
) {
136 if (Value
->EvaluateAsAbsolute(IntValue
, getAssembler())) {
137 EmitSLEB128IntValue(IntValue
);
140 Value
= ForceExpAbs(Value
);
141 new MCLEBFragment(*Value
, true, getCurrentSectionData());
144 void MCObjectStreamer::EmitWeakReference(MCSymbol
*Alias
,
145 const MCSymbol
*Symbol
) {
146 report_fatal_error("This file format doesn't support weak aliases.");
149 void MCObjectStreamer::ChangeSection(const MCSection
*Section
) {
150 assert(Section
&& "Cannot switch to a null section!");
152 CurSectionData
= &getAssembler().getOrCreateSectionData(*Section
);
155 void MCObjectStreamer::EmitInstruction(const MCInst
&Inst
) {
157 for (unsigned i
= Inst
.getNumOperands(); i
--; )
158 if (Inst
.getOperand(i
).isExpr())
159 AddValueSymbols(Inst
.getOperand(i
).getExpr());
161 getCurrentSectionData()->setHasInstructions(true);
163 // Now that a machine instruction has been assembled into this section, make
164 // a line entry for any .loc directive that has been seen.
165 MCLineEntry::Make(this, getCurrentSection());
167 // If this instruction doesn't need relaxation, just emit it as data.
168 if (!getAssembler().getBackend().MayNeedRelaxation(Inst
)) {
169 EmitInstToData(Inst
);
173 // Otherwise, if we are relaxing everything, relax the instruction as much as
174 // possible and emit it as data.
175 if (getAssembler().getRelaxAll()) {
177 getAssembler().getBackend().RelaxInstruction(Inst
, Relaxed
);
178 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed
))
179 getAssembler().getBackend().RelaxInstruction(Relaxed
, Relaxed
);
180 EmitInstToData(Relaxed
);
184 // Otherwise emit to a separate fragment.
185 EmitInstToFragment(Inst
);
188 void MCObjectStreamer::EmitInstToFragment(const MCInst
&Inst
) {
189 MCInstFragment
*IF
= new MCInstFragment(Inst
, getCurrentSectionData());
191 SmallString
<128> Code
;
192 raw_svector_ostream
VecOS(Code
);
193 getAssembler().getEmitter().EncodeInstruction(Inst
, VecOS
, IF
->getFixups());
195 IF
->getCode().append(Code
.begin(), Code
.end());
198 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta
,
199 const MCSymbol
*LastLabel
,
200 const MCSymbol
*Label
) {
202 int PointerSize
= getContext().getTargetAsmInfo().getPointerSize();
203 EmitDwarfSetLineAddr(LineDelta
, Label
, PointerSize
);
206 const MCExpr
*AddrDelta
= BuildSymbolDiff(getContext(), Label
, LastLabel
);
208 if (AddrDelta
->EvaluateAsAbsolute(Res
, getAssembler())) {
209 MCDwarfLineAddr::Emit(this, LineDelta
, Res
);
212 AddrDelta
= ForceExpAbs(AddrDelta
);
213 new MCDwarfLineAddrFragment(LineDelta
, *AddrDelta
, getCurrentSectionData());
216 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol
*LastLabel
,
217 const MCSymbol
*Label
) {
218 const MCExpr
*AddrDelta
= BuildSymbolDiff(getContext(), Label
, LastLabel
);
220 if (AddrDelta
->EvaluateAsAbsolute(Res
, getAssembler())) {
221 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res
);
224 AddrDelta
= ForceExpAbs(AddrDelta
);
225 new MCDwarfCallFrameFragment(*AddrDelta
, getCurrentSectionData());
228 void MCObjectStreamer::EmitValueToOffset(const MCExpr
*Offset
,
229 unsigned char Value
) {
231 if (Offset
->EvaluateAsAbsolute(Res
, getAssembler())) {
232 new MCOrgFragment(*Offset
, Value
, getCurrentSectionData());
236 MCSymbol
*CurrentPos
= getContext().CreateTempSymbol();
237 EmitLabel(CurrentPos
);
238 MCSymbolRefExpr::VariantKind Variant
= MCSymbolRefExpr::VK_None
;
240 MCSymbolRefExpr::Create(CurrentPos
, Variant
, getContext());
241 const MCExpr
*Delta
=
242 MCBinaryExpr::Create(MCBinaryExpr::Sub
, Offset
, Ref
, getContext());
244 if (!Delta
->EvaluateAsAbsolute(Res
, getAssembler()))
245 report_fatal_error("expected assembly-time absolute expression");
246 EmitFill(Res
, Value
, 0);
249 void MCObjectStreamer::Finish() {
250 // Dump out the dwarf file & directory tables and line tables.
251 if (getContext().hasDwarfFiles())
252 MCDwarfFileTable::Emit(this);
254 getAssembler().Finish();