blob: 7db9b2f3bc411a249d3dc4c69b9995b5c540acc2 [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Analysis/ValueTracking.h"
Devang Patelbf3f5a02009-01-30 01:03:10 +000023#include "llvm/Support/Streams.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// DIDescriptor
28//===----------------------------------------------------------------------===//
29
30DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) {
31 GV = gv;
32
33 // If this is non-null, check to see if the Tag matches. If not, set to null.
34 if (GV && getTag() != RequiredTag)
35 GV = 0;
36}
37
Bill Wendling0582ae92009-03-13 04:39:26 +000038const std::string &
39DIDescriptor::getStringField(unsigned Elt, std::string &Result) const {
40 if (GV == 0) {
41 Result.clear();
42 return Result;
43 }
Chris Lattnera45664f2008-11-10 02:56:27 +000044
Chris Lattnera45664f2008-11-10 02:56:27 +000045 Constant *C = GV->getInitializer();
Bill Wendling0582ae92009-03-13 04:39:26 +000046 if (C == 0 || Elt >= C->getNumOperands()) {
47 Result.clear();
48 return Result;
49 }
Chris Lattnera45664f2008-11-10 02:56:27 +000050
Chris Lattnera45664f2008-11-10 02:56:27 +000051 // Fills in the string if it succeeds
Bill Wendling0582ae92009-03-13 04:39:26 +000052 if (!GetConstantStringInfo(C->getOperand(Elt), Result))
53 Result.clear();
54
55 return Result;
Chris Lattnera45664f2008-11-10 02:56:27 +000056}
57
58uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
59 if (GV == 0) return 0;
60 Constant *C = GV->getInitializer();
61 if (C == 0 || Elt >= C->getNumOperands())
62 return 0;
63 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt)))
64 return CI->getZExtValue();
65 return 0;
66}
67
Chris Lattnera45664f2008-11-10 02:56:27 +000068DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
69 if (GV == 0) return DIDescriptor();
70 Constant *C = GV->getInitializer();
71 if (C == 0 || Elt >= C->getNumOperands())
72 return DIDescriptor();
73 C = C->getOperand(Elt);
74 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts()));
75}
76
77GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
78 if (GV == 0) return 0;
79 Constant *C = GV->getInitializer();
80 if (C == 0 || Elt >= C->getNumOperands())
81 return 0;
82 C = C->getOperand(Elt);
83
84 return dyn_cast<GlobalVariable>(C->stripPointerCasts());
85}
86
87
88
Chris Lattnera45664f2008-11-10 02:56:27 +000089//===----------------------------------------------------------------------===//
90// Simple Descriptor Constructors and other Methods
91//===----------------------------------------------------------------------===//
92
93DIAnchor::DIAnchor(GlobalVariable *GV)
94 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
95DIEnumerator::DIEnumerator(GlobalVariable *GV)
96 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
97DISubrange::DISubrange(GlobalVariable *GV)
98 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
99DICompileUnit::DICompileUnit(GlobalVariable *GV)
100 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
101DIBasicType::DIBasicType(GlobalVariable *GV)
102 : DIType(GV, dwarf::DW_TAG_base_type) {}
103DISubprogram::DISubprogram(GlobalVariable *GV)
104 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
105DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV)
106 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
107DIBlock::DIBlock(GlobalVariable *GV)
108 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
Torok Edwinb07fbd92008-12-13 08:25:29 +0000109// needed by DIVariable::getType()
Torok Edwina70c68e2008-12-16 09:06:01 +0000110DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) {
111 if (!gv) return;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000112 unsigned tag = getTag();
113 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) &&
114 !DICompositeType::isCompositeType(tag))
115 GV = 0;
116}
Chris Lattnera45664f2008-11-10 02:56:27 +0000117
118/// isDerivedType - Return true if the specified tag is legal for
119/// DIDerivedType.
Devang Patel486938f2009-01-12 21:38:43 +0000120bool DIType::isDerivedType(unsigned Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000121 switch (Tag) {
122 case dwarf::DW_TAG_typedef:
123 case dwarf::DW_TAG_pointer_type:
124 case dwarf::DW_TAG_reference_type:
125 case dwarf::DW_TAG_const_type:
126 case dwarf::DW_TAG_volatile_type:
127 case dwarf::DW_TAG_restrict_type:
128 case dwarf::DW_TAG_member:
129 case dwarf::DW_TAG_inheritance:
130 return true;
131 default:
132 // FIXME: Even though it doesn't make sense, CompositeTypes are current
133 // modelled as DerivedTypes, this should return true for them as well.
134 return false;
135 }
136}
137
138DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) {
139 if (GV && !isDerivedType(getTag()))
140 GV = 0;
141}
142
143/// isCompositeType - Return true if the specified tag is legal for
144/// DICompositeType.
Devang Patel486938f2009-01-12 21:38:43 +0000145bool DIType::isCompositeType(unsigned TAG) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000146 switch (TAG) {
147 case dwarf::DW_TAG_array_type:
148 case dwarf::DW_TAG_structure_type:
149 case dwarf::DW_TAG_union_type:
150 case dwarf::DW_TAG_enumeration_type:
151 case dwarf::DW_TAG_vector_type:
152 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000153 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000154 return true;
155 default:
156 return false;
157 }
158}
159
160DICompositeType::DICompositeType(GlobalVariable *GV)
161 : DIDerivedType(GV, true, true) {
162 if (GV && !isCompositeType(getTag()))
163 GV = 0;
164}
165
166/// isVariable - Return true if the specified tag is legal for DIVariable.
167bool DIVariable::isVariable(unsigned Tag) {
168 switch (Tag) {
169 case dwarf::DW_TAG_auto_variable:
170 case dwarf::DW_TAG_arg_variable:
171 case dwarf::DW_TAG_return_variable:
172 return true;
173 default:
174 return false;
175 }
176}
177
Devang Patel36375ee2009-02-17 21:23:59 +0000178DIVariable::DIVariable(GlobalVariable *gv) : DIDescriptor(gv) {
179 if (gv && !isVariable(getTag()))
Chris Lattnera45664f2008-11-10 02:56:27 +0000180 GV = 0;
181}
182
Devang Patel68afdc32009-01-05 18:33:01 +0000183unsigned DIArray::getNumElements() const {
184 assert (GV && "Invalid DIArray");
185 Constant *C = GV->getInitializer();
186 assert (C && "Invalid DIArray initializer");
187 return C->getNumOperands();
188}
Chris Lattnera45664f2008-11-10 02:56:27 +0000189
Devang Patelb79b5352009-01-19 23:21:49 +0000190/// Verify - Verify that a compile unit is well formed.
191bool DICompileUnit::Verify() const {
192 if (isNull())
193 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000194 std::string Res;
195 if (getFilename(Res).empty())
196 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000197 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000198 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000199}
200
201/// Verify - Verify that a type descriptor is well formed.
202bool DIType::Verify() const {
203 if (isNull())
204 return false;
205 if (getContext().isNull())
206 return false;
207
208 DICompileUnit CU = getCompileUnit();
209 if (!CU.isNull() && !CU.Verify())
210 return false;
211 return true;
212}
213
214/// Verify - Verify that a composite type descriptor is well formed.
215bool DICompositeType::Verify() const {
216 if (isNull())
217 return false;
218 if (getContext().isNull())
219 return false;
220
221 DICompileUnit CU = getCompileUnit();
222 if (!CU.isNull() && !CU.Verify())
223 return false;
224 return true;
225}
226
227/// Verify - Verify that a subprogram descriptor is well formed.
228bool DISubprogram::Verify() const {
229 if (isNull())
230 return false;
231
232 if (getContext().isNull())
233 return false;
234
235 DICompileUnit CU = getCompileUnit();
236 if (!CU.Verify())
237 return false;
238
239 DICompositeType Ty = getType();
240 if (!Ty.isNull() && !Ty.Verify())
241 return false;
242 return true;
243}
244
245/// Verify - Verify that a global variable descriptor is well formed.
246bool DIGlobalVariable::Verify() const {
247 if (isNull())
248 return false;
249
250 if (getContext().isNull())
251 return false;
252
253 DICompileUnit CU = getCompileUnit();
254 if (!CU.Verify())
255 return false;
256
257 DIType Ty = getType();
258 if (!Ty.Verify())
259 return false;
260
261 if (!getGlobal())
262 return false;
263
264 return true;
265}
266
267/// Verify - Verify that a variable descriptor is well formed.
268bool DIVariable::Verify() const {
269 if (isNull())
270 return false;
271
272 if (getContext().isNull())
273 return false;
274
275 DIType Ty = getType();
276 if (!Ty.Verify())
277 return false;
278
279
280 return true;
281}
282
Devang Patel36375ee2009-02-17 21:23:59 +0000283/// getOriginalTypeSize - If this type is derived from a base type then
284/// return base type size.
285uint64_t DIDerivedType::getOriginalTypeSize() const {
286 if (getTag() != dwarf::DW_TAG_member)
287 return getSizeInBits();
288 DIType BT = getTypeDerivedFrom();
289 if (BT.getTag() != dwarf::DW_TAG_base_type)
290 return getSizeInBits();
291 return BT.getSizeInBits();
292}
Devang Patelb79b5352009-01-19 23:21:49 +0000293
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000294/// describes - Return true if this subprogram provides debugging
295/// information for the function F.
296bool DISubprogram::describes(const Function *F) {
297 assert (F && "Invalid function");
298 std::string Name;
299 getLinkageName(Name);
300 if (Name.empty())
301 getName(Name);
302 if (!Name.empty() && (strcmp(Name.c_str(), F->getNameStart()) == false))
303 return true;
304 return false;
305}
306
Chris Lattnera45664f2008-11-10 02:56:27 +0000307//===----------------------------------------------------------------------===//
308// DIFactory: Basic Helpers
309//===----------------------------------------------------------------------===//
310
Chris Lattner497a7a82008-11-10 04:10:34 +0000311DIFactory::DIFactory(Module &m) : M(m) {
312 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0;
313 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL));
314}
315
316/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
317/// This is only valid when the descriptor is non-null.
318Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
319 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
320 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
321}
322
Chris Lattnera45664f2008-11-10 02:56:27 +0000323Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000324 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000325 "Tag too large for debug encoding!");
Devang Patel6906ba52009-01-20 19:22:03 +0000326 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000327}
328
329Constant *DIFactory::GetStringConstant(const std::string &String) {
330 // Check string cache for previous edition.
331 Constant *&Slot = StringCache[String];
332
333 // Return Constant if previously defined.
334 if (Slot) return Slot;
335
336 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty);
337
338 // If empty string then use a sbyte* null instead.
339 if (String.empty())
340 return Slot = ConstantPointerNull::get(DestTy);
341
342 // Construct string as an llvm constant.
343 Constant *ConstStr = ConstantArray::get(String);
344
345 // Otherwise create and return a new string global.
346 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
347 GlobalVariable::InternalLinkage,
348 ConstStr, ".str", &M);
349 StrGV->setSection("llvm.metadata");
350 return Slot = ConstantExpr::getBitCast(StrGV, DestTy);
351}
352
353/// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it
354/// already exists, return it. If not, create a new one and return it.
355DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) {
Chris Lattner497a7a82008-11-10 04:10:34 +0000356 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
Chris Lattnera45664f2008-11-10 02:56:27 +0000357
358 // Otherwise, create the global or return it if already in the module.
359 Constant *C = M.getOrInsertGlobal(Name, EltTy);
360 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?");
361 GlobalVariable *GV = cast<GlobalVariable>(C);
362
363 // If it has an initializer, it is already in the module.
364 if (GV->hasInitializer())
365 return SubProgramAnchor = DIAnchor(GV);
366
Duncan Sands667d4b82009-03-07 15:45:40 +0000367 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
Chris Lattnera45664f2008-11-10 02:56:27 +0000368 GV->setSection("llvm.metadata");
369 GV->setConstant(true);
370 M.addTypeName("llvm.dbg.anchor.type", EltTy);
371
372 // Otherwise, set the initializer.
373 Constant *Elts[] = {
374 GetTagConstant(dwarf::DW_TAG_anchor),
375 ConstantInt::get(Type::Int32Ty, TAG)
376 };
377
378 GV->setInitializer(ConstantStruct::get(Elts, 2));
379 return DIAnchor(GV);
380}
381
382
383
384//===----------------------------------------------------------------------===//
385// DIFactory: Primary Constructors
386//===----------------------------------------------------------------------===//
387
388/// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
389/// creating a new one if there isn't already one in the module.
390DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() {
391 // If we already created one, just return it.
392 if (!CompileUnitAnchor.isNull())
393 return CompileUnitAnchor;
394 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit,
395 "llvm.dbg.compile_units");
396}
397
398/// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
399/// creating a new one if there isn't already one in the module.
400DIAnchor DIFactory::GetOrCreateSubprogramAnchor() {
401 // If we already created one, just return it.
402 if (!SubProgramAnchor.isNull())
403 return SubProgramAnchor;
404 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram,
405 "llvm.dbg.subprograms");
406}
407
408/// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
409/// creating a new one if there isn't already one in the module.
410DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() {
411 // If we already created one, just return it.
412 if (!GlobalVariableAnchor.isNull())
413 return GlobalVariableAnchor;
414 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable,
415 "llvm.dbg.global_variables");
416}
417
418/// GetOrCreateArray - Create an descriptor for an array of descriptors.
419/// This implicitly uniques the arrays created.
420DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
421 SmallVector<Constant*, 16> Elts;
422
423 for (unsigned i = 0; i != NumTys; ++i)
Chris Lattner497a7a82008-11-10 04:10:34 +0000424 Elts.push_back(getCastToEmpty(Tys[i]));
Chris Lattnera45664f2008-11-10 02:56:27 +0000425
Chris Lattner497a7a82008-11-10 04:10:34 +0000426 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr,
427 Elts.size()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000428 &Elts[0], Elts.size());
429 // If we already have this array, just return the uniqued version.
430 DIDescriptor &Entry = SimpleConstantCache[Init];
431 if (!Entry.isNull()) return DIArray(Entry.getGV());
432
433 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
434 GlobalValue::InternalLinkage,
435 Init, "llvm.dbg.array", &M);
436 GV->setSection("llvm.metadata");
437 Entry = DIDescriptor(GV);
438 return DIArray(GV);
439}
440
441/// GetOrCreateSubrange - Create a descriptor for a value range. This
442/// implicitly uniques the values returned.
443DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
444 Constant *Elts[] = {
445 GetTagConstant(dwarf::DW_TAG_subrange_type),
446 ConstantInt::get(Type::Int64Ty, Lo),
447 ConstantInt::get(Type::Int64Ty, Hi)
448 };
449
450 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
451
452 // If we already have this range, just return the uniqued version.
453 DIDescriptor &Entry = SimpleConstantCache[Init];
454 if (!Entry.isNull()) return DISubrange(Entry.getGV());
455
456 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
457
458 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
459 GlobalValue::InternalLinkage,
460 Init, "llvm.dbg.subrange", &M);
461 GV->setSection("llvm.metadata");
462 Entry = DIDescriptor(GV);
463 return DISubrange(GV);
464}
465
466
467
468/// CreateCompileUnit - Create a new descriptor for the specified compile
469/// unit. Note that this does not unique compile units within the module.
470DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
471 const std::string &Filename,
472 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000473 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000474 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000475 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000476 const char *Flags,
477 unsigned RunTimeVer) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000478 Constant *Elts[] = {
479 GetTagConstant(dwarf::DW_TAG_compile_unit),
Chris Lattner497a7a82008-11-10 04:10:34 +0000480 getCastToEmpty(GetOrCreateCompileUnitAnchor()),
Chris Lattnera45664f2008-11-10 02:56:27 +0000481 ConstantInt::get(Type::Int32Ty, LangID),
482 GetStringConstant(Filename),
483 GetStringConstant(Directory),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000484 GetStringConstant(Producer),
Devang Pateldd9db662009-01-30 18:20:31 +0000485 ConstantInt::get(Type::Int1Ty, isMain),
Devang Patel3b64c6b2009-01-23 22:33:47 +0000486 ConstantInt::get(Type::Int1Ty, isOptimized),
Devang Patel13319ce2009-02-17 22:43:44 +0000487 GetStringConstant(Flags),
488 ConstantInt::get(Type::Int32Ty, RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000489 };
490
491 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
492
493 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
494 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
495 GlobalValue::InternalLinkage,
496 Init, "llvm.dbg.compile_unit", &M);
497 GV->setSection("llvm.metadata");
498 return DICompileUnit(GV);
499}
500
501/// CreateEnumerator - Create a single enumerator value.
502DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
503 Constant *Elts[] = {
504 GetTagConstant(dwarf::DW_TAG_enumerator),
505 GetStringConstant(Name),
506 ConstantInt::get(Type::Int64Ty, Val)
507 };
508
509 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
510
511 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
512 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
513 GlobalValue::InternalLinkage,
514 Init, "llvm.dbg.enumerator", &M);
515 GV->setSection("llvm.metadata");
516 return DIEnumerator(GV);
517}
518
519
520/// CreateBasicType - Create a basic type like int, float, etc.
521DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000522 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000523 DICompileUnit CompileUnit,
524 unsigned LineNumber,
525 uint64_t SizeInBits,
526 uint64_t AlignInBits,
527 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000528 unsigned Encoding) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000529 Constant *Elts[] = {
530 GetTagConstant(dwarf::DW_TAG_base_type),
Chris Lattner497a7a82008-11-10 04:10:34 +0000531 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000532 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000533 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000534 ConstantInt::get(Type::Int32Ty, LineNumber),
535 ConstantInt::get(Type::Int64Ty, SizeInBits),
536 ConstantInt::get(Type::Int64Ty, AlignInBits),
537 ConstantInt::get(Type::Int64Ty, OffsetInBits),
538 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000539 ConstantInt::get(Type::Int32Ty, Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000540 };
541
542 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
543
544 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
545 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
546 GlobalValue::InternalLinkage,
547 Init, "llvm.dbg.basictype", &M);
548 GV->setSection("llvm.metadata");
549 return DIBasicType(GV);
550}
551
552/// CreateDerivedType - Create a derived type like const qualified type,
553/// pointer, typedef, etc.
554DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
555 DIDescriptor Context,
556 const std::string &Name,
557 DICompileUnit CompileUnit,
558 unsigned LineNumber,
559 uint64_t SizeInBits,
560 uint64_t AlignInBits,
561 uint64_t OffsetInBits,
562 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000563 DIType DerivedFrom) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000564 Constant *Elts[] = {
565 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000566 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000567 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000568 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000569 ConstantInt::get(Type::Int32Ty, LineNumber),
570 ConstantInt::get(Type::Int64Ty, SizeInBits),
571 ConstantInt::get(Type::Int64Ty, AlignInBits),
572 ConstantInt::get(Type::Int64Ty, OffsetInBits),
573 ConstantInt::get(Type::Int32Ty, Flags),
Devang Pateldd9db662009-01-30 18:20:31 +0000574 getCastToEmpty(DerivedFrom)
Chris Lattnera45664f2008-11-10 02:56:27 +0000575 };
576
577 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
578
579 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
580 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
581 GlobalValue::InternalLinkage,
582 Init, "llvm.dbg.derivedtype", &M);
583 GV->setSection("llvm.metadata");
584 return DIDerivedType(GV);
585}
586
587/// CreateCompositeType - Create a composite type like array, struct, etc.
588DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
589 DIDescriptor Context,
590 const std::string &Name,
591 DICompileUnit CompileUnit,
592 unsigned LineNumber,
593 uint64_t SizeInBits,
594 uint64_t AlignInBits,
595 uint64_t OffsetInBits,
596 unsigned Flags,
597 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000598 DIArray Elements,
599 unsigned RuntimeLang) {
Devang Patel854967e2008-12-17 22:39:29 +0000600
Chris Lattnera45664f2008-11-10 02:56:27 +0000601 Constant *Elts[] = {
602 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000603 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000604 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000605 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000606 ConstantInt::get(Type::Int32Ty, LineNumber),
607 ConstantInt::get(Type::Int64Ty, SizeInBits),
608 ConstantInt::get(Type::Int64Ty, AlignInBits),
609 ConstantInt::get(Type::Int64Ty, OffsetInBits),
610 ConstantInt::get(Type::Int32Ty, Flags),
Chris Lattner497a7a82008-11-10 04:10:34 +0000611 getCastToEmpty(DerivedFrom),
Devang Patel13319ce2009-02-17 22:43:44 +0000612 getCastToEmpty(Elements),
613 ConstantInt::get(Type::Int32Ty, RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000614 };
615
616 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
617
618 M.addTypeName("llvm.dbg.composite.type", Init->getType());
619 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
620 GlobalValue::InternalLinkage,
621 Init, "llvm.dbg.composite", &M);
622 GV->setSection("llvm.metadata");
623 return DICompositeType(GV);
624}
625
626
627/// CreateSubprogram - Create a new descriptor for the specified subprogram.
628/// See comments in DISubprogram for descriptions of these fields. This
629/// method does not unique the generated descriptors.
630DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
631 const std::string &Name,
632 const std::string &DisplayName,
633 const std::string &LinkageName,
634 DICompileUnit CompileUnit,
635 unsigned LineNo, DIType Type,
636 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000637 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000638
Chris Lattnera45664f2008-11-10 02:56:27 +0000639 Constant *Elts[] = {
640 GetTagConstant(dwarf::DW_TAG_subprogram),
Chris Lattner497a7a82008-11-10 04:10:34 +0000641 getCastToEmpty(GetOrCreateSubprogramAnchor()),
642 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000643 GetStringConstant(Name),
644 GetStringConstant(DisplayName),
645 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000646 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000647 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000648 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000649 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
Devang Pateldd9db662009-01-30 18:20:31 +0000650 ConstantInt::get(Type::Int1Ty, isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000651 };
652
653 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
654
655 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
656 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
657 GlobalValue::InternalLinkage,
658 Init, "llvm.dbg.subprogram", &M);
659 GV->setSection("llvm.metadata");
660 return DISubprogram(GV);
661}
662
663/// CreateGlobalVariable - Create a new descriptor for the specified global.
664DIGlobalVariable
665DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
666 const std::string &DisplayName,
667 const std::string &LinkageName,
668 DICompileUnit CompileUnit,
669 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000670 bool isDefinition, llvm::GlobalVariable *Val) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000671 Constant *Elts[] = {
672 GetTagConstant(dwarf::DW_TAG_variable),
Chris Lattner497a7a82008-11-10 04:10:34 +0000673 getCastToEmpty(GetOrCreateGlobalVariableAnchor()),
674 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000675 GetStringConstant(Name),
676 GetStringConstant(DisplayName),
677 GetStringConstant(LinkageName),
Chris Lattner497a7a82008-11-10 04:10:34 +0000678 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000679 ConstantInt::get(Type::Int32Ty, LineNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000680 getCastToEmpty(Type),
Chris Lattnera45664f2008-11-10 02:56:27 +0000681 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
682 ConstantInt::get(Type::Int1Ty, isDefinition),
Devang Pateldd9db662009-01-30 18:20:31 +0000683 ConstantExpr::getBitCast(Val, EmptyStructPtr)
Chris Lattnera45664f2008-11-10 02:56:27 +0000684 };
685
686 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
687
688 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
689 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
690 GlobalValue::InternalLinkage,
691 Init, "llvm.dbg.global_variable", &M);
692 GV->setSection("llvm.metadata");
693 return DIGlobalVariable(GV);
694}
695
696
697/// CreateVariable - Create a new descriptor for the specified variable.
698DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
699 const std::string &Name,
700 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000701 DIType Type) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000702 Constant *Elts[] = {
703 GetTagConstant(Tag),
Chris Lattner497a7a82008-11-10 04:10:34 +0000704 getCastToEmpty(Context),
Chris Lattnera45664f2008-11-10 02:56:27 +0000705 GetStringConstant(Name),
Chris Lattner497a7a82008-11-10 04:10:34 +0000706 getCastToEmpty(CompileUnit),
Chris Lattnera45664f2008-11-10 02:56:27 +0000707 ConstantInt::get(Type::Int32Ty, LineNo),
Devang Pateldd9db662009-01-30 18:20:31 +0000708 getCastToEmpty(Type)
Chris Lattnera45664f2008-11-10 02:56:27 +0000709 };
710
711 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
712
713 M.addTypeName("llvm.dbg.variable.type", Init->getType());
714 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
715 GlobalValue::InternalLinkage,
716 Init, "llvm.dbg.variable", &M);
717 GV->setSection("llvm.metadata");
718 return DIVariable(GV);
719}
720
721
722/// CreateBlock - This creates a descriptor for a lexical block with the
723/// specified parent context.
724DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
725 Constant *Elts[] = {
726 GetTagConstant(dwarf::DW_TAG_lexical_block),
Chris Lattner497a7a82008-11-10 04:10:34 +0000727 getCastToEmpty(Context)
Chris Lattnera45664f2008-11-10 02:56:27 +0000728 };
729
730 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
731
732 M.addTypeName("llvm.dbg.block.type", Init->getType());
733 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
734 GlobalValue::InternalLinkage,
735 Init, "llvm.dbg.block", &M);
736 GV->setSection("llvm.metadata");
737 return DIBlock(GV);
738}
739
740
741//===----------------------------------------------------------------------===//
742// DIFactory: Routines for inserting code into a function
743//===----------------------------------------------------------------------===//
744
745/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
746/// inserting it at the end of the specified basic block.
747void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
748 unsigned ColNo, BasicBlock *BB) {
749
750 // Lazily construct llvm.dbg.stoppoint function.
751 if (!StopPointFn)
752 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
753 llvm::Intrinsic::dbg_stoppoint);
754
755 // Invoke llvm.dbg.stoppoint
756 Value *Args[] = {
757 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo),
758 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo),
Chris Lattner497a7a82008-11-10 04:10:34 +0000759 getCastToEmpty(CU)
Chris Lattnera45664f2008-11-10 02:56:27 +0000760 };
761 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
762}
763
764/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
765/// mark the start of the specified subprogram.
766void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
767 // Lazily construct llvm.dbg.func.start.
768 if (!FuncStartFn)
769 FuncStartFn = llvm::Intrinsic::getDeclaration(&M,
770 llvm::Intrinsic::dbg_func_start);
771
772 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Chris Lattner497a7a82008-11-10 04:10:34 +0000773 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000774}
775
776/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
777/// mark the start of a region for the specified scoping descriptor.
778void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
779 // Lazily construct llvm.dbg.region.start function.
780 if (!RegionStartFn)
781 RegionStartFn = llvm::Intrinsic::getDeclaration(&M,
782 llvm::Intrinsic::dbg_region_start);
783 // Call llvm.dbg.func.start.
Chris Lattner497a7a82008-11-10 04:10:34 +0000784 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000785}
786
787
788/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
789/// mark the end of a region for the specified scoping descriptor.
790void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
791 // Lazily construct llvm.dbg.region.end function.
792 if (!RegionEndFn)
793 RegionEndFn = llvm::Intrinsic::getDeclaration(&M,
794 llvm::Intrinsic::dbg_region_end);
795
Chris Lattner497a7a82008-11-10 04:10:34 +0000796 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000797}
798
799/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
800void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D,
801 BasicBlock *BB) {
802 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Chris Lattner497a7a82008-11-10 04:10:34 +0000803 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000804
805 if (!DeclareFn)
806 DeclareFn = llvm::Intrinsic::getDeclaration(&M,
807 llvm::Intrinsic::dbg_declare);
Chris Lattner497a7a82008-11-10 04:10:34 +0000808 Value *Args[] = { Storage, getCastToEmpty(D) };
Chris Lattnera45664f2008-11-10 02:56:27 +0000809 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
810}
Torok Edwin620f2802008-12-16 09:07:36 +0000811
812namespace llvm {
813 /// Finds the stoppoint coressponding to this instruction, that is the
814 /// stoppoint that dominates this instruction
815 const DbgStopPointInst *findStopPoint(const Instruction *Inst)
816 {
817 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
818 return DSI;
819
820 const BasicBlock *BB = Inst->getParent();
821 BasicBlock::const_iterator I = Inst, B;
822 do {
823 B = BB->begin();
824 // A BB consisting only of a terminator can't have a stoppoint.
825 if (I != B) {
826 do {
827 --I;
828 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
829 return DSI;
830 } while (I != B);
831 }
832 // This BB didn't have a stoppoint: if there is only one
833 // predecessor, look for a stoppoint there.
834 // We could use getIDom(), but that would require dominator info.
835 BB = I->getParent()->getUniquePredecessor();
836 if (BB)
837 I = BB->getTerminator();
838 } while (BB != 0);
839 return 0;
840 }
841
842 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
843 /// instruction in this Basic Block, and returns the stoppoint for it.
844 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
845 {
846 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
847 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
848 return DSI;
849 }
850 // Fallback to looking for stoppoint of unique predecessor.
851 // Useful if this BB contains no stoppoints, but unique predecessor does.
852 BB = BB->getUniquePredecessor();
853 if (BB)
854 return findStopPoint(BB->getTerminator());
855 return 0;
856 }
857
Torok Edwinff7d0e92009-03-10 13:41:26 +0000858 Value *findDbgGlobalDeclare(GlobalVariable *V)
859 {
860 const Module *M = V->getParent();
861 const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type");
862 if (!Ty)
863 return 0;
864 Ty = PointerType::get(Ty, 0);
865
866 Value *Val = V->stripPointerCasts();
867 for (Value::use_iterator I = Val->use_begin(), E =Val->use_end();
868 I != E; ++I) {
869 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) {
870 if (CE->getOpcode() == Instruction::BitCast) {
871 Value *VV = CE;
872 while (VV->hasOneUse()) {
873 VV = *VV->use_begin();
874 }
875 if (VV->getType() == Ty)
876 return VV;
877 }
878 }
879 }
880
881 if (Val->getType() == Ty)
882 return Val;
883 return 0;
884 }
885
Torok Edwin620f2802008-12-16 09:07:36 +0000886 /// Finds the dbg.declare intrinsic corresponding to this value if any.
887 /// It looks through pointer casts too.
888 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
889 {
890 if (stripCasts) {
891 V = V->stripPointerCasts();
892 // Look for the bitcast.
893 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
894 I != E; ++I) {
895 if (isa<BitCastInst>(I))
896 return findDbgDeclare(*I, false);
897 }
898 return 0;
899 }
900
901 // Find dbg.declare among uses of the instruction.
902 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
903 I != E; ++I) {
904 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
905 return DDI;
906 }
907 return 0;
908 }
Torok Edwinff7d0e92009-03-10 13:41:26 +0000909
910 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
Bill Wendling0582ae92009-03-13 04:39:26 +0000911 unsigned &LineNo, std::string &File, std::string &Dir)
912 {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000913 DICompileUnit Unit;
914 DIType TypeD;
915 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
916 Value *DIGV = findDbgGlobalDeclare(GV);
917 if (!DIGV)
918 return false;
919 DIGlobalVariable Var(cast<GlobalVariable>(DIGV));
Bill Wendling0582ae92009-03-13 04:39:26 +0000920 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000921 LineNo = Var.getLineNumber();
922 Unit = Var.getCompileUnit();
923 TypeD = Var.getType();
924 } else {
925 const DbgDeclareInst *DDI = findDbgDeclare(V);
926 if (!DDI)
927 return false;
928 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendling0582ae92009-03-13 04:39:26 +0000929 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000930 LineNo = Var.getLineNumber();
931 Unit = Var.getCompileUnit();
932 TypeD = Var.getType();
933 }
Bill Wendling0582ae92009-03-13 04:39:26 +0000934 TypeD.getName(Type);
935 Unit.getFilename(File);
936 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000937 return true;
938 }
Torok Edwin620f2802008-12-16 09:07:36 +0000939}
940
Devang Patelbf3f5a02009-01-30 01:03:10 +0000941/// dump - print compile unit.
942void DICompileUnit::dump() const {
Devang Pateld8e880c2009-02-24 23:15:09 +0000943 if (getLanguage())
944 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000945
Bill Wendling0582ae92009-03-13 04:39:26 +0000946 std::string Res1, Res2;
947 cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patelbf3f5a02009-01-30 01:03:10 +0000948}
949
950/// dump - print type.
951void DIType::dump() const {
952 if (isNull()) return;
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000953
Bill Wendling0582ae92009-03-13 04:39:26 +0000954 std::string Res;
955 if (!getName(Res).empty())
956 cerr << " [" << Res << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000957
Devang Patelbf3f5a02009-01-30 01:03:10 +0000958 unsigned Tag = getTag();
959 cerr << " [" << dwarf::TagString(Tag) << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000960
Devang Patelbf3f5a02009-01-30 01:03:10 +0000961 // TODO : Print context
962 getCompileUnit().dump();
963 cerr << " ["
964 << getLineNumber() << ", "
965 << getSizeInBits() << ", "
966 << getAlignInBits() << ", "
967 << getOffsetInBits()
968 << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000969
Devang Patelbf3f5a02009-01-30 01:03:10 +0000970 if (isPrivate())
971 cerr << " [private] ";
972 else if (isProtected())
973 cerr << " [protected] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000974
Devang Patelbf3f5a02009-01-30 01:03:10 +0000975 if (isForwardDecl())
976 cerr << " [fwd] ";
977
978 if (isBasicType(Tag))
979 DIBasicType(GV).dump();
980 else if (isDerivedType(Tag))
981 DIDerivedType(GV).dump();
982 else if (isCompositeType(Tag))
983 DICompositeType(GV).dump();
984 else {
985 cerr << "Invalid DIType\n";
986 return;
987 }
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000988
Devang Patelbf3f5a02009-01-30 01:03:10 +0000989 cerr << "\n";
990}
991
992/// dump - print basic type.
993void DIBasicType::dump() const {
994 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
995
996}
997
998/// dump - print derived type.
999void DIDerivedType::dump() const {
1000 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
1001}
1002
1003/// dump - print composite type.
1004void DICompositeType::dump() const {
1005 DIArray A = getTypeArray();
1006 if (A.isNull())
1007 return;
1008 cerr << " [" << A.getNumElements() << " elements]";
1009}
1010
1011/// dump - print global.
1012void DIGlobal::dump() const {
Bill Wendling0582ae92009-03-13 04:39:26 +00001013 std::string Res;
1014 if (!getName(Res).empty())
1015 cerr << " [" << Res << "] ";
Devang Patelbf3f5a02009-01-30 01:03:10 +00001016
Devang Patelbf3f5a02009-01-30 01:03:10 +00001017 unsigned Tag = getTag();
1018 cerr << " [" << dwarf::TagString(Tag) << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001019
Devang Patelbf3f5a02009-01-30 01:03:10 +00001020 // TODO : Print context
1021 getCompileUnit().dump();
1022 cerr << " [" << getLineNumber() << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001023
Devang Patelbf3f5a02009-01-30 01:03:10 +00001024 if (isLocalToUnit())
1025 cerr << " [local] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001026
Devang Patelbf3f5a02009-01-30 01:03:10 +00001027 if (isDefinition())
1028 cerr << " [def] ";
1029
1030 if (isGlobalVariable(Tag))
1031 DIGlobalVariable(GV).dump();
1032
1033 cerr << "\n";
1034}
1035
1036/// dump - print subprogram.
1037void DISubprogram::dump() const {
1038 DIGlobal::dump();
1039}
1040
1041/// dump - print global variable.
1042void DIGlobalVariable::dump() const {
1043 cerr << " ["; getGlobal()->dump(); cerr << "] ";
1044}
1045
1046/// dump - print variable.
1047void DIVariable::dump() const {
Bill Wendling0582ae92009-03-13 04:39:26 +00001048 std::string Res;
1049 if (!getName(Res).empty())
1050 cerr << " [" << Res << "] ";
Bill Wendlingccbdc7a2009-03-09 05:04:40 +00001051
Devang Patelbf3f5a02009-01-30 01:03:10 +00001052 getCompileUnit().dump();
1053 cerr << " [" << getLineNumber() << "] ";
1054 getType().dump();
1055 cerr << "\n";
1056}