Remove a version of EmitDifference.
[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                            const char *flavor)
36 : O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
37   RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
38   SubprogramCount(0), Flavor(flavor), SetCounter(1) {}
39
40
41 /// getDWLabel - Return the MCSymbol corresponding to the assembler temporary
42 /// label with the specified stem and unique ID.
43 MCSymbol *DwarfPrinter::getDWLabel(const char *Name, unsigned ID) const {
44   // FIXME: REMOVE this.  However, there is stuff in EH that passes counters in
45   // here that can be zero.
46   
47   //assert(ID && "Should use getTempLabel if no ID");
48   if (ID == 0) return getTempLabel(Name);
49   return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
50                                            + Twine(Name) + Twine(ID));
51 }
52
53 /// getTempLabel - Return the MCSymbol corresponding to the assembler temporary
54 /// label with the specified name.
55 MCSymbol *DwarfPrinter::getTempLabel(const char *Name) const {
56   return Asm->OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())
57                                            + Name);
58 }
59
60
61 /// SizeOfEncodedValue - Return the size of the encoding in bytes.
62 unsigned DwarfPrinter::SizeOfEncodedValue(unsigned Encoding) const {
63   if (Encoding == dwarf::DW_EH_PE_omit)
64     return 0;
65
66   switch (Encoding & 0x07) {
67   case dwarf::DW_EH_PE_absptr:
68     return TD->getPointerSize();
69   case dwarf::DW_EH_PE_udata2:
70     return 2;
71   case dwarf::DW_EH_PE_udata4:
72     return 4;
73   case dwarf::DW_EH_PE_udata8:
74     return 8;
75   }
76
77   assert(0 && "Invalid encoded value.");
78   return 0;
79 }
80
81 void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
82   if (isInSection && MAI->getDwarfSectionOffsetDirective())
83     O << MAI->getDwarfSectionOffsetDirective();
84   else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
85     O << MAI->getData32bitsDirective();
86   else
87     O << MAI->getData64bitsDirective();
88 }
89
90 void DwarfPrinter::PrintRelDirective(unsigned Encoding) const {
91   unsigned Size = SizeOfEncodedValue(Encoding);
92   assert((Size == 4 || Size == 8) && "Do not support other types or rels!");
93
94   O << (Size == 4 ?
95         MAI->getData32bitsDirective() : MAI->getData64bitsDirective());
96 }
97
98 /// EOL - Print a newline character to asm stream.  If a comment is present
99 /// then it will be printed first.  Comments should not contain '\n'.
100 void DwarfPrinter::EOL(const Twine &Comment) const {
101   if (Asm->VerboseAsm && !Comment.isTriviallyEmpty()) {
102     Asm->O.PadToColumn(MAI->getCommentColumn());
103     Asm->O << Asm->MAI->getCommentString() << ' ' << Comment;
104   }
105   Asm->O << '\n';
106 }
107
108 static const char *DecodeDWARFEncoding(unsigned Encoding) {
109   switch (Encoding) {
110   case dwarf::DW_EH_PE_absptr: return "absptr";
111   case dwarf::DW_EH_PE_omit:   return "omit";
112   case dwarf::DW_EH_PE_pcrel:  return "pcrel";
113   case dwarf::DW_EH_PE_udata4: return "udata4";
114   case dwarf::DW_EH_PE_udata8: return "udata8";
115   case dwarf::DW_EH_PE_sdata4: return "sdata4";
116   case dwarf::DW_EH_PE_sdata8: return "sdata8";
117   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
118   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
119   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
120   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
121   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
122     return "indirect pcrel udata4";
123   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
124     return "indirect pcrel sdata4";
125   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
126     return "indirect pcrel udata8";
127   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
128     return "indirect pcrel sdata8";
129   }
130   
131   return "<unknown encoding>";
132 }
133
134 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
135 /// encoding.  If verbose assembly output is enabled, we output comments
136 /// describing the encoding.  Desc is an optional string saying what the
137 /// encoding is specifying (e.g. "LSDA").
138 void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
139   if (Asm->VerboseAsm) {
140     if (Desc != 0)
141       Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
142                                   Twine(DecodeDWARFEncoding(Val)));
143     else
144       Asm->OutStreamer.AddComment(Twine("Encoding = ") +
145                                   DecodeDWARFEncoding(Val));
146   }
147
148   Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
149 }
150
151 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
152 void DwarfPrinter::EmitCFAByte(unsigned Val) {
153   if (Asm->VerboseAsm) {
154     if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
155       Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" + 
156                                   Twine(Val-dwarf::DW_CFA_offset) + ")");
157     else
158       Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
159   }
160   Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
161 }
162
163 /// EmitSLEB128 - emit the specified signed leb128 value.
164 void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
165   if (Asm->VerboseAsm && Desc)
166     Asm->OutStreamer.AddComment(Desc);
167     
168   if (MAI->hasLEB128()) {
169     O << "\t.sleb128\t" << Value;
170     Asm->OutStreamer.AddBlankLine();
171     return;
172   }
173
174   // If we don't have .sleb128, emit as .bytes.
175   int Sign = Value >> (8 * sizeof(Value) - 1);
176   bool IsMore;
177   
178   do {
179     unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
180     Value >>= 7;
181     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
182     if (IsMore) Byte |= 0x80;
183     Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
184   } while (IsMore);
185 }
186
187 /// EmitULEB128 - emit the specified signed leb128 value.
188 void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc,
189                                unsigned PadTo) const {
190   if (Asm->VerboseAsm && Desc)
191     Asm->OutStreamer.AddComment(Desc);
192  
193   if (MAI->hasLEB128() && PadTo == 0) {
194     O << "\t.uleb128\t" << Value;
195     Asm->OutStreamer.AddBlankLine();
196     return;
197   }
198   
199   // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
200   do {
201     unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
202     Value >>= 7;
203     if (Value || PadTo != 0) Byte |= 0x80;
204     Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
205   } while (Value);
206
207   if (PadTo) {
208     if (PadTo > 1)
209       Asm->OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
210     Asm->OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
211   }
212 }
213
214
215 /// PrintLabelName - Print label name in form used by Dwarf writer.
216 ///
217 void DwarfPrinter::PrintLabelName(const MCSymbol *Label) const {
218   // FIXME: REMOVE.
219   O << Label->getName();
220 }
221
222 void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
223                                   const char *Suffix) const {
224   // FIXME: REMOVE.
225   O << MAI->getPrivateGlobalPrefix() << Tag;
226   if (Number) O << Number;
227   O << Suffix;
228 }
229
230 /// EmitReference - Emit a reference to a label.
231 ///
232 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
233                                  bool Force32Bit) const {
234   PrintRelDirective(Force32Bit);
235   O << *Sym;
236   if (IsPCRelative) O << "-" << MAI->getPCSymbol();
237 }
238
239 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
240   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
241
242   PrintRelDirective(Encoding);
243   O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
244 }
245
246 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const {
247   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
248
249   PrintRelDirective(Encoding);
250   O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
251                                               Asm->MMI, Encoding);
252 }
253
254 /// EmitDifference - Emit the difference between two labels.  If this assembler
255 /// supports .set, we emit a .set of a temporary and then use it in the .word.
256 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
257                                   bool IsSmall) {
258   if (MAI->hasSetDirective()) {
259     // FIXME: switch to OutStreamer.EmitAssignment.
260     O << "\t.set\t";
261     PrintLabelName("set", SetCounter, Flavor);
262     O << ",";
263     PrintLabelName(TagHi);
264     O << "-";
265     PrintLabelName(TagLo);
266     O << "\n";
267
268     PrintRelDirective(IsSmall);
269     PrintLabelName("set", SetCounter, Flavor);
270     ++SetCounter;
271   } else {
272     PrintRelDirective(IsSmall);
273     PrintLabelName(TagHi);
274     O << "-";
275     PrintLabelName(TagLo);
276   }
277 }
278
279 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
280                                      const MCSymbol *Section,
281                                      bool IsSmall, bool isEH,
282                                      bool useSet) {
283   bool printAbsolute = false;
284   if (isEH)
285     printAbsolute = MAI->isAbsoluteEHSectionOffsets();
286   else
287     printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
288
289   if (MAI->hasSetDirective() && useSet) {
290     // FIXME: switch to OutStreamer.EmitAssignment.
291     O << "\t.set\t";
292     PrintLabelName("set", SetCounter, Flavor);
293     O << ",";
294     PrintLabelName(Label);
295
296     if (!printAbsolute) {
297       O << "-";
298       PrintLabelName(Section);
299     }
300
301     O << "\n";
302     PrintRelDirective(IsSmall);
303     PrintLabelName("set", SetCounter, Flavor);
304     ++SetCounter;
305   } else {
306     PrintRelDirective(IsSmall, true);
307     PrintLabelName(Label);
308
309     if (!printAbsolute) {
310       O << "-";
311       PrintLabelName(Section);
312     }
313   }
314 }
315
316 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
317 /// frame.
318 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
319                                   const std::vector<MachineMove> &Moves,
320                                   bool isEH) {
321   int stackGrowth =
322     Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
323     TargetFrameInfo::StackGrowsUp ?
324     TD->getPointerSize() : -TD->getPointerSize();
325   bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
326
327   for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
328     const MachineMove &Move = Moves[i];
329     unsigned LabelID = Move.getLabelID();
330
331     if (LabelID) {
332       LabelID = MMI->MappedLabel(LabelID);
333
334       // Throw out move if the label is invalid.
335       if (!LabelID) continue;
336     }
337
338     const MachineLocation &Dst = Move.getDestination();
339     const MachineLocation &Src = Move.getSource();
340
341     // Advance row if new location.
342     if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
343       EmitCFAByte(dwarf::DW_CFA_advance_loc4);
344       EmitDifference(getDWLabel("label", LabelID),
345                      getDWLabel(BaseLabel, BaseLabelID), true);
346       Asm->O << '\n';
347
348       BaseLabelID = LabelID;
349       BaseLabel = "label";
350       IsLocal = true;
351     }
352
353     // If advancing cfa.
354     if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
355       if (!Src.isReg()) {
356         if (Src.getReg() == MachineLocation::VirtualFP) {
357           EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
358         } else {
359           EmitCFAByte(dwarf::DW_CFA_def_cfa);
360           EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
361         }
362
363         int Offset = -Src.getOffset();
364         EmitULEB128(Offset, "Offset");
365       } else {
366         llvm_unreachable("Machine move not supported yet.");
367       }
368     } else if (Src.isReg() &&
369                Src.getReg() == MachineLocation::VirtualFP) {
370       if (Dst.isReg()) {
371         EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
372         EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
373       } else {
374         llvm_unreachable("Machine move not supported yet.");
375       }
376     } else {
377       unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
378       int Offset = Dst.getOffset() / stackGrowth;
379
380       if (Offset < 0) {
381         EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
382         EmitULEB128(Reg, "Reg");
383         EmitSLEB128(Offset, "Offset");
384       } else if (Reg < 64) {
385         EmitCFAByte(dwarf::DW_CFA_offset + Reg);
386         EmitULEB128(Offset, "Offset");
387       } else {
388         EmitCFAByte(dwarf::DW_CFA_offset_extended);
389         EmitULEB128(Reg, "Reg");
390         EmitULEB128(Offset, "Offset");
391       }
392     }
393   }
394 }