]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
Debug Info: Emitting a register in DwarfExpression may fail. Report the
[opencl/llvm.git] / lib / CodeGen / AsmPrinter / AsmPrinterDwarf.cpp
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 //===----------------------------------------------------------------------===//
14 #include "ByteStreamer.h"
15 #include "DwarfExpression.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MachineLocation.h"
24 #include "llvm/Support/Dwarf.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Target/TargetFrameLowering.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegisterInfo.h"
30 #include "llvm/Target/TargetSubtargetInfo.h"
31 using namespace llvm;
33 #define DEBUG_TYPE "asm-printer"
35 void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
36   BS.EmitInt8(
37       Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
38                   : dwarf::OperationEncodingString(Op));
39 }
41 void DebugLocDwarfExpression::EmitSigned(int Value) {
42   BS.EmitSLEB128(Value, Twine(Value));
43 }
45 void DebugLocDwarfExpression::EmitUnsigned(unsigned Value) {
46   BS.EmitULEB128(Value, Twine(Value));
47 }
49 bool DebugLocDwarfExpression::isFrameRegister(unsigned MachineReg) {
50   // This information is not available while emitting .debug_loc entries.
51   return false;
52 }
54 //===----------------------------------------------------------------------===//
55 // Dwarf Emission Helper Routines
56 //===----------------------------------------------------------------------===//
58 /// EmitSLEB128 - emit the specified signed leb128 value.
59 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
60   if (isVerbose() && Desc)
61     OutStreamer.AddComment(Desc);
63   OutStreamer.EmitSLEB128IntValue(Value);
64 }
66 /// EmitULEB128 - emit the specified signed leb128 value.
67 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
68                              unsigned PadTo) const {
69   if (isVerbose() && Desc)
70     OutStreamer.AddComment(Desc);
72   OutStreamer.EmitULEB128IntValue(Value, PadTo);
73 }
75 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
76 void AsmPrinter::EmitCFAByte(unsigned Val) const {
77   if (isVerbose()) {
78     if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
79       OutStreamer.AddComment("DW_CFA_offset + Reg (" +
80                              Twine(Val - dwarf::DW_CFA_offset) + ")");
81     else
82       OutStreamer.AddComment(dwarf::CallFrameString(Val));
83   }
84   OutStreamer.EmitIntValue(Val, 1);
85 }
87 static const char *DecodeDWARFEncoding(unsigned Encoding) {
88   switch (Encoding) {
89   case dwarf::DW_EH_PE_absptr:
90     return "absptr";
91   case dwarf::DW_EH_PE_omit:
92     return "omit";
93   case dwarf::DW_EH_PE_pcrel:
94     return "pcrel";
95   case dwarf::DW_EH_PE_udata4:
96     return "udata4";
97   case dwarf::DW_EH_PE_udata8:
98     return "udata8";
99   case dwarf::DW_EH_PE_sdata4:
100     return "sdata4";
101   case dwarf::DW_EH_PE_sdata8:
102     return "sdata8";
103   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
104     return "pcrel udata4";
105   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
106     return "pcrel sdata4";
107   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
108     return "pcrel udata8";
109   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
110     return "pcrel sdata8";
111   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
112       :
113     return "indirect pcrel udata4";
114   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
115       :
116     return "indirect pcrel sdata4";
117   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
118       :
119     return "indirect pcrel udata8";
120   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
121       :
122     return "indirect pcrel sdata8";
123   }
125   return "<unknown encoding>";
128 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
129 /// encoding.  If verbose assembly output is enabled, we output comments
130 /// describing the encoding.  Desc is an optional string saying what the
131 /// encoding is specifying (e.g. "LSDA").
132 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
133   if (isVerbose()) {
134     if (Desc)
135       OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
136                              Twine(DecodeDWARFEncoding(Val)));
137     else
138       OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
139   }
141   OutStreamer.EmitIntValue(Val, 1);
144 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
145 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
146   if (Encoding == dwarf::DW_EH_PE_omit)
147     return 0;
149   switch (Encoding & 0x07) {
150   default:
151     llvm_unreachable("Invalid encoded value.");
152   case dwarf::DW_EH_PE_absptr:
153     return TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
154   case dwarf::DW_EH_PE_udata2:
155     return 2;
156   case dwarf::DW_EH_PE_udata4:
157     return 4;
158   case dwarf::DW_EH_PE_udata8:
159     return 8;
160   }
163 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
164                                     unsigned Encoding) const {
165   if (GV) {
166     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
168     const MCExpr *Exp =
169         TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
170     OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
171   } else
172     OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
175 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
176 /// section.  This can be done with a special directive if the target supports
177 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
178 /// of the section.
179 ///
180 /// SectionLabel is a temporary label emitted at the start of the section that
181 /// Label lives in.
182 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
183                                    const MCSymbol *SectionLabel) const {
184   // On COFF targets, we have to emit the special .secrel32 directive.
185   if (MAI->needsDwarfSectionOffsetDirective()) {
186     OutStreamer.EmitCOFFSecRel32(Label);
187     return;
188   }
190   // Get the section that we're referring to, based on SectionLabel.
191   const MCSection &Section = SectionLabel->getSection();
193   // If Label has already been emitted, verify that it is in the same section as
194   // section label for sanity.
195   assert((!Label->isInSection() || &Label->getSection() == &Section) &&
196          "Section offset using wrong section base for label");
198   // If the section in question will end up with an address of 0 anyway, we can
199   // just emit an absolute reference to save a relocation.
200   if (Section.isBaseAddressKnownZero()) {
201     OutStreamer.EmitSymbolValue(Label, 4);
202     return;
203   }
205   // Otherwise, emit it as a label difference from the start of the section.
206   EmitLabelDifference(Label, SectionLabel, 4);
209 // Some targets do not provide a DWARF register number for every
210 // register.  This function attempts to emit a DWARF register by
211 // emitting a piece of a super-register or by piecing together
212 // multiple subregisters that alias the register.
213 void AsmPrinter::EmitDwarfRegOpPiece(ByteStreamer &Streamer,
214                                      const MachineLocation &MLoc,
215                                      unsigned PieceSizeInBits,
216                                      unsigned PieceOffsetInBits) const {
217   assert(MLoc.isReg() && "MLoc must be a register");
218   DebugLocDwarfExpression Expr(*this, Streamer);
219   Expr.AddMachineRegPiece(MLoc.getReg(), PieceSizeInBits, PieceOffsetInBits);
222 void AsmPrinter::EmitDwarfOpPiece(ByteStreamer &Streamer,
223                                   unsigned PieceSizeInBits,
224                                   unsigned PieceOffsetInBits) const {
225   DebugLocDwarfExpression Expr(*this, Streamer);
226   Expr.AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
229 /// EmitDwarfRegOp - Emit dwarf register operation.
230 void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
231                                 const MachineLocation &MLoc,
232                                 bool Indirect) const {
233   DebugLocDwarfExpression Expr(*this, Streamer);
234   const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
235   int Reg = TRI->getDwarfRegNum(MLoc.getReg(), false);
236   if (Reg < 0) {
237     // We assume that pointers are always in an addressable register.
238     if (Indirect || MLoc.isIndirect())
239       // FIXME: We have no reasonable way of handling errors in here. The
240       // caller might be in the middle of a dwarf expression. We should
241       // probably assert that Reg >= 0 once debug info generation is more
242       // mature.
243       return Expr.EmitOp(dwarf::DW_OP_nop,
244                          "nop (could not find a dwarf register number)");
246     // Attempt to find a valid super- or sub-register.
247     if (!Expr.AddMachineRegPiece(MLoc.getReg()))
248       Expr.EmitOp(dwarf::DW_OP_nop,
249                   "nop (could not find a dwarf register number)");
250     return;
251   }
253   if (MLoc.isIndirect())
254     Expr.AddRegIndirect(Reg, MLoc.getOffset(), Indirect);
255   else if (Indirect)
256     Expr.AddRegIndirect(Reg, 0, false);
257   else
258     Expr.AddReg(Reg);
261 //===----------------------------------------------------------------------===//
262 // Dwarf Lowering Routines
263 //===----------------------------------------------------------------------===//
265 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
266   switch (Inst.getOperation()) {
267   default:
268     llvm_unreachable("Unexpected instruction");
269   case MCCFIInstruction::OpDefCfaOffset:
270     OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
271     break;
272   case MCCFIInstruction::OpDefCfa:
273     OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
274     break;
275   case MCCFIInstruction::OpDefCfaRegister:
276     OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
277     break;
278   case MCCFIInstruction::OpOffset:
279     OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
280     break;
281   case MCCFIInstruction::OpRegister:
282     OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
283     break;
284   case MCCFIInstruction::OpWindowSave:
285     OutStreamer.EmitCFIWindowSave();
286     break;
287   case MCCFIInstruction::OpSameValue:
288     OutStreamer.EmitCFISameValue(Inst.getRegister());
289     break;
290   }