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