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 #include "ByteStreamer.h"
15 #include "DwarfDebug.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MachineLocation.h"
28 #include "llvm/Support/Dwarf.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetSubtargetInfo.h"
35 #define DEBUG_TYPE "asm-printer"
37 //===----------------------------------------------------------------------===//
38 // Dwarf Emission Helper Routines
39 //===----------------------------------------------------------------------===//
41 /// EmitSLEB128 - emit the specified signed leb128 value.
42 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
43 if (isVerbose() && Desc)
44 OutStreamer.AddComment(Desc);
46 OutStreamer.EmitSLEB128IntValue(Value);
49 /// EmitULEB128 - emit the specified signed leb128 value.
50 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
51 unsigned PadTo) const {
52 if (isVerbose() && Desc)
53 OutStreamer.AddComment(Desc);
55 OutStreamer.EmitULEB128IntValue(Value, PadTo);
58 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
59 void AsmPrinter::EmitCFAByte(unsigned Val) const {
61 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
62 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
63 Twine(Val - dwarf::DW_CFA_offset) + ")");
65 OutStreamer.AddComment(dwarf::CallFrameString(Val));
67 OutStreamer.EmitIntValue(Val, 1);
70 static const char *DecodeDWARFEncoding(unsigned Encoding) {
72 case dwarf::DW_EH_PE_absptr:
74 case dwarf::DW_EH_PE_omit:
76 case dwarf::DW_EH_PE_pcrel:
78 case dwarf::DW_EH_PE_udata4:
80 case dwarf::DW_EH_PE_udata8:
82 case dwarf::DW_EH_PE_sdata4:
84 case dwarf::DW_EH_PE_sdata8:
86 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
87 return "pcrel udata4";
88 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
89 return "pcrel sdata4";
90 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
91 return "pcrel udata8";
92 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
93 return "pcrel sdata8";
94 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
96 return "indirect pcrel udata4";
97 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
99 return "indirect pcrel sdata4";
100 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
102 return "indirect pcrel udata8";
103 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
105 return "indirect pcrel sdata8";
108 return "<unknown encoding>";
111 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
112 /// encoding. If verbose assembly output is enabled, we output comments
113 /// describing the encoding. Desc is an optional string saying what the
114 /// encoding is specifying (e.g. "LSDA").
115 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
118 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
119 Twine(DecodeDWARFEncoding(Val)));
121 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
124 OutStreamer.EmitIntValue(Val, 1);
127 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
128 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
129 if (Encoding == dwarf::DW_EH_PE_omit)
132 switch (Encoding & 0x07) {
134 llvm_unreachable("Invalid encoded value.");
135 case dwarf::DW_EH_PE_absptr:
136 return TM.getDataLayout()->getPointerSize();
137 case dwarf::DW_EH_PE_udata2:
139 case dwarf::DW_EH_PE_udata4:
141 case dwarf::DW_EH_PE_udata8:
146 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
147 unsigned Encoding) const {
149 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
152 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
153 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
155 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
158 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
159 /// section. This can be done with a special directive if the target supports
160 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
163 /// SectionLabel is a temporary label emitted at the start of the section that
165 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
166 const MCSymbol *SectionLabel) const {
167 // On COFF targets, we have to emit the special .secrel32 directive.
168 if (MAI->needsDwarfSectionOffsetDirective()) {
169 OutStreamer.EmitCOFFSecRel32(Label);
173 // Get the section that we're referring to, based on SectionLabel.
174 const MCSection &Section = SectionLabel->getSection();
176 // If Label has already been emitted, verify that it is in the same section as
177 // section label for sanity.
178 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
179 "Section offset using wrong section base for label");
181 // If the section in question will end up with an address of 0 anyway, we can
182 // just emit an absolute reference to save a relocation.
183 if (Section.isBaseAddressKnownZero()) {
184 OutStreamer.EmitSymbolValue(Label, 4);
188 // Otherwise, emit it as a label difference from the start of the section.
189 EmitLabelDifference(Label, SectionLabel, 4);
192 /// EmitDwarfRegOp - Emit dwarf register operation.
193 void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
194 const MachineLocation &MLoc) const {
195 DebugLocDwarfExpression Expr(*MF->getSubtarget().getRegisterInfo(),
196 getDwarfDebug()->getDwarfVersion(), Streamer);
197 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
198 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
200 // We assume that pointers are always in an addressable register.
201 if (MLoc.isIndirect())
202 // FIXME: We have no reasonable way of handling errors in here. The
203 // caller might be in the middle of a dwarf expression. We should
204 // probably assert that Reg >= 0 once debug info generation is more
206 return Expr.EmitOp(dwarf::DW_OP_nop,
207 "nop (could not find a dwarf register number)");
209 // Attempt to find a valid super- or sub-register.
210 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
211 Expr.EmitOp(dwarf::DW_OP_nop,
212 "nop (could not find a dwarf register number)");
216 if (MLoc.isIndirect())
217 Expr.AddRegIndirect(Reg, MLoc.getOffset());
222 //===----------------------------------------------------------------------===//
223 // Dwarf Lowering Routines
224 //===----------------------------------------------------------------------===//
226 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
227 switch (Inst.getOperation()) {
229 llvm_unreachable("Unexpected instruction");
230 case MCCFIInstruction::OpDefCfaOffset:
231 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
233 case MCCFIInstruction::OpDefCfa:
234 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
236 case MCCFIInstruction::OpDefCfaRegister:
237 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
239 case MCCFIInstruction::OpOffset:
240 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
242 case MCCFIInstruction::OpRegister:
243 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
245 case MCCFIInstruction::OpWindowSave:
246 OutStreamer.EmitCFIWindowSave();
248 case MCCFIInstruction::OpSameValue:
249 OutStreamer.EmitCFISameValue(Inst.getRegister());