d69b53051ede4cdfab2d827700130b9dc9ad978c
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfPrinter.cpp
1 //===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===//
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 // Emit general DWARF directives.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DwarfPrinter.h"
15 #include "llvm/Module.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetFrameInfo.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/ADT/SmallString.h"
32 using namespace llvm;
33
34 DwarfPrinter::DwarfPrinter(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
35 : O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
36   RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
37   SubprogramCount(0) {}
38
39
40 /// getDWLabel - Return the MCSymbol corresponding to the assembler temporary
41 /// label with the specified stem and unique ID.
42 MCSymbol *DwarfPrinter::getDWLabel(const char *Name, unsigned ID) const {
43   // FIXME: REMOVE this.  However, there is stuff in EH that passes counters in
44   // here that can be zero.
45   
46   //assert(ID && "Should use getTempLabel if no ID");
47   if (ID == 0) return getTempLabel(Name);
48   return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
49                                            + Twine(Name) + Twine(ID));
50 }
51
52 /// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
53 /// label with the specified name.
54 MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
55   return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
56                                            + Name);
57 }
58
59
60 /// SizeOfEncodedValue - Return the size of the encoding in bytes.
61 unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
62   if (Encoding == dwarf::DW_EH_PE_omit)
63     return 0;
64
65   switch (Encoding & 0x07) {
66   case dwarf::DW_EH_PE_absptr:
67     return TD->getPointerSize();
68   case dwarf::DW_EH_PE_udata2:
69     return 2;
70   case dwarf::DW_EH_PE_udata4:
71     return 4;
72   case dwarf::DW_EH_PE_udata8:
73     return 8;
74   }
75
76   assert(0 && "Invalid encoded value.");
77   return 0;
78 }
79
80 static const char *DecodeDWARFEncoding(unsigned Encoding) {
81   switch (Encoding) {
82   case dwarf::DW_EH_PE_absptr: return "absptr";
83   case dwarf::DW_EH_PE_omit:   return "omit";
84   case dwarf::DW_EH_PE_pcrel:  return "pcrel";
85   case dwarf::DW_EH_PE_udata4: return "udata4";
86   case dwarf::DW_EH_PE_udata8: return "udata8";
87   case dwarf::DW_EH_PE_sdata4: return "sdata4";
88   case dwarf::DW_EH_PE_sdata8: return "sdata8";
89   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
90   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
91   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
92   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
93   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
94     return "indirect pcrel udata4";
95   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
96     return "indirect pcrel sdata4";
97   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
98     return "indirect pcrel udata8";
99   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
100     return "indirect pcrel sdata8";
101   }
102   
103   return "<unknown encoding>";
104 }
105
106 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
107 /// encoding.  If verbose assembly output is enabled, we output comments
108 /// describing the encoding.  Desc is an optional string saying what the
109 /// encoding is specifying (e.g. "LSDA").
110 void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
111   if (Asm->VerboseAsm) {
112     if (Desc != 0)
113       Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
114                                   Twine(DecodeDWARFEncoding(Val)));
115     else
116       Asm->OutStreamer.AddComment(Twine("Encoding = ") +
117                                   DecodeDWARFEncoding(Val));
118   }
119
120   Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
121 }
122
123 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
124 void DwarfPrinter::EmitCFAByte(unsigned Val) {
125   if (Asm->VerboseAsm) {
126     if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
127       Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" + 
128                                   Twine(Val-dwarf::DW_CFA_offset) + ")");
129     else
130       Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
131   }
132   Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
133 }
134
135 /// EmitSLEB128 - emit the specified signed leb128 value.
136 void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
137   if (Asm->VerboseAsm && Desc)
138     Asm->OutStreamer.AddComment(Desc);
139     
140   if (MAI->hasLEB128()) {
141     O << "\t.sleb128\t" << Value;
142     Asm->OutStreamer.AddBlankLine();
143     return;
144   }
145
146   // If we don't have .sleb128, emit as .bytes.
147   int Sign = Value >> (8 * sizeof(Value) - 1);
148   bool IsMore;
149   
150   do {
151     unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
152     Value >>= 7;
153     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
154     if (IsMore) Byte |= 0x80;
155     Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
156   } while (IsMore);
157 }
158
159 /// EmitULEB128 - emit the specified signed leb128 value.
160 void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
161                                unsigned PadTo) const {
162   if (Asm->VerboseAsm && Desc)
163     Asm->OutStreamer.AddComment(Desc);
164  
165   if (MAI->hasLEB128() && PadTo == 0) {
166     O << "\t.uleb128\t" << Value;
167     Asm->OutStreamer.AddBlankLine();
168     return;
169   }
170   
171   // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
172   do {
173     unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
174     Value >>= 7;
175     if (Value || PadTo != 0) Byte |= 0x80;
176     Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
177   } while (Value);
178
179   if (PadTo) {
180     if (PadTo > 1)
181       Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
182     Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
183   }
184 }
185
186
187 /// EmitReference - Emit a reference to a label.
188 ///
189 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
190                                  bool Force32Bit) const {
191   unsigned Size = Force32Bit ? 4 : TD->getPointerSize();
192   
193   if (!IsPCRelative) {
194     Asm->OutStreamer.EmitSymbolValue(Sym, Size, 0/*AddrSpace*/);
195     return;
196   }
197   
198   // FIXME: Need an MCExpr for ".".
199   if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
200     O << MAI->getData32bitsDirective();
201   else
202     O << MAI->getData64bitsDirective();
203   O << *Sym;
204   if (IsPCRelative) O << "-" << MAI->getPCSymbol();
205 }
206
207 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
208   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
209
210   const MCExpr *Exp = TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);
211   Asm->OutStreamer.EmitValue(Exp, SizeOfEncodedValue(Encoding), /*addrspace*/0);
212 }
213
214 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
215   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
216
217   const MCExpr *Exp =
218     TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang, Asm->MMI, Encoding);
219   Asm->OutStreamer.EmitValue(Exp, SizeOfEncodedValue(Encoding), /*addrspace*/0);
220 }
221
222 /// EmitDifference - Emit the difference between two labels.  If this assembler
223 /// supports .set, we emit a .set of a temporary and then use it in the .word.
224 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
225                                   bool IsSmall) {
226   unsigned Size = IsSmall ? 4 : TD->getPointerSize();
227   Asm->EmitLabelDifference(TagHi, TagLo, Size);
228 }
229
230 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
231                                      const MCSymbol *Section,
232                                      bool IsSmall, bool isEH) {
233   bool isAbsolute;
234   if (isEH)
235     isAbsolute = MAI->isAbsoluteEHSectionOffsets();
236   else
237     isAbsolute = MAI->isAbsoluteDebugSectionOffsets();
238
239   if (!isAbsolute)
240     return EmitDifference(Label, Section, IsSmall);
241   
242   // On COFF targets, we have to emit the weird .secrel32 directive.
243   if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective())
244     O << SecOffDir << Label->getName();
245   else {
246     unsigned Size = IsSmall ? 4 : TD->getPointerSize();
247     Asm->OutStreamer.EmitSymbolValue(Label, Size, 0/*AddrSpace*/);
248   }
249 }
250
251 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
252 /// frame.
253 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
254                                   const std::vector<MachineMove> &Moves,
255                                   bool isEH) {
256   int stackGrowth =
257     Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
258     TargetFrameInfo::StackGrowsUp ?
259     TD->getPointerSize() : -TD->getPointerSize();
260   bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
261
262   for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
263     const MachineMove &Move = Moves[i];
264     unsigned LabelID = Move.getLabelID();
265
266     // Throw out move if the label is invalid.
267     if (LabelID && MMI->isLabelDeleted(LabelID))
268       continue;
269
270     const MachineLocation &Dst = Move.getDestination();
271     const MachineLocation &Src = Move.getSource();
272
273     // Advance row if new location.
274     if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
275       EmitCFAByte(dwarf::DW_CFA_advance_loc4);
276       EmitDifference(getDWLabel("label", LabelID),
277                      getDWLabel(BaseLabel, BaseLabelID), true);
278       BaseLabelID = LabelID;
279       BaseLabel = "label";
280       IsLocal = true;
281     }
282
283     // If advancing cfa.
284     if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
285       if (!Src.isReg()) {
286         if (Src.getReg() == MachineLocation::VirtualFP) {
287           EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
288         } else {
289           EmitCFAByte(dwarf::DW_CFA_def_cfa);
290           EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
291         }
292
293         int Offset = -Src.getOffset();
294         EmitULEB128(Offset, "Offset");
295       } else {
296         llvm_unreachable("Machine move not supported yet.");
297       }
298     } else if (Src.isReg() &&
299                Src.getReg() == MachineLocation::VirtualFP) {
300       if (Dst.isReg()) {
301         EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
302         EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
303       } else {
304         llvm_unreachable("Machine move not supported yet.");
305       }
306     } else {
307       unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
308       int Offset = Dst.getOffset() / stackGrowth;
309
310       if (Offset < 0) {
311         EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
312         EmitULEB128(Reg, "Reg");
313         EmitSLEB128(Offset, "Offset");
314       } else if (Reg < 64) {
315         EmitCFAByte(dwarf::DW_CFA_offset + Reg);
316         EmitULEB128(Offset, "Offset");
317       } else {
318         EmitCFAByte(dwarf::DW_CFA_offset_extended);
319         EmitULEB128(Reg, "Reg");
320         EmitULEB128(Offset, "Offset");
321       }
322     }
323   }
324 }