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/MachineModuleInfo.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/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetSubtargetInfo.h"
34 #define DEBUG_TYPE "asm-printer"
36 //===----------------------------------------------------------------------===//
37 // Dwarf Emission Helper Routines
38 //===----------------------------------------------------------------------===//
40 /// EmitSLEB128 - emit the specified signed leb128 value.
41 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
42 if (isVerbose() && Desc)
43 OutStreamer.AddComment(Desc);
45 OutStreamer.EmitSLEB128IntValue(Value);
48 /// EmitULEB128 - emit the specified signed leb128 value.
49 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
50 unsigned PadTo) const {
51 if (isVerbose() && Desc)
52 OutStreamer.AddComment(Desc);
54 OutStreamer.EmitULEB128IntValue(Value, PadTo);
57 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
58 void AsmPrinter::EmitCFAByte(unsigned Val) const {
60 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
61 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
62 Twine(Val - dwarf::DW_CFA_offset) + ")");
64 OutStreamer.AddComment(dwarf::CallFrameString(Val));
66 OutStreamer.EmitIntValue(Val, 1);
69 static const char *DecodeDWARFEncoding(unsigned Encoding) {
71 case dwarf::DW_EH_PE_absptr:
73 case dwarf::DW_EH_PE_omit:
75 case dwarf::DW_EH_PE_pcrel:
77 case dwarf::DW_EH_PE_udata4:
79 case dwarf::DW_EH_PE_udata8:
81 case dwarf::DW_EH_PE_sdata4:
83 case dwarf::DW_EH_PE_sdata8:
85 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
86 return "pcrel udata4";
87 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
88 return "pcrel sdata4";
89 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
90 return "pcrel udata8";
91 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
92 return "pcrel sdata8";
93 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
95 return "indirect pcrel udata4";
96 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
98 return "indirect pcrel sdata4";
99 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
101 return "indirect pcrel udata8";
102 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
104 return "indirect pcrel sdata8";
107 return "<unknown encoding>";
110 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
111 /// encoding. If verbose assembly output is enabled, we output comments
112 /// describing the encoding. Desc is an optional string saying what the
113 /// encoding is specifying (e.g. "LSDA").
114 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
117 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
118 Twine(DecodeDWARFEncoding(Val)));
120 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
123 OutStreamer.EmitIntValue(Val, 1);
126 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
127 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
128 if (Encoding == dwarf::DW_EH_PE_omit)
131 switch (Encoding & 0x07) {
133 llvm_unreachable("Invalid encoded value.");
134 case dwarf::DW_EH_PE_absptr:
135 return TM.getDataLayout()->getPointerSize();
136 case dwarf::DW_EH_PE_udata2:
138 case dwarf::DW_EH_PE_udata4:
140 case dwarf::DW_EH_PE_udata8:
145 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
146 unsigned Encoding) const {
148 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
151 TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
152 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
154 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
157 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
158 /// section. This can be done with a special directive if the target supports
159 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
162 /// SectionLabel is a temporary label emitted at the start of the section that
164 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
165 const MCSymbol *SectionLabel) const {
166 // On COFF targets, we have to emit the special .secrel32 directive.
167 if (MAI->needsDwarfSectionOffsetDirective()) {
168 OutStreamer.EmitCOFFSecRel32(Label);
172 // Get the section that we're referring to, based on SectionLabel.
173 const MCSection &Section = SectionLabel->getSection();
175 // If Label has already been emitted, verify that it is in the same section as
176 // section label for sanity.
177 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
178 "Section offset using wrong section base for label");
180 // If the section in question will end up with an address of 0 anyway, we can
181 // just emit an absolute reference to save a relocation.
182 if (Section.isBaseAddressKnownZero()) {
183 OutStreamer.EmitSymbolValue(Label, 4);
187 // Otherwise, emit it as a label difference from the start of the section.
188 EmitLabelDifference(Label, SectionLabel, 4);
191 /// EmitDwarfRegOp - Emit dwarf register operation.
192 void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
193 const MachineLocation &MLoc) const {
194 DebugLocDwarfExpression Expr(*TM.getSubtargetImpl()->getRegisterInfo(),
195 getDwarfDebug()->getDwarfVersion(), Streamer);
196 const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
197 int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
199 // We assume that pointers are always in an addressable register.
200 if (MLoc.isIndirect())
201 // FIXME: We have no reasonable way of handling errors in here. The
202 // caller might be in the middle of a dwarf expression. We should
203 // probably assert that Reg >= 0 once debug info generation is more
205 return Expr.EmitOp(dwarf::DW_OP_nop,
206 "nop (could not find a dwarf register number)");
208 // Attempt to find a valid super- or sub-register.
209 if (!Expr.AddMachineRegPiece(MLoc.getReg()))
210 Expr.EmitOp(dwarf::DW_OP_nop,
211 "nop (could not find a dwarf register number)");
215 if (MLoc.isIndirect())
216 Expr.AddRegIndirect(Reg, MLoc.getOffset());
221 //===----------------------------------------------------------------------===//
222 // Dwarf Lowering Routines
223 //===----------------------------------------------------------------------===//
225 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
226 switch (Inst.getOperation()) {
228 llvm_unreachable("Unexpected instruction");
229 case MCCFIInstruction::OpDefCfaOffset:
230 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
232 case MCCFIInstruction::OpDefCfa:
233 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
235 case MCCFIInstruction::OpDefCfaRegister:
236 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
238 case MCCFIInstruction::OpOffset:
239 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
241 case MCCFIInstruction::OpRegister:
242 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
244 case MCCFIInstruction::OpWindowSave:
245 OutStreamer.EmitCFIWindowSave();
247 case MCCFIInstruction::OpSameValue:
248 OutStreamer.EmitCFISameValue(Inst.getRegister());