blob: 3a79d1921c2983e973c8c14a6e347ae780aa838a [file] [log] [blame]
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +00001//===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCObjectStreamer.h"
11
Michael J. Spencer8067adc2010-07-19 06:13:10 +000012#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000013#include "llvm/MC/MCAssembler.h"
Benjamin Kramer1abcd062010-07-29 17:48:06 +000014#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000015#include "llvm/MC/MCContext.h"
Rafael Espindolaf89671d2010-11-01 16:27:31 +000016#include "llvm/MC/MCDwarf.h"
Michael J. Spencer8067adc2010-07-19 06:13:10 +000017#include "llvm/MC/MCExpr.h"
Rafael Espindolaea4afa92010-11-28 17:18:55 +000018#include "llvm/MC/MCSymbol.h"
Benjamin Kramer1abcd062010-07-29 17:48:06 +000019#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000020#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000021using namespace llvm;
22
23MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +000024 raw_ostream &OS, MCCodeEmitter *Emitter_)
25 : MCStreamer(Context),
26 Assembler(new MCAssembler(Context, TAB,
27 *Emitter_, *TAB.createObjectWriter(OS),
28 OS)),
Daniel Dunbar83b46712010-06-16 20:04:25 +000029 CurSectionData(0)
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000030{
31}
32
33MCObjectStreamer::~MCObjectStreamer() {
Benjamin Kramer1abcd062010-07-29 17:48:06 +000034 delete &Assembler->getBackend();
35 delete &Assembler->getEmitter();
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +000036 delete &Assembler->getWriter();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000037 delete Assembler;
38}
Daniel Dunbar83b46712010-06-16 20:04:25 +000039
Michael J. Spencer8067adc2010-07-19 06:13:10 +000040MCFragment *MCObjectStreamer::getCurrentFragment() const {
41 assert(getCurrentSectionData() && "No current section!");
42
43 if (!getCurrentSectionData()->empty())
44 return &getCurrentSectionData()->getFragmentList().back();
45
46 return 0;
47}
48
49MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const {
50 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
51 if (!F)
52 F = new MCDataFragment(getCurrentSectionData());
53 return F;
54}
55
56const 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:
60 break;
61
62 case MCExpr::Binary: {
63 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
64 AddValueSymbols(BE->getLHS());
65 AddValueSymbols(BE->getRHS());
66 break;
67 }
68
69 case MCExpr::SymbolRef:
70 Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
71 break;
72
73 case MCExpr::Unary:
74 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
75 break;
76 }
77
78 return Value;
79}
80
Rafael Espindola89b93722010-12-10 07:39:47 +000081void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
82 bool isPCRel, unsigned AddrSpace) {
Rafael Espindola6f950232010-11-28 23:08:47 +000083 assert(AddrSpace == 0 && "Address space must be 0!");
84 MCDataFragment *DF = getOrCreateDataFragment();
85
86 // Avoid fixups when possible.
87 int64_t AbsValue;
Daniel Dunbar2d8e3e72010-12-17 01:07:35 +000088 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
Rafael Espindola2df042c2010-12-03 02:54:21 +000089 EmitIntValue(AbsValue, Size, AddrSpace);
90 return;
Rafael Espindola6f950232010-11-28 23:08:47 +000091 }
Rafael Espindola2df042c2010-12-03 02:54:21 +000092 DF->addFixup(MCFixup::Create(DF->getContents().size(),
93 AddValueSymbols(Value),
Rafael Espindola89b93722010-12-10 07:39:47 +000094 MCFixup::getKindForSize(Size, isPCRel)));
Rafael Espindola2df042c2010-12-03 02:54:21 +000095 DF->getContents().resize(DF->getContents().size() + Size, 0);
Rafael Espindola6f950232010-11-28 23:08:47 +000096}
97
Rafael Espindolaea4afa92010-11-28 17:18:55 +000098void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) {
99 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
100 assert(CurSection && "Cannot emit before setting section!");
101
102 Symbol->setSection(*CurSection);
103
104 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
105
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!");
112 SD.setFragment(F);
113 SD.setOffset(F->getContents().size());
114}
115
Rafael Espindola3ff57092010-11-02 17:22:24 +0000116void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value,
117 unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000118 int64_t IntValue;
Daniel Dunbar2d8e3e72010-12-17 01:07:35 +0000119 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000120 EmitULEB128IntValue(IntValue, AddrSpace);
121 return;
122 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000123 new MCLEBFragment(*Value, false, getCurrentSectionData());
124}
125
126void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value,
127 unsigned AddrSpace) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000128 int64_t IntValue;
Daniel Dunbar2d8e3e72010-12-17 01:07:35 +0000129 if (Value->EvaluateAsAbsolute(IntValue)) {
Rafael Espindola660b5fc2010-12-03 01:19:49 +0000130 EmitSLEB128IntValue(IntValue, AddrSpace);
131 return;
132 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000133 new MCLEBFragment(*Value, true, getCurrentSectionData());
134}
135
Rafael Espindola484291c2010-11-01 14:28:48 +0000136void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
137 const MCSymbol *Symbol) {
138 report_fatal_error("This file format doesn't support weak aliases.");
139}
140
Daniel Dunbar83b46712010-06-16 20:04:25 +0000141void MCObjectStreamer::SwitchSection(const MCSection *Section) {
142 assert(Section && "Cannot switch to a null section!");
143
144 // If already in this section, then this is a noop.
145 if (Section == CurSection) return;
146
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000147 PrevSection = CurSection;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000148 CurSection = Section;
149 CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
150}
151
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000152void MCObjectStreamer::EmitInstruction(const MCInst &Inst) {
153 // Scan for values.
154 for (unsigned i = Inst.getNumOperands(); i--; )
155 if (Inst.getOperand(i).isExpr())
156 AddValueSymbols(Inst.getOperand(i).getExpr());
157
158 getCurrentSectionData()->setHasInstructions(true);
159
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());
163
164 // If this instruction doesn't need relaxation, just emit it as data.
165 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
166 EmitInstToData(Inst);
167 return;
168 }
169
170 // Otherwise, if we are relaxing everything, relax the instruction as much as
171 // possible and emit it as data.
172 if (getAssembler().getRelaxAll()) {
173 MCInst Relaxed;
174 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
175 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
176 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
177 EmitInstToData(Relaxed);
178 return;
179 }
180
181 // Otherwise emit to a separate fragment.
182 EmitInstToFragment(Inst);
183}
184
Rafael Espindoladedb0452010-12-02 05:44:06 +0000185void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) {
186 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
187
188 raw_svector_ostream VecOS(IF->getCode());
189 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
190}
191
Rafael Espindola32a006e2010-12-03 00:55:40 +0000192void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
193 const MCSymbol *LastLabel,
194 const MCSymbol *Label) {
195 if (!LastLabel) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000196 int PointerSize = getContext().getTargetAsmInfo().getPointerSize();
Rafael Espindola32a006e2010-12-03 00:55:40 +0000197 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
198 return;
199 }
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,
207 getContext());
208 int64_t Res;
Daniel Dunbar2d8e3e72010-12-17 01:07:35 +0000209 if (AddrDelta->EvaluateAsAbsolute(Res)) {
Rafael Espindola32a006e2010-12-03 00:55:40 +0000210 MCDwarfLineAddr::Emit(this, LineDelta, Res);
211 return;
212 }
213 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, getCurrentSectionData());
214}
215
Rafael Espindolae2393052010-12-02 05:59:38 +0000216void MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset,
217 unsigned char Value) {
218 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
219}
220
Daniel Dunbar83b46712010-06-16 20:04:25 +0000221void MCObjectStreamer::Finish() {
Rafael Espindola89b93722010-12-10 07:39:47 +0000222 // Dump out the dwarf file & directory tables and line tables.
223 if (getContext().hasDwarfFiles())
224 MCDwarfFileTable::Emit(this);
225
Daniel Dunbar83b46712010-06-16 20:04:25 +0000226 getAssembler().Finish();
227}