blob: a168abc02ec19508c1147d6a797beb1a07e205ec [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 Dunbar0705fbf2009-08-21 18:29:01 +000048MCFragment::MCFragment() : Kind(FragmentType(~0)) {
49}
50
Daniel Dunbar5e835962009-08-26 02:48:04 +000051MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000052 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +000053 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000054 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000055{
Daniel Dunbar5e835962009-08-26 02:48:04 +000056 if (Parent)
57 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000058}
59
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000060MCFragment::~MCFragment() {
61}
62
Daniel Dunbar5e835962009-08-26 02:48:04 +000063uint64_t MCFragment::getAddress() const {
64 assert(getParent() && "Missing Section!");
65 return getParent()->getAddress() + Offset;
66}
67
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000068/* *** */
69
Daniel Dunbar81e40002009-08-27 00:38:04 +000070MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000071
72MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +000073 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000074 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +000075 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +000076 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +000077 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +000078 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000079{
80 if (A)
81 A->getSectionList().push_back(this);
82}
83
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000084/* *** */
85
Daniel Dunbarefbb5332009-09-01 04:09:03 +000086MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000087
Daniel Dunbarcb579b32009-08-31 08:08:06 +000088MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000089 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +000090 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000091 IsExternal(false), IsPrivateExtern(false),
92 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000093{
94 if (A)
95 A->getSymbolList().push_back(this);
96}
97
98/* *** */
99
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000100MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000101 MCCodeEmitter &_Emitter, raw_ostream &_OS)
102 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
103 OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000104{
105}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000106
107MCAssembler::~MCAssembler() {
108}
109
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000110static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
111 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000112 const MCValue Target,
113 const MCSection *BaseSection) {
114 // The effective fixup address is
115 // addr(atom(A)) + offset(A)
116 // - addr(atom(B)) - offset(B)
117 // - addr(<base symbol>) + <fixup offset from base symbol>
118 // and the offsets are not relocatable, so the fixup is fully resolved when
119 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
120 //
121 // The simple (Darwin, except on x86_64) way of dealing with this was to
122 // assume that any reference to a temporary symbol *must* be a temporary
123 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
124 // relocation to a temporary symbol (in the same section) is fully
125 // resolved. This also works in conjunction with absolutized .set, which
126 // requires the compiler to use .set to absolutize the differences between
127 // symbols which the compiler knows to be assembly time constants, so we don't
128 // need to worry about consider symbol differences fully resolved.
129
130 // Non-relative fixups are only resolved if constant.
131 if (!BaseSection)
132 return Target.isAbsolute();
133
134 // Otherwise, relative fixups are only resolved if not a difference and the
135 // target is a temporary in the same section.
136 if (Target.isAbsolute() || Target.getSymB())
137 return false;
138
139 const MCSymbol *A = &Target.getSymA()->getSymbol();
140 if (!A->isTemporary() || !A->isInSection() ||
141 &A->getSection() != BaseSection)
142 return false;
143
144 return true;
145}
146
Daniel Dunbar034843a2010-03-19 03:18:18 +0000147static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
148 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000149 const MCValue Target,
150 const MCSymbolData *BaseSymbol) {
151 // The effective fixup address is
152 // addr(atom(A)) + offset(A)
153 // - addr(atom(B)) - offset(B)
154 // - addr(BaseSymbol) + <fixup offset from base symbol>
155 // and the offsets are not relocatable, so the fixup is fully resolved when
156 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
157 //
158 // Note that "false" is almost always conservatively correct (it means we emit
159 // a relocation which is unnecessary), except when it would force us to emit a
160 // relocation which the target cannot encode.
161
162 const MCSymbolData *A_Base = 0, *B_Base = 0;
163 if (const MCSymbolRefExpr *A = Target.getSymA()) {
164 // Modified symbol references cannot be resolved.
165 if (A->getKind() != MCSymbolRefExpr::VK_None)
166 return false;
167
168 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
169 if (!A_Base)
170 return false;
171 }
172
173 if (const MCSymbolRefExpr *B = Target.getSymB()) {
174 // Modified symbol references cannot be resolved.
175 if (B->getKind() != MCSymbolRefExpr::VK_None)
176 return false;
177
178 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
179 if (!B_Base)
180 return false;
181 }
182
183 // If there is no base, A and B have to be the same atom for this fixup to be
184 // fully resolved.
185 if (!BaseSymbol)
186 return A_Base == B_Base;
187
188 // Otherwise, B must be missing and A must be the base.
189 return !B_Base && BaseSymbol == A_Base;
190}
191
Daniel Dunbar23869852010-03-19 03:18:09 +0000192bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
193 // Non-temporary labels should always be visible to the linker.
194 if (!SD->getSymbol().isTemporary())
195 return true;
196
197 // Absolute temporary labels are never visible.
198 if (!SD->getFragment())
199 return false;
200
201 // Otherwise, check if the section requires symbols even for temporary labels.
202 return getBackend().doesSectionRequireSymbols(
203 SD->getFragment()->getParent()->getSection());
204}
205
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000206const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section,
207 uint64_t Address) const {
208 const MCSymbolData *Best = 0;
209 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
210 ie = symbol_end(); it != ie; ++it) {
211 // Ignore non-linker visible symbols.
212 if (!isSymbolLinkerVisible(it))
213 continue;
214
215 // Ignore symbols not in the same section.
216 if (!it->getFragment() || it->getFragment()->getParent() != Section)
217 continue;
218
219 // Otherwise, find the closest symbol preceding this address (ties are
220 // resolved in favor of the last defined symbol).
221 if (it->getAddress() <= Address &&
222 (!Best || it->getAddress() >= Best->getAddress()))
223 Best = it;
224 }
225
226 return Best;
227}
228
229const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
230 // Linker visible symbols define atoms.
231 if (isSymbolLinkerVisible(SD))
232 return SD;
233
234 // Absolute and undefined symbols have no defining atom.
235 if (!SD->getFragment())
236 return 0;
237
238 // Otherwise, search by address.
239 return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress());
240}
241
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000242bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
243 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000244 MCValue &Target, uint64_t &Value) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000245 ++stats::EvaluateFixup;
246
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000247 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
248 llvm_report_error("expected relocatable expression");
249
250 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
251 // doesn't support small relocations, but then under what criteria does the
252 // assembler allow symbol differences?
253
254 Value = Target.getConstant();
255
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000256 bool IsPCRel =
257 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
258 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000259 if (const MCSymbolRefExpr *A = Target.getSymA()) {
260 if (A->getSymbol().isDefined())
261 Value += getSymbolData(A->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000262 else
263 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000264 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000265 if (const MCSymbolRefExpr *B = Target.getSymB()) {
266 if (B->getSymbol().isDefined())
267 Value -= getSymbolData(B->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000268 else
269 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000270 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000271
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000272 // If we are using scattered symbols, determine whether this value is actually
273 // resolved; scattering may cause atoms to move.
274 if (IsResolved && getBackend().hasScatteredSymbols()) {
275 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000276 // If this is a PCrel relocation, find the base atom (identified by its
277 // symbol) that the fixup value is relative to.
278 const MCSymbolData *BaseSymbol = 0;
279 if (IsPCRel) {
280 BaseSymbol = getAtomForAddress(
281 DF->getParent(), DF->getAddress() + Fixup.Offset);
282 if (!BaseSymbol)
283 IsResolved = false;
284 }
285
286 if (IsResolved)
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000287 IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000288 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000289 } else {
290 const MCSection *BaseSection = 0;
291 if (IsPCRel)
292 BaseSection = &DF->getParent()->getSection();
293
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000294 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000295 BaseSection);
296 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000297 }
298
299 if (IsPCRel)
Daniel Dunbarda3e9f72010-03-13 02:38:00 +0000300 Value -= DF->getAddress() + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000301
302 return IsResolved;
303}
304
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000305void MCAssembler::LayoutSection(MCSectionData &SD,
306 MCAsmLayout &Layout) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000307 uint64_t Address = SD.getAddress();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000308
309 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
310 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000311
Daniel Dunbar6742e342009-08-26 04:13:32 +0000312 F.setOffset(Address - SD.getAddress());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000313
314 // Evaluate fragment size.
315 switch (F.getKind()) {
316 case MCFragment::FT_Align: {
317 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000318
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000319 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000320 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000321 AF.setFileSize(0);
322 else
Daniel Dunbar6742e342009-08-26 04:13:32 +0000323 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000324 break;
325 }
326
327 case MCFragment::FT_Data:
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000328 F.setFileSize(cast<MCDataFragment>(F).getContents().size());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000329 break;
330
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000331 case MCFragment::FT_Fill: {
332 MCFillFragment &FF = cast<MCFillFragment>(F);
333 F.setFileSize(FF.getValueSize() * FF.getCount());
334 break;
335 }
336
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000337 case MCFragment::FT_Inst:
338 F.setFileSize(cast<MCInstFragment>(F).getInstSize());
339 break;
340
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000341 case MCFragment::FT_Org: {
342 MCOrgFragment &OF = cast<MCOrgFragment>(F);
343
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000344 int64_t TargetLocation;
345 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
346 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000347
348 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000349 int64_t Offset = TargetLocation - F.getOffset();
350 if (Offset < 0)
351 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
352 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000353
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000354 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000355 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000356 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000357
358 case MCFragment::FT_ZeroFill: {
359 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
360
361 // Align the fragment offset; it is safe to adjust the offset freely since
362 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +0000363 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
364 F.setOffset(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000365
366 // FIXME: This is misnamed.
367 F.setFileSize(ZFF.getSize());
368 break;
369 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000370 }
371
Daniel Dunbar6742e342009-08-26 04:13:32 +0000372 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000373 }
374
Daniel Dunbar6742e342009-08-26 04:13:32 +0000375 // Set the section sizes.
376 SD.setSize(Address - SD.getAddress());
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000377 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000378 SD.setFileSize(0);
379 else
380 SD.setFileSize(Address - SD.getAddress());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000381}
382
Daniel Dunbar53b23382010-03-19 09:28:59 +0000383/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000384static void WriteFragmentData(const MCAssembler &Asm, const MCFragment &F,
385 MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000386 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000387 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000388
Daniel Dunbarff547842010-03-23 23:47:14 +0000389 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000390
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000391 // FIXME: Embed in fragments instead?
392 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000393 case MCFragment::FT_Align: {
394 MCAlignFragment &AF = cast<MCAlignFragment>(F);
395 uint64_t Count = AF.getFileSize() / AF.getValueSize();
396
397 // FIXME: This error shouldn't actually occur (the front end should emit
398 // multiple .align directives to enforce the semantics it wants), but is
399 // severe enough that we want to report it. How to handle this?
400 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000401 llvm_report_error("undefined .align directive, value size '" +
402 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000403 "' is not a divisor of padding size '" +
404 Twine(AF.getFileSize()) + "'");
405
Kevin Enderby6e720482010-02-23 18:26:34 +0000406 // See if we are aligning with nops, and if so do that first to try to fill
407 // the Count bytes. Then if that did not fill any bytes or there are any
408 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000409 // If we are aligning with nops, ask that target to emit the right data.
Kevin Enderby6e720482010-02-23 18:26:34 +0000410 if (AF.getEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000411 if (!Asm.getBackend().WriteNopData(Count, OW))
412 llvm_report_error("unable to write nop sequence of " +
413 Twine(Count) + " bytes");
414 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000415 }
416
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000417 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000418 for (uint64_t i = 0; i != Count; ++i) {
419 switch (AF.getValueSize()) {
420 default:
421 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000422 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
423 case 2: OW->Write16(uint16_t(AF.getValue())); break;
424 case 4: OW->Write32(uint32_t(AF.getValue())); break;
425 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000426 }
427 }
428 break;
429 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000430
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000431 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000432 MCDataFragment &DF = cast<MCDataFragment>(F);
433 assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!");
434 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000435 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000436 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000437
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000438 case MCFragment::FT_Fill: {
439 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000440 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
441 switch (FF.getValueSize()) {
442 default:
443 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000444 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
445 case 2: OW->Write16(uint16_t(FF.getValue())); break;
446 case 4: OW->Write32(uint32_t(FF.getValue())); break;
447 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000448 }
449 }
450 break;
451 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000452
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000453 case MCFragment::FT_Inst:
454 llvm_unreachable("unexpected inst fragment after lowering");
455 break;
456
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000457 case MCFragment::FT_Org: {
458 MCOrgFragment &OF = cast<MCOrgFragment>(F);
459
460 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000461 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000462
463 break;
464 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000465
466 case MCFragment::FT_ZeroFill: {
467 assert(0 && "Invalid zero fill fragment in concrete section!");
468 break;
469 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000470 }
471
Daniel Dunbar53b23382010-03-19 09:28:59 +0000472 assert(OW->getStream().tell() - Start == F.getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000473}
474
Daniel Dunbar53b23382010-03-19 09:28:59 +0000475void MCAssembler::WriteSectionData(const MCSectionData *SD,
476 MCObjectWriter *OW) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000477 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000478 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000479 assert(SD->getFileSize() == 0);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000480 return;
481 }
482
Daniel Dunbar53b23382010-03-19 09:28:59 +0000483 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000484 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000485
Daniel Dunbar53b23382010-03-19 09:28:59 +0000486 for (MCSectionData::const_iterator it = SD->begin(),
487 ie = SD->end(); it != ie; ++it)
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000488 WriteFragmentData(*this, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000489
Daniel Dunbar6742e342009-08-26 04:13:32 +0000490 // Add section padding.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000491 assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
492 OW->WriteZeros(SD->getFileSize() - SD->getSize());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000493
Daniel Dunbar53b23382010-03-19 09:28:59 +0000494 assert(OW->getStream().tell() - Start == SD->getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000495}
496
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000497void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000498 DEBUG_WITH_TYPE("mc-dump", {
499 llvm::errs() << "assembler backend - pre-layout\n--\n";
500 dump(); });
501
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000502 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000503 MCAsmLayout Layout(*this);
504 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000505 continue;
506
507 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000508 llvm::errs() << "assembler backend - post-relaxation\n--\n";
509 dump(); });
510
511 // Finalize the layout, including fragment lowering.
512 FinishLayout(Layout);
513
514 DEBUG_WITH_TYPE("mc-dump", {
515 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000516 dump(); });
517
Daniel Dunbarff547842010-03-23 23:47:14 +0000518 uint64_t StartOffset = OS.tell();
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000519 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
520 if (!Writer)
521 llvm_report_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000522
523 // Allow the object writer a chance to perform post-layout binding (for
524 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000525 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000526
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000527 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000528 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
529 for (MCSectionData::iterator it2 = it->begin(),
530 ie2 = it->end(); it2 != ie2; ++it2) {
531 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
532 if (!DF)
533 continue;
534
535 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
536 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
537 MCAsmFixup &Fixup = *it3;
538
539 // Evaluate the fixup.
540 MCValue Target;
541 uint64_t FixedValue;
542 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
543 // The fixup was unresolved, we need a relocation. Inform the object
544 // writer of the relocation, and give it an opportunity to adjust the
545 // fixup value if need be.
Daniel Dunbarb7514182010-03-22 20:35:50 +0000546 Writer->RecordRelocation(*this, DF, Fixup, Target, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000547 }
548
Daniel Dunbar87190c42010-03-19 09:28:12 +0000549 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000550 }
551 }
552 }
553
Daniel Dunbarbacba992010-03-19 07:09:33 +0000554 // Write the object file.
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000555 Writer->WriteObject(*this);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000556 OS.flush();
Daniel Dunbarff547842010-03-23 23:47:14 +0000557
558 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000559}
560
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000561bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
562 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000563 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000564 // If we cannot resolve the fixup value, it requires relaxation.
565 MCValue Target;
566 uint64_t Value;
567 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
568 return true;
569
570 // Otherwise, relax if the value is too big for a (signed) i8.
571 return int64_t(Value) != int64_t(int8_t(Value));
572}
573
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000574bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
575 const MCAsmLayout &Layout) const {
576 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
577 // are intentionally pushing out inst fragments, or because we relaxed a
578 // previous instruction to one that doesn't need relaxation.
579 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
580 return false;
581
582 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
583 ie = IF->fixup_end(); it != ie; ++it)
584 if (FixupNeedsRelaxation(*it, IF, Layout))
585 return true;
586
587 return false;
588}
589
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000590bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000591 ++stats::RelaxationSteps;
592
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000593 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000594 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000595 MCSectionData *Prev = 0;
596 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000597 MCSectionData &SD = *it;
598
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000599 // Skip virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000600 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000601 continue;
602
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000603 // Align this section if necessary by adding padding bytes to the previous
604 // section.
605 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
606 assert(Prev && "Missing prev section!");
607 Prev->setFileSize(Prev->getFileSize() + Pad);
608 Address += Pad;
609 }
Daniel Dunbar6742e342009-08-26 04:13:32 +0000610
611 // Layout the section fragments and its size.
612 SD.setAddress(Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000613 LayoutSection(SD, Layout);
Daniel Dunbar6742e342009-08-26 04:13:32 +0000614 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000615
616 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +0000617 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000618
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000619 // Layout the virtual sections.
620 for (iterator it = begin(), ie = end(); it != ie; ++it) {
621 MCSectionData &SD = *it;
622
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000623 if (!getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000624 continue;
625
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000626 // Align this section if necessary by adding padding bytes to the previous
627 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +0000628 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000629 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000630
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000631 SD.setAddress(Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000632 LayoutSection(SD, Layout);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000633 Address += SD.getSize();
634 }
635
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000636 // Scan for fragments that need relaxation.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000637 for (iterator it = begin(), ie = end(); it != ie; ++it) {
638 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000639
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000640 for (MCSectionData::iterator it2 = SD.begin(),
641 ie2 = SD.end(); it2 != ie2; ++it2) {
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000642 // Check if this is an instruction fragment that needs relaxation.
643 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
644 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000645 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000646
Daniel Dunbarff547842010-03-23 23:47:14 +0000647 ++stats::RelaxedInstructions;
648
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000649 // FIXME-PERF: We could immediately lower out instructions if we can tell
650 // they are fully resolved, to avoid retesting on later passes.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000651
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000652 // Relax the fragment.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000653
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000654 MCInst Relaxed;
655 getBackend().RelaxInstruction(IF, Relaxed);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000656
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000657 // Encode the new instruction.
658 //
659 // FIXME-PERF: If it matters, we could let the target do this. It can
660 // probably do so more efficiently in many cases.
661 SmallVector<MCFixup, 4> Fixups;
662 SmallString<256> Code;
663 raw_svector_ostream VecOS(Code);
664 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
665 VecOS.flush();
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000666
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000667 // Update the instruction fragment.
668 IF->setInst(Relaxed);
669 IF->getCode() = Code;
670 IF->getFixups().clear();
671 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
672 MCFixup &F = Fixups[i];
673 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
674 F.getKind()));
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000675 }
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000676
677 // Restart layout.
678 //
679 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
680 // smart MCAsmLayout object.
681 return true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000682 }
683 }
684
685 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000686}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000687
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000688void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
689 // Lower out any instruction fragments, to simplify the fixup application and
690 // output.
691 //
692 // FIXME-PERF: We don't have to do this, but the assumption is that it is
693 // cheap (we will mostly end up eliminating fragments and appending on to data
694 // fragments), so the extra complexity downstream isn't worth it. Evaluate
695 // this assumption.
696 for (iterator it = begin(), ie = end(); it != ie; ++it) {
697 MCSectionData &SD = *it;
698
699 for (MCSectionData::iterator it2 = SD.begin(),
700 ie2 = SD.end(); it2 != ie2; ++it2) {
701 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
702 if (!IF)
703 continue;
704
705 // Create a new data fragment for the instruction.
706 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000707 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000708 MCDataFragment *DF = new MCDataFragment();
709 SD.getFragmentList().insert(it2, DF);
710
711 // Update the data fragments layout data.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000712 DF->setParent(IF->getParent());
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000713 DF->setOffset(IF->getOffset());
714 DF->setFileSize(IF->getInstSize());
715
Daniel Dunbar9799de92010-03-23 01:39:05 +0000716 // Copy in the data and the fixups.
717 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
718 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
719 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000720
721 // Delete the instruction fragment and update the iterator.
722 SD.getFragmentList().erase(IF);
723 it2 = DF;
724 }
725 }
726}
727
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000728// Debugging methods
729
730namespace llvm {
731
732raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000733 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
734 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000735 return OS;
736}
737
738}
739
740void MCFragment::dump() {
741 raw_ostream &OS = llvm::errs();
742
743 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
744 << " FileSize:" << FileSize;
745
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000746 OS << ">";
747}
748
749void MCAlignFragment::dump() {
750 raw_ostream &OS = llvm::errs();
751
752 OS << "<MCAlignFragment ";
753 this->MCFragment::dump();
754 OS << "\n ";
755 OS << " Alignment:" << getAlignment()
756 << " Value:" << getValue() << " ValueSize:" << getValueSize()
757 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
758}
759
760void MCDataFragment::dump() {
761 raw_ostream &OS = llvm::errs();
762
763 OS << "<MCDataFragment ";
764 this->MCFragment::dump();
765 OS << "\n ";
766 OS << " Contents:[";
767 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
768 if (i) OS << ",";
769 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
770 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000771 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000772
773 if (!getFixups().empty()) {
774 OS << ",\n ";
775 OS << " Fixups:[";
776 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000777 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000778 OS << *it;
779 }
780 OS << "]";
781 }
782
783 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000784}
785
786void MCFillFragment::dump() {
787 raw_ostream &OS = llvm::errs();
788
789 OS << "<MCFillFragment ";
790 this->MCFragment::dump();
791 OS << "\n ";
792 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
793 << " Count:" << getCount() << ">";
794}
795
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000796void MCInstFragment::dump() {
797 raw_ostream &OS = llvm::errs();
798
799 OS << "<MCInstFragment ";
800 this->MCFragment::dump();
801 OS << "\n ";
802 OS << " Inst:";
803 getInst().dump_pretty(OS);
804 OS << ">";
805}
806
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000807void MCOrgFragment::dump() {
808 raw_ostream &OS = llvm::errs();
809
810 OS << "<MCOrgFragment ";
811 this->MCFragment::dump();
812 OS << "\n ";
813 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
814}
815
816void MCZeroFillFragment::dump() {
817 raw_ostream &OS = llvm::errs();
818
819 OS << "<MCZeroFillFragment ";
820 this->MCFragment::dump();
821 OS << "\n ";
822 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
823}
824
825void MCSectionData::dump() {
826 raw_ostream &OS = llvm::errs();
827
828 OS << "<MCSectionData";
829 OS << " Alignment:" << getAlignment() << " Address:" << Address
830 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000831 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000832 for (iterator it = begin(), ie = end(); it != ie; ++it) {
833 if (it != begin()) OS << ",\n ";
834 it->dump();
835 }
836 OS << "]>";
837}
838
839void MCSymbolData::dump() {
840 raw_ostream &OS = llvm::errs();
841
842 OS << "<MCSymbolData Symbol:" << getSymbol()
843 << " Fragment:" << getFragment() << " Offset:" << getOffset()
844 << " Flags:" << getFlags() << " Index:" << getIndex();
845 if (isCommon())
846 OS << " (common, size:" << getCommonSize()
847 << " align: " << getCommonAlignment() << ")";
848 if (isExternal())
849 OS << " (external)";
850 if (isPrivateExtern())
851 OS << " (private extern)";
852 OS << ">";
853}
854
855void MCAssembler::dump() {
856 raw_ostream &OS = llvm::errs();
857
858 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000859 OS << " Sections:[\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 << "],\n";
865 OS << " Symbols:[";
866
867 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000868 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000869 it->dump();
870 }
871 OS << "]>\n";
872}