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"
12 #include "llvm/Support/ErrorHandling.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/MCSymbol.h"
19 #include "llvm/Target/TargetAsmBackend.h"
20 #include "llvm/Target/TargetAsmInfo.h"
23 MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
24 raw_ostream &OS, MCCodeEmitter *Emitter_)
25 : MCStreamer(Context),
26 Assembler(new MCAssembler(Context, TAB,
27 *Emitter_, *TAB.createObjectWriter(OS),
33 MCObjectStreamer::~MCObjectStreamer() {
34 delete &Assembler->getBackend();
35 delete &Assembler->getEmitter();
36 delete &Assembler->getWriter();
40 MCFragment *MCObjectStreamer::getCurrentFragment() const {
41 assert(getCurrentSectionData() && "No current section!");
43 if (!getCurrentSectionData()->empty())
44 return &getCurrentSectionData()->getFragmentList().back();
49 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
50 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
52 F = new MCDataFragment(getCurrentSectionData());
56 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
57 switch (Value->getKind()) {
58 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
59 case MCExpr::Constant:
62 case MCExpr::Binary: {
63 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
64 AddValueSymbols(BE->getLHS());
65 AddValueSymbols(BE->getRHS());
69 case MCExpr::SymbolRef:
70 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
74 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
81 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
82 bool isPCRel, unsigned AddrSpace) {
83 assert(AddrSpace == 0 && "Address space must be 0!");
84 MCDataFragment *DF = getOrCreateDataFragment();
86 // Avoid fixups when possible.
88 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
89 EmitIntValue(AbsValue, Size, AddrSpace);
92 DF->addFixup(MCFixup::Create(DF->getContents().size(),
93 AddValueSymbols(Value),
94 MCFixup::getKindForSize(Size, isPCRel)));
95 DF->getContents().resize(DF->getContents().size() + Size, 0);
98 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
99 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
100 assert(CurSection && "Cannot emit before setting section!");
102 Symbol->setSection(*CurSection);
104 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
106 // FIXME: This is wasteful, we don't necessarily need to create a data
107 // fragment. Instead, we should mark the symbol as pointing into the data
108 // fragment if it exists, otherwise we should just queue the label and set its
109 // fragment pointer when we emit the next fragment.
110 MCDataFragment *F = getOrCreateDataFragment();
111 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
113 SD.setOffset(F->getContents().size());
116 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
117 unsigned AddrSpace) {
119 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
120 EmitULEB128IntValue(IntValue, AddrSpace);
123 new MCLEBFragment(*Value, false, getCurrentSectionData());
126 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
127 unsigned AddrSpace) {
129 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
130 EmitSLEB128IntValue(IntValue, AddrSpace);
133 new MCLEBFragment(*Value, true, getCurrentSectionData());
136 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
137 const MCSymbol *Symbol) {
138 report_fatal_error("This file format doesn't support weak aliases.");
141 void MCObjectStreamer::SwitchSection(const MCSection *Section) {
142 assert(Section && "Cannot switch to a null section!");
144 // If already in this section, then this is a noop.
145 if (Section == CurSection) return;
147 PrevSection = CurSection;
148 CurSection = Section;
149 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
152 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
154 for (unsigned i = Inst.getNumOperands(); i--; )
155 if (Inst.getOperand(i).isExpr())
156 AddValueSymbols(Inst.getOperand(i).getExpr());
158 getCurrentSectionData()->setHasInstructions(true);
160 // Now that a machine instruction has been assembled into this section, make
161 // a line entry for any .loc directive that has been seen.
162 MCLineEntry::Make(this, getCurrentSection());
164 // If this instruction doesn't need relaxation, just emit it as data.
165 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
166 EmitInstToData(Inst);
170 // Otherwise, if we are relaxing everything, relax the instruction as much as
171 // possible and emit it as data.
172 if (getAssembler().getRelaxAll()) {
174 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
175 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
176 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
177 EmitInstToData(Relaxed);
181 // Otherwise emit to a separate fragment.
182 EmitInstToFragment(Inst);
185 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
186 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
188 raw_svector_ostream VecOS(IF->getCode());
189 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
192 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
193 const MCSymbol *LastLabel,
194 const MCSymbol *Label) {
196 int PointerSize = getContext().getTargetAsmInfo().getPointerSize();
197 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
200 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
201 const MCExpr *LabelRef =
202 MCSymbolRefExpr::Create(Label, Variant, getContext());
203 const MCExpr *LastLabelRef =
204 MCSymbolRefExpr::Create(LastLabel, Variant, getContext());
205 const MCExpr *AddrDelta =
206 MCBinaryExpr::Create(MCBinaryExpr::Sub, LabelRef, LastLabelRef,
209 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
210 MCDwarfLineAddr::Emit(this, LineDelta, Res);
213 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
216 void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
217 unsigned char Value) {
218 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
221 void MCObjectStreamer::Finish() {
222 // Dump out the dwarf file & directory tables and line tables.
223 if (getContext().hasDwarfFiles())
224 MCDwarfFileTable::Emit(this);
226 getAssembler().Finish();