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