LLVM API Documentation

AsmPrinterDwarf.cpp
Go to the documentation of this file.
00001 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Dwarf emissions parts of AsmPrinter.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ByteStreamer.h"
00015 #include "llvm/CodeGen/AsmPrinter.h"
00016 #include "llvm/ADT/SmallBitVector.h"
00017 #include "llvm/ADT/Twine.h"
00018 #include "llvm/IR/DataLayout.h"
00019 #include "llvm/MC/MCAsmInfo.h"
00020 #include "llvm/MC/MCSection.h"
00021 #include "llvm/MC/MCStreamer.h"
00022 #include "llvm/MC/MCSymbol.h"
00023 #include "llvm/MC/MachineLocation.h"
00024 #include "llvm/Support/Dwarf.h"
00025 #include "llvm/Support/ErrorHandling.h"
00026 #include "llvm/Target/TargetFrameLowering.h"
00027 #include "llvm/Target/TargetLoweringObjectFile.h"
00028 #include "llvm/Target/TargetMachine.h"
00029 #include "llvm/Target/TargetRegisterInfo.h"
00030 #include "llvm/Target/TargetSubtargetInfo.h"
00031 using namespace llvm;
00032 
00033 #define DEBUG_TYPE "asm-printer"
00034 
00035 //===----------------------------------------------------------------------===//
00036 // Dwarf Emission Helper Routines
00037 //===----------------------------------------------------------------------===//
00038 
00039 /// EmitSLEB128 - emit the specified signed leb128 value.
00040 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
00041   if (isVerbose() && Desc)
00042     OutStreamer.AddComment(Desc);
00043 
00044   OutStreamer.EmitSLEB128IntValue(Value);
00045 }
00046 
00047 /// EmitULEB128 - emit the specified signed leb128 value.
00048 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
00049                              unsigned PadTo) const {
00050   if (isVerbose() && Desc)
00051     OutStreamer.AddComment(Desc);
00052 
00053   OutStreamer.EmitULEB128IntValue(Value, PadTo);
00054 }
00055 
00056 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
00057 void AsmPrinter::EmitCFAByte(unsigned Val) const {
00058   if (isVerbose()) {
00059     if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
00060       OutStreamer.AddComment("DW_CFA_offset + Reg (" +
00061                              Twine(Val - dwarf::DW_CFA_offset) + ")");
00062     else
00063       OutStreamer.AddComment(dwarf::CallFrameString(Val));
00064   }
00065   OutStreamer.EmitIntValue(Val, 1);
00066 }
00067 
00068 static const char *DecodeDWARFEncoding(unsigned Encoding) {
00069   switch (Encoding) {
00070   case dwarf::DW_EH_PE_absptr:
00071     return "absptr";
00072   case dwarf::DW_EH_PE_omit:
00073     return "omit";
00074   case dwarf::DW_EH_PE_pcrel:
00075     return "pcrel";
00076   case dwarf::DW_EH_PE_udata4:
00077     return "udata4";
00078   case dwarf::DW_EH_PE_udata8:
00079     return "udata8";
00080   case dwarf::DW_EH_PE_sdata4:
00081     return "sdata4";
00082   case dwarf::DW_EH_PE_sdata8:
00083     return "sdata8";
00084   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
00085     return "pcrel udata4";
00086   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
00087     return "pcrel sdata4";
00088   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
00089     return "pcrel udata8";
00090   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
00091     return "pcrel sdata8";
00092   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
00093       :
00094     return "indirect pcrel udata4";
00095   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
00096       :
00097     return "indirect pcrel sdata4";
00098   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
00099       :
00100     return "indirect pcrel udata8";
00101   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
00102       :
00103     return "indirect pcrel sdata8";
00104   }
00105 
00106   return "<unknown encoding>";
00107 }
00108 
00109 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
00110 /// encoding.  If verbose assembly output is enabled, we output comments
00111 /// describing the encoding.  Desc is an optional string saying what the
00112 /// encoding is specifying (e.g. "LSDA").
00113 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
00114   if (isVerbose()) {
00115     if (Desc)
00116       OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
00117                              Twine(DecodeDWARFEncoding(Val)));
00118     else
00119       OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
00120   }
00121 
00122   OutStreamer.EmitIntValue(Val, 1);
00123 }
00124 
00125 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
00126 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
00127   if (Encoding == dwarf::DW_EH_PE_omit)
00128     return 0;
00129 
00130   switch (Encoding & 0x07) {
00131   default:
00132     llvm_unreachable("Invalid encoded value.");
00133   case dwarf::DW_EH_PE_absptr:
00134     return TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
00135   case dwarf::DW_EH_PE_udata2:
00136     return 2;
00137   case dwarf::DW_EH_PE_udata4:
00138     return 4;
00139   case dwarf::DW_EH_PE_udata8:
00140     return 8;
00141   }
00142 }
00143 
00144 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
00145                                     unsigned Encoding) const {
00146   if (GV) {
00147     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
00148 
00149     const MCExpr *Exp =
00150         TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
00151     OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
00152   } else
00153     OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
00154 }
00155 
00156 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
00157 /// section.  This can be done with a special directive if the target supports
00158 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
00159 /// of the section.
00160 ///
00161 /// SectionLabel is a temporary label emitted at the start of the section that
00162 /// Label lives in.
00163 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
00164                                    const MCSymbol *SectionLabel) const {
00165   // On COFF targets, we have to emit the special .secrel32 directive.
00166   if (MAI->needsDwarfSectionOffsetDirective()) {
00167     OutStreamer.EmitCOFFSecRel32(Label);
00168     return;
00169   }
00170 
00171   // Get the section that we're referring to, based on SectionLabel.
00172   const MCSection &Section = SectionLabel->getSection();
00173 
00174   // If Label has already been emitted, verify that it is in the same section as
00175   // section label for sanity.
00176   assert((!Label->isInSection() || &Label->getSection() == &Section) &&
00177          "Section offset using wrong section base for label");
00178 
00179   // If the section in question will end up with an address of 0 anyway, we can
00180   // just emit an absolute reference to save a relocation.
00181   if (Section.isBaseAddressKnownZero()) {
00182     OutStreamer.EmitSymbolValue(Label, 4);
00183     return;
00184   }
00185 
00186   // Otherwise, emit it as a label difference from the start of the section.
00187   EmitLabelDifference(Label, SectionLabel, 4);
00188 }
00189 
00190 /// Emit a dwarf register operation.
00191 static void emitDwarfRegOp(ByteStreamer &Streamer, int Reg) {
00192   assert(Reg >= 0);
00193   if (Reg < 32) {
00194     Streamer.EmitInt8(dwarf::DW_OP_reg0 + Reg,
00195                       dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
00196   } else {
00197     Streamer.EmitInt8(dwarf::DW_OP_regx, "DW_OP_regx");
00198     Streamer.EmitULEB128(Reg, Twine(Reg));
00199   }
00200 }
00201 
00202 /// Emit an (double-)indirect dwarf register operation.
00203 static void emitDwarfRegOpIndirect(ByteStreamer &Streamer, int Reg, int Offset,
00204                                    bool Deref) {
00205   assert(Reg >= 0);
00206   if (Reg < 32) {
00207     Streamer.EmitInt8(dwarf::DW_OP_breg0 + Reg,
00208                       dwarf::OperationEncodingString(dwarf::DW_OP_breg0 + Reg));
00209   } else {
00210     Streamer.EmitInt8(dwarf::DW_OP_bregx, "DW_OP_bregx");
00211     Streamer.EmitULEB128(Reg, Twine(Reg));
00212   }
00213   Streamer.EmitSLEB128(Offset);
00214   if (Deref)
00215     Streamer.EmitInt8(dwarf::DW_OP_deref, "DW_OP_deref");
00216 }
00217 
00218 void AsmPrinter::EmitDwarfOpPiece(ByteStreamer &Streamer, unsigned SizeInBits,
00219                                   unsigned OffsetInBits) const {
00220   assert(SizeInBits > 0 && "piece has size zero");
00221   const unsigned SizeOfByte = 8;
00222   if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
00223     Streamer.EmitInt8(dwarf::DW_OP_bit_piece, "DW_OP_bit_piece");
00224     Streamer.EmitULEB128(SizeInBits, Twine(SizeInBits));
00225     Streamer.EmitULEB128(OffsetInBits, Twine(OffsetInBits));
00226   } else {
00227     Streamer.EmitInt8(dwarf::DW_OP_piece, "DW_OP_piece");
00228     unsigned ByteSize = SizeInBits / SizeOfByte;
00229     Streamer.EmitULEB128(ByteSize, Twine(ByteSize));
00230   }
00231 }
00232 
00233 /// Emit a shift-right dwarf expression.
00234 static void emitDwarfOpShr(ByteStreamer &Streamer,
00235                            unsigned ShiftBy) {
00236   Streamer.EmitInt8(dwarf::DW_OP_constu, "DW_OP_constu");
00237   Streamer.EmitULEB128(ShiftBy);
00238   Streamer.EmitInt8(dwarf::DW_OP_shr, "DW_OP_shr");
00239 }
00240 
00241 // Some targets do not provide a DWARF register number for every
00242 // register.  This function attempts to emit a DWARF register by
00243 // emitting a piece of a super-register or by piecing together
00244 // multiple subregisters that alias the register.
00245 void AsmPrinter::EmitDwarfRegOpPiece(ByteStreamer &Streamer,
00246                                      const MachineLocation &MLoc,
00247                                      unsigned PieceSizeInBits,
00248                                      unsigned PieceOffsetInBits) const {
00249   assert(MLoc.isReg() && "MLoc must be a register");
00250   const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
00251   int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
00252 
00253   // If this is a valid register number, emit it.
00254   if (Reg >= 0) {
00255     emitDwarfRegOp(Streamer, Reg);
00256     EmitDwarfOpPiece(Streamer, PieceSizeInBits, PieceOffsetInBits);
00257     return;
00258   }
00259 
00260   // Walk up the super-register chain until we find a valid number.
00261   // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
00262   for (MCSuperRegIterator SR(MLoc.getReg(), TRI); SR.isValid(); ++SR) {
00263     Reg = TRI->getDwarfRegNum(*SR, false);
00264     if (Reg >= 0) {
00265       unsigned Idx = TRI->getSubRegIndex(*SR, MLoc.getReg());
00266       unsigned Size = TRI->getSubRegIdxSize(Idx);
00267       unsigned RegOffset = TRI->getSubRegIdxOffset(Idx);
00268       OutStreamer.AddComment("super-register");
00269       emitDwarfRegOp(Streamer, Reg);
00270       if (PieceOffsetInBits == RegOffset) {
00271         EmitDwarfOpPiece(Streamer, Size, RegOffset);
00272       } else {
00273         // If this is part of a variable in a sub-register at a
00274         // non-zero offset, we need to manually shift the value into
00275         // place, since the DW_OP_piece describes the part of the
00276         // variable, not the position of the subregister.
00277         if (RegOffset)
00278           emitDwarfOpShr(Streamer, RegOffset);
00279         EmitDwarfOpPiece(Streamer, Size, PieceOffsetInBits);
00280       }
00281       return;
00282     }
00283   }
00284 
00285   // Otherwise, attempt to find a covering set of sub-register numbers.
00286   // For example, Q0 on ARM is a composition of D0+D1.
00287   //
00288   // Keep track of the current position so we can emit the more
00289   // efficient DW_OP_piece.
00290   unsigned CurPos = PieceOffsetInBits;
00291   // The size of the register in bits, assuming 8 bits per byte.
00292   unsigned RegSize = TRI->getMinimalPhysRegClass(MLoc.getReg())->getSize() * 8;
00293   // Keep track of the bits in the register we already emitted, so we
00294   // can avoid emitting redundant aliasing subregs.
00295   SmallBitVector Coverage(RegSize, false);
00296   for (MCSubRegIterator SR(MLoc.getReg(), TRI); SR.isValid(); ++SR) {
00297     unsigned Idx = TRI->getSubRegIndex(MLoc.getReg(), *SR);
00298     unsigned Size = TRI->getSubRegIdxSize(Idx);
00299     unsigned Offset = TRI->getSubRegIdxOffset(Idx);
00300     Reg = TRI->getDwarfRegNum(*SR, false);
00301 
00302     // Intersection between the bits we already emitted and the bits
00303     // covered by this subregister.
00304     SmallBitVector Intersection(RegSize, false);
00305     Intersection.set(Offset, Offset + Size);
00306     Intersection ^= Coverage;
00307 
00308     // If this sub-register has a DWARF number and we haven't covered
00309     // its range, emit a DWARF piece for it.
00310     if (Reg >= 0 && Intersection.any()) {
00311       OutStreamer.AddComment("sub-register");
00312       emitDwarfRegOp(Streamer, Reg);
00313       EmitDwarfOpPiece(Streamer, Size, Offset == CurPos ? 0 : Offset);
00314       CurPos = Offset + Size;
00315 
00316       // Mark it as emitted.
00317       Coverage.set(Offset, Offset + Size);
00318     }
00319   }
00320 
00321   if (CurPos == PieceOffsetInBits) {
00322     // FIXME: We have no reasonable way of handling errors in here.
00323     Streamer.EmitInt8(dwarf::DW_OP_nop,
00324                       "nop (could not find a dwarf register number)");
00325   }
00326 }
00327 
00328 /// EmitDwarfRegOp - Emit dwarf register operation.
00329 void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
00330                                 const MachineLocation &MLoc,
00331                                 bool Indirect) const {
00332   const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
00333   int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
00334   if (Reg < 0) {
00335     // We assume that pointers are always in an addressable register.
00336     if (Indirect || MLoc.isIndirect()) {
00337       // FIXME: We have no reasonable way of handling errors in here. The
00338       // caller might be in the middle of a dwarf expression. We should
00339       // probably assert that Reg >= 0 once debug info generation is more
00340       // mature.
00341       Streamer.EmitInt8(dwarf::DW_OP_nop,
00342                         "nop (invalid dwarf register number for indirect loc)");
00343       return;
00344     }
00345 
00346     // Attempt to find a valid super- or sub-register.
00347     return EmitDwarfRegOpPiece(Streamer, MLoc);
00348   }
00349 
00350   if (MLoc.isIndirect())
00351     emitDwarfRegOpIndirect(Streamer, Reg, MLoc.getOffset(), Indirect);
00352   else if (Indirect)
00353     emitDwarfRegOpIndirect(Streamer, Reg, 0, false);
00354   else
00355     emitDwarfRegOp(Streamer, Reg);
00356 }
00357 
00358 //===----------------------------------------------------------------------===//
00359 // Dwarf Lowering Routines
00360 //===----------------------------------------------------------------------===//
00361 
00362 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
00363   switch (Inst.getOperation()) {
00364   default:
00365     llvm_unreachable("Unexpected instruction");
00366   case MCCFIInstruction::OpDefCfaOffset:
00367     OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
00368     break;
00369   case MCCFIInstruction::OpDefCfa:
00370     OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
00371     break;
00372   case MCCFIInstruction::OpDefCfaRegister:
00373     OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
00374     break;
00375   case MCCFIInstruction::OpOffset:
00376     OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
00377     break;
00378   case MCCFIInstruction::OpRegister:
00379     OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
00380     break;
00381   case MCCFIInstruction::OpWindowSave:
00382     OutStreamer.EmitCFIWindowSave();
00383     break;
00384   case MCCFIInstruction::OpSameValue:
00385     OutStreamer.EmitCFISameValue(Inst.getRegister());
00386     break;
00387   }
00388 }