eliminate the non-MCSymbol versions of EmitReference.
[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) const {
223   // FIXME: REMOVE.
224   O << MAI->getPrivateGlobalPrefix() << Tag;
225   if (Number) O << Number;
226 }
227 void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
228                                   const char *Suffix) const {
229   // FIXME: REMOVE.
230   O << MAI->getPrivateGlobalPrefix() << Tag;
231   if (Number) O << Number;
232   O << Suffix;
233 }
234
235 /// EmitReference - Emit a reference to a label.
236 ///
237 void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
238                                  bool Force32Bit) const {
239   PrintRelDirective(Force32Bit);
240   O << *Sym;
241   if (IsPCRelative) O << "-" << MAI->getPCSymbol();
242 }
243
244 void DwarfPrinter::EmitReference(const char *Tag, unsigned Number,
245                                  unsigned Encoding) const {
246   // FIXME: REMOVE.
247   EmitReference(getDWLabel(Tag, Number), Encoding);
248 }
249
250 void DwarfPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
251   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
252
253   PrintRelDirective(Encoding);
254   O << *TLOF.getSymbolForDwarfReference(Sym, Asm->MMI, Encoding);;
255 }
256
257 void DwarfPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const {
258   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
259
260   PrintRelDirective(Encoding);
261   O << *TLOF.getSymbolForDwarfGlobalReference(GV, Asm->Mang,
262                                               Asm->MMI, Encoding);;
263 }
264
265 /// EmitDifference - Emit the difference between two labels.  If this assembler
266 /// supports .set, we emit a .set of a temporary and then use it in the .word.
267 void DwarfPrinter::EmitDifference(const MCSymbol *TagHi, const MCSymbol *TagLo,
268                                   bool IsSmall) {
269   if (MAI->hasSetDirective()) {
270     // FIXME: switch to OutStreamer.EmitAssignment.
271     O << "\t.set\t";
272     PrintLabelName("set", SetCounter, Flavor);
273     O << ",";
274     PrintLabelName(TagHi);
275     O << "-";
276     PrintLabelName(TagLo);
277     O << "\n";
278
279     PrintRelDirective(IsSmall);
280     PrintLabelName("set", SetCounter, Flavor);
281     ++SetCounter;
282   } else {
283     PrintRelDirective(IsSmall);
284     PrintLabelName(TagHi);
285     O << "-";
286     PrintLabelName(TagLo);
287   }
288 }
289
290 /// EmitDifference - Emit the difference between two labels.  If this assembler
291 /// supports .set, we emit a .set of a temporary and then use it in the .word.
292 void DwarfPrinter::EmitDifference(const char *TagHi, unsigned NumberHi,
293                                   const char *TagLo, unsigned NumberLo,
294                                   bool IsSmall) {
295   if (MAI->hasSetDirective()) {
296     // FIXME: switch to OutStreamer.EmitAssignment.
297     O << "\t.set\t";
298     PrintLabelName("set", SetCounter, Flavor);
299     O << ",";
300     PrintLabelName(TagHi, NumberHi);
301     O << "-";
302     PrintLabelName(TagLo, NumberLo);
303     O << "\n";
304     
305     PrintRelDirective(IsSmall);
306     PrintLabelName("set", SetCounter, Flavor);
307     ++SetCounter;
308   } else {
309     PrintRelDirective(IsSmall);
310     PrintLabelName(TagHi, NumberHi);
311     O << "-";
312     PrintLabelName(TagLo, NumberLo);
313   }
314 }
315
316 void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
317                                      const MCSymbol *Section,
318                                      bool IsSmall, bool isEH,
319                                      bool useSet) {
320   bool printAbsolute = false;
321   if (isEH)
322     printAbsolute = MAI->isAbsoluteEHSectionOffsets();
323   else
324     printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
325
326   if (MAI->hasSetDirective() && useSet) {
327     // FIXME: switch to OutStreamer.EmitAssignment.
328     O << "\t.set\t";
329     PrintLabelName("set", SetCounter, Flavor);
330     O << ",";
331     PrintLabelName(Label);
332
333     if (!printAbsolute) {
334       O << "-";
335       PrintLabelName(Section);
336     }
337
338     O << "\n";
339     PrintRelDirective(IsSmall);
340     PrintLabelName("set", SetCounter, Flavor);
341     ++SetCounter;
342   } else {
343     PrintRelDirective(IsSmall, true);
344     PrintLabelName(Label);
345
346     if (!printAbsolute) {
347       O << "-";
348       PrintLabelName(Section);
349     }
350   }
351 }
352
353 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
354 /// frame.
355 void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
356                                   const std::vector<MachineMove> &Moves,
357                                   bool isEH) {
358   int stackGrowth =
359     Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
360     TargetFrameInfo::StackGrowsUp ?
361     TD->getPointerSize() : -TD->getPointerSize();
362   bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
363
364   for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
365     const MachineMove &Move = Moves[i];
366     unsigned LabelID = Move.getLabelID();
367
368     if (LabelID) {
369       LabelID = MMI->MappedLabel(LabelID);
370
371       // Throw out move if the label is invalid.
372       if (!LabelID) continue;
373     }
374
375     const MachineLocation &Dst = Move.getDestination();
376     const MachineLocation &Src = Move.getSource();
377
378     // Advance row if new location.
379     if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
380       EmitCFAByte(dwarf::DW_CFA_advance_loc4);
381       EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
382       Asm->O << '\n';
383
384       BaseLabelID = LabelID;
385       BaseLabel = "label";
386       IsLocal = true;
387     }
388
389     // If advancing cfa.
390     if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
391       if (!Src.isReg()) {
392         if (Src.getReg() == MachineLocation::VirtualFP) {
393           EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
394         } else {
395           EmitCFAByte(dwarf::DW_CFA_def_cfa);
396           EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
397         }
398
399         int Offset = -Src.getOffset();
400         EmitULEB128(Offset, "Offset");
401       } else {
402         llvm_unreachable("Machine move not supported yet.");
403       }
404     } else if (Src.isReg() &&
405                Src.getReg() == MachineLocation::VirtualFP) {
406       if (Dst.isReg()) {
407         EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
408         EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
409       } else {
410         llvm_unreachable("Machine move not supported yet.");
411       }
412     } else {
413       unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
414       int Offset = Dst.getOffset() / stackGrowth;
415
416       if (Offset < 0) {
417         EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
418         EmitULEB128(Reg, "Reg");
419         EmitSLEB128(Offset, "Offset");
420       } else if (Reg < 64) {
421         EmitCFAByte(dwarf::DW_CFA_offset + Reg);
422         EmitULEB128(Offset, "Offset");
423       } else {
424         EmitCFAByte(dwarf::DW_CFA_offset_extended);
425         EmitULEB128(Reg, "Reg");
426         EmitULEB128(Offset, "Offset");
427       }
428     }
429   }
430 }