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