1 //===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineDebugInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/GlobalVariable.h"
15 #include "llvm/Intrinsics.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Module.h"
18 #include "llvm/Support/Dwarf.h"
24 // Handle the Pass registration stuff necessary to use TargetData's.
26 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
29 //===----------------------------------------------------------------------===//
31 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
32 /// specified value in their initializer somewhere.
34 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
35 // Scan though value users.
36 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
37 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
38 // If the user is a GlobalVariable then add to result.
40 } else if (Constant *C = dyn_cast<Constant>(*I)) {
41 // If the user is a constant variable then scan its users
42 getGlobalVariablesUsing(C, Result);
47 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
48 /// named GlobalVariable.
49 static std::vector<GlobalVariable*>
50 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
51 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
53 std::vector<const Type*> FieldTypes;
54 FieldTypes.push_back(Type::UIntTy);
55 FieldTypes.push_back(PointerType::get(Type::SByteTy));
57 // Get the GlobalVariable root.
58 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
59 StructType::get(FieldTypes));
61 // If present and linkonce then scan for users.
62 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
63 getGlobalVariablesUsing(UseRoot, Result);
69 /// getStringValue - Turn an LLVM constant pointer that eventually points to a
70 /// global into a string value. Return an empty string if we can't do it.
72 static const std::string getStringValue(Value *V, unsigned Offset = 0) {
73 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
74 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
75 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
76 if (Init->isString()) {
77 std::string Result = Init->getAsString();
78 if (Offset < Result.size()) {
79 // If we are pointing INTO The string, erase the beginning...
80 Result.erase(Result.begin(), Result.begin()+Offset);
82 // Take off the null terminator, and any string fragments after it.
83 std::string::size_type NullPos = Result.find_first_of((char)0);
84 if (NullPos != std::string::npos)
85 Result.erase(Result.begin()+NullPos, Result.end());
90 } else if (Constant *C = dyn_cast<Constant>(V)) {
91 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
92 return getStringValue(GV, Offset);
93 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
94 if (CE->getOpcode() == Instruction::GetElementPtr) {
95 // Turn a gep into the specified offset.
96 if (CE->getNumOperands() == 3 &&
97 cast<Constant>(CE->getOperand(1))->isNullValue() &&
98 isa<ConstantInt>(CE->getOperand(2))) {
99 return getStringValue(CE->getOperand(0),
100 Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
108 /// isStringValue - Return true if the given value can be coerced to a string.
110 static bool isStringValue(Value *V) {
111 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
112 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
113 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
114 return Init->isString();
116 } else if (Constant *C = dyn_cast<Constant>(V)) {
117 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
118 return isStringValue(GV);
119 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
120 if (CE->getOpcode() == Instruction::GetElementPtr) {
121 if (CE->getNumOperands() == 3 &&
122 cast<Constant>(CE->getOperand(1))->isNullValue() &&
123 isa<ConstantInt>(CE->getOperand(2))) {
124 return isStringValue(CE->getOperand(0));
132 /// getGlobalVariable - Return either a direct or cast Global value.
134 static GlobalVariable *getGlobalVariable(Value *V) {
135 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
137 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
138 if (CE->getOpcode() == Instruction::Cast) {
139 return dyn_cast<GlobalVariable>(CE->getOperand(0));
145 /// isGlobalVariable - Return true if the given value can be coerced to a
147 static bool isGlobalVariable(Value *V) {
148 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
150 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
151 if (CE->getOpcode() == Instruction::Cast) {
152 return isa<GlobalVariable>(CE->getOperand(0));
158 /// getUIntOperand - Return ith operand if it is an unsigned integer.
160 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
161 // Make sure the GlobalVariable has an initializer.
162 if (!GV->hasInitializer()) return NULL;
164 // Get the initializer constant.
165 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
166 if (!CI) return NULL;
168 // Check if there is at least i + 1 operands.
169 unsigned N = CI->getNumOperands();
170 if (i >= N) return NULL;
173 return dyn_cast<ConstantUInt>(CI->getOperand(i));
175 //===----------------------------------------------------------------------===//
177 /// ApplyToFields - Target the visitor to each field of the debug information
179 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
180 DD->ApplyToFields(this);
183 //===----------------------------------------------------------------------===//
184 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
185 /// the supplied DebugInfoDesc.
186 class DICountVisitor : public DIVisitor {
188 unsigned Count; // Running count of fields.
191 DICountVisitor() : DIVisitor(), Count(0) {}
194 unsigned getCount() const { return Count; }
196 /// Apply - Count each of the fields.
198 virtual void Apply(int &Field) { ++Count; }
199 virtual void Apply(unsigned &Field) { ++Count; }
200 virtual void Apply(bool &Field) { ++Count; }
201 virtual void Apply(std::string &Field) { ++Count; }
202 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
203 virtual void Apply(GlobalVariable *&Field) { ++Count; }
206 //===----------------------------------------------------------------------===//
207 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
208 /// supplied DebugInfoDesc.
209 class DIDeserializeVisitor : public DIVisitor {
211 DIDeserializer &DR; // Active deserializer.
212 unsigned I; // Current operand index.
213 ConstantStruct *CI; // GlobalVariable constant initializer.
216 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
220 , CI(cast<ConstantStruct>(GV->getInitializer()))
223 /// Apply - Set the value of each of the fields.
225 virtual void Apply(int &Field) {
226 Constant *C = CI->getOperand(I++);
227 Field = cast<ConstantSInt>(C)->getValue();
229 virtual void Apply(unsigned &Field) {
230 Constant *C = CI->getOperand(I++);
231 Field = cast<ConstantUInt>(C)->getValue();
233 virtual void Apply(bool &Field) {
234 Constant *C = CI->getOperand(I++);
235 Field = cast<ConstantBool>(C)->getValue();
237 virtual void Apply(std::string &Field) {
238 Constant *C = CI->getOperand(I++);
239 Field = getStringValue(C);
241 virtual void Apply(DebugInfoDesc *&Field) {
242 Constant *C = CI->getOperand(I++);
243 Field = DR.Deserialize(C);
245 virtual void Apply(GlobalVariable *&Field) {
246 Constant *C = CI->getOperand(I++);
247 Field = getGlobalVariable(C);
251 //===----------------------------------------------------------------------===//
252 /// DISerializeVisitor - This DIVisitor serializes all the fields in
253 /// the supplied DebugInfoDesc.
254 class DISerializeVisitor : public DIVisitor {
256 DISerializer &SR; // Active serializer.
257 std::vector<Constant*> &Elements; // Element accumulator.
260 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
266 /// Apply - Set the value of each of the fields.
268 virtual void Apply(int &Field) {
269 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
271 virtual void Apply(unsigned &Field) {
272 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
274 virtual void Apply(bool &Field) {
275 Elements.push_back(ConstantBool::get(Field));
277 virtual void Apply(std::string &Field) {
278 Elements.push_back(SR.getString(Field));
280 virtual void Apply(DebugInfoDesc *&Field) {
281 GlobalVariable *GV = NULL;
283 // If non-NULL the convert to global.
284 if (Field) GV = SR.Serialize(Field);
286 // FIXME - At some point should use specific type.
287 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
290 // Set to pointer to global.
291 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
294 Elements.push_back(ConstantPointerNull::get(EmptyTy));
297 virtual void Apply(GlobalVariable *&Field) {
298 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
300 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
302 Elements.push_back(ConstantPointerNull::get(EmptyTy));
307 //===----------------------------------------------------------------------===//
308 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
309 /// the supplied DebugInfoDesc.
310 class DIGetTypesVisitor : public DIVisitor {
312 DISerializer &SR; // Active serializer.
313 std::vector<const Type*> &Fields; // Type accumulator.
316 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
322 /// Apply - Set the value of each of the fields.
324 virtual void Apply(int &Field) {
325 Fields.push_back(Type::IntTy);
327 virtual void Apply(unsigned &Field) {
328 Fields.push_back(Type::UIntTy);
330 virtual void Apply(bool &Field) {
331 Fields.push_back(Type::BoolTy);
333 virtual void Apply(std::string &Field) {
334 Fields.push_back(SR.getStrPtrType());
336 virtual void Apply(DebugInfoDesc *&Field) {
337 // FIXME - At some point should use specific type.
338 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
339 Fields.push_back(EmptyTy);
341 virtual void Apply(GlobalVariable *&Field) {
342 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
343 Fields.push_back(EmptyTy);
347 //===----------------------------------------------------------------------===//
348 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
349 /// a constant initializer.
350 class DIVerifyVisitor : public DIVisitor {
352 DIVerifier &VR; // Active verifier.
353 bool IsValid; // Validity status.
354 unsigned I; // Current operand index.
355 ConstantStruct *CI; // GlobalVariable constant initializer.
358 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
363 , CI(cast<ConstantStruct>(GV->getInitializer()))
368 bool isValid() const { return IsValid; }
370 /// Apply - Set the value of each of the fields.
372 virtual void Apply(int &Field) {
373 Constant *C = CI->getOperand(I++);
374 IsValid = IsValid && isa<ConstantInt>(C);
376 virtual void Apply(unsigned &Field) {
377 Constant *C = CI->getOperand(I++);
378 IsValid = IsValid && isa<ConstantInt>(C);
380 virtual void Apply(bool &Field) {
381 Constant *C = CI->getOperand(I++);
382 IsValid = IsValid && isa<ConstantBool>(C);
384 virtual void Apply(std::string &Field) {
385 Constant *C = CI->getOperand(I++);
386 IsValid = IsValid && isStringValue(C);
388 virtual void Apply(DebugInfoDesc *&Field) {
389 // FIXME - Prepare the correct descriptor.
390 Constant *C = CI->getOperand(I++);
391 IsValid = IsValid && isGlobalVariable(C);
393 virtual void Apply(GlobalVariable *&Field) {
394 Constant *C = CI->getOperand(I++);
395 IsValid = IsValid && isGlobalVariable(C);
400 //===----------------------------------------------------------------------===//
402 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
404 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
405 ConstantUInt *C = getUIntOperand(GV, 0);
406 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
409 /// DescFactory - Create an instance of debug info descriptor based on Tag.
410 /// Return NULL if not a recognized Tag.
411 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
413 case DI_TAG_anchor: return new AnchorDesc();
414 case DI_TAG_compile_unit: return new CompileUnitDesc();
415 case DI_TAG_global_variable: return new GlobalVariableDesc();
416 case DI_TAG_subprogram: return new SubprogramDesc();
422 /// getLinkage - get linkage appropriate for this type of descriptor.
424 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
425 return GlobalValue::InternalLinkage;
428 /// ApplyToFields - Target the vistor to the fields of the descriptor.
430 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
434 //===----------------------------------------------------------------------===//
436 /// getLinkage - get linkage appropriate for this type of descriptor.
438 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
439 return GlobalValue::LinkOnceLinkage;
442 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
444 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
445 DebugInfoDesc::ApplyToFields(Visitor);
447 Visitor->Apply(Name);
450 /// getDescString - Return a string used to compose global names and labels.
452 const char *AnchorDesc::getDescString() const {
456 /// getTypeString - Return a string used to label this descriptors type.
458 const char *AnchorDesc::getTypeString() const {
459 return "llvm.dbg.anchor.type";
463 void AnchorDesc::dump() {
464 std::cerr << getDescString() << " "
465 << "Tag(" << getTag() << "), "
466 << "Name(" << Name << ")\n";
470 //===----------------------------------------------------------------------===//
472 AnchoredDesc::AnchoredDesc(unsigned T)
477 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
479 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
480 DebugInfoDesc::ApplyToFields(Visitor);
482 Visitor->Apply((DebugInfoDesc *&)Anchor);
485 //===----------------------------------------------------------------------===//
487 CompileUnitDesc::CompileUnitDesc()
488 : AnchoredDesc(DI_TAG_compile_unit)
489 , DebugVersion(LLVMDebugVersion)
496 /// DebugVersionFromGlobal - Returns the version number from a compile unit
498 unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
499 ConstantUInt *C = getUIntOperand(GV, 2);
500 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
503 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
505 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
506 AnchoredDesc::ApplyToFields(Visitor);
508 Visitor->Apply(DebugVersion);
509 Visitor->Apply(Language);
510 Visitor->Apply(FileName);
511 Visitor->Apply(Directory);
512 Visitor->Apply(Producer);
515 /// getDescString - Return a string used to compose global names and labels.
517 const char *CompileUnitDesc::getDescString() const {
518 return "llvm.dbg.compile_unit";
521 /// getTypeString - Return a string used to label this descriptors type.
523 const char *CompileUnitDesc::getTypeString() const {
524 return "llvm.dbg.compile_unit.type";
527 /// getAnchorString - Return a string used to label this descriptor's anchor.
529 const char *CompileUnitDesc::getAnchorString() const {
530 return "llvm.dbg.compile_units";
534 void CompileUnitDesc::dump() {
535 std::cerr << getDescString() << " "
536 << "Tag(" << getTag() << "), "
537 << "Anchor(" << getAnchor() << "), "
538 << "DebugVersion(" << DebugVersion << "), "
539 << "Language(" << Language << "), "
540 << "FileName(\"" << FileName << "\"), "
541 << "Directory(\"" << Directory << "\"), "
542 << "Producer(\"" << Producer << "\")\n";
546 //===----------------------------------------------------------------------===//
548 GlobalDesc::GlobalDesc(unsigned T)
554 , IsDefinition(false)
557 /// ApplyToFields - Target the visitor to the fields of the global.
559 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
560 AnchoredDesc::ApplyToFields(Visitor);
562 Visitor->Apply(Context);
563 Visitor->Apply(Name);
564 Visitor->Apply(TyDesc);
565 Visitor->Apply(IsStatic);
566 Visitor->Apply(IsDefinition);
569 //===----------------------------------------------------------------------===//
571 GlobalVariableDesc::GlobalVariableDesc()
572 : GlobalDesc(DI_TAG_global_variable)
576 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
578 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
579 GlobalDesc::ApplyToFields(Visitor);
581 Visitor->Apply(Global);
582 Visitor->Apply(Line);
585 /// getDescString - Return a string used to compose global names and labels.
587 const char *GlobalVariableDesc::getDescString() const {
588 return "llvm.dbg.global_variable";
591 /// getTypeString - Return a string used to label this descriptors type.
593 const char *GlobalVariableDesc::getTypeString() const {
594 return "llvm.dbg.global_variable.type";
597 /// getAnchorString - Return a string used to label this descriptor's anchor.
599 const char *GlobalVariableDesc::getAnchorString() const {
600 return "llvm.dbg.global_variables";
604 void GlobalVariableDesc::dump() {
605 std::cerr << getDescString() << " "
606 << "Tag(" << getTag() << "), "
607 << "Anchor(" << getAnchor() << "), "
608 << "Name(\"" << getName() << "\"), "
609 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
610 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
611 << "Global(" << Global << "), "
612 << "Line(" << Line << ")\n";
616 //===----------------------------------------------------------------------===//
618 SubprogramDesc::SubprogramDesc()
619 : GlobalDesc(DI_TAG_subprogram)
622 /// ApplyToFields - Target the visitor to the fields of the
624 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
625 GlobalDesc::ApplyToFields(Visitor);
628 /// getDescString - Return a string used to compose global names and labels.
630 const char *SubprogramDesc::getDescString() const {
631 return "llvm.dbg.subprogram";
634 /// getTypeString - Return a string used to label this descriptors type.
636 const char *SubprogramDesc::getTypeString() const {
637 return "llvm.dbg.subprogram.type";
640 /// getAnchorString - Return a string used to label this descriptor's anchor.
642 const char *SubprogramDesc::getAnchorString() const {
643 return "llvm.dbg.subprograms";
647 void SubprogramDesc::dump() {
648 std::cerr << getDescString() << " "
649 << "Tag(" << getTag() << "), "
650 << "Anchor(" << getAnchor() << "), "
651 << "Name(\"" << getName() << "\"), "
652 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
653 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
657 //===----------------------------------------------------------------------===//
659 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
660 return Deserialize(getGlobalVariable(V));
662 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
664 if (!GV) return NULL;
666 // Check to see if it has been already deserialized.
667 DebugInfoDesc *&Slot = GlobalDescs[GV];
668 if (Slot) return Slot;
670 // Get the Tag from the global.
671 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
673 // Get the debug version if a compile unit.
674 if (Tag == DI_TAG_compile_unit) {
675 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
678 // Create an empty instance of the correct sort.
679 Slot = DebugInfoDesc::DescFactory(Tag);
680 assert(Slot && "Unknown Tag");
682 // Deserialize the fields.
683 DIDeserializeVisitor DRAM(*this, GV);
684 DRAM.ApplyToFields(Slot);
689 //===----------------------------------------------------------------------===//
691 /// getStrPtrType - Return a "sbyte *" type.
693 const PointerType *DISerializer::getStrPtrType() {
694 // If not already defined.
696 // Construct the pointer to signed bytes.
697 StrPtrTy = PointerType::get(Type::SByteTy);
703 /// getEmptyStructPtrType - Return a "{ }*" type.
705 const PointerType *DISerializer::getEmptyStructPtrType() {
706 // If not already defined.
707 if (!EmptyStructPtrTy) {
708 // Construct the empty structure type.
709 const StructType *EmptyStructTy =
710 StructType::get(std::vector<const Type*>());
711 // Construct the pointer to empty structure type.
712 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
715 return EmptyStructPtrTy;
718 /// getTagType - Return the type describing the specified descriptor (via tag.)
720 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
721 // Attempt to get the previously defined type.
722 StructType *&Ty = TagTypes[DD->getTag()];
724 // If not already defined.
726 // Set up fields vector.
727 std::vector<const Type*> Fields;
728 // Get types of fields.
729 DIGetTypesVisitor GTAM(*this, Fields);
730 GTAM.ApplyToFields(DD);
732 // Construct structured type.
733 Ty = StructType::get(Fields);
735 // Register type name with module.
736 M->addTypeName(DD->getTypeString(), Ty);
742 /// getString - Construct the string as constant string global.
744 Constant *DISerializer::getString(const std::string &String) {
745 // Check string cache for previous edition.
746 Constant *&Slot = StringCache[String];
747 // return Constant if previously defined.
748 if (Slot) return Slot;
749 // Construct string as an llvm constant.
750 Constant *ConstStr = ConstantArray::get(String);
751 // Otherwise create and return a new string global.
752 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
753 GlobalVariable::InternalLinkage,
755 // Convert to generic string pointer.
756 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
761 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
762 /// so that it can be serialized to a .bc or .ll file.
763 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
764 // Check if the DebugInfoDesc is already in the map.
765 GlobalVariable *&Slot = DescGlobals[DD];
767 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
768 if (Slot) return Slot;
770 // Get the type associated with the Tag.
771 const StructType *Ty = getTagType(DD);
773 // Create the GlobalVariable early to prevent infinite recursion.
774 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
775 NULL, DD->getDescString(), M);
777 // Insert new GlobalVariable in DescGlobals map.
780 // Set up elements vector
781 std::vector<Constant*> Elements;
783 DISerializeVisitor SRAM(*this, Elements);
784 SRAM.ApplyToFields(DD);
786 // Set the globals initializer.
787 GV->setInitializer(ConstantStruct::get(Ty, Elements));
792 //===----------------------------------------------------------------------===//
794 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
795 /// Mark visited otherwise.
796 bool DIVerifier::markVisited(GlobalVariable *GV) {
797 // Check if the GlobalVariable is already in the Visited set.
798 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
800 // See if GlobalVariable exists.
801 bool Exists = VI != Visited.end() && *VI == GV;
804 if (!Exists) Visited.insert(VI, GV);
809 /// Verify - Return true if the GlobalVariable appears to be a valid
810 /// serialization of a DebugInfoDesc.
811 bool DIVerifier::Verify(Value *V) {
812 return Verify(getGlobalVariable(V));
814 bool DIVerifier::Verify(GlobalVariable *GV) {
815 // Check if seen before.
816 if (markVisited(GV)) return true;
819 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
820 if (Tag == DIInvalid) return false;
822 // If a compile unit we need the debug version.
823 if (Tag == DI_TAG_compile_unit) {
824 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
825 if (DebugVersion == DIInvalid) return false;
828 // Construct an empty DebugInfoDesc.
829 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
830 if (!DD) return false;
832 // Get the initializer constant.
833 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
835 // Get the operand count.
836 unsigned N = CI->getNumOperands();
838 // Get the field count.
839 unsigned &Slot = Counts[Tag];
841 // Check the operand count to the field count
843 CTAM.ApplyToFields(DD);
844 Slot = CTAM.getCount();
847 // Field count must equal operand count.
853 // Check each field for valid type.
854 DIVerifyVisitor VRAM(*this, GV);
855 VRAM.ApplyToFields(DD);
857 // Release empty DebugInfoDesc.
860 // Return result of field tests.
861 return VRAM.isValid();
864 //===----------------------------------------------------------------------===//
867 MachineDebugInfo::MachineDebugInfo()
876 MachineDebugInfo::~MachineDebugInfo() {
880 /// doInitialization - Initialize the debug state for a new module.
882 bool MachineDebugInfo::doInitialization() {
886 /// doFinalization - Tear down the debug state after completion of a module.
888 bool MachineDebugInfo::doFinalization() {
892 /// getDescFor - Convert a Value to a debug information descriptor.
894 // FIXME - use new Value type when available.
895 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
896 return DR.Deserialize(V);
899 /// Verify - Verify that a Value is debug information descriptor.
901 bool MachineDebugInfo::Verify(Value *V) {
906 /// AnalyzeModule - Scan the module for global debug information.
908 void MachineDebugInfo::AnalyzeModule(Module &M) {
909 SetupCompileUnits(M);
912 /// SetupCompileUnits - Set up the unique vector of compile units.
914 void MachineDebugInfo::SetupCompileUnits(Module &M) {
915 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
917 for (unsigned i = 0, N = CU.size(); i < N; i++) {
918 CompileUnits.insert(CU[i]);
922 /// getCompileUnits - Return a vector of debug compile units.
924 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
928 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
929 /// named GlobalVariable.
930 std::vector<GlobalVariable*>
931 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
932 const std::string &RootName) {
933 return ::getGlobalVariablesUsing(M, RootName);