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"
23 #include "llvm/MC/MCSectionELF.h"
26 MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
27 raw_ostream &OS, MCCodeEmitter *Emitter_)
28 : MCStreamer(Context),
29 Assembler(new MCAssembler(Context, TAB, *Emitter_,
30 *TAB.createObjectWriter(OS), OS)),
33 MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
34 raw_ostream &OS, MCCodeEmitter *Emitter_,
35 MCAssembler *_Assembler)
36 : MCStreamer(Context), Assembler(_Assembler), CurSectionData(0) {}
38 MCObjectStreamer::~MCObjectStreamer() {
39 delete &Assembler->getBackend();
40 delete &Assembler->getEmitter();
41 delete &Assembler->getWriter();
45 void MCObjectStreamer::reset() {
49 CurInsertionPoint = MCSectionData::iterator();
53 MCFragment *MCObjectStreamer::getCurrentFragment() const {
54 assert(getCurrentSectionData() && "No current section!");
56 if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin())
57 return std::prev(CurInsertionPoint);
62 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
63 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
64 // When bundling is enabled, we don't want to add data to a fragment that
65 // already has instructions (see MCELFStreamer::EmitInstToData for details)
66 if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions())) {
67 const auto *Sec = dyn_cast<MCSectionELF>(&getCurrentSectionData()->getSection());
68 if (Sec && Sec->getSectionName().startswith(".zdebug_"))
69 F = new MCCompressedFragment();
71 F = new MCDataFragment();
77 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
78 switch (Value->getKind()) {
80 cast<MCTargetExpr>(Value)->AddValueSymbols(Assembler);
83 case MCExpr::Constant:
86 case MCExpr::Binary: {
87 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
88 AddValueSymbols(BE->getLHS());
89 AddValueSymbols(BE->getRHS());
93 case MCExpr::SymbolRef:
94 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
98 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
105 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
106 MCDataFragment *DF = getOrCreateDataFragment();
108 MCLineEntry::Make(this, getCurrentSection().first);
110 // Avoid fixups when possible.
112 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
113 EmitIntValue(AbsValue, Size);
116 DF->getFixups().push_back(
117 MCFixup::Create(DF->getContents().size(), Value,
118 MCFixup::getKindForSize(Size, false)));
119 DF->getContents().resize(DF->getContents().size() + Size, 0);
122 void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
123 RecordProcStart(Frame);
126 void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
127 RecordProcEnd(Frame);
130 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
131 MCStreamer::EmitLabel(Symbol);
133 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
135 // FIXME: This is wasteful, we don't necessarily need to create a data
136 // fragment. Instead, we should mark the symbol as pointing into the data
137 // fragment if it exists, otherwise we should just queue the label and set its
138 // fragment pointer when we emit the next fragment.
139 MCDataFragment *F = getOrCreateDataFragment();
140 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
142 SD.setOffset(F->getContents().size());
145 void MCObjectStreamer::EmitDebugLabel(MCSymbol *Symbol) {
149 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
151 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
152 EmitULEB128IntValue(IntValue);
155 Value = ForceExpAbs(Value);
156 insert(new MCLEBFragment(*Value, false));
159 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
161 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
162 EmitSLEB128IntValue(IntValue);
165 Value = ForceExpAbs(Value);
166 insert(new MCLEBFragment(*Value, true));
169 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
170 const MCSymbol *Symbol) {
171 report_fatal_error("This file format doesn't support weak aliases.");
174 void MCObjectStreamer::ChangeSection(const MCSection *Section,
175 const MCExpr *Subsection) {
176 assert(Section && "Cannot switch to a null section!");
178 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
180 int64_t IntSubsection = 0;
182 !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler()))
183 report_fatal_error("Cannot evaluate subsection number");
184 if (IntSubsection < 0 || IntSubsection > 8192)
185 report_fatal_error("Subsection number out of range");
187 CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection));
190 void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
191 getAssembler().getOrCreateSymbolData(*Symbol);
192 AddValueSymbols(Value);
193 MCStreamer::EmitAssignment(Symbol, Value);
196 void MCObjectStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
198 for (unsigned i = Inst.getNumOperands(); i--; )
199 if (Inst.getOperand(i).isExpr())
200 AddValueSymbols(Inst.getOperand(i).getExpr());
202 MCSectionData *SD = getCurrentSectionData();
203 SD->setHasInstructions(true);
205 // Now that a machine instruction has been assembled into this section, make
206 // a line entry for any .loc directive that has been seen.
207 MCLineEntry::Make(this, getCurrentSection().first);
209 // If this instruction doesn't need relaxation, just emit it as data.
210 MCAssembler &Assembler = getAssembler();
211 if (!Assembler.getBackend().mayNeedRelaxation(Inst)) {
212 EmitInstToData(Inst, STI);
216 // Otherwise, relax and emit it as data if either:
217 // - The RelaxAll flag was passed
218 // - Bundling is enabled and this instruction is inside a bundle-locked
219 // group. We want to emit all such instructions into the same data
221 if (Assembler.getRelaxAll() ||
222 (Assembler.isBundlingEnabled() && SD->isBundleLocked())) {
224 getAssembler().getBackend().relaxInstruction(Inst, Relaxed);
225 while (getAssembler().getBackend().mayNeedRelaxation(Relaxed))
226 getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed);
227 EmitInstToData(Relaxed, STI);
231 // Otherwise emit to a separate fragment.
232 EmitInstToFragment(Inst, STI);
235 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst,
236 const MCSubtargetInfo &STI) {
237 // Always create a new, separate fragment here, because its size can change
238 // during relaxation.
239 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
242 SmallString<128> Code;
243 raw_svector_ostream VecOS(Code);
244 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups(),
247 IF->getContents().append(Code.begin(), Code.end());
251 static const char *const BundlingNotImplementedMsg =
252 "Aligned bundling is not implemented for this object format";
255 void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
256 llvm_unreachable(BundlingNotImplementedMsg);
259 void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
260 llvm_unreachable(BundlingNotImplementedMsg);
263 void MCObjectStreamer::EmitBundleUnlock() {
264 llvm_unreachable(BundlingNotImplementedMsg);
267 void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
268 unsigned Column, unsigned Flags,
270 unsigned Discriminator,
271 StringRef FileName) {
272 // In case we see two .loc directives in a row, make sure the
273 // first one gets a line entry.
274 MCLineEntry::Make(this, getCurrentSection().first);
276 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
277 Isa, Discriminator, FileName);
280 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
281 const MCSymbol *LastLabel,
282 const MCSymbol *Label,
283 unsigned PointerSize) {
285 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
288 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
290 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
291 MCDwarfLineAddr::Emit(this, LineDelta, Res);
294 AddrDelta = ForceExpAbs(AddrDelta);
295 insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
298 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
299 const MCSymbol *Label) {
300 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
302 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
303 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
306 AddrDelta = ForceExpAbs(AddrDelta);
307 insert(new MCDwarfCallFrameFragment(*AddrDelta));
310 void MCObjectStreamer::EmitBytes(StringRef Data) {
311 MCLineEntry::Make(this, getCurrentSection().first);
312 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
315 void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
318 unsigned MaxBytesToEmit) {
319 if (MaxBytesToEmit == 0)
320 MaxBytesToEmit = ByteAlignment;
321 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
323 // Update the maximum alignment on the current section if necessary.
324 if (ByteAlignment > getCurrentSectionData()->getAlignment())
325 getCurrentSectionData()->setAlignment(ByteAlignment);
328 void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
329 unsigned MaxBytesToEmit) {
330 EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
331 cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
334 bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
335 unsigned char Value) {
337 if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
338 insert(new MCOrgFragment(*Offset, Value));
342 MCSymbol *CurrentPos = getContext().CreateTempSymbol();
343 EmitLabel(CurrentPos);
344 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
346 MCSymbolRefExpr::Create(CurrentPos, Variant, getContext());
347 const MCExpr *Delta =
348 MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext());
350 if (!Delta->EvaluateAsAbsolute(Res, getAssembler()))
352 EmitFill(Res, Value);
356 // Associate GPRel32 fixup with data and resize data area
357 void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
358 MCDataFragment *DF = getOrCreateDataFragment();
360 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
362 DF->getContents().resize(DF->getContents().size() + 4, 0);
365 // Associate GPRel32 fixup with data and resize data area
366 void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
367 MCDataFragment *DF = getOrCreateDataFragment();
369 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
371 DF->getContents().resize(DF->getContents().size() + 8, 0);
374 void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
375 // FIXME: A MCFillFragment would be more memory efficient but MCExpr has
376 // problems evaluating expressions across multiple fragments.
377 getOrCreateDataFragment()->getContents().append(NumBytes, FillValue);
380 void MCObjectStreamer::EmitZeros(uint64_t NumBytes) {
381 unsigned ItemSize = getCurrentSection().first->isVirtualSection() ? 0 : 1;
382 insert(new MCFillFragment(0, ItemSize, NumBytes));
385 void MCObjectStreamer::FinishImpl() {
386 // Dump out the dwarf file & directory tables and line tables.
387 const MCSymbol *LineSectionSymbol = NULL;
388 if (getContext().hasMCLineSections())
389 LineSectionSymbol = MCDwarfLineTable::Emit(this);
391 // If we are generating dwarf for assembly source files dump out the sections.
392 if (getContext().getGenDwarfForAssembly())
393 MCGenDwarfInfo::Emit(this, LineSectionSymbol);
395 getAssembler().Finish();