blob: c8d3c845f285ff44eeb7aeeaf22ed09bafda5f35 [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()!");
Daniel Dunbar432cd5f2010-03-25 02:00:02 +000050 return getSectionAddress(F->getParent()) + getFragmentOffset(F);
51}
52
53uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
54 assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
55 return F->EffectiveSize;
56}
57
58void MCAsmLayout::setFragmentEffectiveSize(MCFragment *F, uint64_t Value) {
59 F->EffectiveSize = Value;
60}
61
62uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
63 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
64 return F->Offset;
65}
66
67void MCAsmLayout::setFragmentOffset(MCFragment *F, uint64_t Value) {
68 F->Offset = Value;
Daniel Dunbar207e06e2010-03-24 03:43:40 +000069}
70
71uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000072 assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
73 return getFragmentAddress(SD->getFragment()) + SD->getOffset();
Daniel Dunbar207e06e2010-03-24 03:43:40 +000074}
75
76uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000077 assert(SD->Address != ~UINT64_C(0) && "Address not set!");
78 return SD->Address;
Daniel Dunbar207e06e2010-03-24 03:43:40 +000079}
80
81void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000082 SD->Address = Value;
Daniel Dunbar207e06e2010-03-24 03:43:40 +000083}
84
85/* *** */
86
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000087MCFragment::MCFragment() : Kind(FragmentType(~0)) {
88}
89
Daniel Dunbar5e835962009-08-26 02:48:04 +000090MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000091 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +000092 Parent(_Parent),
Daniel Dunbar432cd5f2010-03-25 02:00:02 +000093 EffectiveSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000094{
Daniel Dunbar5e835962009-08-26 02:48:04 +000095 if (Parent)
96 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000097}
98
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000099MCFragment::~MCFragment() {
100}
101
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000102/* *** */
103
Daniel Dunbar81e40002009-08-27 00:38:04 +0000104MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000105
106MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000107 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000108 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000109 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +0000110 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000111 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000112 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000113{
114 if (A)
115 A->getSectionList().push_back(this);
116}
117
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000118/* *** */
119
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000120MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000121
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000122MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000123 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000124 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000125 IsExternal(false), IsPrivateExtern(false),
126 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000127{
128 if (A)
129 A->getSymbolList().push_back(this);
130}
131
132/* *** */
133
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000134MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000135 MCCodeEmitter &_Emitter, raw_ostream &_OS)
136 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
137 OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000138{
139}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000140
141MCAssembler::~MCAssembler() {
142}
143
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000144static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
145 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000146 const MCValue Target,
147 const MCSection *BaseSection) {
148 // The effective fixup address is
149 // addr(atom(A)) + offset(A)
150 // - addr(atom(B)) - offset(B)
151 // - addr(<base symbol>) + <fixup offset from base symbol>
152 // and the offsets are not relocatable, so the fixup is fully resolved when
153 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
154 //
155 // The simple (Darwin, except on x86_64) way of dealing with this was to
156 // assume that any reference to a temporary symbol *must* be a temporary
157 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
158 // relocation to a temporary symbol (in the same section) is fully
159 // resolved. This also works in conjunction with absolutized .set, which
160 // requires the compiler to use .set to absolutize the differences between
161 // symbols which the compiler knows to be assembly time constants, so we don't
162 // need to worry about consider symbol differences fully resolved.
163
164 // Non-relative fixups are only resolved if constant.
165 if (!BaseSection)
166 return Target.isAbsolute();
167
168 // Otherwise, relative fixups are only resolved if not a difference and the
169 // target is a temporary in the same section.
170 if (Target.isAbsolute() || Target.getSymB())
171 return false;
172
173 const MCSymbol *A = &Target.getSymA()->getSymbol();
174 if (!A->isTemporary() || !A->isInSection() ||
175 &A->getSection() != BaseSection)
176 return false;
177
178 return true;
179}
180
Daniel Dunbar034843a2010-03-19 03:18:18 +0000181static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000182 const MCAsmLayout &Layout,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000183 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000184 const MCValue Target,
185 const MCSymbolData *BaseSymbol) {
186 // The effective fixup address is
187 // addr(atom(A)) + offset(A)
188 // - addr(atom(B)) - offset(B)
189 // - addr(BaseSymbol) + <fixup offset from base symbol>
190 // and the offsets are not relocatable, so the fixup is fully resolved when
191 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
192 //
193 // Note that "false" is almost always conservatively correct (it means we emit
194 // a relocation which is unnecessary), except when it would force us to emit a
195 // relocation which the target cannot encode.
196
197 const MCSymbolData *A_Base = 0, *B_Base = 0;
198 if (const MCSymbolRefExpr *A = Target.getSymA()) {
199 // Modified symbol references cannot be resolved.
200 if (A->getKind() != MCSymbolRefExpr::VK_None)
201 return false;
202
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000203 A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000204 if (!A_Base)
205 return false;
206 }
207
208 if (const MCSymbolRefExpr *B = Target.getSymB()) {
209 // Modified symbol references cannot be resolved.
210 if (B->getKind() != MCSymbolRefExpr::VK_None)
211 return false;
212
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000213 B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000214 if (!B_Base)
215 return false;
216 }
217
218 // If there is no base, A and B have to be the same atom for this fixup to be
219 // fully resolved.
220 if (!BaseSymbol)
221 return A_Base == B_Base;
222
223 // Otherwise, B must be missing and A must be the base.
224 return !B_Base && BaseSymbol == A_Base;
225}
226
Daniel Dunbar23869852010-03-19 03:18:09 +0000227bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
228 // Non-temporary labels should always be visible to the linker.
229 if (!SD->getSymbol().isTemporary())
230 return true;
231
232 // Absolute temporary labels are never visible.
233 if (!SD->getFragment())
234 return false;
235
236 // Otherwise, check if the section requires symbols even for temporary labels.
237 return getBackend().doesSectionRequireSymbols(
238 SD->getFragment()->getParent()->getSection());
239}
240
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000241// FIXME-PERF: This routine is really slow.
242const MCSymbolData *MCAssembler::getAtomForAddress(const MCAsmLayout &Layout,
243 const MCSectionData *Section,
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000244 uint64_t Address) const {
245 const MCSymbolData *Best = 0;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000246 uint64_t BestAddress = 0;
247
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000248 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
249 ie = symbol_end(); it != ie; ++it) {
250 // Ignore non-linker visible symbols.
251 if (!isSymbolLinkerVisible(it))
252 continue;
253
254 // Ignore symbols not in the same section.
255 if (!it->getFragment() || it->getFragment()->getParent() != Section)
256 continue;
257
258 // Otherwise, find the closest symbol preceding this address (ties are
259 // resolved in favor of the last defined symbol).
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000260 uint64_t SymbolAddress = Layout.getSymbolAddress(it);
261 if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000262 Best = it;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000263 BestAddress = SymbolAddress;
264 }
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000265 }
266
267 return Best;
268}
269
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000270// FIXME-PERF: This routine is really slow.
271const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout,
272 const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000273 // Linker visible symbols define atoms.
274 if (isSymbolLinkerVisible(SD))
275 return SD;
276
277 // Absolute and undefined symbols have no defining atom.
278 if (!SD->getFragment())
279 return 0;
280
281 // Otherwise, search by address.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000282 return getAtomForAddress(Layout, SD->getFragment()->getParent(),
283 Layout.getSymbolAddress(SD));
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000284}
285
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000286bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
287 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000288 MCValue &Target, uint64_t &Value) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000289 ++stats::EvaluateFixup;
290
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000291 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
292 llvm_report_error("expected relocatable expression");
293
294 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
295 // doesn't support small relocations, but then under what criteria does the
296 // assembler allow symbol differences?
297
298 Value = Target.getConstant();
299
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000300 bool IsPCRel =
301 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
302 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000303 if (const MCSymbolRefExpr *A = Target.getSymA()) {
304 if (A->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000305 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000306 else
307 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000308 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000309 if (const MCSymbolRefExpr *B = Target.getSymB()) {
310 if (B->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000311 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000312 else
313 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000314 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000315
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000316 // If we are using scattered symbols, determine whether this value is actually
317 // resolved; scattering may cause atoms to move.
318 if (IsResolved && getBackend().hasScatteredSymbols()) {
319 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000320 // If this is a PCrel relocation, find the base atom (identified by its
321 // symbol) that the fixup value is relative to.
322 const MCSymbolData *BaseSymbol = 0;
323 if (IsPCRel) {
324 BaseSymbol = getAtomForAddress(
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000325 Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset);
Daniel Dunbar034843a2010-03-19 03:18:18 +0000326 if (!BaseSymbol)
327 IsResolved = false;
328 }
329
330 if (IsResolved)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000331 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000332 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000333 } else {
334 const MCSection *BaseSection = 0;
335 if (IsPCRel)
336 BaseSection = &DF->getParent()->getSection();
337
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000338 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000339 BaseSection);
340 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000341 }
342
343 if (IsPCRel)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000344 Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000345
346 return IsResolved;
347}
348
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000349void MCAssembler::LayoutSection(MCSectionData &SD,
350 MCAsmLayout &Layout) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000351 uint64_t Address, StartAddress = Address = Layout.getSectionAddress(&SD);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000352
353 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
354 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000355
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000356 uint64_t FragmentOffset = Address - StartAddress;
357 Layout.setFragmentOffset(&F, FragmentOffset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000358
359 // Evaluate fragment size.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000360 uint64_t EffectiveSize = 0;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000361 switch (F.getKind()) {
362 case MCFragment::FT_Align: {
363 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000364
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000365 EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
366 if (EffectiveSize > AF.getMaxBytesToEmit())
367 EffectiveSize = 0;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000368 break;
369 }
370
371 case MCFragment::FT_Data:
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000372 EffectiveSize = cast<MCDataFragment>(F).getContents().size();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000373 break;
374
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000375 case MCFragment::FT_Fill: {
376 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000377 EffectiveSize = FF.getValueSize() * FF.getCount();
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000378 break;
379 }
380
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000381 case MCFragment::FT_Inst:
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000382 EffectiveSize = cast<MCInstFragment>(F).getInstSize();
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000383 break;
384
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000385 case MCFragment::FT_Org: {
386 MCOrgFragment &OF = cast<MCOrgFragment>(F);
387
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000388 int64_t TargetLocation;
389 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
390 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000391
392 // FIXME: We need a way to communicate this error.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000393 int64_t Offset = TargetLocation - FragmentOffset;
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000394 if (Offset < 0)
395 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000396 "' (at offset '" + Twine(FragmentOffset) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000397
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000398 EffectiveSize = Offset;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000399 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000400 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000401
402 case MCFragment::FT_ZeroFill: {
403 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
404
405 // Align the fragment offset; it is safe to adjust the offset freely since
406 // this is only in virtual sections.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000407 //
408 // FIXME: We shouldn't be doing this here.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +0000409 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000410 Layout.setFragmentOffset(&F, Address - StartAddress);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000411
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000412 EffectiveSize = ZFF.getSize();
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000413 break;
414 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000415 }
416
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000417 Layout.setFragmentEffectiveSize(&F, EffectiveSize);
418 Address += EffectiveSize;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000419 }
420
Daniel Dunbar6742e342009-08-26 04:13:32 +0000421 // Set the section sizes.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000422 SD.setSize(Address - StartAddress);
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000423 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000424 SD.setFileSize(0);
425 else
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000426 SD.setFileSize(Address - StartAddress);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000427}
428
Daniel Dunbar53b23382010-03-19 09:28:59 +0000429/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000430static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
431 const MCFragment &F, MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000432 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000433 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000434
Daniel Dunbarff547842010-03-23 23:47:14 +0000435 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000436
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000437 // FIXME: Embed in fragments instead?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000438 uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000439 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000440 case MCFragment::FT_Align: {
441 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000442 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000443
444 // FIXME: This error shouldn't actually occur (the front end should emit
445 // multiple .align directives to enforce the semantics it wants), but is
446 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000447 if (Count * AF.getValueSize() != FragmentSize)
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000448 llvm_report_error("undefined .align directive, value size '" +
449 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000450 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000451 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000452
Kevin Enderby6e720482010-02-23 18:26:34 +0000453 // See if we are aligning with nops, and if so do that first to try to fill
454 // the Count bytes. Then if that did not fill any bytes or there are any
455 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000456 // If we are aligning with nops, ask that target to emit the right data.
Kevin Enderby6e720482010-02-23 18:26:34 +0000457 if (AF.getEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000458 if (!Asm.getBackend().WriteNopData(Count, OW))
459 llvm_report_error("unable to write nop sequence of " +
460 Twine(Count) + " bytes");
461 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000462 }
463
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000464 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000465 for (uint64_t i = 0; i != Count; ++i) {
466 switch (AF.getValueSize()) {
467 default:
468 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000469 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
470 case 2: OW->Write16(uint16_t(AF.getValue())); break;
471 case 4: OW->Write32(uint32_t(AF.getValue())); break;
472 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000473 }
474 }
475 break;
476 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000477
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000478 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000479 MCDataFragment &DF = cast<MCDataFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000480 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000481 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000482 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000483 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000484
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000485 case MCFragment::FT_Fill: {
486 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000487 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
488 switch (FF.getValueSize()) {
489 default:
490 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000491 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
492 case 2: OW->Write16(uint16_t(FF.getValue())); break;
493 case 4: OW->Write32(uint32_t(FF.getValue())); break;
494 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000495 }
496 }
497 break;
498 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000499
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000500 case MCFragment::FT_Inst:
501 llvm_unreachable("unexpected inst fragment after lowering");
502 break;
503
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000504 case MCFragment::FT_Org: {
505 MCOrgFragment &OF = cast<MCOrgFragment>(F);
506
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000507 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000508 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000509
510 break;
511 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000512
513 case MCFragment::FT_ZeroFill: {
514 assert(0 && "Invalid zero fill fragment in concrete section!");
515 break;
516 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000517 }
518
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000519 assert(OW->getStream().tell() - Start == FragmentSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000520}
521
Daniel Dunbar53b23382010-03-19 09:28:59 +0000522void MCAssembler::WriteSectionData(const MCSectionData *SD,
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000523 const MCAsmLayout &Layout,
Daniel Dunbar53b23382010-03-19 09:28:59 +0000524 MCObjectWriter *OW) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000525 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000526 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000527 assert(SD->getFileSize() == 0);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000528 return;
529 }
530
Daniel Dunbar53b23382010-03-19 09:28:59 +0000531 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000532 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000533
Daniel Dunbar53b23382010-03-19 09:28:59 +0000534 for (MCSectionData::const_iterator it = SD->begin(),
535 ie = SD->end(); it != ie; ++it)
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000536 WriteFragmentData(*this, Layout, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000537
Daniel Dunbar6742e342009-08-26 04:13:32 +0000538 // Add section padding.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000539 assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
540 OW->WriteZeros(SD->getFileSize() - SD->getSize());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000541
Daniel Dunbar53b23382010-03-19 09:28:59 +0000542 assert(OW->getStream().tell() - Start == SD->getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000543}
544
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000545void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000546 DEBUG_WITH_TYPE("mc-dump", {
547 llvm::errs() << "assembler backend - pre-layout\n--\n";
548 dump(); });
549
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000550 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000551 MCAsmLayout Layout(*this);
552 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000553 continue;
554
555 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000556 llvm::errs() << "assembler backend - post-relaxation\n--\n";
557 dump(); });
558
559 // Finalize the layout, including fragment lowering.
560 FinishLayout(Layout);
561
562 DEBUG_WITH_TYPE("mc-dump", {
563 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000564 dump(); });
565
Daniel Dunbarff547842010-03-23 23:47:14 +0000566 uint64_t StartOffset = OS.tell();
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000567 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
568 if (!Writer)
569 llvm_report_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000570
571 // Allow the object writer a chance to perform post-layout binding (for
572 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000573 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000574
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000575 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000576 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
577 for (MCSectionData::iterator it2 = it->begin(),
578 ie2 = it->end(); it2 != ie2; ++it2) {
579 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
580 if (!DF)
581 continue;
582
583 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
584 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
585 MCAsmFixup &Fixup = *it3;
586
587 // Evaluate the fixup.
588 MCValue Target;
589 uint64_t FixedValue;
590 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
591 // The fixup was unresolved, we need a relocation. Inform the object
592 // writer of the relocation, and give it an opportunity to adjust the
593 // fixup value if need be.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000594 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000595 }
596
Daniel Dunbar87190c42010-03-19 09:28:12 +0000597 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000598 }
599 }
600 }
601
Daniel Dunbarbacba992010-03-19 07:09:33 +0000602 // Write the object file.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000603 Writer->WriteObject(*this, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000604 OS.flush();
Daniel Dunbarff547842010-03-23 23:47:14 +0000605
606 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000607}
608
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000609bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
610 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000611 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000612 // If we cannot resolve the fixup value, it requires relaxation.
613 MCValue Target;
614 uint64_t Value;
615 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
616 return true;
617
618 // Otherwise, relax if the value is too big for a (signed) i8.
619 return int64_t(Value) != int64_t(int8_t(Value));
620}
621
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000622bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
623 const MCAsmLayout &Layout) const {
624 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
625 // are intentionally pushing out inst fragments, or because we relaxed a
626 // previous instruction to one that doesn't need relaxation.
627 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
628 return false;
629
630 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
631 ie = IF->fixup_end(); it != ie; ++it)
632 if (FixupNeedsRelaxation(*it, IF, Layout))
633 return true;
634
635 return false;
636}
637
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000638bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000639 ++stats::RelaxationSteps;
640
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000641 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000642 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000643 MCSectionData *Prev = 0;
644 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000645 MCSectionData &SD = *it;
646
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000647 // Skip virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000648 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000649 continue;
650
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000651 // Align this section if necessary by adding padding bytes to the previous
652 // section.
653 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
654 assert(Prev && "Missing prev section!");
655 Prev->setFileSize(Prev->getFileSize() + Pad);
656 Address += Pad;
657 }
Daniel Dunbar6742e342009-08-26 04:13:32 +0000658
659 // Layout the section fragments and its size.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000660 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000661 LayoutSection(SD, Layout);
Daniel Dunbar6742e342009-08-26 04:13:32 +0000662 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000663
664 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +0000665 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000666
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000667 // Layout the virtual sections.
668 for (iterator it = begin(), ie = end(); it != ie; ++it) {
669 MCSectionData &SD = *it;
670
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000671 if (!getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000672 continue;
673
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000674 // Align this section if necessary by adding padding bytes to the previous
675 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +0000676 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000677 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000678
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000679 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000680 LayoutSection(SD, Layout);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000681 Address += SD.getSize();
682 }
683
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000684 // Scan for fragments that need relaxation.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000685 for (iterator it = begin(), ie = end(); it != ie; ++it) {
686 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000687
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000688 for (MCSectionData::iterator it2 = SD.begin(),
689 ie2 = SD.end(); it2 != ie2; ++it2) {
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000690 // Check if this is an instruction fragment that needs relaxation.
691 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
692 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000693 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000694
Daniel Dunbarff547842010-03-23 23:47:14 +0000695 ++stats::RelaxedInstructions;
696
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000697 // FIXME-PERF: We could immediately lower out instructions if we can tell
698 // they are fully resolved, to avoid retesting on later passes.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000699
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000700 // Relax the fragment.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000701
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000702 MCInst Relaxed;
703 getBackend().RelaxInstruction(IF, Relaxed);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000704
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000705 // Encode the new instruction.
706 //
707 // FIXME-PERF: If it matters, we could let the target do this. It can
708 // probably do so more efficiently in many cases.
709 SmallVector<MCFixup, 4> Fixups;
710 SmallString<256> Code;
711 raw_svector_ostream VecOS(Code);
712 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
713 VecOS.flush();
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000714
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000715 // Update the instruction fragment.
716 IF->setInst(Relaxed);
717 IF->getCode() = Code;
718 IF->getFixups().clear();
719 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
720 MCFixup &F = Fixups[i];
721 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
722 F.getKind()));
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000723 }
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000724
725 // Restart layout.
726 //
727 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
728 // smart MCAsmLayout object.
729 return true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000730 }
731 }
732
733 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000734}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000735
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000736void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
737 // Lower out any instruction fragments, to simplify the fixup application and
738 // output.
739 //
740 // FIXME-PERF: We don't have to do this, but the assumption is that it is
741 // cheap (we will mostly end up eliminating fragments and appending on to data
742 // fragments), so the extra complexity downstream isn't worth it. Evaluate
743 // this assumption.
744 for (iterator it = begin(), ie = end(); it != ie; ++it) {
745 MCSectionData &SD = *it;
746
747 for (MCSectionData::iterator it2 = SD.begin(),
748 ie2 = SD.end(); it2 != ie2; ++it2) {
749 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
750 if (!IF)
751 continue;
752
753 // Create a new data fragment for the instruction.
754 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000755 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000756 MCDataFragment *DF = new MCDataFragment();
757 SD.getFragmentList().insert(it2, DF);
758
759 // Update the data fragments layout data.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000760 //
761 // FIXME: Add MCAsmLayout utility for this.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000762 DF->setParent(IF->getParent());
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000763 Layout.setFragmentOffset(DF, Layout.getFragmentOffset(IF));
764 Layout.setFragmentEffectiveSize(DF, Layout.getFragmentEffectiveSize(IF));
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000765
Daniel Dunbar9799de92010-03-23 01:39:05 +0000766 // Copy in the data and the fixups.
767 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
768 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
769 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000770
771 // Delete the instruction fragment and update the iterator.
772 SD.getFragmentList().erase(IF);
773 it2 = DF;
774 }
775 }
776}
777
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000778// Debugging methods
779
780namespace llvm {
781
782raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000783 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
784 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000785 return OS;
786}
787
788}
789
790void MCFragment::dump() {
791 raw_ostream &OS = llvm::errs();
792
793 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000794 << " EffectiveSize:" << EffectiveSize;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000795
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000796 OS << ">";
797}
798
799void MCAlignFragment::dump() {
800 raw_ostream &OS = llvm::errs();
801
802 OS << "<MCAlignFragment ";
803 this->MCFragment::dump();
804 OS << "\n ";
805 OS << " Alignment:" << getAlignment()
806 << " Value:" << getValue() << " ValueSize:" << getValueSize()
807 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
808}
809
810void MCDataFragment::dump() {
811 raw_ostream &OS = llvm::errs();
812
813 OS << "<MCDataFragment ";
814 this->MCFragment::dump();
815 OS << "\n ";
816 OS << " Contents:[";
817 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
818 if (i) OS << ",";
819 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
820 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000821 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000822
823 if (!getFixups().empty()) {
824 OS << ",\n ";
825 OS << " Fixups:[";
826 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000827 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000828 OS << *it;
829 }
830 OS << "]";
831 }
832
833 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000834}
835
836void MCFillFragment::dump() {
837 raw_ostream &OS = llvm::errs();
838
839 OS << "<MCFillFragment ";
840 this->MCFragment::dump();
841 OS << "\n ";
842 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
843 << " Count:" << getCount() << ">";
844}
845
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000846void MCInstFragment::dump() {
847 raw_ostream &OS = llvm::errs();
848
849 OS << "<MCInstFragment ";
850 this->MCFragment::dump();
851 OS << "\n ";
852 OS << " Inst:";
853 getInst().dump_pretty(OS);
854 OS << ">";
855}
856
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000857void MCOrgFragment::dump() {
858 raw_ostream &OS = llvm::errs();
859
860 OS << "<MCOrgFragment ";
861 this->MCFragment::dump();
862 OS << "\n ";
863 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
864}
865
866void MCZeroFillFragment::dump() {
867 raw_ostream &OS = llvm::errs();
868
869 OS << "<MCZeroFillFragment ";
870 this->MCFragment::dump();
871 OS << "\n ";
872 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
873}
874
875void MCSectionData::dump() {
876 raw_ostream &OS = llvm::errs();
877
878 OS << "<MCSectionData";
879 OS << " Alignment:" << getAlignment() << " Address:" << Address
880 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000881 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000882 for (iterator it = begin(), ie = end(); it != ie; ++it) {
883 if (it != begin()) OS << ",\n ";
884 it->dump();
885 }
886 OS << "]>";
887}
888
889void MCSymbolData::dump() {
890 raw_ostream &OS = llvm::errs();
891
892 OS << "<MCSymbolData Symbol:" << getSymbol()
893 << " Fragment:" << getFragment() << " Offset:" << getOffset()
894 << " Flags:" << getFlags() << " Index:" << getIndex();
895 if (isCommon())
896 OS << " (common, size:" << getCommonSize()
897 << " align: " << getCommonAlignment() << ")";
898 if (isExternal())
899 OS << " (external)";
900 if (isPrivateExtern())
901 OS << " (private extern)";
902 OS << ">";
903}
904
905void MCAssembler::dump() {
906 raw_ostream &OS = llvm::errs();
907
908 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000909 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000910 for (iterator it = begin(), ie = end(); it != ie; ++it) {
911 if (it != begin()) OS << ",\n ";
912 it->dump();
913 }
914 OS << "],\n";
915 OS << " Symbols:[";
916
917 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000918 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000919 it->dump();
920 }
921 OS << "]>\n";
922}