1 //===-- llvm/CodeGen/MachineDebugInfoDesc.cpp -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineDebugInfoDesc.h"
11 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/Support/Dwarf.h"
15 #include "llvm/Support/Streams.h"
18 using namespace llvm::dwarf;
20 /// getUIntOperand - Return ith operand if it is an unsigned integer.
22 static ConstantInt *getUIntOperand(const GlobalVariable *GV, unsigned i) {
23 // Make sure the GlobalVariable has an initializer.
24 if (!GV->hasInitializer()) return NULL;
26 // Get the initializer constant.
27 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
30 // Check if there is at least i + 1 operands.
31 unsigned N = CI->getNumOperands();
32 if (i >= N) return NULL;
35 return dyn_cast<ConstantInt>(CI->getOperand(i));
38 //===----------------------------------------------------------------------===//
40 /// Supply a home for the DebugInfoDesc's v-table.
41 DebugInfoDesc::~DebugInfoDesc() {}
43 /// TagFromGlobal - Returns the tag number from a debug info descriptor
44 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
45 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
46 ConstantInt *C = getUIntOperand(GV, 0);
47 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
48 (unsigned)DW_TAG_invalid;
51 /// VersionFromGlobal - Returns the version number from a debug info
52 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
54 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
55 ConstantInt *C = getUIntOperand(GV, 0);
56 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
57 (unsigned)DW_TAG_invalid;
60 /// DescFactory - Create an instance of debug info descriptor based on Tag.
61 /// Return NULL if not a recognized Tag.
62 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
64 case DW_TAG_anchor: return new AnchorDesc();
65 case DW_TAG_compile_unit: return new CompileUnitDesc();
66 case DW_TAG_variable: return new GlobalVariableDesc();
67 case DW_TAG_subprogram: return new SubprogramDesc();
68 case DW_TAG_lexical_block: return new BlockDesc();
69 case DW_TAG_base_type: return new BasicTypeDesc();
71 case DW_TAG_pointer_type:
72 case DW_TAG_reference_type:
73 case DW_TAG_const_type:
74 case DW_TAG_volatile_type:
75 case DW_TAG_restrict_type:
77 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
78 case DW_TAG_array_type:
79 case DW_TAG_structure_type:
80 case DW_TAG_union_type:
81 case DW_TAG_enumeration_type:
82 case DW_TAG_vector_type:
83 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
84 case DW_TAG_subrange_type: return new SubrangeDesc();
85 case DW_TAG_enumerator: return new EnumeratorDesc();
86 case DW_TAG_return_variable:
87 case DW_TAG_arg_variable:
88 case DW_TAG_auto_variable: return new VariableDesc(Tag);
94 /// getLinkage - get linkage appropriate for this type of descriptor.
95 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
96 return GlobalValue::InternalLinkage;
99 /// ApplyToFields - Target the vistor to the fields of the descriptor.
100 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
104 //===----------------------------------------------------------------------===//
106 AnchorDesc::AnchorDesc()
107 : DebugInfoDesc(DW_TAG_anchor), AnchorTag(0){
109 AnchorDesc::AnchorDesc(AnchoredDesc *D)
110 : DebugInfoDesc(DW_TAG_anchor), AnchorTag(D->getTag()) {
113 // Implement isa/cast/dyncast.
114 bool AnchorDesc::classof(const DebugInfoDesc *D) {
115 return D->getTag() == DW_TAG_anchor;
118 /// getLinkage - get linkage appropriate for this type of descriptor.
119 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
120 return GlobalValue::LinkOnceLinkage;
123 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
124 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
125 DebugInfoDesc::ApplyToFields(Visitor);
126 Visitor->Apply(AnchorTag);
129 /// getDescString - Return a string used to compose global names and labels. A
130 /// global variable name needs to be defined for each debug descriptor that is
131 /// anchored. NOTE: that each global variable named here also needs to be added
132 /// to the list of names left external in the internalizer.
134 /// ExternalNames.insert("llvm.dbg.compile_units");
135 /// ExternalNames.insert("llvm.dbg.global_variables");
136 /// ExternalNames.insert("llvm.dbg.subprograms");
137 const char *AnchorDesc::getDescString() const {
139 case DW_TAG_compile_unit: {
141 return CUD.getAnchorString();
143 case DW_TAG_variable: {
144 GlobalVariableDesc GVD;
145 return GVD.getAnchorString();
147 case DW_TAG_subprogram: {
149 return SPD.getAnchorString();
154 assert(0 && "Tag does not have a case for anchor string");
159 void AnchorDesc::dump() {
160 cerr << getDescString() << " "
161 << "Version(" << getVersion() << "), "
162 << "Tag(" << getTag() << "), "
163 << "AnchorTag(" << AnchorTag << ")\n";
167 //===----------------------------------------------------------------------===//
169 AnchoredDesc::AnchoredDesc(unsigned T)
170 : DebugInfoDesc(T), Anchor(NULL) {
173 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
174 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
175 DebugInfoDesc::ApplyToFields(Visitor);
176 Visitor->Apply(Anchor);
179 //===----------------------------------------------------------------------===//
181 CompileUnitDesc::CompileUnitDesc()
182 : AnchoredDesc(DW_TAG_compile_unit), Language(0), FileName(""),
183 Directory(""), Producer("") {
186 // Implement isa/cast/dyncast.
187 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
188 return D->getTag() == DW_TAG_compile_unit;
191 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
193 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
194 AnchoredDesc::ApplyToFields(Visitor);
196 // Handle cases out of sync with compiler.
197 if (getVersion() == 0) {
198 unsigned DebugVersion;
199 Visitor->Apply(DebugVersion);
202 Visitor->Apply(Language);
203 Visitor->Apply(FileName);
204 Visitor->Apply(Directory);
205 Visitor->Apply(Producer);
209 void CompileUnitDesc::dump() {
210 cerr << getDescString() << " "
211 << "Version(" << getVersion() << "), "
212 << "Tag(" << getTag() << "), "
213 << "Anchor(" << getAnchor() << "), "
214 << "Language(" << Language << "), "
215 << "FileName(\"" << FileName << "\"), "
216 << "Directory(\"" << Directory << "\"), "
217 << "Producer(\"" << Producer << "\")\n";
221 //===----------------------------------------------------------------------===//
223 TypeDesc::TypeDesc(unsigned T)
224 : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), Size(0),
225 Align(0), Offset(0), Flags(0) {
228 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
230 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
231 DebugInfoDesc::ApplyToFields(Visitor);
232 Visitor->Apply(Context);
233 Visitor->Apply(Name);
234 Visitor->Apply(File);
235 Visitor->Apply(Line);
236 Visitor->Apply(Size);
237 Visitor->Apply(Align);
238 Visitor->Apply(Offset);
239 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
243 void TypeDesc::dump() {
244 cerr << getDescString() << " "
245 << "Version(" << getVersion() << "), "
246 << "Tag(" << getTag() << "), "
247 << "Context(" << Context << "), "
248 << "Name(\"" << Name << "\"), "
249 << "File(" << File << "), "
250 << "Line(" << Line << "), "
251 << "Size(" << Size << "), "
252 << "Align(" << Align << "), "
253 << "Offset(" << Offset << "), "
254 << "Flags(" << Flags << ")\n";
258 //===----------------------------------------------------------------------===//
260 BasicTypeDesc::BasicTypeDesc()
261 : TypeDesc(DW_TAG_base_type), Encoding(0) {
264 // Implement isa/cast/dyncast.
265 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
266 return D->getTag() == DW_TAG_base_type;
269 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
270 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
271 TypeDesc::ApplyToFields(Visitor);
272 Visitor->Apply(Encoding);
276 void BasicTypeDesc::dump() {
277 cerr << getDescString() << " "
278 << "Version(" << getVersion() << "), "
279 << "Tag(" << getTag() << "), "
280 << "Context(" << getContext() << "), "
281 << "Name(\"" << getName() << "\"), "
282 << "Size(" << getSize() << "), "
283 << "Encoding(" << Encoding << ")\n";
287 //===----------------------------------------------------------------------===//
289 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
290 : TypeDesc(T), FromType(NULL) {
293 // Implement isa/cast/dyncast.
294 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
295 unsigned T = D->getTag();
298 case DW_TAG_pointer_type:
299 case DW_TAG_reference_type:
300 case DW_TAG_const_type:
301 case DW_TAG_volatile_type:
302 case DW_TAG_restrict_type:
304 case DW_TAG_inheritance:
311 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
312 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
313 TypeDesc::ApplyToFields(Visitor);
314 Visitor->Apply(FromType);
318 void DerivedTypeDesc::dump() {
319 cerr << getDescString() << " "
320 << "Version(" << getVersion() << "), "
321 << "Tag(" << getTag() << "), "
322 << "Context(" << getContext() << "), "
323 << "Name(\"" << getName() << "\"), "
324 << "Size(" << getSize() << "), "
325 << "File(" << getFile() << "), "
326 << "Line(" << getLine() << "), "
327 << "FromType(" << FromType << ")\n";
331 //===----------------------------------------------------------------------===//
333 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
334 : DerivedTypeDesc(T), Elements() {
337 // Implement isa/cast/dyncast.
338 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
339 unsigned T = D->getTag();
341 case DW_TAG_array_type:
342 case DW_TAG_structure_type:
343 case DW_TAG_union_type:
344 case DW_TAG_enumeration_type:
345 case DW_TAG_vector_type:
346 case DW_TAG_subroutine_type:
353 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
355 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
356 DerivedTypeDesc::ApplyToFields(Visitor);
357 Visitor->Apply(Elements);
361 void CompositeTypeDesc::dump() {
362 cerr << getDescString() << " "
363 << "Version(" << getVersion() << "), "
364 << "Tag(" << getTag() << "), "
365 << "Context(" << getContext() << "), "
366 << "Name(\"" << getName() << "\"), "
367 << "Size(" << getSize() << "), "
368 << "File(" << getFile() << "), "
369 << "Line(" << getLine() << "), "
370 << "FromType(" << getFromType() << "), "
371 << "Elements.size(" << Elements.size() << ")\n";
375 //===----------------------------------------------------------------------===//
377 SubrangeDesc::SubrangeDesc()
378 : DebugInfoDesc(DW_TAG_subrange_type), Lo(0), Hi(0) {
381 // Implement isa/cast/dyncast.
382 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
383 return D->getTag() == DW_TAG_subrange_type;
386 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
387 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
388 DebugInfoDesc::ApplyToFields(Visitor);
394 void SubrangeDesc::dump() {
395 cerr << getDescString() << " "
396 << "Version(" << getVersion() << "), "
397 << "Tag(" << getTag() << "), "
398 << "Lo(" << Lo << "), "
399 << "Hi(" << Hi << ")\n";
403 //===----------------------------------------------------------------------===//
405 EnumeratorDesc::EnumeratorDesc()
406 : DebugInfoDesc(DW_TAG_enumerator), Name(""), Value(0) {
409 // Implement isa/cast/dyncast.
410 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
411 return D->getTag() == DW_TAG_enumerator;
414 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
415 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
416 DebugInfoDesc::ApplyToFields(Visitor);
417 Visitor->Apply(Name);
418 Visitor->Apply(Value);
422 void EnumeratorDesc::dump() {
423 cerr << getDescString() << " "
424 << "Version(" << getVersion() << "), "
425 << "Tag(" << getTag() << "), "
426 << "Name(" << Name << "), "
427 << "Value(" << Value << ")\n";
431 //===----------------------------------------------------------------------===//
433 VariableDesc::VariableDesc(unsigned T)
434 : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), TyDesc(0) {
437 // Implement isa/cast/dyncast.
438 bool VariableDesc::classof(const DebugInfoDesc *D) {
439 unsigned T = D->getTag();
441 case DW_TAG_auto_variable:
442 case DW_TAG_arg_variable:
443 case DW_TAG_return_variable:
450 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
451 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
452 DebugInfoDesc::ApplyToFields(Visitor);
453 Visitor->Apply(Context);
454 Visitor->Apply(Name);
455 Visitor->Apply(File);
456 Visitor->Apply(Line);
457 Visitor->Apply(TyDesc);
461 void VariableDesc::dump() {
462 cerr << getDescString() << " "
463 << "Version(" << getVersion() << "), "
464 << "Tag(" << getTag() << "), "
465 << "Context(" << Context << "), "
466 << "Name(\"" << Name << "\"), "
467 << "File(" << File << "), "
468 << "Line(" << Line << "), "
469 << "TyDesc(" << TyDesc << ")\n";
473 //===----------------------------------------------------------------------===//
475 GlobalDesc::GlobalDesc(unsigned T)
476 : AnchoredDesc(T), Context(0), Name(""), FullName(""), LinkageName(""),
477 File(NULL), Line(0), TyDesc(NULL), IsStatic(false), IsDefinition(false) {
480 /// ApplyToFields - Target the visitor to the fields of the global.
482 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
483 AnchoredDesc::ApplyToFields(Visitor);
484 Visitor->Apply(Context);
485 Visitor->Apply(Name);
486 Visitor->Apply(FullName);
487 Visitor->Apply(LinkageName);
488 Visitor->Apply(File);
489 Visitor->Apply(Line);
490 Visitor->Apply(TyDesc);
491 Visitor->Apply(IsStatic);
492 Visitor->Apply(IsDefinition);
495 //===----------------------------------------------------------------------===//
497 GlobalVariableDesc::GlobalVariableDesc()
498 : GlobalDesc(DW_TAG_variable), Global(NULL) {
501 // Implement isa/cast/dyncast.
502 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
503 return D->getTag() == DW_TAG_variable;
506 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
507 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
508 GlobalDesc::ApplyToFields(Visitor);
509 Visitor->Apply(Global);
513 void GlobalVariableDesc::dump() {
514 cerr << getDescString() << " "
515 << "Version(" << getVersion() << "), "
516 << "Tag(" << getTag() << "), "
517 << "Anchor(" << getAnchor() << "), "
518 << "Name(\"" << getName() << "\"), "
519 << "FullName(\"" << getFullName() << "\"), "
520 << "LinkageName(\"" << getLinkageName() << "\"), "
521 << "File(" << getFile() << "),"
522 << "Line(" << getLine() << "),"
523 << "Type(" << getType() << "), "
524 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
525 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
526 << "Global(" << Global << ")\n";
530 //===----------------------------------------------------------------------===//
532 SubprogramDesc::SubprogramDesc()
533 : GlobalDesc(DW_TAG_subprogram) {
536 // Implement isa/cast/dyncast.
537 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
538 return D->getTag() == DW_TAG_subprogram;
541 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
542 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
543 GlobalDesc::ApplyToFields(Visitor);
547 void SubprogramDesc::dump() {
548 cerr << getDescString() << " "
549 << "Version(" << getVersion() << "), "
550 << "Tag(" << getTag() << "), "
551 << "Anchor(" << getAnchor() << "), "
552 << "Name(\"" << getName() << "\"), "
553 << "FullName(\"" << getFullName() << "\"), "
554 << "LinkageName(\"" << getLinkageName() << "\"), "
555 << "File(" << getFile() << "),"
556 << "Line(" << getLine() << "),"
557 << "Type(" << getType() << "), "
558 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
559 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
563 //===----------------------------------------------------------------------===//
565 BlockDesc::BlockDesc()
566 : DebugInfoDesc(DW_TAG_lexical_block), Context(NULL) {
569 // Implement isa/cast/dyncast.
570 bool BlockDesc::classof(const DebugInfoDesc *D) {
571 return D->getTag() == DW_TAG_lexical_block;
574 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
575 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
576 DebugInfoDesc::ApplyToFields(Visitor);
578 Visitor->Apply(Context);
582 void BlockDesc::dump() {
583 cerr << getDescString() << " "
584 << "Version(" << getVersion() << "), "
585 << "Tag(" << getTag() << "),"
586 << "Context(" << Context << ")\n";