Rename ConstantVec -> BUILD_VECTOR and VConstant -> VBUILD_VECTOR. Allow*BUILD_VECTO...
[oota-llvm.git] / lib / CodeGen / MachineDebugInfo.cpp
1 //===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/CodeGen/MachineDebugInfo.h"
11
12 #include "llvm/Constants.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/GlobalVariable.h"
15 #include "llvm/Intrinsics.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Module.h"
18 #include "llvm/Support/Dwarf.h"
19
20 #include <iostream>
21
22 using namespace llvm;
23 using namespace llvm::dwarf;
24
25 // Handle the Pass registration stuff necessary to use TargetData's.
26 namespace {
27   RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
28 }
29
30 //===----------------------------------------------------------------------===//
31
32 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
33 /// specified value in their initializer somewhere.
34 static void
35 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
36   // Scan though value users.
37   for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
38     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
39       // If the user is a GlobalVariable then add to result.
40       Result.push_back(GV);
41     } else if (Constant *C = dyn_cast<Constant>(*I)) {
42       // If the user is a constant variable then scan its users
43       getGlobalVariablesUsing(C, Result);
44     }
45   }
46 }
47
48 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
49 /// named GlobalVariable.
50 static std::vector<GlobalVariable*>
51 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
52   std::vector<GlobalVariable*> Result;  // GlobalVariables matching criteria.
53   
54   std::vector<const Type*> FieldTypes;
55   FieldTypes.push_back(Type::UIntTy);
56   FieldTypes.push_back(Type::UIntTy);
57
58   // Get the GlobalVariable root.
59   GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
60                                                 StructType::get(FieldTypes));
61
62   // If present and linkonce then scan for users.
63   if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
64     getGlobalVariablesUsing(UseRoot, Result);
65   }
66   
67   return Result;
68 }
69   
70 /// isStringValue - Return true if the given value can be coerced to a string.
71 ///
72 static bool isStringValue(Value *V) {
73   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
74     if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
75       ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
76       return Init->isString();
77     }
78   } else if (Constant *C = dyn_cast<Constant>(V)) {
79     if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
80       return isStringValue(GV);
81     else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
82       if (CE->getOpcode() == Instruction::GetElementPtr) {
83         if (CE->getNumOperands() == 3 &&
84             cast<Constant>(CE->getOperand(1))->isNullValue() &&
85             isa<ConstantInt>(CE->getOperand(2))) {
86           return isStringValue(CE->getOperand(0));
87         }
88       }
89     }
90   }
91   return false;
92 }
93
94 /// getGlobalVariable - Return either a direct or cast Global value.
95 ///
96 static GlobalVariable *getGlobalVariable(Value *V) {
97   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
98     return GV;
99   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
100     if (CE->getOpcode() == Instruction::Cast) {
101       return dyn_cast<GlobalVariable>(CE->getOperand(0));
102     }
103   }
104   return NULL;
105 }
106
107 /// isGlobalVariable - Return true if the given value can be coerced to a
108 /// GlobalVariable.
109 static bool isGlobalVariable(Value *V) {
110   if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
111     return true;
112   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
113     if (CE->getOpcode() == Instruction::Cast) {
114       return isa<GlobalVariable>(CE->getOperand(0));
115     }
116   }
117   return false;
118 }
119
120 /// getUIntOperand - Return ith operand if it is an unsigned integer.
121 ///
122 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
123   // Make sure the GlobalVariable has an initializer.
124   if (!GV->hasInitializer()) return NULL;
125   
126   // Get the initializer constant.
127   ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
128   if (!CI) return NULL;
129   
130   // Check if there is at least i + 1 operands.
131   unsigned N = CI->getNumOperands();
132   if (i >= N) return NULL;
133
134   // Check constant.
135   return dyn_cast<ConstantUInt>(CI->getOperand(i));
136 }
137 //===----------------------------------------------------------------------===//
138
139 /// ApplyToFields - Target the visitor to each field of the debug information
140 /// descriptor.
141 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
142   DD->ApplyToFields(this);
143 }
144
145 //===----------------------------------------------------------------------===//
146 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
147 /// the supplied DebugInfoDesc.
148 class DICountVisitor : public DIVisitor {
149 private:
150   unsigned Count;                       // Running count of fields.
151   
152 public:
153   DICountVisitor() : DIVisitor(), Count(0) {}
154   
155   // Accessors.
156   unsigned getCount() const { return Count; }
157   
158   /// Apply - Count each of the fields.
159   ///
160   virtual void Apply(int &Field)             { ++Count; }
161   virtual void Apply(unsigned &Field)        { ++Count; }
162   virtual void Apply(int64_t &Field)         { ++Count; }
163   virtual void Apply(uint64_t &Field)        { ++Count; }
164   virtual void Apply(bool &Field)            { ++Count; }
165   virtual void Apply(std::string &Field)     { ++Count; }
166   virtual void Apply(DebugInfoDesc *&Field)  { ++Count; }
167   virtual void Apply(GlobalVariable *&Field) { ++Count; }
168   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
169     ++Count;
170   }
171 };
172
173 //===----------------------------------------------------------------------===//
174 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
175 /// supplied DebugInfoDesc.
176 class DIDeserializeVisitor : public DIVisitor {
177 private:
178   DIDeserializer &DR;                   // Active deserializer.
179   unsigned I;                           // Current operand index.
180   ConstantStruct *CI;                   // GlobalVariable constant initializer.
181
182 public:
183   DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
184   : DIVisitor()
185   , DR(D)
186   , I(0)
187   , CI(cast<ConstantStruct>(GV->getInitializer()))
188   {}
189   
190   /// Apply - Set the value of each of the fields.
191   ///
192   virtual void Apply(int &Field) {
193     Constant *C = CI->getOperand(I++);
194     Field = cast<ConstantSInt>(C)->getValue();
195   }
196   virtual void Apply(unsigned &Field) {
197     Constant *C = CI->getOperand(I++);
198     Field = cast<ConstantUInt>(C)->getValue();
199   }
200   virtual void Apply(int64_t &Field) {
201     Constant *C = CI->getOperand(I++);
202     Field = cast<ConstantSInt>(C)->getValue();
203   }
204   virtual void Apply(uint64_t &Field) {
205     Constant *C = CI->getOperand(I++);
206     Field = cast<ConstantUInt>(C)->getValue();
207   }
208   virtual void Apply(bool &Field) {
209     Constant *C = CI->getOperand(I++);
210     Field = cast<ConstantBool>(C)->getValue();
211   }
212   virtual void Apply(std::string &Field) {
213     Constant *C = CI->getOperand(I++);
214     Field = C->getStringValue();
215   }
216   virtual void Apply(DebugInfoDesc *&Field) {
217     Constant *C = CI->getOperand(I++);
218     Field = DR.Deserialize(C);
219   }
220   virtual void Apply(GlobalVariable *&Field) {
221     Constant *C = CI->getOperand(I++);
222     Field = getGlobalVariable(C);
223   }
224   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
225     Constant *C = CI->getOperand(I++);
226     GlobalVariable *GV = getGlobalVariable(C);
227     Field.resize(0);
228     // Have to be able to deal with the empty array case (zero initializer)
229     if (!GV->hasInitializer()) return;
230     if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
231       for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
232         GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
233         DebugInfoDesc *DE = DR.Deserialize(GVE);
234         Field.push_back(DE);
235       }
236     }
237   }
238 };
239
240 //===----------------------------------------------------------------------===//
241 /// DISerializeVisitor - This DIVisitor serializes all the fields in
242 /// the supplied DebugInfoDesc.
243 class DISerializeVisitor : public DIVisitor {
244 private:
245   DISerializer &SR;                     // Active serializer.
246   std::vector<Constant*> &Elements;     // Element accumulator.
247   
248 public:
249   DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
250   : DIVisitor()
251   , SR(S)
252   , Elements(E)
253   {}
254   
255   /// Apply - Set the value of each of the fields.
256   ///
257   virtual void Apply(int &Field) {
258     Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
259   }
260   virtual void Apply(unsigned &Field) {
261     Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
262   }
263   virtual void Apply(int64_t &Field) {
264     Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
265   }
266   virtual void Apply(uint64_t &Field) {
267     Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
268   }
269   virtual void Apply(bool &Field) {
270     Elements.push_back(ConstantBool::get(Field));
271   }
272   virtual void Apply(std::string &Field) {
273       Elements.push_back(SR.getString(Field));
274   }
275   virtual void Apply(DebugInfoDesc *&Field) {
276     GlobalVariable *GV = NULL;
277     
278     // If non-NULL then convert to global.
279     if (Field) GV = SR.Serialize(Field);
280     
281     // FIXME - At some point should use specific type.
282     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
283     
284     if (GV) {
285       // Set to pointer to global.
286       Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
287     } else {
288       // Use NULL.
289       Elements.push_back(ConstantPointerNull::get(EmptyTy));
290     }
291   }
292   virtual void Apply(GlobalVariable *&Field) {
293     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
294     if (Field) {
295       Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
296     } else {
297       Elements.push_back(ConstantPointerNull::get(EmptyTy));
298     }
299   }
300   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
301     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
302     unsigned N = Field.size();
303     ArrayType *AT = ArrayType::get(EmptyTy, N);
304     std::vector<Constant *> ArrayElements;
305
306     for (unsigned i = 0, N = Field.size(); i < N; ++i) {
307       GlobalVariable *GVE = SR.Serialize(Field[i]);
308       Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
309       ArrayElements.push_back(cast<Constant>(CE));
310     }
311     
312     Constant *CA = ConstantArray::get(AT, ArrayElements);
313     GlobalVariable *CAGV = new GlobalVariable(AT, true,
314                                               GlobalValue::InternalLinkage,
315                                               CA, "llvm.dbg.array",
316                                               SR.getModule());
317     CAGV->setSection("llvm.metadata");
318     Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
319     Elements.push_back(CAE);
320   }
321 };
322
323 //===----------------------------------------------------------------------===//
324 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
325 /// the supplied DebugInfoDesc.
326 class DIGetTypesVisitor : public DIVisitor {
327 private:
328   DISerializer &SR;                     // Active serializer.
329   std::vector<const Type*> &Fields;     // Type accumulator.
330   
331 public:
332   DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
333   : DIVisitor()
334   , SR(S)
335   , Fields(F)
336   {}
337   
338   /// Apply - Set the value of each of the fields.
339   ///
340   virtual void Apply(int &Field) {
341     Fields.push_back(Type::IntTy);
342   }
343   virtual void Apply(unsigned &Field) {
344     Fields.push_back(Type::UIntTy);
345   }
346   virtual void Apply(int64_t &Field) {
347     Fields.push_back(Type::IntTy);
348   }
349   virtual void Apply(uint64_t &Field) {
350     Fields.push_back(Type::UIntTy);
351   }
352   virtual void Apply(bool &Field) {
353     Fields.push_back(Type::BoolTy);
354   }
355   virtual void Apply(std::string &Field) {
356     Fields.push_back(SR.getStrPtrType());
357   }
358   virtual void Apply(DebugInfoDesc *&Field) {
359     // FIXME - At some point should use specific type.
360     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
361     Fields.push_back(EmptyTy);
362   }
363   virtual void Apply(GlobalVariable *&Field) {
364     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
365     Fields.push_back(EmptyTy);
366   }
367   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
368     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
369     Fields.push_back(EmptyTy);
370   }
371 };
372
373 //===----------------------------------------------------------------------===//
374 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
375 /// a constant initializer.
376 class DIVerifyVisitor : public DIVisitor {
377 private:
378   DIVerifier &VR;                       // Active verifier.
379   bool IsValid;                         // Validity status.
380   unsigned I;                           // Current operand index.
381   ConstantStruct *CI;                   // GlobalVariable constant initializer.
382   
383 public:
384   DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
385   : DIVisitor()
386   , VR(V)
387   , IsValid(true)
388   , I(0)
389   , CI(cast<ConstantStruct>(GV->getInitializer()))
390   {
391   }
392   
393   // Accessors.
394   bool isValid() const { return IsValid; }
395   
396   /// Apply - Set the value of each of the fields.
397   ///
398   virtual void Apply(int &Field) {
399     Constant *C = CI->getOperand(I++);
400     IsValid = IsValid && isa<ConstantInt>(C);
401   }
402   virtual void Apply(unsigned &Field) {
403     Constant *C = CI->getOperand(I++);
404     IsValid = IsValid && isa<ConstantInt>(C);
405   }
406   virtual void Apply(int64_t &Field) {
407     Constant *C = CI->getOperand(I++);
408     IsValid = IsValid && isa<ConstantInt>(C);
409   }
410   virtual void Apply(uint64_t &Field) {
411     Constant *C = CI->getOperand(I++);
412     IsValid = IsValid && isa<ConstantInt>(C);
413   }
414   virtual void Apply(bool &Field) {
415     Constant *C = CI->getOperand(I++);
416     IsValid = IsValid && isa<ConstantBool>(C);
417   }
418   virtual void Apply(std::string &Field) {
419     Constant *C = CI->getOperand(I++);
420     IsValid = IsValid && (!C || isStringValue(C));
421   }
422   virtual void Apply(DebugInfoDesc *&Field) {
423     // FIXME - Prepare the correct descriptor.
424     Constant *C = CI->getOperand(I++);
425     IsValid = IsValid && isGlobalVariable(C);
426   }
427   virtual void Apply(GlobalVariable *&Field) {
428     Constant *C = CI->getOperand(I++);
429     IsValid = IsValid && isGlobalVariable(C);
430   }
431   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
432     Constant *C = CI->getOperand(I++);
433     IsValid = IsValid && isGlobalVariable(C);
434     if (!IsValid) return;
435
436     GlobalVariable *GV = getGlobalVariable(C);
437     IsValid = IsValid && GV && GV->hasInitializer();
438     if (!IsValid) return;
439     
440     ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
441     IsValid = IsValid && CA;
442     if (!IsValid) return;
443
444     for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
445       IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
446       if (!IsValid) return;
447     
448       GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
449       VR.Verify(GVE);
450     }
451   }
452 };
453
454
455 //===----------------------------------------------------------------------===//
456
457 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
458 /// GlobalVariable.  
459 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
460   ConstantUInt *C = getUIntOperand(GV, 0);
461   return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
462 }
463
464 /// DescFactory - Create an instance of debug info descriptor based on Tag.
465 /// Return NULL if not a recognized Tag.
466 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
467   switch (Tag) {
468   case DW_TAG_anchor:           return new AnchorDesc();
469   case DW_TAG_compile_unit:     return new CompileUnitDesc();
470   case DW_TAG_variable:         return new GlobalVariableDesc();
471   case DW_TAG_subprogram:       return new SubprogramDesc();
472   case DW_TAG_lexical_block:    return new BlockDesc();
473   case DW_TAG_base_type:        return new BasicTypeDesc();
474   case DW_TAG_typedef:
475   case DW_TAG_pointer_type:        
476   case DW_TAG_reference_type:
477   case DW_TAG_const_type:
478   case DW_TAG_volatile_type:        
479   case DW_TAG_restrict_type:
480   case DW_TAG_formal_parameter:
481   case DW_TAG_member:           return new DerivedTypeDesc(Tag);
482   case DW_TAG_array_type:
483   case DW_TAG_structure_type:
484   case DW_TAG_union_type:
485   case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
486   case DW_TAG_subrange_type:    return new SubrangeDesc();
487   case DW_TAG_enumerator:       return new EnumeratorDesc();
488   default: break;
489   }
490   return NULL;
491 }
492
493 /// getLinkage - get linkage appropriate for this type of descriptor.
494 ///
495 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
496   return GlobalValue::InternalLinkage;
497 }
498
499 /// ApplyToFields - Target the vistor to the fields of the descriptor.
500 ///
501 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
502   Visitor->Apply(Tag);
503 }
504
505 //===----------------------------------------------------------------------===//
506
507 AnchorDesc::AnchorDesc()
508 : DebugInfoDesc(DW_TAG_anchor)
509 , AnchorTag(0)
510 {}
511 AnchorDesc::AnchorDesc(AnchoredDesc *D)
512 : DebugInfoDesc(DW_TAG_anchor)
513 , AnchorTag(D->getTag())
514 {}
515
516 // Implement isa/cast/dyncast.
517 bool AnchorDesc::classof(const DebugInfoDesc *D) {
518   return D->getTag() == DW_TAG_anchor;
519 }
520   
521 /// getLinkage - get linkage appropriate for this type of descriptor.
522 ///
523 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
524   return GlobalValue::LinkOnceLinkage;
525 }
526
527 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
528 ///
529 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
530   DebugInfoDesc::ApplyToFields(Visitor);
531   
532   Visitor->Apply(AnchorTag);
533 }
534
535 /// getDescString - Return a string used to compose global names and labels. A
536 /// A global variable name needs to be defined for each debug descriptor that is
537 /// anchored. NOTE: that each global variable named here also needs to be added
538 /// to the list of names left external in the internalizer.
539 ///   ExternalNames.insert("llvm.dbg.compile_units");
540 ///   ExternalNames.insert("llvm.dbg.global_variables");
541 ///   ExternalNames.insert("llvm.dbg.subprograms");
542 const char *AnchorDesc::getDescString() const {
543   switch (AnchorTag) {
544   case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
545   case DW_TAG_variable:     return GlobalVariableDesc::AnchorString;
546   case DW_TAG_subprogram:   return SubprogramDesc::AnchorString;
547   default: break;
548   }
549
550   assert(0 && "Tag does not have a case for anchor string");
551   return "";
552 }
553
554 /// getTypeString - Return a string used to label this descriptors type.
555 ///
556 const char *AnchorDesc::getTypeString() const {
557   return "llvm.dbg.anchor.type";
558 }
559
560 #ifndef NDEBUG
561 void AnchorDesc::dump() {
562   std::cerr << getDescString() << " "
563             << "Tag(" << getTag() << "), "
564             << "AnchorTag(" << AnchorTag << ")\n";
565 }
566 #endif
567
568 //===----------------------------------------------------------------------===//
569
570 AnchoredDesc::AnchoredDesc(unsigned T)
571 : DebugInfoDesc(T)
572 , Anchor(NULL)
573 {}
574
575 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
576 ///
577 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
578   DebugInfoDesc::ApplyToFields(Visitor);
579
580   Visitor->Apply((DebugInfoDesc *&)Anchor);
581 }
582
583 //===----------------------------------------------------------------------===//
584
585 CompileUnitDesc::CompileUnitDesc()
586 : AnchoredDesc(DW_TAG_compile_unit)
587 , DebugVersion(LLVMDebugVersion)
588 , Language(0)
589 , FileName("")
590 , Directory("")
591 , Producer("")
592 {}
593
594 // Implement isa/cast/dyncast.
595 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
596   return D->getTag() == DW_TAG_compile_unit;
597 }
598
599 /// DebugVersionFromGlobal - Returns the version number from a compile unit
600 /// GlobalVariable.
601 unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
602   ConstantUInt *C = getUIntOperand(GV, 2);
603   return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
604 }
605   
606 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
607 ///
608 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
609   AnchoredDesc::ApplyToFields(Visitor);
610
611   Visitor->Apply(DebugVersion);
612   Visitor->Apply(Language);
613   Visitor->Apply(FileName);
614   Visitor->Apply(Directory);
615   Visitor->Apply(Producer);
616 }
617
618 /// getDescString - Return a string used to compose global names and labels.
619 ///
620 const char *CompileUnitDesc::getDescString() const {
621   return "llvm.dbg.compile_unit";
622 }
623
624 /// getTypeString - Return a string used to label this descriptors type.
625 ///
626 const char *CompileUnitDesc::getTypeString() const {
627   return "llvm.dbg.compile_unit.type";
628 }
629
630 /// getAnchorString - Return a string used to label this descriptor's anchor.
631 ///
632 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
633 const char *CompileUnitDesc::getAnchorString() const {
634   return AnchorString;
635 }
636
637 #ifndef NDEBUG
638 void CompileUnitDesc::dump() {
639   std::cerr << getDescString() << " "
640             << "Tag(" << getTag() << "), "
641             << "Anchor(" << getAnchor() << "), "
642             << "DebugVersion(" << DebugVersion << "), "
643             << "Language(" << Language << "), "
644             << "FileName(\"" << FileName << "\"), "
645             << "Directory(\"" << Directory << "\"), "
646             << "Producer(\"" << Producer << "\")\n";
647 }
648 #endif
649
650 //===----------------------------------------------------------------------===//
651
652 TypeDesc::TypeDesc(unsigned T)
653 : DebugInfoDesc(T)
654 , Context(NULL)
655 , Name("")
656 , File(NULL)
657 , Size(0)
658 , Align(0)
659 , Offset(0)
660 {}
661
662 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
663 ///
664 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
665   DebugInfoDesc::ApplyToFields(Visitor);
666   
667   Visitor->Apply(Context);
668   Visitor->Apply(Name);
669   Visitor->Apply((DebugInfoDesc *&)File);
670   Visitor->Apply(Line);
671   Visitor->Apply(Size);
672   Visitor->Apply(Align);
673   Visitor->Apply(Offset);
674 }
675
676 /// getDescString - Return a string used to compose global names and labels.
677 ///
678 const char *TypeDesc::getDescString() const {
679   return "llvm.dbg.type";
680 }
681
682 /// getTypeString - Return a string used to label this descriptor's type.
683 ///
684 const char *TypeDesc::getTypeString() const {
685   return "llvm.dbg.type.type";
686 }
687
688 #ifndef NDEBUG
689 void TypeDesc::dump() {
690   std::cerr << getDescString() << " "
691             << "Tag(" << getTag() << "), "
692             << "Context(" << Context << "), "
693             << "Name(\"" << Name << "\"), "
694             << "File(" << File << "), "
695             << "Line(" << Line << "), "
696             << "Size(" << Size << "), "
697             << "Align(" << Align << "), "
698             << "Offset(" << Offset << ")\n";
699 }
700 #endif
701
702 //===----------------------------------------------------------------------===//
703
704 BasicTypeDesc::BasicTypeDesc()
705 : TypeDesc(DW_TAG_base_type)
706 , Encoding(0)
707 {}
708
709 // Implement isa/cast/dyncast.
710 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
711   return D->getTag() == DW_TAG_base_type;
712 }
713
714 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
715 ///
716 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
717   TypeDesc::ApplyToFields(Visitor);
718   
719   Visitor->Apply(Encoding);
720 }
721
722 /// getDescString - Return a string used to compose global names and labels.
723 ///
724 const char *BasicTypeDesc::getDescString() const {
725   return "llvm.dbg.basictype";
726 }
727
728 /// getTypeString - Return a string used to label this descriptor's type.
729 ///
730 const char *BasicTypeDesc::getTypeString() const {
731   return "llvm.dbg.basictype.type";
732 }
733
734 #ifndef NDEBUG
735 void BasicTypeDesc::dump() {
736   std::cerr << getDescString() << " "
737             << "Tag(" << getTag() << "), "
738             << "Context(" << getContext() << "), "
739             << "Name(\"" << getName() << "\"), "
740             << "Size(" << getSize() << "), "
741             << "Encoding(" << Encoding << ")\n";
742 }
743 #endif
744
745 //===----------------------------------------------------------------------===//
746
747 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
748 : TypeDesc(T)
749 , FromType(NULL)
750 {}
751
752 // Implement isa/cast/dyncast.
753 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
754   unsigned T =  D->getTag();
755   switch (T) {
756   case DW_TAG_typedef:
757   case DW_TAG_pointer_type:
758   case DW_TAG_reference_type:
759   case DW_TAG_const_type:
760   case DW_TAG_volatile_type:
761   case DW_TAG_restrict_type:
762   case DW_TAG_formal_parameter:
763   case DW_TAG_member:
764     return true;
765   default: break;
766   }
767   return false;
768 }
769
770 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
771 ///
772 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
773   TypeDesc::ApplyToFields(Visitor);
774   
775   Visitor->Apply((DebugInfoDesc *&)FromType);
776 }
777
778 /// getDescString - Return a string used to compose global names and labels.
779 ///
780 const char *DerivedTypeDesc::getDescString() const {
781   return "llvm.dbg.derivedtype";
782 }
783
784 /// getTypeString - Return a string used to label this descriptor's type.
785 ///
786 const char *DerivedTypeDesc::getTypeString() const {
787   return "llvm.dbg.derivedtype.type";
788 }
789
790 #ifndef NDEBUG
791 void DerivedTypeDesc::dump() {
792   std::cerr << getDescString() << " "
793             << "Tag(" << getTag() << "), "
794             << "Context(" << getContext() << "), "
795             << "Name(\"" << getName() << "\"), "
796             << "Size(" << getSize() << "), "
797             << "File(" << getFile() << "), "
798             << "Line(" << getLine() << "), "
799             << "FromType(" << FromType << ")\n";
800 }
801 #endif
802
803 //===----------------------------------------------------------------------===//
804
805 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
806 : DerivedTypeDesc(T)
807 , Elements()
808 {}
809   
810 // Implement isa/cast/dyncast.
811 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
812   unsigned T =  D->getTag();
813   switch (T) {
814   case DW_TAG_array_type:
815   case DW_TAG_structure_type:
816   case DW_TAG_union_type:
817   case DW_TAG_enumeration_type:
818     return true;
819   default: break;
820   }
821   return false;
822 }
823
824 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
825 ///
826 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
827   DerivedTypeDesc::ApplyToFields(Visitor);
828   
829   Visitor->Apply(Elements);
830 }
831
832 /// getDescString - Return a string used to compose global names and labels.
833 ///
834 const char *CompositeTypeDesc::getDescString() const {
835   return "llvm.dbg.compositetype";
836 }
837
838 /// getTypeString - Return a string used to label this descriptor's type.
839 ///
840 const char *CompositeTypeDesc::getTypeString() const {
841   return "llvm.dbg.compositetype.type";
842 }
843
844 #ifndef NDEBUG
845 void CompositeTypeDesc::dump() {
846   std::cerr << getDescString() << " "
847             << "Tag(" << getTag() << "), "
848             << "Context(" << getContext() << "), "
849             << "Name(\"" << getName() << "\"), "
850             << "Size(" << getSize() << "), "
851             << "File(" << getFile() << "), "
852             << "Line(" << getLine() << "), "
853             << "FromType(" << getFromType() << "), "
854             << "Elements.size(" << Elements.size() << ")\n";
855 }
856 #endif
857
858 //===----------------------------------------------------------------------===//
859
860 SubrangeDesc::SubrangeDesc()
861 : DebugInfoDesc(DW_TAG_subrange_type)
862 , Lo(0)
863 , Hi(0)
864 {}
865
866 // Implement isa/cast/dyncast.
867 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
868   return D->getTag() == DW_TAG_subrange_type;
869 }
870
871 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
872 ///
873 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
874   DebugInfoDesc::ApplyToFields(Visitor);
875
876   Visitor->Apply(Lo);
877   Visitor->Apply(Hi);
878 }
879
880 /// getDescString - Return a string used to compose global names and labels.
881 ///
882 const char *SubrangeDesc::getDescString() const {
883   return "llvm.dbg.subrange";
884 }
885   
886 /// getTypeString - Return a string used to label this descriptor's type.
887 ///
888 const char *SubrangeDesc::getTypeString() const {
889   return "llvm.dbg.subrange.type";
890 }
891
892 #ifndef NDEBUG
893 void SubrangeDesc::dump() {
894   std::cerr << getDescString() << " "
895             << "Tag(" << getTag() << "), "
896             << "Lo(" << Lo << "), "
897             << "Hi(" << Hi << ")\n";
898 }
899 #endif
900
901 //===----------------------------------------------------------------------===//
902
903 EnumeratorDesc::EnumeratorDesc()
904 : DebugInfoDesc(DW_TAG_enumerator)
905 , Name("")
906 , Value(0)
907 {}
908
909 // Implement isa/cast/dyncast.
910 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
911   return D->getTag() == DW_TAG_enumerator;
912 }
913
914 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
915 ///
916 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
917   DebugInfoDesc::ApplyToFields(Visitor);
918
919   Visitor->Apply(Name);
920   Visitor->Apply(Value);
921 }
922
923 /// getDescString - Return a string used to compose global names and labels.
924 ///
925 const char *EnumeratorDesc::getDescString() const {
926   return "llvm.dbg.enumerator";
927 }
928   
929 /// getTypeString - Return a string used to label this descriptor's type.
930 ///
931 const char *EnumeratorDesc::getTypeString() const {
932   return "llvm.dbg.enumerator.type";
933 }
934
935 #ifndef NDEBUG
936 void EnumeratorDesc::dump() {
937   std::cerr << getDescString() << " "
938             << "Tag(" << getTag() << "), "
939             << "Name(" << Name << "), "
940             << "Value(" << Value << ")\n";
941 }
942 #endif
943
944 //===----------------------------------------------------------------------===//
945
946 GlobalDesc::GlobalDesc(unsigned T)
947 : AnchoredDesc(T)
948 , Context(0)
949 , Name("")
950 , File(NULL)
951 , Line(0)
952 , TyDesc(NULL)
953 , IsStatic(false)
954 , IsDefinition(false)
955 {}
956
957 /// ApplyToFields - Target the visitor to the fields of the global.
958 ///
959 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
960   AnchoredDesc::ApplyToFields(Visitor);
961
962   Visitor->Apply(Context);
963   Visitor->Apply(Name);
964   Visitor->Apply((DebugInfoDesc *&)File);
965   Visitor->Apply(Line);
966   Visitor->Apply((DebugInfoDesc *&)TyDesc);
967   Visitor->Apply(IsStatic);
968   Visitor->Apply(IsDefinition);
969 }
970
971 //===----------------------------------------------------------------------===//
972
973 GlobalVariableDesc::GlobalVariableDesc()
974 : GlobalDesc(DW_TAG_variable)
975 , Global(NULL)
976 {}
977
978 // Implement isa/cast/dyncast.
979 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
980   return D->getTag() == DW_TAG_variable; 
981 }
982
983 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
984 ///
985 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
986   GlobalDesc::ApplyToFields(Visitor);
987
988   Visitor->Apply(Global);
989 }
990
991 /// getDescString - Return a string used to compose global names and labels.
992 ///
993 const char *GlobalVariableDesc::getDescString() const {
994   return "llvm.dbg.global_variable";
995 }
996
997 /// getTypeString - Return a string used to label this descriptors type.
998 ///
999 const char *GlobalVariableDesc::getTypeString() const {
1000   return "llvm.dbg.global_variable.type";
1001 }
1002
1003 /// getAnchorString - Return a string used to label this descriptor's anchor.
1004 ///
1005 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1006 const char *GlobalVariableDesc::getAnchorString() const {
1007   return AnchorString;
1008 }
1009
1010 #ifndef NDEBUG
1011 void GlobalVariableDesc::dump() {
1012   std::cerr << getDescString() << " "
1013             << "Tag(" << getTag() << "), "
1014             << "Anchor(" << getAnchor() << "), "
1015             << "Name(\"" << getName() << "\"), "
1016             << "File(" << getFile() << "),"
1017             << "Line(" << getLine() << "),"
1018             << "Type(\"" << getTypeDesc() << "\"), "
1019             << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1020             << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1021             << "Global(" << Global << ")\n";
1022 }
1023 #endif
1024
1025 //===----------------------------------------------------------------------===//
1026
1027 SubprogramDesc::SubprogramDesc()
1028 : GlobalDesc(DW_TAG_subprogram)
1029 {}
1030
1031 // Implement isa/cast/dyncast.
1032 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1033   return D->getTag() == DW_TAG_subprogram;
1034 }
1035
1036 /// ApplyToFields - Target the visitor to the fields of the
1037 /// SubprogramDesc.
1038 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1039   GlobalDesc::ApplyToFields(Visitor);
1040
1041   Visitor->Apply(Elements);
1042 }
1043
1044 /// getDescString - Return a string used to compose global names and labels.
1045 ///
1046 const char *SubprogramDesc::getDescString() const {
1047   return "llvm.dbg.subprogram";
1048 }
1049
1050 /// getTypeString - Return a string used to label this descriptors type.
1051 ///
1052 const char *SubprogramDesc::getTypeString() const {
1053   return "llvm.dbg.subprogram.type";
1054 }
1055
1056 /// getAnchorString - Return a string used to label this descriptor's anchor.
1057 ///
1058 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1059 const char *SubprogramDesc::getAnchorString() const {
1060   return AnchorString;
1061 }
1062
1063 #ifndef NDEBUG
1064 void SubprogramDesc::dump() {
1065   std::cerr << getDescString() << " "
1066             << "Tag(" << getTag() << "), "
1067             << "Anchor(" << getAnchor() << "), "
1068             << "Name(\"" << getName() << "\"), "
1069             << "File(" << getFile() << "),"
1070             << "Line(" << getLine() << "),"
1071             << "Type(\"" << getTypeDesc() << "\"), "
1072             << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1073             << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1074 }
1075 #endif
1076
1077 //===----------------------------------------------------------------------===//
1078
1079 BlockDesc::BlockDesc()
1080 : DebugInfoDesc(DW_TAG_lexical_block)
1081 {}
1082
1083 // Implement isa/cast/dyncast.
1084 bool BlockDesc::classof(const DebugInfoDesc *D) {
1085   return D->getTag() == DW_TAG_lexical_block;
1086 }
1087
1088 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1089 ///
1090 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1091   DebugInfoDesc::ApplyToFields(Visitor);
1092
1093   Visitor->Apply(Elements);
1094 }
1095
1096 /// getDescString - Return a string used to compose global names and labels.
1097 ///
1098 const char *BlockDesc::getDescString() const {
1099   return "llvm.dbg.block";
1100 }
1101
1102 /// getTypeString - Return a string used to label this descriptors type.
1103 ///
1104 const char *BlockDesc::getTypeString() const {
1105   return "llvm.dbg.block.type";
1106 }
1107
1108 #ifndef NDEBUG
1109 void BlockDesc::dump() {
1110   std::cerr << getDescString() << " "
1111             << "Tag(" << getTag() << ")\n";
1112 }
1113 #endif
1114
1115 //===----------------------------------------------------------------------===//
1116
1117 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1118   return Deserialize(getGlobalVariable(V));
1119 }
1120 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1121   // Handle NULL.
1122   if (!GV) return NULL;
1123
1124   // Check to see if it has been already deserialized.
1125   DebugInfoDesc *&Slot = GlobalDescs[GV];
1126   if (Slot) return Slot;
1127
1128   // Get the Tag from the global.
1129   unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1130   
1131   // Get the debug version if a compile unit.
1132   if (Tag == DW_TAG_compile_unit) {
1133     DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1134   }
1135   
1136   // Create an empty instance of the correct sort.
1137   Slot = DebugInfoDesc::DescFactory(Tag);
1138   
1139   // If not a user defined descriptor.
1140   if (Slot) {
1141     // Deserialize the fields.
1142     DIDeserializeVisitor DRAM(*this, GV);
1143     DRAM.ApplyToFields(Slot);
1144   }
1145   
1146   return Slot;
1147 }
1148
1149 //===----------------------------------------------------------------------===//
1150
1151 /// getStrPtrType - Return a "sbyte *" type.
1152 ///
1153 const PointerType *DISerializer::getStrPtrType() {
1154   // If not already defined.
1155   if (!StrPtrTy) {
1156     // Construct the pointer to signed bytes.
1157     StrPtrTy = PointerType::get(Type::SByteTy);
1158   }
1159   
1160   return StrPtrTy;
1161 }
1162
1163 /// getEmptyStructPtrType - Return a "{ }*" type.
1164 ///
1165 const PointerType *DISerializer::getEmptyStructPtrType() {
1166   // If not already defined.
1167   if (!EmptyStructPtrTy) {
1168     // Construct the empty structure type.
1169     const StructType *EmptyStructTy =
1170                                     StructType::get(std::vector<const Type*>());
1171     // Construct the pointer to empty structure type.
1172     EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1173   }
1174   
1175   return EmptyStructPtrTy;
1176 }
1177
1178 /// getTagType - Return the type describing the specified descriptor (via tag.)
1179 ///
1180 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1181   // Attempt to get the previously defined type.
1182   StructType *&Ty = TagTypes[DD->getTag()];
1183   
1184   // If not already defined.
1185   if (!Ty) {
1186     // Set up fields vector.
1187     std::vector<const Type*> Fields;
1188     // Get types of fields.
1189     DIGetTypesVisitor GTAM(*this, Fields);
1190     GTAM.ApplyToFields(DD);
1191
1192     // Construct structured type.
1193     Ty = StructType::get(Fields);
1194     
1195     // Register type name with module.
1196     M->addTypeName(DD->getTypeString(), Ty);
1197   }
1198   
1199   return Ty;
1200 }
1201
1202 /// getString - Construct the string as constant string global.
1203 ///
1204 Constant *DISerializer::getString(const std::string &String) {
1205   // Check string cache for previous edition.
1206   Constant *&Slot = StringCache[String];
1207   // Return Constant if previously defined.
1208   if (Slot) return Slot;
1209   // If empty string then use a sbyte* null instead.
1210   if (String.empty()) {
1211     Slot = ConstantPointerNull::get(getStrPtrType());
1212   } else {
1213     // Construct string as an llvm constant.
1214     Constant *ConstStr = ConstantArray::get(String);
1215     // Otherwise create and return a new string global.
1216     GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1217                                                GlobalVariable::InternalLinkage,
1218                                                ConstStr, "str", M);
1219     StrGV->setSection("llvm.metadata");
1220     // Convert to generic string pointer.
1221     Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1222   }
1223   return Slot;
1224   
1225 }
1226
1227 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1228 /// so that it can be serialized to a .bc or .ll file.
1229 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1230   // Check if the DebugInfoDesc is already in the map.
1231   GlobalVariable *&Slot = DescGlobals[DD];
1232   
1233   // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1234   if (Slot) return Slot;
1235   
1236   // Get the type associated with the Tag.
1237   const StructType *Ty = getTagType(DD);
1238
1239   // Create the GlobalVariable early to prevent infinite recursion.
1240   GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1241                                           NULL, DD->getDescString(), M);
1242   GV->setSection("llvm.metadata");
1243
1244   // Insert new GlobalVariable in DescGlobals map.
1245   Slot = GV;
1246  
1247   // Set up elements vector
1248   std::vector<Constant*> Elements;
1249   // Add fields.
1250   DISerializeVisitor SRAM(*this, Elements);
1251   SRAM.ApplyToFields(DD);
1252   
1253   // Set the globals initializer.
1254   GV->setInitializer(ConstantStruct::get(Ty, Elements));
1255   
1256   return GV;
1257 }
1258
1259 //===----------------------------------------------------------------------===//
1260
1261 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
1262 /// Mark visited otherwise.
1263 bool DIVerifier::markVisited(GlobalVariable *GV) {
1264   // Check if the GlobalVariable is already in the Visited set.
1265   std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1266   
1267   // See if GlobalVariable exists.
1268   bool Exists = VI != Visited.end() && *VI == GV;
1269
1270   // Insert in set.
1271   if (!Exists) Visited.insert(VI, GV);
1272   
1273   return Exists;
1274 }
1275
1276 /// Verify - Return true if the GlobalVariable appears to be a valid
1277 /// serialization of a DebugInfoDesc.
1278 bool DIVerifier::Verify(Value *V) {
1279   return Verify(getGlobalVariable(V));
1280 }
1281 bool DIVerifier::Verify(GlobalVariable *GV) {
1282   // Check if seen before.
1283   if (markVisited(GV)) return true;
1284   
1285   // Get the Tag
1286   unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1287   if (Tag == DW_TAG_invalid) return false;
1288
1289   // If a compile unit we need the debug version.
1290   if (Tag == DW_TAG_compile_unit) {
1291     DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1292     if (DebugVersion == DW_TAG_invalid) return false;
1293   }
1294
1295   // Construct an empty DebugInfoDesc.
1296   DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1297   
1298   // Allow for user defined descriptors.
1299   if (!DD) return true;
1300   
1301   // Get the initializer constant.
1302   ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1303   
1304   // Get the operand count.
1305   unsigned N = CI->getNumOperands();
1306   
1307   // Get the field count.
1308   unsigned &Slot = Counts[Tag];
1309   if (!Slot) {
1310     // Check the operand count to the field count
1311     DICountVisitor CTAM;
1312     CTAM.ApplyToFields(DD);
1313     Slot = CTAM.getCount();
1314   }
1315   
1316   // Field count must be at most equal operand count.
1317   if (Slot >  N) {
1318     delete DD;
1319     return false;
1320   }
1321   
1322   // Check each field for valid type.
1323   DIVerifyVisitor VRAM(*this, GV);
1324   VRAM.ApplyToFields(DD);
1325   
1326   // Release empty DebugInfoDesc.
1327   delete DD;
1328   
1329   // Return result of field tests.
1330   return VRAM.isValid();
1331 }
1332
1333 //===----------------------------------------------------------------------===//
1334
1335
1336 MachineDebugInfo::MachineDebugInfo()
1337 : DR()
1338 , CompileUnits()
1339 , Directories()
1340 , SourceFiles()
1341 , Lines()
1342 {
1343   
1344 }
1345 MachineDebugInfo::~MachineDebugInfo() {
1346
1347 }
1348
1349 /// doInitialization - Initialize the debug state for a new module.
1350 ///
1351 bool MachineDebugInfo::doInitialization() {
1352   return false;
1353 }
1354
1355 /// doFinalization - Tear down the debug state after completion of a module.
1356 ///
1357 bool MachineDebugInfo::doFinalization() {
1358   return false;
1359 }
1360
1361 /// getDescFor - Convert a Value to a debug information descriptor.
1362 ///
1363 // FIXME - use new Value type when available.
1364 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
1365   return DR.Deserialize(V);
1366 }
1367
1368 /// Verify - Verify that a Value is debug information descriptor.
1369 ///
1370 bool MachineDebugInfo::Verify(Value *V) {
1371   DIVerifier VR;
1372   return VR.Verify(V);
1373 }
1374
1375 /// AnalyzeModule - Scan the module for global debug information.
1376 ///
1377 void MachineDebugInfo::AnalyzeModule(Module &M) {
1378   SetupCompileUnits(M);
1379 }
1380
1381 /// SetupCompileUnits - Set up the unique vector of compile units.
1382 ///
1383 void MachineDebugInfo::SetupCompileUnits(Module &M) {
1384   std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1385   
1386   for (unsigned i = 0, N = CU.size(); i < N; i++) {
1387     CompileUnits.insert(CU[i]);
1388   }
1389 }
1390
1391 /// getCompileUnits - Return a vector of debug compile units.
1392 ///
1393 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
1394   return CompileUnits;
1395 }
1396
1397 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1398 /// named GlobalVariable.
1399 std::vector<GlobalVariable*>
1400 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1401                                           const std::string &RootName) {
1402   return ::getGlobalVariablesUsing(M, RootName);
1403 }