LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - AsmPrinterDwarf.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 112 140 80.0 %
Date: 2017-09-14 15:23:50 Functions: 11 11 100.0 %
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 "DwarfDebug.h"
      16             : #include "DwarfExpression.h"
      17             : #include "llvm/ADT/Twine.h"
      18             : #include "llvm/BinaryFormat/Dwarf.h"
      19             : #include "llvm/CodeGen/AsmPrinter.h"
      20             : #include "llvm/CodeGen/DIE.h"
      21             : #include "llvm/CodeGen/MachineFunction.h"
      22             : #include "llvm/CodeGen/MachineModuleInfo.h"
      23             : #include "llvm/IR/DataLayout.h"
      24             : #include "llvm/MC/MCAsmInfo.h"
      25             : #include "llvm/MC/MCRegisterInfo.h"
      26             : #include "llvm/MC/MCSection.h"
      27             : #include "llvm/MC/MCStreamer.h"
      28             : #include "llvm/MC/MCSymbol.h"
      29             : #include "llvm/MC/MachineLocation.h"
      30             : #include "llvm/Support/ErrorHandling.h"
      31             : #include "llvm/Target/TargetLoweringObjectFile.h"
      32             : #include "llvm/Target/TargetMachine.h"
      33             : #include "llvm/Target/TargetSubtargetInfo.h"
      34             : using namespace llvm;
      35             : 
      36             : #define DEBUG_TYPE "asm-printer"
      37             : 
      38             : //===----------------------------------------------------------------------===//
      39             : // Dwarf Emission Helper Routines
      40             : //===----------------------------------------------------------------------===//
      41             : 
      42             : /// EmitSLEB128 - emit the specified signed leb128 value.
      43        2946 : void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
      44        2946 :   if (isVerbose() && Desc)
      45           0 :     OutStreamer->AddComment(Desc);
      46             : 
      47        5892 :   OutStreamer->EmitSLEB128IntValue(Value);
      48        2946 : }
      49             : 
      50             : /// EmitULEB128 - emit the specified unsigned leb128 value.
      51      351083 : void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
      52             :                              unsigned PadTo) const {
      53      351083 :   if (isVerbose() && Desc)
      54       65049 :     OutStreamer->AddComment(Desc);
      55             : 
      56      702166 :   OutStreamer->EmitULEB128IntValue(Value, PadTo);
      57      351083 : }
      58             : 
      59         717 : static const char *DecodeDWARFEncoding(unsigned Encoding) {
      60         717 :   switch (Encoding) {
      61             :   case dwarf::DW_EH_PE_absptr:
      62             :     return "absptr";
      63         249 :   case dwarf::DW_EH_PE_omit:
      64         249 :     return "omit";
      65           0 :   case dwarf::DW_EH_PE_pcrel:
      66           0 :     return "pcrel";
      67         294 :   case dwarf::DW_EH_PE_udata4:
      68         294 :     return "udata4";
      69           0 :   case dwarf::DW_EH_PE_udata8:
      70           0 :     return "udata8";
      71           0 :   case dwarf::DW_EH_PE_sdata4:
      72           0 :     return "sdata4";
      73           0 :   case dwarf::DW_EH_PE_sdata8:
      74           0 :     return "sdata8";
      75           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
      76           0 :     return "pcrel udata4";
      77           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
      78           0 :     return "pcrel sdata4";
      79           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
      80           0 :     return "pcrel udata8";
      81           0 :   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
      82           0 :     return "pcrel sdata8";
      83           0 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
      84             :       :
      85           0 :     return "indirect pcrel udata4";
      86          75 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
      87             :       :
      88          75 :     return "indirect pcrel sdata4";
      89           6 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
      90             :       :
      91           6 :     return "indirect pcrel udata8";
      92           2 :   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
      93             :       :
      94           2 :     return "indirect pcrel sdata8";
      95             :   }
      96             : 
      97           0 :   return "<unknown encoding>";
      98             : }
      99             : 
     100             : /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
     101             : /// encoding.  If verbose assembly output is enabled, we output comments
     102             : /// describing the encoding.  Desc is an optional string saying what the
     103             : /// encoding is specifying (e.g. "LSDA").
     104        7413 : void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
     105        7413 :   if (isVerbose()) {
     106         717 :     if (Desc)
     107        4302 :       OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
     108        2151 :                               Twine(DecodeDWARFEncoding(Val)));
     109             :     else
     110           0 :       OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
     111             :   }
     112             : 
     113       14826 :   OutStreamer->EmitIntValue(Val, 1);
     114        7413 : }
     115             : 
     116             : /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
     117        3421 : unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
     118        3421 :   if (Encoding == dwarf::DW_EH_PE_omit)
     119             :     return 0;
     120             : 
     121        3421 :   switch (Encoding & 0x07) {
     122           0 :   default:
     123           0 :     llvm_unreachable("Invalid encoded value.");
     124         182 :   case dwarf::DW_EH_PE_absptr:
     125         182 :     return MF->getDataLayout().getPointerSize();
     126             :   case dwarf::DW_EH_PE_udata2:
     127             :     return 2;
     128        3224 :   case dwarf::DW_EH_PE_udata4:
     129        3224 :     return 4;
     130          15 :   case dwarf::DW_EH_PE_udata8:
     131          15 :     return 8;
     132             :   }
     133             : }
     134             : 
     135         960 : void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
     136             :                                     unsigned Encoding) const {
     137         960 :   if (GV) {
     138         100 :     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
     139             : 
     140             :     const MCExpr *Exp =
     141         200 :         TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
     142         200 :     OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
     143             :   } else
     144        1720 :     OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
     145         960 : }
     146             : 
     147       10488 : void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
     148             :                                           bool ForceOffset) const {
     149       10488 :   if (!ForceOffset) {
     150             :     // On COFF targets, we have to emit the special .secrel32 directive.
     151       10483 :     if (MAI->needsDwarfSectionOffsetDirective()) {
     152         212 :       OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
     153         106 :       return;
     154             :     }
     155             : 
     156             :     // If the format uses relocations with dwarf, refer to the symbol directly.
     157       10377 :     if (MAI->doesDwarfUseRelocationsAcrossSections()) {
     158       20034 :       OutStreamer->EmitSymbolValue(Label, 4);
     159       10017 :       return;
     160             :     }
     161             :   }
     162             : 
     163             :   // Otherwise, emit it as a label difference from the start of the section.
     164         365 :   EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
     165             : }
     166             : 
     167         906 : void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
     168         906 :   if (MAI->doesDwarfUseRelocationsAcrossSections()) {
     169          40 :     emitDwarfSymbolReference(S.getSymbol());
     170          40 :     return;
     171             :   }
     172             : 
     173             :   // Just emit the offset directly; no need for symbol math.
     174         866 :   EmitInt32(S.getOffset());
     175             : }
     176             : 
     177             : //===----------------------------------------------------------------------===//
     178             : // Dwarf Lowering Routines
     179             : //===----------------------------------------------------------------------===//
     180             : 
     181      207605 : void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
     182      207605 :   switch (Inst.getOperation()) {
     183           0 :   default:
     184           0 :     llvm_unreachable("Unexpected instruction");
     185       28488 :   case MCCFIInstruction::OpDefCfaOffset:
     186       56976 :     OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
     187       28488 :     break;
     188      135185 :   case MCCFIInstruction::OpAdjustCfaOffset:
     189      270370 :     OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
     190      135185 :     break;
     191        1733 :   case MCCFIInstruction::OpDefCfa:
     192        3466 :     OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
     193        1733 :     break;
     194        2089 :   case MCCFIInstruction::OpDefCfaRegister:
     195        4178 :     OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
     196        2089 :     break;
     197       31332 :   case MCCFIInstruction::OpOffset:
     198       62664 :     OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
     199       31332 :     break;
     200         380 :   case MCCFIInstruction::OpRegister:
     201         760 :     OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
     202         380 :     break;
     203         380 :   case MCCFIInstruction::OpWindowSave:
     204         760 :     OutStreamer->EmitCFIWindowSave();
     205         380 :     break;
     206           4 :   case MCCFIInstruction::OpSameValue:
     207           8 :     OutStreamer->EmitCFISameValue(Inst.getRegister());
     208           4 :     break;
     209        8014 :   case MCCFIInstruction::OpGnuArgsSize:
     210       16028 :     OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
     211        8014 :     break;
     212           0 :   case MCCFIInstruction::OpEscape:
     213           0 :     OutStreamer->EmitCFIEscape(Inst.getValues());
     214           0 :     break;
     215             :   }
     216      207605 : }
     217             : 
     218      202721 : void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
     219             :   // Emit the code (index) for the abbreviation.
     220      202721 :   if (isVerbose())
     221       20780 :     OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
     222       12468 :                             Twine::utohexstr(Die.getOffset()) + ":0x" +
     223       12468 :                             Twine::utohexstr(Die.getSize()) + " " +
     224        6234 :                             dwarf::TagString(Die.getTag()));
     225      202721 :   EmitULEB128(Die.getAbbrevNumber());
     226             : 
     227             :   // Emit the DIE attribute values.
     228     1839794 :   for (const auto &V : Die.values()) {
     229      717176 :     dwarf::Attribute Attr = V.getAttribute();
     230             :     assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
     231             : 
     232      717176 :     if (isVerbose()) {
     233       28545 :       OutStreamer->AddComment(dwarf::AttributeString(Attr));
     234        9515 :       if (Attr == dwarf::DW_AT_accessibility)
     235          66 :         OutStreamer->AddComment(
     236          88 :             dwarf::AccessibilityString(V.getDIEInteger().getValue()));
     237             :     }
     238             : 
     239             :     // Emit an attribute using the defined form.
     240      717176 :     V.EmitValue(this);
     241             :   }
     242             : 
     243             :   // Emit the DIE children if any.
     244             :   if (Die.hasChildren()) {
     245      273291 :     for (auto &Child : Die.children())
     246      201776 :       emitDwarfDIE(Child);
     247             : 
     248      214545 :     OutStreamer->AddComment("End Of Children Mark");
     249       71515 :     EmitInt8(0);
     250             :   }
     251      202721 : }
     252             : 
     253        7878 : void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
     254             :   // Emit the abbreviations code (base 1 index.)
     255        7878 :   EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
     256             : 
     257             :   // Emit the abbreviations data.
     258        7878 :   Abbrev.Emit(this);
     259        7878 : }

Generated by: LCOV version 1.13