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/MCDwarf.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Target/TargetAsmBackend.h"
21 MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
22 raw_ostream &_OS, MCCodeEmitter *_Emitter)
23 : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB,
30 MCObjectStreamer::~MCObjectStreamer() {
31 delete &Assembler->getBackend();
32 delete &Assembler->getEmitter();
36 MCFragment *MCObjectStreamer::getCurrentFragment() const {
37 assert(getCurrentSectionData() && "No current section!");
39 if (!getCurrentSectionData()->empty())
40 return &getCurrentSectionData()->getFragmentList().back();
45 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
46 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
48 F = new MCDataFragment(getCurrentSectionData());
52 const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
53 switch (Value->getKind()) {
54 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
55 case MCExpr::Constant:
58 case MCExpr::Binary: {
59 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
60 AddValueSymbols(BE->getLHS());
61 AddValueSymbols(BE->getRHS());
65 case MCExpr::SymbolRef:
66 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
70 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
77 void MCObjectStreamer::EmitValue(const MCExpr *Value, unsigned Size,
79 assert(AddrSpace == 0 && "Address space must be 0!");
80 MCDataFragment *DF = getOrCreateDataFragment();
82 // Avoid fixups when possible.
84 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
85 EmitIntValue(AbsValue, Size, AddrSpace);
88 DF->addFixup(MCFixup::Create(DF->getContents().size(),
89 AddValueSymbols(Value),
90 MCFixup::getKindForSize(Size, false)));
91 DF->getContents().resize(DF->getContents().size() + Size, 0);
94 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
95 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
96 assert(CurSection && "Cannot emit before setting section!");
98 Symbol->setSection(*CurSection);
100 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
102 // FIXME: This is wasteful, we don't necessarily need to create a data
103 // fragment. Instead, we should mark the symbol as pointing into the data
104 // fragment if it exists, otherwise we should just queue the label and set its
105 // fragment pointer when we emit the next fragment.
106 MCDataFragment *F = getOrCreateDataFragment();
107 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
109 SD.setOffset(F->getContents().size());
112 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
113 unsigned AddrSpace) {
115 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
116 EmitULEB128IntValue(IntValue, AddrSpace);
119 new MCLEBFragment(*Value, false, getCurrentSectionData());
122 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
123 unsigned AddrSpace) {
125 if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
126 EmitSLEB128IntValue(IntValue, AddrSpace);
129 new MCLEBFragment(*Value, true, getCurrentSectionData());
132 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
133 const MCSymbol *Symbol) {
134 report_fatal_error("This file format doesn't support weak aliases.");
137 void MCObjectStreamer::SwitchSection(const MCSection *Section) {
138 assert(Section && "Cannot switch to a null section!");
140 // If already in this section, then this is a noop.
141 if (Section == CurSection) return;
143 PrevSection = CurSection;
144 CurSection = Section;
145 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
148 void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
150 for (unsigned i = Inst.getNumOperands(); i--; )
151 if (Inst.getOperand(i).isExpr())
152 AddValueSymbols(Inst.getOperand(i).getExpr());
154 getCurrentSectionData()->setHasInstructions(true);
156 // Now that a machine instruction has been assembled into this section, make
157 // a line entry for any .loc directive that has been seen.
158 MCLineEntry::Make(this, getCurrentSection());
160 // If this instruction doesn't need relaxation, just emit it as data.
161 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
162 EmitInstToData(Inst);
166 // Otherwise, if we are relaxing everything, relax the instruction as much as
167 // possible and emit it as data.
168 if (getAssembler().getRelaxAll()) {
170 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
171 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
172 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
173 EmitInstToData(Relaxed);
177 // Otherwise emit to a separate fragment.
178 EmitInstToFragment(Inst);
181 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
182 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
184 raw_svector_ostream VecOS(IF->getCode());
185 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
188 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
189 const MCSymbol *LastLabel,
190 const MCSymbol *Label) {
192 int PointerSize = getAssembler().getBackend().getPointerSize();
193 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
196 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
197 const MCExpr *LabelRef =
198 MCSymbolRefExpr::Create(Label, Variant, getContext());
199 const MCExpr *LastLabelRef =
200 MCSymbolRefExpr::Create(LastLabel, Variant, getContext());
201 const MCExpr *AddrDelta =
202 MCBinaryExpr::Create(MCBinaryExpr::Sub, LabelRef, LastLabelRef,
205 if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
206 MCDwarfLineAddr::Emit(this, LineDelta, Res);
209 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
212 void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
213 unsigned char Value) {
214 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
217 void MCObjectStreamer::Finish() {
218 getAssembler().Finish();