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/MCObjectStreamer.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAsmInfo.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/MCObjectWriter.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/ErrorHandling.h"
25 MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
26 raw_ostream &OS, MCCodeEmitter *Emitter_)
27 : MCStreamer(Context),
28 Assembler(new MCAssembler(Context, TAB, *Emitter_,
29 *TAB.createObjectWriter(OS), OS)),
30 CurSectionData(nullptr), EmitEHFrame(true), EmitDebugFrame(false) {}
32 MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
33 raw_ostream &OS, MCCodeEmitter *Emitter_,
34 MCAssembler *_Assembler)
35 : MCStreamer(Context), Assembler(_Assembler), CurSectionData(nullptr),
36 EmitEHFrame(true), EmitDebugFrame(false) {}
38 MCObjectStreamer::~MCObjectStreamer() {
39 delete &Assembler->getBackend();
40 delete &Assembler->getEmitter();
41 delete &Assembler->getWriter();
45 void MCObjectStreamer::reset() {
48 CurSectionData = nullptr;
49 CurInsertionPoint = MCSectionData::iterator();
51 EmitDebugFrame = false;
55 void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) {
56 if (!getNumFrameInfos())
60 MCDwarfFrameEmitter::Emit(*this, MAB, true);
63 MCDwarfFrameEmitter::Emit(*this, MAB, false);
66 MCFragment *MCObjectStreamer::getCurrentFragment() const {
67 assert(getCurrentSectionData() && "No current section!");
69 if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin())
70 return std::prev(CurInsertionPoint);
75 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
76 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
77 // When bundling is enabled, we don't want to add data to a fragment that
78 // already has instructions (see MCELFStreamer::EmitInstToData for details)
79 if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions())) {
80 F = new MCDataFragment();
86 void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) {
87 Assembler->getOrCreateSymbolData(Sym);
90 void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) {
91 MCStreamer::EmitCFISections(EH, Debug);
93 EmitDebugFrame = Debug;
96 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
98 MCStreamer::EmitValueImpl(Value, Size, Loc);
99 MCDataFragment *DF = getOrCreateDataFragment();
101 MCLineEntry::Make(this, getCurrentSection().first);
103 // Avoid fixups when possible.
105 if (Value->EvaluateAsAbsolute(AbsValue, getAssembler())) {
106 EmitIntValue(AbsValue, Size);
109 DF->getFixups().push_back(
110 MCFixup::Create(DF->getContents().size(), Value,
111 MCFixup::getKindForSize(Size, false), Loc));
112 DF->getContents().resize(DF->getContents().size() + Size, 0);
115 void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
116 // We need to create a local symbol to avoid relocations.
117 Frame.Begin = getContext().CreateTempSymbol();
118 EmitLabel(Frame.Begin);
121 void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
122 Frame.End = getContext().CreateTempSymbol();
123 EmitLabel(Frame.End);
126 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
127 MCStreamer::EmitLabel(Symbol);
129 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
131 // FIXME: This is wasteful, we don't necessarily need to create a data
132 // fragment. Instead, we should mark the symbol as pointing into the data
133 // fragment if it exists, otherwise we should just queue the label and set its
134 // fragment pointer when we emit the next fragment.
135 MCDataFragment *F = getOrCreateDataFragment();
136 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
138 SD.setOffset(F->getContents().size());
141 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
143 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
144 EmitULEB128IntValue(IntValue);
147 Value = ForceExpAbs(Value);
148 insert(new MCLEBFragment(*Value, false));
151 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
153 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
154 EmitSLEB128IntValue(IntValue);
157 Value = ForceExpAbs(Value);
158 insert(new MCLEBFragment(*Value, true));
161 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
162 const MCSymbol *Symbol) {
163 report_fatal_error("This file format doesn't support weak aliases.");
166 void MCObjectStreamer::ChangeSection(const MCSection *Section,
167 const MCExpr *Subsection) {
168 assert(Section && "Cannot switch to a null section!");
170 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
172 int64_t IntSubsection = 0;
174 !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler()))
175 report_fatal_error("Cannot evaluate subsection number");
176 if (IntSubsection < 0 || IntSubsection > 8192)
177 report_fatal_error("Subsection number out of range");
179 CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection));
182 void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
183 getAssembler().getOrCreateSymbolData(*Symbol);
184 MCStreamer::EmitAssignment(Symbol, Value);
187 void MCObjectStreamer::EmitInstruction(const MCInst &Inst,
188 const MCSubtargetInfo &STI) {
189 MCStreamer::EmitInstruction(Inst, STI);
191 MCSectionData *SD = getCurrentSectionData();
192 SD->setHasInstructions(true);
194 // Now that a machine instruction has been assembled into this section, make
195 // a line entry for any .loc directive that has been seen.
196 MCLineEntry::Make(this, getCurrentSection().first);
198 // If this instruction doesn't need relaxation, just emit it as data.
199 MCAssembler &Assembler = getAssembler();
200 if (!Assembler.getBackend().mayNeedRelaxation(Inst)) {
201 EmitInstToData(Inst, STI);
205 // Otherwise, relax and emit it as data if either:
206 // - The RelaxAll flag was passed
207 // - Bundling is enabled and this instruction is inside a bundle-locked
208 // group. We want to emit all such instructions into the same data
210 if (Assembler.getRelaxAll() ||
211 (Assembler.isBundlingEnabled() && SD->isBundleLocked())) {
213 getAssembler().getBackend().relaxInstruction(Inst, Relaxed);
214 while (getAssembler().getBackend().mayNeedRelaxation(Relaxed))
215 getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed);
216 EmitInstToData(Relaxed, STI);
220 // Otherwise emit to a separate fragment.
221 EmitInstToFragment(Inst, STI);
224 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst,
225 const MCSubtargetInfo &STI) {
226 // Always create a new, separate fragment here, because its size can change
227 // during relaxation.
228 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
231 SmallString<128> Code;
232 raw_svector_ostream VecOS(Code);
233 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups(),
236 IF->getContents().append(Code.begin(), Code.end());
240 static const char *const BundlingNotImplementedMsg =
241 "Aligned bundling is not implemented for this object format";
244 void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
245 llvm_unreachable(BundlingNotImplementedMsg);
248 void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
249 llvm_unreachable(BundlingNotImplementedMsg);
252 void MCObjectStreamer::EmitBundleUnlock() {
253 llvm_unreachable(BundlingNotImplementedMsg);
256 void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
257 unsigned Column, unsigned Flags,
259 unsigned Discriminator,
260 StringRef FileName) {
261 // In case we see two .loc directives in a row, make sure the
262 // first one gets a line entry.
263 MCLineEntry::Make(this, getCurrentSection().first);
265 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
266 Isa, Discriminator, FileName);
269 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
270 const MCSymbol *LastLabel,
271 const MCSymbol *Label,
272 unsigned PointerSize) {
274 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
277 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
279 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
280 MCDwarfLineAddr::Emit(this, LineDelta, Res);
283 AddrDelta = ForceExpAbs(AddrDelta);
284 insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
287 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
288 const MCSymbol *Label) {
289 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
291 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
292 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
295 AddrDelta = ForceExpAbs(AddrDelta);
296 insert(new MCDwarfCallFrameFragment(*AddrDelta));
299 void MCObjectStreamer::EmitBytes(StringRef Data) {
300 MCLineEntry::Make(this, getCurrentSection().first);
301 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
304 void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
307 unsigned MaxBytesToEmit) {
308 if (MaxBytesToEmit == 0)
309 MaxBytesToEmit = ByteAlignment;
310 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
312 // Update the maximum alignment on the current section if necessary.
313 if (ByteAlignment > getCurrentSectionData()->getAlignment())
314 getCurrentSectionData()->setAlignment(ByteAlignment);
317 void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
318 unsigned MaxBytesToEmit) {
319 EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
320 cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
323 bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
324 unsigned char Value) {
326 if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
327 insert(new MCOrgFragment(*Offset, Value));
331 MCSymbol *CurrentPos = getContext().CreateTempSymbol();
332 EmitLabel(CurrentPos);
333 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
335 MCSymbolRefExpr::Create(CurrentPos, Variant, getContext());
336 const MCExpr *Delta =
337 MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext());
339 if (!Delta->EvaluateAsAbsolute(Res, getAssembler()))
341 EmitFill(Res, Value);
345 // Associate GPRel32 fixup with data and resize data area
346 void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
347 MCDataFragment *DF = getOrCreateDataFragment();
349 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
351 DF->getContents().resize(DF->getContents().size() + 4, 0);
354 // Associate GPRel32 fixup with data and resize data area
355 void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
356 MCDataFragment *DF = getOrCreateDataFragment();
358 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
360 DF->getContents().resize(DF->getContents().size() + 8, 0);
363 void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
364 // FIXME: A MCFillFragment would be more memory efficient but MCExpr has
365 // problems evaluating expressions across multiple fragments.
366 getOrCreateDataFragment()->getContents().append(NumBytes, FillValue);
369 void MCObjectStreamer::EmitZeros(uint64_t NumBytes) {
370 unsigned ItemSize = getCurrentSection().first->isVirtualSection() ? 0 : 1;
371 insert(new MCFillFragment(0, ItemSize, NumBytes));
374 void MCObjectStreamer::FinishImpl() {
375 // If we are generating dwarf for assembly source files dump out the sections.
376 if (getContext().getGenDwarfForAssembly())
377 MCGenDwarfInfo::Emit(this);
379 // Dump out the dwarf file & directory tables and line tables.
380 MCDwarfLineTable::Emit(this);
382 getAssembler().Finish();