Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / CodeGen / MachineDebugInfo.cpp
blobfa2f57f0e0c85db5de23906231db7a4a520b1582
1 //===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineDebugInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/CodeGen/MachineLocation.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/Intrinsics.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/Dwarf.h"
21 #include <iostream>
23 using namespace llvm;
24 using namespace llvm::dwarf;
26 // Handle the Pass registration stuff necessary to use TargetData's.
27 namespace {
28 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
31 //===----------------------------------------------------------------------===//
33 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
34 /// specified value in their initializer somewhere.
35 static void
36 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
37 // Scan though value users.
38 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
39 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
40 // If the user is a GlobalVariable then add to result.
41 Result.push_back(GV);
42 } else if (Constant *C = dyn_cast<Constant>(*I)) {
43 // If the user is a constant variable then scan its users
44 getGlobalVariablesUsing(C, Result);
49 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
50 /// named GlobalVariable.
51 static std::vector<GlobalVariable*>
52 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
53 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
55 std::vector<const Type*> FieldTypes;
56 FieldTypes.push_back(Type::UIntTy);
57 FieldTypes.push_back(Type::UIntTy);
59 // Get the GlobalVariable root.
60 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
61 StructType::get(FieldTypes));
63 // If present and linkonce then scan for users.
64 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
65 getGlobalVariablesUsing(UseRoot, Result);
68 return Result;
71 /// isStringValue - Return true if the given value can be coerced to a string.
72 ///
73 static bool isStringValue(Value *V) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 return Init->isString();
79 } else if (Constant *C = dyn_cast<Constant>(V)) {
80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
81 return isStringValue(GV);
82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 if (CE->getOpcode() == Instruction::GetElementPtr) {
84 if (CE->getNumOperands() == 3 &&
85 cast<Constant>(CE->getOperand(1))->isNullValue() &&
86 isa<ConstantInt>(CE->getOperand(2))) {
87 return isStringValue(CE->getOperand(0));
92 return false;
95 /// getGlobalVariable - Return either a direct or cast Global value.
96 ///
97 static GlobalVariable *getGlobalVariable(Value *V) {
98 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
99 return GV;
100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
101 if (CE->getOpcode() == Instruction::Cast) {
102 return dyn_cast<GlobalVariable>(CE->getOperand(0));
105 return NULL;
108 /// isGlobalVariable - Return true if the given value can be coerced to a
109 /// GlobalVariable.
110 static bool isGlobalVariable(Value *V) {
111 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
112 return true;
113 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
114 if (CE->getOpcode() == Instruction::Cast) {
115 return isa<GlobalVariable>(CE->getOperand(0));
118 return false;
121 /// getUIntOperand - Return ith operand if it is an unsigned integer.
123 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
124 // Make sure the GlobalVariable has an initializer.
125 if (!GV->hasInitializer()) return NULL;
127 // Get the initializer constant.
128 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
129 if (!CI) return NULL;
131 // Check if there is at least i + 1 operands.
132 unsigned N = CI->getNumOperands();
133 if (i >= N) return NULL;
135 // Check constant.
136 return dyn_cast<ConstantUInt>(CI->getOperand(i));
138 //===----------------------------------------------------------------------===//
140 /// ApplyToFields - Target the visitor to each field of the debug information
141 /// descriptor.
142 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
143 DD->ApplyToFields(this);
146 //===----------------------------------------------------------------------===//
147 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
148 /// the supplied DebugInfoDesc.
149 class DICountVisitor : public DIVisitor {
150 private:
151 unsigned Count; // Running count of fields.
153 public:
154 DICountVisitor() : DIVisitor(), Count(0) {}
156 // Accessors.
157 unsigned getCount() const { return Count; }
159 /// Apply - Count each of the fields.
161 virtual void Apply(int &Field) { ++Count; }
162 virtual void Apply(unsigned &Field) { ++Count; }
163 virtual void Apply(int64_t &Field) { ++Count; }
164 virtual void Apply(uint64_t &Field) { ++Count; }
165 virtual void Apply(bool &Field) { ++Count; }
166 virtual void Apply(std::string &Field) { ++Count; }
167 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
168 virtual void Apply(GlobalVariable *&Field) { ++Count; }
169 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
170 ++Count;
174 //===----------------------------------------------------------------------===//
175 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
176 /// supplied DebugInfoDesc.
177 class DIDeserializeVisitor : public DIVisitor {
178 private:
179 DIDeserializer &DR; // Active deserializer.
180 unsigned I; // Current operand index.
181 ConstantStruct *CI; // GlobalVariable constant initializer.
183 public:
184 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
185 : DIVisitor()
186 , DR(D)
187 , I(0)
188 , CI(cast<ConstantStruct>(GV->getInitializer()))
191 /// Apply - Set the value of each of the fields.
193 virtual void Apply(int &Field) {
194 Constant *C = CI->getOperand(I++);
195 Field = cast<ConstantSInt>(C)->getValue();
197 virtual void Apply(unsigned &Field) {
198 Constant *C = CI->getOperand(I++);
199 Field = cast<ConstantUInt>(C)->getValue();
201 virtual void Apply(int64_t &Field) {
202 Constant *C = CI->getOperand(I++);
203 Field = cast<ConstantSInt>(C)->getValue();
205 virtual void Apply(uint64_t &Field) {
206 Constant *C = CI->getOperand(I++);
207 Field = cast<ConstantUInt>(C)->getValue();
209 virtual void Apply(bool &Field) {
210 Constant *C = CI->getOperand(I++);
211 Field = cast<ConstantBool>(C)->getValue();
213 virtual void Apply(std::string &Field) {
214 Constant *C = CI->getOperand(I++);
215 Field = C->getStringValue();
217 virtual void Apply(DebugInfoDesc *&Field) {
218 Constant *C = CI->getOperand(I++);
219 Field = DR.Deserialize(C);
221 virtual void Apply(GlobalVariable *&Field) {
222 Constant *C = CI->getOperand(I++);
223 Field = getGlobalVariable(C);
225 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
226 Field.resize(0);
227 Constant *C = CI->getOperand(I++);
228 GlobalVariable *GV = getGlobalVariable(C);
229 if (GV->hasInitializer()) {
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);
234 Field.push_back(DE);
236 } else if (GV->getInitializer()->isNullValue()) {
237 if (const ArrayType *T =
238 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
239 Field.resize(T->getNumElements());
246 //===----------------------------------------------------------------------===//
247 /// DISerializeVisitor - This DIVisitor serializes all the fields in
248 /// the supplied DebugInfoDesc.
249 class DISerializeVisitor : public DIVisitor {
250 private:
251 DISerializer &SR; // Active serializer.
252 std::vector<Constant*> &Elements; // Element accumulator.
254 public:
255 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
256 : DIVisitor()
257 , SR(S)
258 , Elements(E)
261 /// Apply - Set the value of each of the fields.
263 virtual void Apply(int &Field) {
264 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
266 virtual void Apply(unsigned &Field) {
267 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
269 virtual void Apply(int64_t &Field) {
270 Elements.push_back(ConstantSInt::get(Type::LongTy, Field));
272 virtual void Apply(uint64_t &Field) {
273 Elements.push_back(ConstantUInt::get(Type::ULongTy, Field));
275 virtual void Apply(bool &Field) {
276 Elements.push_back(ConstantBool::get(Field));
278 virtual void Apply(std::string &Field) {
279 Elements.push_back(SR.getString(Field));
281 virtual void Apply(DebugInfoDesc *&Field) {
282 GlobalVariable *GV = NULL;
284 // If non-NULL then convert to global.
285 if (Field) GV = SR.Serialize(Field);
287 // FIXME - At some point should use specific type.
288 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
290 if (GV) {
291 // Set to pointer to global.
292 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
293 } else {
294 // Use NULL.
295 Elements.push_back(ConstantPointerNull::get(EmptyTy));
298 virtual void Apply(GlobalVariable *&Field) {
299 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
300 if (Field) {
301 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
302 } else {
303 Elements.push_back(ConstantPointerNull::get(EmptyTy));
306 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
307 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
308 unsigned N = Field.size();
309 ArrayType *AT = ArrayType::get(EmptyTy, N);
310 std::vector<Constant *> ArrayElements;
312 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
313 if (DebugInfoDesc *Element = Field[i]) {
314 GlobalVariable *GVE = SR.Serialize(Element);
315 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
316 ArrayElements.push_back(cast<Constant>(CE));
317 } else {
318 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
322 Constant *CA = ConstantArray::get(AT, ArrayElements);
323 GlobalVariable *CAGV = new GlobalVariable(AT, true,
324 GlobalValue::InternalLinkage,
325 CA, "llvm.dbg.array",
326 SR.getModule());
327 CAGV->setSection("llvm.metadata");
328 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
329 Elements.push_back(CAE);
333 //===----------------------------------------------------------------------===//
334 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
335 /// the supplied DebugInfoDesc.
336 class DIGetTypesVisitor : public DIVisitor {
337 private:
338 DISerializer &SR; // Active serializer.
339 std::vector<const Type*> &Fields; // Type accumulator.
341 public:
342 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
343 : DIVisitor()
344 , SR(S)
345 , Fields(F)
348 /// Apply - Set the value of each of the fields.
350 virtual void Apply(int &Field) {
351 Fields.push_back(Type::IntTy);
353 virtual void Apply(unsigned &Field) {
354 Fields.push_back(Type::UIntTy);
356 virtual void Apply(int64_t &Field) {
357 Fields.push_back(Type::LongTy);
359 virtual void Apply(uint64_t &Field) {
360 Fields.push_back(Type::ULongTy);
362 virtual void Apply(bool &Field) {
363 Fields.push_back(Type::BoolTy);
365 virtual void Apply(std::string &Field) {
366 Fields.push_back(SR.getStrPtrType());
368 virtual void Apply(DebugInfoDesc *&Field) {
369 // FIXME - At some point should use specific type.
370 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
371 Fields.push_back(EmptyTy);
373 virtual void Apply(GlobalVariable *&Field) {
374 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
375 Fields.push_back(EmptyTy);
377 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
378 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
379 Fields.push_back(EmptyTy);
383 //===----------------------------------------------------------------------===//
384 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
385 /// a constant initializer.
386 class DIVerifyVisitor : public DIVisitor {
387 private:
388 DIVerifier &VR; // Active verifier.
389 bool IsValid; // Validity status.
390 unsigned I; // Current operand index.
391 ConstantStruct *CI; // GlobalVariable constant initializer.
393 public:
394 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
395 : DIVisitor()
396 , VR(V)
397 , IsValid(true)
398 , I(0)
399 , CI(cast<ConstantStruct>(GV->getInitializer()))
403 // Accessors.
404 bool isValid() const { return IsValid; }
406 /// Apply - Set the value of each of the fields.
408 virtual void Apply(int &Field) {
409 Constant *C = CI->getOperand(I++);
410 IsValid = IsValid && isa<ConstantInt>(C);
412 virtual void Apply(unsigned &Field) {
413 Constant *C = CI->getOperand(I++);
414 IsValid = IsValid && isa<ConstantInt>(C);
416 virtual void Apply(int64_t &Field) {
417 Constant *C = CI->getOperand(I++);
418 IsValid = IsValid && isa<ConstantInt>(C);
420 virtual void Apply(uint64_t &Field) {
421 Constant *C = CI->getOperand(I++);
422 IsValid = IsValid && isa<ConstantInt>(C);
424 virtual void Apply(bool &Field) {
425 Constant *C = CI->getOperand(I++);
426 IsValid = IsValid && isa<ConstantBool>(C);
428 virtual void Apply(std::string &Field) {
429 Constant *C = CI->getOperand(I++);
430 IsValid = IsValid && (!C || isStringValue(C));
432 virtual void Apply(DebugInfoDesc *&Field) {
433 // FIXME - Prepare the correct descriptor.
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isGlobalVariable(C);
437 virtual void Apply(GlobalVariable *&Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isGlobalVariable(C);
441 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isGlobalVariable(C);
444 if (!IsValid) return;
446 GlobalVariable *GV = getGlobalVariable(C);
447 IsValid = IsValid && GV && GV->hasInitializer();
448 if (!IsValid) return;
450 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
451 IsValid = IsValid && CA;
452 if (!IsValid) return;
454 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
455 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
456 if (!IsValid) return;
458 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
459 VR.Verify(GVE);
465 //===----------------------------------------------------------------------===//
467 /// TagFromGlobal - Returns the tag number from a debug info descriptor
468 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
469 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
470 ConstantUInt *C = getUIntOperand(GV, 0);
471 return C ? ((unsigned)C->getValue() & ~LLVMDebugVersionMask) :
472 (unsigned)DW_TAG_invalid;
475 /// VersionFromGlobal - Returns the version number from a debug info
476 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
477 /// int.
478 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
479 ConstantUInt *C = getUIntOperand(GV, 0);
480 return C ? ((unsigned)C->getValue() & LLVMDebugVersionMask) :
481 (unsigned)DW_TAG_invalid;
484 /// DescFactory - Create an instance of debug info descriptor based on Tag.
485 /// Return NULL if not a recognized Tag.
486 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
487 switch (Tag) {
488 case DW_TAG_anchor: return new AnchorDesc();
489 case DW_TAG_compile_unit: return new CompileUnitDesc();
490 case DW_TAG_variable: return new GlobalVariableDesc();
491 case DW_TAG_subprogram: return new SubprogramDesc();
492 case DW_TAG_lexical_block: return new BlockDesc();
493 case DW_TAG_base_type: return new BasicTypeDesc();
494 case DW_TAG_typedef:
495 case DW_TAG_pointer_type:
496 case DW_TAG_reference_type:
497 case DW_TAG_const_type:
498 case DW_TAG_volatile_type:
499 case DW_TAG_restrict_type:
500 case DW_TAG_member: return new DerivedTypeDesc(Tag);
501 case DW_TAG_array_type:
502 case DW_TAG_structure_type:
503 case DW_TAG_union_type:
504 case DW_TAG_enumeration_type:
505 case DW_TAG_vector_type:
506 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
507 case DW_TAG_subrange_type: return new SubrangeDesc();
508 case DW_TAG_enumerator: return new EnumeratorDesc();
509 case DW_TAG_return_variable:
510 case DW_TAG_arg_variable:
511 case DW_TAG_auto_variable: return new VariableDesc(Tag);
512 default: break;
514 return NULL;
517 /// getLinkage - get linkage appropriate for this type of descriptor.
519 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
520 return GlobalValue::InternalLinkage;
523 /// ApplyToFields - Target the vistor to the fields of the descriptor.
525 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
526 Visitor->Apply(Tag);
529 //===----------------------------------------------------------------------===//
531 AnchorDesc::AnchorDesc()
532 : DebugInfoDesc(DW_TAG_anchor)
533 , AnchorTag(0)
535 AnchorDesc::AnchorDesc(AnchoredDesc *D)
536 : DebugInfoDesc(DW_TAG_anchor)
537 , AnchorTag(D->getTag())
540 // Implement isa/cast/dyncast.
541 bool AnchorDesc::classof(const DebugInfoDesc *D) {
542 return D->getTag() == DW_TAG_anchor;
545 /// getLinkage - get linkage appropriate for this type of descriptor.
547 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
548 return GlobalValue::LinkOnceLinkage;
551 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
553 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
554 DebugInfoDesc::ApplyToFields(Visitor);
556 Visitor->Apply(AnchorTag);
559 /// getDescString - Return a string used to compose global names and labels. A
560 /// A global variable name needs to be defined for each debug descriptor that is
561 /// anchored. NOTE: that each global variable named here also needs to be added
562 /// to the list of names left external in the internalizer.
563 /// ExternalNames.insert("llvm.dbg.compile_units");
564 /// ExternalNames.insert("llvm.dbg.global_variables");
565 /// ExternalNames.insert("llvm.dbg.subprograms");
566 const char *AnchorDesc::getDescString() const {
567 switch (AnchorTag) {
568 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
569 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
570 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
571 default: break;
574 assert(0 && "Tag does not have a case for anchor string");
575 return "";
578 /// getTypeString - Return a string used to label this descriptors type.
580 const char *AnchorDesc::getTypeString() const {
581 return "llvm.dbg.anchor.type";
584 #ifndef NDEBUG
585 void AnchorDesc::dump() {
586 std::cerr << getDescString() << " "
587 << "Version(" << getVersion() << "), "
588 << "Tag(" << getTag() << "), "
589 << "AnchorTag(" << AnchorTag << ")\n";
591 #endif
593 //===----------------------------------------------------------------------===//
595 AnchoredDesc::AnchoredDesc(unsigned T)
596 : DebugInfoDesc(T)
597 , Anchor(NULL)
600 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
602 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
603 DebugInfoDesc::ApplyToFields(Visitor);
605 Visitor->Apply(Anchor);
608 //===----------------------------------------------------------------------===//
610 CompileUnitDesc::CompileUnitDesc()
611 : AnchoredDesc(DW_TAG_compile_unit)
612 , Language(0)
613 , FileName("")
614 , Directory("")
615 , Producer("")
618 // Implement isa/cast/dyncast.
619 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
620 return D->getTag() == DW_TAG_compile_unit;
623 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
625 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
626 AnchoredDesc::ApplyToFields(Visitor);
628 // Handle cases out of sync with compiler.
629 if (getVersion() == 0) {
630 unsigned DebugVersion;
631 Visitor->Apply(DebugVersion);
634 Visitor->Apply(Language);
635 Visitor->Apply(FileName);
636 Visitor->Apply(Directory);
637 Visitor->Apply(Producer);
640 /// getDescString - Return a string used to compose global names and labels.
642 const char *CompileUnitDesc::getDescString() const {
643 return "llvm.dbg.compile_unit";
646 /// getTypeString - Return a string used to label this descriptors type.
648 const char *CompileUnitDesc::getTypeString() const {
649 return "llvm.dbg.compile_unit.type";
652 /// getAnchorString - Return a string used to label this descriptor's anchor.
654 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
655 const char *CompileUnitDesc::getAnchorString() const {
656 return AnchorString;
659 #ifndef NDEBUG
660 void CompileUnitDesc::dump() {
661 std::cerr << getDescString() << " "
662 << "Version(" << getVersion() << "), "
663 << "Tag(" << getTag() << "), "
664 << "Anchor(" << getAnchor() << "), "
665 << "Language(" << Language << "), "
666 << "FileName(\"" << FileName << "\"), "
667 << "Directory(\"" << Directory << "\"), "
668 << "Producer(\"" << Producer << "\")\n";
670 #endif
672 //===----------------------------------------------------------------------===//
674 TypeDesc::TypeDesc(unsigned T)
675 : DebugInfoDesc(T)
676 , Context(NULL)
677 , Name("")
678 , File(NULL)
679 , Line(0)
680 , Size(0)
681 , Align(0)
682 , Offset(0)
683 , Flags(0)
686 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
688 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
689 DebugInfoDesc::ApplyToFields(Visitor);
691 Visitor->Apply(Context);
692 Visitor->Apply(Name);
693 Visitor->Apply(File);
694 Visitor->Apply(Line);
695 Visitor->Apply(Size);
696 Visitor->Apply(Align);
697 Visitor->Apply(Offset);
698 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
701 /// getDescString - Return a string used to compose global names and labels.
703 const char *TypeDesc::getDescString() const {
704 return "llvm.dbg.type";
707 /// getTypeString - Return a string used to label this descriptor's type.
709 const char *TypeDesc::getTypeString() const {
710 return "llvm.dbg.type.type";
713 #ifndef NDEBUG
714 void TypeDesc::dump() {
715 std::cerr << getDescString() << " "
716 << "Version(" << getVersion() << "), "
717 << "Tag(" << getTag() << "), "
718 << "Context(" << Context << "), "
719 << "Name(\"" << Name << "\"), "
720 << "File(" << File << "), "
721 << "Line(" << Line << "), "
722 << "Size(" << Size << "), "
723 << "Align(" << Align << "), "
724 << "Offset(" << Offset << "), "
725 << "Flags(" << Flags << ")\n";
727 #endif
729 //===----------------------------------------------------------------------===//
731 BasicTypeDesc::BasicTypeDesc()
732 : TypeDesc(DW_TAG_base_type)
733 , Encoding(0)
736 // Implement isa/cast/dyncast.
737 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
738 return D->getTag() == DW_TAG_base_type;
741 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
743 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
744 TypeDesc::ApplyToFields(Visitor);
746 Visitor->Apply(Encoding);
749 /// getDescString - Return a string used to compose global names and labels.
751 const char *BasicTypeDesc::getDescString() const {
752 return "llvm.dbg.basictype";
755 /// getTypeString - Return a string used to label this descriptor's type.
757 const char *BasicTypeDesc::getTypeString() const {
758 return "llvm.dbg.basictype.type";
761 #ifndef NDEBUG
762 void BasicTypeDesc::dump() {
763 std::cerr << getDescString() << " "
764 << "Version(" << getVersion() << "), "
765 << "Tag(" << getTag() << "), "
766 << "Context(" << getContext() << "), "
767 << "Name(\"" << getName() << "\"), "
768 << "Size(" << getSize() << "), "
769 << "Encoding(" << Encoding << ")\n";
771 #endif
773 //===----------------------------------------------------------------------===//
775 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
776 : TypeDesc(T)
777 , FromType(NULL)
780 // Implement isa/cast/dyncast.
781 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
782 unsigned T = D->getTag();
783 switch (T) {
784 case DW_TAG_typedef:
785 case DW_TAG_pointer_type:
786 case DW_TAG_reference_type:
787 case DW_TAG_const_type:
788 case DW_TAG_volatile_type:
789 case DW_TAG_restrict_type:
790 case DW_TAG_member:
791 return true;
792 default: break;
794 return false;
797 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
799 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
800 TypeDesc::ApplyToFields(Visitor);
802 Visitor->Apply(FromType);
805 /// getDescString - Return a string used to compose global names and labels.
807 const char *DerivedTypeDesc::getDescString() const {
808 return "llvm.dbg.derivedtype";
811 /// getTypeString - Return a string used to label this descriptor's type.
813 const char *DerivedTypeDesc::getTypeString() const {
814 return "llvm.dbg.derivedtype.type";
817 #ifndef NDEBUG
818 void DerivedTypeDesc::dump() {
819 std::cerr << getDescString() << " "
820 << "Version(" << getVersion() << "), "
821 << "Tag(" << getTag() << "), "
822 << "Context(" << getContext() << "), "
823 << "Name(\"" << getName() << "\"), "
824 << "Size(" << getSize() << "), "
825 << "File(" << getFile() << "), "
826 << "Line(" << getLine() << "), "
827 << "FromType(" << FromType << ")\n";
829 #endif
831 //===----------------------------------------------------------------------===//
833 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
834 : DerivedTypeDesc(T)
835 , Elements()
838 // Implement isa/cast/dyncast.
839 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
840 unsigned T = D->getTag();
841 switch (T) {
842 case DW_TAG_array_type:
843 case DW_TAG_structure_type:
844 case DW_TAG_union_type:
845 case DW_TAG_enumeration_type:
846 case DW_TAG_vector_type:
847 case DW_TAG_subroutine_type:
848 return true;
849 default: break;
851 return false;
854 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
856 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
857 DerivedTypeDesc::ApplyToFields(Visitor);
859 Visitor->Apply(Elements);
862 /// getDescString - Return a string used to compose global names and labels.
864 const char *CompositeTypeDesc::getDescString() const {
865 return "llvm.dbg.compositetype";
868 /// getTypeString - Return a string used to label this descriptor's type.
870 const char *CompositeTypeDesc::getTypeString() const {
871 return "llvm.dbg.compositetype.type";
874 #ifndef NDEBUG
875 void CompositeTypeDesc::dump() {
876 std::cerr << getDescString() << " "
877 << "Version(" << getVersion() << "), "
878 << "Tag(" << getTag() << "), "
879 << "Context(" << getContext() << "), "
880 << "Name(\"" << getName() << "\"), "
881 << "Size(" << getSize() << "), "
882 << "File(" << getFile() << "), "
883 << "Line(" << getLine() << "), "
884 << "FromType(" << getFromType() << "), "
885 << "Elements.size(" << Elements.size() << ")\n";
887 #endif
889 //===----------------------------------------------------------------------===//
891 SubrangeDesc::SubrangeDesc()
892 : DebugInfoDesc(DW_TAG_subrange_type)
893 , Lo(0)
894 , Hi(0)
897 // Implement isa/cast/dyncast.
898 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
899 return D->getTag() == DW_TAG_subrange_type;
902 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
904 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
905 DebugInfoDesc::ApplyToFields(Visitor);
907 Visitor->Apply(Lo);
908 Visitor->Apply(Hi);
911 /// getDescString - Return a string used to compose global names and labels.
913 const char *SubrangeDesc::getDescString() const {
914 return "llvm.dbg.subrange";
917 /// getTypeString - Return a string used to label this descriptor's type.
919 const char *SubrangeDesc::getTypeString() const {
920 return "llvm.dbg.subrange.type";
923 #ifndef NDEBUG
924 void SubrangeDesc::dump() {
925 std::cerr << getDescString() << " "
926 << "Version(" << getVersion() << "), "
927 << "Tag(" << getTag() << "), "
928 << "Lo(" << Lo << "), "
929 << "Hi(" << Hi << ")\n";
931 #endif
933 //===----------------------------------------------------------------------===//
935 EnumeratorDesc::EnumeratorDesc()
936 : DebugInfoDesc(DW_TAG_enumerator)
937 , Name("")
938 , Value(0)
941 // Implement isa/cast/dyncast.
942 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
943 return D->getTag() == DW_TAG_enumerator;
946 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
948 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
949 DebugInfoDesc::ApplyToFields(Visitor);
951 Visitor->Apply(Name);
952 Visitor->Apply(Value);
955 /// getDescString - Return a string used to compose global names and labels.
957 const char *EnumeratorDesc::getDescString() const {
958 return "llvm.dbg.enumerator";
961 /// getTypeString - Return a string used to label this descriptor's type.
963 const char *EnumeratorDesc::getTypeString() const {
964 return "llvm.dbg.enumerator.type";
967 #ifndef NDEBUG
968 void EnumeratorDesc::dump() {
969 std::cerr << getDescString() << " "
970 << "Version(" << getVersion() << "), "
971 << "Tag(" << getTag() << "), "
972 << "Name(" << Name << "), "
973 << "Value(" << Value << ")\n";
975 #endif
977 //===----------------------------------------------------------------------===//
979 VariableDesc::VariableDesc(unsigned T)
980 : DebugInfoDesc(T)
981 , Context(NULL)
982 , Name("")
983 , File(NULL)
984 , Line(0)
985 , TyDesc(0)
988 // Implement isa/cast/dyncast.
989 bool VariableDesc::classof(const DebugInfoDesc *D) {
990 unsigned T = D->getTag();
991 switch (T) {
992 case DW_TAG_auto_variable:
993 case DW_TAG_arg_variable:
994 case DW_TAG_return_variable:
995 return true;
996 default: break;
998 return false;
1001 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1003 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1004 DebugInfoDesc::ApplyToFields(Visitor);
1006 Visitor->Apply(Context);
1007 Visitor->Apply(Name);
1008 Visitor->Apply(File);
1009 Visitor->Apply(Line);
1010 Visitor->Apply(TyDesc);
1013 /// getDescString - Return a string used to compose global names and labels.
1015 const char *VariableDesc::getDescString() const {
1016 return "llvm.dbg.variable";
1019 /// getTypeString - Return a string used to label this descriptor's type.
1021 const char *VariableDesc::getTypeString() const {
1022 return "llvm.dbg.variable.type";
1025 #ifndef NDEBUG
1026 void VariableDesc::dump() {
1027 std::cerr << getDescString() << " "
1028 << "Version(" << getVersion() << "), "
1029 << "Tag(" << getTag() << "), "
1030 << "Context(" << Context << "), "
1031 << "Name(\"" << Name << "\"), "
1032 << "File(" << File << "), "
1033 << "Line(" << Line << "), "
1034 << "TyDesc(" << TyDesc << ")\n";
1036 #endif
1038 //===----------------------------------------------------------------------===//
1040 GlobalDesc::GlobalDesc(unsigned T)
1041 : AnchoredDesc(T)
1042 , Context(0)
1043 , Name("")
1044 , DisplayName("")
1045 , File(NULL)
1046 , Line(0)
1047 , TyDesc(NULL)
1048 , IsStatic(false)
1049 , IsDefinition(false)
1052 /// ApplyToFields - Target the visitor to the fields of the global.
1054 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1055 AnchoredDesc::ApplyToFields(Visitor);
1057 Visitor->Apply(Context);
1058 Visitor->Apply(Name);
1059 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
1060 Visitor->Apply(File);
1061 Visitor->Apply(Line);
1062 Visitor->Apply(TyDesc);
1063 Visitor->Apply(IsStatic);
1064 Visitor->Apply(IsDefinition);
1067 //===----------------------------------------------------------------------===//
1069 GlobalVariableDesc::GlobalVariableDesc()
1070 : GlobalDesc(DW_TAG_variable)
1071 , Global(NULL)
1074 // Implement isa/cast/dyncast.
1075 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1076 return D->getTag() == DW_TAG_variable;
1079 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1081 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1082 GlobalDesc::ApplyToFields(Visitor);
1084 Visitor->Apply(Global);
1087 /// getDescString - Return a string used to compose global names and labels.
1089 const char *GlobalVariableDesc::getDescString() const {
1090 return "llvm.dbg.global_variable";
1093 /// getTypeString - Return a string used to label this descriptors type.
1095 const char *GlobalVariableDesc::getTypeString() const {
1096 return "llvm.dbg.global_variable.type";
1099 /// getAnchorString - Return a string used to label this descriptor's anchor.
1101 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
1102 const char *GlobalVariableDesc::getAnchorString() const {
1103 return AnchorString;
1106 #ifndef NDEBUG
1107 void GlobalVariableDesc::dump() {
1108 std::cerr << getDescString() << " "
1109 << "Version(" << getVersion() << "), "
1110 << "Tag(" << getTag() << "), "
1111 << "Anchor(" << getAnchor() << "), "
1112 << "Name(\"" << getName() << "\"), "
1113 << "DisplayName(\"" << getDisplayName() << "\"), "
1114 << "File(" << getFile() << "),"
1115 << "Line(" << getLine() << "),"
1116 << "Type(\"" << getType() << "\"), "
1117 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1118 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1119 << "Global(" << Global << ")\n";
1121 #endif
1123 //===----------------------------------------------------------------------===//
1125 SubprogramDesc::SubprogramDesc()
1126 : GlobalDesc(DW_TAG_subprogram)
1129 // Implement isa/cast/dyncast.
1130 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1131 return D->getTag() == DW_TAG_subprogram;
1134 /// ApplyToFields - Target the visitor to the fields of the
1135 /// SubprogramDesc.
1136 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1137 GlobalDesc::ApplyToFields(Visitor);
1140 /// getDescString - Return a string used to compose global names and labels.
1142 const char *SubprogramDesc::getDescString() const {
1143 return "llvm.dbg.subprogram";
1146 /// getTypeString - Return a string used to label this descriptors type.
1148 const char *SubprogramDesc::getTypeString() const {
1149 return "llvm.dbg.subprogram.type";
1152 /// getAnchorString - Return a string used to label this descriptor's anchor.
1154 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
1155 const char *SubprogramDesc::getAnchorString() const {
1156 return AnchorString;
1159 #ifndef NDEBUG
1160 void SubprogramDesc::dump() {
1161 std::cerr << getDescString() << " "
1162 << "Version(" << getVersion() << "), "
1163 << "Tag(" << getTag() << "), "
1164 << "Anchor(" << getAnchor() << "), "
1165 << "Name(\"" << getName() << "\"), "
1166 << "DisplayName(\"" << getDisplayName() << "\"), "
1167 << "File(" << getFile() << "),"
1168 << "Line(" << getLine() << "),"
1169 << "Type(\"" << getType() << "\"), "
1170 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1171 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1173 #endif
1175 //===----------------------------------------------------------------------===//
1177 BlockDesc::BlockDesc()
1178 : DebugInfoDesc(DW_TAG_lexical_block)
1179 , Context(NULL)
1182 // Implement isa/cast/dyncast.
1183 bool BlockDesc::classof(const DebugInfoDesc *D) {
1184 return D->getTag() == DW_TAG_lexical_block;
1187 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1189 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1190 DebugInfoDesc::ApplyToFields(Visitor);
1192 Visitor->Apply(Context);
1195 /// getDescString - Return a string used to compose global names and labels.
1197 const char *BlockDesc::getDescString() const {
1198 return "llvm.dbg.block";
1201 /// getTypeString - Return a string used to label this descriptors type.
1203 const char *BlockDesc::getTypeString() const {
1204 return "llvm.dbg.block.type";
1207 #ifndef NDEBUG
1208 void BlockDesc::dump() {
1209 std::cerr << getDescString() << " "
1210 << "Version(" << getVersion() << "), "
1211 << "Tag(" << getTag() << "),"
1212 << "Context(" << Context << ")\n";
1214 #endif
1216 //===----------------------------------------------------------------------===//
1218 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1219 return Deserialize(getGlobalVariable(V));
1221 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1222 // Handle NULL.
1223 if (!GV) return NULL;
1225 // Check to see if it has been already deserialized.
1226 DebugInfoDesc *&Slot = GlobalDescs[GV];
1227 if (Slot) return Slot;
1229 // Get the Tag from the global.
1230 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1232 // Create an empty instance of the correct sort.
1233 Slot = DebugInfoDesc::DescFactory(Tag);
1235 // If not a user defined descriptor.
1236 if (Slot) {
1237 // Deserialize the fields.
1238 DIDeserializeVisitor DRAM(*this, GV);
1239 DRAM.ApplyToFields(Slot);
1242 return Slot;
1245 //===----------------------------------------------------------------------===//
1247 /// getStrPtrType - Return a "sbyte *" type.
1249 const PointerType *DISerializer::getStrPtrType() {
1250 // If not already defined.
1251 if (!StrPtrTy) {
1252 // Construct the pointer to signed bytes.
1253 StrPtrTy = PointerType::get(Type::SByteTy);
1256 return StrPtrTy;
1259 /// getEmptyStructPtrType - Return a "{ }*" type.
1261 const PointerType *DISerializer::getEmptyStructPtrType() {
1262 // If not already defined.
1263 if (!EmptyStructPtrTy) {
1264 // Construct the empty structure type.
1265 const StructType *EmptyStructTy =
1266 StructType::get(std::vector<const Type*>());
1267 // Construct the pointer to empty structure type.
1268 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1271 return EmptyStructPtrTy;
1274 /// getTagType - Return the type describing the specified descriptor (via tag.)
1276 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1277 // Attempt to get the previously defined type.
1278 StructType *&Ty = TagTypes[DD->getTag()];
1280 // If not already defined.
1281 if (!Ty) {
1282 // Set up fields vector.
1283 std::vector<const Type*> Fields;
1284 // Get types of fields.
1285 DIGetTypesVisitor GTAM(*this, Fields);
1286 GTAM.ApplyToFields(DD);
1288 // Construct structured type.
1289 Ty = StructType::get(Fields);
1291 // Register type name with module.
1292 M->addTypeName(DD->getTypeString(), Ty);
1295 return Ty;
1298 /// getString - Construct the string as constant string global.
1300 Constant *DISerializer::getString(const std::string &String) {
1301 // Check string cache for previous edition.
1302 Constant *&Slot = StringCache[String];
1303 // Return Constant if previously defined.
1304 if (Slot) return Slot;
1305 // If empty string then use a sbyte* null instead.
1306 if (String.empty()) {
1307 Slot = ConstantPointerNull::get(getStrPtrType());
1308 } else {
1309 // Construct string as an llvm constant.
1310 Constant *ConstStr = ConstantArray::get(String);
1311 // Otherwise create and return a new string global.
1312 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1313 GlobalVariable::InternalLinkage,
1314 ConstStr, "str", M);
1315 StrGV->setSection("llvm.metadata");
1316 // Convert to generic string pointer.
1317 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1319 return Slot;
1323 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1324 /// so that it can be serialized to a .bc or .ll file.
1325 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1326 // Check if the DebugInfoDesc is already in the map.
1327 GlobalVariable *&Slot = DescGlobals[DD];
1329 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1330 if (Slot) return Slot;
1332 // Get the type associated with the Tag.
1333 const StructType *Ty = getTagType(DD);
1335 // Create the GlobalVariable early to prevent infinite recursion.
1336 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1337 NULL, DD->getDescString(), M);
1338 GV->setSection("llvm.metadata");
1340 // Insert new GlobalVariable in DescGlobals map.
1341 Slot = GV;
1343 // Set up elements vector
1344 std::vector<Constant*> Elements;
1345 // Add fields.
1346 DISerializeVisitor SRAM(*this, Elements);
1347 SRAM.ApplyToFields(DD);
1349 // Set the globals initializer.
1350 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1352 return GV;
1355 //===----------------------------------------------------------------------===//
1357 /// Verify - Return true if the GlobalVariable appears to be a valid
1358 /// serialization of a DebugInfoDesc.
1359 bool DIVerifier::Verify(Value *V) {
1360 return !V || Verify(getGlobalVariable(V));
1362 bool DIVerifier::Verify(GlobalVariable *GV) {
1363 // NULLs are valid.
1364 if (!GV) return true;
1366 // Check prior validity.
1367 unsigned &ValiditySlot = Validity[GV];
1369 // If visited before then use old state.
1370 if (ValiditySlot) return ValiditySlot == Valid;
1372 // Assume validity for the time being (recursion.)
1373 ValiditySlot = Valid;
1375 // Make sure the global is internal or link once (anchor.)
1376 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1377 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1378 ValiditySlot = Invalid;
1379 return false;
1382 // Get the Tag
1383 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1385 // Check for user defined descriptors.
1386 if (Tag == DW_TAG_invalid) return true;
1388 // Construct an empty DebugInfoDesc.
1389 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1391 // Allow for user defined descriptors.
1392 if (!DD) return true;
1394 // Get the initializer constant.
1395 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1397 // Get the operand count.
1398 unsigned N = CI->getNumOperands();
1400 // Get the field count.
1401 unsigned &CountSlot = Counts[Tag];
1402 if (!CountSlot) {
1403 // Check the operand count to the field count
1404 DICountVisitor CTAM;
1405 CTAM.ApplyToFields(DD);
1406 CountSlot = CTAM.getCount();
1409 // Field count must be at most equal operand count.
1410 if (CountSlot > N) {
1411 delete DD;
1412 ValiditySlot = Invalid;
1413 return false;
1416 // Check each field for valid type.
1417 DIVerifyVisitor VRAM(*this, GV);
1418 VRAM.ApplyToFields(DD);
1420 // Release empty DebugInfoDesc.
1421 delete DD;
1423 // If fields are not valid.
1424 if (!VRAM.isValid()) {
1425 ValiditySlot = Invalid;
1426 return false;
1429 return true;
1432 //===----------------------------------------------------------------------===//
1434 DebugScope::~DebugScope() {
1435 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1436 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1439 //===----------------------------------------------------------------------===//
1441 MachineDebugInfo::MachineDebugInfo()
1442 : DR()
1443 , VR()
1444 , CompileUnits()
1445 , Directories()
1446 , SourceFiles()
1447 , Lines()
1448 , LabelID(0)
1449 , ScopeMap()
1450 , RootScope(NULL)
1451 , FrameMoves()
1453 MachineDebugInfo::~MachineDebugInfo() {
1457 /// doInitialization - Initialize the debug state for a new module.
1459 bool MachineDebugInfo::doInitialization() {
1460 return false;
1463 /// doFinalization - Tear down the debug state after completion of a module.
1465 bool MachineDebugInfo::doFinalization() {
1466 return false;
1469 /// BeginFunction - Begin gathering function debug information.
1471 void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1472 // Coming soon.
1475 /// MachineDebugInfo::EndFunction - Discard function debug information.
1477 void MachineDebugInfo::EndFunction() {
1478 // Clean up scope information.
1479 if (RootScope) {
1480 delete RootScope;
1481 ScopeMap.clear();
1482 RootScope = NULL;
1485 // Clean up frame info.
1486 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1487 FrameMoves.clear();
1490 /// getDescFor - Convert a Value to a debug information descriptor.
1492 // FIXME - use new Value type when available.
1493 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
1494 return DR.Deserialize(V);
1497 /// Verify - Verify that a Value is debug information descriptor.
1499 bool MachineDebugInfo::Verify(Value *V) {
1500 return VR.Verify(V);
1503 /// AnalyzeModule - Scan the module for global debug information.
1505 void MachineDebugInfo::AnalyzeModule(Module &M) {
1506 SetupCompileUnits(M);
1509 /// SetupCompileUnits - Set up the unique vector of compile units.
1511 void MachineDebugInfo::SetupCompileUnits(Module &M) {
1512 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1514 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1515 CompileUnits.insert(CU[i]);
1519 /// getCompileUnits - Return a vector of debug compile units.
1521 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
1522 return CompileUnits;
1525 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1526 /// named GlobalVariable.
1527 std::vector<GlobalVariable*>
1528 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1529 const std::string &RootName) {
1530 return ::getGlobalVariablesUsing(M, RootName);
1533 /// RecordLabel - Records location information and associates it with a
1534 /// debug label. Returns a unique label ID used to generate a label and
1535 /// provide correspondence to the source line list.
1536 unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1537 unsigned Source) {
1538 unsigned ID = NextLabelID();
1539 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1540 return ID;
1543 /// RecordSource - Register a source file with debug info. Returns an source
1544 /// ID.
1545 unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1546 const std::string &Source) {
1547 unsigned DirectoryID = Directories.insert(Directory);
1548 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1550 unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1551 return RecordSource(CompileUnit->getDirectory(),
1552 CompileUnit->getFileName());
1555 /// RecordRegionStart - Indicate the start of a region.
1557 unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1558 // FIXME - need to be able to handle split scopes because of bb cloning.
1559 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1560 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1561 unsigned ID = NextLabelID();
1562 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1563 return ID;
1566 /// RecordRegionEnd - Indicate the end of a region.
1568 unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1569 // FIXME - need to be able to handle split scopes because of bb cloning.
1570 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1571 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1572 unsigned ID = NextLabelID();
1573 Scope->setEndLabelID(ID);
1574 return ID;
1577 /// RecordVariable - Indicate the declaration of a local variable.
1579 void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1580 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1581 DebugScope *Scope = getOrCreateScope(VD->getContext());
1582 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1583 Scope->AddVariable(DV);
1586 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1588 DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1589 DebugScope *&Slot = ScopeMap[ScopeDesc];
1590 if (!Slot) {
1591 // FIXME - breaks down when the context is an inlined function.
1592 DebugInfoDesc *ParentDesc = NULL;
1593 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1594 ParentDesc = Block->getContext();
1596 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1597 Slot = new DebugScope(Parent, ScopeDesc);
1598 if (Parent) {
1599 Parent->AddScope(Slot);
1600 } else if (RootScope) {
1601 // FIXME - Add inlined function scopes to the root so we can delete
1602 // them later. Long term, handle inlined functions properly.
1603 RootScope->AddScope(Slot);
1604 } else {
1605 // First function is top level function.
1606 RootScope = Slot;
1609 return Slot;