Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- 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 Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "assembler" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | b36052f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCCodeEmitter.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCObjectWriter.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSymbol.h" |
| 17 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetAsmBackend.h" |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 28 | #include <vector> |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 31 | namespace { |
| 32 | namespace stats { |
| 33 | STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); |
| 34 | STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 35 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments"); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 36 | STATISTIC(EvaluateFixup, "Number of evaluated fixups"); |
| 37 | STATISTIC(ObjectBytes, "Number of emitted object file bytes"); |
| 38 | } |
| 39 | } |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 41 | // 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 Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 46 | /* *** */ |
| 47 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 48 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 49 | } |
| 50 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 51 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 52 | : Kind(_Kind), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 53 | Parent(_Parent), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 54 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 55 | { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 56 | if (Parent) |
| 57 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 60 | MCFragment::~MCFragment() { |
| 61 | } |
| 62 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 63 | uint64_t MCFragment::getAddress() const { |
| 64 | assert(getParent() && "Missing Section!"); |
| 65 | return getParent()->getAddress() + Offset; |
| 66 | } |
| 67 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 68 | /* *** */ |
| 69 | |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 70 | MCSectionData::MCSectionData() : Section(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 71 | |
| 72 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 73 | : Section(&_Section), |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 74 | Alignment(1), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 75 | Address(~UINT64_C(0)), |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 76 | Size(~UINT64_C(0)), |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 77 | FileSize(~UINT64_C(0)), |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 78 | HasInstructions(false) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 79 | { |
| 80 | if (A) |
| 81 | A->getSectionList().push_back(this); |
| 82 | } |
| 83 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 84 | /* *** */ |
| 85 | |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 86 | MCSymbolData::MCSymbolData() : Symbol(0) {} |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 88 | MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 89 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 90 | : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 91 | IsExternal(false), IsPrivateExtern(false), |
| 92 | CommonSize(0), CommonAlign(0), Flags(0), Index(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 93 | { |
| 94 | if (A) |
| 95 | A->getSymbolList().push_back(this); |
| 96 | } |
| 97 | |
| 98 | /* *** */ |
| 99 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 100 | MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, |
Daniel Dunbar | cf871e5 | 2010-03-19 10:43:18 +0000 | [diff] [blame] | 101 | MCCodeEmitter &_Emitter, raw_ostream &_OS) |
| 102 | : Context(_Context), Backend(_Backend), Emitter(_Emitter), |
| 103 | OS(_OS), SubsectionsViaSymbols(false) |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 104 | { |
| 105 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 106 | |
| 107 | MCAssembler::~MCAssembler() { |
| 108 | } |
| 109 | |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 110 | static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm, |
| 111 | const MCAsmFixup &Fixup, |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 112 | const MCValue Target, |
| 113 | const MCSection *BaseSection) { |
| 114 | // The effective fixup address is |
| 115 | // addr(atom(A)) + offset(A) |
| 116 | // - addr(atom(B)) - offset(B) |
| 117 | // - addr(<base symbol>) + <fixup offset from base symbol> |
| 118 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 119 | // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0. |
| 120 | // |
| 121 | // The simple (Darwin, except on x86_64) way of dealing with this was to |
| 122 | // assume that any reference to a temporary symbol *must* be a temporary |
| 123 | // symbol in the same atom, unless the sections differ. Therefore, any PCrel |
| 124 | // relocation to a temporary symbol (in the same section) is fully |
| 125 | // resolved. This also works in conjunction with absolutized .set, which |
| 126 | // requires the compiler to use .set to absolutize the differences between |
| 127 | // symbols which the compiler knows to be assembly time constants, so we don't |
| 128 | // need to worry about consider symbol differences fully resolved. |
| 129 | |
| 130 | // Non-relative fixups are only resolved if constant. |
| 131 | if (!BaseSection) |
| 132 | return Target.isAbsolute(); |
| 133 | |
| 134 | // Otherwise, relative fixups are only resolved if not a difference and the |
| 135 | // target is a temporary in the same section. |
| 136 | if (Target.isAbsolute() || Target.getSymB()) |
| 137 | return false; |
| 138 | |
| 139 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 140 | if (!A->isTemporary() || !A->isInSection() || |
| 141 | &A->getSection() != BaseSection) |
| 142 | return false; |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 147 | static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, |
| 148 | const MCAsmFixup &Fixup, |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 149 | const MCValue Target, |
| 150 | const MCSymbolData *BaseSymbol) { |
| 151 | // The effective fixup address is |
| 152 | // addr(atom(A)) + offset(A) |
| 153 | // - addr(atom(B)) - offset(B) |
| 154 | // - addr(BaseSymbol) + <fixup offset from base symbol> |
| 155 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 156 | // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0. |
| 157 | // |
| 158 | // Note that "false" is almost always conservatively correct (it means we emit |
| 159 | // a relocation which is unnecessary), except when it would force us to emit a |
| 160 | // relocation which the target cannot encode. |
| 161 | |
| 162 | const MCSymbolData *A_Base = 0, *B_Base = 0; |
| 163 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 164 | // Modified symbol references cannot be resolved. |
| 165 | if (A->getKind() != MCSymbolRefExpr::VK_None) |
| 166 | return false; |
| 167 | |
| 168 | A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol())); |
| 169 | if (!A_Base) |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 174 | // Modified symbol references cannot be resolved. |
| 175 | if (B->getKind() != MCSymbolRefExpr::VK_None) |
| 176 | return false; |
| 177 | |
| 178 | B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol())); |
| 179 | if (!B_Base) |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // If there is no base, A and B have to be the same atom for this fixup to be |
| 184 | // fully resolved. |
| 185 | if (!BaseSymbol) |
| 186 | return A_Base == B_Base; |
| 187 | |
| 188 | // Otherwise, B must be missing and A must be the base. |
| 189 | return !B_Base && BaseSymbol == A_Base; |
| 190 | } |
| 191 | |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 192 | bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const { |
| 193 | // Non-temporary labels should always be visible to the linker. |
| 194 | if (!SD->getSymbol().isTemporary()) |
| 195 | return true; |
| 196 | |
| 197 | // Absolute temporary labels are never visible. |
| 198 | if (!SD->getFragment()) |
| 199 | return false; |
| 200 | |
| 201 | // Otherwise, check if the section requires symbols even for temporary labels. |
| 202 | return getBackend().doesSectionRequireSymbols( |
| 203 | SD->getFragment()->getParent()->getSection()); |
| 204 | } |
| 205 | |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 206 | const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section, |
| 207 | uint64_t Address) const { |
| 208 | const MCSymbolData *Best = 0; |
| 209 | for (MCAssembler::const_symbol_iterator it = symbol_begin(), |
| 210 | ie = symbol_end(); it != ie; ++it) { |
| 211 | // Ignore non-linker visible symbols. |
| 212 | if (!isSymbolLinkerVisible(it)) |
| 213 | continue; |
| 214 | |
| 215 | // Ignore symbols not in the same section. |
| 216 | if (!it->getFragment() || it->getFragment()->getParent() != Section) |
| 217 | continue; |
| 218 | |
| 219 | // Otherwise, find the closest symbol preceding this address (ties are |
| 220 | // resolved in favor of the last defined symbol). |
| 221 | if (it->getAddress() <= Address && |
| 222 | (!Best || it->getAddress() >= Best->getAddress())) |
| 223 | Best = it; |
| 224 | } |
| 225 | |
| 226 | return Best; |
| 227 | } |
| 228 | |
| 229 | const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { |
| 230 | // Linker visible symbols define atoms. |
| 231 | if (isSymbolLinkerVisible(SD)) |
| 232 | return SD; |
| 233 | |
| 234 | // Absolute and undefined symbols have no defining atom. |
| 235 | if (!SD->getFragment()) |
| 236 | return 0; |
| 237 | |
| 238 | // Otherwise, search by address. |
| 239 | return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress()); |
| 240 | } |
| 241 | |
Daniel Dunbar | 9d39e61 | 2010-03-22 21:49:41 +0000 | [diff] [blame] | 242 | bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, |
| 243 | const MCAsmFixup &Fixup, const MCFragment *DF, |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 244 | MCValue &Target, uint64_t &Value) const { |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 245 | ++stats::EvaluateFixup; |
| 246 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 247 | if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout)) |
| 248 | llvm_report_error("expected relocatable expression"); |
| 249 | |
| 250 | // FIXME: How do non-scattered symbols work in ELF? I presume the linker |
| 251 | // doesn't support small relocations, but then under what criteria does the |
| 252 | // assembler allow symbol differences? |
| 253 | |
| 254 | Value = Target.getConstant(); |
| 255 | |
Daniel Dunbar | b36052f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 256 | bool IsPCRel = |
| 257 | Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 258 | bool IsResolved = true; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 259 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 260 | if (A->getSymbol().isDefined()) |
| 261 | Value += getSymbolData(A->getSymbol()).getAddress(); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 262 | else |
| 263 | IsResolved = false; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 264 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 265 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 266 | if (B->getSymbol().isDefined()) |
| 267 | Value -= getSymbolData(B->getSymbol()).getAddress(); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 268 | else |
| 269 | IsResolved = false; |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 270 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 271 | |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 272 | // If we are using scattered symbols, determine whether this value is actually |
| 273 | // resolved; scattering may cause atoms to move. |
| 274 | if (IsResolved && getBackend().hasScatteredSymbols()) { |
| 275 | if (getBackend().hasReliableSymbolDifference()) { |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 276 | // If this is a PCrel relocation, find the base atom (identified by its |
| 277 | // symbol) that the fixup value is relative to. |
| 278 | const MCSymbolData *BaseSymbol = 0; |
| 279 | if (IsPCRel) { |
| 280 | BaseSymbol = getAtomForAddress( |
| 281 | DF->getParent(), DF->getAddress() + Fixup.Offset); |
| 282 | if (!BaseSymbol) |
| 283 | IsResolved = false; |
| 284 | } |
| 285 | |
| 286 | if (IsResolved) |
Daniel Dunbar | c6f5982 | 2010-03-22 21:49:38 +0000 | [diff] [blame] | 287 | IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target, |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 288 | BaseSymbol); |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 289 | } else { |
| 290 | const MCSection *BaseSection = 0; |
| 291 | if (IsPCRel) |
| 292 | BaseSection = &DF->getParent()->getSection(); |
| 293 | |
Daniel Dunbar | c6f5982 | 2010-03-22 21:49:38 +0000 | [diff] [blame] | 294 | IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target, |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 295 | BaseSection); |
| 296 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (IsPCRel) |
Daniel Dunbar | da3e9f7 | 2010-03-13 02:38:00 +0000 | [diff] [blame] | 300 | Value -= DF->getAddress() + Fixup.Offset; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 301 | |
| 302 | return IsResolved; |
| 303 | } |
| 304 | |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 305 | void MCAssembler::LayoutSection(MCSectionData &SD, |
| 306 | MCAsmLayout &Layout) { |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 307 | uint64_t Address = SD.getAddress(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 308 | |
| 309 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 310 | MCFragment &F = *it; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 312 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 313 | |
| 314 | // Evaluate fragment size. |
| 315 | switch (F.getKind()) { |
| 316 | case MCFragment::FT_Align: { |
| 317 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 318 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 319 | uint64_t Size = OffsetToAlignment(Address, AF.getAlignment()); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 320 | if (Size > AF.getMaxBytesToEmit()) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 321 | AF.setFileSize(0); |
| 322 | else |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 323 | AF.setFileSize(Size); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | case MCFragment::FT_Data: |
Daniel Dunbar | 2a6e3f5 | 2010-03-22 20:35:43 +0000 | [diff] [blame] | 328 | F.setFileSize(cast<MCDataFragment>(F).getContents().size()); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 329 | break; |
| 330 | |
Daniel Dunbar | 2a6e3f5 | 2010-03-22 20:35:43 +0000 | [diff] [blame] | 331 | case MCFragment::FT_Fill: { |
| 332 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 333 | F.setFileSize(FF.getValueSize() * FF.getCount()); |
| 334 | break; |
| 335 | } |
| 336 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 337 | case MCFragment::FT_Inst: |
| 338 | F.setFileSize(cast<MCInstFragment>(F).getInstSize()); |
| 339 | break; |
| 340 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 341 | case MCFragment::FT_Org: { |
| 342 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 343 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 344 | int64_t TargetLocation; |
| 345 | if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout)) |
| 346 | llvm_report_error("expected assembly-time absolute expression"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 347 | |
| 348 | // FIXME: We need a way to communicate this error. |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 349 | int64_t Offset = TargetLocation - F.getOffset(); |
| 350 | if (Offset < 0) |
| 351 | llvm_report_error("invalid .org offset '" + Twine(TargetLocation) + |
| 352 | "' (at offset '" + Twine(F.getOffset()) + "'"); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 353 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 354 | F.setFileSize(Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 355 | break; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 356 | } |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 357 | |
| 358 | case MCFragment::FT_ZeroFill: { |
| 359 | MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F); |
| 360 | |
| 361 | // Align the fragment offset; it is safe to adjust the offset freely since |
| 362 | // this is only in virtual sections. |
Daniel Dunbar | 37fad5c | 2010-03-08 21:10:42 +0000 | [diff] [blame] | 363 | Address = RoundUpToAlignment(Address, ZFF.getAlignment()); |
| 364 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 365 | |
| 366 | // FIXME: This is misnamed. |
| 367 | F.setFileSize(ZFF.getSize()); |
| 368 | break; |
| 369 | } |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 372 | Address += F.getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 375 | // Set the section sizes. |
| 376 | SD.setSize(Address - SD.getAddress()); |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 377 | if (getBackend().isVirtualSection(SD.getSection())) |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 378 | SD.setFileSize(0); |
| 379 | else |
| 380 | SD.setFileSize(Address - SD.getAddress()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 383 | /// WriteFragmentData - Write the \arg F data to the output file. |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 384 | static void WriteFragmentData(const MCAssembler &Asm, const MCFragment &F, |
| 385 | MCObjectWriter *OW) { |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 386 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 387 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 389 | ++stats::EmittedFragments; |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 390 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 391 | // FIXME: Embed in fragments instead? |
| 392 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 393 | case MCFragment::FT_Align: { |
| 394 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 395 | uint64_t Count = AF.getFileSize() / AF.getValueSize(); |
| 396 | |
| 397 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 398 | // multiple .align directives to enforce the semantics it wants), but is |
| 399 | // severe enough that we want to report it. How to handle this? |
| 400 | if (Count * AF.getValueSize() != AF.getFileSize()) |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 401 | llvm_report_error("undefined .align directive, value size '" + |
| 402 | Twine(AF.getValueSize()) + |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 403 | "' is not a divisor of padding size '" + |
| 404 | Twine(AF.getFileSize()) + "'"); |
| 405 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 406 | // See if we are aligning with nops, and if so do that first to try to fill |
| 407 | // the Count bytes. Then if that did not fill any bytes or there are any |
| 408 | // bytes left to fill use the the Value and ValueSize to fill the rest. |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 409 | // If we are aligning with nops, ask that target to emit the right data. |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 410 | if (AF.getEmitNops()) { |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 411 | if (!Asm.getBackend().WriteNopData(Count, OW)) |
| 412 | llvm_report_error("unable to write nop sequence of " + |
| 413 | Twine(Count) + " bytes"); |
| 414 | break; |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 417 | // Otherwise, write out in multiples of the value size. |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 418 | for (uint64_t i = 0; i != Count; ++i) { |
| 419 | switch (AF.getValueSize()) { |
| 420 | default: |
| 421 | assert(0 && "Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 422 | case 1: OW->Write8 (uint8_t (AF.getValue())); break; |
| 423 | case 2: OW->Write16(uint16_t(AF.getValue())); break; |
| 424 | case 4: OW->Write32(uint32_t(AF.getValue())); break; |
| 425 | case 8: OW->Write64(uint64_t(AF.getValue())); break; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | break; |
| 429 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 430 | |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 431 | case MCFragment::FT_Data: { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 432 | MCDataFragment &DF = cast<MCDataFragment>(F); |
| 433 | assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!"); |
| 434 | OW->WriteBytes(DF.getContents().str()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 435 | break; |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 436 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 437 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 438 | case MCFragment::FT_Fill: { |
| 439 | MCFillFragment &FF = cast<MCFillFragment>(F); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 440 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 441 | switch (FF.getValueSize()) { |
| 442 | default: |
| 443 | assert(0 && "Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 444 | case 1: OW->Write8 (uint8_t (FF.getValue())); break; |
| 445 | case 2: OW->Write16(uint16_t(FF.getValue())); break; |
| 446 | case 4: OW->Write32(uint32_t(FF.getValue())); break; |
| 447 | case 8: OW->Write64(uint64_t(FF.getValue())); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | break; |
| 451 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 452 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 453 | case MCFragment::FT_Inst: |
| 454 | llvm_unreachable("unexpected inst fragment after lowering"); |
| 455 | break; |
| 456 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 457 | case MCFragment::FT_Org: { |
| 458 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 459 | |
| 460 | for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i) |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 461 | OW->Write8(uint8_t(OF.getValue())); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 462 | |
| 463 | break; |
| 464 | } |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 465 | |
| 466 | case MCFragment::FT_ZeroFill: { |
| 467 | assert(0 && "Invalid zero fill fragment in concrete section!"); |
| 468 | break; |
| 469 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 472 | assert(OW->getStream().tell() - Start == F.getFileSize()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 475 | void MCAssembler::WriteSectionData(const MCSectionData *SD, |
| 476 | MCObjectWriter *OW) const { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 477 | // Ignore virtual sections. |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 478 | if (getBackend().isVirtualSection(SD->getSection())) { |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 479 | assert(SD->getFileSize() == 0); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 483 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 484 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 485 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 486 | for (MCSectionData::const_iterator it = SD->begin(), |
| 487 | ie = SD->end(); it != ie; ++it) |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 488 | WriteFragmentData(*this, *it, OW); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 490 | // Add section padding. |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 491 | assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!"); |
| 492 | OW->WriteZeros(SD->getFileSize() - SD->getSize()); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 493 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 494 | assert(OW->getStream().tell() - Start == SD->getFileSize()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 497 | void MCAssembler::Finish() { |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 498 | DEBUG_WITH_TYPE("mc-dump", { |
| 499 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 500 | dump(); }); |
| 501 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 502 | // Layout until everything fits. |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 503 | MCAsmLayout Layout(*this); |
| 504 | while (LayoutOnce(Layout)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 505 | continue; |
| 506 | |
| 507 | DEBUG_WITH_TYPE("mc-dump", { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 508 | llvm::errs() << "assembler backend - post-relaxation\n--\n"; |
| 509 | dump(); }); |
| 510 | |
| 511 | // Finalize the layout, including fragment lowering. |
| 512 | FinishLayout(Layout); |
| 513 | |
| 514 | DEBUG_WITH_TYPE("mc-dump", { |
| 515 | llvm::errs() << "assembler backend - final-layout\n--\n"; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 516 | dump(); }); |
| 517 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 518 | uint64_t StartOffset = OS.tell(); |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 519 | llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS)); |
| 520 | if (!Writer) |
| 521 | llvm_report_error("unable to create object writer!"); |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 522 | |
| 523 | // Allow the object writer a chance to perform post-layout binding (for |
| 524 | // example, to set the index fields in the symbol data). |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 525 | Writer->ExecutePostLayoutBinding(*this); |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 526 | |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 527 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 528 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 529 | for (MCSectionData::iterator it2 = it->begin(), |
| 530 | ie2 = it->end(); it2 != ie2; ++it2) { |
| 531 | MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); |
| 532 | if (!DF) |
| 533 | continue; |
| 534 | |
| 535 | for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), |
| 536 | ie3 = DF->fixup_end(); it3 != ie3; ++it3) { |
| 537 | MCAsmFixup &Fixup = *it3; |
| 538 | |
| 539 | // Evaluate the fixup. |
| 540 | MCValue Target; |
| 541 | uint64_t FixedValue; |
| 542 | if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) { |
| 543 | // The fixup was unresolved, we need a relocation. Inform the object |
| 544 | // writer of the relocation, and give it an opportunity to adjust the |
| 545 | // fixup value if need be. |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 546 | Writer->RecordRelocation(*this, DF, Fixup, Target, FixedValue); |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Daniel Dunbar | 87190c4 | 2010-03-19 09:28:12 +0000 | [diff] [blame] | 549 | getBackend().ApplyFixup(Fixup, *DF, FixedValue); |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 554 | // Write the object file. |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 555 | Writer->WriteObject(*this); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 556 | OS.flush(); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 557 | |
| 558 | stats::ObjectBytes += OS.tell() - StartOffset; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Daniel Dunbar | 9d39e61 | 2010-03-22 21:49:41 +0000 | [diff] [blame] | 561 | bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup, |
| 562 | const MCFragment *DF, |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 563 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 564 | // If we cannot resolve the fixup value, it requires relaxation. |
| 565 | MCValue Target; |
| 566 | uint64_t Value; |
| 567 | if (!EvaluateFixup(Layout, Fixup, DF, Target, Value)) |
| 568 | return true; |
| 569 | |
| 570 | // Otherwise, relax if the value is too big for a (signed) i8. |
| 571 | return int64_t(Value) != int64_t(int8_t(Value)); |
| 572 | } |
| 573 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 574 | bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF, |
| 575 | const MCAsmLayout &Layout) const { |
| 576 | // If this inst doesn't ever need relaxation, ignore it. This occurs when we |
| 577 | // are intentionally pushing out inst fragments, or because we relaxed a |
| 578 | // previous instruction to one that doesn't need relaxation. |
| 579 | if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups())) |
| 580 | return false; |
| 581 | |
| 582 | for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(), |
| 583 | ie = IF->fixup_end(); it != ie; ++it) |
| 584 | if (FixupNeedsRelaxation(*it, IF, Layout)) |
| 585 | return true; |
| 586 | |
| 587 | return false; |
| 588 | } |
| 589 | |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 590 | bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) { |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 591 | ++stats::RelaxationSteps; |
| 592 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 593 | // Layout the concrete sections and fragments. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 594 | uint64_t Address = 0; |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 595 | MCSectionData *Prev = 0; |
| 596 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 597 | MCSectionData &SD = *it; |
| 598 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 599 | // Skip virtual sections. |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 600 | if (getBackend().isVirtualSection(SD.getSection())) |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 601 | continue; |
| 602 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 603 | // Align this section if necessary by adding padding bytes to the previous |
| 604 | // section. |
| 605 | if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) { |
| 606 | assert(Prev && "Missing prev section!"); |
| 607 | Prev->setFileSize(Prev->getFileSize() + Pad); |
| 608 | Address += Pad; |
| 609 | } |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 610 | |
| 611 | // Layout the section fragments and its size. |
| 612 | SD.setAddress(Address); |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 613 | LayoutSection(SD, Layout); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 614 | Address += SD.getFileSize(); |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 615 | |
| 616 | Prev = &SD; |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 617 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 618 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 619 | // Layout the virtual sections. |
| 620 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 621 | MCSectionData &SD = *it; |
| 622 | |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 623 | if (!getBackend().isVirtualSection(SD.getSection())) |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 624 | continue; |
| 625 | |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 626 | // Align this section if necessary by adding padding bytes to the previous |
| 627 | // section. |
Daniel Dunbar | f8b8ad7 | 2010-03-09 01:12:20 +0000 | [diff] [blame] | 628 | if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 629 | Address += Pad; |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 630 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 631 | SD.setAddress(Address); |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 632 | LayoutSection(SD, Layout); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 633 | Address += SD.getSize(); |
| 634 | } |
| 635 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 636 | // Scan for fragments that need relaxation. |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 637 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 638 | MCSectionData &SD = *it; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 639 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 640 | for (MCSectionData::iterator it2 = SD.begin(), |
| 641 | ie2 = SD.end(); it2 != ie2; ++it2) { |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 642 | // Check if this is an instruction fragment that needs relaxation. |
| 643 | MCInstFragment *IF = dyn_cast<MCInstFragment>(it2); |
| 644 | if (!IF || !FragmentNeedsRelaxation(IF, Layout)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 645 | continue; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 646 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame^] | 647 | ++stats::RelaxedInstructions; |
| 648 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 649 | // FIXME-PERF: We could immediately lower out instructions if we can tell |
| 650 | // they are fully resolved, to avoid retesting on later passes. |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 651 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 652 | // Relax the fragment. |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 653 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 654 | MCInst Relaxed; |
| 655 | getBackend().RelaxInstruction(IF, Relaxed); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 656 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 657 | // Encode the new instruction. |
| 658 | // |
| 659 | // FIXME-PERF: If it matters, we could let the target do this. It can |
| 660 | // probably do so more efficiently in many cases. |
| 661 | SmallVector<MCFixup, 4> Fixups; |
| 662 | SmallString<256> Code; |
| 663 | raw_svector_ostream VecOS(Code); |
| 664 | getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups); |
| 665 | VecOS.flush(); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 666 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 667 | // Update the instruction fragment. |
| 668 | IF->setInst(Relaxed); |
| 669 | IF->getCode() = Code; |
| 670 | IF->getFixups().clear(); |
| 671 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 672 | MCFixup &F = Fixups[i]; |
| 673 | IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(), |
| 674 | F.getKind())); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 675 | } |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 676 | |
| 677 | // Restart layout. |
| 678 | // |
| 679 | // FIXME-PERF: This is O(N^2), but will be eliminated once we have a |
| 680 | // smart MCAsmLayout object. |
| 681 | return true; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | return false; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 686 | } |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 687 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 688 | void MCAssembler::FinishLayout(MCAsmLayout &Layout) { |
| 689 | // Lower out any instruction fragments, to simplify the fixup application and |
| 690 | // output. |
| 691 | // |
| 692 | // FIXME-PERF: We don't have to do this, but the assumption is that it is |
| 693 | // cheap (we will mostly end up eliminating fragments and appending on to data |
| 694 | // fragments), so the extra complexity downstream isn't worth it. Evaluate |
| 695 | // this assumption. |
| 696 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 697 | MCSectionData &SD = *it; |
| 698 | |
| 699 | for (MCSectionData::iterator it2 = SD.begin(), |
| 700 | ie2 = SD.end(); it2 != ie2; ++it2) { |
| 701 | MCInstFragment *IF = dyn_cast<MCInstFragment>(it2); |
| 702 | if (!IF) |
| 703 | continue; |
| 704 | |
| 705 | // Create a new data fragment for the instruction. |
| 706 | // |
Daniel Dunbar | 337055e | 2010-03-23 03:13:05 +0000 | [diff] [blame] | 707 | // FIXME-PERF: Reuse previous data fragment if possible. |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 708 | MCDataFragment *DF = new MCDataFragment(); |
| 709 | SD.getFragmentList().insert(it2, DF); |
| 710 | |
| 711 | // Update the data fragments layout data. |
Daniel Dunbar | 9799de9 | 2010-03-23 01:39:05 +0000 | [diff] [blame] | 712 | DF->setParent(IF->getParent()); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 713 | DF->setOffset(IF->getOffset()); |
| 714 | DF->setFileSize(IF->getInstSize()); |
| 715 | |
Daniel Dunbar | 9799de9 | 2010-03-23 01:39:05 +0000 | [diff] [blame] | 716 | // Copy in the data and the fixups. |
| 717 | DF->getContents().append(IF->getCode().begin(), IF->getCode().end()); |
| 718 | for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i) |
| 719 | DF->getFixups().push_back(IF->getFixups()[i]); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 720 | |
| 721 | // Delete the instruction fragment and update the iterator. |
| 722 | SD.getFragmentList().erase(IF); |
| 723 | it2 = DF; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 728 | // Debugging methods |
| 729 | |
| 730 | namespace llvm { |
| 731 | |
| 732 | raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) { |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 733 | OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value |
| 734 | << " Kind:" << AF.Kind << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 735 | return OS; |
| 736 | } |
| 737 | |
| 738 | } |
| 739 | |
| 740 | void MCFragment::dump() { |
| 741 | raw_ostream &OS = llvm::errs(); |
| 742 | |
| 743 | OS << "<MCFragment " << (void*) this << " Offset:" << Offset |
| 744 | << " FileSize:" << FileSize; |
| 745 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 746 | OS << ">"; |
| 747 | } |
| 748 | |
| 749 | void MCAlignFragment::dump() { |
| 750 | raw_ostream &OS = llvm::errs(); |
| 751 | |
| 752 | OS << "<MCAlignFragment "; |
| 753 | this->MCFragment::dump(); |
| 754 | OS << "\n "; |
| 755 | OS << " Alignment:" << getAlignment() |
| 756 | << " Value:" << getValue() << " ValueSize:" << getValueSize() |
| 757 | << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">"; |
| 758 | } |
| 759 | |
| 760 | void MCDataFragment::dump() { |
| 761 | raw_ostream &OS = llvm::errs(); |
| 762 | |
| 763 | OS << "<MCDataFragment "; |
| 764 | this->MCFragment::dump(); |
| 765 | OS << "\n "; |
| 766 | OS << " Contents:["; |
| 767 | for (unsigned i = 0, e = getContents().size(); i != e; ++i) { |
| 768 | if (i) OS << ","; |
| 769 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
| 770 | } |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 771 | OS << "] (" << getContents().size() << " bytes)"; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 772 | |
| 773 | if (!getFixups().empty()) { |
| 774 | OS << ",\n "; |
| 775 | OS << " Fixups:["; |
| 776 | for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 777 | if (it != fixup_begin()) OS << ",\n "; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 778 | OS << *it; |
| 779 | } |
| 780 | OS << "]"; |
| 781 | } |
| 782 | |
| 783 | OS << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void MCFillFragment::dump() { |
| 787 | raw_ostream &OS = llvm::errs(); |
| 788 | |
| 789 | OS << "<MCFillFragment "; |
| 790 | this->MCFragment::dump(); |
| 791 | OS << "\n "; |
| 792 | OS << " Value:" << getValue() << " ValueSize:" << getValueSize() |
| 793 | << " Count:" << getCount() << ">"; |
| 794 | } |
| 795 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 796 | void MCInstFragment::dump() { |
| 797 | raw_ostream &OS = llvm::errs(); |
| 798 | |
| 799 | OS << "<MCInstFragment "; |
| 800 | this->MCFragment::dump(); |
| 801 | OS << "\n "; |
| 802 | OS << " Inst:"; |
| 803 | getInst().dump_pretty(OS); |
| 804 | OS << ">"; |
| 805 | } |
| 806 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 807 | void MCOrgFragment::dump() { |
| 808 | raw_ostream &OS = llvm::errs(); |
| 809 | |
| 810 | OS << "<MCOrgFragment "; |
| 811 | this->MCFragment::dump(); |
| 812 | OS << "\n "; |
| 813 | OS << " Offset:" << getOffset() << " Value:" << getValue() << ">"; |
| 814 | } |
| 815 | |
| 816 | void MCZeroFillFragment::dump() { |
| 817 | raw_ostream &OS = llvm::errs(); |
| 818 | |
| 819 | OS << "<MCZeroFillFragment "; |
| 820 | this->MCFragment::dump(); |
| 821 | OS << "\n "; |
| 822 | OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">"; |
| 823 | } |
| 824 | |
| 825 | void MCSectionData::dump() { |
| 826 | raw_ostream &OS = llvm::errs(); |
| 827 | |
| 828 | OS << "<MCSectionData"; |
| 829 | OS << " Alignment:" << getAlignment() << " Address:" << Address |
| 830 | << " Size:" << Size << " FileSize:" << FileSize |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 831 | << " Fragments:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 832 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 833 | if (it != begin()) OS << ",\n "; |
| 834 | it->dump(); |
| 835 | } |
| 836 | OS << "]>"; |
| 837 | } |
| 838 | |
| 839 | void MCSymbolData::dump() { |
| 840 | raw_ostream &OS = llvm::errs(); |
| 841 | |
| 842 | OS << "<MCSymbolData Symbol:" << getSymbol() |
| 843 | << " Fragment:" << getFragment() << " Offset:" << getOffset() |
| 844 | << " Flags:" << getFlags() << " Index:" << getIndex(); |
| 845 | if (isCommon()) |
| 846 | OS << " (common, size:" << getCommonSize() |
| 847 | << " align: " << getCommonAlignment() << ")"; |
| 848 | if (isExternal()) |
| 849 | OS << " (external)"; |
| 850 | if (isPrivateExtern()) |
| 851 | OS << " (private extern)"; |
| 852 | OS << ">"; |
| 853 | } |
| 854 | |
| 855 | void MCAssembler::dump() { |
| 856 | raw_ostream &OS = llvm::errs(); |
| 857 | |
| 858 | OS << "<MCAssembler\n"; |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 859 | OS << " Sections:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 860 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 861 | if (it != begin()) OS << ",\n "; |
| 862 | it->dump(); |
| 863 | } |
| 864 | OS << "],\n"; |
| 865 | OS << " Symbols:["; |
| 866 | |
| 867 | for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 868 | if (it != symbol_begin()) OS << ",\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 869 | it->dump(); |
| 870 | } |
| 871 | OS << "]>\n"; |
| 872 | } |