Revert "AsmPrinter: Change DIEValue to be stored by value"
[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/DIE.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MachineLocation.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
34 using namespace llvm;
35
36 #define DEBUG_TYPE "asm-printer"
37
38 //===----------------------------------------------------------------------===//
39 // Dwarf Emission Helper Routines
40 //===----------------------------------------------------------------------===//
41
42 /// EmitSLEB128 - emit the specified signed leb128 value.
43 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
44   if (isVerbose() && Desc)
45     OutStreamer->AddComment(Desc);
46
47   OutStreamer->EmitSLEB128IntValue(Value);
48 }
49
50 /// EmitULEB128 - emit the specified signed leb128 value.
51 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
52                              unsigned PadTo) const {
53   if (isVerbose() && Desc)
54     OutStreamer->AddComment(Desc);
55
56   OutStreamer->EmitULEB128IntValue(Value, PadTo);
57 }
58
59 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
60 void AsmPrinter::EmitCFAByte(unsigned Val) const {
61   if (isVerbose()) {
62     if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
63       OutStreamer->AddComment("DW_CFA_offset + Reg (" +
64                               Twine(Val - dwarf::DW_CFA_offset) + ")");
65     else
66       OutStreamer->AddComment(dwarf::CallFrameString(Val));
67   }
68   OutStreamer->EmitIntValue(Val, 1);
69 }
70
71 static const char *DecodeDWARFEncoding(unsigned Encoding) {
72   switch (Encoding) {
73   case dwarf::DW_EH_PE_absptr:
74     return "absptr";
75   case dwarf::DW_EH_PE_omit:
76     return "omit";
77   case dwarf::DW_EH_PE_pcrel:
78     return "pcrel";
79   case dwarf::DW_EH_PE_udata4:
80     return "udata4";
81   case dwarf::DW_EH_PE_udata8:
82     return "udata8";
83   case dwarf::DW_EH_PE_sdata4:
84     return "sdata4";
85   case dwarf::DW_EH_PE_sdata8:
86     return "sdata8";
87   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
88     return "pcrel udata4";
89   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
90     return "pcrel sdata4";
91   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
92     return "pcrel udata8";
93   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
94     return "pcrel sdata8";
95   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
96       :
97     return "indirect pcrel udata4";
98   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
99       :
100     return "indirect pcrel sdata4";
101   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
102       :
103     return "indirect pcrel udata8";
104   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
105       :
106     return "indirect pcrel sdata8";
107   }
108
109   return "<unknown encoding>";
110 }
111
112 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
113 /// encoding.  If verbose assembly output is enabled, we output comments
114 /// describing the encoding.  Desc is an optional string saying what the
115 /// encoding is specifying (e.g. "LSDA").
116 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
117   if (isVerbose()) {
118     if (Desc)
119       OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
120                               Twine(DecodeDWARFEncoding(Val)));
121     else
122       OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
123   }
124
125   OutStreamer->EmitIntValue(Val, 1);
126 }
127
128 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
129 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
130   if (Encoding == dwarf::DW_EH_PE_omit)
131     return 0;
132
133   switch (Encoding & 0x07) {
134   default:
135     llvm_unreachable("Invalid encoded value.");
136   case dwarf::DW_EH_PE_absptr:
137     return TM.getDataLayout()->getPointerSize();
138   case dwarf::DW_EH_PE_udata2:
139     return 2;
140   case dwarf::DW_EH_PE_udata4:
141     return 4;
142   case dwarf::DW_EH_PE_udata8:
143     return 8;
144   }
145 }
146
147 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
148                                     unsigned Encoding) const {
149   if (GV) {
150     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
151
152     const MCExpr *Exp =
153         TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI,
154                                      *OutStreamer);
155     OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
156   } else
157     OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
158 }
159
160 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
161 /// section.  This can be done with a special directive if the target supports
162 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
163 /// of the section.
164 ///
165 /// SectionLabel is a temporary label emitted at the start of the section that
166 /// Label lives in.
167 void AsmPrinter::emitSectionOffset(const MCSymbol *Label) const {
168   // On COFF targets, we have to emit the special .secrel32 directive.
169   if (MAI->needsDwarfSectionOffsetDirective()) {
170     OutStreamer->EmitCOFFSecRel32(Label);
171     return;
172   }
173
174   // If the format uses relocations with dwarf, refer to the symbol directly.
175   if (MAI->doesDwarfUseRelocationsAcrossSections()) {
176     OutStreamer->EmitSymbolValue(Label, 4);
177     return;
178   }
179
180   // Otherwise, emit it as a label difference from the start of the section.
181   EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
182 }
183
184 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
185   if (MAI->doesDwarfUseRelocationsAcrossSections()) {
186     emitSectionOffset(S.getSymbol());
187     return;
188   }
189
190   // Just emit the offset directly; no need for symbol math.
191   EmitInt32(S.getOffset());
192 }
193
194 /// EmitDwarfRegOp - Emit dwarf register operation.
195 void AsmPrinter::EmitDwarfRegOp(ByteStreamer &Streamer,
196                                 const MachineLocation &MLoc) const {
197   DebugLocDwarfExpression Expr(*MF->getSubtarget().getRegisterInfo(),
198                                getDwarfDebug()->getDwarfVersion(), Streamer);
199   const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
200   int Reg = MRI->getDwarfRegNum(MLoc.getReg(), false);
201   if (Reg < 0) {
202     // We assume that pointers are always in an addressable register.
203     if (MLoc.isIndirect())
204       // FIXME: We have no reasonable way of handling errors in here. The
205       // caller might be in the middle of a dwarf expression. We should
206       // probably assert that Reg >= 0 once debug info generation is more
207       // mature.
208       return Expr.EmitOp(dwarf::DW_OP_nop,
209                          "nop (could not find a dwarf register number)");
210
211     // Attempt to find a valid super- or sub-register.
212     if (!Expr.AddMachineRegPiece(MLoc.getReg()))
213       Expr.EmitOp(dwarf::DW_OP_nop,
214                   "nop (could not find a dwarf register number)");
215     return;
216   }
217
218   if (MLoc.isIndirect())
219     Expr.AddRegIndirect(Reg, MLoc.getOffset());
220   else
221     Expr.AddReg(Reg);
222 }
223
224 //===----------------------------------------------------------------------===//
225 // Dwarf Lowering Routines
226 //===----------------------------------------------------------------------===//
227
228 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
229   switch (Inst.getOperation()) {
230   default:
231     llvm_unreachable("Unexpected instruction");
232   case MCCFIInstruction::OpDefCfaOffset:
233     OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
234     break;
235   case MCCFIInstruction::OpDefCfa:
236     OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
237     break;
238   case MCCFIInstruction::OpDefCfaRegister:
239     OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
240     break;
241   case MCCFIInstruction::OpOffset:
242     OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
243     break;
244   case MCCFIInstruction::OpRegister:
245     OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
246     break;
247   case MCCFIInstruction::OpWindowSave:
248     OutStreamer->EmitCFIWindowSave();
249     break;
250   case MCCFIInstruction::OpSameValue:
251     OutStreamer->EmitCFISameValue(Inst.getRegister());
252     break;
253   }
254 }
255
256 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
257   // Get the abbreviation for this DIE.
258   const DIEAbbrev &Abbrev = Die.getAbbrev();
259
260   // Emit the code (index) for the abbreviation.
261   if (isVerbose())
262     OutStreamer->AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
263                             "] 0x" + Twine::utohexstr(Die.getOffset()) +
264                             ":0x" + Twine::utohexstr(Die.getSize()) + " " +
265                             dwarf::TagString(Abbrev.getTag()));
266   EmitULEB128(Abbrev.getNumber());
267
268   const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
269   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
270
271   // Emit the DIE attribute values.
272   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
273     dwarf::Attribute Attr = AbbrevData[i].getAttribute();
274     dwarf::Form Form = AbbrevData[i].getForm();
275     assert(Form && "Too many attributes for DIE (check abbreviation)");
276
277     if (isVerbose()) {
278       OutStreamer->AddComment(dwarf::AttributeString(Attr));
279       if (Attr == dwarf::DW_AT_accessibility)
280         OutStreamer->AddComment(dwarf::AccessibilityString(
281             cast<DIEInteger>(Values[i])->getValue()));
282     }
283
284     // Emit an attribute using the defined form.
285     Values[i]->EmitValue(this, Form);
286   }
287
288   // Emit the DIE children if any.
289   if (Abbrev.hasChildren()) {
290     for (auto &Child : Die.getChildren())
291       emitDwarfDIE(*Child);
292
293     OutStreamer->AddComment("End Of Children Mark");
294     EmitInt8(0);
295   }
296 }
297
298 void
299 AsmPrinter::emitDwarfAbbrevs(const std::vector<DIEAbbrev *>& Abbrevs) const {
300   // For each abbrevation.
301   for (const DIEAbbrev *Abbrev : Abbrevs) {
302     // Emit the abbrevations code (base 1 index.)
303     EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
304
305     // Emit the abbreviations data.
306     Abbrev->Emit(this);
307   }
308
309   // Mark end of abbreviations.
310   EmitULEB128(0, "EOM(3)");
311 }