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