blob: 3fac6c833499cf113fb5382a4bb5b0b820d0d8f5 [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 {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000049 assert(F->getParent() && "Missing section()!");
50 return getSectionAddress(F->getParent()) + F->getOffset();
Daniel Dunbar207e06e2010-03-24 03:43:40 +000051}
52
53uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000054 assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
55 return getFragmentAddress(SD->getFragment()) + SD->getOffset();
Daniel Dunbar207e06e2010-03-24 03:43:40 +000056}
57
58uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000059 assert(SD->Address != ~UINT64_C(0) && "Address not set!");
60 return SD->Address;
Daniel Dunbar207e06e2010-03-24 03:43:40 +000061}
62
63void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000064 SD->Address = Value;
Daniel Dunbar207e06e2010-03-24 03:43:40 +000065}
66
67/* *** */
68
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000069MCFragment::MCFragment() : Kind(FragmentType(~0)) {
70}
71
Daniel Dunbar5e835962009-08-26 02:48:04 +000072MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000073 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +000074 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000075 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000076{
Daniel Dunbar5e835962009-08-26 02:48:04 +000077 if (Parent)
78 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000079}
80
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000081MCFragment::~MCFragment() {
82}
83
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000084/* *** */
85
Daniel Dunbar81e40002009-08-27 00:38:04 +000086MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000087
88MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +000089 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000090 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +000091 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +000092 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +000093 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +000094 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000095{
96 if (A)
97 A->getSectionList().push_back(this);
98}
99
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000100/* *** */
101
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000102MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000103
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000104MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000105 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000106 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000107 IsExternal(false), IsPrivateExtern(false),
108 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000109{
110 if (A)
111 A->getSymbolList().push_back(this);
112}
113
114/* *** */
115
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000116MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000117 MCCodeEmitter &_Emitter, raw_ostream &_OS)
118 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
119 OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000120{
121}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000122
123MCAssembler::~MCAssembler() {
124}
125
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000126static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
127 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000128 const MCValue Target,
129 const MCSection *BaseSection) {
130 // The effective fixup address is
131 // addr(atom(A)) + offset(A)
132 // - addr(atom(B)) - offset(B)
133 // - addr(<base symbol>) + <fixup offset from base symbol>
134 // and the offsets are not relocatable, so the fixup is fully resolved when
135 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
136 //
137 // The simple (Darwin, except on x86_64) way of dealing with this was to
138 // assume that any reference to a temporary symbol *must* be a temporary
139 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
140 // relocation to a temporary symbol (in the same section) is fully
141 // resolved. This also works in conjunction with absolutized .set, which
142 // requires the compiler to use .set to absolutize the differences between
143 // symbols which the compiler knows to be assembly time constants, so we don't
144 // need to worry about consider symbol differences fully resolved.
145
146 // Non-relative fixups are only resolved if constant.
147 if (!BaseSection)
148 return Target.isAbsolute();
149
150 // Otherwise, relative fixups are only resolved if not a difference and the
151 // target is a temporary in the same section.
152 if (Target.isAbsolute() || Target.getSymB())
153 return false;
154
155 const MCSymbol *A = &Target.getSymA()->getSymbol();
156 if (!A->isTemporary() || !A->isInSection() ||
157 &A->getSection() != BaseSection)
158 return false;
159
160 return true;
161}
162
Daniel Dunbar034843a2010-03-19 03:18:18 +0000163static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000164 const MCAsmLayout &Layout,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000165 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000166 const MCValue Target,
167 const MCSymbolData *BaseSymbol) {
168 // The effective fixup address is
169 // addr(atom(A)) + offset(A)
170 // - addr(atom(B)) - offset(B)
171 // - addr(BaseSymbol) + <fixup offset from base symbol>
172 // and the offsets are not relocatable, so the fixup is fully resolved when
173 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
174 //
175 // Note that "false" is almost always conservatively correct (it means we emit
176 // a relocation which is unnecessary), except when it would force us to emit a
177 // relocation which the target cannot encode.
178
179 const MCSymbolData *A_Base = 0, *B_Base = 0;
180 if (const MCSymbolRefExpr *A = Target.getSymA()) {
181 // Modified symbol references cannot be resolved.
182 if (A->getKind() != MCSymbolRefExpr::VK_None)
183 return false;
184
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000185 A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000186 if (!A_Base)
187 return false;
188 }
189
190 if (const MCSymbolRefExpr *B = Target.getSymB()) {
191 // Modified symbol references cannot be resolved.
192 if (B->getKind() != MCSymbolRefExpr::VK_None)
193 return false;
194
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000195 B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000196 if (!B_Base)
197 return false;
198 }
199
200 // If there is no base, A and B have to be the same atom for this fixup to be
201 // fully resolved.
202 if (!BaseSymbol)
203 return A_Base == B_Base;
204
205 // Otherwise, B must be missing and A must be the base.
206 return !B_Base && BaseSymbol == A_Base;
207}
208
Daniel Dunbar23869852010-03-19 03:18:09 +0000209bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
210 // Non-temporary labels should always be visible to the linker.
211 if (!SD->getSymbol().isTemporary())
212 return true;
213
214 // Absolute temporary labels are never visible.
215 if (!SD->getFragment())
216 return false;
217
218 // Otherwise, check if the section requires symbols even for temporary labels.
219 return getBackend().doesSectionRequireSymbols(
220 SD->getFragment()->getParent()->getSection());
221}
222
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000223// FIXME-PERF: This routine is really slow.
224const MCSymbolData *MCAssembler::getAtomForAddress(const MCAsmLayout &Layout,
225 const MCSectionData *Section,
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000226 uint64_t Address) const {
227 const MCSymbolData *Best = 0;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000228 uint64_t BestAddress = 0;
229
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000230 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
231 ie = symbol_end(); it != ie; ++it) {
232 // Ignore non-linker visible symbols.
233 if (!isSymbolLinkerVisible(it))
234 continue;
235
236 // Ignore symbols not in the same section.
237 if (!it->getFragment() || it->getFragment()->getParent() != Section)
238 continue;
239
240 // Otherwise, find the closest symbol preceding this address (ties are
241 // resolved in favor of the last defined symbol).
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000242 uint64_t SymbolAddress = Layout.getSymbolAddress(it);
243 if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000244 Best = it;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000245 BestAddress = SymbolAddress;
246 }
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000247 }
248
249 return Best;
250}
251
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000252// FIXME-PERF: This routine is really slow.
253const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout,
254 const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000255 // Linker visible symbols define atoms.
256 if (isSymbolLinkerVisible(SD))
257 return SD;
258
259 // Absolute and undefined symbols have no defining atom.
260 if (!SD->getFragment())
261 return 0;
262
263 // Otherwise, search by address.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000264 return getAtomForAddress(Layout, SD->getFragment()->getParent(),
265 Layout.getSymbolAddress(SD));
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000266}
267
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000268bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
269 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000270 MCValue &Target, uint64_t &Value) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000271 ++stats::EvaluateFixup;
272
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000273 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
274 llvm_report_error("expected relocatable expression");
275
276 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
277 // doesn't support small relocations, but then under what criteria does the
278 // assembler allow symbol differences?
279
280 Value = Target.getConstant();
281
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000282 bool IsPCRel =
283 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
284 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000285 if (const MCSymbolRefExpr *A = Target.getSymA()) {
286 if (A->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000287 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000288 else
289 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000290 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000291 if (const MCSymbolRefExpr *B = Target.getSymB()) {
292 if (B->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000293 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000294 else
295 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000296 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000297
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000298 // If we are using scattered symbols, determine whether this value is actually
299 // resolved; scattering may cause atoms to move.
300 if (IsResolved && getBackend().hasScatteredSymbols()) {
301 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000302 // If this is a PCrel relocation, find the base atom (identified by its
303 // symbol) that the fixup value is relative to.
304 const MCSymbolData *BaseSymbol = 0;
305 if (IsPCRel) {
306 BaseSymbol = getAtomForAddress(
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000307 Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset);
Daniel Dunbar034843a2010-03-19 03:18:18 +0000308 if (!BaseSymbol)
309 IsResolved = false;
310 }
311
312 if (IsResolved)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000313 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000314 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000315 } else {
316 const MCSection *BaseSection = 0;
317 if (IsPCRel)
318 BaseSection = &DF->getParent()->getSection();
319
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000320 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000321 BaseSection);
322 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000323 }
324
325 if (IsPCRel)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000326 Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000327
328 return IsResolved;
329}
330
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000331void MCAssembler::LayoutSection(MCSectionData &SD,
332 MCAsmLayout &Layout) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000333 uint64_t Address, StartAddress = Address = Layout.getSectionAddress(&SD);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000334
335 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
336 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000337
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000338 F.setOffset(Address - StartAddress);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000339
340 // Evaluate fragment size.
341 switch (F.getKind()) {
342 case MCFragment::FT_Align: {
343 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000344
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000345 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000346 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000347 AF.setFileSize(0);
348 else
Daniel Dunbar6742e342009-08-26 04:13:32 +0000349 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000350 break;
351 }
352
353 case MCFragment::FT_Data:
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000354 F.setFileSize(cast<MCDataFragment>(F).getContents().size());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000355 break;
356
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000357 case MCFragment::FT_Fill: {
358 MCFillFragment &FF = cast<MCFillFragment>(F);
359 F.setFileSize(FF.getValueSize() * FF.getCount());
360 break;
361 }
362
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000363 case MCFragment::FT_Inst:
364 F.setFileSize(cast<MCInstFragment>(F).getInstSize());
365 break;
366
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000367 case MCFragment::FT_Org: {
368 MCOrgFragment &OF = cast<MCOrgFragment>(F);
369
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000370 int64_t TargetLocation;
371 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
372 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000373
374 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000375 int64_t Offset = TargetLocation - F.getOffset();
376 if (Offset < 0)
377 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
378 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000379
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000380 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000381 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000382 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000383
384 case MCFragment::FT_ZeroFill: {
385 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
386
387 // Align the fragment offset; it is safe to adjust the offset freely since
388 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +0000389 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000390 F.setOffset(Address - StartAddress);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000391
392 // FIXME: This is misnamed.
393 F.setFileSize(ZFF.getSize());
394 break;
395 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000396 }
397
Daniel Dunbar6742e342009-08-26 04:13:32 +0000398 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000399 }
400
Daniel Dunbar6742e342009-08-26 04:13:32 +0000401 // Set the section sizes.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000402 SD.setSize(Address - StartAddress);
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000403 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000404 SD.setFileSize(0);
405 else
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000406 SD.setFileSize(Address - StartAddress);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000407}
408
Daniel Dunbar53b23382010-03-19 09:28:59 +0000409/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000410static void WriteFragmentData(const MCAssembler &Asm, const MCFragment &F,
411 MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000412 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000413 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000414
Daniel Dunbarff547842010-03-23 23:47:14 +0000415 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000416
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000417 // FIXME: Embed in fragments instead?
418 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000419 case MCFragment::FT_Align: {
420 MCAlignFragment &AF = cast<MCAlignFragment>(F);
421 uint64_t Count = AF.getFileSize() / AF.getValueSize();
422
423 // FIXME: This error shouldn't actually occur (the front end should emit
424 // multiple .align directives to enforce the semantics it wants), but is
425 // severe enough that we want to report it. How to handle this?
426 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000427 llvm_report_error("undefined .align directive, value size '" +
428 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000429 "' is not a divisor of padding size '" +
430 Twine(AF.getFileSize()) + "'");
431
Kevin Enderby6e720482010-02-23 18:26:34 +0000432 // See if we are aligning with nops, and if so do that first to try to fill
433 // the Count bytes. Then if that did not fill any bytes or there are any
434 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000435 // If we are aligning with nops, ask that target to emit the right data.
Kevin Enderby6e720482010-02-23 18:26:34 +0000436 if (AF.getEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000437 if (!Asm.getBackend().WriteNopData(Count, OW))
438 llvm_report_error("unable to write nop sequence of " +
439 Twine(Count) + " bytes");
440 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000441 }
442
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000443 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000444 for (uint64_t i = 0; i != Count; ++i) {
445 switch (AF.getValueSize()) {
446 default:
447 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000448 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
449 case 2: OW->Write16(uint16_t(AF.getValue())); break;
450 case 4: OW->Write32(uint32_t(AF.getValue())); break;
451 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000452 }
453 }
454 break;
455 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000456
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000457 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000458 MCDataFragment &DF = cast<MCDataFragment>(F);
459 assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!");
460 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000461 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000462 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000463
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000464 case MCFragment::FT_Fill: {
465 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000466 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
467 switch (FF.getValueSize()) {
468 default:
469 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000470 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
471 case 2: OW->Write16(uint16_t(FF.getValue())); break;
472 case 4: OW->Write32(uint32_t(FF.getValue())); break;
473 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000474 }
475 }
476 break;
477 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000478
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000479 case MCFragment::FT_Inst:
480 llvm_unreachable("unexpected inst fragment after lowering");
481 break;
482
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000483 case MCFragment::FT_Org: {
484 MCOrgFragment &OF = cast<MCOrgFragment>(F);
485
486 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000487 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000488
489 break;
490 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000491
492 case MCFragment::FT_ZeroFill: {
493 assert(0 && "Invalid zero fill fragment in concrete section!");
494 break;
495 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000496 }
497
Daniel Dunbar53b23382010-03-19 09:28:59 +0000498 assert(OW->getStream().tell() - Start == F.getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000499}
500
Daniel Dunbar53b23382010-03-19 09:28:59 +0000501void MCAssembler::WriteSectionData(const MCSectionData *SD,
502 MCObjectWriter *OW) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000503 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000504 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000505 assert(SD->getFileSize() == 0);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000506 return;
507 }
508
Daniel Dunbar53b23382010-03-19 09:28:59 +0000509 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000510 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000511
Daniel Dunbar53b23382010-03-19 09:28:59 +0000512 for (MCSectionData::const_iterator it = SD->begin(),
513 ie = SD->end(); it != ie; ++it)
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000514 WriteFragmentData(*this, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000515
Daniel Dunbar6742e342009-08-26 04:13:32 +0000516 // Add section padding.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000517 assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
518 OW->WriteZeros(SD->getFileSize() - SD->getSize());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000519
Daniel Dunbar53b23382010-03-19 09:28:59 +0000520 assert(OW->getStream().tell() - Start == SD->getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000521}
522
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000523void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000524 DEBUG_WITH_TYPE("mc-dump", {
525 llvm::errs() << "assembler backend - pre-layout\n--\n";
526 dump(); });
527
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000528 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000529 MCAsmLayout Layout(*this);
530 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000531 continue;
532
533 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000534 llvm::errs() << "assembler backend - post-relaxation\n--\n";
535 dump(); });
536
537 // Finalize the layout, including fragment lowering.
538 FinishLayout(Layout);
539
540 DEBUG_WITH_TYPE("mc-dump", {
541 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000542 dump(); });
543
Daniel Dunbarff547842010-03-23 23:47:14 +0000544 uint64_t StartOffset = OS.tell();
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000545 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
546 if (!Writer)
547 llvm_report_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000548
549 // Allow the object writer a chance to perform post-layout binding (for
550 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000551 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000552
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000553 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000554 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
555 for (MCSectionData::iterator it2 = it->begin(),
556 ie2 = it->end(); it2 != ie2; ++it2) {
557 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
558 if (!DF)
559 continue;
560
561 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
562 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
563 MCAsmFixup &Fixup = *it3;
564
565 // Evaluate the fixup.
566 MCValue Target;
567 uint64_t FixedValue;
568 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
569 // The fixup was unresolved, we need a relocation. Inform the object
570 // writer of the relocation, and give it an opportunity to adjust the
571 // fixup value if need be.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000572 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000573 }
574
Daniel Dunbar87190c42010-03-19 09:28:12 +0000575 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000576 }
577 }
578 }
579
Daniel Dunbarbacba992010-03-19 07:09:33 +0000580 // Write the object file.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000581 Writer->WriteObject(*this, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000582 OS.flush();
Daniel Dunbarff547842010-03-23 23:47:14 +0000583
584 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000585}
586
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000587bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
588 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000589 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000590 // If we cannot resolve the fixup value, it requires relaxation.
591 MCValue Target;
592 uint64_t Value;
593 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
594 return true;
595
596 // Otherwise, relax if the value is too big for a (signed) i8.
597 return int64_t(Value) != int64_t(int8_t(Value));
598}
599
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000600bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
601 const MCAsmLayout &Layout) const {
602 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
603 // are intentionally pushing out inst fragments, or because we relaxed a
604 // previous instruction to one that doesn't need relaxation.
605 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
606 return false;
607
608 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
609 ie = IF->fixup_end(); it != ie; ++it)
610 if (FixupNeedsRelaxation(*it, IF, Layout))
611 return true;
612
613 return false;
614}
615
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000616bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000617 ++stats::RelaxationSteps;
618
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000619 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000620 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000621 MCSectionData *Prev = 0;
622 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000623 MCSectionData &SD = *it;
624
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000625 // Skip virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000626 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000627 continue;
628
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000629 // Align this section if necessary by adding padding bytes to the previous
630 // section.
631 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
632 assert(Prev && "Missing prev section!");
633 Prev->setFileSize(Prev->getFileSize() + Pad);
634 Address += Pad;
635 }
Daniel Dunbar6742e342009-08-26 04:13:32 +0000636
637 // Layout the section fragments and its size.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000638 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000639 LayoutSection(SD, Layout);
Daniel Dunbar6742e342009-08-26 04:13:32 +0000640 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000641
642 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +0000643 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000644
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000645 // Layout the virtual sections.
646 for (iterator it = begin(), ie = end(); it != ie; ++it) {
647 MCSectionData &SD = *it;
648
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000649 if (!getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000650 continue;
651
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000652 // Align this section if necessary by adding padding bytes to the previous
653 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +0000654 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000655 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000656
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000657 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000658 LayoutSection(SD, Layout);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000659 Address += SD.getSize();
660 }
661
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000662 // Scan for fragments that need relaxation.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000663 for (iterator it = begin(), ie = end(); it != ie; ++it) {
664 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000665
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000666 for (MCSectionData::iterator it2 = SD.begin(),
667 ie2 = SD.end(); it2 != ie2; ++it2) {
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000668 // Check if this is an instruction fragment that needs relaxation.
669 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
670 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000671 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000672
Daniel Dunbarff547842010-03-23 23:47:14 +0000673 ++stats::RelaxedInstructions;
674
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000675 // FIXME-PERF: We could immediately lower out instructions if we can tell
676 // they are fully resolved, to avoid retesting on later passes.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000677
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000678 // Relax the fragment.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000679
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000680 MCInst Relaxed;
681 getBackend().RelaxInstruction(IF, Relaxed);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000682
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000683 // Encode the new instruction.
684 //
685 // FIXME-PERF: If it matters, we could let the target do this. It can
686 // probably do so more efficiently in many cases.
687 SmallVector<MCFixup, 4> Fixups;
688 SmallString<256> Code;
689 raw_svector_ostream VecOS(Code);
690 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
691 VecOS.flush();
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000692
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000693 // Update the instruction fragment.
694 IF->setInst(Relaxed);
695 IF->getCode() = Code;
696 IF->getFixups().clear();
697 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
698 MCFixup &F = Fixups[i];
699 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
700 F.getKind()));
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000701 }
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000702
703 // Restart layout.
704 //
705 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
706 // smart MCAsmLayout object.
707 return true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000708 }
709 }
710
711 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000712}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000713
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000714void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
715 // Lower out any instruction fragments, to simplify the fixup application and
716 // output.
717 //
718 // FIXME-PERF: We don't have to do this, but the assumption is that it is
719 // cheap (we will mostly end up eliminating fragments and appending on to data
720 // fragments), so the extra complexity downstream isn't worth it. Evaluate
721 // this assumption.
722 for (iterator it = begin(), ie = end(); it != ie; ++it) {
723 MCSectionData &SD = *it;
724
725 for (MCSectionData::iterator it2 = SD.begin(),
726 ie2 = SD.end(); it2 != ie2; ++it2) {
727 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
728 if (!IF)
729 continue;
730
731 // Create a new data fragment for the instruction.
732 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000733 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000734 MCDataFragment *DF = new MCDataFragment();
735 SD.getFragmentList().insert(it2, DF);
736
737 // Update the data fragments layout data.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000738 DF->setParent(IF->getParent());
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000739 DF->setOffset(IF->getOffset());
740 DF->setFileSize(IF->getInstSize());
741
Daniel Dunbar9799de92010-03-23 01:39:05 +0000742 // Copy in the data and the fixups.
743 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
744 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
745 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000746
747 // Delete the instruction fragment and update the iterator.
748 SD.getFragmentList().erase(IF);
749 it2 = DF;
750 }
751 }
752}
753
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000754// Debugging methods
755
756namespace llvm {
757
758raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000759 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
760 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000761 return OS;
762}
763
764}
765
766void MCFragment::dump() {
767 raw_ostream &OS = llvm::errs();
768
769 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
770 << " FileSize:" << FileSize;
771
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000772 OS << ">";
773}
774
775void MCAlignFragment::dump() {
776 raw_ostream &OS = llvm::errs();
777
778 OS << "<MCAlignFragment ";
779 this->MCFragment::dump();
780 OS << "\n ";
781 OS << " Alignment:" << getAlignment()
782 << " Value:" << getValue() << " ValueSize:" << getValueSize()
783 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
784}
785
786void MCDataFragment::dump() {
787 raw_ostream &OS = llvm::errs();
788
789 OS << "<MCDataFragment ";
790 this->MCFragment::dump();
791 OS << "\n ";
792 OS << " Contents:[";
793 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
794 if (i) OS << ",";
795 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
796 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000797 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000798
799 if (!getFixups().empty()) {
800 OS << ",\n ";
801 OS << " Fixups:[";
802 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000803 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000804 OS << *it;
805 }
806 OS << "]";
807 }
808
809 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000810}
811
812void MCFillFragment::dump() {
813 raw_ostream &OS = llvm::errs();
814
815 OS << "<MCFillFragment ";
816 this->MCFragment::dump();
817 OS << "\n ";
818 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
819 << " Count:" << getCount() << ">";
820}
821
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000822void MCInstFragment::dump() {
823 raw_ostream &OS = llvm::errs();
824
825 OS << "<MCInstFragment ";
826 this->MCFragment::dump();
827 OS << "\n ";
828 OS << " Inst:";
829 getInst().dump_pretty(OS);
830 OS << ">";
831}
832
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000833void MCOrgFragment::dump() {
834 raw_ostream &OS = llvm::errs();
835
836 OS << "<MCOrgFragment ";
837 this->MCFragment::dump();
838 OS << "\n ";
839 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
840}
841
842void MCZeroFillFragment::dump() {
843 raw_ostream &OS = llvm::errs();
844
845 OS << "<MCZeroFillFragment ";
846 this->MCFragment::dump();
847 OS << "\n ";
848 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
849}
850
851void MCSectionData::dump() {
852 raw_ostream &OS = llvm::errs();
853
854 OS << "<MCSectionData";
855 OS << " Alignment:" << getAlignment() << " Address:" << Address
856 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000857 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000858 for (iterator it = begin(), ie = end(); it != ie; ++it) {
859 if (it != begin()) OS << ",\n ";
860 it->dump();
861 }
862 OS << "]>";
863}
864
865void MCSymbolData::dump() {
866 raw_ostream &OS = llvm::errs();
867
868 OS << "<MCSymbolData Symbol:" << getSymbol()
869 << " Fragment:" << getFragment() << " Offset:" << getOffset()
870 << " Flags:" << getFlags() << " Index:" << getIndex();
871 if (isCommon())
872 OS << " (common, size:" << getCommonSize()
873 << " align: " << getCommonAlignment() << ")";
874 if (isExternal())
875 OS << " (external)";
876 if (isPrivateExtern())
877 OS << " (private extern)";
878 OS << ">";
879}
880
881void MCAssembler::dump() {
882 raw_ostream &OS = llvm::errs();
883
884 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000885 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000886 for (iterator it = begin(), ie = end(); it != ie; ++it) {
887 if (it != begin()) OS << ",\n ";
888 it->dump();
889 }
890 OS << "],\n";
891 OS << " Symbols:[";
892
893 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000894 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000895 it->dump();
896 }
897 OS << "]>\n";
898}