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

Generated by: LCOV version 1.13