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 unsigned DebugInfoDesc::getLinkage() const {
96 return (unsigned)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()) {}
112 // Implement isa/cast/dyncast.
113 bool AnchorDesc::classof(const DebugInfoDesc *D) {
114 return D->getTag() == DW_TAG_anchor;
117 /// getLinkage - get linkage appropriate for this type of descriptor.
118 unsigned AnchorDesc::getLinkage() const {
119 return (unsigned)GlobalValue::LinkOnceLinkage;
122 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
123 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
124 DebugInfoDesc::ApplyToFields(Visitor);
125 Visitor->Apply(AnchorTag);
128 /// getDescString - Return a string used to compose global names and labels. A
129 /// global variable name needs to be defined for each debug descriptor that is
130 /// anchored. NOTE: that each global variable named here also needs to be added
131 /// to the list of names left external in the internalizer.
133 /// ExternalNames.insert("llvm.dbg.compile_units");
134 /// ExternalNames.insert("llvm.dbg.global_variables");
135 /// ExternalNames.insert("llvm.dbg.subprograms");
136 const char *AnchorDesc::getDescString() const {
138 case DW_TAG_compile_unit: {
140 return CUD.getAnchorString();
142 case DW_TAG_variable: {
143 GlobalVariableDesc GVD;
144 return GVD.getAnchorString();
146 case DW_TAG_subprogram: {
148 return SPD.getAnchorString();
153 assert(0 && "Tag does not have a case for anchor string");
158 void AnchorDesc::dump() {
159 cerr << getDescString() << " "
160 << "Version(" << getVersion() << "), "
161 << "Tag(" << getTag() << "), "
162 << "AnchorTag(" << AnchorTag << ")\n";
166 //===----------------------------------------------------------------------===//
168 AnchoredDesc::AnchoredDesc(unsigned T)
169 : DebugInfoDesc(T), Anchor(NULL) {}
171 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
172 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
173 DebugInfoDesc::ApplyToFields(Visitor);
174 Visitor->Apply(Anchor);
177 //===----------------------------------------------------------------------===//
179 CompileUnitDesc::CompileUnitDesc()
180 : AnchoredDesc(DW_TAG_compile_unit), Language(0), FileName(""),
181 Directory(""), Producer("") {}
183 // Implement isa/cast/dyncast.
184 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
185 return D->getTag() == DW_TAG_compile_unit;
188 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
190 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
191 AnchoredDesc::ApplyToFields(Visitor);
193 // Handle cases out of sync with compiler.
194 if (getVersion() == 0) {
195 unsigned DebugVersion;
196 Visitor->Apply(DebugVersion);
199 Visitor->Apply(Language);
200 Visitor->Apply(FileName);
201 Visitor->Apply(Directory);
202 Visitor->Apply(Producer);
206 void CompileUnitDesc::dump() {
207 cerr << getDescString() << " "
208 << "Version(" << getVersion() << "), "
209 << "Tag(" << getTag() << "), "
210 << "Anchor(" << getAnchor() << "), "
211 << "Language(" << Language << "), "
212 << "FileName(\"" << FileName << "\"), "
213 << "Directory(\"" << Directory << "\"), "
214 << "Producer(\"" << Producer << "\")\n";
218 //===----------------------------------------------------------------------===//
220 TypeDesc::TypeDesc(unsigned T)
221 : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), Size(0),
222 Align(0), Offset(0), Flags(0) {}
224 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
226 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
227 DebugInfoDesc::ApplyToFields(Visitor);
228 Visitor->Apply(Context);
229 Visitor->Apply(Name);
230 Visitor->Apply(File);
231 Visitor->Apply(Line);
232 Visitor->Apply(Size);
233 Visitor->Apply(Align);
234 Visitor->Apply(Offset);
235 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
239 void TypeDesc::dump() {
240 cerr << getDescString() << " "
241 << "Version(" << getVersion() << "), "
242 << "Tag(" << getTag() << "), "
243 << "Context(" << Context << "), "
244 << "Name(\"" << Name << "\"), "
245 << "File(" << File << "), "
246 << "Line(" << Line << "), "
247 << "Size(" << Size << "), "
248 << "Align(" << Align << "), "
249 << "Offset(" << Offset << "), "
250 << "Flags(" << Flags << ")\n";
254 //===----------------------------------------------------------------------===//
256 BasicTypeDesc::BasicTypeDesc()
257 : TypeDesc(DW_TAG_base_type), Encoding(0) {}
259 // Implement isa/cast/dyncast.
260 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
261 return D->getTag() == DW_TAG_base_type;
264 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
265 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
266 TypeDesc::ApplyToFields(Visitor);
267 Visitor->Apply(Encoding);
271 void BasicTypeDesc::dump() {
272 cerr << getDescString() << " "
273 << "Version(" << getVersion() << "), "
274 << "Tag(" << getTag() << "), "
275 << "Context(" << getContext() << "), "
276 << "Name(\"" << getName() << "\"), "
277 << "Size(" << getSize() << "), "
278 << "Encoding(" << Encoding << ")\n";
282 //===----------------------------------------------------------------------===//
284 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
285 : TypeDesc(T), FromType(NULL) {}
287 // Implement isa/cast/dyncast.
288 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
289 unsigned T = D->getTag();
292 case DW_TAG_pointer_type:
293 case DW_TAG_reference_type:
294 case DW_TAG_const_type:
295 case DW_TAG_volatile_type:
296 case DW_TAG_restrict_type:
298 case DW_TAG_inheritance:
305 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
306 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
307 TypeDesc::ApplyToFields(Visitor);
308 Visitor->Apply(FromType);
312 void DerivedTypeDesc::dump() {
313 cerr << getDescString() << " "
314 << "Version(" << getVersion() << "), "
315 << "Tag(" << getTag() << "), "
316 << "Context(" << getContext() << "), "
317 << "Name(\"" << getName() << "\"), "
318 << "Size(" << getSize() << "), "
319 << "File(" << getFile() << "), "
320 << "Line(" << getLine() << "), "
321 << "FromType(" << FromType << ")\n";
325 //===----------------------------------------------------------------------===//
327 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
328 : DerivedTypeDesc(T), Elements() {}
330 // Implement isa/cast/dyncast.
331 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
332 unsigned T = D->getTag();
334 case DW_TAG_array_type:
335 case DW_TAG_structure_type:
336 case DW_TAG_union_type:
337 case DW_TAG_enumeration_type:
338 case DW_TAG_vector_type:
339 case DW_TAG_subroutine_type:
346 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
348 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
349 DerivedTypeDesc::ApplyToFields(Visitor);
350 Visitor->Apply(Elements);
354 void CompositeTypeDesc::dump() {
355 cerr << getDescString() << " "
356 << "Version(" << getVersion() << "), "
357 << "Tag(" << getTag() << "), "
358 << "Context(" << getContext() << "), "
359 << "Name(\"" << getName() << "\"), "
360 << "Size(" << getSize() << "), "
361 << "File(" << getFile() << "), "
362 << "Line(" << getLine() << "), "
363 << "FromType(" << getFromType() << "), "
364 << "Elements.size(" << Elements.size() << ")\n";
368 //===----------------------------------------------------------------------===//
370 SubrangeDesc::SubrangeDesc()
371 : DebugInfoDesc(DW_TAG_subrange_type), Lo(0), Hi(0) {}
373 // Implement isa/cast/dyncast.
374 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
375 return D->getTag() == DW_TAG_subrange_type;
378 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
379 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
380 DebugInfoDesc::ApplyToFields(Visitor);
386 void SubrangeDesc::dump() {
387 cerr << getDescString() << " "
388 << "Version(" << getVersion() << "), "
389 << "Tag(" << getTag() << "), "
390 << "Lo(" << Lo << "), "
391 << "Hi(" << Hi << ")\n";
395 //===----------------------------------------------------------------------===//
397 EnumeratorDesc::EnumeratorDesc()
398 : DebugInfoDesc(DW_TAG_enumerator), Name(""), Value(0) {}
400 // Implement isa/cast/dyncast.
401 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
402 return D->getTag() == DW_TAG_enumerator;
405 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
406 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
407 DebugInfoDesc::ApplyToFields(Visitor);
408 Visitor->Apply(Name);
409 Visitor->Apply(Value);
413 void EnumeratorDesc::dump() {
414 cerr << getDescString() << " "
415 << "Version(" << getVersion() << "), "
416 << "Tag(" << getTag() << "), "
417 << "Name(" << Name << "), "
418 << "Value(" << Value << ")\n";
422 //===----------------------------------------------------------------------===//
424 VariableDesc::VariableDesc(unsigned T)
425 : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), TyDesc(0)
428 // Implement isa/cast/dyncast.
429 bool VariableDesc::classof(const DebugInfoDesc *D) {
430 unsigned T = D->getTag();
432 case DW_TAG_auto_variable:
433 case DW_TAG_arg_variable:
434 case DW_TAG_return_variable:
441 /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
442 void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
443 DebugInfoDesc::ApplyToFields(Visitor);
444 Visitor->Apply(Context);
445 Visitor->Apply(Name);
446 Visitor->Apply(File);
447 Visitor->Apply(Line);
448 Visitor->Apply(TyDesc);
452 void VariableDesc::dump() {
453 cerr << getDescString() << " "
454 << "Version(" << getVersion() << "), "
455 << "Tag(" << getTag() << "), "
456 << "Context(" << Context << "), "
457 << "Name(\"" << Name << "\"), "
458 << "File(" << File << "), "
459 << "Line(" << Line << "), "
460 << "TyDesc(" << TyDesc << ")\n";
464 //===----------------------------------------------------------------------===//
466 GlobalDesc::GlobalDesc(unsigned T)
467 : AnchoredDesc(T), Context(0), Name(""), FullName(""), LinkageName(""),
468 File(NULL), Line(0), TyDesc(NULL), IsStatic(false), IsDefinition(false) {}
470 /// ApplyToFields - Target the visitor to the fields of the global.
472 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
473 AnchoredDesc::ApplyToFields(Visitor);
474 Visitor->Apply(Context);
475 Visitor->Apply(Name);
476 Visitor->Apply(FullName);
477 Visitor->Apply(LinkageName);
478 Visitor->Apply(File);
479 Visitor->Apply(Line);
480 Visitor->Apply(TyDesc);
481 Visitor->Apply(IsStatic);
482 Visitor->Apply(IsDefinition);
485 //===----------------------------------------------------------------------===//
487 GlobalVariableDesc::GlobalVariableDesc()
488 : GlobalDesc(DW_TAG_variable), Global(NULL) {}
490 // Implement isa/cast/dyncast.
491 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
492 return D->getTag() == DW_TAG_variable;
495 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
496 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
497 GlobalDesc::ApplyToFields(Visitor);
498 Visitor->Apply(Global);
502 void GlobalVariableDesc::dump() {
503 cerr << getDescString() << " "
504 << "Version(" << getVersion() << "), "
505 << "Tag(" << getTag() << "), "
506 << "Anchor(" << getAnchor() << "), "
507 << "Name(\"" << getName() << "\"), "
508 << "FullName(\"" << getFullName() << "\"), "
509 << "LinkageName(\"" << getLinkageName() << "\"), "
510 << "File(" << getFile() << "),"
511 << "Line(" << getLine() << "),"
512 << "Type(" << getType() << "), "
513 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
514 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
515 << "Global(" << Global << ")\n";
519 //===----------------------------------------------------------------------===//
521 SubprogramDesc::SubprogramDesc()
522 : GlobalDesc(DW_TAG_subprogram) {}
524 // Implement isa/cast/dyncast.
525 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
526 return D->getTag() == DW_TAG_subprogram;
529 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
530 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
531 GlobalDesc::ApplyToFields(Visitor);
535 void SubprogramDesc::dump() {
536 cerr << getDescString() << " "
537 << "Version(" << getVersion() << "), "
538 << "Tag(" << getTag() << "), "
539 << "Anchor(" << getAnchor() << "), "
540 << "Name(\"" << getName() << "\"), "
541 << "FullName(\"" << getFullName() << "\"), "
542 << "LinkageName(\"" << getLinkageName() << "\"), "
543 << "File(" << getFile() << "),"
544 << "Line(" << getLine() << "),"
545 << "Type(" << getType() << "), "
546 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
547 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
551 //===----------------------------------------------------------------------===//
553 BlockDesc::BlockDesc()
554 : DebugInfoDesc(DW_TAG_lexical_block), Context(NULL) {}
556 // Implement isa/cast/dyncast.
557 bool BlockDesc::classof(const DebugInfoDesc *D) {
558 return D->getTag() == DW_TAG_lexical_block;
561 /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
562 void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
563 DebugInfoDesc::ApplyToFields(Visitor);
565 Visitor->Apply(Context);
569 void BlockDesc::dump() {
570 cerr << getDescString() << " "
571 << "Version(" << getVersion() << "), "
572 << "Tag(" << getTag() << "),"
573 << "Context(" << Context << ")\n";