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/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCDwarf.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/ErrorHandling.h"
23 MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
24 raw_ostream &OS, MCCodeEmitter *Emitter_)
25 : MCStreamer(Context),
26 Assembler(new MCAssembler(Context, TAB,
27 *Emitter_, *TAB.createObjectWriter(OS),
33 MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
34 raw_ostream &OS, MCCodeEmitter *Emitter_,
35 MCAssembler *_Assembler)
36 : MCStreamer(Context), Assembler(_Assembler), CurSectionData(0)
40 MCObjectStreamer::~MCObjectStreamer() {
41 delete &Assembler->getBackend();
42 delete &Assembler->getEmitter();
43 delete &Assembler->getWriter();
47 void MCObjectStreamer::reset() {
54 MCFragment *MCObjectStreamer::getCurrentFragment() const {
55 assert(getCurrentSectionData() && "No current section!");
57 if (!getCurrentSectionData()->empty())
58 return &getCurrentSectionData()->getFragmentList().back();
63 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
64 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
66 F = new MCDataFragment(getCurrentSectionData());
70 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
71 switch (Value->getKind()) {
73 cast<MCTargetExpr>(Value)->AddValueSymbols(Assembler);
76 case MCExpr::Constant:
79 case MCExpr::Binary: {
80 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
81 AddValueSymbols(BE->getLHS());
82 AddValueSymbols(BE->getRHS());
86 case MCExpr::SymbolRef:
87 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
91 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
98 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
100 assert(AddrSpace == 0 && "Address space must be 0!");
101 MCDataFragment *DF = getOrCreateDataFragment();
103 // Avoid fixups when possible.
105 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
106 EmitIntValue(AbsValue, Size, AddrSpace);
109 DF->getFixups().push_back(
110 MCFixup::Create(DF->getContents().size(), Value,
111 MCFixup::getKindForSize(Size, false)));
112 DF->getContents().resize(DF->getContents().size() + Size, 0);
115 void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
116 RecordProcStart(Frame);
119 void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
120 RecordProcEnd(Frame);
123 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
124 MCStreamer::EmitLabel(Symbol);
126 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
128 // FIXME: This is wasteful, we don't necessarily need to create a data
129 // fragment. Instead, we should mark the symbol as pointing into the data
130 // fragment if it exists, otherwise we should just queue the label and set its
131 // fragment pointer when we emit the next fragment.
132 MCDataFragment *F = getOrCreateDataFragment();
133 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
135 SD.setOffset(F->getContents().size());
138 void MCObjectStreamer::EmitDebugLabel(MCSymbol *Symbol) {
142 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
144 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
145 EmitULEB128IntValue(IntValue);
148 Value = ForceExpAbs(Value);
149 new MCLEBFragment(*Value, false, getCurrentSectionData());
152 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
154 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
155 EmitSLEB128IntValue(IntValue);
158 Value = ForceExpAbs(Value);
159 new MCLEBFragment(*Value, true, getCurrentSectionData());
162 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
163 const MCSymbol *Symbol) {
164 report_fatal_error("This file format doesn't support weak aliases.");
167 void MCObjectStreamer::ChangeSection(const MCSection *Section) {
168 assert(Section && "Cannot switch to a null section!");
170 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
173 void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
174 getAssembler().getOrCreateSymbolData(*Symbol);
175 Symbol->setVariableValue(AddValueSymbols(Value));
178 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
180 for (unsigned i = Inst.getNumOperands(); i--; )
181 if (Inst.getOperand(i).isExpr())
182 AddValueSymbols(Inst.getOperand(i).getExpr());
184 MCSectionData *SD = getCurrentSectionData();
185 SD->setHasInstructions(true);
187 // Now that a machine instruction has been assembled into this section, make
188 // a line entry for any .loc directive that has been seen.
189 MCLineEntry::Make(this, getCurrentSection());
191 // If this instruction doesn't need relaxation, just emit it as data.
192 MCAssembler &Assembler = getAssembler();
193 if (!Assembler.getBackend().mayNeedRelaxation(Inst)) {
194 EmitInstToData(Inst);
198 // Otherwise, relax and emit it as data if either:
199 // - The RelaxAll flag was passed
200 // - Bundling is enabled and this instruction is inside a bundle-locked
201 // group. We want to emit all such instructions into the same data
203 if (Assembler.getRelaxAll() ||
204 (Assembler.isBundlingEnabled() && SD->isBundleLocked())) {
206 getAssembler().getBackend().relaxInstruction(Inst, Relaxed);
207 while (getAssembler().getBackend().mayNeedRelaxation(Relaxed))
208 getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed);
209 EmitInstToData(Relaxed);
213 // Otherwise emit to a separate fragment.
214 EmitInstToFragment(Inst);
217 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
218 // Always create a new, separate fragment here, because its size can change
219 // during relaxation.
220 MCRelaxableFragment *IF =
221 new MCRelaxableFragment(Inst, getCurrentSectionData());
223 SmallString<128> Code;
224 raw_svector_ostream VecOS(Code);
225 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
227 IF->getContents().append(Code.begin(), Code.end());
230 const char *BundlingNotImplementedMsg =
231 "Aligned bundling is not implemented for this object format";
233 void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
234 llvm_unreachable(BundlingNotImplementedMsg);
237 void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
238 llvm_unreachable(BundlingNotImplementedMsg);
241 void MCObjectStreamer::EmitBundleUnlock() {
242 llvm_unreachable(BundlingNotImplementedMsg);
245 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
246 const MCSymbol *LastLabel,
247 const MCSymbol *Label,
248 unsigned PointerSize) {
250 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
253 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
255 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
256 MCDwarfLineAddr::Emit(this, LineDelta, Res);
259 AddrDelta = ForceExpAbs(AddrDelta);
260 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
263 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
264 const MCSymbol *Label) {
265 const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
267 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
268 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
271 AddrDelta = ForceExpAbs(AddrDelta);
272 new MCDwarfCallFrameFragment(*AddrDelta, getCurrentSectionData());
275 void MCObjectStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
276 assert(AddrSpace == 0 && "Address space must be 0!");
277 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
280 void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
283 unsigned MaxBytesToEmit) {
284 if (MaxBytesToEmit == 0)
285 MaxBytesToEmit = ByteAlignment;
286 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
287 getCurrentSectionData());
289 // Update the maximum alignment on the current section if necessary.
290 if (ByteAlignment > getCurrentSectionData()->getAlignment())
291 getCurrentSectionData()->setAlignment(ByteAlignment);
294 void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
295 unsigned MaxBytesToEmit) {
296 EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
297 cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
300 bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
301 unsigned char Value) {
303 if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
304 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
308 MCSymbol *CurrentPos = getContext().CreateTempSymbol();
309 EmitLabel(CurrentPos);
310 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
312 MCSymbolRefExpr::Create(CurrentPos, Variant, getContext());
313 const MCExpr *Delta =
314 MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext());
316 if (!Delta->EvaluateAsAbsolute(Res, getAssembler()))
318 EmitFill(Res, Value);
322 // Associate GPRel32 fixup with data and resize data area
323 void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
324 MCDataFragment *DF = getOrCreateDataFragment();
326 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
328 DF->getContents().resize(DF->getContents().size() + 4, 0);
331 // Associate GPRel32 fixup with data and resize data area
332 void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
333 MCDataFragment *DF = getOrCreateDataFragment();
335 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
337 DF->getContents().resize(DF->getContents().size() + 8, 0);
340 void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
341 unsigned AddrSpace) {
342 assert(AddrSpace == 0 && "Address space must be 0!");
343 // FIXME: A MCFillFragment would be more memory efficient but MCExpr has
344 // problems evaluating expressions across multiple fragments.
345 getOrCreateDataFragment()->getContents().append(NumBytes, FillValue);
348 void MCObjectStreamer::FinishImpl() {
349 // Dump out the dwarf file & directory tables and line tables.
350 const MCSymbol *LineSectionSymbol = NULL;
351 if (getContext().hasDwarfFiles())
352 LineSectionSymbol = MCDwarfFileTable::Emit(this);
354 // If we are generating dwarf for assembly source files dump out the sections.
355 if (getContext().getGenDwarfForAssembly())
356 MCGenDwarfInfo::Emit(this, LineSectionSymbol);
358 getAssembler().Finish();