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" |
Rafael Espindola | fea753b | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCObjectWriter.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSymbol.h" |
| 19 | #include "llvm/MC/MCValue.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDwarf.h" |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame^] | 21 | #include "llvm/MC/MCAsmBackend.h" |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | namespace stats { |
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"); |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 37 | STATISTIC(FragmentLayouts, "Number of fragment layouts"); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 38 | STATISTIC(ObjectBytes, "Number of emitted object file bytes"); |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 39 | STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); |
| 40 | STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 43 | |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 44 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 45 | // what is a 64-bit assembler value used for computation into a value in the |
| 46 | // object file, which may truncate it. We should detect that truncation where |
| 47 | // invalid and report errors back. |
| 48 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 49 | /* *** */ |
| 50 | |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 51 | MCAsmLayout::MCAsmLayout(MCAssembler &Asm) |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 52 | : Assembler(Asm), LastValidFragment() |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 53 | { |
Daniel Dunbar | bc1a0cf | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 54 | // Compute the section layout order. Virtual sections must go last. |
| 55 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 56 | if (!it->getSection().isVirtualSection()) |
Daniel Dunbar | bc1a0cf | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 57 | SectionOrder.push_back(&*it); |
| 58 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 59 | if (it->getSection().isVirtualSection()) |
Daniel Dunbar | bc1a0cf | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 60 | SectionOrder.push_back(&*it); |
| 61 | } |
| 62 | |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 63 | bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const { |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 64 | const MCSectionData &SD = *F->getParent(); |
| 65 | const MCFragment *LastValid = LastValidFragment.lookup(&SD); |
| 66 | if (!LastValid) |
| 67 | return false; |
| 68 | assert(LastValid->getParent() == F->getParent()); |
| 69 | return F->getLayoutOrder() <= LastValid->getLayoutOrder(); |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Rafael Espindola | a9d4281 | 2010-11-23 08:08:33 +0000 | [diff] [blame] | 72 | void MCAsmLayout::Invalidate(MCFragment *F) { |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 73 | // If this fragment wasn't already up-to-date, we don't need to do anything. |
| 74 | if (!isFragmentUpToDate(F)) |
| 75 | return; |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 76 | |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 77 | // Otherwise, reset the last valid fragment to this fragment. |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 78 | const MCSectionData &SD = *F->getParent(); |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 79 | LastValidFragment[&SD] = F; |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 80 | } |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 81 | |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 82 | void MCAsmLayout::EnsureValid(const MCFragment *F) const { |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 83 | MCSectionData &SD = *F->getParent(); |
| 84 | |
| 85 | MCFragment *Cur = LastValidFragment[&SD]; |
| 86 | if (!Cur) |
| 87 | Cur = &*SD.begin(); |
| 88 | else |
| 89 | Cur = Cur->getNextNode(); |
| 90 | |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 91 | // Advance the layout position until the fragment is up-to-date. |
| 92 | while (!isFragmentUpToDate(F)) { |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 93 | const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur); |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 94 | Cur = Cur->getNextNode(); |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 98 | uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const { |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 99 | EnsureValid(F); |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 100 | assert(F->Offset != ~UINT64_C(0) && "Address not set!"); |
| 101 | return F->Offset; |
| 102 | } |
| 103 | |
Rafael Espindola | ffd902b | 2010-12-06 02:57:26 +0000 | [diff] [blame] | 104 | uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const { |
Daniel Dunbar | c8497b6 | 2011-04-29 18:20:20 +0000 | [diff] [blame] | 105 | const MCSymbol &S = SD->getSymbol(); |
| 106 | |
| 107 | // If this is a variable, then recursively evaluate now. |
| 108 | if (S.isVariable()) { |
| 109 | MCValue Target; |
| 110 | if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this)) |
| 111 | report_fatal_error("unable to evaluate offset for variable '" + |
| 112 | S.getName() + "'"); |
| 113 | |
| 114 | // Verify that any used symbols are defined. |
| 115 | if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined()) |
| 116 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 117 | Target.getSymA()->getSymbol().getName() + "'"); |
| 118 | if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined()) |
| 119 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 120 | Target.getSymB()->getSymbol().getName() + "'"); |
| 121 | |
| 122 | uint64_t Offset = Target.getConstant(); |
| 123 | if (Target.getSymA()) |
| 124 | Offset += getSymbolOffset(&Assembler.getSymbolData( |
| 125 | Target.getSymA()->getSymbol())); |
| 126 | if (Target.getSymB()) |
| 127 | Offset -= getSymbolOffset(&Assembler.getSymbolData( |
| 128 | Target.getSymB()->getSymbol())); |
| 129 | return Offset; |
| 130 | } |
| 131 | |
Rafael Espindola | ffd902b | 2010-12-06 02:57:26 +0000 | [diff] [blame] | 132 | assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!"); |
| 133 | return getFragmentOffset(SD->getFragment()) + SD->getOffset(); |
| 134 | } |
| 135 | |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 136 | uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const { |
Daniel Dunbar | afc6acd | 2010-05-14 00:37:11 +0000 | [diff] [blame] | 137 | // The size is the last fragment's end offset. |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 138 | const MCFragment &F = SD->getFragmentList().back(); |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 139 | return getFragmentOffset(&F) + getAssembler().ComputeFragmentSize(*this, F); |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const { |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 143 | // Virtual sections have no file size. |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 144 | if (SD->getSection().isVirtualSection()) |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 145 | return 0; |
| 146 | |
| 147 | // Otherwise, the file size is the same as the address space size. |
| 148 | return getSectionAddressSize(SD); |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 151 | /* *** */ |
| 152 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 153 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 154 | } |
| 155 | |
Daniel Dunbar | 36880e7 | 2010-07-28 20:28:45 +0000 | [diff] [blame] | 156 | MCFragment::~MCFragment() { |
| 157 | } |
| 158 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 159 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 160 | : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 161 | { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 162 | if (Parent) |
| 163 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* *** */ |
| 167 | |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 168 | MCSectionData::MCSectionData() : Section(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 169 | |
| 170 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 171 | : Section(&_Section), |
Rafael Espindola | f8803fe | 2010-12-06 03:48:09 +0000 | [diff] [blame] | 172 | Ordinal(~UINT32_C(0)), |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 173 | Alignment(1), |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 174 | HasInstructions(false) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 175 | { |
| 176 | if (A) |
| 177 | A->getSectionList().push_back(this); |
| 178 | } |
| 179 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 180 | /* *** */ |
| 181 | |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 182 | MCSymbolData::MCSymbolData() : Symbol(0) {} |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 183 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 184 | MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 185 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 186 | : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 187 | IsExternal(false), IsPrivateExtern(false), |
Matt Fleming | 6c8b3d2 | 2010-08-16 18:34:31 +0000 | [diff] [blame] | 188 | CommonSize(0), SymbolSize(0), CommonAlign(0), |
| 189 | Flags(0), Index(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 190 | { |
| 191 | if (A) |
| 192 | A->getSymbolList().push_back(this); |
| 193 | } |
| 194 | |
| 195 | /* *** */ |
| 196 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame^] | 197 | MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, |
Daniel Dunbar | feb7ba3 | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 198 | MCCodeEmitter &Emitter_, MCObjectWriter &Writer_, |
| 199 | raw_ostream &OS_) |
| 200 | : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_), |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 201 | OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false) |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 202 | { |
| 203 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 204 | |
| 205 | MCAssembler::~MCAssembler() { |
| 206 | } |
| 207 | |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 208 | bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 209 | // Non-temporary labels should always be visible to the linker. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 210 | if (!Symbol.isTemporary()) |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 211 | return true; |
| 212 | |
| 213 | // Absolute temporary labels are never visible. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 214 | if (!Symbol.isInSection()) |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 215 | return false; |
| 216 | |
| 217 | // Otherwise, check if the section requires symbols even for temporary labels. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 218 | return getBackend().doesSectionRequireSymbols(Symbol.getSection()); |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Rafael Espindola | b814110 | 2010-09-27 18:13:03 +0000 | [diff] [blame] | 221 | const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 222 | // Linker visible symbols define atoms. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 223 | if (isSymbolLinkerVisible(SD->getSymbol())) |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 224 | return SD; |
| 225 | |
| 226 | // Absolute and undefined symbols have no defining atom. |
| 227 | if (!SD->getFragment()) |
| 228 | return 0; |
| 229 | |
Daniel Dunbar | a5f1d57 | 2010-05-12 00:38:17 +0000 | [diff] [blame] | 230 | // Non-linker visible symbols in sections which can't be atomized have no |
| 231 | // defining atom. |
| 232 | if (!getBackend().isSectionAtomizable( |
| 233 | SD->getFragment()->getParent()->getSection())) |
| 234 | return 0; |
| 235 | |
Daniel Dunbar | 651804c | 2010-05-11 17:22:50 +0000 | [diff] [blame] | 236 | // Otherwise, return the atom for the containing fragment. |
| 237 | return SD->getFragment()->getAtom(); |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 240 | bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 241 | const MCFixup &Fixup, const MCFragment *DF, |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 242 | MCValue &Target, uint64_t &Value) const { |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 243 | ++stats::EvaluateFixup; |
| 244 | |
Rafael Espindola | 33a38a1 | 2010-12-22 16:11:57 +0000 | [diff] [blame] | 245 | if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout)) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 246 | report_fatal_error("expected relocatable expression"); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 247 | |
Rafael Espindola | fea753b | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 248 | bool IsPCRel = Backend.getFixupKindInfo( |
| 249 | Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 250 | |
| 251 | bool IsResolved; |
| 252 | if (IsPCRel) { |
| 253 | if (Target.getSymB()) { |
| 254 | IsResolved = false; |
| 255 | } else if (!Target.getSymA()) { |
| 256 | IsResolved = false; |
| 257 | } else { |
Rafael Espindola | 908159b | 2011-02-16 03:25:55 +0000 | [diff] [blame] | 258 | const MCSymbolRefExpr *A = Target.getSymA(); |
| 259 | const MCSymbol &SA = A->getSymbol(); |
| 260 | if (A->getKind() != MCSymbolRefExpr::VK_None || |
| 261 | SA.AliasedSymbol().isUndefined()) { |
Rafael Espindola | fea753b | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 262 | IsResolved = false; |
| 263 | } else { |
| 264 | const MCSymbolData &DataA = getSymbolData(SA); |
| 265 | IsResolved = |
| 266 | getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA, |
| 267 | *DF, false, true); |
| 268 | } |
| 269 | } |
| 270 | } else { |
| 271 | IsResolved = Target.isAbsolute(); |
| 272 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 273 | |
| 274 | Value = Target.getConstant(); |
| 275 | |
Jim Grosbach | 4750020 | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 276 | bool IsThumb = false; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 277 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 278 | const MCSymbol &Sym = A->getSymbol().AliasedSymbol(); |
| 279 | if (Sym.isDefined()) |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 280 | Value += Layout.getSymbolOffset(&getSymbolData(Sym)); |
Jim Grosbach | 4750020 | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 281 | if (isThumbFunc(&Sym)) |
| 282 | IsThumb = true; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 283 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 284 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 285 | const MCSymbol &Sym = B->getSymbol().AliasedSymbol(); |
| 286 | if (Sym.isDefined()) |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 287 | Value -= Layout.getSymbolOffset(&getSymbolData(Sym)); |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 288 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 289 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 290 | |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 291 | bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
Owen Anderson | 47dbd42 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 292 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits; |
| 293 | assert((ShouldAlignPC ? IsPCRel : true) && |
| 294 | "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"); |
| 295 | |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 296 | if (IsPCRel) { |
Owen Anderson | 175fb36 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 297 | uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset(); |
Owen Anderson | 47dbd42 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 298 | |
| 299 | // A number of ARM fixups in Thumb mode require that the effective PC |
| 300 | // address be determined as the 32-bit aligned version of the actual offset. |
Owen Anderson | d18e011 | 2010-12-15 19:24:24 +0000 | [diff] [blame] | 301 | if (ShouldAlignPC) Offset &= ~0x3; |
Owen Anderson | 175fb36 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 302 | Value -= Offset; |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 303 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 304 | |
Jim Grosbach | 4750020 | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 305 | // ARM fixups based from a thumb function address need to have the low |
| 306 | // bit set. The actual value is always at least 16-bit aligned, so the |
| 307 | // low bit is normally clear and available for use as an ISA flag for |
| 308 | // interworking. |
| 309 | if (IsThumb) |
| 310 | Value |= 1; |
| 311 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 312 | return IsResolved; |
| 313 | } |
| 314 | |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 315 | uint64_t MCAssembler::ComputeFragmentSize(const MCAsmLayout &Layout, |
| 316 | const MCFragment &F) const { |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 317 | switch (F.getKind()) { |
| 318 | case MCFragment::FT_Data: |
| 319 | return cast<MCDataFragment>(F).getContents().size(); |
| 320 | case MCFragment::FT_Fill: |
| 321 | return cast<MCFillFragment>(F).getSize(); |
| 322 | case MCFragment::FT_Inst: |
| 323 | return cast<MCInstFragment>(F).getInstSize(); |
| 324 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 325 | case MCFragment::FT_LEB: |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 326 | return cast<MCLEBFragment>(F).getContents().size(); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 327 | |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 328 | case MCFragment::FT_Align: { |
| 329 | const MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 330 | unsigned Offset = Layout.getFragmentOffset(&AF); |
| 331 | unsigned Size = OffsetToAlignment(Offset, AF.getAlignment()); |
| 332 | if (Size > AF.getMaxBytesToEmit()) |
| 333 | return 0; |
| 334 | return Size; |
| 335 | } |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 336 | |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 337 | case MCFragment::FT_Org: { |
| 338 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 339 | int64_t TargetLocation; |
| 340 | if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout)) |
| 341 | report_fatal_error("expected assembly-time absolute expression"); |
| 342 | |
| 343 | // FIXME: We need a way to communicate this error. |
| 344 | uint64_t FragmentOffset = Layout.getFragmentOffset(&OF); |
| 345 | int64_t Size = TargetLocation - FragmentOffset; |
| 346 | if (Size < 0 || Size >= 0x40000000) |
| 347 | report_fatal_error("invalid .org offset '" + Twine(TargetLocation) + |
| 348 | "' (at offset '" + Twine(FragmentOffset) + "')"); |
| 349 | return Size; |
| 350 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 351 | |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 352 | case MCFragment::FT_Dwarf: |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 353 | return cast<MCDwarfLineAddrFragment>(F).getContents().size(); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 354 | case MCFragment::FT_DwarfFrame: |
| 355 | return cast<MCDwarfCallFrameFragment>(F).getContents().size(); |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | assert(0 && "invalid fragment kind"); |
| 359 | return 0; |
| 360 | } |
| 361 | |
Daniel Dunbar | b69fc04 | 2010-05-13 20:40:12 +0000 | [diff] [blame] | 362 | void MCAsmLayout::LayoutFragment(MCFragment *F) { |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 363 | MCFragment *Prev = F->getPrevNode(); |
Daniel Dunbar | f0d17d2 | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 364 | |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 365 | // We should never try to recompute something which is up-to-date. |
| 366 | assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!"); |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 367 | // We should never try to compute the fragment layout if it's predecessor |
| 368 | // isn't up-to-date. |
| 369 | assert((!Prev || isFragmentUpToDate(Prev)) && |
| 370 | "Attempt to compute fragment before it's predecessor!"); |
Daniel Dunbar | f0d17d2 | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 371 | |
| 372 | ++stats::FragmentLayouts; |
| 373 | |
Daniel Dunbar | b69fc04 | 2010-05-13 20:40:12 +0000 | [diff] [blame] | 374 | // Compute fragment offset and size. |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 375 | uint64_t Offset = 0; |
| 376 | if (Prev) |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 377 | Offset += Prev->Offset + getAssembler().ComputeFragmentSize(*this, *Prev); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 378 | |
| 379 | F->Offset = Offset; |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 380 | LastValidFragment[F->getParent()] = F; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 383 | /// WriteFragmentData - Write the \arg F data to the output file. |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 384 | static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 385 | const MCFragment &F) { |
| 386 | MCObjectWriter *OW = &Asm.getWriter(); |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 387 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 388 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 389 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 390 | ++stats::EmittedFragments; |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 392 | // FIXME: Embed in fragments instead? |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 393 | uint64_t FragmentSize = Asm.ComputeFragmentSize(Layout, F); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 394 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 395 | case MCFragment::FT_Align: { |
| 396 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 397 | uint64_t Count = FragmentSize / AF.getValueSize(); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 398 | |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 399 | assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 400 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 401 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 402 | // multiple .align directives to enforce the semantics it wants), but is |
| 403 | // severe enough that we want to report it. How to handle this? |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 404 | if (Count * AF.getValueSize() != FragmentSize) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 405 | report_fatal_error("undefined .align directive, value size '" + |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 406 | Twine(AF.getValueSize()) + |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 407 | "' is not a divisor of padding size '" + |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 408 | Twine(FragmentSize) + "'"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 409 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 410 | // See if we are aligning with nops, and if so do that first to try to fill |
| 411 | // the Count bytes. Then if that did not fill any bytes or there are any |
| 412 | // 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] | 413 | // If we are aligning with nops, ask that target to emit the right data. |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 414 | if (AF.hasEmitNops()) { |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 415 | if (!Asm.getBackend().WriteNopData(Count, OW)) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 416 | report_fatal_error("unable to write nop sequence of " + |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 417 | Twine(Count) + " bytes"); |
| 418 | break; |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 421 | // Otherwise, write out in multiples of the value size. |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 422 | for (uint64_t i = 0; i != Count; ++i) { |
| 423 | switch (AF.getValueSize()) { |
| 424 | default: |
| 425 | assert(0 && "Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 426 | case 1: OW->Write8 (uint8_t (AF.getValue())); break; |
| 427 | case 2: OW->Write16(uint16_t(AF.getValue())); break; |
| 428 | case 4: OW->Write32(uint32_t(AF.getValue())); break; |
| 429 | case 8: OW->Write64(uint64_t(AF.getValue())); break; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | break; |
| 433 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 434 | |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 435 | case MCFragment::FT_Data: { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 436 | MCDataFragment &DF = cast<MCDataFragment>(F); |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 437 | assert(FragmentSize == DF.getContents().size() && "Invalid size!"); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 438 | OW->WriteBytes(DF.getContents().str()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 439 | break; |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 440 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 442 | case MCFragment::FT_Fill: { |
| 443 | MCFillFragment &FF = cast<MCFillFragment>(F); |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 444 | |
| 445 | assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 446 | |
Daniel Dunbar | 3153fec | 2010-05-12 22:51:32 +0000 | [diff] [blame] | 447 | for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 448 | switch (FF.getValueSize()) { |
| 449 | default: |
| 450 | assert(0 && "Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 451 | case 1: OW->Write8 (uint8_t (FF.getValue())); break; |
| 452 | case 2: OW->Write16(uint16_t(FF.getValue())); break; |
| 453 | case 4: OW->Write32(uint32_t(FF.getValue())); break; |
| 454 | case 8: OW->Write64(uint64_t(FF.getValue())); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | break; |
| 458 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 459 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 460 | case MCFragment::FT_Inst: { |
| 461 | MCInstFragment &IF = cast<MCInstFragment>(F); |
| 462 | OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size())); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 463 | break; |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 464 | } |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 465 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 466 | case MCFragment::FT_LEB: { |
| 467 | MCLEBFragment &LF = cast<MCLEBFragment>(F); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 468 | OW->WriteBytes(LF.getContents().str()); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 472 | case MCFragment::FT_Org: { |
| 473 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 474 | |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 475 | for (uint64_t i = 0, e = FragmentSize; i != e; ++i) |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 476 | OW->Write8(uint8_t(OF.getValue())); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 477 | |
| 478 | break; |
| 479 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 480 | |
| 481 | case MCFragment::FT_Dwarf: { |
| 482 | const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 483 | OW->WriteBytes(OF.getContents().str()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 484 | break; |
| 485 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 486 | case MCFragment::FT_DwarfFrame: { |
| 487 | const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F); |
| 488 | OW->WriteBytes(CF.getContents().str()); |
| 489 | break; |
| 490 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 493 | assert(OW->getStream().tell() - Start == FragmentSize); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 496 | void MCAssembler::WriteSectionData(const MCSectionData *SD, |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 497 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 498 | // Ignore virtual sections. |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 499 | if (SD->getSection().isVirtualSection()) { |
Daniel Dunbar | 054be92 | 2010-05-13 03:50:50 +0000 | [diff] [blame] | 500 | assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!"); |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 501 | |
| 502 | // Check that contents are only things legal inside a virtual section. |
| 503 | for (MCSectionData::const_iterator it = SD->begin(), |
| 504 | ie = SD->end(); it != ie; ++it) { |
| 505 | switch (it->getKind()) { |
| 506 | default: |
| 507 | assert(0 && "Invalid fragment in virtual section!"); |
Daniel Dunbar | c983b20 | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 508 | case MCFragment::FT_Data: { |
| 509 | // Check that we aren't trying to write a non-zero contents (or fixups) |
| 510 | // into a virtual section. This is to support clients which use standard |
| 511 | // directives to fill the contents of virtual sections. |
| 512 | MCDataFragment &DF = cast<MCDataFragment>(*it); |
| 513 | assert(DF.fixup_begin() == DF.fixup_end() && |
| 514 | "Cannot have fixups in virtual section!"); |
| 515 | for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) |
| 516 | assert(DF.getContents()[i] == 0 && |
| 517 | "Invalid data value for virtual section!"); |
| 518 | break; |
| 519 | } |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 520 | case MCFragment::FT_Align: |
Daniel Dunbar | c983b20 | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 521 | // Check that we aren't trying to write a non-zero value into a virtual |
| 522 | // section. |
| 523 | assert((!cast<MCAlignFragment>(it)->getValueSize() || |
| 524 | !cast<MCAlignFragment>(it)->getValue()) && |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 525 | "Invalid align in virtual section!"); |
| 526 | break; |
| 527 | case MCFragment::FT_Fill: |
| 528 | assert(!cast<MCFillFragment>(it)->getValueSize() && |
| 529 | "Invalid fill in virtual section!"); |
| 530 | break; |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 537 | uint64_t Start = getWriter().getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 538 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 539 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 540 | for (MCSectionData::const_iterator it = SD->begin(), |
| 541 | ie = SD->end(); it != ie; ++it) |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 542 | WriteFragmentData(*this, Layout, *it); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 543 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 544 | assert(getWriter().getStream().tell() - Start == |
| 545 | Layout.getSectionAddressSize(SD)); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 548 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 549 | uint64_t MCAssembler::HandleFixup(const MCAsmLayout &Layout, |
| 550 | MCFragment &F, |
| 551 | const MCFixup &Fixup) { |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 552 | // Evaluate the fixup. |
| 553 | MCValue Target; |
| 554 | uint64_t FixedValue; |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 555 | if (!EvaluateFixup(Layout, Fixup, &F, Target, FixedValue)) { |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 556 | // The fixup was unresolved, we need a relocation. Inform the object |
| 557 | // writer of the relocation, and give it an opportunity to adjust the |
| 558 | // fixup value if need be. |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 559 | getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue); |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 560 | } |
| 561 | return FixedValue; |
| 562 | } |
| 563 | |
Daniel Dunbar | feb7ba3 | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 564 | void MCAssembler::Finish() { |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 565 | DEBUG_WITH_TYPE("mc-dump", { |
| 566 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 567 | dump(); }); |
| 568 | |
Daniel Dunbar | 61066db | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 569 | // Create the layout object. |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 570 | MCAsmLayout Layout(*this); |
Daniel Dunbar | 61066db | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 571 | |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 572 | // Create dummy fragments and assign section ordinals. |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 573 | unsigned SectionIndex = 0; |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 574 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 575 | // Create dummy fragments to eliminate any empty sections, this simplifies |
| 576 | // layout. |
Duncan Sands | 6f74f69 | 2010-06-29 13:30:08 +0000 | [diff] [blame] | 577 | if (it->getFragmentList().empty()) |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 578 | new MCDataFragment(it); |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 579 | |
| 580 | it->setOrdinal(SectionIndex++); |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 581 | } |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 582 | |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 583 | // Assign layout order indices to sections and fragments. |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 584 | for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) { |
| 585 | MCSectionData *SD = Layout.getSectionOrder()[i]; |
| 586 | SD->setLayoutOrder(i); |
| 587 | |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 588 | unsigned FragmentIndex = 0; |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 589 | for (MCSectionData::iterator it2 = SD->begin(), |
| 590 | ie2 = SD->end(); it2 != ie2; ++it2) |
| 591 | it2->setLayoutOrder(FragmentIndex++); |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Daniel Dunbar | 61066db | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 594 | // Layout until everything fits. |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 595 | while (LayoutOnce(Layout)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 596 | continue; |
| 597 | |
| 598 | DEBUG_WITH_TYPE("mc-dump", { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 599 | llvm::errs() << "assembler backend - post-relaxation\n--\n"; |
| 600 | dump(); }); |
| 601 | |
| 602 | // Finalize the layout, including fragment lowering. |
| 603 | FinishLayout(Layout); |
| 604 | |
| 605 | DEBUG_WITH_TYPE("mc-dump", { |
| 606 | llvm::errs() << "assembler backend - final-layout\n--\n"; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 607 | dump(); }); |
| 608 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 609 | uint64_t StartOffset = OS.tell(); |
Reid Kleckner | c96a82a | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 610 | |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 611 | // Allow the object writer a chance to perform post-layout binding (for |
| 612 | // example, to set the index fields in the symbol data). |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 613 | getWriter().ExecutePostLayoutBinding(*this, Layout); |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 614 | |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 615 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 616 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 617 | for (MCSectionData::iterator it2 = it->begin(), |
| 618 | ie2 = it->end(); it2 != ie2; ++it2) { |
| 619 | MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 620 | if (DF) { |
| 621 | for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), |
| 622 | ie3 = DF->fixup_end(); it3 != ie3; ++it3) { |
| 623 | MCFixup &Fixup = *it3; |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 624 | uint64_t FixedValue = HandleFixup(Layout, *DF, Fixup); |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 625 | getBackend().ApplyFixup(Fixup, DF->getContents().data(), |
| 626 | DF->getContents().size(), FixedValue); |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 627 | } |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 628 | } |
| 629 | MCInstFragment *IF = dyn_cast<MCInstFragment>(it2); |
| 630 | if (IF) { |
| 631 | for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(), |
| 632 | ie3 = IF->fixup_end(); it3 != ie3; ++it3) { |
| 633 | MCFixup &Fixup = *it3; |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 634 | uint64_t FixedValue = HandleFixup(Layout, *IF, Fixup); |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 635 | getBackend().ApplyFixup(Fixup, IF->getCode().data(), |
| 636 | IF->getCode().size(), FixedValue); |
| 637 | } |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 642 | // Write the object file. |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 643 | getWriter().WriteObject(*this, Layout); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 644 | |
| 645 | stats::ObjectBytes += OS.tell() - StartOffset; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 648 | bool MCAssembler::FixupNeedsRelaxation(const MCFixup &Fixup, |
Daniel Dunbar | 9d39e61 | 2010-03-22 21:49:41 +0000 | [diff] [blame] | 649 | const MCFragment *DF, |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 650 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 651 | if (getRelaxAll()) |
| 652 | return true; |
| 653 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 654 | // If we cannot resolve the fixup value, it requires relaxation. |
| 655 | MCValue Target; |
| 656 | uint64_t Value; |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 657 | if (!EvaluateFixup(Layout, Fixup, DF, Target, Value)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 658 | return true; |
| 659 | |
| 660 | // Otherwise, relax if the value is too big for a (signed) i8. |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 661 | // |
| 662 | // FIXME: This is target dependent! |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 663 | return int64_t(Value) != int64_t(int8_t(Value)); |
| 664 | } |
| 665 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 666 | bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF, |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 667 | const MCAsmLayout &Layout) const { |
| 668 | // If this inst doesn't ever need relaxation, ignore it. This occurs when we |
| 669 | // are intentionally pushing out inst fragments, or because we relaxed a |
| 670 | // previous instruction to one that doesn't need relaxation. |
Daniel Dunbar | 8488252 | 2010-05-26 17:45:29 +0000 | [diff] [blame] | 671 | if (!getBackend().MayNeedRelaxation(IF->getInst())) |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 672 | return false; |
| 673 | |
| 674 | for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(), |
| 675 | ie = IF->fixup_end(); it != ie; ++it) |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 676 | if (FixupNeedsRelaxation(*it, IF, Layout)) |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 677 | return true; |
| 678 | |
| 679 | return false; |
| 680 | } |
| 681 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 682 | bool MCAssembler::RelaxInstruction(MCAsmLayout &Layout, |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 683 | MCInstFragment &IF) { |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 684 | if (!FragmentNeedsRelaxation(&IF, Layout)) |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 685 | return false; |
| 686 | |
| 687 | ++stats::RelaxedInstructions; |
| 688 | |
| 689 | // FIXME-PERF: We could immediately lower out instructions if we can tell |
| 690 | // they are fully resolved, to avoid retesting on later passes. |
| 691 | |
| 692 | // Relax the fragment. |
| 693 | |
| 694 | MCInst Relaxed; |
| 695 | getBackend().RelaxInstruction(IF.getInst(), Relaxed); |
| 696 | |
| 697 | // Encode the new instruction. |
| 698 | // |
| 699 | // FIXME-PERF: If it matters, we could let the target do this. It can |
| 700 | // probably do so more efficiently in many cases. |
| 701 | SmallVector<MCFixup, 4> Fixups; |
| 702 | SmallString<256> Code; |
| 703 | raw_svector_ostream VecOS(Code); |
| 704 | getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups); |
| 705 | VecOS.flush(); |
| 706 | |
| 707 | // Update the instruction fragment. |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 708 | IF.setInst(Relaxed); |
| 709 | IF.getCode() = Code; |
| 710 | IF.getFixups().clear(); |
| 711 | // FIXME: Eliminate copy. |
| 712 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 713 | IF.getFixups().push_back(Fixups[i]); |
| 714 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 715 | return true; |
| 716 | } |
| 717 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 718 | bool MCAssembler::RelaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 719 | int64_t Value = 0; |
| 720 | uint64_t OldSize = LF.getContents().size(); |
Rafael Espindola | d88cac0 | 2011-04-26 02:17:58 +0000 | [diff] [blame] | 721 | bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout); |
| 722 | (void)IsAbs; |
| 723 | assert(IsAbs); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 724 | SmallString<8> &Data = LF.getContents(); |
| 725 | Data.clear(); |
| 726 | raw_svector_ostream OSE(Data); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 727 | if (LF.isSigned()) |
| 728 | MCObjectWriter::EncodeSLEB128(Value, OSE); |
| 729 | else |
| 730 | MCObjectWriter::EncodeULEB128(Value, OSE); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 731 | OSE.flush(); |
| 732 | return OldSize != LF.getContents().size(); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 735 | bool MCAssembler::RelaxDwarfLineAddr(MCAsmLayout &Layout, |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 736 | MCDwarfLineAddrFragment &DF) { |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 737 | int64_t AddrDelta = 0; |
| 738 | uint64_t OldSize = DF.getContents().size(); |
Rafael Espindola | 835439a | 2010-12-22 22:04:28 +0000 | [diff] [blame] | 739 | bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout); |
| 740 | (void)IsAbs; |
| 741 | assert(IsAbs); |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 742 | int64_t LineDelta; |
| 743 | LineDelta = DF.getLineDelta(); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 744 | SmallString<8> &Data = DF.getContents(); |
| 745 | Data.clear(); |
| 746 | raw_svector_ostream OSE(Data); |
| 747 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE); |
| 748 | OSE.flush(); |
| 749 | return OldSize != Data.size(); |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 752 | bool MCAssembler::RelaxDwarfCallFrameFragment(MCAsmLayout &Layout, |
| 753 | MCDwarfCallFrameFragment &DF) { |
| 754 | int64_t AddrDelta = 0; |
| 755 | uint64_t OldSize = DF.getContents().size(); |
| 756 | bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout); |
| 757 | (void)IsAbs; |
| 758 | assert(IsAbs); |
| 759 | SmallString<8> &Data = DF.getContents(); |
| 760 | Data.clear(); |
| 761 | raw_svector_ostream OSE(Data); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 762 | MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 763 | OSE.flush(); |
| 764 | return OldSize != Data.size(); |
| 765 | } |
| 766 | |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 767 | bool MCAssembler::LayoutSectionOnce(MCAsmLayout &Layout, |
| 768 | MCSectionData &SD) { |
| 769 | MCFragment *FirstInvalidFragment = NULL; |
| 770 | // Scan for fragments that need relaxation. |
| 771 | for (MCSectionData::iterator it2 = SD.begin(), |
| 772 | ie2 = SD.end(); it2 != ie2; ++it2) { |
| 773 | // Check if this is an fragment that needs relaxation. |
| 774 | bool relaxedFrag = false; |
| 775 | switch(it2->getKind()) { |
| 776 | default: |
| 777 | break; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 778 | case MCFragment::FT_Inst: |
| 779 | relaxedFrag = RelaxInstruction(Layout, *cast<MCInstFragment>(it2)); |
| 780 | break; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 781 | case MCFragment::FT_Dwarf: |
| 782 | relaxedFrag = RelaxDwarfLineAddr(Layout, |
| 783 | *cast<MCDwarfLineAddrFragment>(it2)); |
| 784 | break; |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 785 | case MCFragment::FT_DwarfFrame: |
| 786 | relaxedFrag = |
| 787 | RelaxDwarfCallFrameFragment(Layout, |
| 788 | *cast<MCDwarfCallFrameFragment>(it2)); |
| 789 | break; |
| 790 | case MCFragment::FT_LEB: |
| 791 | relaxedFrag = RelaxLEB(Layout, *cast<MCLEBFragment>(it2)); |
| 792 | break; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 793 | } |
| 794 | // Update the layout, and remember that we relaxed. |
| 795 | if (relaxedFrag && !FirstInvalidFragment) |
| 796 | FirstInvalidFragment = it2; |
| 797 | } |
| 798 | if (FirstInvalidFragment) { |
| 799 | Layout.Invalidate(FirstInvalidFragment); |
| 800 | return true; |
| 801 | } |
| 802 | return false; |
| 803 | } |
| 804 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 805 | bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) { |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 806 | ++stats::RelaxationSteps; |
| 807 | |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 808 | bool WasRelaxed = false; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 809 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 810 | MCSectionData &SD = *it; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 811 | while(LayoutSectionOnce(Layout, SD)) |
| 812 | WasRelaxed = true; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 815 | return WasRelaxed; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 816 | } |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 818 | void MCAssembler::FinishLayout(MCAsmLayout &Layout) { |
Rafael Espindola | b4172fa | 2010-12-04 22:47:22 +0000 | [diff] [blame] | 819 | // The layout is done. Mark every fragment as valid. |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 820 | for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) { |
| 821 | Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin()); |
| 822 | } |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 825 | // Debugging methods |
| 826 | |
| 827 | namespace llvm { |
| 828 | |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 829 | raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { |
| 830 | OS << "<MCFixup" << " Offset:" << AF.getOffset() |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 831 | << " Value:" << *AF.getValue() |
| 832 | << " Kind:" << AF.getKind() << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 833 | return OS; |
| 834 | } |
| 835 | |
| 836 | } |
| 837 | |
| 838 | void MCFragment::dump() { |
| 839 | raw_ostream &OS = llvm::errs(); |
| 840 | |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 841 | OS << "<"; |
| 842 | switch (getKind()) { |
| 843 | case MCFragment::FT_Align: OS << "MCAlignFragment"; break; |
| 844 | case MCFragment::FT_Data: OS << "MCDataFragment"; break; |
| 845 | case MCFragment::FT_Fill: OS << "MCFillFragment"; break; |
| 846 | case MCFragment::FT_Inst: OS << "MCInstFragment"; break; |
| 847 | case MCFragment::FT_Org: OS << "MCOrgFragment"; break; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 848 | case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break; |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 849 | case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break; |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 850 | case MCFragment::FT_LEB: OS << "MCLEBFragment"; break; |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 853 | OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 854 | << " Offset:" << Offset << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 856 | switch (getKind()) { |
| 857 | case MCFragment::FT_Align: { |
| 858 | const MCAlignFragment *AF = cast<MCAlignFragment>(this); |
| 859 | if (AF->hasEmitNops()) |
| 860 | OS << " (emit nops)"; |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 861 | OS << "\n "; |
| 862 | OS << " Alignment:" << AF->getAlignment() |
| 863 | << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize() |
| 864 | << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">"; |
| 865 | break; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 866 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 867 | case MCFragment::FT_Data: { |
| 868 | const MCDataFragment *DF = cast<MCDataFragment>(this); |
| 869 | OS << "\n "; |
| 870 | OS << " Contents:["; |
| 871 | const SmallVectorImpl<char> &Contents = DF->getContents(); |
| 872 | for (unsigned i = 0, e = Contents.size(); i != e; ++i) { |
| 873 | if (i) OS << ","; |
| 874 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 875 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 876 | OS << "] (" << Contents.size() << " bytes)"; |
| 877 | |
| 878 | if (!DF->getFixups().empty()) { |
| 879 | OS << ",\n "; |
| 880 | OS << " Fixups:["; |
| 881 | for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(), |
| 882 | ie = DF->fixup_end(); it != ie; ++it) { |
| 883 | if (it != DF->fixup_begin()) OS << ",\n "; |
| 884 | OS << *it; |
| 885 | } |
| 886 | OS << "]"; |
| 887 | } |
| 888 | break; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 889 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 890 | case MCFragment::FT_Fill: { |
| 891 | const MCFillFragment *FF = cast<MCFillFragment>(this); |
| 892 | OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize() |
| 893 | << " Size:" << FF->getSize(); |
| 894 | break; |
| 895 | } |
| 896 | case MCFragment::FT_Inst: { |
| 897 | const MCInstFragment *IF = cast<MCInstFragment>(this); |
| 898 | OS << "\n "; |
| 899 | OS << " Inst:"; |
| 900 | IF->getInst().dump_pretty(OS); |
| 901 | break; |
| 902 | } |
| 903 | case MCFragment::FT_Org: { |
| 904 | const MCOrgFragment *OF = cast<MCOrgFragment>(this); |
| 905 | OS << "\n "; |
| 906 | OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue(); |
| 907 | break; |
| 908 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 909 | case MCFragment::FT_Dwarf: { |
| 910 | const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this); |
| 911 | OS << "\n "; |
| 912 | OS << " AddrDelta:" << OF->getAddrDelta() |
| 913 | << " LineDelta:" << OF->getLineDelta(); |
| 914 | break; |
| 915 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 916 | case MCFragment::FT_DwarfFrame: { |
| 917 | const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this); |
| 918 | OS << "\n "; |
| 919 | OS << " AddrDelta:" << CF->getAddrDelta(); |
| 920 | break; |
| 921 | } |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 922 | case MCFragment::FT_LEB: { |
| 923 | const MCLEBFragment *LF = cast<MCLEBFragment>(this); |
| 924 | OS << "\n "; |
| 925 | OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned(); |
| 926 | break; |
| 927 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 928 | } |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 929 | OS << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 932 | void MCSectionData::dump() { |
| 933 | raw_ostream &OS = llvm::errs(); |
| 934 | |
| 935 | OS << "<MCSectionData"; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 936 | OS << " Alignment:" << getAlignment() << " Fragments:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 937 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 938 | if (it != begin()) OS << ",\n "; |
| 939 | it->dump(); |
| 940 | } |
| 941 | OS << "]>"; |
| 942 | } |
| 943 | |
| 944 | void MCSymbolData::dump() { |
| 945 | raw_ostream &OS = llvm::errs(); |
| 946 | |
| 947 | OS << "<MCSymbolData Symbol:" << getSymbol() |
| 948 | << " Fragment:" << getFragment() << " Offset:" << getOffset() |
| 949 | << " Flags:" << getFlags() << " Index:" << getIndex(); |
| 950 | if (isCommon()) |
| 951 | OS << " (common, size:" << getCommonSize() |
| 952 | << " align: " << getCommonAlignment() << ")"; |
| 953 | if (isExternal()) |
| 954 | OS << " (external)"; |
| 955 | if (isPrivateExtern()) |
| 956 | OS << " (private extern)"; |
| 957 | OS << ">"; |
| 958 | } |
| 959 | |
| 960 | void MCAssembler::dump() { |
| 961 | raw_ostream &OS = llvm::errs(); |
| 962 | |
| 963 | OS << "<MCAssembler\n"; |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 964 | OS << " Sections:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 965 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 966 | if (it != begin()) OS << ",\n "; |
| 967 | it->dump(); |
| 968 | } |
| 969 | OS << "],\n"; |
| 970 | OS << " Symbols:["; |
| 971 | |
| 972 | for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 973 | if (it != symbol_begin()) OS << ",\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 974 | it->dump(); |
| 975 | } |
| 976 | OS << "]>\n"; |
| 977 | } |