LLVM API Documentation

MachineDebugInfo.cpp

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by James M. Laskey and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/CodeGen/MachineDebugInfo.h"
00011 
00012 #include "llvm/Constants.h"
00013 #include "llvm/CodeGen/MachineLocation.h"
00014 #include "llvm/DerivedTypes.h"
00015 #include "llvm/GlobalVariable.h"
00016 #include "llvm/Intrinsics.h"
00017 #include "llvm/Instructions.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/Support/Dwarf.h"
00020 
00021 #include <iostream>
00022 
00023 using namespace llvm;
00024 using namespace llvm::dwarf;
00025 
00026 // Handle the Pass registration stuff necessary to use TargetData's.
00027 namespace {
00028   RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
00029 }
00030 
00031 //===----------------------------------------------------------------------===//
00032 
00033 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
00034 /// specified value in their initializer somewhere.
00035 static void
00036 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
00037   // Scan though value users.
00038   for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
00039     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
00040       // If the user is a GlobalVariable then add to result.
00041       Result.push_back(GV);
00042     } else if (Constant *C = dyn_cast<Constant>(*I)) {
00043       // If the user is a constant variable then scan its users
00044       getGlobalVariablesUsing(C, Result);
00045     }
00046   }
00047 }
00048 
00049 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
00050 /// named GlobalVariable.
00051 static std::vector<GlobalVariable*>
00052 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
00053   std::vector<GlobalVariable*> Result;  // GlobalVariables matching criteria.
00054   
00055   std::vector<const Type*> FieldTypes;
00056   FieldTypes.push_back(Type::UIntTy);
00057   FieldTypes.push_back(Type::UIntTy);
00058 
00059   // Get the GlobalVariable root.
00060   GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
00061                                                 StructType::get(FieldTypes));
00062 
00063   // If present and linkonce then scan for users.
00064   if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
00065     getGlobalVariablesUsing(UseRoot, Result);
00066   }
00067   
00068   return Result;
00069 }
00070   
00071 /// isStringValue - Return true if the given value can be coerced to a string.
00072 ///
00073 static bool isStringValue(Value *V) {
00074   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
00075     if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
00076       ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
00077       return Init->isString();
00078     }
00079   } else if (Constant *C = dyn_cast<Constant>(V)) {
00080     if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
00081       return isStringValue(GV);
00082     else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00083       if (CE->getOpcode() == Instruction::GetElementPtr) {
00084         if (CE->getNumOperands() == 3 &&
00085             cast<Constant>(CE->getOperand(1))->isNullValue() &&
00086             isa<ConstantInt>(CE->getOperand(2))) {
00087           return isStringValue(CE->getOperand(0));
00088         }
00089       }
00090     }
00091   }
00092   return false;
00093 }
00094 
00095 /// getGlobalVariable - Return either a direct or cast Global value.
00096 ///
00097 static GlobalVariable *getGlobalVariable(Value *V) {
00098   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
00099     return GV;
00100   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
00101     if (CE->getOpcode() == Instruction::Cast) {
00102       return dyn_cast<GlobalVariable>(CE->getOperand(0));
00103     }
00104   }
00105   return NULL;
00106 }
00107 
00108 /// isGlobalVariable - Return true if the given value can be coerced to a
00109 /// GlobalVariable.
00110 static bool isGlobalVariable(Value *V) {
00111   if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
00112     return true;
00113   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
00114     if (CE->getOpcode() == Instruction::Cast) {
00115       return isa<GlobalVariable>(CE->getOperand(0));
00116     }
00117   }
00118   return false;
00119 }
00120 
00121 /// getUIntOperand - Return ith operand if it is an unsigned integer.
00122 ///
00123 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
00124   // Make sure the GlobalVariable has an initializer.
00125   if (!GV->hasInitializer()) return NULL;
00126   
00127   // Get the initializer constant.
00128   ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
00129   if (!CI) return NULL;
00130   
00131   // Check if there is at least i + 1 operands.
00132   unsigned N = CI->getNumOperands();
00133   if (i >= N) return NULL;
00134 
00135   // Check constant.
00136   return dyn_cast<ConstantUInt>(CI->getOperand(i));
00137 }
00138 //===----------------------------------------------------------------------===//
00139 
00140 /// ApplyToFields - Target the visitor to each field of the debug information
00141 /// descriptor.
00142 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
00143   DD->ApplyToFields(this);
00144 }
00145 
00146 //===----------------------------------------------------------------------===//
00147 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
00148 /// the supplied DebugInfoDesc.
00149 class DICountVisitor : public DIVisitor {
00150 private:
00151   unsigned Count;                       // Running count of fields.
00152   
00153 public:
00154   DICountVisitor() : DIVisitor(), Count(0) {}
00155   
00156   // Accessors.
00157   unsigned getCount() const { return Count; }
00158   
00159   /// Apply - Count each of the fields.
00160   ///
00161   virtual void Apply(int &Field)             { ++Count; }
00162   virtual void Apply(unsigned &Field)        { ++Count; }
00163   virtual void Apply(int64_t &Field)         { ++Count; }
00164   virtual void Apply(uint64_t &Field)        { ++Count; }
00165   virtual void Apply(bool &Field)            { ++Count; }
00166   virtual void Apply(std::string &Field)     { ++Count; }
00167   virtual void Apply(DebugInfoDesc *&Field)  { ++Count; }
00168   virtual void Apply(GlobalVariable *&Field) { ++Count; }
00169   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
00170     ++Count;
00171   }
00172 };
00173 
00174 //===----------------------------------------------------------------------===//
00175 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
00176 /// supplied DebugInfoDesc.
00177 class DIDeserializeVisitor : public DIVisitor {
00178 private:
00179   DIDeserializer &DR;                   // Active deserializer.
00180   unsigned I;                           // Current operand index.
00181   ConstantStruct *CI;                   // GlobalVariable constant initializer.
00182 
00183 public:
00184   DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
00185   : DIVisitor()
00186   , DR(D)
00187   , I(0)
00188   , CI(cast<ConstantStruct>(GV->getInitializer()))
00189   {}
00190   
00191   /// Apply - Set the value of each of the fields.
00192   ///
00193   virtual void Apply(int &Field) {
00194     Constant *C = CI->getOperand(I++);
00195     Field = cast<ConstantSInt>(C)->getValue();
00196   }
00197   virtual void Apply(unsigned &Field) {
00198     Constant *C = CI->getOperand(I++);
00199     Field = cast<ConstantUInt>(C)->getValue();
00200   }
00201   virtual void Apply(int64_t &Field) {
00202     Constant *C = CI->getOperand(I++);
00203     Field = cast<ConstantSInt>(C)->getValue();
00204   }
00205   virtual void Apply(uint64_t &Field) {
00206     Constant *C = CI->getOperand(I++);
00207     Field = cast<ConstantUInt>(C)->getValue();
00208   }
00209   virtual void Apply(bool &Field) {
00210     Constant *C = CI->getOperand(I++);
00211     Field = cast<ConstantBool>(C)->getValue();
00212   }
00213   virtual void Apply(std::string &Field) {
00214     Constant *C = CI->getOperand(I++);
00215     Field = C->getStringValue();
00216   }
00217   virtual void Apply(DebugInfoDesc *&Field) {
00218     Constant *C = CI->getOperand(I++);
00219     Field = DR.Deserialize(C);
00220   }
00221   virtual void Apply(GlobalVariable *&Field) {
00222     Constant *C = CI->getOperand(I++);
00223     Field = getGlobalVariable(C);
00224   }
00225   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
00226     Field.resize(0);
00227     Constant *C = CI->getOperand(I++);
00228     GlobalVariable *GV = getGlobalVariable(C);
00229     if (GV->hasInitializer()) {
00230       if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
00231         for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
00232           GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
00233           DebugInfoDesc *DE = DR.Deserialize(GVE);
00234           Field.push_back(DE);
00235         }
00236       } else if (GV->getInitializer()->isNullValue()) {
00237         if (const ArrayType *T =
00238             dyn_cast<ArrayType>(GV->getType()->getElementType())) {
00239           Field.resize(T->getNumElements());
00240         }
00241       }
00242     }
00243   }
00244 };
00245 
00246 //===----------------------------------------------------------------------===//
00247 /// DISerializeVisitor - This DIVisitor serializes all the fields in
00248 /// the supplied DebugInfoDesc.
00249 class DISerializeVisitor : public DIVisitor {
00250 private:
00251   DISerializer &SR;                     // Active serializer.
00252   std::vector<Constant*> &Elements;     // Element accumulator.
00253   
00254 public:
00255   DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
00256   : DIVisitor()
00257   , SR(S)
00258   , Elements(E)
00259   {}
00260   
00261   /// Apply - Set the value of each of the fields.
00262   ///
00263   virtual void Apply(int &Field) {
00264     Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
00265   }
00266   virtual void Apply(unsigned &Field) {
00267     Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
00268   }
00269   virtual void Apply(int64_t &Field) {
00270     Elements.push_back(ConstantSInt::get(Type::LongTy, Field));
00271   }
00272   virtual void Apply(uint64_t &Field) {
00273     Elements.push_back(ConstantUInt::get(Type::ULongTy, Field));
00274   }
00275   virtual void Apply(bool &Field) {
00276     Elements.push_back(ConstantBool::get(Field));
00277   }
00278   virtual void Apply(std::string &Field) {
00279       Elements.push_back(SR.getString(Field));
00280   }
00281   virtual void Apply(DebugInfoDesc *&Field) {
00282     GlobalVariable *GV = NULL;
00283     
00284     // If non-NULL then convert to global.
00285     if (Field) GV = SR.Serialize(Field);
00286     
00287     // FIXME - At some point should use specific type.
00288     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
00289     
00290     if (GV) {
00291       // Set to pointer to global.
00292       Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
00293     } else {
00294       // Use NULL.
00295       Elements.push_back(ConstantPointerNull::get(EmptyTy));
00296     }
00297   }
00298   virtual void Apply(GlobalVariable *&Field) {
00299     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
00300     if (Field) {
00301       Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
00302     } else {
00303       Elements.push_back(ConstantPointerNull::get(EmptyTy));
00304     }
00305   }
00306   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
00307     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
00308     unsigned N = Field.size();
00309     ArrayType *AT = ArrayType::get(EmptyTy, N);
00310     std::vector<Constant *> ArrayElements;
00311 
00312     for (unsigned i = 0, N = Field.size(); i < N; ++i) {
00313       if (DebugInfoDesc *Element = Field[i]) {
00314         GlobalVariable *GVE = SR.Serialize(Element);
00315         Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
00316         ArrayElements.push_back(cast<Constant>(CE));
00317       } else {
00318         ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
00319       }
00320     }
00321     
00322     Constant *CA = ConstantArray::get(AT, ArrayElements);
00323     GlobalVariable *CAGV = new GlobalVariable(AT, true,
00324                                               GlobalValue::InternalLinkage,
00325                                               CA, "llvm.dbg.array",
00326                                               SR.getModule());
00327     CAGV->setSection("llvm.metadata");
00328     Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
00329     Elements.push_back(CAE);
00330   }
00331 };
00332 
00333 //===----------------------------------------------------------------------===//
00334 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
00335 /// the supplied DebugInfoDesc.
00336 class DIGetTypesVisitor : public DIVisitor {
00337 private:
00338   DISerializer &SR;                     // Active serializer.
00339   std::vector<const Type*> &Fields;     // Type accumulator.
00340   
00341 public:
00342   DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
00343   : DIVisitor()
00344   , SR(S)
00345   , Fields(F)
00346   {}
00347   
00348   /// Apply - Set the value of each of the fields.
00349   ///
00350   virtual void Apply(int &Field) {
00351     Fields.push_back(Type::IntTy);
00352   }
00353   virtual void Apply(unsigned &Field) {
00354     Fields.push_back(Type::UIntTy);
00355   }
00356   virtual void Apply(int64_t &Field) {
00357     Fields.push_back(Type::LongTy);
00358   }
00359   virtual void Apply(uint64_t &Field) {
00360     Fields.push_back(Type::ULongTy);
00361   }
00362   virtual void Apply(bool &Field) {
00363     Fields.push_back(Type::BoolTy);
00364   }
00365   virtual void Apply(std::string &Field) {
00366     Fields.push_back(SR.getStrPtrType());
00367   }
00368   virtual void Apply(DebugInfoDesc *&Field) {
00369     // FIXME - At some point should use specific type.
00370     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
00371     Fields.push_back(EmptyTy);
00372   }
00373   virtual void Apply(GlobalVariable *&Field) {
00374     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
00375     Fields.push_back(EmptyTy);
00376   }
00377   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
00378     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
00379     Fields.push_back(EmptyTy);
00380   }
00381 };
00382 
00383 //===----------------------------------------------------------------------===//
00384 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
00385 /// a constant initializer.
00386 class DIVerifyVisitor : public DIVisitor {
00387 private:
00388   DIVerifier &VR;                       // Active verifier.
00389   bool IsValid;                         // Validity status.
00390   unsigned I;                           // Current operand index.
00391   ConstantStruct *CI;                   // GlobalVariable constant initializer.
00392   
00393 public:
00394   DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
00395   : DIVisitor()
00396   , VR(V)
00397   , IsValid(true)
00398   , I(0)
00399   , CI(cast<ConstantStruct>(GV->getInitializer()))
00400   {
00401   }
00402   
00403   // Accessors.
00404   bool isValid() const { return IsValid; }
00405   
00406   /// Apply - Set the value of each of the fields.
00407   ///
00408   virtual void Apply(int &Field) {
00409     Constant *C = CI->getOperand(I++);
00410     IsValid = IsValid && isa<ConstantInt>(C);
00411   }
00412   virtual void Apply(unsigned &Field) {
00413     Constant *C = CI->getOperand(I++);
00414     IsValid = IsValid && isa<ConstantInt>(C);
00415   }
00416   virtual void Apply(int64_t &Field) {
00417     Constant *C = CI->getOperand(I++);
00418     IsValid = IsValid && isa<ConstantInt>(C);
00419   }
00420   virtual void Apply(uint64_t &Field) {
00421     Constant *C = CI->getOperand(I++);
00422     IsValid = IsValid && isa<ConstantInt>(C);
00423   }
00424   virtual void Apply(bool &Field) {
00425     Constant *C = CI->getOperand(I++);
00426     IsValid = IsValid && isa<ConstantBool>(C);
00427   }
00428   virtual void Apply(std::string &Field) {
00429     Constant *C = CI->getOperand(I++);
00430     IsValid = IsValid && (!C || isStringValue(C));
00431   }
00432   virtual void Apply(DebugInfoDesc *&Field) {
00433     // FIXME - Prepare the correct descriptor.
00434     Constant *C = CI->getOperand(I++);
00435     IsValid = IsValid && isGlobalVariable(C);
00436   }
00437   virtual void Apply(GlobalVariable *&Field) {
00438     Constant *C = CI->getOperand(I++);
00439     IsValid = IsValid && isGlobalVariable(C);
00440   }
00441   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
00442     Constant *C = CI->getOperand(I++);
00443     IsValid = IsValid && isGlobalVariable(C);
00444     if (!IsValid) return;
00445 
00446     GlobalVariable *GV = getGlobalVariable(C);
00447     IsValid = IsValid && GV && GV->hasInitializer();
00448     if (!IsValid) return;
00449     
00450     ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
00451     IsValid = IsValid && CA;
00452     if (!IsValid) return;
00453 
00454     for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
00455       IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
00456       if (!IsValid) return;
00457     
00458       GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
00459       VR.Verify(GVE);
00460     }
00461   }
00462 };
00463 
00464 
00465 //===----------------------------------------------------------------------===//
00466 
00467 /// TagFromGlobal - Returns the tag number from a debug info descriptor
00468 /// GlobalVariable.   Return DIIValid if operand is not an unsigned int. 
00469 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
00470   ConstantUInt *C = getUIntOperand(GV, 0);
00471   return C ? ((unsigned)C->getValue() & ~LLVMDebugVersionMask) :
00472              (unsigned)DW_TAG_invalid;
00473 }
00474 
00475 /// VersionFromGlobal - Returns the version number from a debug info
00476 /// descriptor GlobalVariable.  Return DIIValid if operand is not an unsigned
00477 /// int.
00478 unsigned  DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
00479   ConstantUInt *C = getUIntOperand(GV, 0);
00480   return C ? ((unsigned)C->getValue() & LLVMDebugVersionMask) :
00481              (unsigned)DW_TAG_invalid;
00482 }
00483 
00484 /// DescFactory - Create an instance of debug info descriptor based on Tag.
00485 /// Return NULL if not a recognized Tag.
00486 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
00487   switch (Tag) {
00488   case DW_TAG_anchor:           return new AnchorDesc();
00489   case DW_TAG_compile_unit:     return new CompileUnitDesc();
00490   case DW_TAG_variable:         return new GlobalVariableDesc();
00491   case DW_TAG_subprogram:       return new SubprogramDesc();
00492   case DW_TAG_lexical_block:    return new BlockDesc();
00493   case DW_TAG_base_type:        return new BasicTypeDesc();
00494   case DW_TAG_typedef:
00495   case DW_TAG_pointer_type:        
00496   case DW_TAG_reference_type:
00497   case DW_TAG_const_type:
00498   case DW_TAG_volatile_type:        
00499   case DW_TAG_restrict_type:
00500   case DW_TAG_member:           return new DerivedTypeDesc(Tag);
00501   case DW_TAG_array_type:
00502   case DW_TAG_structure_type:
00503   case DW_TAG_union_type:
00504   case DW_TAG_enumeration_type:
00505   case DW_TAG_vector_type:
00506   case DW_TAG_subroutine_type:  return new CompositeTypeDesc(Tag);
00507   case DW_TAG_subrange_type:    return new SubrangeDesc();
00508   case DW_TAG_enumerator:       return new EnumeratorDesc();
00509   case DW_TAG_return_variable:
00510   case DW_TAG_arg_variable:
00511   case DW_TAG_auto_variable:    return new VariableDesc(Tag);
00512   default: break;
00513   }
00514   return NULL;
00515 }
00516 
00517 /// getLinkage - get linkage appropriate for this type of descriptor.
00518 ///
00519 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
00520   return GlobalValue::InternalLinkage;
00521 }
00522 
00523 /// ApplyToFields - Target the vistor to the fields of the descriptor.
00524 ///
00525 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
00526   Visitor->Apply(Tag);
00527 }
00528 
00529 //===----------------------------------------------------------------------===//
00530 
00531 AnchorDesc::AnchorDesc()
00532 : DebugInfoDesc(DW_TAG_anchor)
00533 , AnchorTag(0)
00534 {}
00535 AnchorDesc::AnchorDesc(AnchoredDesc *D)
00536 : DebugInfoDesc(DW_TAG_anchor)
00537 , AnchorTag(D->getTag())
00538 {}
00539 
00540 // Implement isa/cast/dyncast.
00541 bool AnchorDesc::classof(const DebugInfoDesc *D) {
00542   return D->getTag() == DW_TAG_anchor;
00543 }
00544   
00545 /// getLinkage - get linkage appropriate for this type of descriptor.
00546 ///
00547 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
00548   return GlobalValue::LinkOnceLinkage;
00549 }
00550 
00551 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
00552 ///
00553 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
00554   DebugInfoDesc::ApplyToFields(Visitor);
00555   
00556   Visitor->Apply(AnchorTag);
00557 }
00558 
00559 /// getDescString - Return a string used to compose global names and labels. A
00560 /// A global variable name needs to be defined for each debug descriptor that is
00561 /// anchored. NOTE: that each global variable named here also needs to be added
00562 /// to the list of names left external in the internalizer.
00563 ///   ExternalNames.insert("llvm.dbg.compile_units");
00564 ///   ExternalNames.insert("llvm.dbg.global_variables");
00565 ///   ExternalNames.insert("llvm.dbg.subprograms");
00566 const char *AnchorDesc::getDescString() const {
00567   switch (AnchorTag) {
00568   case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
00569   case DW_TAG_variable:     return GlobalVariableDesc::AnchorString;
00570   case DW_TAG_subprogram:   return SubprogramDesc::AnchorString;
00571   default: break;
00572   }
00573 
00574   assert(0 && "Tag does not have a case for anchor string");
00575   return "";
00576 }
00577 
00578 /// getTypeString - Return a string used to label this descriptors type.
00579 ///
00580 const char *AnchorDesc::getTypeString() const {
00581   return "llvm.dbg.anchor.type";
00582 }
00583 
00584 #ifndef NDEBUG
00585 void AnchorDesc::dump() {
00586   std::cerr << getDescString() << " "
00587             << "Version(" << getVersion() << "), "
00588             << "Tag(" << getTag() << "), "
00589             << "AnchorTag(" << AnchorTag << ")\n";
00590 }
00591 #endif
00592 
00593 //===----------------------------------------------------------------------===//
00594 
00595 AnchoredDesc::AnchoredDesc(unsigned T)
00596 : DebugInfoDesc(T)
00597 , Anchor(NULL)
00598 {}
00599 
00600 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
00601 ///
00602 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
00603   DebugInfoDesc::ApplyToFields(Visitor);
00604 
00605   Visitor->Apply(Anchor);
00606 }
00607 
00608 //===----------------------------------------------------------------------===//
00609 
00610 CompileUnitDesc::CompileUnitDesc()
00611 : AnchoredDesc(DW_TAG_compile_unit)
00612 , Language(0)
00613 , FileName("")
00614 , Directory("")
00615 , Producer("")
00616 {}
00617 
00618 // Implement isa/cast/dyncast.
00619 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
00620   return D->getTag() == DW_TAG_compile_unit;
00621 }
00622 
00623 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
00624 ///
00625 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
00626   AnchoredDesc::ApplyToFields(Visitor);
00627   
00628   // Handle cases out of sync with compiler.
00629   if (getVersion() == 0) {
00630     unsigned DebugVersion;
00631     Visitor->Apply(DebugVersion);
00632   }
00633 
00634   Visitor->Apply(Language);
00635   Visitor->Apply(FileName);
00636   Visitor->Apply(Directory);
00637   Visitor->Apply(Producer);
00638 }
00639 
00640 /// getDescString - Return a string used to compose global names and labels.
00641 ///
00642 const char *CompileUnitDesc::getDescString() const {
00643   return "llvm.dbg.compile_unit";
00644 }
00645 
00646 /// getTypeString - Return a string used to label this descriptors type.
00647 ///
00648 const char *CompileUnitDesc::getTypeString() const {
00649   return "llvm.dbg.compile_unit.type";
00650 }
00651 
00652 /// getAnchorString - Return a string used to label this descriptor's anchor.
00653 ///
00654 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
00655 const char *CompileUnitDesc::getAnchorString() const {
00656   return AnchorString;
00657 }
00658 
00659 #ifndef NDEBUG
00660 void CompileUnitDesc::dump() {
00661   std::cerr << getDescString() << " "
00662             << "Version(" << getVersion() << "), "
00663             << "Tag(" << getTag() << "), "
00664             << "Anchor(" << getAnchor() << "), "
00665             << "Language(" << Language << "), "
00666             << "FileName(\"" << FileName << "\"), "
00667             << "Directory(\"" << Directory << "\"), "
00668             << "Producer(\"" << Producer << "\")\n";
00669 }
00670 #endif
00671 
00672 //===----------------------------------------------------------------------===//
00673 
00674 TypeDesc::TypeDesc(unsigned T)
00675 : DebugInfoDesc(T)
00676 , Context(NULL)
00677 , Name("")
00678 , File(NULL)
00679 , Line(0)
00680 , Size(0)
00681 , Align(0)
00682 , Offset(0)
00683 , Flags(0)
00684 {}
00685 
00686 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
00687 ///
00688 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
00689   DebugInfoDesc::ApplyToFields(Visitor);
00690   
00691   Visitor->Apply(Context);
00692   Visitor->Apply(Name);
00693   Visitor->Apply(File);
00694   Visitor->Apply(Line);
00695   Visitor->Apply(Size);
00696   Visitor->Apply(Align);
00697   Visitor->Apply(Offset);
00698   if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
00699 }
00700 
00701 /// getDescString - Return a string used to compose global names and labels.
00702 ///
00703 const char *TypeDesc::getDescString() const {
00704   return "llvm.dbg.type";
00705 }
00706 
00707 /// getTypeString - Return a string used to label this descriptor's type.
00708 ///
00709 const char *TypeDesc::getTypeString() const {
00710   return "llvm.dbg.type.type";
00711 }
00712 
00713 #ifndef NDEBUG
00714 void TypeDesc::dump() {
00715   std::cerr << getDescString() << " "
00716             << "Version(" << getVersion() << "), "
00717             << "Tag(" << getTag() << "), "
00718             << "Context(" << Context << "), "
00719             << "Name(\"" << Name << "\"), "
00720             << "File(" << File << "), "
00721             << "Line(" << Line << "), "
00722             << "Size(" << Size << "), "
00723             << "Align(" << Align << "), "
00724             << "Offset(" << Offset << "), "
00725             << "Flags(" << Flags << ")\n";
00726 }
00727 #endif
00728 
00729 //===----------------------------------------------------------------------===//
00730 
00731 BasicTypeDesc::BasicTypeDesc()
00732 : TypeDesc(DW_TAG_base_type)
00733 , Encoding(0)
00734 {}
00735 
00736 // Implement isa/cast/dyncast.
00737 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
00738   return D->getTag() == DW_TAG_base_type;
00739 }
00740 
00741 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
00742 ///
00743 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
00744   TypeDesc::ApplyToFields(Visitor);
00745   
00746   Visitor->Apply(Encoding);
00747 }
00748 
00749 /// getDescString - Return a string used to compose global names and labels.
00750 ///
00751 const char *BasicTypeDesc::getDescString() const {
00752   return "llvm.dbg.basictype";
00753 }
00754 
00755 /// getTypeString - Return a string used to label this descriptor's type.
00756 ///
00757 const char *BasicTypeDesc::getTypeString() const {
00758   return "llvm.dbg.basictype.type";
00759 }
00760 
00761 #ifndef NDEBUG
00762 void BasicTypeDesc::dump() {
00763   std::cerr << getDescString() << " "
00764             << "Version(" << getVersion() << "), "
00765             << "Tag(" << getTag() << "), "
00766             << "Context(" << getContext() << "), "
00767             << "Name(\"" << getName() << "\"), "
00768             << "Size(" << getSize() << "), "
00769             << "Encoding(" << Encoding << ")\n";
00770 }
00771 #endif
00772 
00773 //===----------------------------------------------------------------------===//
00774 
00775 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
00776 : TypeDesc(T)
00777 , FromType(NULL)
00778 {}
00779 
00780 // Implement isa/cast/dyncast.
00781 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
00782   unsigned T =  D->getTag();
00783   switch (T) {
00784   case DW_TAG_typedef:
00785   case DW_TAG_pointer_type:
00786   case DW_TAG_reference_type:
00787   case DW_TAG_const_type:
00788   case DW_TAG_volatile_type:
00789   case DW_TAG_restrict_type:
00790   case DW_TAG_member:
00791     return true;
00792   default: break;
00793   }
00794   return false;
00795 }
00796 
00797 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
00798 ///
00799 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
00800   TypeDesc::ApplyToFields(Visitor);
00801   
00802   Visitor->Apply(FromType);
00803 }
00804 
00805 /// getDescString - Return a string used to compose global names and labels.
00806 ///
00807 const char *DerivedTypeDesc::getDescString() const {
00808   return "llvm.dbg.derivedtype";
00809 }
00810 
00811 /// getTypeString - Return a string used to label this descriptor's type.
00812 ///
00813 const char *DerivedTypeDesc::getTypeString() const {
00814   return "llvm.dbg.derivedtype.type";
00815 }
00816 
00817 #ifndef NDEBUG
00818 void DerivedTypeDesc::dump() {
00819   std::cerr << getDescString() << " "
00820             << "Version(" << getVersion() << "), "
00821             << "Tag(" << getTag() << "), "
00822             << "Context(" << getContext() << "), "
00823             << "Name(\"" << getName() << "\"), "
00824             << "Size(" << getSize() << "), "
00825             << "File(" << getFile() << "), "
00826             << "Line(" << getLine() << "), "
00827             << "FromType(" << FromType << ")\n";
00828 }
00829 #endif
00830 
00831 //===----------------------------------------------------------------------===//
00832 
00833 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
00834 : DerivedTypeDesc(T)
00835 , Elements()
00836 {}
00837   
00838 // Implement isa/cast/dyncast.
00839 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
00840   unsigned T =  D->getTag();
00841   switch (T) {
00842   case DW_TAG_array_type:
00843   case DW_TAG_structure_type:
00844   case DW_TAG_union_type:
00845   case DW_TAG_enumeration_type:
00846   case DW_TAG_vector_type:
00847   case DW_TAG_subroutine_type:
00848     return true;
00849   default: break;
00850   }
00851   return false;
00852 }
00853 
00854 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
00855 ///
00856 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
00857   DerivedTypeDesc::ApplyToFields(Visitor);  
00858 
00859   Visitor->Apply(Elements);
00860 }
00861 
00862 /// getDescString - Return a string used to compose global names and labels.
00863 ///
00864 const char *CompositeTypeDesc::getDescString() const {
00865   return "llvm.dbg.compositetype";
00866 }
00867 
00868 /// getTypeString - Return a string used to label this descriptor's type.
00869 ///
00870 const char *CompositeTypeDesc::getTypeString() const {
00871   return "llvm.dbg.compositetype.type";
00872 }
00873 
00874 #ifndef NDEBUG
00875 void CompositeTypeDesc::dump() {
00876   std::cerr << getDescString() << " "
00877             << "Version(" << getVersion() << "), "
00878             << "Tag(" << getTag() << "), "
00879             << "Context(" << getContext() << "), "
00880             << "Name(\"" << getName() << "\"), "
00881             << "Size(" << getSize() << "), "
00882             << "File(" << getFile() << "), "
00883             << "Line(" << getLine() << "), "
00884             << "FromType(" << getFromType() << "), "
00885             << "Elements.size(" << Elements.size() << ")\n";
00886 }
00887 #endif
00888 
00889 //===----------------------------------------------------------------------===//
00890 
00891 SubrangeDesc::SubrangeDesc()
00892 : DebugInfoDesc(DW_TAG_subrange_type)
00893 , Lo(0)
00894 , Hi(0)
00895 {}
00896 
00897 // Implement isa/cast/dyncast.
00898 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
00899   return D->getTag() == DW_TAG_subrange_type;
00900 }
00901 
00902 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
00903 ///
00904 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
00905   DebugInfoDesc::ApplyToFields(Visitor);
00906 
00907   Visitor->Apply(Lo);
00908   Visitor->Apply(Hi);
00909 }
00910 
00911 /// getDescString - Return a string used to compose global names and labels.
00912 ///
00913 const char *SubrangeDesc::getDescString() const {
00914   return "llvm.dbg.subrange";
00915 }
00916   
00917 /// getTypeString - Return a string used to label this descriptor's type.
00918 ///
00919 const char *SubrangeDesc::getTypeString() const {
00920   return "llvm.dbg.subrange.type";
00921 }
00922 
00923 #ifndef NDEBUG
00924 void SubrangeDesc::dump() {
00925   std::cerr << getDescString() << " "
00926             << "Version(" << getVersion() << "), "
00927             << "Tag(" << getTag() << "), "
00928             << "Lo(" << Lo << "), "
00929             << "Hi(" << Hi << ")\n";
00930 }
00931 #endif
00932 
00933 //===----------------------------------------------------------------------===//
00934 
00935 EnumeratorDesc::EnumeratorDesc()
00936 : DebugInfoDesc(DW_TAG_enumerator)
00937 , Name("")
00938 , Value(0)
00939 {}
00940 
00941 // Implement isa/cast/dyncast.
00942 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
00943   return D->getTag() == DW_TAG_enumerator;
00944 }
00945 
00946 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
00947 ///
00948 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
00949   DebugInfoDesc::ApplyToFields(Visitor);
00950 
00951   Visitor->Apply(Name);
00952   Visitor->Apply(Value);
00953 }
00954 
00955 /// getDescString - Return a string used to compose global names and labels.
00956 ///
00957 const char *EnumeratorDesc::getDescString() const {
00958   return "llvm.dbg.enumerator";
00959 }
00960   
00961 /// getTypeString - Return a string used to label this descriptor's type.
00962 ///
00963 const char *EnumeratorDesc::getTypeString() const {
00964   return "llvm.dbg.enumerator.type";
00965 }
00966 
00967 #ifndef NDEBUG
00968 void EnumeratorDesc::dump() {
00969   std::cerr << getDescString() << " "
00970             << "Version(" << getVersion() << "), "
00971             << "Tag(" << getTag() << "), "
00972             << "Name(" << Name << "), "
00973             << "Value(" << Value << ")\n";
00974 }
00975 #endif
00976 
00977 //===----------------------------------------------------------------------===//
00978 
00979 VariableDesc::VariableDesc(unsigned T)
00980 : DebugInfoDesc(T)
00981 , Context(NULL)
00982 , Name("")
00983 , File(NULL)
00984 , Line(0)
00985 , TyDesc(0)
00986 {}
00987 
00988 // Implement isa/cast/dyncast.
00989 bool VariableDesc::classof(const DebugInfoDesc *D) {
00990   unsigned T =  D->getTag();
00991   switch (T) {
00992   case DW_TAG_auto_variable:
00993   case DW_TAG_arg_variable:
00994   case DW_TAG_return_variable:
00995     return true;
00996   default: break;
00997   }
00998   return false;
00999 }
01000 
01001 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
01002 ///
01003 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
01004   DebugInfoDesc::ApplyToFields(Visitor);
01005   
01006   Visitor->Apply(Context);
01007   Visitor->Apply(Name);
01008   Visitor->Apply(File);
01009   Visitor->Apply(Line);
01010   Visitor->Apply(TyDesc);
01011 }
01012 
01013 /// getDescString - Return a string used to compose global names and labels.
01014 ///
01015 const char *VariableDesc::getDescString() const {
01016   return "llvm.dbg.variable";
01017 }
01018 
01019 /// getTypeString - Return a string used to label this descriptor's type.
01020 ///
01021 const char *VariableDesc::getTypeString() const {
01022   return "llvm.dbg.variable.type";
01023 }
01024 
01025 #ifndef NDEBUG
01026 void VariableDesc::dump() {
01027   std::cerr << getDescString() << " "
01028             << "Version(" << getVersion() << "), "
01029             << "Tag(" << getTag() << "), "
01030             << "Context(" << Context << "), "
01031             << "Name(\"" << Name << "\"), "
01032             << "File(" << File << "), "
01033             << "Line(" << Line << "), "
01034             << "TyDesc(" << TyDesc << ")\n";
01035 }
01036 #endif
01037 
01038 //===----------------------------------------------------------------------===//
01039 
01040 GlobalDesc::GlobalDesc(unsigned T)
01041 : AnchoredDesc(T)
01042 , Context(0)
01043 , Name("")
01044 , DisplayName("")
01045 , File(NULL)
01046 , Line(0)
01047 , TyDesc(NULL)
01048 , IsStatic(false)
01049 , IsDefinition(false)
01050 {}
01051 
01052 /// ApplyToFields - Target the visitor to the fields of the global.
01053 ///
01054 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
01055   AnchoredDesc::ApplyToFields(Visitor);
01056 
01057   Visitor->Apply(Context);
01058   Visitor->Apply(Name);
01059   if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
01060   Visitor->Apply(File);
01061   Visitor->Apply(Line);
01062   Visitor->Apply(TyDesc);
01063   Visitor->Apply(IsStatic);
01064   Visitor->Apply(IsDefinition);
01065 }
01066 
01067 //===----------------------------------------------------------------------===//
01068 
01069 GlobalVariableDesc::GlobalVariableDesc()
01070 : GlobalDesc(DW_TAG_variable)
01071 , Global(NULL)
01072 {}
01073 
01074 // Implement isa/cast/dyncast.
01075 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
01076   return D->getTag() == DW_TAG_variable; 
01077 }
01078 
01079 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
01080 ///
01081 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
01082   GlobalDesc::ApplyToFields(Visitor);
01083 
01084   Visitor->Apply(Global);
01085 }
01086 
01087 /// getDescString - Return a string used to compose global names and labels.
01088 ///
01089 const char *GlobalVariableDesc::getDescString() const {
01090   return "llvm.dbg.global_variable";
01091 }
01092 
01093 /// getTypeString - Return a string used to label this descriptors type.
01094 ///
01095 const char *GlobalVariableDesc::getTypeString() const {
01096   return "llvm.dbg.global_variable.type";
01097 }
01098 
01099 /// getAnchorString - Return a string used to label this descriptor's anchor.
01100 ///
01101 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
01102 const char *GlobalVariableDesc::getAnchorString() const {
01103   return AnchorString;
01104 }
01105 
01106 #ifndef NDEBUG
01107 void GlobalVariableDesc::dump() {
01108   std::cerr << getDescString() << " "
01109             << "Version(" << getVersion() << "), "
01110             << "Tag(" << getTag() << "), "
01111             << "Anchor(" << getAnchor() << "), "
01112             << "Name(\"" << getName() << "\"), "
01113             << "DisplayName(\"" << getDisplayName() << "\"), "
01114             << "File(" << getFile() << "),"
01115             << "Line(" << getLine() << "),"
01116             << "Type(\"" << getType() << "\"), "
01117             << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
01118             << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
01119             << "Global(" << Global << ")\n";
01120 }
01121 #endif
01122 
01123 //===----------------------------------------------------------------------===//
01124 
01125 SubprogramDesc::SubprogramDesc()
01126 : GlobalDesc(DW_TAG_subprogram)
01127 {}
01128 
01129 // Implement isa/cast/dyncast.
01130 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
01131   return D->getTag() == DW_TAG_subprogram;
01132 }
01133 
01134 /// ApplyToFields - Target the visitor to the fields of the
01135 /// SubprogramDesc.
01136 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
01137   GlobalDesc::ApplyToFields(Visitor);
01138 }
01139 
01140 /// getDescString - Return a string used to compose global names and labels.
01141 ///
01142 const char *SubprogramDesc::getDescString() const {
01143   return "llvm.dbg.subprogram";
01144 }
01145 
01146 /// getTypeString - Return a string used to label this descriptors type.
01147 ///
01148 const char *SubprogramDesc::getTypeString() const {
01149   return "llvm.dbg.subprogram.type";
01150 }
01151 
01152 /// getAnchorString - Return a string used to label this descriptor's anchor.
01153 ///
01154 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
01155 const char *SubprogramDesc::getAnchorString() const {
01156   return AnchorString;
01157 }
01158 
01159 #ifndef NDEBUG
01160 void SubprogramDesc::dump() {
01161   std::cerr << getDescString() << " "
01162             << "Version(" << getVersion() << "), "
01163             << "Tag(" << getTag() << "), "
01164             << "Anchor(" << getAnchor() << "), "
01165             << "Name(\"" << getName() << "\"), "
01166             << "DisplayName(\"" << getDisplayName() << "\"), "
01167             << "File(" << getFile() << "),"
01168             << "Line(" << getLine() << "),"
01169             << "Type(\"" << getType() << "\"), "
01170             << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
01171             << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
01172 }
01173 #endif
01174 
01175 //===----------------------------------------------------------------------===//
01176 
01177 BlockDesc::BlockDesc()
01178 : DebugInfoDesc(DW_TAG_lexical_block)
01179 , Context(NULL)
01180 {}
01181 
01182 // Implement isa/cast/dyncast.
01183 bool BlockDesc::classof(const DebugInfoDesc *D) {
01184   return D->getTag() == DW_TAG_lexical_block;
01185 }
01186 
01187 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
01188 ///
01189 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
01190   DebugInfoDesc::ApplyToFields(Visitor);
01191 
01192   Visitor->Apply(Context);
01193 }
01194 
01195 /// getDescString - Return a string used to compose global names and labels.
01196 ///
01197 const char *BlockDesc::getDescString() const {
01198   return "llvm.dbg.block";
01199 }
01200 
01201 /// getTypeString - Return a string used to label this descriptors type.
01202 ///
01203 const char *BlockDesc::getTypeString() const {
01204   return "llvm.dbg.block.type";
01205 }
01206 
01207 #ifndef NDEBUG
01208 void BlockDesc::dump() {
01209   std::cerr << getDescString() << " "
01210             << "Version(" << getVersion() << "), "
01211             << "Tag(" << getTag() << "),"
01212             << "Context(" << Context << ")\n";
01213 }
01214 #endif
01215 
01216 //===----------------------------------------------------------------------===//
01217 
01218 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
01219   return Deserialize(getGlobalVariable(V));
01220 }
01221 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
01222   // Handle NULL.
01223   if (!GV) return NULL;
01224 
01225   // Check to see if it has been already deserialized.
01226   DebugInfoDesc *&Slot = GlobalDescs[GV];
01227   if (Slot) return Slot;
01228 
01229   // Get the Tag from the global.
01230   unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
01231   
01232   // Create an empty instance of the correct sort.
01233   Slot = DebugInfoDesc::DescFactory(Tag);
01234   
01235   // If not a user defined descriptor.
01236   if (Slot) {
01237     // Deserialize the fields.
01238     DIDeserializeVisitor DRAM(*this, GV);
01239     DRAM.ApplyToFields(Slot);
01240   }
01241   
01242   return Slot;
01243 }
01244 
01245 //===----------------------------------------------------------------------===//
01246 
01247 /// getStrPtrType - Return a "sbyte *" type.
01248 ///
01249 const PointerType *DISerializer::getStrPtrType() {
01250   // If not already defined.
01251   if (!StrPtrTy) {
01252     // Construct the pointer to signed bytes.
01253     StrPtrTy = PointerType::get(Type::SByteTy);
01254   }
01255   
01256   return StrPtrTy;
01257 }
01258 
01259 /// getEmptyStructPtrType - Return a "{ }*" type.
01260 ///
01261 const PointerType *DISerializer::getEmptyStructPtrType() {
01262   // If not already defined.
01263   if (!EmptyStructPtrTy) {
01264     // Construct the empty structure type.
01265     const StructType *EmptyStructTy =
01266                                     StructType::get(std::vector<const Type*>());
01267     // Construct the pointer to empty structure type.
01268     EmptyStructPtrTy = PointerType::get(EmptyStructTy);
01269   }
01270   
01271   return EmptyStructPtrTy;
01272 }
01273 
01274 /// getTagType - Return the type describing the specified descriptor (via tag.)
01275 ///
01276 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
01277   // Attempt to get the previously defined type.
01278   StructType *&Ty = TagTypes[DD->getTag()];
01279   
01280   // If not already defined.
01281   if (!Ty) {
01282     // Set up fields vector.
01283     std::vector<const Type*> Fields;
01284     // Get types of fields.
01285     DIGetTypesVisitor GTAM(*this, Fields);
01286     GTAM.ApplyToFields(DD);
01287 
01288     // Construct structured type.
01289     Ty = StructType::get(Fields);
01290     
01291     // Register type name with module.
01292     M->addTypeName(DD->getTypeString(), Ty);
01293   }
01294   
01295   return Ty;
01296 }
01297 
01298 /// getString - Construct the string as constant string global.
01299 ///
01300 Constant *DISerializer::getString(const std::string &String) {
01301   // Check string cache for previous edition.
01302   Constant *&Slot = StringCache[String];
01303   // Return Constant if previously defined.
01304   if (Slot) return Slot;
01305   // If empty string then use a sbyte* null instead.
01306   if (String.empty()) {
01307     Slot = ConstantPointerNull::get(getStrPtrType());
01308   } else {
01309     // Construct string as an llvm constant.
01310     Constant *ConstStr = ConstantArray::get(String);
01311     // Otherwise create and return a new string global.
01312     GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
01313                                                GlobalVariable::InternalLinkage,
01314                                                ConstStr, "str", M);
01315     StrGV->setSection("llvm.metadata");
01316     // Convert to generic string pointer.
01317     Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
01318   }
01319   return Slot;
01320   
01321 }
01322 
01323 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
01324 /// so that it can be serialized to a .bc or .ll file.
01325 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
01326   // Check if the DebugInfoDesc is already in the map.
01327   GlobalVariable *&Slot = DescGlobals[DD];
01328   
01329   // See if DebugInfoDesc exists, if so return prior GlobalVariable.
01330   if (Slot) return Slot;
01331   
01332   // Get the type associated with the Tag.
01333   const StructType *Ty = getTagType(DD);
01334 
01335   // Create the GlobalVariable early to prevent infinite recursion.
01336   GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
01337                                           NULL, DD->getDescString(), M);
01338   GV->setSection("llvm.metadata");
01339 
01340   // Insert new GlobalVariable in DescGlobals map.
01341   Slot = GV;
01342  
01343   // Set up elements vector
01344   std::vector<Constant*> Elements;
01345   // Add fields.
01346   DISerializeVisitor SRAM(*this, Elements);
01347   SRAM.ApplyToFields(DD);
01348   
01349   // Set the globals initializer.
01350   GV->setInitializer(ConstantStruct::get(Ty, Elements));
01351   
01352   return GV;
01353 }
01354 
01355 //===----------------------------------------------------------------------===//
01356 
01357 /// Verify - Return true if the GlobalVariable appears to be a valid
01358 /// serialization of a DebugInfoDesc.
01359 bool DIVerifier::Verify(Value *V) {
01360   return !V || Verify(getGlobalVariable(V));
01361 }
01362 bool DIVerifier::Verify(GlobalVariable *GV) {
01363   // NULLs are valid.
01364   if (!GV) return true;
01365   
01366   // Check prior validity.
01367   unsigned &ValiditySlot = Validity[GV];
01368   
01369   // If visited before then use old state.
01370   if (ValiditySlot) return ValiditySlot == Valid;
01371   
01372   // Assume validity for the time being (recursion.)
01373   ValiditySlot = Valid;
01374   
01375   // Make sure the global is internal or link once (anchor.)
01376   if (GV->getLinkage() != GlobalValue::InternalLinkage &&
01377       GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
01378     ValiditySlot = Invalid;
01379     return false;
01380   }
01381 
01382   // Get the Tag
01383   unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
01384   
01385   // Check for user defined descriptors.
01386   if (Tag == DW_TAG_invalid) return true;
01387 
01388   // Construct an empty DebugInfoDesc.
01389   DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
01390   
01391   // Allow for user defined descriptors.
01392   if (!DD) return true;
01393   
01394   // Get the initializer constant.
01395   ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
01396   
01397   // Get the operand count.
01398   unsigned N = CI->getNumOperands();
01399   
01400   // Get the field count.
01401   unsigned &CountSlot = Counts[Tag];
01402   if (!CountSlot) {
01403     // Check the operand count to the field count
01404     DICountVisitor CTAM;
01405     CTAM.ApplyToFields(DD);
01406     CountSlot = CTAM.getCount();
01407   }
01408   
01409   // Field count must be at most equal operand count.
01410   if (CountSlot >  N) {
01411     delete DD;
01412     ValiditySlot = Invalid;
01413     return false;
01414   }
01415   
01416   // Check each field for valid type.
01417   DIVerifyVisitor VRAM(*this, GV);
01418   VRAM.ApplyToFields(DD);
01419   
01420   // Release empty DebugInfoDesc.
01421   delete DD;
01422   
01423   // If fields are not valid.
01424   if (!VRAM.isValid()) {
01425     ValiditySlot = Invalid;
01426     return false;
01427   }
01428   
01429   return true;
01430 }
01431 
01432 //===----------------------------------------------------------------------===//
01433 
01434 DebugScope::~DebugScope() {
01435   for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
01436   for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
01437 }
01438 
01439 //===----------------------------------------------------------------------===//
01440 
01441 MachineDebugInfo::MachineDebugInfo()
01442 : DR()
01443 , VR()
01444 , CompileUnits()
01445 , Directories()
01446 , SourceFiles()
01447 , Lines()
01448 , LabelID(0)
01449 , ScopeMap()
01450 , RootScope(NULL)
01451 , FrameMoves()
01452 {}
01453 MachineDebugInfo::~MachineDebugInfo() {
01454 
01455 }
01456 
01457 /// doInitialization - Initialize the debug state for a new module.
01458 ///
01459 bool MachineDebugInfo::doInitialization() {
01460   return false;
01461 }
01462 
01463 /// doFinalization - Tear down the debug state after completion of a module.
01464 ///
01465 bool MachineDebugInfo::doFinalization() {
01466   return false;
01467 }
01468 
01469 /// BeginFunction - Begin gathering function debug information.
01470 ///
01471 void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
01472   // Coming soon.
01473 }
01474 
01475 /// MachineDebugInfo::EndFunction - Discard function debug information.
01476 ///
01477 void MachineDebugInfo::EndFunction() {
01478   // Clean up scope information.
01479   if (RootScope) {
01480     delete RootScope;
01481     ScopeMap.clear();
01482     RootScope = NULL;
01483   }
01484   
01485   // Clean up frame info.
01486   for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
01487   FrameMoves.clear();
01488 }
01489 
01490 /// getDescFor - Convert a Value to a debug information descriptor.
01491 ///
01492 // FIXME - use new Value type when available.
01493 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
01494   return DR.Deserialize(V);
01495 }
01496 
01497 /// Verify - Verify that a Value is debug information descriptor.
01498 ///
01499 bool MachineDebugInfo::Verify(Value *V) {
01500   return VR.Verify(V);
01501 }
01502 
01503 /// AnalyzeModule - Scan the module for global debug information.
01504 ///
01505 void MachineDebugInfo::AnalyzeModule(Module &M) {
01506   SetupCompileUnits(M);
01507 }
01508 
01509 /// SetupCompileUnits - Set up the unique vector of compile units.
01510 ///
01511 void MachineDebugInfo::SetupCompileUnits(Module &M) {
01512   std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
01513   
01514   for (unsigned i = 0, N = CU.size(); i < N; i++) {
01515     CompileUnits.insert(CU[i]);
01516   }
01517 }
01518 
01519 /// getCompileUnits - Return a vector of debug compile units.
01520 ///
01521 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
01522   return CompileUnits;
01523 }
01524 
01525 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
01526 /// named GlobalVariable.
01527 std::vector<GlobalVariable*>
01528 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
01529                                           const std::string &RootName) {
01530   return ::getGlobalVariablesUsing(M, RootName);
01531 }
01532 
01533 /// RecordLabel - Records location information and associates it with a
01534 /// debug label.  Returns a unique label ID used to generate a label and 
01535 /// provide correspondence to the source line list.
01536 unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
01537                                        unsigned Source) {
01538   unsigned ID = NextLabelID();
01539   Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
01540   return ID;
01541 }
01542 
01543 /// RecordSource - Register a source file with debug info. Returns an source
01544 /// ID.
01545 unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
01546                                         const std::string &Source) {
01547   unsigned DirectoryID = Directories.insert(Directory);
01548   return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
01549 }
01550 unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
01551   return RecordSource(CompileUnit->getDirectory(),
01552                       CompileUnit->getFileName());
01553 }
01554 
01555 /// RecordRegionStart - Indicate the start of a region.
01556 ///
01557 unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
01558   // FIXME - need to be able to handle split scopes because of bb cloning.
01559   DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
01560   DebugScope *Scope = getOrCreateScope(ScopeDesc);
01561   unsigned ID = NextLabelID();
01562   if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
01563   return ID;
01564 }
01565 
01566 /// RecordRegionEnd - Indicate the end of a region.
01567 ///
01568 unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
01569   // FIXME - need to be able to handle split scopes because of bb cloning.
01570   DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
01571   DebugScope *Scope = getOrCreateScope(ScopeDesc);
01572   unsigned ID = NextLabelID();
01573   Scope->setEndLabelID(ID);
01574   return ID;
01575 }
01576 
01577 /// RecordVariable - Indicate the declaration of  a local variable.
01578 ///
01579 void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
01580   VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
01581   DebugScope *Scope = getOrCreateScope(VD->getContext());
01582   DebugVariable *DV = new DebugVariable(VD, FrameIndex);
01583   Scope->AddVariable(DV);
01584 }
01585 
01586 /// getOrCreateScope - Returns the scope associated with the given descriptor.
01587 ///
01588 DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
01589   DebugScope *&Slot = ScopeMap[ScopeDesc];
01590   if (!Slot) {
01591     // FIXME - breaks down when the context is an inlined function.
01592     DebugInfoDesc *ParentDesc = NULL;
01593     if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
01594       ParentDesc = Block->getContext();
01595     }
01596     DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
01597     Slot = new DebugScope(Parent, ScopeDesc);
01598     if (Parent) {
01599       Parent->AddScope(Slot);
01600     } else if (RootScope) {
01601       // FIXME - Add inlined function scopes to the root so we can delete
01602       // them later.  Long term, handle inlined functions properly.
01603       RootScope->AddScope(Slot);
01604     } else {
01605       // First function is top level function.
01606       RootScope = Slot;
01607     }
01608   }
01609   return Slot;
01610 }
01611 
01612