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"
23 using namespace llvm::dwarf;
25 // Handle the Pass registration stuff necessary to use TargetData's.
27 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
30 //===----------------------------------------------------------------------===//
32 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
33 /// specified value in their initializer somewhere.
35 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
36 // Scan though value users.
37 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
38 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
39 // If the user is a GlobalVariable then add to result.
41 } else if (Constant *C = dyn_cast<Constant>(*I)) {
42 // If the user is a constant variable then scan its users
43 getGlobalVariablesUsing(C, Result);
48 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
49 /// named GlobalVariable.
50 static std::vector<GlobalVariable*>
51 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
52 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
54 std::vector<const Type*> FieldTypes;
55 FieldTypes.push_back(Type::UIntTy);
56 FieldTypes.push_back(Type::UIntTy);
58 // Get the GlobalVariable root.
59 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
60 StructType::get(FieldTypes));
62 // If present and linkonce then scan for users.
63 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
64 getGlobalVariablesUsing(UseRoot, Result);
70 /// isStringValue - Return true if the given value can be coerced to a string.
72 static bool isStringValue(Value *V) {
73 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
74 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
75 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
76 return Init->isString();
78 } else if (Constant *C = dyn_cast<Constant>(V)) {
79 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
80 return isStringValue(GV);
81 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
82 if (CE->getOpcode() == Instruction::GetElementPtr) {
83 if (CE->getNumOperands() == 3 &&
84 cast<Constant>(CE->getOperand(1))->isNullValue() &&
85 isa<ConstantInt>(CE->getOperand(2))) {
86 return isStringValue(CE->getOperand(0));
94 /// getGlobalVariable - Return either a direct or cast Global value.
96 static GlobalVariable *getGlobalVariable(Value *V) {
97 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
99 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
100 if (CE->getOpcode() == Instruction::Cast) {
101 return dyn_cast<GlobalVariable>(CE->getOperand(0));
107 /// isGlobalVariable - Return true if the given value can be coerced to a
109 static bool isGlobalVariable(Value *V) {
110 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
112 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
113 if (CE->getOpcode() == Instruction::Cast) {
114 return isa<GlobalVariable>(CE->getOperand(0));
120 /// getUIntOperand - Return ith operand if it is an unsigned integer.
122 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
123 // Make sure the GlobalVariable has an initializer.
124 if (!GV->hasInitializer()) return NULL;
126 // Get the initializer constant.
127 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
128 if (!CI) return NULL;
130 // Check if there is at least i + 1 operands.
131 unsigned N = CI->getNumOperands();
132 if (i >= N) return NULL;
135 return dyn_cast<ConstantUInt>(CI->getOperand(i));
137 //===----------------------------------------------------------------------===//
139 /// ApplyToFields - Target the visitor to each field of the debug information
141 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
142 DD->ApplyToFields(this);
145 //===----------------------------------------------------------------------===//
146 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
147 /// the supplied DebugInfoDesc.
148 class DICountVisitor : public DIVisitor {
150 unsigned Count; // Running count of fields.
153 DICountVisitor() : DIVisitor(), Count(0) {}
156 unsigned getCount() const { return Count; }
158 /// Apply - Count each of the fields.
160 virtual void Apply(int &Field) { ++Count; }
161 virtual void Apply(unsigned &Field) { ++Count; }
162 virtual void Apply(int64_t &Field) { ++Count; }
163 virtual void Apply(uint64_t &Field) { ++Count; }
164 virtual void Apply(bool &Field) { ++Count; }
165 virtual void Apply(std::string &Field) { ++Count; }
166 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
167 virtual void Apply(GlobalVariable *&Field) { ++Count; }
168 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
173 //===----------------------------------------------------------------------===//
174 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
175 /// supplied DebugInfoDesc.
176 class DIDeserializeVisitor : public DIVisitor {
178 DIDeserializer &DR; // Active deserializer.
179 unsigned I; // Current operand index.
180 ConstantStruct *CI; // GlobalVariable constant initializer.
183 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
187 , CI(cast<ConstantStruct>(GV->getInitializer()))
190 /// Apply - Set the value of each of the fields.
192 virtual void Apply(int &Field) {
193 Constant *C = CI->getOperand(I++);
194 Field = cast<ConstantSInt>(C)->getValue();
196 virtual void Apply(unsigned &Field) {
197 Constant *C = CI->getOperand(I++);
198 Field = cast<ConstantUInt>(C)->getValue();
200 virtual void Apply(int64_t &Field) {
201 Constant *C = CI->getOperand(I++);
202 Field = cast<ConstantSInt>(C)->getValue();
204 virtual void Apply(uint64_t &Field) {
205 Constant *C = CI->getOperand(I++);
206 Field = cast<ConstantUInt>(C)->getValue();
208 virtual void Apply(bool &Field) {
209 Constant *C = CI->getOperand(I++);
210 Field = cast<ConstantBool>(C)->getValue();
212 virtual void Apply(std::string &Field) {
213 Constant *C = CI->getOperand(I++);
214 Field = C->getStringValue();
216 virtual void Apply(DebugInfoDesc *&Field) {
217 Constant *C = CI->getOperand(I++);
218 Field = DR.Deserialize(C);
220 virtual void Apply(GlobalVariable *&Field) {
221 Constant *C = CI->getOperand(I++);
222 Field = getGlobalVariable(C);
224 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
225 Constant *C = CI->getOperand(I++);
226 GlobalVariable *GV = getGlobalVariable(C);
228 // Have to be able to deal with the empty array case (zero initializer)
229 if (!GV->hasInitializer()) return;
230 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
231 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
232 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
233 DebugInfoDesc *DE = DR.Deserialize(GVE);
240 //===----------------------------------------------------------------------===//
241 /// DISerializeVisitor - This DIVisitor serializes all the fields in
242 /// the supplied DebugInfoDesc.
243 class DISerializeVisitor : public DIVisitor {
245 DISerializer &SR; // Active serializer.
246 std::vector<Constant*> &Elements; // Element accumulator.
249 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
255 /// Apply - Set the value of each of the fields.
257 virtual void Apply(int &Field) {
258 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
260 virtual void Apply(unsigned &Field) {
261 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
263 virtual void Apply(int64_t &Field) {
264 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
266 virtual void Apply(uint64_t &Field) {
267 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
269 virtual void Apply(bool &Field) {
270 Elements.push_back(ConstantBool::get(Field));
272 virtual void Apply(std::string &Field) {
273 Elements.push_back(SR.getString(Field));
275 virtual void Apply(DebugInfoDesc *&Field) {
276 GlobalVariable *GV = NULL;
278 // If non-NULL the convert to global.
279 if (Field) GV = SR.Serialize(Field);
281 // FIXME - At some point should use specific type.
282 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
285 // Set to pointer to global.
286 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
289 Elements.push_back(ConstantPointerNull::get(EmptyTy));
292 virtual void Apply(GlobalVariable *&Field) {
293 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
295 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
297 Elements.push_back(ConstantPointerNull::get(EmptyTy));
300 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
301 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
302 unsigned N = Field.size();
303 ArrayType *AT = ArrayType::get(EmptyTy, N);
304 std::vector<Constant *> ArrayElements;
306 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
307 GlobalVariable *GVE = SR.Serialize(Field[i]);
308 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
309 ArrayElements.push_back(cast<Constant>(CE));
312 Constant *CA = ConstantArray::get(AT, ArrayElements);
313 GlobalVariable *CAGV = new GlobalVariable(AT, true,
314 GlobalValue::InternalLinkage,
315 CA, "llvm.dbg.array",
317 CAGV->setSection("llvm.metadata");
318 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
319 Elements.push_back(CAE);
323 //===----------------------------------------------------------------------===//
324 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
325 /// the supplied DebugInfoDesc.
326 class DIGetTypesVisitor : public DIVisitor {
328 DISerializer &SR; // Active serializer.
329 std::vector<const Type*> &Fields; // Type accumulator.
332 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
338 /// Apply - Set the value of each of the fields.
340 virtual void Apply(int &Field) {
341 Fields.push_back(Type::IntTy);
343 virtual void Apply(unsigned &Field) {
344 Fields.push_back(Type::UIntTy);
346 virtual void Apply(int64_t &Field) {
347 Fields.push_back(Type::IntTy);
349 virtual void Apply(uint64_t &Field) {
350 Fields.push_back(Type::UIntTy);
352 virtual void Apply(bool &Field) {
353 Fields.push_back(Type::BoolTy);
355 virtual void Apply(std::string &Field) {
356 Fields.push_back(SR.getStrPtrType());
358 virtual void Apply(DebugInfoDesc *&Field) {
359 // FIXME - At some point should use specific type.
360 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
361 Fields.push_back(EmptyTy);
363 virtual void Apply(GlobalVariable *&Field) {
364 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
365 Fields.push_back(EmptyTy);
367 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
368 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
369 Fields.push_back(EmptyTy);
373 //===----------------------------------------------------------------------===//
374 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
375 /// a constant initializer.
376 class DIVerifyVisitor : public DIVisitor {
378 DIVerifier &VR; // Active verifier.
379 bool IsValid; // Validity status.
380 unsigned I; // Current operand index.
381 ConstantStruct *CI; // GlobalVariable constant initializer.
384 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
389 , CI(cast<ConstantStruct>(GV->getInitializer()))
394 bool isValid() const { return IsValid; }
396 /// Apply - Set the value of each of the fields.
398 virtual void Apply(int &Field) {
399 Constant *C = CI->getOperand(I++);
400 IsValid = IsValid && isa<ConstantInt>(C);
402 virtual void Apply(unsigned &Field) {
403 Constant *C = CI->getOperand(I++);
404 IsValid = IsValid && isa<ConstantInt>(C);
406 virtual void Apply(int64_t &Field) {
407 Constant *C = CI->getOperand(I++);
408 IsValid = IsValid && isa<ConstantInt>(C);
410 virtual void Apply(uint64_t &Field) {
411 Constant *C = CI->getOperand(I++);
412 IsValid = IsValid && isa<ConstantInt>(C);
414 virtual void Apply(bool &Field) {
415 Constant *C = CI->getOperand(I++);
416 IsValid = IsValid && isa<ConstantBool>(C);
418 virtual void Apply(std::string &Field) {
419 Constant *C = CI->getOperand(I++);
420 IsValid = IsValid && isStringValue(C);
422 virtual void Apply(DebugInfoDesc *&Field) {
423 // FIXME - Prepare the correct descriptor.
424 Constant *C = CI->getOperand(I++);
425 IsValid = IsValid && isGlobalVariable(C);
427 virtual void Apply(GlobalVariable *&Field) {
428 Constant *C = CI->getOperand(I++);
429 IsValid = IsValid && isGlobalVariable(C);
431 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
432 Constant *C = CI->getOperand(I++);
433 IsValid = IsValid && isGlobalVariable(C);
434 if (!IsValid) return;
436 GlobalVariable *GV = getGlobalVariable(C);
437 IsValid = IsValid && GV && GV->hasInitializer();
438 if (!IsValid) return;
440 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
441 IsValid = IsValid && CA;
442 if (!IsValid) return;
444 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
445 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
446 if (!IsValid) return;
448 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
455 //===----------------------------------------------------------------------===//
457 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
459 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
460 ConstantUInt *C = getUIntOperand(GV, 0);
461 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
464 /// DescFactory - Create an instance of debug info descriptor based on Tag.
465 /// Return NULL if not a recognized Tag.
466 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
468 case DW_TAG_anchor: return new AnchorDesc();
469 case DW_TAG_compile_unit: return new CompileUnitDesc();
470 case DW_TAG_variable: return new GlobalVariableDesc();
471 case DW_TAG_subprogram: return new SubprogramDesc();
472 case DW_TAG_base_type: return new BasicTypeDesc();
474 case DW_TAG_pointer_type:
475 case DW_TAG_reference_type:
476 case DW_TAG_const_type:
477 case DW_TAG_volatile_type:
478 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
479 case DW_TAG_array_type:
480 case DW_TAG_structure_type:
481 case DW_TAG_union_type:
482 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
483 case DW_TAG_subrange_type: return new SubrangeDesc();
484 case DW_TAG_member: return new DerivedTypeDesc(DW_TAG_member);
485 case DW_TAG_enumerator: return new EnumeratorDesc();
491 /// getLinkage - get linkage appropriate for this type of descriptor.
493 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
494 return GlobalValue::InternalLinkage;
497 /// ApplyToFields - Target the vistor to the fields of the descriptor.
499 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
503 //===----------------------------------------------------------------------===//
505 AnchorDesc::AnchorDesc()
506 : DebugInfoDesc(DW_TAG_anchor)
509 AnchorDesc::AnchorDesc(AnchoredDesc *D)
510 : DebugInfoDesc(DW_TAG_anchor)
511 , AnchorTag(D->getTag())
514 // Implement isa/cast/dyncast.
515 bool AnchorDesc::classof(const DebugInfoDesc *D) {
516 return D->getTag() == DW_TAG_anchor;
519 /// getLinkage - get linkage appropriate for this type of descriptor.
521 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
522 return GlobalValue::LinkOnceLinkage;
525 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
527 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
528 DebugInfoDesc::ApplyToFields(Visitor);
530 Visitor->Apply(AnchorTag);
533 /// getDescString - Return a string used to compose global names and labels. A
534 /// A global variable name needs to be defined for each debug descriptor that is
535 /// anchored. NOTE: that each global variable named here also needs to be added
536 /// to the list of names left external in the internalizer.
537 /// ExternalNames.insert("llvm.dbg.compile_units");
538 /// ExternalNames.insert("llvm.dbg.global_variables");
539 /// ExternalNames.insert("llvm.dbg.subprograms");
540 const char *AnchorDesc::getDescString() const {
542 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
543 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
544 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
548 assert(0 && "Tag does not have a case for anchor string");
552 /// getTypeString - Return a string used to label this descriptors type.
554 const char *AnchorDesc::getTypeString() const {
555 return "llvm.dbg.anchor.type";
559 void AnchorDesc::dump() {
560 std::cerr << getDescString() << " "
561 << "Tag(" << getTag() << "), "
562 << "AnchorTag(" << AnchorTag << ")\n";
566 //===----------------------------------------------------------------------===//
568 AnchoredDesc::AnchoredDesc(unsigned T)
573 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
575 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
576 DebugInfoDesc::ApplyToFields(Visitor);
578 Visitor->Apply((DebugInfoDesc *&)Anchor);
581 //===----------------------------------------------------------------------===//
583 CompileUnitDesc::CompileUnitDesc()
584 : AnchoredDesc(DW_TAG_compile_unit)
585 , DebugVersion(LLVMDebugVersion)
592 // Implement isa/cast/dyncast.
593 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
594 return D->getTag() == DW_TAG_compile_unit;
597 /// DebugVersionFromGlobal - Returns the version number from a compile unit
599 unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
600 ConstantUInt *C = getUIntOperand(GV, 2);
601 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
604 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
606 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
607 AnchoredDesc::ApplyToFields(Visitor);
609 Visitor->Apply(DebugVersion);
610 Visitor->Apply(Language);
611 Visitor->Apply(FileName);
612 Visitor->Apply(Directory);
613 Visitor->Apply(Producer);
616 /// getDescString - Return a string used to compose global names and labels.
618 const char *CompileUnitDesc::getDescString() const {
619 return "llvm.dbg.compile_unit";
622 /// getTypeString - Return a string used to label this descriptors type.
624 const char *CompileUnitDesc::getTypeString() const {
625 return "llvm.dbg.compile_unit.type";
628 /// getAnchorString - Return a string used to label this descriptor's anchor.
630 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
631 const char *CompileUnitDesc::getAnchorString() const {
636 void CompileUnitDesc::dump() {
637 std::cerr << getDescString() << " "
638 << "Tag(" << getTag() << "), "
639 << "Anchor(" << getAnchor() << "), "
640 << "DebugVersion(" << DebugVersion << "), "
641 << "Language(" << Language << "), "
642 << "FileName(\"" << FileName << "\"), "
643 << "Directory(\"" << Directory << "\"), "
644 << "Producer(\"" << Producer << "\")\n";
648 //===----------------------------------------------------------------------===//
650 TypeDesc::TypeDesc(unsigned T)
660 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
662 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
663 DebugInfoDesc::ApplyToFields(Visitor);
665 Visitor->Apply(Context);
666 Visitor->Apply(Name);
667 Visitor->Apply((DebugInfoDesc *&)File);
668 Visitor->Apply(Line);
669 Visitor->Apply(Size);
670 Visitor->Apply(Align);
671 Visitor->Apply(Offset);
674 /// getDescString - Return a string used to compose global names and labels.
676 const char *TypeDesc::getDescString() const {
677 return "llvm.dbg.type";
680 /// getTypeString - Return a string used to label this descriptor's type.
682 const char *TypeDesc::getTypeString() const {
683 return "llvm.dbg.type.type";
687 void TypeDesc::dump() {
688 std::cerr << getDescString() << " "
689 << "Tag(" << getTag() << "), "
690 << "Context(" << Context << "), "
691 << "Name(\"" << Name << "\"), "
692 << "File(" << File << "), "
693 << "Line(" << Line << "), "
694 << "Size(" << Size << "), "
695 << "Align(" << Align << "), "
696 << "Offset(" << Offset << ")\n";
700 //===----------------------------------------------------------------------===//
702 BasicTypeDesc::BasicTypeDesc()
703 : TypeDesc(DW_TAG_base_type)
707 // Implement isa/cast/dyncast.
708 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
709 return D->getTag() == DW_TAG_base_type;
712 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
714 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
715 TypeDesc::ApplyToFields(Visitor);
717 Visitor->Apply(Encoding);
720 /// getDescString - Return a string used to compose global names and labels.
722 const char *BasicTypeDesc::getDescString() const {
723 return "llvm.dbg.basictype";
726 /// getTypeString - Return a string used to label this descriptor's type.
728 const char *BasicTypeDesc::getTypeString() const {
729 return "llvm.dbg.basictype.type";
733 void BasicTypeDesc::dump() {
734 std::cerr << getDescString() << " "
735 << "Tag(" << getTag() << "), "
736 << "Context(" << getContext() << "), "
737 << "Name(\"" << getName() << "\"), "
738 << "Size(" << getSize() << "), "
739 << "Encoding(" << Encoding << ")\n";
743 //===----------------------------------------------------------------------===//
745 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
750 // Implement isa/cast/dyncast.
751 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
752 unsigned T = D->getTag();
755 case DW_TAG_pointer_type:
756 case DW_TAG_reference_type:
757 case DW_TAG_const_type:
758 case DW_TAG_volatile_type:
759 case DW_TAG_restrict_type:
767 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
769 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
770 TypeDesc::ApplyToFields(Visitor);
772 Visitor->Apply((DebugInfoDesc *&)FromType);
775 /// getDescString - Return a string used to compose global names and labels.
777 const char *DerivedTypeDesc::getDescString() const {
778 return "llvm.dbg.derivedtype";
781 /// getTypeString - Return a string used to label this descriptor's type.
783 const char *DerivedTypeDesc::getTypeString() const {
784 return "llvm.dbg.derivedtype.type";
788 void DerivedTypeDesc::dump() {
789 std::cerr << getDescString() << " "
790 << "Tag(" << getTag() << "), "
791 << "Context(" << getContext() << "), "
792 << "Name(\"" << getName() << "\"), "
793 << "Size(" << getSize() << "), "
794 << "File(" << getFile() << "), "
795 << "Line(" << getLine() << "), "
796 << "FromType(" << FromType << ")\n";
800 //===----------------------------------------------------------------------===//
802 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
807 // Implement isa/cast/dyncast.
808 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
809 unsigned T = D->getTag();
811 case DW_TAG_array_type:
812 case DW_TAG_structure_type:
813 case DW_TAG_union_type:
814 case DW_TAG_enumeration_type:
821 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
823 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
824 DerivedTypeDesc::ApplyToFields(Visitor);
826 Visitor->Apply(Elements);
829 /// getDescString - Return a string used to compose global names and labels.
831 const char *CompositeTypeDesc::getDescString() const {
832 return "llvm.dbg.compositetype";
835 /// getTypeString - Return a string used to label this descriptor's type.
837 const char *CompositeTypeDesc::getTypeString() const {
838 return "llvm.dbg.compositetype.type";
842 void CompositeTypeDesc::dump() {
843 std::cerr << getDescString() << " "
844 << "Tag(" << getTag() << "), "
845 << "Context(" << getContext() << "), "
846 << "Name(\"" << getName() << "\"), "
847 << "Size(" << getSize() << "), "
848 << "File(" << getFile() << "), "
849 << "Line(" << getLine() << "), "
850 << "FromType(" << getFromType() << "), "
851 << "Elements.size(" << Elements.size() << ")\n";
855 //===----------------------------------------------------------------------===//
857 SubrangeDesc::SubrangeDesc()
858 : DebugInfoDesc(DW_TAG_subrange_type)
863 // Implement isa/cast/dyncast.
864 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
865 return D->getTag() == DW_TAG_subrange_type;
868 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
870 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
871 DebugInfoDesc::ApplyToFields(Visitor);
877 /// getDescString - Return a string used to compose global names and labels.
879 const char *SubrangeDesc::getDescString() const {
880 return "llvm.dbg.subrange";
883 /// getTypeString - Return a string used to label this descriptor's type.
885 const char *SubrangeDesc::getTypeString() const {
886 return "llvm.dbg.subrange.type";
890 void SubrangeDesc::dump() {
891 std::cerr << getDescString() << " "
892 << "Tag(" << getTag() << "), "
893 << "Lo(" << Lo << "), "
894 << "Hi(" << Hi << ")\n";
898 //===----------------------------------------------------------------------===//
900 EnumeratorDesc::EnumeratorDesc()
901 : DebugInfoDesc(DW_TAG_enumerator)
906 // Implement isa/cast/dyncast.
907 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
908 return D->getTag() == DW_TAG_enumerator;
911 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
913 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
914 DebugInfoDesc::ApplyToFields(Visitor);
916 Visitor->Apply(Name);
917 Visitor->Apply(Value);
920 /// getDescString - Return a string used to compose global names and labels.
922 const char *EnumeratorDesc::getDescString() const {
923 return "llvm.dbg.enumerator";
926 /// getTypeString - Return a string used to label this descriptor's type.
928 const char *EnumeratorDesc::getTypeString() const {
929 return "llvm.dbg.enumerator.type";
933 void EnumeratorDesc::dump() {
934 std::cerr << getDescString() << " "
935 << "Tag(" << getTag() << "), "
936 << "Name(" << Name << "), "
937 << "Value(" << Value << ")\n";
941 //===----------------------------------------------------------------------===//
943 GlobalDesc::GlobalDesc(unsigned T)
949 , IsDefinition(false)
952 /// ApplyToFields - Target the visitor to the fields of the global.
954 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
955 AnchoredDesc::ApplyToFields(Visitor);
957 Visitor->Apply(Context);
958 Visitor->Apply(Name);
959 Visitor->Apply((DebugInfoDesc *&)TyDesc);
960 Visitor->Apply(IsStatic);
961 Visitor->Apply(IsDefinition);
964 //===----------------------------------------------------------------------===//
966 GlobalVariableDesc::GlobalVariableDesc()
967 : GlobalDesc(DW_TAG_variable)
971 // Implement isa/cast/dyncast.
972 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
973 return D->getTag() == DW_TAG_variable;
976 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
978 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
979 GlobalDesc::ApplyToFields(Visitor);
981 Visitor->Apply(Global);
982 Visitor->Apply(Line);
985 /// getDescString - Return a string used to compose global names and labels.
987 const char *GlobalVariableDesc::getDescString() const {
988 return "llvm.dbg.global_variable";
991 /// getTypeString - Return a string used to label this descriptors type.
993 const char *GlobalVariableDesc::getTypeString() const {
994 return "llvm.dbg.global_variable.type";
997 /// getAnchorString - Return a string used to label this descriptor's anchor.
999 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1000 const char *GlobalVariableDesc::getAnchorString() const {
1001 return AnchorString;
1005 void GlobalVariableDesc::dump() {
1006 std::cerr << getDescString() << " "
1007 << "Tag(" << getTag() << "), "
1008 << "Anchor(" << getAnchor() << "), "
1009 << "Name(\"" << getName() << "\"), "
1010 << "Type(\"" << getTypeDesc() << "\"), "
1011 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1012 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1013 << "Global(" << Global << "), "
1014 << "Line(" << Line << ")\n";
1018 //===----------------------------------------------------------------------===//
1020 SubprogramDesc::SubprogramDesc()
1021 : GlobalDesc(DW_TAG_subprogram)
1024 // Implement isa/cast/dyncast.
1025 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1026 return D->getTag() == DW_TAG_subprogram;
1029 /// ApplyToFields - Target the visitor to the fields of the
1031 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1032 GlobalDesc::ApplyToFields(Visitor);
1035 /// getDescString - Return a string used to compose global names and labels.
1037 const char *SubprogramDesc::getDescString() const {
1038 return "llvm.dbg.subprogram";
1041 /// getTypeString - Return a string used to label this descriptors type.
1043 const char *SubprogramDesc::getTypeString() const {
1044 return "llvm.dbg.subprogram.type";
1047 /// getAnchorString - Return a string used to label this descriptor's anchor.
1049 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1050 const char *SubprogramDesc::getAnchorString() const {
1051 return AnchorString;
1055 void SubprogramDesc::dump() {
1056 std::cerr << getDescString() << " "
1057 << "Tag(" << getTag() << "), "
1058 << "Anchor(" << getAnchor() << "), "
1059 << "Name(\"" << getName() << "\"), "
1060 << "Type(\"" << getTypeDesc() << "\"), "
1061 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1062 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1066 //===----------------------------------------------------------------------===//
1068 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1069 return Deserialize(getGlobalVariable(V));
1071 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1073 if (!GV) return NULL;
1075 // Check to see if it has been already deserialized.
1076 DebugInfoDesc *&Slot = GlobalDescs[GV];
1077 if (Slot) return Slot;
1079 // Get the Tag from the global.
1080 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1082 // Get the debug version if a compile unit.
1083 if (Tag == DW_TAG_compile_unit) {
1084 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1087 // Create an empty instance of the correct sort.
1088 Slot = DebugInfoDesc::DescFactory(Tag);
1089 assert(Slot && "Unknown Tag");
1091 // Deserialize the fields.
1092 DIDeserializeVisitor DRAM(*this, GV);
1093 DRAM.ApplyToFields(Slot);
1098 //===----------------------------------------------------------------------===//
1100 /// getStrPtrType - Return a "sbyte *" type.
1102 const PointerType *DISerializer::getStrPtrType() {
1103 // If not already defined.
1105 // Construct the pointer to signed bytes.
1106 StrPtrTy = PointerType::get(Type::SByteTy);
1112 /// getEmptyStructPtrType - Return a "{ }*" type.
1114 const PointerType *DISerializer::getEmptyStructPtrType() {
1115 // If not already defined.
1116 if (!EmptyStructPtrTy) {
1117 // Construct the empty structure type.
1118 const StructType *EmptyStructTy =
1119 StructType::get(std::vector<const Type*>());
1120 // Construct the pointer to empty structure type.
1121 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1124 return EmptyStructPtrTy;
1127 /// getTagType - Return the type describing the specified descriptor (via tag.)
1129 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1130 // Attempt to get the previously defined type.
1131 StructType *&Ty = TagTypes[DD->getTag()];
1133 // If not already defined.
1135 // Set up fields vector.
1136 std::vector<const Type*> Fields;
1137 // Get types of fields.
1138 DIGetTypesVisitor GTAM(*this, Fields);
1139 GTAM.ApplyToFields(DD);
1141 // Construct structured type.
1142 Ty = StructType::get(Fields);
1144 // Register type name with module.
1145 M->addTypeName(DD->getTypeString(), Ty);
1151 /// getString - Construct the string as constant string global.
1153 Constant *DISerializer::getString(const std::string &String) {
1154 // Check string cache for previous edition.
1155 Constant *&Slot = StringCache[String];
1156 // return Constant if previously defined.
1157 if (Slot) return Slot;
1158 // Construct string as an llvm constant.
1159 Constant *ConstStr = ConstantArray::get(String);
1160 // Otherwise create and return a new string global.
1161 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1162 GlobalVariable::InternalLinkage,
1163 ConstStr, "str", M);
1164 StrGV->setSection("llvm.metadata");
1165 // Convert to generic string pointer.
1166 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1171 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1172 /// so that it can be serialized to a .bc or .ll file.
1173 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1174 // Check if the DebugInfoDesc is already in the map.
1175 GlobalVariable *&Slot = DescGlobals[DD];
1177 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1178 if (Slot) return Slot;
1180 // Get the type associated with the Tag.
1181 const StructType *Ty = getTagType(DD);
1183 // Create the GlobalVariable early to prevent infinite recursion.
1184 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1185 NULL, DD->getDescString(), M);
1186 GV->setSection("llvm.metadata");
1188 // Insert new GlobalVariable in DescGlobals map.
1191 // Set up elements vector
1192 std::vector<Constant*> Elements;
1194 DISerializeVisitor SRAM(*this, Elements);
1195 SRAM.ApplyToFields(DD);
1197 // Set the globals initializer.
1198 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1203 //===----------------------------------------------------------------------===//
1205 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
1206 /// Mark visited otherwise.
1207 bool DIVerifier::markVisited(GlobalVariable *GV) {
1208 // Check if the GlobalVariable is already in the Visited set.
1209 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1211 // See if GlobalVariable exists.
1212 bool Exists = VI != Visited.end() && *VI == GV;
1215 if (!Exists) Visited.insert(VI, GV);
1220 /// Verify - Return true if the GlobalVariable appears to be a valid
1221 /// serialization of a DebugInfoDesc.
1222 bool DIVerifier::Verify(Value *V) {
1223 return Verify(getGlobalVariable(V));
1225 bool DIVerifier::Verify(GlobalVariable *GV) {
1226 // Check if seen before.
1227 if (markVisited(GV)) return true;
1230 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1231 if (Tag == DW_TAG_invalid) return false;
1233 // If a compile unit we need the debug version.
1234 if (Tag == DW_TAG_compile_unit) {
1235 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1236 if (DebugVersion == DW_TAG_invalid) return false;
1239 // Construct an empty DebugInfoDesc.
1240 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1241 if (!DD) return false;
1243 // Get the initializer constant.
1244 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1246 // Get the operand count.
1247 unsigned N = CI->getNumOperands();
1249 // Get the field count.
1250 unsigned &Slot = Counts[Tag];
1252 // Check the operand count to the field count
1253 DICountVisitor CTAM;
1254 CTAM.ApplyToFields(DD);
1255 Slot = CTAM.getCount();
1258 // Field count must equal operand count.
1264 // Check each field for valid type.
1265 DIVerifyVisitor VRAM(*this, GV);
1266 VRAM.ApplyToFields(DD);
1268 // Release empty DebugInfoDesc.
1271 // Return result of field tests.
1272 return VRAM.isValid();
1275 //===----------------------------------------------------------------------===//
1278 MachineDebugInfo::MachineDebugInfo()
1287 MachineDebugInfo::~MachineDebugInfo() {
1291 /// doInitialization - Initialize the debug state for a new module.
1293 bool MachineDebugInfo::doInitialization() {
1297 /// doFinalization - Tear down the debug state after completion of a module.
1299 bool MachineDebugInfo::doFinalization() {
1303 /// getDescFor - Convert a Value to a debug information descriptor.
1305 // FIXME - use new Value type when available.
1306 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
1307 return DR.Deserialize(V);
1310 /// Verify - Verify that a Value is debug information descriptor.
1312 bool MachineDebugInfo::Verify(Value *V) {
1314 return VR.Verify(V);
1317 /// AnalyzeModule - Scan the module for global debug information.
1319 void MachineDebugInfo::AnalyzeModule(Module &M) {
1320 SetupCompileUnits(M);
1323 /// SetupCompileUnits - Set up the unique vector of compile units.
1325 void MachineDebugInfo::SetupCompileUnits(Module &M) {
1326 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1328 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1329 CompileUnits.insert(CU[i]);
1333 /// getCompileUnits - Return a vector of debug compile units.
1335 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
1336 return CompileUnits;
1339 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1340 /// named GlobalVariable.
1341 std::vector<GlobalVariable*>
1342 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1343 const std::string &RootName) {
1344 return ::getGlobalVariablesUsing(M, RootName);