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() {
35 delete &Assembler->getBackend();
36 delete &Assembler->getEmitter();
37 delete &Assembler->getWriter();
41 MCFragment *MCObjectStreamer::getCurrentFragment() const {
42 assert(getCurrentSectionData() && "No current section!");
44 if (!getCurrentSectionData()->empty())
45 return &getCurrentSectionData()->getFragmentList().back();
50 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
51 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
53 F = new MCDataFragment(getCurrentSectionData());
57 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
58 switch (Value->getKind()) {
59 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
60 case MCExpr::Constant:
63 case MCExpr::Binary: {
64 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
65 AddValueSymbols(BE->getLHS());
66 AddValueSymbols(BE->getRHS());
70 case MCExpr::SymbolRef:
71 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
75 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
82 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
83 bool isPCRel, unsigned AddrSpace) {
84 assert(AddrSpace == 0 && "Address space must be 0!");
85 MCDataFragment *DF = getOrCreateDataFragment();
87 // Avoid fixups when possible.
89 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
90 EmitIntValue(AbsValue, Size, AddrSpace);
93 DF->addFixup(MCFixup::Create(DF->getContents().size(),
95 MCFixup::getKindForSize(Size, isPCRel)));
96 DF->getContents().resize(DF->getContents().size() + Size, 0);
99 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
100 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
101 assert(CurSection && "Cannot emit before setting section!");
103 Symbol->setSection(*CurSection);
105 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
107 // FIXME: This is wasteful, we don't necessarily need to create a data
108 // fragment. Instead, we should mark the symbol as pointing into the data
109 // fragment if it exists, otherwise we should just queue the label and set its
110 // fragment pointer when we emit the next fragment.
111 MCDataFragment *F = getOrCreateDataFragment();
112 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
114 SD.setOffset(F->getContents().size());
117 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
118 unsigned AddrSpace) {
120 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
121 EmitULEB128IntValue(IntValue, AddrSpace);
124 new MCLEBFragment(*Value, false, getCurrentSectionData());
127 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
128 unsigned AddrSpace) {
130 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
131 EmitSLEB128IntValue(IntValue, AddrSpace);
134 new MCLEBFragment(*Value, true, getCurrentSectionData());
137 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
138 const MCSymbol *Symbol) {
139 report_fatal_error("This file format doesn't support weak aliases.");
142 void MCObjectStreamer::SwitchSection(const MCSection *Section) {
143 assert(Section && "Cannot switch to a null section!");
145 // If already in this section, then this is a noop.
146 if (Section == CurSection) return;
148 PrevSection = CurSection;
149 CurSection = Section;
150 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
153 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
155 for (unsigned i = Inst.getNumOperands(); i--; )
156 if (Inst.getOperand(i).isExpr())
157 AddValueSymbols(Inst.getOperand(i).getExpr());
159 getCurrentSectionData()->setHasInstructions(true);
161 // Now that a machine instruction has been assembled into this section, make
162 // a line entry for any .loc directive that has been seen.
163 MCLineEntry::Make(this, getCurrentSection());
165 // If this instruction doesn't need relaxation, just emit it as data.
166 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
167 EmitInstToData(Inst);
171 // Otherwise, if we are relaxing everything, relax the instruction as much as
172 // possible and emit it as data.
173 if (getAssembler().getRelaxAll()) {
175 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
176 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
177 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
178 EmitInstToData(Relaxed);
182 // Otherwise emit to a separate fragment.
183 EmitInstToFragment(Inst);
186 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
187 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
189 raw_svector_ostream VecOS(IF->getCode());
190 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
193 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
194 const MCSymbol *LastLabel,
195 const MCSymbol *Label) {
197 int PointerSize = getContext().getTargetAsmInfo().getPointerSize();
198 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
201 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
202 const MCExpr *LabelRef =
203 MCSymbolRefExpr::Create(Label, Variant, getContext());
204 const MCExpr *LastLabelRef =
205 MCSymbolRefExpr::Create(LastLabel, Variant, getContext());
206 const MCExpr *AddrDelta =
207 MCBinaryExpr::Create(MCBinaryExpr::Sub, LabelRef, LastLabelRef,
210 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
211 MCDwarfLineAddr::Emit(this, LineDelta, Res);
214 if (!getContext().getAsmInfo().hasAggressiveSymbolFolding()) {
215 MCSymbol *ABS = getContext().CreateTempSymbol();
216 EmitAssignment(ABS, AddrDelta);
217 AddrDelta = MCSymbolRefExpr::Create(ABS, getContext());
219 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
222 void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
223 unsigned char Value) {
224 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
227 void MCObjectStreamer::Finish() {
228 // Dump out the dwarf file & directory tables and line tables.
229 if (getContext().hasDwarfFiles())
230 MCDwarfFileTable::Emit(this);
232 getAssembler().Finish();