LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - AsmPrinterDwarf.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 120 153 78.4 %
Date: 2018-06-17 00:07:59 Functions: 12 13 92.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
       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             : // This file implements the Dwarf emissions parts of AsmPrinter.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "ByteStreamer.h"
      15             : #include "llvm/ADT/Twine.h"
      16             : #include "llvm/BinaryFormat/Dwarf.h"
      17             : #include "llvm/CodeGen/AsmPrinter.h"
      18             : #include "llvm/CodeGen/DIE.h"
      19             : #include "llvm/CodeGen/MachineFunction.h"
      20             : #include "llvm/IR/DataLayout.h"
      21             : #include "llvm/MC/MCAsmInfo.h"
      22             : #include "llvm/MC/MCRegisterInfo.h"
      23             : #include "llvm/MC/MCSection.h"
      24             : #include "llvm/MC/MCStreamer.h"
      25             : #include "llvm/MC/MCSymbol.h"
      26             : #include "llvm/MC/MachineLocation.h"
      27             : #include "llvm/Support/ErrorHandling.h"
      28             : #include "llvm/Target/TargetLoweringObjectFile.h"
      29             : #include "llvm/Target/TargetMachine.h"
      30             : using namespace llvm;
      31             : 
      32             : #define DEBUG_TYPE "asm-printer"
      33             : 
      34             : //===----------------------------------------------------------------------===//
      35             : // Dwarf Emission Helper Routines
      36             : //===----------------------------------------------------------------------===//
      37             : 
      38             : /// EmitSLEB128 - emit the specified signed leb128 value.
      39       87639 : void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
      40       87639 :   if (isVerbose() && Desc)
      41           0 :     OutStreamer->AddComment(Desc);
      42             : 
      43       87639 :   OutStreamer->EmitSLEB128IntValue(Value);
      44       87639 : }
      45             : 
      46     2958268 : void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc) const {
      47     2958268 :   if (isVerbose() && Desc)
      48       50750 :     OutStreamer->AddComment(Desc);
      49             : 
      50     2958268 :   OutStreamer->EmitULEB128IntValue(Value);
      51     2958268 : }
      52             : 
      53             : /// Emit something like ".uleb128 Hi-Lo".
      54      115935 : void AsmPrinter::EmitLabelDifferenceAsULEB128(const MCSymbol *Hi,
      55             :                                               const MCSymbol *Lo) const {
      56      115935 :   OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
      57      115935 : }
      58             : 
      59         837 : static const char *DecodeDWARFEncoding(unsigned Encoding) {
      60         837 :   switch (Encoding) {
      61             :   case dwarf::DW_EH_PE_absptr:
      62             :     return "absptr";
      63         369 :   case dwarf::DW_EH_PE_omit:
      64         369 :     return "omit";
      65           0 :   case dwarf::DW_EH_PE_pcrel:
      66           0 :     return "pcrel";
      67         247 :   case dwarf::DW_EH_PE_uleb128:
      68         247 :     return "uleb128";
      69           0 :   case dwarf::DW_EH_PE_sleb128:
      70           0 :     return "sleb128";
      71          79 :   case dwarf::DW_EH_PE_udata4:
      72          79 :     return "udata4";
      73           0 :   case dwarf::DW_EH_PE_udata8:
      74           0 :     return "udata8";
      75           0 :   case dwarf::DW_EH_PE_sdata4:
      76           0 :     return "sdata4";
      77           0 :   case dwarf::DW_EH_PE_sdata8:
      78           0 :     return "sdata8";
      79           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
      80           0 :     return "pcrel udata4";
      81           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
      82           0 :     return "pcrel sdata4";
      83           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
      84           0 :     return "pcrel udata8";
      85           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
      86           0 :     return "pcrel sdata8";
      87           0 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
      88             :       :
      89           0 :     return "indirect pcrel udata4";
      90          55 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
      91             :       :
      92          55 :     return "indirect pcrel sdata4";
      93           1 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
      94             :       :
      95           1 :     return "indirect pcrel udata8";
      96           2 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
      97             :       :
      98           2 :     return "indirect pcrel sdata8";
      99             :   }
     100             : 
     101           0 :   return "<unknown encoding>";
     102             : }
     103             : 
     104             : /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
     105             : /// encoding.  If verbose assembly output is enabled, we output comments
     106             : /// describing the encoding.  Desc is an optional string saying what the
     107             : /// encoding is specifying (e.g. "LSDA").
     108       19620 : void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
     109       19620 :   if (isVerbose()) {
     110         837 :     if (Desc)
     111         837 :       OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
     112        1674 :                               Twine(DecodeDWARFEncoding(Val)));
     113             :     else
     114           0 :       OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
     115             :   }
     116             : 
     117       19620 :   OutStreamer->EmitIntValue(Val, 1);
     118       19620 : }
     119             : 
     120             : /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
     121        3894 : unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
     122        3894 :   if (Encoding == dwarf::DW_EH_PE_omit)
     123             :     return 0;
     124             : 
     125        3894 :   switch (Encoding & 0x07) {
     126           0 :   default:
     127           0 :     llvm_unreachable("Invalid encoded value.");
     128         102 :   case dwarf::DW_EH_PE_absptr:
     129         102 :     return MF->getDataLayout().getPointerSize();
     130             :   case dwarf::DW_EH_PE_udata2:
     131             :     return 2;
     132        3787 :   case dwarf::DW_EH_PE_udata4:
     133        3787 :     return 4;
     134           5 :   case dwarf::DW_EH_PE_udata8:
     135           5 :     return 8;
     136             :   }
     137             : }
     138             : 
     139        3894 : void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
     140             :                                     unsigned Encoding) const {
     141        3894 :   if (GV) {
     142         137 :     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
     143             : 
     144             :     const MCExpr *Exp =
     145         274 :         TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
     146         137 :     OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
     147             :   } else
     148        3757 :     OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
     149        3894 : }
     150             : 
     151       25712 : void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
     152             :                                           bool ForceOffset) const {
     153       25712 :   if (!ForceOffset) {
     154             :     // On COFF targets, we have to emit the special .secrel32 directive.
     155       25657 :     if (MAI->needsDwarfSectionOffsetDirective()) {
     156         130 :       OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
     157         130 :       return;
     158             :     }
     159             : 
     160             :     // If the format uses relocations with dwarf, refer to the symbol directly.
     161       25527 :     if (MAI->doesDwarfUseRelocationsAcrossSections()) {
     162       25140 :       OutStreamer->EmitSymbolValue(Label, 4);
     163       25140 :       return;
     164             :     }
     165             :   }
     166             : 
     167             :   // Otherwise, emit it as a label difference from the start of the section.
     168         442 :   EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
     169             : }
     170             : 
     171        2378 : void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const {
     172        2378 :   if (MAI->doesDwarfUseRelocationsAcrossSections()) {
     173             :     assert(S.Symbol && "No symbol available");
     174         789 :     emitDwarfSymbolReference(S.Symbol);
     175         789 :     return;
     176             :   }
     177             : 
     178             :   // Just emit the offset directly; no need for symbol math.
     179        1589 :   emitInt32(S.Offset);
     180             : }
     181             : 
     182           0 : void AsmPrinter::EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
     183           0 :   EmitLabelPlusOffset(Label, Offset, MAI->getCodePointerSize());
     184           0 : }
     185             : 
     186             : //===----------------------------------------------------------------------===//
     187             : // Dwarf Lowering Routines
     188             : //===----------------------------------------------------------------------===//
     189             : 
     190      291761 : void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
     191      291761 :   switch (Inst.getOperation()) {
     192           0 :   default:
     193           0 :     llvm_unreachable("Unexpected instruction");
     194       98776 :   case MCCFIInstruction::OpDefCfaOffset:
     195       98776 :     OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
     196       98776 :     break;
     197       27780 :   case MCCFIInstruction::OpAdjustCfaOffset:
     198       27780 :     OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
     199       27780 :     break;
     200       43566 :   case MCCFIInstruction::OpDefCfa:
     201       43566 :     OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
     202       43566 :     break;
     203       39337 :   case MCCFIInstruction::OpDefCfaRegister:
     204       39337 :     OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
     205       39337 :     break;
     206       71684 :   case MCCFIInstruction::OpOffset:
     207       71684 :     OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
     208       71684 :     break;
     209         393 :   case MCCFIInstruction::OpRegister:
     210         393 :     OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
     211         393 :     break;
     212         393 :   case MCCFIInstruction::OpWindowSave:
     213         393 :     OutStreamer->EmitCFIWindowSave();
     214         393 :     break;
     215           4 :   case MCCFIInstruction::OpSameValue:
     216           4 :     OutStreamer->EmitCFISameValue(Inst.getRegister());
     217           4 :     break;
     218        9826 :   case MCCFIInstruction::OpGnuArgsSize:
     219        9826 :     OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
     220        9826 :     break;
     221           0 :   case MCCFIInstruction::OpEscape:
     222           0 :     OutStreamer->EmitCFIEscape(Inst.getValues());
     223           0 :     break;
     224           2 :   case MCCFIInstruction::OpRestore:
     225           2 :     OutStreamer->EmitCFIRestore(Inst.getRegister());
     226           2 :     break;
     227             :   }
     228      291761 : }
     229             : 
     230     1613344 : void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
     231             :   // Emit the code (index) for the abbreviation.
     232     1613344 :   if (isVerbose())
     233        9768 :     OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
     234        9768 :                             Twine::utohexstr(Die.getOffset()) + ":0x" +
     235        9768 :                             Twine::utohexstr(Die.getSize()) + " " +
     236        6512 :                             dwarf::TagString(Die.getTag()));
     237     1613344 :   EmitULEB128(Die.getAbbrevNumber());
     238             : 
     239             :   // Emit the DIE attribute values.
     240     6875890 :   for (const auto &V : Die.values()) {
     241     5262546 :     dwarf::Attribute Attr = V.getAttribute();
     242             :     assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
     243             : 
     244     5262546 :     if (isVerbose()) {
     245       27540 :       OutStreamer->AddComment(dwarf::AttributeString(Attr));
     246       13770 :       if (Attr == dwarf::DW_AT_accessibility)
     247          34 :         OutStreamer->AddComment(
     248         102 :             dwarf::AccessibilityString(V.getDIEInteger().getValue()));
     249             :     }
     250             : 
     251             :     // Emit an attribute using the defined form.
     252     5262546 :     V.EmitValue(this);
     253             :   }
     254             : 
     255             :   // Emit the DIE children if any.
     256             :   if (Die.hasChildren()) {
     257     2037142 :     for (auto &Child : Die.children())
     258     1609222 :       emitDwarfDIE(Child);
     259             : 
     260      855840 :     OutStreamer->AddComment("End Of Children Mark");
     261      427920 :     emitInt8(0);
     262             :   }
     263     1613344 : }
     264             : 
     265       72275 : void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
     266             :   // Emit the abbreviations code (base 1 index.)
     267       72275 :   EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
     268             : 
     269             :   // Emit the abbreviations data.
     270       72275 :   Abbrev.Emit(this);
     271       72275 : }

Generated by: LCOV version 1.13