1 //===- lib/MC/MCFragment.cpp - Assembler Fragment Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCFragment.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixupKindInfo.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCValue.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
31 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
32 : Assembler(Asm), LastValidFragment()
34 // Compute the section layout order. Virtual sections must go last.
35 for (MCSection &Sec : Asm)
36 if (!Sec.isVirtualSection())
37 SectionOrder.push_back(&Sec);
38 for (MCSection &Sec : Asm)
39 if (Sec.isVirtualSection())
40 SectionOrder.push_back(&Sec);
43 bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
44 const MCSection *Sec = F->getParent();
45 const MCFragment *LastValid = LastValidFragment.lookup(Sec);
48 assert(LastValid->getParent() == Sec);
49 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
52 void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
53 // If this fragment wasn't already valid, we don't need to do anything.
54 if (!isFragmentValid(F))
57 // Otherwise, reset the last valid fragment to the previous fragment
58 // (if this is the first fragment, it will be NULL).
59 LastValidFragment[F->getParent()] = F->getPrevNode();
62 void MCAsmLayout::ensureValid(const MCFragment *F) const {
63 MCSection *Sec = F->getParent();
64 MCSection::iterator I;
65 if (MCFragment *Cur = LastValidFragment[Sec])
66 I = ++MCSection::iterator(Cur);
70 // Advance the layout position until the fragment is valid.
71 while (!isFragmentValid(F)) {
72 assert(I != Sec->end() && "Layout bookkeeping error");
73 const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
78 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
80 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
84 // Simple getSymbolOffset helper for the non-varibale case.
85 static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
86 bool ReportError, uint64_t &Val) {
87 if (!S.getFragment()) {
89 report_fatal_error("unable to evaluate offset to undefined symbol '" +
93 Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
97 static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
98 bool ReportError, uint64_t &Val) {
100 return getLabelOffset(Layout, S, ReportError, Val);
102 // If SD is a variable, evaluate it.
104 if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
105 report_fatal_error("unable to evaluate offset for variable '" +
108 uint64_t Offset = Target.getConstant();
110 const MCSymbolRefExpr *A = Target.getSymA();
113 if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
118 const MCSymbolRefExpr *B = Target.getSymB();
121 if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
130 bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
131 return getSymbolOffsetImpl(*this, S, false, Val);
134 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
136 getSymbolOffsetImpl(*this, S, true, Val);
140 const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
141 if (!Symbol.isVariable())
144 const MCExpr *Expr = Symbol.getVariableValue();
146 if (!Expr->evaluateAsValue(Value, *this)) {
147 Assembler.getContext().reportError(
148 SMLoc(), "expression could not be evaluated");
152 const MCSymbolRefExpr *RefB = Value.getSymB();
154 Assembler.getContext().reportError(
155 SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
156 "' could not be evaluated in a subtraction expression");
160 const MCSymbolRefExpr *A = Value.getSymA();
164 const MCSymbol &ASym = A->getSymbol();
165 const MCAssembler &Asm = getAssembler();
166 if (ASym.isCommon()) {
167 // FIXME: we should probably add a SMLoc to MCExpr.
168 Asm.getContext().reportError(SMLoc(),
169 "Common symbol '" + ASym.getName() +
170 "' cannot be used in assignment expr");
177 uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
178 // The size is the last fragment's end offset.
179 const MCFragment &F = Sec->getFragmentList().back();
180 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
183 uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
184 // Virtual sections have no file size.
185 if (Sec->isVirtualSection())
188 // Otherwise, the file size is the same as the address space size.
189 return getSectionAddressSize(Sec);
192 uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
194 uint64_t FOffset, uint64_t FSize) {
195 uint64_t BundleSize = Assembler.getBundleAlignSize();
196 assert(BundleSize > 0 &&
197 "computeBundlePadding should only be called if bundling is enabled");
198 uint64_t BundleMask = BundleSize - 1;
199 uint64_t OffsetInBundle = FOffset & BundleMask;
200 uint64_t EndOfFragment = OffsetInBundle + FSize;
202 // There are two kinds of bundling restrictions:
204 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
205 // *end* on a bundle boundary.
206 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
207 // would, add padding until the end of the bundle so that the fragment
208 // will start in a new one.
209 if (F->alignToBundleEnd()) {
210 // Three possibilities here:
212 // A) The fragment just happens to end at a bundle boundary, so we're good.
213 // B) The fragment ends before the current bundle boundary: pad it just
214 // enough to reach the boundary.
215 // C) The fragment ends after the current bundle boundary: pad it until it
216 // reaches the end of the next bundle boundary.
218 // Note: this code could be made shorter with some modulo trickery, but it's
219 // intentionally kept in its more explicit form for simplicity.
220 if (EndOfFragment == BundleSize)
222 else if (EndOfFragment < BundleSize)
223 return BundleSize - EndOfFragment;
224 else { // EndOfFragment > BundleSize
225 return 2 * BundleSize - EndOfFragment;
227 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
228 return BundleSize - OffsetInBundle;
235 void ilist_node_traits<MCFragment>::deleteNode(MCFragment *V) {
239 MCFragment::MCFragment() : Kind(FragmentType(~0)), HasInstructions(false),
240 AlignToBundleEnd(false), BundlePadding(0) {
243 MCFragment::~MCFragment() { }
245 MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
246 uint8_t BundlePadding, MCSection *Parent)
247 : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false),
248 BundlePadding(BundlePadding), Parent(Parent), Atom(nullptr),
249 Offset(~UINT64_C(0)) {
250 if (Parent && !isDummy())
251 Parent->getFragmentList().push_back(this);
254 void MCFragment::destroy() {
255 // First check if we are the sentinal.
256 if (Kind == FragmentType(~0)) {
263 delete cast<MCAlignFragment>(this);
266 delete cast<MCDataFragment>(this);
268 case FT_CompactEncodedInst:
269 delete cast<MCCompactEncodedInstFragment>(this);
272 delete cast<MCFillFragment>(this);
275 delete cast<MCRelaxableFragment>(this);
278 delete cast<MCOrgFragment>(this);
281 delete cast<MCDwarfLineAddrFragment>(this);
284 delete cast<MCDwarfCallFrameFragment>(this);
287 delete cast<MCLEBFragment>(this);
290 delete cast<MCSafeSEHFragment>(this);
293 delete cast<MCDummyFragment>(this);
304 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
305 OS << "<MCFixup" << " Offset:" << AF.getOffset()
306 << " Value:" << *AF.getValue()
307 << " Kind:" << AF.getKind() << ">";
313 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
314 void MCFragment::dump() {
315 raw_ostream &OS = llvm::errs();
319 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
320 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
321 case MCFragment::FT_CompactEncodedInst:
322 OS << "MCCompactEncodedInstFragment"; break;
323 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
324 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
325 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
326 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
327 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
328 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
329 case MCFragment::FT_SafeSEH: OS << "MCSafeSEHFragment"; break;
330 case MCFragment::FT_Dummy:
331 OS << "MCDummyFragment";
335 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
336 << " Offset:" << Offset
337 << " HasInstructions:" << hasInstructions()
338 << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">";
341 case MCFragment::FT_Align: {
342 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
343 if (AF->hasEmitNops())
344 OS << " (emit nops)";
346 OS << " Alignment:" << AF->getAlignment()
347 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
348 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
351 case MCFragment::FT_Data: {
352 const MCDataFragment *DF = cast<MCDataFragment>(this);
355 const SmallVectorImpl<char> &Contents = DF->getContents();
356 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
358 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
360 OS << "] (" << Contents.size() << " bytes)";
362 if (DF->fixup_begin() != DF->fixup_end()) {
365 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
366 ie = DF->fixup_end(); it != ie; ++it) {
367 if (it != DF->fixup_begin()) OS << ",\n ";
374 case MCFragment::FT_CompactEncodedInst: {
375 const MCCompactEncodedInstFragment *CEIF =
376 cast<MCCompactEncodedInstFragment>(this);
379 const SmallVectorImpl<char> &Contents = CEIF->getContents();
380 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
382 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
384 OS << "] (" << Contents.size() << " bytes)";
387 case MCFragment::FT_Fill: {
388 const MCFillFragment *FF = cast<MCFillFragment>(this);
389 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
390 << " Size:" << FF->getSize();
393 case MCFragment::FT_Relaxable: {
394 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
397 F->getInst().dump_pretty(OS);
400 case MCFragment::FT_Org: {
401 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
403 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
406 case MCFragment::FT_Dwarf: {
407 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
409 OS << " AddrDelta:" << OF->getAddrDelta()
410 << " LineDelta:" << OF->getLineDelta();
413 case MCFragment::FT_DwarfFrame: {
414 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
416 OS << " AddrDelta:" << CF->getAddrDelta();
419 case MCFragment::FT_LEB: {
420 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
422 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
425 case MCFragment::FT_SafeSEH: {
426 const MCSafeSEHFragment *F = cast<MCSafeSEHFragment>(this);
428 OS << " Sym:" << F->getSymbol();
431 case MCFragment::FT_Dummy:
437 void MCAssembler::dump() {
438 raw_ostream &OS = llvm::errs();
440 OS << "<MCAssembler\n";
441 OS << " Sections:[\n ";
442 for (iterator it = begin(), ie = end(); it != ie; ++it) {
443 if (it != begin()) OS << ",\n ";
449 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
450 if (it != symbol_begin()) OS << ",\n ";
453 OS << ", Index:" << it->getIndex() << ", ";