LCOV - code coverage report
Current view: top level - lib/MC - MCFragment.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 113 124 91.1 %
Date: 2018-06-17 00:07:59 Functions: 16 17 94.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- lib/MC/MCFragment.cpp - Assembler Fragment 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             : 
      10             : #include "llvm/MC/MCFragment.h"
      11             : #include "llvm/ADT/SmallVector.h"
      12             : #include "llvm/ADT/StringExtras.h"
      13             : #include "llvm/ADT/Twine.h"
      14             : #include "llvm/Config/llvm-config.h"
      15             : #include "llvm/MC/MCAsmLayout.h"
      16             : #include "llvm/MC/MCAssembler.h"
      17             : #include "llvm/MC/MCContext.h"
      18             : #include "llvm/MC/MCExpr.h"
      19             : #include "llvm/MC/MCFixup.h"
      20             : #include "llvm/MC/MCSection.h"
      21             : #include "llvm/MC/MCSymbol.h"
      22             : #include "llvm/MC/MCValue.h"
      23             : #include "llvm/Support/Casting.h"
      24             : #include "llvm/Support/Compiler.h"
      25             : #include "llvm/Support/ErrorHandling.h"
      26             : #include "llvm/Support/raw_ostream.h"
      27             : #include <cassert>
      28             : #include <cstdint>
      29             : #include <utility>
      30             : 
      31             : using namespace llvm;
      32             : 
      33        7896 : MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
      34             :   // Compute the section layout order. Virtual sections must go last.
      35      460844 :   for (MCSection &Sec : Asm)
      36      452948 :     if (!Sec.isVirtualSection())
      37      427085 :       SectionOrder.push_back(&Sec);
      38      460844 :   for (MCSection &Sec : Asm)
      39      452948 :     if (Sec.isVirtualSection())
      40       25863 :       SectionOrder.push_back(&Sec);
      41        7896 : }
      42             : 
      43    13493707 : bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
      44    13493707 :   const MCSection *Sec = F->getParent();
      45    13493707 :   const MCFragment *LastValid = LastValidFragment.lookup(Sec);
      46    13488861 :   if (!LastValid)
      47             :     return false;
      48             :   assert(LastValid->getParent() == Sec);
      49    13035913 :   return F->getLayoutOrder() <= LastValid->getLayoutOrder();
      50             : }
      51             : 
      52        8142 : void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
      53             :   // If this fragment wasn't already valid, we don't need to do anything.
      54        8142 :   if (!isFragmentValid(F))
      55             :     return;
      56             : 
      57             :   // Otherwise, reset the last valid fragment to the previous fragment
      58             :   // (if this is the first fragment, it will be NULL).
      59        6586 :   LastValidFragment[F->getParent()] = F->getPrevNode();
      60             : }
      61             : 
      62     9871284 : void MCAsmLayout::ensureValid(const MCFragment *F) const {
      63     9871284 :   MCSection *Sec = F->getParent();
      64             :   MCSection::iterator I;
      65    19742568 :   if (MCFragment *Cur = LastValidFragment[Sec])
      66             :     I = ++MCSection::iterator(Cur);
      67             :   else
      68             :     I = Sec->begin();
      69             : 
      70             :   // Advance the layout position until the fragment is valid.
      71    13485565 :   while (!isFragmentValid(F)) {
      72             :     assert(I != Sec->end() && "Layout bookkeeping error");
      73     3614282 :     const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
      74             :     ++I;
      75             :   }
      76     9871283 : }
      77             : 
      78     9871284 : uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
      79     9871284 :   ensureValid(F);
      80             :   assert(F->Offset != ~UINT64_C(0) && "Address not set!");
      81     9871283 :   return F->Offset;
      82             : }
      83             : 
      84             : // Simple getSymbolOffset helper for the non-variable case.
      85     6044606 : static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
      86             :                            bool ReportError, uint64_t &Val) {
      87     6044606 :   if (!S.getFragment()) {
      88       17620 :     if (ReportError)
      89           1 :       report_fatal_error("unable to evaluate offset to undefined symbol '" +
      90           1 :                          S.getName() + "'");
      91             :     return false;
      92             :   }
      93     6026986 :   Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
      94     6026986 :   return true;
      95             : }
      96             : 
      97     6044588 : static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
      98             :                                 bool ReportError, uint64_t &Val) {
      99     6044588 :   if (!S.isVariable())
     100     6043418 :     return getLabelOffset(Layout, S, ReportError, Val);
     101             : 
     102             :   // If SD is a variable, evaluate it.
     103        1170 :   MCValue Target;
     104        1170 :   if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
     105           0 :     report_fatal_error("unable to evaluate offset for variable '" +
     106           0 :                        S.getName() + "'");
     107             : 
     108        1170 :   uint64_t Offset = Target.getConstant();
     109             : 
     110        1170 :   const MCSymbolRefExpr *A = Target.getSymA();
     111        1170 :   if (A) {
     112             :     uint64_t ValA;
     113         876 :     if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
     114          10 :       return false;
     115         866 :     Offset += ValA;
     116             :   }
     117             : 
     118        1160 :   const MCSymbolRefExpr *B = Target.getSymB();
     119        1160 :   if (B) {
     120             :     uint64_t ValB;
     121         312 :     if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
     122           0 :       return false;
     123         311 :     Offset -= ValB;
     124             :   }
     125             : 
     126        1159 :   Val = Offset;
     127        1159 :   return true;
     128             : }
     129             : 
     130      233375 : bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
     131      233375 :   return getSymbolOffsetImpl(*this, S, false, Val);
     132             : }
     133             : 
     134     5811213 : uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
     135             :   uint64_t Val;
     136     5811213 :   getSymbolOffsetImpl(*this, S, true, Val);
     137     5811212 :   return Val;
     138             : }
     139             : 
     140      233881 : const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
     141      233881 :   if (!Symbol.isVariable())
     142             :     return &Symbol;
     143             : 
     144             :   const MCExpr *Expr = Symbol.getVariableValue();
     145         666 :   MCValue Value;
     146         666 :   if (!Expr->evaluateAsValue(Value, *this)) {
     147           6 :     Assembler.getContext().reportError(
     148             :         Expr->getLoc(), "expression could not be evaluated");
     149           3 :     return nullptr;
     150             :   }
     151             : 
     152         663 :   const MCSymbolRefExpr *RefB = Value.getSymB();
     153         663 :   if (RefB) {
     154           4 :     Assembler.getContext().reportError(
     155           6 :         Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
     156           2 :                      "' could not be evaluated in a subtraction expression");
     157           2 :     return nullptr;
     158             :   }
     159             : 
     160         661 :   const MCSymbolRefExpr *A = Value.getSymA();
     161         661 :   if (!A)
     162             :     return nullptr;
     163             : 
     164         369 :   const MCSymbol &ASym = A->getSymbol();
     165         369 :   const MCAssembler &Asm = getAssembler();
     166         369 :   if (ASym.isCommon()) {
     167           8 :     Asm.getContext().reportError(Expr->getLoc(),
     168           8 :                                  "Common symbol '" + ASym.getName() +
     169           4 :                                      "' cannot be used in assignment expr");
     170           4 :     return nullptr;
     171             :   }
     172             : 
     173             :   return &ASym;
     174             : }
     175             : 
     176       56383 : uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
     177             :   // The size is the last fragment's end offset.
     178             :   const MCFragment &F = Sec->getFragmentList().back();
     179       56383 :   return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
     180             : }
     181             : 
     182        4701 : uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
     183             :   // Virtual sections have no file size.
     184        4701 :   if (Sec->isVirtualSection())
     185             :     return 0;
     186             : 
     187             :   // Otherwise, the file size is the same as the address space size.
     188        4653 :   return getSectionAddressSize(Sec);
     189             : }
     190             : 
     191         886 : uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
     192             :                                     const MCEncodedFragment *F,
     193             :                                     uint64_t FOffset, uint64_t FSize) {
     194         886 :   uint64_t BundleSize = Assembler.getBundleAlignSize();
     195             :   assert(BundleSize > 0 &&
     196             :          "computeBundlePadding should only be called if bundling is enabled");
     197         886 :   uint64_t BundleMask = BundleSize - 1;
     198         886 :   uint64_t OffsetInBundle = FOffset & BundleMask;
     199         886 :   uint64_t EndOfFragment = OffsetInBundle + FSize;
     200             : 
     201             :   // There are two kinds of bundling restrictions:
     202             :   //
     203             :   // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
     204             :   //    *end* on a bundle boundary.
     205             :   // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
     206             :   //    would, add padding until the end of the bundle so that the fragment
     207             :   //    will start in a new one.
     208         886 :   if (F->alignToBundleEnd()) {
     209             :     // Three possibilities here:
     210             :     //
     211             :     // A) The fragment just happens to end at a bundle boundary, so we're good.
     212             :     // B) The fragment ends before the current bundle boundary: pad it just
     213             :     //    enough to reach the boundary.
     214             :     // C) The fragment ends after the current bundle boundary: pad it until it
     215             :     //    reaches the end of the next bundle boundary.
     216             :     //
     217             :     // Note: this code could be made shorter with some modulo trickery, but it's
     218             :     // intentionally kept in its more explicit form for simplicity.
     219         288 :     if (EndOfFragment == BundleSize)
     220             :       return 0;
     221         270 :     else if (EndOfFragment < BundleSize)
     222         142 :       return BundleSize - EndOfFragment;
     223             :     else { // EndOfFragment > BundleSize
     224         128 :       return 2 * BundleSize - EndOfFragment;
     225             :     }
     226         598 :   } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
     227         161 :     return BundleSize - OffsetInBundle;
     228             :   else
     229             :     return 0;
     230             : }
     231             : 
     232             : /* *** */
     233             : 
     234     4634291 : void ilist_alloc_traits<MCFragment>::deleteNode(MCFragment *V) { V->destroy(); }
     235             : 
     236             : MCFragment::~MCFragment() = default;
     237             : 
     238     6730157 : MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
     239     6730157 :                        MCSection *Parent)
     240             :     : Kind(Kind), HasInstructions(HasInstructions), Parent(Parent),
     241     6730157 :       Atom(nullptr), Offset(~UINT64_C(0)) {
     242     6730157 :   if (Parent && !isDummy())
     243             :     Parent->getFragmentList().push_back(this);
     244     6730157 : }
     245             : 
     246     4634291 : void MCFragment::destroy() {
     247             :   // First check if we are the sentinal.
     248     4634291 :   if (Kind == FragmentType(~0)) {
     249           0 :     delete this;
     250             :     return;
     251             :   }
     252             : 
     253     4634291 :   switch (Kind) {
     254             :     case FT_Align:
     255      190016 :       delete cast<MCAlignFragment>(this);
     256             :       return;
     257             :     case FT_Data:
     258     3141603 :       delete cast<MCDataFragment>(this);
     259             :       return;
     260             :     case FT_CompactEncodedInst:
     261         135 :       delete cast<MCCompactEncodedInstFragment>(this);
     262             :       return;
     263             :     case FT_Fill:
     264      200134 :       delete cast<MCFillFragment>(this);
     265             :       return;
     266             :     case FT_Relaxable:
     267       62466 :       delete cast<MCRelaxableFragment>(this);
     268             :       return;
     269             :     case FT_Org:
     270          19 :       delete cast<MCOrgFragment>(this);
     271             :       return;
     272             :     case FT_Dwarf:
     273      721112 :       delete cast<MCDwarfLineAddrFragment>(this);
     274             :       return;
     275             :     case FT_DwarfFrame:
     276      259549 :       delete cast<MCDwarfCallFrameFragment>(this);
     277             :       return;
     278             :     case FT_LEB:
     279       59041 :       delete cast<MCLEBFragment>(this);
     280             :       return;
     281             :     case FT_Padding:
     282           0 :       delete cast<MCPaddingFragment>(this);
     283             :       return;
     284             :     case FT_SymbolId:
     285          12 :       delete cast<MCSymbolIdFragment>(this);
     286             :       return;
     287             :     case FT_CVInlineLines:
     288          23 :       delete cast<MCCVInlineLineTableFragment>(this);
     289             :       return;
     290             :     case FT_CVDefRange:
     291         181 :       delete cast<MCCVDefRangeFragment>(this);
     292             :       return;
     293             :     case FT_Dummy:
     294           0 :       delete cast<MCDummyFragment>(this);
     295             :       return;
     296             :   }
     297             : }
     298             : 
     299             : // Debugging methods
     300             : 
     301             : namespace llvm {
     302             : 
     303           0 : raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
     304           0 :   OS << "<MCFixup" << " Offset:" << AF.getOffset()
     305           0 :      << " Value:" << *AF.getValue()
     306           0 :      << " Kind:" << AF.getKind() << ">";
     307           0 :   return OS;
     308             : }
     309             : 
     310             : } // end namespace llvm
     311             : 
     312             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     313             : LLVM_DUMP_METHOD void MCFragment::dump() const {
     314             :   raw_ostream &OS = errs();
     315             : 
     316             :   OS << "<";
     317             :   switch (getKind()) {
     318             :   case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
     319             :   case MCFragment::FT_Data:  OS << "MCDataFragment"; break;
     320             :   case MCFragment::FT_CompactEncodedInst:
     321             :     OS << "MCCompactEncodedInstFragment"; break;
     322             :   case MCFragment::FT_Fill:  OS << "MCFillFragment"; break;
     323             :   case MCFragment::FT_Relaxable:  OS << "MCRelaxableFragment"; break;
     324             :   case MCFragment::FT_Org:   OS << "MCOrgFragment"; break;
     325             :   case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
     326             :   case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
     327             :   case MCFragment::FT_LEB:   OS << "MCLEBFragment"; break;
     328             :   case MCFragment::FT_Padding: OS << "MCPaddingFragment"; break;
     329             :   case MCFragment::FT_SymbolId:    OS << "MCSymbolIdFragment"; break;
     330             :   case MCFragment::FT_CVInlineLines: OS << "MCCVInlineLineTableFragment"; break;
     331             :   case MCFragment::FT_CVDefRange: OS << "MCCVDefRangeTableFragment"; break;
     332             :   case MCFragment::FT_Dummy: OS << "MCDummyFragment"; break;
     333             :   }
     334             : 
     335             :   OS << "<MCFragment " << (const void *)this << " LayoutOrder:" << LayoutOrder
     336             :      << " Offset:" << Offset << " HasInstructions:" << hasInstructions();
     337             :   if (const MCEncodedFragment *EF = cast<MCEncodedFragment>(this))
     338             :     OS << " BundlePadding:" << static_cast<unsigned>(EF->getBundlePadding());
     339             :   OS << ">";
     340             : 
     341             :   switch (getKind()) {
     342             :   case MCFragment::FT_Align: {
     343             :     const MCAlignFragment *AF = cast<MCAlignFragment>(this);
     344             :     if (AF->hasEmitNops())
     345             :       OS << " (emit nops)";
     346             :     OS << "\n       ";
     347             :     OS << " Alignment:" << AF->getAlignment()
     348             :        << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
     349             :        << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
     350             :     break;
     351             :   }
     352             :   case MCFragment::FT_Data:  {
     353             :     const MCDataFragment *DF = cast<MCDataFragment>(this);
     354             :     OS << "\n       ";
     355             :     OS << " Contents:[";
     356             :     const SmallVectorImpl<char> &Contents = DF->getContents();
     357             :     for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
     358             :       if (i) OS << ",";
     359             :       OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
     360             :     }
     361             :     OS << "] (" << Contents.size() << " bytes)";
     362             : 
     363             :     if (DF->fixup_begin() != DF->fixup_end()) {
     364             :       OS << ",\n       ";
     365             :       OS << " Fixups:[";
     366             :       for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
     367             :              ie = DF->fixup_end(); it != ie; ++it) {
     368             :         if (it != DF->fixup_begin()) OS << ",\n                ";
     369             :         OS << *it;
     370             :       }
     371             :       OS << "]";
     372             :     }
     373             :     break;
     374             :   }
     375             :   case MCFragment::FT_CompactEncodedInst: {
     376             :     const MCCompactEncodedInstFragment *CEIF =
     377             :       cast<MCCompactEncodedInstFragment>(this);
     378             :     OS << "\n       ";
     379             :     OS << " Contents:[";
     380             :     const SmallVectorImpl<char> &Contents = CEIF->getContents();
     381             :     for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
     382             :       if (i) OS << ",";
     383             :       OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
     384             :     }
     385             :     OS << "] (" << Contents.size() << " bytes)";
     386             :     break;
     387             :   }
     388             :   case MCFragment::FT_Fill:  {
     389             :     const MCFillFragment *FF = cast<MCFillFragment>(this);
     390             :     OS << " Value:" << static_cast<unsigned>(FF->getValue())
     391             :        << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
     392             :        << " NumValues:" << FF->getNumValues();
     393             :     break;
     394             :   }
     395             :   case MCFragment::FT_Relaxable:  {
     396             :     const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
     397             :     OS << "\n       ";
     398             :     OS << " Inst:";
     399             :     F->getInst().dump_pretty(OS);
     400             :     break;
     401             :   }
     402             :   case MCFragment::FT_Org:  {
     403             :     const MCOrgFragment *OF = cast<MCOrgFragment>(this);
     404             :     OS << "\n       ";
     405             :     OS << " Offset:" << OF->getOffset()
     406             :        << " Value:" << static_cast<unsigned>(OF->getValue());
     407             :     break;
     408             :   }
     409             :   case MCFragment::FT_Dwarf:  {
     410             :     const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
     411             :     OS << "\n       ";
     412             :     OS << " AddrDelta:" << OF->getAddrDelta()
     413             :        << " LineDelta:" << OF->getLineDelta();
     414             :     break;
     415             :   }
     416             :   case MCFragment::FT_DwarfFrame:  {
     417             :     const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
     418             :     OS << "\n       ";
     419             :     OS << " AddrDelta:" << CF->getAddrDelta();
     420             :     break;
     421             :   }
     422             :   case MCFragment::FT_LEB: {
     423             :     const MCLEBFragment *LF = cast<MCLEBFragment>(this);
     424             :     OS << "\n       ";
     425             :     OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
     426             :     break;
     427             :   }
     428             :   case MCFragment::FT_Padding: {
     429             :     const MCPaddingFragment *F = cast<MCPaddingFragment>(this);
     430             :     OS << "\n       ";
     431             :     OS << " PaddingPoliciesMask:" << F->getPaddingPoliciesMask()
     432             :        << " IsInsertionPoint:" << F->isInsertionPoint()
     433             :        << " Size:" << F->getSize();
     434             :     OS << "\n       ";
     435             :     OS << " Inst:";
     436             :     F->getInst().dump_pretty(OS);
     437             :     OS << " InstSize:" << F->getInstSize();
     438             :     OS << "\n       ";
     439             :     break;
     440             :   }
     441             :   case MCFragment::FT_SymbolId: {
     442             :     const MCSymbolIdFragment *F = cast<MCSymbolIdFragment>(this);
     443             :     OS << "\n       ";
     444             :     OS << " Sym:" << F->getSymbol();
     445             :     break;
     446             :   }
     447             :   case MCFragment::FT_CVInlineLines: {
     448             :     const auto *F = cast<MCCVInlineLineTableFragment>(this);
     449             :     OS << "\n       ";
     450             :     OS << " Sym:" << *F->getFnStartSym();
     451             :     break;
     452             :   }
     453             :   case MCFragment::FT_CVDefRange: {
     454             :     const auto *F = cast<MCCVDefRangeFragment>(this);
     455             :     OS << "\n       ";
     456             :     for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
     457             :          F->getRanges()) {
     458             :       OS << " RangeStart:" << RangeStartEnd.first;
     459             :       OS << " RangeEnd:" << RangeStartEnd.second;
     460             :     }
     461             :     break;
     462             :   }
     463             :   case MCFragment::FT_Dummy:
     464             :     break;
     465             :   }
     466             :   OS << ">";
     467             : }
     468             : #endif

Generated by: LCOV version 1.13