blob: 4a5db8ce22224a3eaeaaf8f17622aac24be3850b [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
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
Daniel Dunbar0adcd352009-08-25 21:10:45 +000010#define DEBUG_TYPE "assembler"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +000012#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbarb36052f2010-03-19 10:43:23 +000013#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000015#include "llvm/MC/MCObjectWriter.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000016#include "llvm/MC/MCSymbol.h"
17#include "llvm/MC/MCValue.h"
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000018#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar0adcd352009-08-25 21:10:45 +000019#include "llvm/ADT/Statistic.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000021#include "llvm/ADT/Twine.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000022#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000024#include "llvm/Support/Debug.h"
Daniel Dunbaree0d8922010-03-13 22:10:17 +000025#include "llvm/Target/TargetRegistry.h"
Daniel Dunbardf3c8f22010-03-12 21:00:49 +000026#include "llvm/Target/TargetAsmBackend.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000027
Chris Lattner23132b12009-08-24 03:52:50 +000028#include <vector>
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000029using namespace llvm;
30
Daniel Dunbarff547842010-03-23 23:47:14 +000031namespace {
32namespace stats {
33STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
34STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
Daniel Dunbar0adcd352009-08-25 21:10:45 +000035STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
Daniel Dunbarff547842010-03-23 23:47:14 +000036STATISTIC(EvaluateFixup, "Number of evaluated fixups");
37STATISTIC(ObjectBytes, "Number of emitted object file bytes");
38}
39}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000040
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000041// FIXME FIXME FIXME: There are number of places in this file where we convert
42// what is a 64-bit assembler value used for computation into a value in the
43// object file, which may truncate it. We should detect that truncation where
44// invalid and report errors back.
45
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046/* *** */
47
Daniel Dunbar207e06e2010-03-24 03:43:40 +000048uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
49 return F->getAddress();
50}
51
52uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
53 return SD->getAddress();
54}
55
56uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
57 return SD->getAddress();
58}
59
60void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
61 SD->setAddress(Value);
62}
63
64/* *** */
65
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000066MCFragment::MCFragment() : Kind(FragmentType(~0)) {
67}
68
Daniel Dunbar5e835962009-08-26 02:48:04 +000069MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000070 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +000071 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000072 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000073{
Daniel Dunbar5e835962009-08-26 02:48:04 +000074 if (Parent)
75 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000076}
77
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000078MCFragment::~MCFragment() {
79}
80
Daniel Dunbar5e835962009-08-26 02:48:04 +000081uint64_t MCFragment::getAddress() const {
82 assert(getParent() && "Missing Section!");
83 return getParent()->getAddress() + Offset;
84}
85
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000086/* *** */
87
Daniel Dunbar81e40002009-08-27 00:38:04 +000088MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000089
90MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +000091 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000092 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +000093 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +000094 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +000095 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +000096 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000097{
98 if (A)
99 A->getSectionList().push_back(this);
100}
101
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000102/* *** */
103
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000104MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000105
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000106MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000107 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000108 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000109 IsExternal(false), IsPrivateExtern(false),
110 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000111{
112 if (A)
113 A->getSymbolList().push_back(this);
114}
115
116/* *** */
117
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000118MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000119 MCCodeEmitter &_Emitter, raw_ostream &_OS)
120 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
121 OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000122{
123}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000124
125MCAssembler::~MCAssembler() {
126}
127
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000128static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
129 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000130 const MCValue Target,
131 const MCSection *BaseSection) {
132 // The effective fixup address is
133 // addr(atom(A)) + offset(A)
134 // - addr(atom(B)) - offset(B)
135 // - addr(<base symbol>) + <fixup offset from base symbol>
136 // and the offsets are not relocatable, so the fixup is fully resolved when
137 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
138 //
139 // The simple (Darwin, except on x86_64) way of dealing with this was to
140 // assume that any reference to a temporary symbol *must* be a temporary
141 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
142 // relocation to a temporary symbol (in the same section) is fully
143 // resolved. This also works in conjunction with absolutized .set, which
144 // requires the compiler to use .set to absolutize the differences between
145 // symbols which the compiler knows to be assembly time constants, so we don't
146 // need to worry about consider symbol differences fully resolved.
147
148 // Non-relative fixups are only resolved if constant.
149 if (!BaseSection)
150 return Target.isAbsolute();
151
152 // Otherwise, relative fixups are only resolved if not a difference and the
153 // target is a temporary in the same section.
154 if (Target.isAbsolute() || Target.getSymB())
155 return false;
156
157 const MCSymbol *A = &Target.getSymA()->getSymbol();
158 if (!A->isTemporary() || !A->isInSection() ||
159 &A->getSection() != BaseSection)
160 return false;
161
162 return true;
163}
164
Daniel Dunbar034843a2010-03-19 03:18:18 +0000165static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000166 const MCAsmLayout &Layout,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000167 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000168 const MCValue Target,
169 const MCSymbolData *BaseSymbol) {
170 // The effective fixup address is
171 // addr(atom(A)) + offset(A)
172 // - addr(atom(B)) - offset(B)
173 // - addr(BaseSymbol) + <fixup offset from base symbol>
174 // and the offsets are not relocatable, so the fixup is fully resolved when
175 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
176 //
177 // Note that "false" is almost always conservatively correct (it means we emit
178 // a relocation which is unnecessary), except when it would force us to emit a
179 // relocation which the target cannot encode.
180
181 const MCSymbolData *A_Base = 0, *B_Base = 0;
182 if (const MCSymbolRefExpr *A = Target.getSymA()) {
183 // Modified symbol references cannot be resolved.
184 if (A->getKind() != MCSymbolRefExpr::VK_None)
185 return false;
186
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000187 A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000188 if (!A_Base)
189 return false;
190 }
191
192 if (const MCSymbolRefExpr *B = Target.getSymB()) {
193 // Modified symbol references cannot be resolved.
194 if (B->getKind() != MCSymbolRefExpr::VK_None)
195 return false;
196
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000197 B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000198 if (!B_Base)
199 return false;
200 }
201
202 // If there is no base, A and B have to be the same atom for this fixup to be
203 // fully resolved.
204 if (!BaseSymbol)
205 return A_Base == B_Base;
206
207 // Otherwise, B must be missing and A must be the base.
208 return !B_Base && BaseSymbol == A_Base;
209}
210
Daniel Dunbar23869852010-03-19 03:18:09 +0000211bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
212 // Non-temporary labels should always be visible to the linker.
213 if (!SD->getSymbol().isTemporary())
214 return true;
215
216 // Absolute temporary labels are never visible.
217 if (!SD->getFragment())
218 return false;
219
220 // Otherwise, check if the section requires symbols even for temporary labels.
221 return getBackend().doesSectionRequireSymbols(
222 SD->getFragment()->getParent()->getSection());
223}
224
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000225// FIXME-PERF: This routine is really slow.
226const MCSymbolData *MCAssembler::getAtomForAddress(const MCAsmLayout &Layout,
227 const MCSectionData *Section,
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000228 uint64_t Address) const {
229 const MCSymbolData *Best = 0;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000230 uint64_t BestAddress = 0;
231
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000232 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
233 ie = symbol_end(); it != ie; ++it) {
234 // Ignore non-linker visible symbols.
235 if (!isSymbolLinkerVisible(it))
236 continue;
237
238 // Ignore symbols not in the same section.
239 if (!it->getFragment() || it->getFragment()->getParent() != Section)
240 continue;
241
242 // Otherwise, find the closest symbol preceding this address (ties are
243 // resolved in favor of the last defined symbol).
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000244 uint64_t SymbolAddress = Layout.getSymbolAddress(it);
245 if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000246 Best = it;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000247 BestAddress = SymbolAddress;
248 }
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000249 }
250
251 return Best;
252}
253
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000254// FIXME-PERF: This routine is really slow.
255const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout,
256 const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000257 // Linker visible symbols define atoms.
258 if (isSymbolLinkerVisible(SD))
259 return SD;
260
261 // Absolute and undefined symbols have no defining atom.
262 if (!SD->getFragment())
263 return 0;
264
265 // Otherwise, search by address.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000266 return getAtomForAddress(Layout, SD->getFragment()->getParent(),
267 Layout.getSymbolAddress(SD));
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000268}
269
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000270bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
271 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000272 MCValue &Target, uint64_t &Value) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000273 ++stats::EvaluateFixup;
274
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000275 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
276 llvm_report_error("expected relocatable expression");
277
278 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
279 // doesn't support small relocations, but then under what criteria does the
280 // assembler allow symbol differences?
281
282 Value = Target.getConstant();
283
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000284 bool IsPCRel =
285 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
286 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000287 if (const MCSymbolRefExpr *A = Target.getSymA()) {
288 if (A->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000289 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000290 else
291 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000292 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000293 if (const MCSymbolRefExpr *B = Target.getSymB()) {
294 if (B->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000295 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000296 else
297 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000298 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000299
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000300 // If we are using scattered symbols, determine whether this value is actually
301 // resolved; scattering may cause atoms to move.
302 if (IsResolved && getBackend().hasScatteredSymbols()) {
303 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000304 // If this is a PCrel relocation, find the base atom (identified by its
305 // symbol) that the fixup value is relative to.
306 const MCSymbolData *BaseSymbol = 0;
307 if (IsPCRel) {
308 BaseSymbol = getAtomForAddress(
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000309 Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset);
Daniel Dunbar034843a2010-03-19 03:18:18 +0000310 if (!BaseSymbol)
311 IsResolved = false;
312 }
313
314 if (IsResolved)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000315 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000316 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000317 } else {
318 const MCSection *BaseSection = 0;
319 if (IsPCRel)
320 BaseSection = &DF->getParent()->getSection();
321
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000322 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000323 BaseSection);
324 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000325 }
326
327 if (IsPCRel)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000328 Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000329
330 return IsResolved;
331}
332
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000333void MCAssembler::LayoutSection(MCSectionData &SD,
334 MCAsmLayout &Layout) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000335 uint64_t Address, StartAddress = Address = Layout.getSectionAddress(&SD);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000336
337 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
338 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000339
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000340 F.setOffset(Address - StartAddress);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000341
342 // Evaluate fragment size.
343 switch (F.getKind()) {
344 case MCFragment::FT_Align: {
345 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000346
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000347 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000348 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000349 AF.setFileSize(0);
350 else
Daniel Dunbar6742e342009-08-26 04:13:32 +0000351 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000352 break;
353 }
354
355 case MCFragment::FT_Data:
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000356 F.setFileSize(cast<MCDataFragment>(F).getContents().size());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000357 break;
358
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000359 case MCFragment::FT_Fill: {
360 MCFillFragment &FF = cast<MCFillFragment>(F);
361 F.setFileSize(FF.getValueSize() * FF.getCount());
362 break;
363 }
364
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000365 case MCFragment::FT_Inst:
366 F.setFileSize(cast<MCInstFragment>(F).getInstSize());
367 break;
368
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000369 case MCFragment::FT_Org: {
370 MCOrgFragment &OF = cast<MCOrgFragment>(F);
371
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000372 int64_t TargetLocation;
373 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
374 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000375
376 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000377 int64_t Offset = TargetLocation - F.getOffset();
378 if (Offset < 0)
379 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
380 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000381
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000382 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000383 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000384 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000385
386 case MCFragment::FT_ZeroFill: {
387 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
388
389 // Align the fragment offset; it is safe to adjust the offset freely since
390 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +0000391 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000392 F.setOffset(Address - StartAddress);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000393
394 // FIXME: This is misnamed.
395 F.setFileSize(ZFF.getSize());
396 break;
397 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000398 }
399
Daniel Dunbar6742e342009-08-26 04:13:32 +0000400 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000401 }
402
Daniel Dunbar6742e342009-08-26 04:13:32 +0000403 // Set the section sizes.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000404 SD.setSize(Address - StartAddress);
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000405 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000406 SD.setFileSize(0);
407 else
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000408 SD.setFileSize(Address - StartAddress);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000409}
410
Daniel Dunbar53b23382010-03-19 09:28:59 +0000411/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000412static void WriteFragmentData(const MCAssembler &Asm, const MCFragment &F,
413 MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000414 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000415 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000416
Daniel Dunbarff547842010-03-23 23:47:14 +0000417 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000418
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000419 // FIXME: Embed in fragments instead?
420 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000421 case MCFragment::FT_Align: {
422 MCAlignFragment &AF = cast<MCAlignFragment>(F);
423 uint64_t Count = AF.getFileSize() / AF.getValueSize();
424
425 // FIXME: This error shouldn't actually occur (the front end should emit
426 // multiple .align directives to enforce the semantics it wants), but is
427 // severe enough that we want to report it. How to handle this?
428 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000429 llvm_report_error("undefined .align directive, value size '" +
430 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000431 "' is not a divisor of padding size '" +
432 Twine(AF.getFileSize()) + "'");
433
Kevin Enderby6e720482010-02-23 18:26:34 +0000434 // See if we are aligning with nops, and if so do that first to try to fill
435 // the Count bytes. Then if that did not fill any bytes or there are any
436 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000437 // If we are aligning with nops, ask that target to emit the right data.
Kevin Enderby6e720482010-02-23 18:26:34 +0000438 if (AF.getEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000439 if (!Asm.getBackend().WriteNopData(Count, OW))
440 llvm_report_error("unable to write nop sequence of " +
441 Twine(Count) + " bytes");
442 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000443 }
444
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000445 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000446 for (uint64_t i = 0; i != Count; ++i) {
447 switch (AF.getValueSize()) {
448 default:
449 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000450 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
451 case 2: OW->Write16(uint16_t(AF.getValue())); break;
452 case 4: OW->Write32(uint32_t(AF.getValue())); break;
453 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000454 }
455 }
456 break;
457 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000458
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000459 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000460 MCDataFragment &DF = cast<MCDataFragment>(F);
461 assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!");
462 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000463 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000464 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000465
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000466 case MCFragment::FT_Fill: {
467 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000468 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
469 switch (FF.getValueSize()) {
470 default:
471 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000472 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
473 case 2: OW->Write16(uint16_t(FF.getValue())); break;
474 case 4: OW->Write32(uint32_t(FF.getValue())); break;
475 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000476 }
477 }
478 break;
479 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000480
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000481 case MCFragment::FT_Inst:
482 llvm_unreachable("unexpected inst fragment after lowering");
483 break;
484
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000485 case MCFragment::FT_Org: {
486 MCOrgFragment &OF = cast<MCOrgFragment>(F);
487
488 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000489 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000490
491 break;
492 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000493
494 case MCFragment::FT_ZeroFill: {
495 assert(0 && "Invalid zero fill fragment in concrete section!");
496 break;
497 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000498 }
499
Daniel Dunbar53b23382010-03-19 09:28:59 +0000500 assert(OW->getStream().tell() - Start == F.getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000501}
502
Daniel Dunbar53b23382010-03-19 09:28:59 +0000503void MCAssembler::WriteSectionData(const MCSectionData *SD,
504 MCObjectWriter *OW) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000505 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000506 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000507 assert(SD->getFileSize() == 0);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000508 return;
509 }
510
Daniel Dunbar53b23382010-03-19 09:28:59 +0000511 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000512 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000513
Daniel Dunbar53b23382010-03-19 09:28:59 +0000514 for (MCSectionData::const_iterator it = SD->begin(),
515 ie = SD->end(); it != ie; ++it)
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000516 WriteFragmentData(*this, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000517
Daniel Dunbar6742e342009-08-26 04:13:32 +0000518 // Add section padding.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000519 assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
520 OW->WriteZeros(SD->getFileSize() - SD->getSize());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000521
Daniel Dunbar53b23382010-03-19 09:28:59 +0000522 assert(OW->getStream().tell() - Start == SD->getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000523}
524
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000525void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000526 DEBUG_WITH_TYPE("mc-dump", {
527 llvm::errs() << "assembler backend - pre-layout\n--\n";
528 dump(); });
529
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000530 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000531 MCAsmLayout Layout(*this);
532 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000533 continue;
534
535 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000536 llvm::errs() << "assembler backend - post-relaxation\n--\n";
537 dump(); });
538
539 // Finalize the layout, including fragment lowering.
540 FinishLayout(Layout);
541
542 DEBUG_WITH_TYPE("mc-dump", {
543 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000544 dump(); });
545
Daniel Dunbarff547842010-03-23 23:47:14 +0000546 uint64_t StartOffset = OS.tell();
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000547 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
548 if (!Writer)
549 llvm_report_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000550
551 // Allow the object writer a chance to perform post-layout binding (for
552 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000553 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000554
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000555 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000556 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
557 for (MCSectionData::iterator it2 = it->begin(),
558 ie2 = it->end(); it2 != ie2; ++it2) {
559 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
560 if (!DF)
561 continue;
562
563 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
564 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
565 MCAsmFixup &Fixup = *it3;
566
567 // Evaluate the fixup.
568 MCValue Target;
569 uint64_t FixedValue;
570 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
571 // The fixup was unresolved, we need a relocation. Inform the object
572 // writer of the relocation, and give it an opportunity to adjust the
573 // fixup value if need be.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000574 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000575 }
576
Daniel Dunbar87190c42010-03-19 09:28:12 +0000577 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000578 }
579 }
580 }
581
Daniel Dunbarbacba992010-03-19 07:09:33 +0000582 // Write the object file.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000583 Writer->WriteObject(*this, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000584 OS.flush();
Daniel Dunbarff547842010-03-23 23:47:14 +0000585
586 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000587}
588
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000589bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
590 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000591 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000592 // If we cannot resolve the fixup value, it requires relaxation.
593 MCValue Target;
594 uint64_t Value;
595 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
596 return true;
597
598 // Otherwise, relax if the value is too big for a (signed) i8.
599 return int64_t(Value) != int64_t(int8_t(Value));
600}
601
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000602bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
603 const MCAsmLayout &Layout) const {
604 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
605 // are intentionally pushing out inst fragments, or because we relaxed a
606 // previous instruction to one that doesn't need relaxation.
607 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
608 return false;
609
610 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
611 ie = IF->fixup_end(); it != ie; ++it)
612 if (FixupNeedsRelaxation(*it, IF, Layout))
613 return true;
614
615 return false;
616}
617
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000618bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000619 ++stats::RelaxationSteps;
620
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000621 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000622 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000623 MCSectionData *Prev = 0;
624 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000625 MCSectionData &SD = *it;
626
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000627 // Skip virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000628 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000629 continue;
630
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000631 // Align this section if necessary by adding padding bytes to the previous
632 // section.
633 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
634 assert(Prev && "Missing prev section!");
635 Prev->setFileSize(Prev->getFileSize() + Pad);
636 Address += Pad;
637 }
Daniel Dunbar6742e342009-08-26 04:13:32 +0000638
639 // Layout the section fragments and its size.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000640 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000641 LayoutSection(SD, Layout);
Daniel Dunbar6742e342009-08-26 04:13:32 +0000642 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000643
644 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +0000645 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000646
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000647 // Layout the virtual sections.
648 for (iterator it = begin(), ie = end(); it != ie; ++it) {
649 MCSectionData &SD = *it;
650
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000651 if (!getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000652 continue;
653
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000654 // Align this section if necessary by adding padding bytes to the previous
655 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +0000656 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000657 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000658
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000659 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000660 LayoutSection(SD, Layout);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000661 Address += SD.getSize();
662 }
663
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000664 // Scan for fragments that need relaxation.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000665 for (iterator it = begin(), ie = end(); it != ie; ++it) {
666 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000667
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000668 for (MCSectionData::iterator it2 = SD.begin(),
669 ie2 = SD.end(); it2 != ie2; ++it2) {
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000670 // Check if this is an instruction fragment that needs relaxation.
671 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
672 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000673 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000674
Daniel Dunbarff547842010-03-23 23:47:14 +0000675 ++stats::RelaxedInstructions;
676
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000677 // FIXME-PERF: We could immediately lower out instructions if we can tell
678 // they are fully resolved, to avoid retesting on later passes.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000679
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000680 // Relax the fragment.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000681
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000682 MCInst Relaxed;
683 getBackend().RelaxInstruction(IF, Relaxed);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000684
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000685 // Encode the new instruction.
686 //
687 // FIXME-PERF: If it matters, we could let the target do this. It can
688 // probably do so more efficiently in many cases.
689 SmallVector<MCFixup, 4> Fixups;
690 SmallString<256> Code;
691 raw_svector_ostream VecOS(Code);
692 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
693 VecOS.flush();
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000694
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000695 // Update the instruction fragment.
696 IF->setInst(Relaxed);
697 IF->getCode() = Code;
698 IF->getFixups().clear();
699 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
700 MCFixup &F = Fixups[i];
701 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
702 F.getKind()));
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000703 }
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000704
705 // Restart layout.
706 //
707 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
708 // smart MCAsmLayout object.
709 return true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000710 }
711 }
712
713 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000714}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000715
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000716void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
717 // Lower out any instruction fragments, to simplify the fixup application and
718 // output.
719 //
720 // FIXME-PERF: We don't have to do this, but the assumption is that it is
721 // cheap (we will mostly end up eliminating fragments and appending on to data
722 // fragments), so the extra complexity downstream isn't worth it. Evaluate
723 // this assumption.
724 for (iterator it = begin(), ie = end(); it != ie; ++it) {
725 MCSectionData &SD = *it;
726
727 for (MCSectionData::iterator it2 = SD.begin(),
728 ie2 = SD.end(); it2 != ie2; ++it2) {
729 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
730 if (!IF)
731 continue;
732
733 // Create a new data fragment for the instruction.
734 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000735 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000736 MCDataFragment *DF = new MCDataFragment();
737 SD.getFragmentList().insert(it2, DF);
738
739 // Update the data fragments layout data.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000740 DF->setParent(IF->getParent());
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000741 DF->setOffset(IF->getOffset());
742 DF->setFileSize(IF->getInstSize());
743
Daniel Dunbar9799de92010-03-23 01:39:05 +0000744 // Copy in the data and the fixups.
745 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
746 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
747 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000748
749 // Delete the instruction fragment and update the iterator.
750 SD.getFragmentList().erase(IF);
751 it2 = DF;
752 }
753 }
754}
755
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000756// Debugging methods
757
758namespace llvm {
759
760raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000761 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
762 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000763 return OS;
764}
765
766}
767
768void MCFragment::dump() {
769 raw_ostream &OS = llvm::errs();
770
771 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
772 << " FileSize:" << FileSize;
773
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000774 OS << ">";
775}
776
777void MCAlignFragment::dump() {
778 raw_ostream &OS = llvm::errs();
779
780 OS << "<MCAlignFragment ";
781 this->MCFragment::dump();
782 OS << "\n ";
783 OS << " Alignment:" << getAlignment()
784 << " Value:" << getValue() << " ValueSize:" << getValueSize()
785 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
786}
787
788void MCDataFragment::dump() {
789 raw_ostream &OS = llvm::errs();
790
791 OS << "<MCDataFragment ";
792 this->MCFragment::dump();
793 OS << "\n ";
794 OS << " Contents:[";
795 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
796 if (i) OS << ",";
797 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
798 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000799 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000800
801 if (!getFixups().empty()) {
802 OS << ",\n ";
803 OS << " Fixups:[";
804 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000805 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000806 OS << *it;
807 }
808 OS << "]";
809 }
810
811 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000812}
813
814void MCFillFragment::dump() {
815 raw_ostream &OS = llvm::errs();
816
817 OS << "<MCFillFragment ";
818 this->MCFragment::dump();
819 OS << "\n ";
820 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
821 << " Count:" << getCount() << ">";
822}
823
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000824void MCInstFragment::dump() {
825 raw_ostream &OS = llvm::errs();
826
827 OS << "<MCInstFragment ";
828 this->MCFragment::dump();
829 OS << "\n ";
830 OS << " Inst:";
831 getInst().dump_pretty(OS);
832 OS << ">";
833}
834
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000835void MCOrgFragment::dump() {
836 raw_ostream &OS = llvm::errs();
837
838 OS << "<MCOrgFragment ";
839 this->MCFragment::dump();
840 OS << "\n ";
841 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
842}
843
844void MCZeroFillFragment::dump() {
845 raw_ostream &OS = llvm::errs();
846
847 OS << "<MCZeroFillFragment ";
848 this->MCFragment::dump();
849 OS << "\n ";
850 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
851}
852
853void MCSectionData::dump() {
854 raw_ostream &OS = llvm::errs();
855
856 OS << "<MCSectionData";
857 OS << " Alignment:" << getAlignment() << " Address:" << Address
858 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000859 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000860 for (iterator it = begin(), ie = end(); it != ie; ++it) {
861 if (it != begin()) OS << ",\n ";
862 it->dump();
863 }
864 OS << "]>";
865}
866
867void MCSymbolData::dump() {
868 raw_ostream &OS = llvm::errs();
869
870 OS << "<MCSymbolData Symbol:" << getSymbol()
871 << " Fragment:" << getFragment() << " Offset:" << getOffset()
872 << " Flags:" << getFlags() << " Index:" << getIndex();
873 if (isCommon())
874 OS << " (common, size:" << getCommonSize()
875 << " align: " << getCommonAlignment() << ")";
876 if (isExternal())
877 OS << " (external)";
878 if (isPrivateExtern())
879 OS << " (private extern)";
880 OS << ">";
881}
882
883void MCAssembler::dump() {
884 raw_ostream &OS = llvm::errs();
885
886 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000887 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000888 for (iterator it = begin(), ie = end(); it != ie; ++it) {
889 if (it != begin()) OS << ",\n ";
890 it->dump();
891 }
892 OS << "],\n";
893 OS << " Symbols:[";
894
895 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000896 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000897 it->dump();
898 }
899 OS << "]>\n";
900}