Refactor DebugLocDWARFExpression so it doesn't require access to the
[oota-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 //===----------------------------------------------------------------------===//
13
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"
32 using namespace llvm;
33
34 #define DEBUG_TYPE "asm-printer"
35
36 //===----------------------------------------------------------------------===//
37 // Dwarf Emission Helper Routines
38 //===----------------------------------------------------------------------===//
39
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);
44
45   OutStreamer.EmitSLEB128IntValue(Value);
46 }
47
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);
53
54   OutStreamer.EmitULEB128IntValue(Value, PadTo);
55 }
56
57 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
58 void AsmPrinter::EmitCFAByte(unsigned Val) const {
59   if (isVerbose()) {
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) + ")");
63     else
64       OutStreamer.AddComment(dwarf::CallFrameString(Val));
65   }
66   OutStreamer.EmitIntValue(Val, 1);
67 }
68
69 static const char *DecodeDWARFEncoding(unsigned Encoding) {
70   switch (Encoding) {
71   case dwarf::DW_EH_PE_absptr:
72     return "absptr";
73   case dwarf::DW_EH_PE_omit:
74     return "omit";
75   case dwarf::DW_EH_PE_pcrel:
76     return "pcrel";
77   case dwarf::DW_EH_PE_udata4:
78     return "udata4";
79   case dwarf::DW_EH_PE_udata8:
80     return "udata8";
81   case dwarf::DW_EH_PE_sdata4:
82     return "sdata4";
83   case dwarf::DW_EH_PE_sdata8:
84     return "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
94       :
95     return "indirect pcrel udata4";
96   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
97       :
98     return "indirect pcrel sdata4";
99   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
100       :
101     return "indirect pcrel udata8";
102   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
103       :
104     return "indirect pcrel sdata8";
105   }
106
107   return "<unknown encoding>";
108 }
109
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 {
115   if (isVerbose()) {
116     if (Desc)
117       OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
118                              Twine(DecodeDWARFEncoding(Val)));
119     else
120       OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
121   }
122
123   OutStreamer.EmitIntValue(Val, 1);
124 }
125
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)
129     return 0;
130
131   switch (Encoding & 0x07) {
132   default:
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:
137     return 2;
138   case dwarf::DW_EH_PE_udata4:
139     return 4;
140   case dwarf::DW_EH_PE_udata8:
141     return 8;
142   }
143 }
144
145 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
146                                     unsigned Encoding) const {
147   if (GV) {
148     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
149
150     const MCExpr *Exp =
151         TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
152     OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
153   } else
154     OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
155 }
156
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
160 /// of the section.
161 ///
162 /// SectionLabel is a temporary label emitted at the start of the section that
163 /// Label lives in.
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);
169     return;
170   }
171
172   // Get the section that we're referring to, based on SectionLabel.
173   const MCSection &Section = SectionLabel->getSection();
174
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");
179
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);
184     return;
185   }
186
187   // Otherwise, emit it as a label difference from the start of the section.
188   EmitLabelDifference(Label, SectionLabel, 4);
189 }
190
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);
198   if (Reg < 0) {
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
204       // mature.
205       return Expr.EmitOp(dwarf::DW_OP_nop,
206                          "nop (could not find a dwarf register number)");
207
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)");
212     return;
213   }
214
215   if (MLoc.isIndirect())
216     Expr.AddRegIndirect(Reg, MLoc.getOffset());
217   else
218     Expr.AddReg(Reg);
219 }
220
221 //===----------------------------------------------------------------------===//
222 // Dwarf Lowering Routines
223 //===----------------------------------------------------------------------===//
224
225 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
226   switch (Inst.getOperation()) {
227   default:
228     llvm_unreachable("Unexpected instruction");
229   case MCCFIInstruction::OpDefCfaOffset:
230     OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
231     break;
232   case MCCFIInstruction::OpDefCfa:
233     OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
234     break;
235   case MCCFIInstruction::OpDefCfaRegister:
236     OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
237     break;
238   case MCCFIInstruction::OpOffset:
239     OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
240     break;
241   case MCCFIInstruction::OpRegister:
242     OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
243     break;
244   case MCCFIInstruction::OpWindowSave:
245     OutStreamer.EmitCFIWindowSave();
246     break;
247   case MCCFIInstruction::OpSameValue:
248     OutStreamer.EmitCFISameValue(Inst.getRegister());
249     break;
250   }
251 }