e293609579e5b9be5282e8716a134e028eb7fcc1
[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 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
96   return 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
113 // Implement isa/cast/dyncast.
114 bool AnchorDesc::classof(const DebugInfoDesc *D) {
115   return D->getTag() == DW_TAG_anchor;
116 }
117   
118 /// getLinkage - get linkage appropriate for this type of descriptor.
119 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
120   return GlobalValue::LinkOnceLinkage;
121 }
122
123 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
124 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
125   DebugInfoDesc::ApplyToFields(Visitor);
126   Visitor->Apply(AnchorTag);
127 }
128
129 /// getDescString - Return a string used to compose global names and labels. A
130 /// global variable name needs to be defined for each debug descriptor that is
131 /// anchored. NOTE: that each global variable named here also needs to be added
132 /// to the list of names left external in the internalizer.
133 ///
134 ///   ExternalNames.insert("llvm.dbg.compile_units");
135 ///   ExternalNames.insert("llvm.dbg.global_variables");
136 ///   ExternalNames.insert("llvm.dbg.subprograms");
137 const char *AnchorDesc::getDescString() const {
138   switch (AnchorTag) {
139   case DW_TAG_compile_unit: {
140     CompileUnitDesc CUD;
141     return CUD.getAnchorString();
142   }
143   case DW_TAG_variable: {
144     GlobalVariableDesc GVD;
145     return GVD.getAnchorString();
146   }
147   case DW_TAG_subprogram: {
148     SubprogramDesc SPD;
149     return SPD.getAnchorString();
150   }
151   default: break;
152   }
153
154   assert(0 && "Tag does not have a case for anchor string");
155   return "";
156 }
157
158 #ifndef NDEBUG
159 void AnchorDesc::dump() {
160   cerr << getDescString() << " "
161        << "Version(" << getVersion() << "), "
162        << "Tag(" << getTag() << "), "
163        << "AnchorTag(" << AnchorTag << ")\n";
164 }
165 #endif
166
167 //===----------------------------------------------------------------------===//
168
169 AnchoredDesc::AnchoredDesc(unsigned T)
170   : DebugInfoDesc(T), Anchor(NULL) {
171 }
172
173 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
174 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
175   DebugInfoDesc::ApplyToFields(Visitor);
176   Visitor->Apply(Anchor);
177 }
178
179 //===----------------------------------------------------------------------===//
180
181 CompileUnitDesc::CompileUnitDesc()
182   : AnchoredDesc(DW_TAG_compile_unit), Language(0), FileName(""),
183     Directory(""), Producer("") {
184 }
185
186 // Implement isa/cast/dyncast.
187 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
188   return D->getTag() == DW_TAG_compile_unit;
189 }
190
191 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
192 ///
193 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
194   AnchoredDesc::ApplyToFields(Visitor);
195   
196   // Handle cases out of sync with compiler.
197   if (getVersion() == 0) {
198     unsigned DebugVersion;
199     Visitor->Apply(DebugVersion);
200   }
201
202   Visitor->Apply(Language);
203   Visitor->Apply(FileName);
204   Visitor->Apply(Directory);
205   Visitor->Apply(Producer);
206 }
207
208 #ifndef NDEBUG
209 void CompileUnitDesc::dump() {
210   cerr << getDescString() << " "
211        << "Version(" << getVersion() << "), "
212        << "Tag(" << getTag() << "), "
213        << "Anchor(" << getAnchor() << "), "
214        << "Language(" << Language << "), "
215        << "FileName(\"" << FileName << "\"), "
216        << "Directory(\"" << Directory << "\"), "
217        << "Producer(\"" << Producer << "\")\n";
218 }
219 #endif
220
221 //===----------------------------------------------------------------------===//
222
223 TypeDesc::TypeDesc(unsigned T)
224   : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), Size(0),
225     Align(0), Offset(0), Flags(0) {
226 }
227
228 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
229 ///
230 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
231   DebugInfoDesc::ApplyToFields(Visitor);
232   Visitor->Apply(Context);
233   Visitor->Apply(Name);
234   Visitor->Apply(File);
235   Visitor->Apply(Line);
236   Visitor->Apply(Size);
237   Visitor->Apply(Align);
238   Visitor->Apply(Offset);
239   if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
240 }
241
242 #ifndef NDEBUG
243 void TypeDesc::dump() {
244   cerr << getDescString() << " "
245        << "Version(" << getVersion() << "), "
246        << "Tag(" << getTag() << "), "
247        << "Context(" << Context << "), "
248        << "Name(\"" << Name << "\"), "
249        << "File(" << File << "), "
250        << "Line(" << Line << "), "
251        << "Size(" << Size << "), "
252        << "Align(" << Align << "), "
253        << "Offset(" << Offset << "), "
254        << "Flags(" << Flags << ")\n";
255 }
256 #endif
257
258 //===----------------------------------------------------------------------===//
259
260 BasicTypeDesc::BasicTypeDesc()
261   : TypeDesc(DW_TAG_base_type), Encoding(0) {
262 }
263
264 // Implement isa/cast/dyncast.
265 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
266   return D->getTag() == DW_TAG_base_type;
267 }
268
269 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
270 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
271   TypeDesc::ApplyToFields(Visitor);
272   Visitor->Apply(Encoding);
273 }
274
275 #ifndef NDEBUG
276 void BasicTypeDesc::dump() {
277   cerr << getDescString() << " "
278        << "Version(" << getVersion() << "), "
279        << "Tag(" << getTag() << "), "
280        << "Context(" << getContext() << "), "
281        << "Name(\"" << getName() << "\"), "
282        << "Size(" << getSize() << "), "
283        << "Encoding(" << Encoding << ")\n";
284 }
285 #endif
286
287 //===----------------------------------------------------------------------===//
288
289 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
290   : TypeDesc(T), FromType(NULL) {
291 }
292
293 // Implement isa/cast/dyncast.
294 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
295   unsigned T =  D->getTag();
296   switch (T) {
297   case DW_TAG_typedef:
298   case DW_TAG_pointer_type:
299   case DW_TAG_reference_type:
300   case DW_TAG_const_type:
301   case DW_TAG_volatile_type:
302   case DW_TAG_restrict_type:
303   case DW_TAG_member:
304   case DW_TAG_inheritance:
305     return true;
306   default: break;
307   }
308   return false;
309 }
310
311 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
312 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
313   TypeDesc::ApplyToFields(Visitor);
314   Visitor->Apply(FromType);
315 }
316
317 #ifndef NDEBUG
318 void DerivedTypeDesc::dump() {
319   cerr << getDescString() << " "
320        << "Version(" << getVersion() << "), "
321        << "Tag(" << getTag() << "), "
322        << "Context(" << getContext() << "), "
323        << "Name(\"" << getName() << "\"), "
324        << "Size(" << getSize() << "), "
325        << "File(" << getFile() << "), "
326        << "Line(" << getLine() << "), "
327        << "FromType(" << FromType << ")\n";
328 }
329 #endif
330
331 //===----------------------------------------------------------------------===//
332
333 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
334   : DerivedTypeDesc(T), Elements() {
335 }
336   
337 // Implement isa/cast/dyncast.
338 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
339   unsigned T =  D->getTag();
340   switch (T) {
341   case DW_TAG_array_type:
342   case DW_TAG_structure_type:
343   case DW_TAG_union_type:
344   case DW_TAG_enumeration_type:
345   case DW_TAG_vector_type:
346   case DW_TAG_subroutine_type:
347     return true;
348   default: break;
349   }
350   return false;
351 }
352
353 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
354 ///
355 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
356   DerivedTypeDesc::ApplyToFields(Visitor);  
357   Visitor->Apply(Elements);
358 }
359
360 #ifndef NDEBUG
361 void CompositeTypeDesc::dump() {
362   cerr << getDescString() << " "
363        << "Version(" << getVersion() << "), "
364        << "Tag(" << getTag() << "), "
365        << "Context(" << getContext() << "), "
366        << "Name(\"" << getName() << "\"), "
367        << "Size(" << getSize() << "), "
368        << "File(" << getFile() << "), "
369        << "Line(" << getLine() << "), "
370        << "FromType(" << getFromType() << "), "
371        << "Elements.size(" << Elements.size() << ")\n";
372 }
373 #endif
374
375 //===----------------------------------------------------------------------===//
376
377 SubrangeDesc::SubrangeDesc()
378   : DebugInfoDesc(DW_TAG_subrange_type), Lo(0), Hi(0) {
379 }
380
381 // Implement isa/cast/dyncast.
382 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
383   return D->getTag() == DW_TAG_subrange_type;
384 }
385
386 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
387 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
388   DebugInfoDesc::ApplyToFields(Visitor);
389   Visitor->Apply(Lo);
390   Visitor->Apply(Hi);
391 }
392
393 #ifndef NDEBUG
394 void SubrangeDesc::dump() {
395   cerr << getDescString() << " "
396        << "Version(" << getVersion() << "), "
397        << "Tag(" << getTag() << "), "
398        << "Lo(" << Lo << "), "
399        << "Hi(" << Hi << ")\n";
400 }
401 #endif
402
403 //===----------------------------------------------------------------------===//
404
405 EnumeratorDesc::EnumeratorDesc()
406   : DebugInfoDesc(DW_TAG_enumerator), Name(""), Value(0) {
407 }
408
409 // Implement isa/cast/dyncast.
410 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
411   return D->getTag() == DW_TAG_enumerator;
412 }
413
414 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
415 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
416   DebugInfoDesc::ApplyToFields(Visitor);
417   Visitor->Apply(Name);
418   Visitor->Apply(Value);
419 }
420
421 #ifndef NDEBUG
422 void EnumeratorDesc::dump() {
423   cerr << getDescString() << " "
424        << "Version(" << getVersion() << "), "
425        << "Tag(" << getTag() << "), "
426        << "Name(" << Name << "), "
427        << "Value(" << Value << ")\n";
428 }
429 #endif
430
431 //===----------------------------------------------------------------------===//
432
433 VariableDesc::VariableDesc(unsigned T)
434   : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), TyDesc(0) {
435 }
436
437 // Implement isa/cast/dyncast.
438 bool VariableDesc::classof(const DebugInfoDesc *D) {
439   unsigned T =  D->getTag();
440   switch (T) {
441   case DW_TAG_auto_variable:
442   case DW_TAG_arg_variable:
443   case DW_TAG_return_variable:
444     return true;
445   default: break;
446   }
447   return false;
448 }
449
450 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
451 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
452   DebugInfoDesc::ApplyToFields(Visitor);
453   Visitor->Apply(Context);
454   Visitor->Apply(Name);
455   Visitor->Apply(File);
456   Visitor->Apply(Line);
457   Visitor->Apply(TyDesc);
458 }
459
460 #ifndef NDEBUG
461 void VariableDesc::dump() {
462   cerr << getDescString() << " "
463        << "Version(" << getVersion() << "), "
464        << "Tag(" << getTag() << "), "
465        << "Context(" << Context << "), "
466        << "Name(\"" << Name << "\"), "
467        << "File(" << File << "), "
468        << "Line(" << Line << "), "
469        << "TyDesc(" << TyDesc << ")\n";
470 }
471 #endif
472
473 //===----------------------------------------------------------------------===//
474
475 GlobalDesc::GlobalDesc(unsigned T)
476   : AnchoredDesc(T), Context(0), Name(""), FullName(""), LinkageName(""),
477     File(NULL), Line(0), TyDesc(NULL), IsStatic(false), IsDefinition(false) {
478 }
479
480 /// ApplyToFields - Target the visitor to the fields of the global.
481 ///
482 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
483   AnchoredDesc::ApplyToFields(Visitor);
484   Visitor->Apply(Context);
485   Visitor->Apply(Name);
486   Visitor->Apply(FullName);
487   Visitor->Apply(LinkageName);
488   Visitor->Apply(File);
489   Visitor->Apply(Line);
490   Visitor->Apply(TyDesc);
491   Visitor->Apply(IsStatic);
492   Visitor->Apply(IsDefinition);
493 }
494
495 //===----------------------------------------------------------------------===//
496
497 GlobalVariableDesc::GlobalVariableDesc()
498   : GlobalDesc(DW_TAG_variable), Global(NULL) {
499 }
500
501 // Implement isa/cast/dyncast.
502 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
503   return D->getTag() == DW_TAG_variable; 
504 }
505
506 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
507 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
508   GlobalDesc::ApplyToFields(Visitor);
509   Visitor->Apply(Global);
510 }
511
512 #ifndef NDEBUG
513 void GlobalVariableDesc::dump() {
514   cerr << getDescString() << " "
515        << "Version(" << getVersion() << "), "
516        << "Tag(" << getTag() << "), "
517        << "Anchor(" << getAnchor() << "), "
518        << "Name(\"" << getName() << "\"), "
519        << "FullName(\"" << getFullName() << "\"), "
520        << "LinkageName(\"" << getLinkageName() << "\"), "
521        << "File(" << getFile() << "),"
522        << "Line(" << getLine() << "),"
523        << "Type(" << getType() << "), "
524        << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
525        << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
526        << "Global(" << Global << ")\n";
527 }
528 #endif
529
530 //===----------------------------------------------------------------------===//
531
532 SubprogramDesc::SubprogramDesc()
533   : GlobalDesc(DW_TAG_subprogram) {
534 }
535
536 // Implement isa/cast/dyncast.
537 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
538   return D->getTag() == DW_TAG_subprogram;
539 }
540
541 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
542 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
543   GlobalDesc::ApplyToFields(Visitor);
544 }
545
546 #ifndef NDEBUG
547 void SubprogramDesc::dump() {
548   cerr << getDescString() << " "
549        << "Version(" << getVersion() << "), "
550        << "Tag(" << getTag() << "), "
551        << "Anchor(" << getAnchor() << "), "
552        << "Name(\"" << getName() << "\"), "
553        << "FullName(\"" << getFullName() << "\"), "
554        << "LinkageName(\"" << getLinkageName() << "\"), "
555        << "File(" << getFile() << "),"
556        << "Line(" << getLine() << "),"
557        << "Type(" << getType() << "), "
558        << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
559        << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
560 }
561 #endif
562
563 //===----------------------------------------------------------------------===//
564
565 BlockDesc::BlockDesc()
566   : DebugInfoDesc(DW_TAG_lexical_block), Context(NULL) {
567 }
568
569 // Implement isa/cast/dyncast.
570 bool BlockDesc::classof(const DebugInfoDesc *D) {
571   return D->getTag() == DW_TAG_lexical_block;
572 }
573
574 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
575 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
576   DebugInfoDesc::ApplyToFields(Visitor);
577
578   Visitor->Apply(Context);
579 }
580
581 #ifndef NDEBUG
582 void BlockDesc::dump() {
583   cerr << getDescString() << " "
584        << "Version(" << getVersion() << "), "
585        << "Tag(" << getTag() << "),"
586        << "Context(" << Context << ")\n";
587 }
588 #endif