- Update comments.
[oota-llvm.git] / lib / CodeGen / MachineDebugInfoDesc.cpp
1 //===-- llvm/CodeGen/MachineDebugInfoDesc.cpp -------------------*- C++ -*-===//
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 #include "llvm/CodeGen/MachineDebugInfoDesc.h"
11 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/Support/Dwarf.h"
15 #include "llvm/Support/Streams.h"
16
17 using namespace llvm;
18 using namespace llvm::dwarf;
19
20 /// getUIntOperand - Return ith operand if it is an unsigned integer.
21 ///
22 static ConstantInt *getUIntOperand(const GlobalVariable *GV, unsigned i) {
23   // Make sure the GlobalVariable has an initializer.
24   if (!GV->hasInitializer()) return NULL;
25   
26   // Get the initializer constant.
27   ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
28   if (!CI) return NULL;
29   
30   // Check if there is at least i + 1 operands.
31   unsigned N = CI->getNumOperands();
32   if (i >= N) return NULL;
33
34   // Check constant.
35   return dyn_cast<ConstantInt>(CI->getOperand(i));
36 }
37
38 //===----------------------------------------------------------------------===//
39
40 /// Supply a home for the DebugInfoDesc's v-table.
41 DebugInfoDesc::~DebugInfoDesc() {}
42
43 /// TagFromGlobal - Returns the tag number from a debug info descriptor
44 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
45 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
46   ConstantInt *C = getUIntOperand(GV, 0);
47   return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
48              (unsigned)DW_TAG_invalid;
49 }
50
51 /// VersionFromGlobal - Returns the version number from a debug info
52 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
53 /// int.
54 unsigned  DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
55   ConstantInt *C = getUIntOperand(GV, 0);
56   return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
57              (unsigned)DW_TAG_invalid;
58 }
59
60 /// DescFactory - Create an instance of debug info descriptor based on Tag.
61 /// Return NULL if not a recognized Tag.
62 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
63   switch (Tag) {
64   case DW_TAG_anchor:           return new AnchorDesc();
65   case DW_TAG_compile_unit:     return new CompileUnitDesc();
66   case DW_TAG_variable:         return new GlobalVariableDesc();
67   case DW_TAG_subprogram:       return new SubprogramDesc();
68   case DW_TAG_lexical_block:    return new BlockDesc();
69   case DW_TAG_base_type:        return new BasicTypeDesc();
70   case DW_TAG_typedef:
71   case DW_TAG_pointer_type:        
72   case DW_TAG_reference_type:
73   case DW_TAG_const_type:
74   case DW_TAG_volatile_type:        
75   case DW_TAG_restrict_type:
76   case DW_TAG_member:
77   case DW_TAG_inheritance:      return new DerivedTypeDesc(Tag);
78   case DW_TAG_array_type:
79   case DW_TAG_structure_type:
80   case DW_TAG_union_type:
81   case DW_TAG_enumeration_type:
82   case DW_TAG_vector_type:
83   case DW_TAG_subroutine_type:  return new CompositeTypeDesc(Tag);
84   case DW_TAG_subrange_type:    return new SubrangeDesc();
85   case DW_TAG_enumerator:       return new EnumeratorDesc();
86   case DW_TAG_return_variable:
87   case DW_TAG_arg_variable:
88   case DW_TAG_auto_variable:    return new VariableDesc(Tag);
89   default: break;
90   }
91   return NULL;
92 }
93
94 /// getLinkage - get linkage appropriate for this type of descriptor.
95 unsigned DebugInfoDesc::getLinkage() const {
96   return (unsigned)GlobalValue::InternalLinkage;
97 }
98
99 /// ApplyToFields - Target the vistor to the fields of the descriptor.
100 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
101   Visitor->Apply(Tag);
102 }
103
104 //===----------------------------------------------------------------------===//
105
106 AnchorDesc::AnchorDesc()
107   : DebugInfoDesc(DW_TAG_anchor), AnchorTag(0) {}
108
109 AnchorDesc::AnchorDesc(AnchoredDesc *D)
110   : DebugInfoDesc(DW_TAG_anchor), AnchorTag(D->getTag()) {}
111
112 // Implement isa/cast/dyncast.
113 bool AnchorDesc::classof(const DebugInfoDesc *D) {
114   return D->getTag() == DW_TAG_anchor;
115 }
116   
117 /// getLinkage - get linkage appropriate for this type of descriptor.
118 unsigned AnchorDesc::getLinkage() const {
119   return (unsigned)GlobalValue::LinkOnceLinkage;
120 }
121
122 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
123 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
124   DebugInfoDesc::ApplyToFields(Visitor);
125   Visitor->Apply(AnchorTag);
126 }
127
128 /// getDescString - Return a string used to compose global names and labels. A
129 /// global variable name needs to be defined for each debug descriptor that is
130 /// anchored. NOTE: that each global variable named here also needs to be added
131 /// to the list of names left external in the internalizer.
132 ///
133 ///   ExternalNames.insert("llvm.dbg.compile_units");
134 ///   ExternalNames.insert("llvm.dbg.global_variables");
135 ///   ExternalNames.insert("llvm.dbg.subprograms");
136 const char *AnchorDesc::getDescString() const {
137   switch (AnchorTag) {
138   case DW_TAG_compile_unit: {
139     CompileUnitDesc CUD;
140     return CUD.getAnchorString();
141   }
142   case DW_TAG_variable: {
143     GlobalVariableDesc GVD;
144     return GVD.getAnchorString();
145   }
146   case DW_TAG_subprogram: {
147     SubprogramDesc SPD;
148     return SPD.getAnchorString();
149   }
150   default: break;
151   }
152
153   assert(0 && "Tag does not have a case for anchor string");
154   return "";
155 }
156
157 #ifndef NDEBUG
158 void AnchorDesc::dump() {
159   cerr << getDescString() << " "
160        << "Version(" << getVersion() << "), "
161        << "Tag(" << getTag() << "), "
162        << "AnchorTag(" << AnchorTag << ")\n";
163 }
164 #endif
165
166 //===----------------------------------------------------------------------===//
167
168 AnchoredDesc::AnchoredDesc(unsigned T)
169   : DebugInfoDesc(T), Anchor(NULL) {}
170
171 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
172 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
173   DebugInfoDesc::ApplyToFields(Visitor);
174   Visitor->Apply(Anchor);
175 }
176
177 //===----------------------------------------------------------------------===//
178
179 CompileUnitDesc::CompileUnitDesc()
180   : AnchoredDesc(DW_TAG_compile_unit), Language(0), FileName(""),
181     Directory(""), Producer("") {}
182
183 // Implement isa/cast/dyncast.
184 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
185   return D->getTag() == DW_TAG_compile_unit;
186 }
187
188 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
189 ///
190 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
191   AnchoredDesc::ApplyToFields(Visitor);
192   
193   // Handle cases out of sync with compiler.
194   if (getVersion() == 0) {
195     unsigned DebugVersion;
196     Visitor->Apply(DebugVersion);
197   }
198
199   Visitor->Apply(Language);
200   Visitor->Apply(FileName);
201   Visitor->Apply(Directory);
202   Visitor->Apply(Producer);
203 }
204
205 #ifndef NDEBUG
206 void CompileUnitDesc::dump() {
207   cerr << getDescString() << " "
208        << "Version(" << getVersion() << "), "
209        << "Tag(" << getTag() << "), "
210        << "Anchor(" << getAnchor() << "), "
211        << "Language(" << Language << "), "
212        << "FileName(\"" << FileName << "\"), "
213        << "Directory(\"" << Directory << "\"), "
214        << "Producer(\"" << Producer << "\")\n";
215 }
216 #endif
217
218 //===----------------------------------------------------------------------===//
219
220 TypeDesc::TypeDesc(unsigned T)
221   : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), Size(0),
222     Align(0), Offset(0), Flags(0) {}
223
224 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
225 ///
226 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
227   DebugInfoDesc::ApplyToFields(Visitor);
228   Visitor->Apply(Context);
229   Visitor->Apply(Name);
230   Visitor->Apply(File);
231   Visitor->Apply(Line);
232   Visitor->Apply(Size);
233   Visitor->Apply(Align);
234   Visitor->Apply(Offset);
235   if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
236 }
237
238 #ifndef NDEBUG
239 void TypeDesc::dump() {
240   cerr << getDescString() << " "
241        << "Version(" << getVersion() << "), "
242        << "Tag(" << getTag() << "), "
243        << "Context(" << Context << "), "
244        << "Name(\"" << Name << "\"), "
245        << "File(" << File << "), "
246        << "Line(" << Line << "), "
247        << "Size(" << Size << "), "
248        << "Align(" << Align << "), "
249        << "Offset(" << Offset << "), "
250        << "Flags(" << Flags << ")\n";
251 }
252 #endif
253
254 //===----------------------------------------------------------------------===//
255
256 BasicTypeDesc::BasicTypeDesc()
257   : TypeDesc(DW_TAG_base_type), Encoding(0) {}
258
259 // Implement isa/cast/dyncast.
260 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
261   return D->getTag() == DW_TAG_base_type;
262 }
263
264 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
265 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
266   TypeDesc::ApplyToFields(Visitor);
267   Visitor->Apply(Encoding);
268 }
269
270 #ifndef NDEBUG
271 void BasicTypeDesc::dump() {
272   cerr << getDescString() << " "
273        << "Version(" << getVersion() << "), "
274        << "Tag(" << getTag() << "), "
275        << "Context(" << getContext() << "), "
276        << "Name(\"" << getName() << "\"), "
277        << "Size(" << getSize() << "), "
278        << "Encoding(" << Encoding << ")\n";
279 }
280 #endif
281
282 //===----------------------------------------------------------------------===//
283
284 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
285   : TypeDesc(T), FromType(NULL) {}
286
287 // Implement isa/cast/dyncast.
288 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
289   unsigned T =  D->getTag();
290   switch (T) {
291   case DW_TAG_typedef:
292   case DW_TAG_pointer_type:
293   case DW_TAG_reference_type:
294   case DW_TAG_const_type:
295   case DW_TAG_volatile_type:
296   case DW_TAG_restrict_type:
297   case DW_TAG_member:
298   case DW_TAG_inheritance:
299     return true;
300   default: break;
301   }
302   return false;
303 }
304
305 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
306 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
307   TypeDesc::ApplyToFields(Visitor);
308   Visitor->Apply(FromType);
309 }
310
311 #ifndef NDEBUG
312 void DerivedTypeDesc::dump() {
313   cerr << getDescString() << " "
314        << "Version(" << getVersion() << "), "
315        << "Tag(" << getTag() << "), "
316        << "Context(" << getContext() << "), "
317        << "Name(\"" << getName() << "\"), "
318        << "Size(" << getSize() << "), "
319        << "File(" << getFile() << "), "
320        << "Line(" << getLine() << "), "
321        << "FromType(" << FromType << ")\n";
322 }
323 #endif
324
325 //===----------------------------------------------------------------------===//
326
327 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
328   : DerivedTypeDesc(T), Elements() {}
329   
330 // Implement isa/cast/dyncast.
331 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
332   unsigned T =  D->getTag();
333   switch (T) {
334   case DW_TAG_array_type:
335   case DW_TAG_structure_type:
336   case DW_TAG_union_type:
337   case DW_TAG_enumeration_type:
338   case DW_TAG_vector_type:
339   case DW_TAG_subroutine_type:
340     return true;
341   default: break;
342   }
343   return false;
344 }
345
346 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
347 ///
348 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
349   DerivedTypeDesc::ApplyToFields(Visitor);  
350   Visitor->Apply(Elements);
351 }
352
353 #ifndef NDEBUG
354 void CompositeTypeDesc::dump() {
355   cerr << getDescString() << " "
356        << "Version(" << getVersion() << "), "
357        << "Tag(" << getTag() << "), "
358        << "Context(" << getContext() << "), "
359        << "Name(\"" << getName() << "\"), "
360        << "Size(" << getSize() << "), "
361        << "File(" << getFile() << "), "
362        << "Line(" << getLine() << "), "
363        << "FromType(" << getFromType() << "), "
364        << "Elements.size(" << Elements.size() << ")\n";
365 }
366 #endif
367
368 //===----------------------------------------------------------------------===//
369
370 SubrangeDesc::SubrangeDesc()
371   : DebugInfoDesc(DW_TAG_subrange_type), Lo(0), Hi(0) {}
372
373 // Implement isa/cast/dyncast.
374 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
375   return D->getTag() == DW_TAG_subrange_type;
376 }
377
378 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
379 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
380   DebugInfoDesc::ApplyToFields(Visitor);
381   Visitor->Apply(Lo);
382   Visitor->Apply(Hi);
383 }
384
385 #ifndef NDEBUG
386 void SubrangeDesc::dump() {
387   cerr << getDescString() << " "
388        << "Version(" << getVersion() << "), "
389        << "Tag(" << getTag() << "), "
390        << "Lo(" << Lo << "), "
391        << "Hi(" << Hi << ")\n";
392 }
393 #endif
394
395 //===----------------------------------------------------------------------===//
396
397 EnumeratorDesc::EnumeratorDesc()
398   : DebugInfoDesc(DW_TAG_enumerator), Name(""), Value(0) {}
399
400 // Implement isa/cast/dyncast.
401 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
402   return D->getTag() == DW_TAG_enumerator;
403 }
404
405 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
406 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
407   DebugInfoDesc::ApplyToFields(Visitor);
408   Visitor->Apply(Name);
409   Visitor->Apply(Value);
410 }
411
412 #ifndef NDEBUG
413 void EnumeratorDesc::dump() {
414   cerr << getDescString() << " "
415        << "Version(" << getVersion() << "), "
416        << "Tag(" << getTag() << "), "
417        << "Name(" << Name << "), "
418        << "Value(" << Value << ")\n";
419 }
420 #endif
421
422 //===----------------------------------------------------------------------===//
423
424 VariableDesc::VariableDesc(unsigned T)
425   : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), TyDesc(0)
426 {}
427
428 // Implement isa/cast/dyncast.
429 bool VariableDesc::classof(const DebugInfoDesc *D) {
430   unsigned T =  D->getTag();
431   switch (T) {
432   case DW_TAG_auto_variable:
433   case DW_TAG_arg_variable:
434   case DW_TAG_return_variable:
435     return true;
436   default: break;
437   }
438   return false;
439 }
440
441 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
442 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
443   DebugInfoDesc::ApplyToFields(Visitor);
444   Visitor->Apply(Context);
445   Visitor->Apply(Name);
446   Visitor->Apply(File);
447   Visitor->Apply(Line);
448   Visitor->Apply(TyDesc);
449 }
450
451 #ifndef NDEBUG
452 void VariableDesc::dump() {
453   cerr << getDescString() << " "
454        << "Version(" << getVersion() << "), "
455        << "Tag(" << getTag() << "), "
456        << "Context(" << Context << "), "
457        << "Name(\"" << Name << "\"), "
458        << "File(" << File << "), "
459        << "Line(" << Line << "), "
460        << "TyDesc(" << TyDesc << ")\n";
461 }
462 #endif
463
464 //===----------------------------------------------------------------------===//
465
466 GlobalDesc::GlobalDesc(unsigned T)
467   : AnchoredDesc(T), Context(0), Name(""), FullName(""), LinkageName(""),
468     File(NULL), Line(0), TyDesc(NULL), IsStatic(false), IsDefinition(false) {}
469
470 /// ApplyToFields - Target the visitor to the fields of the global.
471 ///
472 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
473   AnchoredDesc::ApplyToFields(Visitor);
474   Visitor->Apply(Context);
475   Visitor->Apply(Name);
476   Visitor->Apply(FullName);
477   Visitor->Apply(LinkageName);
478   Visitor->Apply(File);
479   Visitor->Apply(Line);
480   Visitor->Apply(TyDesc);
481   Visitor->Apply(IsStatic);
482   Visitor->Apply(IsDefinition);
483 }
484
485 //===----------------------------------------------------------------------===//
486
487 GlobalVariableDesc::GlobalVariableDesc()
488   : GlobalDesc(DW_TAG_variable), Global(NULL) {}
489
490 // Implement isa/cast/dyncast.
491 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
492   return D->getTag() == DW_TAG_variable; 
493 }
494
495 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
496 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
497   GlobalDesc::ApplyToFields(Visitor);
498   Visitor->Apply(Global);
499 }
500
501 #ifndef NDEBUG
502 void GlobalVariableDesc::dump() {
503   cerr << getDescString() << " "
504        << "Version(" << getVersion() << "), "
505        << "Tag(" << getTag() << "), "
506        << "Anchor(" << getAnchor() << "), "
507        << "Name(\"" << getName() << "\"), "
508        << "FullName(\"" << getFullName() << "\"), "
509        << "LinkageName(\"" << getLinkageName() << "\"), "
510        << "File(" << getFile() << "),"
511        << "Line(" << getLine() << "),"
512        << "Type(" << getType() << "), "
513        << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
514        << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
515        << "Global(" << Global << ")\n";
516 }
517 #endif
518
519 //===----------------------------------------------------------------------===//
520
521 SubprogramDesc::SubprogramDesc()
522   : GlobalDesc(DW_TAG_subprogram) {}
523
524 // Implement isa/cast/dyncast.
525 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
526   return D->getTag() == DW_TAG_subprogram;
527 }
528
529 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
530 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
531   GlobalDesc::ApplyToFields(Visitor);
532 }
533
534 #ifndef NDEBUG
535 void SubprogramDesc::dump() {
536   cerr << getDescString() << " "
537        << "Version(" << getVersion() << "), "
538        << "Tag(" << getTag() << "), "
539        << "Anchor(" << getAnchor() << "), "
540        << "Name(\"" << getName() << "\"), "
541        << "FullName(\"" << getFullName() << "\"), "
542        << "LinkageName(\"" << getLinkageName() << "\"), "
543        << "File(" << getFile() << "),"
544        << "Line(" << getLine() << "),"
545        << "Type(" << getType() << "), "
546        << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
547        << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
548 }
549 #endif
550
551 //===----------------------------------------------------------------------===//
552
553 BlockDesc::BlockDesc()
554   : DebugInfoDesc(DW_TAG_lexical_block), Context(NULL) {}
555
556 // Implement isa/cast/dyncast.
557 bool BlockDesc::classof(const DebugInfoDesc *D) {
558   return D->getTag() == DW_TAG_lexical_block;
559 }
560
561 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
562 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
563   DebugInfoDesc::ApplyToFields(Visitor);
564
565   Visitor->Apply(Context);
566 }
567
568 #ifndef NDEBUG
569 void BlockDesc::dump() {
570   cerr << getDescString() << " "
571        << "Version(" << getVersion() << "), "
572        << "Tag(" << getTag() << "),"
573        << "Context(" << Context << ")\n";
574 }
575 #endif