blob: 8efc78c1f9a83d93d1794cdd6f9d1b65d61be4f9 [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
Daniel Dunbar5d428512010-03-25 02:00:07 +000085uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
86 assert(SD->Size != ~UINT64_C(0) && "File size not set!");
87 return SD->Size;
88}
89void MCAsmLayout::setSectionSize(MCSectionData *SD, uint64_t Value) {
90 SD->Size = Value;
91}
92
93uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
94 assert(SD->FileSize != ~UINT64_C(0) && "File size not set!");
95 return SD->FileSize;
96}
97void MCAsmLayout::setSectionFileSize(MCSectionData *SD, uint64_t Value) {
98 SD->FileSize = Value;
99}
100
101 /// @}
102
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000103/* *** */
104
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000105MCFragment::MCFragment() : Kind(FragmentType(~0)) {
106}
107
Daniel Dunbar5e835962009-08-26 02:48:04 +0000108MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000109 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000110 Parent(_Parent),
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000111 EffectiveSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000112{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000113 if (Parent)
114 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000115}
116
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000117MCFragment::~MCFragment() {
118}
119
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000120/* *** */
121
Daniel Dunbar81e40002009-08-27 00:38:04 +0000122MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000123
124MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000125 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000126 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000127 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +0000128 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000129 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000130 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000131{
132 if (A)
133 A->getSectionList().push_back(this);
134}
135
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000136/* *** */
137
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000138MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000139
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000140MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000141 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000142 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000143 IsExternal(false), IsPrivateExtern(false),
144 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000145{
146 if (A)
147 A->getSymbolList().push_back(this);
148}
149
150/* *** */
151
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000152MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +0000153 MCCodeEmitter &_Emitter, raw_ostream &_OS)
154 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
155 OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000156{
157}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000158
159MCAssembler::~MCAssembler() {
160}
161
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000162static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
163 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000164 const MCValue Target,
165 const MCSection *BaseSection) {
166 // The effective fixup address is
167 // addr(atom(A)) + offset(A)
168 // - addr(atom(B)) - offset(B)
169 // - addr(<base symbol>) + <fixup offset from base symbol>
170 // and the offsets are not relocatable, so the fixup is fully resolved when
171 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
172 //
173 // The simple (Darwin, except on x86_64) way of dealing with this was to
174 // assume that any reference to a temporary symbol *must* be a temporary
175 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
176 // relocation to a temporary symbol (in the same section) is fully
177 // resolved. This also works in conjunction with absolutized .set, which
178 // requires the compiler to use .set to absolutize the differences between
179 // symbols which the compiler knows to be assembly time constants, so we don't
180 // need to worry about consider symbol differences fully resolved.
181
182 // Non-relative fixups are only resolved if constant.
183 if (!BaseSection)
184 return Target.isAbsolute();
185
186 // Otherwise, relative fixups are only resolved if not a difference and the
187 // target is a temporary in the same section.
188 if (Target.isAbsolute() || Target.getSymB())
189 return false;
190
191 const MCSymbol *A = &Target.getSymA()->getSymbol();
192 if (!A->isTemporary() || !A->isInSection() ||
193 &A->getSection() != BaseSection)
194 return false;
195
196 return true;
197}
198
Daniel Dunbar034843a2010-03-19 03:18:18 +0000199static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000200 const MCAsmLayout &Layout,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000201 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000202 const MCValue Target,
203 const MCSymbolData *BaseSymbol) {
204 // The effective fixup address is
205 // addr(atom(A)) + offset(A)
206 // - addr(atom(B)) - offset(B)
207 // - addr(BaseSymbol) + <fixup offset from base symbol>
208 // and the offsets are not relocatable, so the fixup is fully resolved when
209 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
210 //
211 // Note that "false" is almost always conservatively correct (it means we emit
212 // a relocation which is unnecessary), except when it would force us to emit a
213 // relocation which the target cannot encode.
214
215 const MCSymbolData *A_Base = 0, *B_Base = 0;
216 if (const MCSymbolRefExpr *A = Target.getSymA()) {
217 // Modified symbol references cannot be resolved.
218 if (A->getKind() != MCSymbolRefExpr::VK_None)
219 return false;
220
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000221 A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000222 if (!A_Base)
223 return false;
224 }
225
226 if (const MCSymbolRefExpr *B = Target.getSymB()) {
227 // Modified symbol references cannot be resolved.
228 if (B->getKind() != MCSymbolRefExpr::VK_None)
229 return false;
230
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000231 B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
Daniel Dunbar034843a2010-03-19 03:18:18 +0000232 if (!B_Base)
233 return false;
234 }
235
236 // If there is no base, A and B have to be the same atom for this fixup to be
237 // fully resolved.
238 if (!BaseSymbol)
239 return A_Base == B_Base;
240
241 // Otherwise, B must be missing and A must be the base.
242 return !B_Base && BaseSymbol == A_Base;
243}
244
Daniel Dunbar23869852010-03-19 03:18:09 +0000245bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
246 // Non-temporary labels should always be visible to the linker.
247 if (!SD->getSymbol().isTemporary())
248 return true;
249
250 // Absolute temporary labels are never visible.
251 if (!SD->getFragment())
252 return false;
253
254 // Otherwise, check if the section requires symbols even for temporary labels.
255 return getBackend().doesSectionRequireSymbols(
256 SD->getFragment()->getParent()->getSection());
257}
258
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000259// FIXME-PERF: This routine is really slow.
260const MCSymbolData *MCAssembler::getAtomForAddress(const MCAsmLayout &Layout,
261 const MCSectionData *Section,
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000262 uint64_t Address) const {
263 const MCSymbolData *Best = 0;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000264 uint64_t BestAddress = 0;
265
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000266 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
267 ie = symbol_end(); it != ie; ++it) {
268 // Ignore non-linker visible symbols.
269 if (!isSymbolLinkerVisible(it))
270 continue;
271
272 // Ignore symbols not in the same section.
273 if (!it->getFragment() || it->getFragment()->getParent() != Section)
274 continue;
275
276 // Otherwise, find the closest symbol preceding this address (ties are
277 // resolved in favor of the last defined symbol).
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000278 uint64_t SymbolAddress = Layout.getSymbolAddress(it);
279 if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000280 Best = it;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000281 BestAddress = SymbolAddress;
282 }
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000283 }
284
285 return Best;
286}
287
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000288// FIXME-PERF: This routine is really slow.
289const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout,
290 const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000291 // Linker visible symbols define atoms.
292 if (isSymbolLinkerVisible(SD))
293 return SD;
294
295 // Absolute and undefined symbols have no defining atom.
296 if (!SD->getFragment())
297 return 0;
298
299 // Otherwise, search by address.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000300 return getAtomForAddress(Layout, SD->getFragment()->getParent(),
301 Layout.getSymbolAddress(SD));
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000302}
303
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000304bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
305 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000306 MCValue &Target, uint64_t &Value) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000307 ++stats::EvaluateFixup;
308
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000309 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
310 llvm_report_error("expected relocatable expression");
311
312 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
313 // doesn't support small relocations, but then under what criteria does the
314 // assembler allow symbol differences?
315
316 Value = Target.getConstant();
317
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000318 bool IsPCRel =
319 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
320 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000321 if (const MCSymbolRefExpr *A = Target.getSymA()) {
322 if (A->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000323 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000324 else
325 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000326 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000327 if (const MCSymbolRefExpr *B = Target.getSymB()) {
328 if (B->getSymbol().isDefined())
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000329 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000330 else
331 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000332 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000333
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000334 // If we are using scattered symbols, determine whether this value is actually
335 // resolved; scattering may cause atoms to move.
336 if (IsResolved && getBackend().hasScatteredSymbols()) {
337 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000338 // If this is a PCrel relocation, find the base atom (identified by its
339 // symbol) that the fixup value is relative to.
340 const MCSymbolData *BaseSymbol = 0;
341 if (IsPCRel) {
342 BaseSymbol = getAtomForAddress(
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000343 Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset);
Daniel Dunbar034843a2010-03-19 03:18:18 +0000344 if (!BaseSymbol)
345 IsResolved = false;
346 }
347
348 if (IsResolved)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000349 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000350 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000351 } else {
352 const MCSection *BaseSection = 0;
353 if (IsPCRel)
354 BaseSection = &DF->getParent()->getSection();
355
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000356 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000357 BaseSection);
358 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000359 }
360
361 if (IsPCRel)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000362 Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000363
364 return IsResolved;
365}
366
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000367void MCAssembler::LayoutSection(MCSectionData &SD,
368 MCAsmLayout &Layout) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000369 uint64_t Address, StartAddress = Address = Layout.getSectionAddress(&SD);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000370
371 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
372 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000373
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000374 uint64_t FragmentOffset = Address - StartAddress;
375 Layout.setFragmentOffset(&F, FragmentOffset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000376
377 // Evaluate fragment size.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000378 uint64_t EffectiveSize = 0;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000379 switch (F.getKind()) {
380 case MCFragment::FT_Align: {
381 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000382
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000383 EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
384 if (EffectiveSize > AF.getMaxBytesToEmit())
385 EffectiveSize = 0;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000386 break;
387 }
388
389 case MCFragment::FT_Data:
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000390 EffectiveSize = cast<MCDataFragment>(F).getContents().size();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000391 break;
392
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000393 case MCFragment::FT_Fill: {
394 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000395 EffectiveSize = FF.getValueSize() * FF.getCount();
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000396 break;
397 }
398
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000399 case MCFragment::FT_Inst:
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000400 EffectiveSize = cast<MCInstFragment>(F).getInstSize();
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000401 break;
402
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000403 case MCFragment::FT_Org: {
404 MCOrgFragment &OF = cast<MCOrgFragment>(F);
405
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000406 int64_t TargetLocation;
407 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
408 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000409
410 // FIXME: We need a way to communicate this error.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000411 int64_t Offset = TargetLocation - FragmentOffset;
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000412 if (Offset < 0)
413 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000414 "' (at offset '" + Twine(FragmentOffset) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000415
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000416 EffectiveSize = Offset;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000417 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000418 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000419
420 case MCFragment::FT_ZeroFill: {
421 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
422
423 // Align the fragment offset; it is safe to adjust the offset freely since
424 // this is only in virtual sections.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000425 //
426 // FIXME: We shouldn't be doing this here.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +0000427 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000428 Layout.setFragmentOffset(&F, Address - StartAddress);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000429
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000430 EffectiveSize = ZFF.getSize();
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000431 break;
432 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000433 }
434
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000435 Layout.setFragmentEffectiveSize(&F, EffectiveSize);
436 Address += EffectiveSize;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000437 }
438
Daniel Dunbar6742e342009-08-26 04:13:32 +0000439 // Set the section sizes.
Daniel Dunbar5d428512010-03-25 02:00:07 +0000440 Layout.setSectionSize(&SD, Address - StartAddress);
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000441 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbar5d428512010-03-25 02:00:07 +0000442 Layout.setSectionFileSize(&SD, 0);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000443 else
Daniel Dunbar5d428512010-03-25 02:00:07 +0000444 Layout.setSectionFileSize(&SD, Address - StartAddress);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000445}
446
Daniel Dunbar53b23382010-03-19 09:28:59 +0000447/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000448static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
449 const MCFragment &F, MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000450 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000451 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000452
Daniel Dunbarff547842010-03-23 23:47:14 +0000453 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000454
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000455 // FIXME: Embed in fragments instead?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000456 uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000457 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000458 case MCFragment::FT_Align: {
459 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000460 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000461
462 // FIXME: This error shouldn't actually occur (the front end should emit
463 // multiple .align directives to enforce the semantics it wants), but is
464 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000465 if (Count * AF.getValueSize() != FragmentSize)
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000466 llvm_report_error("undefined .align directive, value size '" +
467 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000468 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000469 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000470
Kevin Enderby6e720482010-02-23 18:26:34 +0000471 // See if we are aligning with nops, and if so do that first to try to fill
472 // the Count bytes. Then if that did not fill any bytes or there are any
473 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000474 // If we are aligning with nops, ask that target to emit the right data.
Kevin Enderby6e720482010-02-23 18:26:34 +0000475 if (AF.getEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000476 if (!Asm.getBackend().WriteNopData(Count, OW))
477 llvm_report_error("unable to write nop sequence of " +
478 Twine(Count) + " bytes");
479 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000480 }
481
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000482 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000483 for (uint64_t i = 0; i != Count; ++i) {
484 switch (AF.getValueSize()) {
485 default:
486 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000487 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
488 case 2: OW->Write16(uint16_t(AF.getValue())); break;
489 case 4: OW->Write32(uint32_t(AF.getValue())); break;
490 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000491 }
492 }
493 break;
494 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000495
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000496 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000497 MCDataFragment &DF = cast<MCDataFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000498 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000499 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000500 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000501 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000502
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000503 case MCFragment::FT_Fill: {
504 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000505 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
506 switch (FF.getValueSize()) {
507 default:
508 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000509 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
510 case 2: OW->Write16(uint16_t(FF.getValue())); break;
511 case 4: OW->Write32(uint32_t(FF.getValue())); break;
512 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000513 }
514 }
515 break;
516 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000517
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000518 case MCFragment::FT_Inst:
519 llvm_unreachable("unexpected inst fragment after lowering");
520 break;
521
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000522 case MCFragment::FT_Org: {
523 MCOrgFragment &OF = cast<MCOrgFragment>(F);
524
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000525 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000526 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000527
528 break;
529 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000530
531 case MCFragment::FT_ZeroFill: {
532 assert(0 && "Invalid zero fill fragment in concrete section!");
533 break;
534 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000535 }
536
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000537 assert(OW->getStream().tell() - Start == FragmentSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000538}
539
Daniel Dunbar53b23382010-03-19 09:28:59 +0000540void MCAssembler::WriteSectionData(const MCSectionData *SD,
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000541 const MCAsmLayout &Layout,
Daniel Dunbar53b23382010-03-19 09:28:59 +0000542 MCObjectWriter *OW) const {
Daniel Dunbar5d428512010-03-25 02:00:07 +0000543 uint64_t SectionSize = Layout.getSectionSize(SD);
544 uint64_t SectionFileSize = Layout.getSectionFileSize(SD);
545
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000546 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000547 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar5d428512010-03-25 02:00:07 +0000548 assert(SectionFileSize == 0 && "Invalid size for section!");
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000549 return;
550 }
551
Daniel Dunbar53b23382010-03-19 09:28:59 +0000552 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000553 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000554
Daniel Dunbar53b23382010-03-19 09:28:59 +0000555 for (MCSectionData::const_iterator it = SD->begin(),
556 ie = SD->end(); it != ie; ++it)
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000557 WriteFragmentData(*this, Layout, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000558
Daniel Dunbar6742e342009-08-26 04:13:32 +0000559 // Add section padding.
Daniel Dunbar5d428512010-03-25 02:00:07 +0000560 assert(SectionFileSize >= SectionSize && "Invalid section sizes!");
561 OW->WriteZeros(SectionFileSize - SectionSize);
Daniel Dunbar6742e342009-08-26 04:13:32 +0000562
Daniel Dunbar5d428512010-03-25 02:00:07 +0000563 assert(OW->getStream().tell() - Start == SectionFileSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000564}
565
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000566void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000567 DEBUG_WITH_TYPE("mc-dump", {
568 llvm::errs() << "assembler backend - pre-layout\n--\n";
569 dump(); });
570
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000571 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000572 MCAsmLayout Layout(*this);
573 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000574 continue;
575
576 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000577 llvm::errs() << "assembler backend - post-relaxation\n--\n";
578 dump(); });
579
580 // Finalize the layout, including fragment lowering.
581 FinishLayout(Layout);
582
583 DEBUG_WITH_TYPE("mc-dump", {
584 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000585 dump(); });
586
Daniel Dunbarff547842010-03-23 23:47:14 +0000587 uint64_t StartOffset = OS.tell();
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000588 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
589 if (!Writer)
590 llvm_report_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000591
592 // Allow the object writer a chance to perform post-layout binding (for
593 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000594 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000595
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000596 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000597 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
598 for (MCSectionData::iterator it2 = it->begin(),
599 ie2 = it->end(); it2 != ie2; ++it2) {
600 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
601 if (!DF)
602 continue;
603
604 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
605 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
606 MCAsmFixup &Fixup = *it3;
607
608 // Evaluate the fixup.
609 MCValue Target;
610 uint64_t FixedValue;
611 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
612 // The fixup was unresolved, we need a relocation. Inform the object
613 // writer of the relocation, and give it an opportunity to adjust the
614 // fixup value if need be.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000615 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000616 }
617
Daniel Dunbar87190c42010-03-19 09:28:12 +0000618 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000619 }
620 }
621 }
622
Daniel Dunbarbacba992010-03-19 07:09:33 +0000623 // Write the object file.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000624 Writer->WriteObject(*this, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000625 OS.flush();
Daniel Dunbarff547842010-03-23 23:47:14 +0000626
627 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000628}
629
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000630bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
631 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000632 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000633 // If we cannot resolve the fixup value, it requires relaxation.
634 MCValue Target;
635 uint64_t Value;
636 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
637 return true;
638
639 // Otherwise, relax if the value is too big for a (signed) i8.
640 return int64_t(Value) != int64_t(int8_t(Value));
641}
642
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000643bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
644 const MCAsmLayout &Layout) const {
645 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
646 // are intentionally pushing out inst fragments, or because we relaxed a
647 // previous instruction to one that doesn't need relaxation.
648 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
649 return false;
650
651 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
652 ie = IF->fixup_end(); it != ie; ++it)
653 if (FixupNeedsRelaxation(*it, IF, Layout))
654 return true;
655
656 return false;
657}
658
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000659bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000660 ++stats::RelaxationSteps;
661
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000662 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000663 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000664 MCSectionData *Prev = 0;
665 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000666 MCSectionData &SD = *it;
667
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000668 // Skip virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000669 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000670 continue;
671
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000672 // Align this section if necessary by adding padding bytes to the previous
673 // section.
674 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
675 assert(Prev && "Missing prev section!");
Daniel Dunbar5d428512010-03-25 02:00:07 +0000676 Layout.setSectionFileSize(Prev, Layout.getSectionFileSize(Prev) + Pad);
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000677 Address += Pad;
678 }
Daniel Dunbar6742e342009-08-26 04:13:32 +0000679
680 // Layout the section fragments and its size.
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000681 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000682 LayoutSection(SD, Layout);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000683 Address += Layout.getSectionFileSize(&SD);
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000684
685 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +0000686 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000687
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000688 // Layout the virtual sections.
689 for (iterator it = begin(), ie = end(); it != ie; ++it) {
690 MCSectionData &SD = *it;
691
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000692 if (!getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000693 continue;
694
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000695 // Align this section if necessary by adding padding bytes to the previous
696 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +0000697 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000698 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000699
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000700 Layout.setSectionAddress(&SD, Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000701 LayoutSection(SD, Layout);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000702 Address += Layout.getSectionSize(&SD);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000703 }
704
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000705 // Scan for fragments that need relaxation.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000706 for (iterator it = begin(), ie = end(); it != ie; ++it) {
707 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000708
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000709 for (MCSectionData::iterator it2 = SD.begin(),
710 ie2 = SD.end(); it2 != ie2; ++it2) {
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000711 // Check if this is an instruction fragment that needs relaxation.
712 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
713 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000714 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000715
Daniel Dunbarff547842010-03-23 23:47:14 +0000716 ++stats::RelaxedInstructions;
717
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000718 // FIXME-PERF: We could immediately lower out instructions if we can tell
719 // they are fully resolved, to avoid retesting on later passes.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000720
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000721 // Relax the fragment.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000722
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000723 MCInst Relaxed;
724 getBackend().RelaxInstruction(IF, Relaxed);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000725
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000726 // Encode the new instruction.
727 //
728 // FIXME-PERF: If it matters, we could let the target do this. It can
729 // probably do so more efficiently in many cases.
730 SmallVector<MCFixup, 4> Fixups;
731 SmallString<256> Code;
732 raw_svector_ostream VecOS(Code);
733 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
734 VecOS.flush();
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000735
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000736 // Update the instruction fragment.
737 IF->setInst(Relaxed);
738 IF->getCode() = Code;
739 IF->getFixups().clear();
740 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
741 MCFixup &F = Fixups[i];
742 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
743 F.getKind()));
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000744 }
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000745
746 // Restart layout.
747 //
748 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
749 // smart MCAsmLayout object.
750 return true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000751 }
752 }
753
754 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000755}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000756
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000757void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
758 // Lower out any instruction fragments, to simplify the fixup application and
759 // output.
760 //
761 // FIXME-PERF: We don't have to do this, but the assumption is that it is
762 // cheap (we will mostly end up eliminating fragments and appending on to data
763 // fragments), so the extra complexity downstream isn't worth it. Evaluate
764 // this assumption.
765 for (iterator it = begin(), ie = end(); it != ie; ++it) {
766 MCSectionData &SD = *it;
767
768 for (MCSectionData::iterator it2 = SD.begin(),
769 ie2 = SD.end(); it2 != ie2; ++it2) {
770 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
771 if (!IF)
772 continue;
773
774 // Create a new data fragment for the instruction.
775 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000776 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000777 MCDataFragment *DF = new MCDataFragment();
778 SD.getFragmentList().insert(it2, DF);
779
780 // Update the data fragments layout data.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000781 //
782 // FIXME: Add MCAsmLayout utility for this.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000783 DF->setParent(IF->getParent());
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000784 Layout.setFragmentOffset(DF, Layout.getFragmentOffset(IF));
785 Layout.setFragmentEffectiveSize(DF, Layout.getFragmentEffectiveSize(IF));
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000786
Daniel Dunbar9799de92010-03-23 01:39:05 +0000787 // Copy in the data and the fixups.
788 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
789 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
790 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000791
792 // Delete the instruction fragment and update the iterator.
793 SD.getFragmentList().erase(IF);
794 it2 = DF;
795 }
796 }
797}
798
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000799// Debugging methods
800
801namespace llvm {
802
803raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000804 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
805 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000806 return OS;
807}
808
809}
810
811void MCFragment::dump() {
812 raw_ostream &OS = llvm::errs();
813
814 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000815 << " EffectiveSize:" << EffectiveSize;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000816
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000817 OS << ">";
818}
819
820void MCAlignFragment::dump() {
821 raw_ostream &OS = llvm::errs();
822
823 OS << "<MCAlignFragment ";
824 this->MCFragment::dump();
825 OS << "\n ";
826 OS << " Alignment:" << getAlignment()
827 << " Value:" << getValue() << " ValueSize:" << getValueSize()
828 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
829}
830
831void MCDataFragment::dump() {
832 raw_ostream &OS = llvm::errs();
833
834 OS << "<MCDataFragment ";
835 this->MCFragment::dump();
836 OS << "\n ";
837 OS << " Contents:[";
838 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
839 if (i) OS << ",";
840 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
841 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000842 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000843
844 if (!getFixups().empty()) {
845 OS << ",\n ";
846 OS << " Fixups:[";
847 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000848 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000849 OS << *it;
850 }
851 OS << "]";
852 }
853
854 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000855}
856
857void MCFillFragment::dump() {
858 raw_ostream &OS = llvm::errs();
859
860 OS << "<MCFillFragment ";
861 this->MCFragment::dump();
862 OS << "\n ";
863 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
864 << " Count:" << getCount() << ">";
865}
866
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000867void MCInstFragment::dump() {
868 raw_ostream &OS = llvm::errs();
869
870 OS << "<MCInstFragment ";
871 this->MCFragment::dump();
872 OS << "\n ";
873 OS << " Inst:";
874 getInst().dump_pretty(OS);
875 OS << ">";
876}
877
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000878void MCOrgFragment::dump() {
879 raw_ostream &OS = llvm::errs();
880
881 OS << "<MCOrgFragment ";
882 this->MCFragment::dump();
883 OS << "\n ";
884 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
885}
886
887void MCZeroFillFragment::dump() {
888 raw_ostream &OS = llvm::errs();
889
890 OS << "<MCZeroFillFragment ";
891 this->MCFragment::dump();
892 OS << "\n ";
893 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
894}
895
896void MCSectionData::dump() {
897 raw_ostream &OS = llvm::errs();
898
899 OS << "<MCSectionData";
900 OS << " Alignment:" << getAlignment() << " Address:" << Address
901 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000902 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000903 for (iterator it = begin(), ie = end(); it != ie; ++it) {
904 if (it != begin()) OS << ",\n ";
905 it->dump();
906 }
907 OS << "]>";
908}
909
910void MCSymbolData::dump() {
911 raw_ostream &OS = llvm::errs();
912
913 OS << "<MCSymbolData Symbol:" << getSymbol()
914 << " Fragment:" << getFragment() << " Offset:" << getOffset()
915 << " Flags:" << getFlags() << " Index:" << getIndex();
916 if (isCommon())
917 OS << " (common, size:" << getCommonSize()
918 << " align: " << getCommonAlignment() << ")";
919 if (isExternal())
920 OS << " (external)";
921 if (isPrivateExtern())
922 OS << " (private extern)";
923 OS << ">";
924}
925
926void MCAssembler::dump() {
927 raw_ostream &OS = llvm::errs();
928
929 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000930 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000931 for (iterator it = begin(), ie = end(); it != ie; ++it) {
932 if (it != begin()) OS << ",\n ";
933 it->dump();
934 }
935 OS << "],\n";
936 OS << " Symbols:[";
937
938 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000939 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000940 it->dump();
941 }
942 OS << "]>\n";
943}