1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Dwarf emissions parts of AsmPrinter.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "asm-printer"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/MC/MachineLocation.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetFrameLowering.h"
23 #include "llvm/Target/TargetLoweringObjectFile.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Support/Dwarf.h"
30 //===----------------------------------------------------------------------===//
31 // Dwarf Emission Helper Routines
32 //===----------------------------------------------------------------------===//
34 /// EmitSLEB128 - emit the specified signed leb128 value.
35 void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const {
36 if (isVerbose() && Desc)
37 OutStreamer.AddComment(Desc);
39 OutStreamer.EmitSLEB128IntValue(Value);
42 /// EmitULEB128 - emit the specified signed leb128 value.
43 void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
44 unsigned PadTo) const {
45 if (isVerbose() && Desc)
46 OutStreamer.AddComment(Desc);
48 OutStreamer.EmitULEB128IntValue(Value, 0/*addrspace*/, PadTo);
51 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
52 void AsmPrinter::EmitCFAByte(unsigned Val) const {
54 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
55 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
56 Twine(Val-dwarf::DW_CFA_offset) + ")");
58 OutStreamer.AddComment(dwarf::CallFrameString(Val));
60 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
63 static const char *DecodeDWARFEncoding(unsigned Encoding) {
65 case dwarf::DW_EH_PE_absptr: return "absptr";
66 case dwarf::DW_EH_PE_omit: return "omit";
67 case dwarf::DW_EH_PE_pcrel: return "pcrel";
68 case dwarf::DW_EH_PE_udata4: return "udata4";
69 case dwarf::DW_EH_PE_udata8: return "udata8";
70 case dwarf::DW_EH_PE_sdata4: return "sdata4";
71 case dwarf::DW_EH_PE_sdata8: return "sdata8";
72 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
73 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
74 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
75 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
76 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
77 return "indirect pcrel udata4";
78 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
79 return "indirect pcrel sdata4";
80 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
81 return "indirect pcrel udata8";
82 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
83 return "indirect pcrel sdata8";
86 return "<unknown encoding>";
90 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
91 /// encoding. If verbose assembly output is enabled, we output comments
92 /// describing the encoding. Desc is an optional string saying what the
93 /// encoding is specifying (e.g. "LSDA").
94 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
97 OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
98 Twine(DecodeDWARFEncoding(Val)));
100 OutStreamer.AddComment(Twine("Encoding = ") +
101 DecodeDWARFEncoding(Val));
104 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
107 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
108 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
109 if (Encoding == dwarf::DW_EH_PE_omit)
112 switch (Encoding & 0x07) {
113 default: assert(0 && "Invalid encoded value.");
114 case dwarf::DW_EH_PE_absptr: return TM.getTargetData()->getPointerSize();
115 case dwarf::DW_EH_PE_udata2: return 2;
116 case dwarf::DW_EH_PE_udata4: return 4;
117 case dwarf::DW_EH_PE_udata8: return 8;
121 void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
122 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
125 TLOF.getExprForDwarfReference(Sym, Encoding, OutStreamer);
126 OutStreamer.EmitAbsValue(Exp, GetSizeOfEncodedValue(Encoding));
129 void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
130 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
133 TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
134 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
137 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
138 /// section. This can be done with a special directive if the target supports
139 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
142 /// SectionLabel is a temporary label emitted at the start of the section that
144 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
145 const MCSymbol *SectionLabel) const {
146 // On COFF targets, we have to emit the special .secrel32 directive.
147 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
149 OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
153 // Get the section that we're referring to, based on SectionLabel.
154 const MCSection &Section = SectionLabel->getSection();
156 // If Label has already been emitted, verify that it is in the same section as
157 // section label for sanity.
158 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
159 "Section offset using wrong section base for label");
161 // If the section in question will end up with an address of 0 anyway, we can
162 // just emit an absolute reference to save a relocation.
163 if (Section.isBaseAddressKnownZero()) {
164 OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
168 // Otherwise, emit it as a label difference from the start of the section.
169 EmitLabelDifference(Label, SectionLabel, 4);
172 //===----------------------------------------------------------------------===//
173 // Dwarf Lowering Routines
174 //===----------------------------------------------------------------------===//
176 /// EmitCFIFrameMove - Emit a frame instruction.
177 void AsmPrinter::EmitCFIFrameMove(const MachineMove &Move) const {
178 const TargetRegisterInfo *RI = TM.getRegisterInfo();
180 const MachineLocation &Dst = Move.getDestination();
181 const MachineLocation &Src = Move.getSource();
184 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
185 if (Src.getReg() == MachineLocation::VirtualFP) {
186 OutStreamer.EmitCFIDefCfaOffset(-Src.getOffset());
189 OutStreamer.EmitCFIDefCfa(RI->getDwarfRegNum(Src.getReg(), true),
192 } else if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
193 assert(Dst.isReg() && "Machine move not supported yet.");
194 OutStreamer.EmitCFIDefCfaRegister(RI->getDwarfRegNum(Dst.getReg(), true));
196 assert(!Dst.isReg() && "Machine move not supported yet.");
197 OutStreamer.EmitCFIOffset(RI->getDwarfRegNum(Src.getReg(), true),