blob: de36c52c6800ee51c3143f2c0a5ece4ddc6a35ad [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 Dunbarac2884a2010-03-25 22:49:09 +000022#include "llvm/Support/Debug.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000023#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024#include "llvm/Support/raw_ostream.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 {
Daniel Dunbar0adcd352009-08-25 21:10:45 +000033STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
Daniel Dunbarff547842010-03-23 23:47:14 +000034STATISTIC(EvaluateFixup, "Number of evaluated fixups");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000035STATISTIC(FragmentLayouts, "Number of fragment layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000036STATISTIC(ObjectBytes, "Number of emitted object file bytes");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000037STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
38STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
39STATISTIC(SectionLayouts, "Number of section layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000040}
41}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000042
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000043// FIXME FIXME FIXME: There are number of places in this file where we convert
44// what is a 64-bit assembler value used for computation into a value in the
45// object file, which may truncate it. We should detect that truncation where
46// invalid and report errors back.
47
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000048/* *** */
49
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000050MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
51 // Compute the section layout order. Virtual sections must go last.
52 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
53 if (!Asm.getBackend().isVirtualSection(it->getSection()))
54 SectionOrder.push_back(&*it);
55 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
56 if (Asm.getBackend().isVirtualSection(it->getSection()))
57 SectionOrder.push_back(&*it);
58}
59
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000060void MCAsmLayout::UpdateForSlide(MCFragment *F, int SlideAmount) {
61 // We shouldn't have to do anything special to support negative slides, and it
Daniel Dunbar651804c2010-05-11 17:22:50 +000062 // is a perfectly valid thing to do as long as other parts of the system can
63 // guarantee convergence.
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000064 assert(SlideAmount >= 0 && "Negative slides not yet supported");
65
66 // Update the layout by simply recomputing the layout for the entire
67 // file. This is trivially correct, but very slow.
68 //
69 // FIXME-PERF: This is O(N^2), but will be eliminated once we get smarter.
70
Daniel Dunbard13a0ca2010-05-12 17:56:47 +000071 // Layout the sections in order.
72 for (unsigned i = 0, e = getSectionOrder().size(); i != e; ++i)
73 getAssembler().LayoutSection(*this, i);
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000074}
75
Daniel Dunbaraa0d3502010-05-13 08:43:31 +000076void MCAsmLayout::FragmentReplaced(MCFragment *Src, MCFragment *Dst) {
77 Dst->Offset = Src->Offset;
78 Dst->EffectiveSize = Src->EffectiveSize;
79}
80
Daniel Dunbar207e06e2010-03-24 03:43:40 +000081uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +000082 assert(F->getParent() && "Missing section()!");
Daniel Dunbar432cd5f2010-03-25 02:00:02 +000083 return getSectionAddress(F->getParent()) + getFragmentOffset(F);
84}
85
86uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
87 assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
88 return F->EffectiveSize;
89}
90
91void MCAsmLayout::setFragmentEffectiveSize(MCFragment *F, uint64_t Value) {
92 F->EffectiveSize = Value;
93}
94
95uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
96 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
97 return F->Offset;
98}
99
100void MCAsmLayout::setFragmentOffset(MCFragment *F, uint64_t Value) {
101 F->Offset = Value;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000102}
103
104uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +0000105 assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
106 return getFragmentAddress(SD->getFragment()) + SD->getOffset();
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000107}
108
109uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +0000110 assert(SD->Address != ~UINT64_C(0) && "Address not set!");
111 return SD->Address;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000112}
113
114void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
Daniel Dunbar7c3d45a2010-03-25 01:03:24 +0000115 SD->Address = Value;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000116}
117
Daniel Dunbar2661f112010-05-13 03:19:50 +0000118uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
119 // Empty sections have no size.
120 if (SD->getFragmentList().empty())
121 return 0;
122
123 // Otherwise, the size is the last fragment's end offset.
124 const MCFragment &F = SD->getFragmentList().back();
125 return getFragmentOffset(&F) + getFragmentEffectiveSize(&F);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000126}
127
128uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
Daniel Dunbar2661f112010-05-13 03:19:50 +0000129 // Virtual sections have no file size.
130 if (getAssembler().getBackend().isVirtualSection(SD->getSection()))
131 return 0;
132
133 // Otherwise, the file size is the same as the address space size.
134 return getSectionAddressSize(SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000135}
136
Daniel Dunbar2661f112010-05-13 03:19:50 +0000137uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
138 // Empty sections have no size.
139 if (SD->getFragmentList().empty())
140 return 0;
141
142 // The logical size is the address space size minus any tail padding.
143 uint64_t Size = getSectionAddressSize(SD);
144 const MCAlignFragment *AF =
145 dyn_cast<MCAlignFragment>(&(SD->getFragmentList().back()));
146 if (AF && AF->hasOnlyAlignAddress())
147 Size -= getFragmentEffectiveSize(AF);
148
149 return Size;
Daniel Dunbarb5844ff2010-05-13 01:10:22 +0000150}
151
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000152/* *** */
153
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000154MCFragment::MCFragment() : Kind(FragmentType(~0)) {
155}
156
Daniel Dunbar5e835962009-08-26 02:48:04 +0000157MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000158 : Kind(_Kind), Parent(_Parent), Atom(0), EffectiveSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000159{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000160 if (Parent)
161 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000162}
163
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000164MCFragment::~MCFragment() {
165}
166
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000167/* *** */
168
Daniel Dunbar81e40002009-08-27 00:38:04 +0000169MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000170
171MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000172 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000173 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000174 Address(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000175 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176{
177 if (A)
178 A->getSectionList().push_back(this);
179}
180
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000181/* *** */
182
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000183MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000184
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000185MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000186 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000187 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000188 IsExternal(false), IsPrivateExtern(false),
189 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000190{
191 if (A)
192 A->getSymbolList().push_back(this);
193}
194
195/* *** */
196
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000197MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000198 MCCodeEmitter &_Emitter, raw_ostream &_OS)
199 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000200 OS(_OS), RelaxAll(false), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000201{
202}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000203
204MCAssembler::~MCAssembler() {
205}
206
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000207static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
208 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000209 const MCValue Target,
210 const MCSection *BaseSection) {
211 // The effective fixup address is
212 // addr(atom(A)) + offset(A)
213 // - addr(atom(B)) - offset(B)
214 // - addr(<base symbol>) + <fixup offset from base symbol>
215 // and the offsets are not relocatable, so the fixup is fully resolved when
216 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
217 //
218 // The simple (Darwin, except on x86_64) way of dealing with this was to
219 // assume that any reference to a temporary symbol *must* be a temporary
220 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
221 // relocation to a temporary symbol (in the same section) is fully
222 // resolved. This also works in conjunction with absolutized .set, which
223 // requires the compiler to use .set to absolutize the differences between
224 // symbols which the compiler knows to be assembly time constants, so we don't
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000225 // need to worry about considering symbol differences fully resolved.
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000226
227 // Non-relative fixups are only resolved if constant.
228 if (!BaseSection)
229 return Target.isAbsolute();
230
231 // Otherwise, relative fixups are only resolved if not a difference and the
232 // target is a temporary in the same section.
233 if (Target.isAbsolute() || Target.getSymB())
234 return false;
235
236 const MCSymbol *A = &Target.getSymA()->getSymbol();
237 if (!A->isTemporary() || !A->isInSection() ||
238 &A->getSection() != BaseSection)
239 return false;
240
241 return true;
242}
243
Daniel Dunbar034843a2010-03-19 03:18:18 +0000244static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000245 const MCAsmLayout &Layout,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000246 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000247 const MCValue Target,
248 const MCSymbolData *BaseSymbol) {
249 // The effective fixup address is
250 // addr(atom(A)) + offset(A)
251 // - addr(atom(B)) - offset(B)
252 // - addr(BaseSymbol) + <fixup offset from base symbol>
253 // and the offsets are not relocatable, so the fixup is fully resolved when
254 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
255 //
256 // Note that "false" is almost always conservatively correct (it means we emit
257 // a relocation which is unnecessary), except when it would force us to emit a
258 // relocation which the target cannot encode.
259
260 const MCSymbolData *A_Base = 0, *B_Base = 0;
261 if (const MCSymbolRefExpr *A = Target.getSymA()) {
262 // Modified symbol references cannot be resolved.
263 if (A->getKind() != MCSymbolRefExpr::VK_None)
264 return false;
265
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000266 A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000267 if (!A_Base)
268 return false;
269 }
270
271 if (const MCSymbolRefExpr *B = Target.getSymB()) {
272 // Modified symbol references cannot be resolved.
273 if (B->getKind() != MCSymbolRefExpr::VK_None)
274 return false;
275
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000276 B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000277 if (!B_Base)
278 return false;
279 }
280
281 // If there is no base, A and B have to be the same atom for this fixup to be
282 // fully resolved.
283 if (!BaseSymbol)
284 return A_Base == B_Base;
285
286 // Otherwise, B must be missing and A must be the base.
287 return !B_Base && BaseSymbol == A_Base;
288}
289
Daniel Dunbar23869852010-03-19 03:18:09 +0000290bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
291 // Non-temporary labels should always be visible to the linker.
292 if (!SD->getSymbol().isTemporary())
293 return true;
294
295 // Absolute temporary labels are never visible.
296 if (!SD->getFragment())
297 return false;
298
299 // Otherwise, check if the section requires symbols even for temporary labels.
300 return getBackend().doesSectionRequireSymbols(
301 SD->getFragment()->getParent()->getSection());
302}
303
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000304const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout,
305 const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000306 // Linker visible symbols define atoms.
307 if (isSymbolLinkerVisible(SD))
308 return SD;
309
310 // Absolute and undefined symbols have no defining atom.
311 if (!SD->getFragment())
312 return 0;
313
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000314 // Non-linker visible symbols in sections which can't be atomized have no
315 // defining atom.
316 if (!getBackend().isSectionAtomizable(
317 SD->getFragment()->getParent()->getSection()))
318 return 0;
319
Daniel Dunbar651804c2010-05-11 17:22:50 +0000320 // Otherwise, return the atom for the containing fragment.
321 return SD->getFragment()->getAtom();
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000322}
323
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000324bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
325 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000326 MCValue &Target, uint64_t &Value) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000327 ++stats::EvaluateFixup;
328
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000329 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
Chris Lattner75361b62010-04-07 22:58:41 +0000330 report_fatal_error("expected relocatable expression");
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000331
332 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
333 // doesn't support small relocations, but then under what criteria does the
334 // assembler allow symbol differences?
335
336 Value = Target.getConstant();
337
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000338 bool IsPCRel =
339 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
340 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000341 if (const MCSymbolRefExpr *A = Target.getSymA()) {
342 if (A->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000343 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000344 else
345 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000346 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000347 if (const MCSymbolRefExpr *B = Target.getSymB()) {
348 if (B->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000349 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000350 else
351 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000352 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000353
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000354 // If we are using scattered symbols, determine whether this value is actually
355 // resolved; scattering may cause atoms to move.
356 if (IsResolved && getBackend().hasScatteredSymbols()) {
357 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000358 // If this is a PCrel relocation, find the base atom (identified by its
359 // symbol) that the fixup value is relative to.
360 const MCSymbolData *BaseSymbol = 0;
361 if (IsPCRel) {
Daniel Dunbar651804c2010-05-11 17:22:50 +0000362 BaseSymbol = DF->getAtom();
Daniel Dunbar034843a2010-03-19 03:18:18 +0000363 if (!BaseSymbol)
364 IsResolved = false;
365 }
366
367 if (IsResolved)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000368 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000369 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000370 } else {
371 const MCSection *BaseSection = 0;
372 if (IsPCRel)
373 BaseSection = &DF->getParent()->getSection();
374
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000375 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000376 BaseSection);
377 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000378 }
379
380 if (IsPCRel)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000381 Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000382
383 return IsResolved;
384}
385
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000386void MCAssembler::LayoutFragment(MCAsmLayout &Layout, MCFragment &F) {
387 uint64_t StartAddress = Layout.getSectionAddress(F.getParent());
388
389 // Get the fragment start address.
390 uint64_t Address = StartAddress;
391 MCSectionData::iterator it = &F;
392 if (MCFragment *Prev = F.getPrevNode())
393 Address = (StartAddress + Layout.getFragmentOffset(Prev) +
394 Layout.getFragmentEffectiveSize(Prev));
395
396 ++stats::FragmentLayouts;
397
398 uint64_t FragmentOffset = Address - StartAddress;
399 Layout.setFragmentOffset(&F, FragmentOffset);
400
401 // Evaluate fragment size.
402 uint64_t EffectiveSize = 0;
403 switch (F.getKind()) {
404 case MCFragment::FT_Align: {
405 MCAlignFragment &AF = cast<MCAlignFragment>(F);
406
Daniel Dunbar456b5012010-05-13 01:10:26 +0000407 assert((!AF.hasOnlyAlignAddress() || !AF.getNextNode()) &&
408 "Invalid OnlyAlignAddress bit, not the last fragment!");
409
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000410 EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
411 if (EffectiveSize > AF.getMaxBytesToEmit())
412 EffectiveSize = 0;
413 break;
414 }
415
416 case MCFragment::FT_Data:
417 EffectiveSize = cast<MCDataFragment>(F).getContents().size();
418 break;
419
420 case MCFragment::FT_Fill: {
Daniel Dunbar4e544872010-05-12 22:51:38 +0000421 EffectiveSize = cast<MCFillFragment>(F).getSize();
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000422 break;
423 }
424
425 case MCFragment::FT_Inst:
426 EffectiveSize = cast<MCInstFragment>(F).getInstSize();
427 break;
428
429 case MCFragment::FT_Org: {
430 MCOrgFragment &OF = cast<MCOrgFragment>(F);
431
432 int64_t TargetLocation;
433 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
434 report_fatal_error("expected assembly-time absolute expression");
435
436 // FIXME: We need a way to communicate this error.
437 int64_t Offset = TargetLocation - FragmentOffset;
438 if (Offset < 0)
439 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
440 "' (at offset '" + Twine(FragmentOffset) + "'");
441
442 EffectiveSize = Offset;
443 break;
444 }
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000445 }
446
447 Layout.setFragmentEffectiveSize(&F, EffectiveSize);
448}
449
Daniel Dunbard13a0ca2010-05-12 17:56:47 +0000450void MCAssembler::LayoutSection(MCAsmLayout &Layout,
451 unsigned SectionOrderIndex) {
452 MCSectionData &SD = *Layout.getSectionOrder()[SectionOrderIndex];
Daniel Dunbarf476b002010-03-25 18:16:42 +0000453
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000454 ++stats::SectionLayouts;
455
Daniel Dunbar61066db2010-05-13 02:34:14 +0000456 // Compute the section start address.
Daniel Dunbard13a0ca2010-05-12 17:56:47 +0000457 uint64_t StartAddress = 0;
458 if (SectionOrderIndex) {
459 MCSectionData *Prev = Layout.getSectionOrder()[SectionOrderIndex - 1];
Daniel Dunbarb5844ff2010-05-13 01:10:22 +0000460 StartAddress = (Layout.getSectionAddress(Prev) +
461 Layout.getSectionAddressSize(Prev));
Daniel Dunbard13a0ca2010-05-12 17:56:47 +0000462 }
463
Daniel Dunbar61066db2010-05-13 02:34:14 +0000464 // Honor the section alignment requirements.
465 StartAddress = RoundUpToAlignment(StartAddress, SD.getAlignment());
Daniel Dunbarf476b002010-03-25 18:16:42 +0000466
Daniel Dunbar61066db2010-05-13 02:34:14 +0000467 // Set the section address.
Daniel Dunbarbe644a32010-03-25 18:16:38 +0000468 Layout.setSectionAddress(&SD, StartAddress);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000469
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000470 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it)
471 LayoutFragment(Layout, *it);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000472}
473
Daniel Dunbar53b23382010-03-19 09:28:59 +0000474/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000475static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
476 const MCFragment &F, MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000477 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000478 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000479
Daniel Dunbarff547842010-03-23 23:47:14 +0000480 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000481
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000482 // FIXME: Embed in fragments instead?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000483 uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000484 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000485 case MCFragment::FT_Align: {
486 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000487 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000488
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000489 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
490
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000491 // FIXME: This error shouldn't actually occur (the front end should emit
492 // multiple .align directives to enforce the semantics it wants), but is
493 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000494 if (Count * AF.getValueSize() != FragmentSize)
Chris Lattner75361b62010-04-07 22:58:41 +0000495 report_fatal_error("undefined .align directive, value size '" +
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000496 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000497 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000498 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000499
Kevin Enderby6e720482010-02-23 18:26:34 +0000500 // See if we are aligning with nops, and if so do that first to try to fill
501 // the Count bytes. Then if that did not fill any bytes or there are any
502 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000503 // If we are aligning with nops, ask that target to emit the right data.
Daniel Dunbar1c154132010-05-12 22:56:23 +0000504 if (AF.hasEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000505 if (!Asm.getBackend().WriteNopData(Count, OW))
Chris Lattner75361b62010-04-07 22:58:41 +0000506 report_fatal_error("unable to write nop sequence of " +
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000507 Twine(Count) + " bytes");
508 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000509 }
510
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000511 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000512 for (uint64_t i = 0; i != Count; ++i) {
513 switch (AF.getValueSize()) {
514 default:
515 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000516 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
517 case 2: OW->Write16(uint16_t(AF.getValue())); break;
518 case 4: OW->Write32(uint32_t(AF.getValue())); break;
519 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000520 }
521 }
522 break;
523 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000524
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000525 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000526 MCDataFragment &DF = cast<MCDataFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000527 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000528 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000529 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000530 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000531
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000532 case MCFragment::FT_Fill: {
533 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000534
535 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
536
Daniel Dunbar3153fec2010-05-12 22:51:32 +0000537 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000538 switch (FF.getValueSize()) {
539 default:
540 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000541 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
542 case 2: OW->Write16(uint16_t(FF.getValue())); break;
543 case 4: OW->Write32(uint32_t(FF.getValue())); break;
544 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000545 }
546 }
547 break;
548 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000549
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000550 case MCFragment::FT_Inst:
551 llvm_unreachable("unexpected inst fragment after lowering");
552 break;
553
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000554 case MCFragment::FT_Org: {
555 MCOrgFragment &OF = cast<MCOrgFragment>(F);
556
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000557 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000558 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000559
560 break;
561 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000562 }
563
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000564 assert(OW->getStream().tell() - Start == FragmentSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000565}
566
Daniel Dunbar53b23382010-03-19 09:28:59 +0000567void MCAssembler::WriteSectionData(const MCSectionData *SD,
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000568 const MCAsmLayout &Layout,
Daniel Dunbar53b23382010-03-19 09:28:59 +0000569 MCObjectWriter *OW) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000570 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000571 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar054be922010-05-13 03:50:50 +0000572 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000573
574 // Check that contents are only things legal inside a virtual section.
575 for (MCSectionData::const_iterator it = SD->begin(),
576 ie = SD->end(); it != ie; ++it) {
577 switch (it->getKind()) {
578 default:
579 assert(0 && "Invalid fragment in virtual section!");
580 case MCFragment::FT_Align:
581 assert(!cast<MCAlignFragment>(it)->getValueSize() &&
582 "Invalid align in virtual section!");
583 break;
584 case MCFragment::FT_Fill:
585 assert(!cast<MCFillFragment>(it)->getValueSize() &&
586 "Invalid fill in virtual section!");
587 break;
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000588 }
589 }
590
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000591 return;
592 }
593
Daniel Dunbar53b23382010-03-19 09:28:59 +0000594 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000595 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000596
Daniel Dunbar53b23382010-03-19 09:28:59 +0000597 for (MCSectionData::const_iterator it = SD->begin(),
598 ie = SD->end(); it != ie; ++it)
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000599 WriteFragmentData(*this, Layout, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000600
Daniel Dunbar054be922010-05-13 03:50:50 +0000601 assert(OW->getStream().tell() - Start == Layout.getSectionFileSize(SD));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000602}
603
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000604void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000605 DEBUG_WITH_TYPE("mc-dump", {
606 llvm::errs() << "assembler backend - pre-layout\n--\n";
607 dump(); });
608
Daniel Dunbar5a6e97a2010-03-25 07:10:11 +0000609 // Assign section and fragment ordinals, all subsequent backend code is
610 // responsible for updating these in place.
611 unsigned SectionIndex = 0;
612 unsigned FragmentIndex = 0;
613 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
614 it->setOrdinal(SectionIndex++);
615
616 for (MCSectionData::iterator it2 = it->begin(),
617 ie2 = it->end(); it2 != ie2; ++it2)
618 it2->setOrdinal(FragmentIndex++);
619 }
620
Daniel Dunbar61066db2010-05-13 02:34:14 +0000621 // Create the layout object.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000622 MCAsmLayout Layout(*this);
Daniel Dunbar61066db2010-05-13 02:34:14 +0000623
624 // Insert additional align fragments for concrete sections to explicitly pad
625 // the previous section to match their alignment requirements. This is for
626 // 'gas' compatibility, it shouldn't strictly be necessary.
627 //
628 // FIXME: This may be Mach-O specific.
629 for (unsigned i = 1, e = Layout.getSectionOrder().size(); i < e; ++i) {
630 MCSectionData *SD = Layout.getSectionOrder()[i];
631
632 // Ignore sections without alignment requirements.
633 unsigned Align = SD->getAlignment();
634 if (Align <= 1)
635 continue;
636
637 // Ignore virtual sections, they don't cause file size modifications.
638 if (getBackend().isVirtualSection(SD->getSection()))
639 continue;
640
641 // Otherwise, create a new align fragment at the end of the previous
642 // section.
643 MCAlignFragment *AF = new MCAlignFragment(Align, 0, 1, Align,
644 Layout.getSectionOrder()[i - 1]);
645 AF->setOnlyAlignAddress(true);
646 }
647
648 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000649 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000650 continue;
651
652 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000653 llvm::errs() << "assembler backend - post-relaxation\n--\n";
654 dump(); });
655
656 // Finalize the layout, including fragment lowering.
657 FinishLayout(Layout);
658
659 DEBUG_WITH_TYPE("mc-dump", {
660 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000661 dump(); });
662
Daniel Dunbarff547842010-03-23 23:47:14 +0000663 uint64_t StartOffset = OS.tell();
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000664 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
665 if (!Writer)
Chris Lattner75361b62010-04-07 22:58:41 +0000666 report_fatal_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000667
668 // Allow the object writer a chance to perform post-layout binding (for
669 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000670 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000671
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000672 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000673 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
674 for (MCSectionData::iterator it2 = it->begin(),
675 ie2 = it->end(); it2 != ie2; ++it2) {
676 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
677 if (!DF)
678 continue;
679
680 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
681 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
682 MCAsmFixup &Fixup = *it3;
683
684 // Evaluate the fixup.
685 MCValue Target;
686 uint64_t FixedValue;
687 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
688 // The fixup was unresolved, we need a relocation. Inform the object
689 // writer of the relocation, and give it an opportunity to adjust the
690 // fixup value if need be.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000691 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000692 }
693
Daniel Dunbar87190c42010-03-19 09:28:12 +0000694 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000695 }
696 }
697 }
698
Daniel Dunbarbacba992010-03-19 07:09:33 +0000699 // Write the object file.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000700 Writer->WriteObject(*this, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000701 OS.flush();
Daniel Dunbarff547842010-03-23 23:47:14 +0000702
703 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000704}
705
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000706bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
707 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000708 const MCAsmLayout &Layout) const {
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000709 if (getRelaxAll())
710 return true;
711
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000712 // If we cannot resolve the fixup value, it requires relaxation.
713 MCValue Target;
714 uint64_t Value;
715 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
716 return true;
717
718 // Otherwise, relax if the value is too big for a (signed) i8.
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000719 //
720 // FIXME: This is target dependent!
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000721 return int64_t(Value) != int64_t(int8_t(Value));
722}
723
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000724bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
725 const MCAsmLayout &Layout) const {
726 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
727 // are intentionally pushing out inst fragments, or because we relaxed a
728 // previous instruction to one that doesn't need relaxation.
729 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
730 return false;
731
732 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
733 ie = IF->fixup_end(); it != ie; ++it)
734 if (FixupNeedsRelaxation(*it, IF, Layout))
735 return true;
736
737 return false;
738}
739
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000740bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000741 ++stats::RelaxationSteps;
742
Daniel Dunbard13a0ca2010-05-12 17:56:47 +0000743 // Layout the sections in order.
744 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i)
745 LayoutSection(Layout, i);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000746
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000747 // Scan for fragments that need relaxation.
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000748 bool WasRelaxed = false;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000749 for (iterator it = begin(), ie = end(); it != ie; ++it) {
750 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000751
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000752 for (MCSectionData::iterator it2 = SD.begin(),
753 ie2 = SD.end(); it2 != ie2; ++it2) {
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000754 // Check if this is an instruction fragment that needs relaxation.
755 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
756 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000757 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000758
Daniel Dunbarff547842010-03-23 23:47:14 +0000759 ++stats::RelaxedInstructions;
760
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000761 // FIXME-PERF: We could immediately lower out instructions if we can tell
762 // they are fully resolved, to avoid retesting on later passes.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000763
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000764 // Relax the fragment.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000765
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000766 MCInst Relaxed;
767 getBackend().RelaxInstruction(IF, Relaxed);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000768
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000769 // Encode the new instruction.
770 //
771 // FIXME-PERF: If it matters, we could let the target do this. It can
772 // probably do so more efficiently in many cases.
773 SmallVector<MCFixup, 4> Fixups;
774 SmallString<256> Code;
775 raw_svector_ostream VecOS(Code);
776 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
777 VecOS.flush();
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000778
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000779 // Update the instruction fragment.
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000780 int SlideAmount = Code.size() - IF->getInstSize();
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000781 IF->setInst(Relaxed);
782 IF->getCode() = Code;
783 IF->getFixups().clear();
784 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
785 MCFixup &F = Fixups[i];
786 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
787 F.getKind()));
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000788 }
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000789
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000790 // Update the layout, and remember that we relaxed. If we are relaxing
791 // everything, we can skip this step since nothing will depend on updating
792 // the values.
793 if (!getRelaxAll())
794 Layout.UpdateForSlide(IF, SlideAmount);
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000795 WasRelaxed = true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000796 }
797 }
798
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000799 return WasRelaxed;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000800}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000801
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000802void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
803 // Lower out any instruction fragments, to simplify the fixup application and
804 // output.
805 //
806 // FIXME-PERF: We don't have to do this, but the assumption is that it is
807 // cheap (we will mostly end up eliminating fragments and appending on to data
808 // fragments), so the extra complexity downstream isn't worth it. Evaluate
809 // this assumption.
810 for (iterator it = begin(), ie = end(); it != ie; ++it) {
811 MCSectionData &SD = *it;
812
813 for (MCSectionData::iterator it2 = SD.begin(),
814 ie2 = SD.end(); it2 != ie2; ++it2) {
815 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
816 if (!IF)
817 continue;
818
819 // Create a new data fragment for the instruction.
820 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000821 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000822 MCDataFragment *DF = new MCDataFragment();
823 SD.getFragmentList().insert(it2, DF);
824
825 // Update the data fragments layout data.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000826 DF->setParent(IF->getParent());
Daniel Dunbar651804c2010-05-11 17:22:50 +0000827 DF->setAtom(IF->getAtom());
Daniel Dunbar5a6e97a2010-03-25 07:10:11 +0000828 DF->setOrdinal(IF->getOrdinal());
Daniel Dunbaraa0d3502010-05-13 08:43:31 +0000829 Layout.FragmentReplaced(IF, DF);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000830
Daniel Dunbar9799de92010-03-23 01:39:05 +0000831 // Copy in the data and the fixups.
832 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
833 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
834 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000835
836 // Delete the instruction fragment and update the iterator.
837 SD.getFragmentList().erase(IF);
838 it2 = DF;
839 }
840 }
841}
842
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000843// Debugging methods
844
845namespace llvm {
846
847raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000848 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
849 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000850 return OS;
851}
852
853}
854
855void MCFragment::dump() {
856 raw_ostream &OS = llvm::errs();
857
858 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
Daniel Dunbarb5844ff2010-05-13 01:10:22 +0000859 << " EffectiveSize:" << EffectiveSize << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000860}
861
862void MCAlignFragment::dump() {
863 raw_ostream &OS = llvm::errs();
864
865 OS << "<MCAlignFragment ";
866 this->MCFragment::dump();
Daniel Dunbar456b5012010-05-13 01:10:26 +0000867 if (hasEmitNops())
868 OS << " (emit nops)";
869 if (hasOnlyAlignAddress())
870 OS << " (only align section)";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000871 OS << "\n ";
872 OS << " Alignment:" << getAlignment()
873 << " Value:" << getValue() << " ValueSize:" << getValueSize()
874 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
875}
876
877void MCDataFragment::dump() {
878 raw_ostream &OS = llvm::errs();
879
880 OS << "<MCDataFragment ";
881 this->MCFragment::dump();
882 OS << "\n ";
883 OS << " Contents:[";
884 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
885 if (i) OS << ",";
886 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
887 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000888 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000889
890 if (!getFixups().empty()) {
891 OS << ",\n ";
892 OS << " Fixups:[";
893 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000894 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000895 OS << *it;
896 }
897 OS << "]";
898 }
899
900 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000901}
902
903void MCFillFragment::dump() {
904 raw_ostream &OS = llvm::errs();
905
906 OS << "<MCFillFragment ";
907 this->MCFragment::dump();
908 OS << "\n ";
909 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
Daniel Dunbar3153fec2010-05-12 22:51:32 +0000910 << " Size:" << getSize() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000911}
912
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000913void MCInstFragment::dump() {
914 raw_ostream &OS = llvm::errs();
915
916 OS << "<MCInstFragment ";
917 this->MCFragment::dump();
918 OS << "\n ";
919 OS << " Inst:";
920 getInst().dump_pretty(OS);
921 OS << ">";
922}
923
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000924void MCOrgFragment::dump() {
925 raw_ostream &OS = llvm::errs();
926
927 OS << "<MCOrgFragment ";
928 this->MCFragment::dump();
929 OS << "\n ";
930 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
931}
932
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000933void MCSectionData::dump() {
934 raw_ostream &OS = llvm::errs();
935
936 OS << "<MCSectionData";
937 OS << " Alignment:" << getAlignment() << " Address:" << Address
Daniel Dunbar2661f112010-05-13 03:19:50 +0000938 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000939 for (iterator it = begin(), ie = end(); it != ie; ++it) {
940 if (it != begin()) OS << ",\n ";
941 it->dump();
942 }
943 OS << "]>";
944}
945
946void MCSymbolData::dump() {
947 raw_ostream &OS = llvm::errs();
948
949 OS << "<MCSymbolData Symbol:" << getSymbol()
950 << " Fragment:" << getFragment() << " Offset:" << getOffset()
951 << " Flags:" << getFlags() << " Index:" << getIndex();
952 if (isCommon())
953 OS << " (common, size:" << getCommonSize()
954 << " align: " << getCommonAlignment() << ")";
955 if (isExternal())
956 OS << " (external)";
957 if (isPrivateExtern())
958 OS << " (private extern)";
959 OS << ">";
960}
961
962void MCAssembler::dump() {
963 raw_ostream &OS = llvm::errs();
964
965 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000966 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000967 for (iterator it = begin(), ie = end(); it != ie; ++it) {
968 if (it != begin()) OS << ",\n ";
969 it->dump();
970 }
971 OS << "],\n";
972 OS << " Symbols:[";
973
974 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000975 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000976 it->dump();
977 }
978 OS << "]>\n";
979}