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(PointerType::get(Type::SByteTy));
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 /// getStringValue - Turn an LLVM constant pointer that eventually points to a
71 /// global into a string value. Return an empty string if we can't do it.
73 static const std::string getStringValue(Value *V, unsigned Offset = 0) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 if (Init->isString()) {
78 std::string Result = Init->getAsString();
79 if (Offset < Result.size()) {
80 // If we are pointing INTO The string, erase the beginning...
81 Result.erase(Result.begin(), Result.begin()+Offset);
83 // Take off the null terminator, and any string fragments after it.
84 std::string::size_type NullPos = Result.find_first_of((char)0);
85 if (NullPos != std::string::npos)
86 Result.erase(Result.begin()+NullPos, Result.end());
91 } else if (Constant *C = dyn_cast<Constant>(V)) {
92 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
93 return getStringValue(GV, Offset);
94 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
95 if (CE->getOpcode() == Instruction::GetElementPtr) {
96 // Turn a gep into the specified offset.
97 if (CE->getNumOperands() == 3 &&
98 cast<Constant>(CE->getOperand(1))->isNullValue() &&
99 isa<ConstantInt>(CE->getOperand(2))) {
100 return getStringValue(CE->getOperand(0),
101 Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
109 /// isStringValue - Return true if the given value can be coerced to a string.
111 static bool isStringValue(Value *V) {
112 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
113 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
114 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
115 return Init->isString();
117 } else if (Constant *C = dyn_cast<Constant>(V)) {
118 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
119 return isStringValue(GV);
120 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
121 if (CE->getOpcode() == Instruction::GetElementPtr) {
122 if (CE->getNumOperands() == 3 &&
123 cast<Constant>(CE->getOperand(1))->isNullValue() &&
124 isa<ConstantInt>(CE->getOperand(2))) {
125 return isStringValue(CE->getOperand(0));
133 /// getGlobalVariable - Return either a direct or cast Global value.
135 static GlobalVariable *getGlobalVariable(Value *V) {
136 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
138 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
139 if (CE->getOpcode() == Instruction::Cast) {
140 return dyn_cast<GlobalVariable>(CE->getOperand(0));
146 /// isGlobalVariable - Return true if the given value can be coerced to a
148 static bool isGlobalVariable(Value *V) {
149 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
151 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
152 if (CE->getOpcode() == Instruction::Cast) {
153 return isa<GlobalVariable>(CE->getOperand(0));
159 /// getUIntOperand - Return ith operand if it is an unsigned integer.
161 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
162 // Make sure the GlobalVariable has an initializer.
163 if (!GV->hasInitializer()) return NULL;
165 // Get the initializer constant.
166 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
167 if (!CI) return NULL;
169 // Check if there is at least i + 1 operands.
170 unsigned N = CI->getNumOperands();
171 if (i >= N) return NULL;
174 return dyn_cast<ConstantUInt>(CI->getOperand(i));
176 //===----------------------------------------------------------------------===//
178 /// ApplyToFields - Target the visitor to each field of the debug information
180 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
181 DD->ApplyToFields(this);
184 //===----------------------------------------------------------------------===//
185 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
186 /// the supplied DebugInfoDesc.
187 class DICountVisitor : public DIVisitor {
189 unsigned Count; // Running count of fields.
192 DICountVisitor() : DIVisitor(), Count(0) {}
195 unsigned getCount() const { return Count; }
197 /// Apply - Count each of the fields.
199 virtual void Apply(int &Field) { ++Count; }
200 virtual void Apply(unsigned &Field) { ++Count; }
201 virtual void Apply(int64_t &Field) { ++Count; }
202 virtual void Apply(uint64_t &Field) { ++Count; }
203 virtual void Apply(bool &Field) { ++Count; }
204 virtual void Apply(std::string &Field) { ++Count; }
205 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
206 virtual void Apply(GlobalVariable *&Field) { ++Count; }
207 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
212 //===----------------------------------------------------------------------===//
213 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
214 /// supplied DebugInfoDesc.
215 class DIDeserializeVisitor : public DIVisitor {
217 DIDeserializer &DR; // Active deserializer.
218 unsigned I; // Current operand index.
219 ConstantStruct *CI; // GlobalVariable constant initializer.
222 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
226 , CI(cast<ConstantStruct>(GV->getInitializer()))
229 /// Apply - Set the value of each of the fields.
231 virtual void Apply(int &Field) {
232 Constant *C = CI->getOperand(I++);
233 Field = cast<ConstantSInt>(C)->getValue();
235 virtual void Apply(unsigned &Field) {
236 Constant *C = CI->getOperand(I++);
237 Field = cast<ConstantUInt>(C)->getValue();
239 virtual void Apply(int64_t &Field) {
240 Constant *C = CI->getOperand(I++);
241 Field = cast<ConstantSInt>(C)->getValue();
243 virtual void Apply(uint64_t &Field) {
244 Constant *C = CI->getOperand(I++);
245 Field = cast<ConstantUInt>(C)->getValue();
247 virtual void Apply(bool &Field) {
248 Constant *C = CI->getOperand(I++);
249 Field = cast<ConstantBool>(C)->getValue();
251 virtual void Apply(std::string &Field) {
252 Constant *C = CI->getOperand(I++);
253 Field = getStringValue(C);
255 virtual void Apply(DebugInfoDesc *&Field) {
256 Constant *C = CI->getOperand(I++);
257 Field = DR.Deserialize(C);
259 virtual void Apply(GlobalVariable *&Field) {
260 Constant *C = CI->getOperand(I++);
261 Field = getGlobalVariable(C);
263 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
264 Constant *C = CI->getOperand(I++);
265 GlobalVariable *GV = getGlobalVariable(C);
266 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
268 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
269 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
270 DebugInfoDesc *DE = DR.Deserialize(GVE);
276 //===----------------------------------------------------------------------===//
277 /// DISerializeVisitor - This DIVisitor serializes all the fields in
278 /// the supplied DebugInfoDesc.
279 class DISerializeVisitor : public DIVisitor {
281 DISerializer &SR; // Active serializer.
282 std::vector<Constant*> &Elements; // Element accumulator.
285 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
291 /// Apply - Set the value of each of the fields.
293 virtual void Apply(int &Field) {
294 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
296 virtual void Apply(unsigned &Field) {
297 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
299 virtual void Apply(int64_t &Field) {
300 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
302 virtual void Apply(uint64_t &Field) {
303 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
305 virtual void Apply(bool &Field) {
306 Elements.push_back(ConstantBool::get(Field));
308 virtual void Apply(std::string &Field) {
309 Elements.push_back(SR.getString(Field));
311 virtual void Apply(DebugInfoDesc *&Field) {
312 GlobalVariable *GV = NULL;
314 // If non-NULL the convert to global.
315 if (Field) GV = SR.Serialize(Field);
317 // FIXME - At some point should use specific type.
318 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
321 // Set to pointer to global.
322 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
325 Elements.push_back(ConstantPointerNull::get(EmptyTy));
328 virtual void Apply(GlobalVariable *&Field) {
329 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
331 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
333 Elements.push_back(ConstantPointerNull::get(EmptyTy));
336 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
337 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
338 unsigned N = Field.size();
339 ArrayType *AT = ArrayType::get(EmptyTy, N);
340 std::vector<Constant *> ArrayElements;
342 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
343 GlobalVariable *GVE = SR.Serialize(Field[i]);
344 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
345 ArrayElements.push_back(cast<Constant>(CE));
348 Constant *CA = ConstantArray::get(AT, ArrayElements);
349 GlobalVariable *CAGV = new GlobalVariable(AT, true,
350 GlobalValue::InternalLinkage,
351 CA, "llvm.dbg.array",
353 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
354 Elements.push_back(CAE);
358 //===----------------------------------------------------------------------===//
359 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
360 /// the supplied DebugInfoDesc.
361 class DIGetTypesVisitor : public DIVisitor {
363 DISerializer &SR; // Active serializer.
364 std::vector<const Type*> &Fields; // Type accumulator.
367 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
373 /// Apply - Set the value of each of the fields.
375 virtual void Apply(int &Field) {
376 Fields.push_back(Type::IntTy);
378 virtual void Apply(unsigned &Field) {
379 Fields.push_back(Type::UIntTy);
381 virtual void Apply(int64_t &Field) {
382 Fields.push_back(Type::IntTy);
384 virtual void Apply(uint64_t &Field) {
385 Fields.push_back(Type::UIntTy);
387 virtual void Apply(bool &Field) {
388 Fields.push_back(Type::BoolTy);
390 virtual void Apply(std::string &Field) {
391 Fields.push_back(SR.getStrPtrType());
393 virtual void Apply(DebugInfoDesc *&Field) {
394 // FIXME - At some point should use specific type.
395 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396 Fields.push_back(EmptyTy);
398 virtual void Apply(GlobalVariable *&Field) {
399 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
400 Fields.push_back(EmptyTy);
402 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
403 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
404 Fields.push_back(EmptyTy);
408 //===----------------------------------------------------------------------===//
409 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
410 /// a constant initializer.
411 class DIVerifyVisitor : public DIVisitor {
413 DIVerifier &VR; // Active verifier.
414 bool IsValid; // Validity status.
415 unsigned I; // Current operand index.
416 ConstantStruct *CI; // GlobalVariable constant initializer.
419 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
424 , CI(cast<ConstantStruct>(GV->getInitializer()))
429 bool isValid() const { return IsValid; }
431 /// Apply - Set the value of each of the fields.
433 virtual void Apply(int &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isa<ConstantInt>(C);
437 virtual void Apply(unsigned &Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isa<ConstantInt>(C);
441 virtual void Apply(int64_t &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isa<ConstantInt>(C);
445 virtual void Apply(uint64_t &Field) {
446 Constant *C = CI->getOperand(I++);
447 IsValid = IsValid && isa<ConstantInt>(C);
449 virtual void Apply(bool &Field) {
450 Constant *C = CI->getOperand(I++);
451 IsValid = IsValid && isa<ConstantBool>(C);
453 virtual void Apply(std::string &Field) {
454 Constant *C = CI->getOperand(I++);
455 IsValid = IsValid && isStringValue(C);
457 virtual void Apply(DebugInfoDesc *&Field) {
458 // FIXME - Prepare the correct descriptor.
459 Constant *C = CI->getOperand(I++);
460 IsValid = IsValid && isGlobalVariable(C);
462 virtual void Apply(GlobalVariable *&Field) {
463 Constant *C = CI->getOperand(I++);
464 IsValid = IsValid && isGlobalVariable(C);
466 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
467 Constant *C = CI->getOperand(I++);
468 IsValid = IsValid && isGlobalVariable(C);
469 if (!IsValid) return;
471 GlobalVariable *GV = getGlobalVariable(C);
472 IsValid = IsValid && GV && GV->hasInitializer();
473 if (!IsValid) return;
475 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
476 IsValid = IsValid && CA;
477 if (!IsValid) return;
479 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
480 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
481 if (!IsValid) return;
483 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
490 //===----------------------------------------------------------------------===//
492 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
494 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
495 ConstantUInt *C = getUIntOperand(GV, 0);
496 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
499 /// DescFactory - Create an instance of debug info descriptor based on Tag.
500 /// Return NULL if not a recognized Tag.
501 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
503 case DW_TAG_anchor: return new AnchorDesc();
504 case DW_TAG_compile_unit: return new CompileUnitDesc();
505 case DW_TAG_variable: return new GlobalVariableDesc();
506 case DW_TAG_subprogram: return new SubprogramDesc();
507 case DW_TAG_base_type: return new BasicTypeDesc();
509 case DW_TAG_pointer_type:
510 case DW_TAG_reference_type:
511 case DW_TAG_const_type:
512 case DW_TAG_volatile_type:
513 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
514 case DW_TAG_array_type:
515 case DW_TAG_structure_type:
516 case DW_TAG_union_type:
517 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
518 case DW_TAG_subrange_type: return new SubrangeDesc();
519 case DW_TAG_member: return new DerivedTypeDesc(DW_TAG_member);
520 case DW_TAG_enumerator: return new EnumeratorDesc();
526 /// getLinkage - get linkage appropriate for this type of descriptor.
528 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
529 return GlobalValue::InternalLinkage;
532 /// ApplyToFields - Target the vistor to the fields of the descriptor.
534 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
538 //===----------------------------------------------------------------------===//
540 AnchorDesc::AnchorDesc()
541 : DebugInfoDesc(DW_TAG_anchor)
544 AnchorDesc::AnchorDesc(const std::string &N)
545 : DebugInfoDesc(DW_TAG_anchor)
549 // Implement isa/cast/dyncast.
550 bool AnchorDesc::classof(const DebugInfoDesc *D) {
551 return D->getTag() == DW_TAG_anchor;
554 /// getLinkage - get linkage appropriate for this type of descriptor.
556 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
557 return GlobalValue::LinkOnceLinkage;
560 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
562 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
563 DebugInfoDesc::ApplyToFields(Visitor);
565 Visitor->Apply(Name);
568 /// getDescString - Return a string used to compose global names and labels.
570 const char *AnchorDesc::getDescString() const {
574 /// getTypeString - Return a string used to label this descriptors type.
576 const char *AnchorDesc::getTypeString() const {
577 return "llvm.dbg.anchor.type";
581 void AnchorDesc::dump() {
582 std::cerr << getDescString() << " "
583 << "Tag(" << getTag() << "), "
584 << "Name(" << Name << ")\n";
588 //===----------------------------------------------------------------------===//
590 AnchoredDesc::AnchoredDesc(unsigned T)
595 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
597 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
598 DebugInfoDesc::ApplyToFields(Visitor);
600 Visitor->Apply((DebugInfoDesc *&)Anchor);
603 //===----------------------------------------------------------------------===//
605 CompileUnitDesc::CompileUnitDesc()
606 : AnchoredDesc(DW_TAG_compile_unit)
607 , DebugVersion(LLVMDebugVersion)
614 // Implement isa/cast/dyncast.
615 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
616 return D->getTag() == DW_TAG_compile_unit;
619 /// DebugVersionFromGlobal - Returns the version number from a compile unit
621 unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
622 ConstantUInt *C = getUIntOperand(GV, 2);
623 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
626 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
628 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
629 AnchoredDesc::ApplyToFields(Visitor);
631 Visitor->Apply(DebugVersion);
632 Visitor->Apply(Language);
633 Visitor->Apply(FileName);
634 Visitor->Apply(Directory);
635 Visitor->Apply(Producer);
638 /// getDescString - Return a string used to compose global names and labels.
640 const char *CompileUnitDesc::getDescString() const {
641 return "llvm.dbg.compile_unit";
644 /// getTypeString - Return a string used to label this descriptors type.
646 const char *CompileUnitDesc::getTypeString() const {
647 return "llvm.dbg.compile_unit.type";
650 /// getAnchorString - Return a string used to label this descriptor's anchor.
652 const char *CompileUnitDesc::getAnchorString() const {
653 return "llvm.dbg.compile_units";
657 void CompileUnitDesc::dump() {
658 std::cerr << getDescString() << " "
659 << "Tag(" << getTag() << "), "
660 << "Anchor(" << getAnchor() << "), "
661 << "DebugVersion(" << DebugVersion << "), "
662 << "Language(" << Language << "), "
663 << "FileName(\"" << FileName << "\"), "
664 << "Directory(\"" << Directory << "\"), "
665 << "Producer(\"" << Producer << "\")\n";
669 //===----------------------------------------------------------------------===//
671 TypeDesc::TypeDesc(unsigned T)
680 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
682 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
683 DebugInfoDesc::ApplyToFields(Visitor);
685 Visitor->Apply(Context);
686 Visitor->Apply(Name);
687 Visitor->Apply((DebugInfoDesc *&)File);
688 Visitor->Apply(Line);
689 Visitor->Apply(Size);
690 Visitor->Apply(Offset);
693 /// getDescString - Return a string used to compose global names and labels.
695 const char *TypeDesc::getDescString() const {
696 return "llvm.dbg.type";
699 /// getTypeString - Return a string used to label this descriptor's type.
701 const char *TypeDesc::getTypeString() const {
702 return "llvm.dbg.type.type";
706 void TypeDesc::dump() {
707 std::cerr << getDescString() << " "
708 << "Tag(" << getTag() << "), "
709 << "Context(" << Context << "), "
710 << "Name(\"" << Name << "\"), "
711 << "File(" << File << "), "
712 << "Line(" << Line << "), "
713 << "Size(" << Size << "), "
714 << "Offset(" << Offset << ")\n";
718 //===----------------------------------------------------------------------===//
720 BasicTypeDesc::BasicTypeDesc()
721 : TypeDesc(DW_TAG_base_type)
725 // Implement isa/cast/dyncast.
726 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
727 return D->getTag() == DW_TAG_base_type;
730 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
732 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
733 TypeDesc::ApplyToFields(Visitor);
735 Visitor->Apply(Encoding);
738 /// getDescString - Return a string used to compose global names and labels.
740 const char *BasicTypeDesc::getDescString() const {
741 return "llvm.dbg.basictype";
744 /// getTypeString - Return a string used to label this descriptor's type.
746 const char *BasicTypeDesc::getTypeString() const {
747 return "llvm.dbg.basictype.type";
751 void BasicTypeDesc::dump() {
752 std::cerr << getDescString() << " "
753 << "Tag(" << getTag() << "), "
754 << "Context(" << getContext() << "), "
755 << "Name(\"" << getName() << "\"), "
756 << "Size(" << getSize() << "), "
757 << "Encoding(" << Encoding << ")\n";
761 //===----------------------------------------------------------------------===//
763 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
768 // Implement isa/cast/dyncast.
769 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
770 unsigned T = D->getTag();
773 case DW_TAG_pointer_type:
774 case DW_TAG_reference_type:
775 case DW_TAG_const_type:
776 case DW_TAG_volatile_type:
777 case DW_TAG_restrict_type:
785 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
787 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
788 TypeDesc::ApplyToFields(Visitor);
790 Visitor->Apply((DebugInfoDesc *&)FromType);
793 /// getDescString - Return a string used to compose global names and labels.
795 const char *DerivedTypeDesc::getDescString() const {
796 return "llvm.dbg.derivedtype";
799 /// getTypeString - Return a string used to label this descriptor's type.
801 const char *DerivedTypeDesc::getTypeString() const {
802 return "llvm.dbg.derivedtype.type";
806 void DerivedTypeDesc::dump() {
807 std::cerr << getDescString() << " "
808 << "Tag(" << getTag() << "), "
809 << "Context(" << getContext() << "), "
810 << "Name(\"" << getName() << "\"), "
811 << "Size(" << getSize() << "), "
812 << "File(" << getFile() << "), "
813 << "Line(" << getLine() << "), "
814 << "FromType(" << FromType << ")\n";
818 //===----------------------------------------------------------------------===//
820 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
825 // Implement isa/cast/dyncast.
826 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
827 unsigned T = D->getTag();
829 case DW_TAG_array_type:
830 case DW_TAG_structure_type:
831 case DW_TAG_union_type:
832 case DW_TAG_enumeration_type:
839 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
841 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
842 DerivedTypeDesc::ApplyToFields(Visitor);
844 Visitor->Apply(Elements);
847 /// getDescString - Return a string used to compose global names and labels.
849 const char *CompositeTypeDesc::getDescString() const {
850 return "llvm.dbg.compositetype";
853 /// getTypeString - Return a string used to label this descriptor's type.
855 const char *CompositeTypeDesc::getTypeString() const {
856 return "llvm.dbg.compositetype.type";
860 void CompositeTypeDesc::dump() {
861 std::cerr << getDescString() << " "
862 << "Tag(" << getTag() << "), "
863 << "Context(" << getContext() << "), "
864 << "Name(\"" << getName() << "\"), "
865 << "Size(" << getSize() << "), "
866 << "File(" << getFile() << "), "
867 << "Line(" << getLine() << "), "
868 << "FromType(" << getFromType() << "), "
869 << "Elements.size(" << Elements.size() << ")\n";
873 //===----------------------------------------------------------------------===//
875 SubrangeDesc::SubrangeDesc()
876 : DebugInfoDesc(DW_TAG_subrange_type)
881 // Implement isa/cast/dyncast.
882 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
883 return D->getTag() == DW_TAG_subrange_type;
886 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
888 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
889 DebugInfoDesc::ApplyToFields(Visitor);
895 /// getDescString - Return a string used to compose global names and labels.
897 const char *SubrangeDesc::getDescString() const {
898 return "llvm.dbg.subrange";
901 /// getTypeString - Return a string used to label this descriptor's type.
903 const char *SubrangeDesc::getTypeString() const {
904 return "llvm.dbg.subrange.type";
908 void SubrangeDesc::dump() {
909 std::cerr << getDescString() << " "
910 << "Tag(" << getTag() << "), "
911 << "Lo(" << Lo << "), "
912 << "Hi(" << Hi << ")\n";
916 //===----------------------------------------------------------------------===//
918 EnumeratorDesc::EnumeratorDesc()
919 : DebugInfoDesc(DW_TAG_enumerator)
924 // Implement isa/cast/dyncast.
925 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
926 return D->getTag() == DW_TAG_enumerator;
929 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
931 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
932 DebugInfoDesc::ApplyToFields(Visitor);
934 Visitor->Apply(Name);
935 Visitor->Apply(Value);
938 /// getDescString - Return a string used to compose global names and labels.
940 const char *EnumeratorDesc::getDescString() const {
941 return "llvm.dbg.enumerator";
944 /// getTypeString - Return a string used to label this descriptor's type.
946 const char *EnumeratorDesc::getTypeString() const {
947 return "llvm.dbg.enumerator.type";
951 void EnumeratorDesc::dump() {
952 std::cerr << getDescString() << " "
953 << "Tag(" << getTag() << "), "
954 << "Name(" << Name << "), "
955 << "Value(" << Value << ")\n";
959 //===----------------------------------------------------------------------===//
961 GlobalDesc::GlobalDesc(unsigned T)
967 , IsDefinition(false)
970 /// ApplyToFields - Target the visitor to the fields of the global.
972 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
973 AnchoredDesc::ApplyToFields(Visitor);
975 Visitor->Apply(Context);
976 Visitor->Apply(Name);
977 Visitor->Apply((DebugInfoDesc *&)TyDesc);
978 Visitor->Apply(IsStatic);
979 Visitor->Apply(IsDefinition);
982 //===----------------------------------------------------------------------===//
984 GlobalVariableDesc::GlobalVariableDesc()
985 : GlobalDesc(DW_TAG_variable)
989 // Implement isa/cast/dyncast.
990 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
991 return D->getTag() == DW_TAG_variable;
994 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
996 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
997 GlobalDesc::ApplyToFields(Visitor);
999 Visitor->Apply(Global);
1000 Visitor->Apply(Line);
1003 /// getDescString - Return a string used to compose global names and labels.
1005 const char *GlobalVariableDesc::getDescString() const {
1006 return "llvm.dbg.global_variable";
1009 /// getTypeString - Return a string used to label this descriptors type.
1011 const char *GlobalVariableDesc::getTypeString() const {
1012 return "llvm.dbg.global_variable.type";
1015 /// getAnchorString - Return a string used to label this descriptor's anchor.
1017 const char *GlobalVariableDesc::getAnchorString() const {
1018 return "llvm.dbg.global_variables";
1022 void GlobalVariableDesc::dump() {
1023 std::cerr << getDescString() << " "
1024 << "Tag(" << getTag() << "), "
1025 << "Anchor(" << getAnchor() << "), "
1026 << "Name(\"" << getName() << "\"), "
1027 << "Type(\"" << getTypeDesc() << "\"), "
1028 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1029 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1030 << "Global(" << Global << "), "
1031 << "Line(" << Line << ")\n";
1035 //===----------------------------------------------------------------------===//
1037 SubprogramDesc::SubprogramDesc()
1038 : GlobalDesc(DW_TAG_subprogram)
1041 // Implement isa/cast/dyncast.
1042 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1043 return D->getTag() == DW_TAG_subprogram;
1046 /// ApplyToFields - Target the visitor to the fields of the
1048 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1049 GlobalDesc::ApplyToFields(Visitor);
1052 /// getDescString - Return a string used to compose global names and labels.
1054 const char *SubprogramDesc::getDescString() const {
1055 return "llvm.dbg.subprogram";
1058 /// getTypeString - Return a string used to label this descriptors type.
1060 const char *SubprogramDesc::getTypeString() const {
1061 return "llvm.dbg.subprogram.type";
1064 /// getAnchorString - Return a string used to label this descriptor's anchor.
1066 const char *SubprogramDesc::getAnchorString() const {
1067 return "llvm.dbg.subprograms";
1071 void SubprogramDesc::dump() {
1072 std::cerr << getDescString() << " "
1073 << "Tag(" << getTag() << "), "
1074 << "Anchor(" << getAnchor() << "), "
1075 << "Name(\"" << getName() << "\"), "
1076 << "Type(\"" << getTypeDesc() << "\"), "
1077 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1078 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1082 //===----------------------------------------------------------------------===//
1084 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1085 return Deserialize(getGlobalVariable(V));
1087 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1089 if (!GV) return NULL;
1091 // Check to see if it has been already deserialized.
1092 DebugInfoDesc *&Slot = GlobalDescs[GV];
1093 if (Slot) return Slot;
1095 // Get the Tag from the global.
1096 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1098 // Get the debug version if a compile unit.
1099 if (Tag == DW_TAG_compile_unit) {
1100 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1103 // Create an empty instance of the correct sort.
1104 Slot = DebugInfoDesc::DescFactory(Tag);
1105 assert(Slot && "Unknown Tag");
1107 // Deserialize the fields.
1108 DIDeserializeVisitor DRAM(*this, GV);
1109 DRAM.ApplyToFields(Slot);
1114 //===----------------------------------------------------------------------===//
1116 /// getStrPtrType - Return a "sbyte *" type.
1118 const PointerType *DISerializer::getStrPtrType() {
1119 // If not already defined.
1121 // Construct the pointer to signed bytes.
1122 StrPtrTy = PointerType::get(Type::SByteTy);
1128 /// getEmptyStructPtrType - Return a "{ }*" type.
1130 const PointerType *DISerializer::getEmptyStructPtrType() {
1131 // If not already defined.
1132 if (!EmptyStructPtrTy) {
1133 // Construct the empty structure type.
1134 const StructType *EmptyStructTy =
1135 StructType::get(std::vector<const Type*>());
1136 // Construct the pointer to empty structure type.
1137 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1140 return EmptyStructPtrTy;
1143 /// getTagType - Return the type describing the specified descriptor (via tag.)
1145 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1146 // Attempt to get the previously defined type.
1147 StructType *&Ty = TagTypes[DD->getTag()];
1149 // If not already defined.
1151 // Set up fields vector.
1152 std::vector<const Type*> Fields;
1153 // Get types of fields.
1154 DIGetTypesVisitor GTAM(*this, Fields);
1155 GTAM.ApplyToFields(DD);
1157 // Construct structured type.
1158 Ty = StructType::get(Fields);
1160 // Register type name with module.
1161 M->addTypeName(DD->getTypeString(), Ty);
1167 /// getString - Construct the string as constant string global.
1169 Constant *DISerializer::getString(const std::string &String) {
1170 // Check string cache for previous edition.
1171 Constant *&Slot = StringCache[String];
1172 // return Constant if previously defined.
1173 if (Slot) return Slot;
1174 // Construct string as an llvm constant.
1175 Constant *ConstStr = ConstantArray::get(String);
1176 // Otherwise create and return a new string global.
1177 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1178 GlobalVariable::InternalLinkage,
1179 ConstStr, "str", M);
1180 // Convert to generic string pointer.
1181 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1186 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1187 /// so that it can be serialized to a .bc or .ll file.
1188 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1189 // Check if the DebugInfoDesc is already in the map.
1190 GlobalVariable *&Slot = DescGlobals[DD];
1192 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1193 if (Slot) return Slot;
1195 // Get the type associated with the Tag.
1196 const StructType *Ty = getTagType(DD);
1198 // Create the GlobalVariable early to prevent infinite recursion.
1199 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1200 NULL, DD->getDescString(), M);
1202 // Insert new GlobalVariable in DescGlobals map.
1205 // Set up elements vector
1206 std::vector<Constant*> Elements;
1208 DISerializeVisitor SRAM(*this, Elements);
1209 SRAM.ApplyToFields(DD);
1211 // Set the globals initializer.
1212 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1217 //===----------------------------------------------------------------------===//
1219 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
1220 /// Mark visited otherwise.
1221 bool DIVerifier::markVisited(GlobalVariable *GV) {
1222 // Check if the GlobalVariable is already in the Visited set.
1223 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1225 // See if GlobalVariable exists.
1226 bool Exists = VI != Visited.end() && *VI == GV;
1229 if (!Exists) Visited.insert(VI, GV);
1234 /// Verify - Return true if the GlobalVariable appears to be a valid
1235 /// serialization of a DebugInfoDesc.
1236 bool DIVerifier::Verify(Value *V) {
1237 return Verify(getGlobalVariable(V));
1239 bool DIVerifier::Verify(GlobalVariable *GV) {
1240 // Check if seen before.
1241 if (markVisited(GV)) return true;
1244 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1245 if (Tag == DW_TAG_invalid) return false;
1247 // If a compile unit we need the debug version.
1248 if (Tag == DW_TAG_compile_unit) {
1249 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1250 if (DebugVersion == DW_TAG_invalid) return false;
1253 // Construct an empty DebugInfoDesc.
1254 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1255 if (!DD) return false;
1257 // Get the initializer constant.
1258 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1260 // Get the operand count.
1261 unsigned N = CI->getNumOperands();
1263 // Get the field count.
1264 unsigned &Slot = Counts[Tag];
1266 // Check the operand count to the field count
1267 DICountVisitor CTAM;
1268 CTAM.ApplyToFields(DD);
1269 Slot = CTAM.getCount();
1272 // Field count must equal operand count.
1278 // Check each field for valid type.
1279 DIVerifyVisitor VRAM(*this, GV);
1280 VRAM.ApplyToFields(DD);
1282 // Release empty DebugInfoDesc.
1285 // Return result of field tests.
1286 return VRAM.isValid();
1289 //===----------------------------------------------------------------------===//
1292 MachineDebugInfo::MachineDebugInfo()
1301 MachineDebugInfo::~MachineDebugInfo() {
1305 /// doInitialization - Initialize the debug state for a new module.
1307 bool MachineDebugInfo::doInitialization() {
1311 /// doFinalization - Tear down the debug state after completion of a module.
1313 bool MachineDebugInfo::doFinalization() {
1317 /// getDescFor - Convert a Value to a debug information descriptor.
1319 // FIXME - use new Value type when available.
1320 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
1321 return DR.Deserialize(V);
1324 /// Verify - Verify that a Value is debug information descriptor.
1326 bool MachineDebugInfo::Verify(Value *V) {
1328 return VR.Verify(V);
1331 /// AnalyzeModule - Scan the module for global debug information.
1333 void MachineDebugInfo::AnalyzeModule(Module &M) {
1334 SetupCompileUnits(M);
1337 /// SetupCompileUnits - Set up the unique vector of compile units.
1339 void MachineDebugInfo::SetupCompileUnits(Module &M) {
1340 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1342 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1343 CompileUnits.insert(CU[i]);
1347 /// getCompileUnits - Return a vector of debug compile units.
1349 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
1350 return CompileUnits;
1353 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1354 /// named GlobalVariable.
1355 std::vector<GlobalVariable*>
1356 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1357 const std::string &RootName) {
1358 return ::getGlobalVariablesUsing(M, RootName);