blob: 23853862c28fa51c095c89c1eacee2e14b0ea7e1 [file] [log] [blame]
Bill Wendling305635a2008-06-27 00:09:40 +00001//===-- llvm/CodeGen/MachineDebugInfoDesc.cpp -------------------*- C++ -*-===//
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#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"
16
17using namespace llvm;
18using namespace llvm::dwarf;
19
20/// getUIntOperand - Return ith operand if it is an unsigned integer.
21///
22static ConstantInt *getUIntOperand(const GlobalVariable *GV, unsigned i) {
23 // Make sure the GlobalVariable has an initializer.
24 if (!GV->hasInitializer()) return NULL;
25
26 // Get the initializer constant.
27 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
28 if (!CI) return NULL;
29
30 // Check if there is at least i + 1 operands.
31 unsigned N = CI->getNumOperands();
32 if (i >= N) return NULL;
33
34 // Check constant.
35 return dyn_cast<ConstantInt>(CI->getOperand(i));
36}
37
38//===----------------------------------------------------------------------===//
39
40/// Supply a home for the DebugInfoDesc's v-table.
41DebugInfoDesc::~DebugInfoDesc() {}
42
43/// TagFromGlobal - Returns the tag number from a debug info descriptor
44/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
45unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
46 ConstantInt *C = getUIntOperand(GV, 0);
47 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
48 (unsigned)DW_TAG_invalid;
49}
50
51/// VersionFromGlobal - Returns the version number from a debug info
52/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
53/// int.
54unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
55 ConstantInt *C = getUIntOperand(GV, 0);
56 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
57 (unsigned)DW_TAG_invalid;
58}
59
60/// DescFactory - Create an instance of debug info descriptor based on Tag.
61/// Return NULL if not a recognized Tag.
62DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
63 switch (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();
70 case DW_TAG_typedef:
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:
76 case DW_TAG_member:
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);
89 default: break;
90 }
91 return NULL;
92}
93
94/// getLinkage - get linkage appropriate for this type of descriptor.
Bill Wendling12432cf2008-07-02 00:35:47 +000095GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
96 return GlobalValue::InternalLinkage;
Bill Wendling305635a2008-06-27 00:09:40 +000097}
98
99/// ApplyToFields - Target the vistor to the fields of the descriptor.
100void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
101 Visitor->Apply(Tag);
102}
103
104//===----------------------------------------------------------------------===//
105
106AnchorDesc::AnchorDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000107 : DebugInfoDesc(DW_TAG_anchor), AnchorTag(0) {}
108
Bill Wendling305635a2008-06-27 00:09:40 +0000109AnchorDesc::AnchorDesc(AnchoredDesc *D)
Bill Wendlinga28cd122008-07-01 22:08:01 +0000110 : DebugInfoDesc(DW_TAG_anchor), AnchorTag(D->getTag()) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000111
112// Implement isa/cast/dyncast.
113bool AnchorDesc::classof(const DebugInfoDesc *D) {
114 return D->getTag() == DW_TAG_anchor;
115}
116
117/// getLinkage - get linkage appropriate for this type of descriptor.
Bill Wendling12432cf2008-07-02 00:35:47 +0000118GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
119 return GlobalValue::LinkOnceLinkage;
Bill Wendling305635a2008-06-27 00:09:40 +0000120}
121
122/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
123void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
124 DebugInfoDesc::ApplyToFields(Visitor);
125 Visitor->Apply(AnchorTag);
126}
127
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.
132///
133/// ExternalNames.insert("llvm.dbg.compile_units");
134/// ExternalNames.insert("llvm.dbg.global_variables");
135/// ExternalNames.insert("llvm.dbg.subprograms");
136const char *AnchorDesc::getDescString() const {
137 switch (AnchorTag) {
138 case DW_TAG_compile_unit: {
139 CompileUnitDesc CUD;
140 return CUD.getAnchorString();
141 }
142 case DW_TAG_variable: {
143 GlobalVariableDesc GVD;
144 return GVD.getAnchorString();
145 }
146 case DW_TAG_subprogram: {
147 SubprogramDesc SPD;
148 return SPD.getAnchorString();
149 }
150 default: break;
151 }
152
153 assert(0 && "Tag does not have a case for anchor string");
154 return "";
155}
156
157#ifndef NDEBUG
158void AnchorDesc::dump() {
159 cerr << getDescString() << " "
160 << "Version(" << getVersion() << "), "
161 << "Tag(" << getTag() << "), "
162 << "AnchorTag(" << AnchorTag << ")\n";
163}
164#endif
165
166//===----------------------------------------------------------------------===//
167
168AnchoredDesc::AnchoredDesc(unsigned T)
Bill Wendlinga28cd122008-07-01 22:08:01 +0000169 : DebugInfoDesc(T), Anchor(NULL) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000170
171/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
172void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
173 DebugInfoDesc::ApplyToFields(Visitor);
174 Visitor->Apply(Anchor);
175}
176
177//===----------------------------------------------------------------------===//
178
179CompileUnitDesc::CompileUnitDesc()
180 : AnchoredDesc(DW_TAG_compile_unit), Language(0), FileName(""),
Bill Wendlinga28cd122008-07-01 22:08:01 +0000181 Directory(""), Producer("") {}
Bill Wendling305635a2008-06-27 00:09:40 +0000182
183// Implement isa/cast/dyncast.
184bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
185 return D->getTag() == DW_TAG_compile_unit;
186}
187
188/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
189///
190void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
191 AnchoredDesc::ApplyToFields(Visitor);
192
193 // Handle cases out of sync with compiler.
194 if (getVersion() == 0) {
195 unsigned DebugVersion;
196 Visitor->Apply(DebugVersion);
197 }
198
199 Visitor->Apply(Language);
200 Visitor->Apply(FileName);
201 Visitor->Apply(Directory);
202 Visitor->Apply(Producer);
203}
204
205#ifndef NDEBUG
206void 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";
215}
216#endif
217
218//===----------------------------------------------------------------------===//
219
220TypeDesc::TypeDesc(unsigned T)
221 : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), Size(0),
Bill Wendlinga28cd122008-07-01 22:08:01 +0000222 Align(0), Offset(0), Flags(0) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000223
224/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
225///
226void 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);
236}
237
238#ifndef NDEBUG
239void 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";
251}
252#endif
253
254//===----------------------------------------------------------------------===//
255
256BasicTypeDesc::BasicTypeDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000257 : TypeDesc(DW_TAG_base_type), Encoding(0) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000258
259// Implement isa/cast/dyncast.
260bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
261 return D->getTag() == DW_TAG_base_type;
262}
263
264/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
265void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
266 TypeDesc::ApplyToFields(Visitor);
267 Visitor->Apply(Encoding);
268}
269
270#ifndef NDEBUG
271void BasicTypeDesc::dump() {
272 cerr << getDescString() << " "
273 << "Version(" << getVersion() << "), "
274 << "Tag(" << getTag() << "), "
275 << "Context(" << getContext() << "), "
276 << "Name(\"" << getName() << "\"), "
277 << "Size(" << getSize() << "), "
278 << "Encoding(" << Encoding << ")\n";
279}
280#endif
281
282//===----------------------------------------------------------------------===//
283
284DerivedTypeDesc::DerivedTypeDesc(unsigned T)
Bill Wendlinga28cd122008-07-01 22:08:01 +0000285 : TypeDesc(T), FromType(NULL) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000286
287// Implement isa/cast/dyncast.
288bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
289 unsigned T = D->getTag();
290 switch (T) {
291 case DW_TAG_typedef:
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:
297 case DW_TAG_member:
298 case DW_TAG_inheritance:
299 return true;
300 default: break;
301 }
302 return false;
303}
304
305/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
306void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
307 TypeDesc::ApplyToFields(Visitor);
308 Visitor->Apply(FromType);
309}
310
311#ifndef NDEBUG
312void 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";
322}
323#endif
324
325//===----------------------------------------------------------------------===//
326
327CompositeTypeDesc::CompositeTypeDesc(unsigned T)
Bill Wendlinga28cd122008-07-01 22:08:01 +0000328 : DerivedTypeDesc(T), Elements() {}
Bill Wendling305635a2008-06-27 00:09:40 +0000329
330// Implement isa/cast/dyncast.
331bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
332 unsigned T = D->getTag();
333 switch (T) {
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:
340 return true;
341 default: break;
342 }
343 return false;
344}
345
346/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
347///
348void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
349 DerivedTypeDesc::ApplyToFields(Visitor);
350 Visitor->Apply(Elements);
351}
352
353#ifndef NDEBUG
354void 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";
365}
366#endif
367
368//===----------------------------------------------------------------------===//
369
370SubrangeDesc::SubrangeDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000371 : DebugInfoDesc(DW_TAG_subrange_type), Lo(0), Hi(0) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000372
373// Implement isa/cast/dyncast.
374bool SubrangeDesc::classof(const DebugInfoDesc *D) {
375 return D->getTag() == DW_TAG_subrange_type;
376}
377
378/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
379void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
380 DebugInfoDesc::ApplyToFields(Visitor);
381 Visitor->Apply(Lo);
382 Visitor->Apply(Hi);
383}
384
385#ifndef NDEBUG
386void SubrangeDesc::dump() {
387 cerr << getDescString() << " "
388 << "Version(" << getVersion() << "), "
389 << "Tag(" << getTag() << "), "
390 << "Lo(" << Lo << "), "
391 << "Hi(" << Hi << ")\n";
392}
393#endif
394
395//===----------------------------------------------------------------------===//
396
397EnumeratorDesc::EnumeratorDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000398 : DebugInfoDesc(DW_TAG_enumerator), Name(""), Value(0) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000399
400// Implement isa/cast/dyncast.
401bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
402 return D->getTag() == DW_TAG_enumerator;
403}
404
405/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
406void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
407 DebugInfoDesc::ApplyToFields(Visitor);
408 Visitor->Apply(Name);
409 Visitor->Apply(Value);
410}
411
412#ifndef NDEBUG
413void EnumeratorDesc::dump() {
414 cerr << getDescString() << " "
415 << "Version(" << getVersion() << "), "
416 << "Tag(" << getTag() << "), "
417 << "Name(" << Name << "), "
418 << "Value(" << Value << ")\n";
419}
420#endif
421
422//===----------------------------------------------------------------------===//
423
424VariableDesc::VariableDesc(unsigned T)
Bill Wendlinga28cd122008-07-01 22:08:01 +0000425 : DebugInfoDesc(T), Context(NULL), Name(""), File(NULL), Line(0), TyDesc(0)
426{}
Bill Wendling305635a2008-06-27 00:09:40 +0000427
428// Implement isa/cast/dyncast.
429bool VariableDesc::classof(const DebugInfoDesc *D) {
430 unsigned T = D->getTag();
431 switch (T) {
432 case DW_TAG_auto_variable:
433 case DW_TAG_arg_variable:
434 case DW_TAG_return_variable:
435 return true;
436 default: break;
437 }
438 return false;
439}
440
441/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
442void 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);
449}
450
451#ifndef NDEBUG
452void 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";
461}
462#endif
463
464//===----------------------------------------------------------------------===//
465
466GlobalDesc::GlobalDesc(unsigned T)
467 : AnchoredDesc(T), Context(0), Name(""), FullName(""), LinkageName(""),
Bill Wendlinga28cd122008-07-01 22:08:01 +0000468 File(NULL), Line(0), TyDesc(NULL), IsStatic(false), IsDefinition(false) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000469
470/// ApplyToFields - Target the visitor to the fields of the global.
471///
472void 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);
483}
484
485//===----------------------------------------------------------------------===//
486
487GlobalVariableDesc::GlobalVariableDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000488 : GlobalDesc(DW_TAG_variable), Global(NULL) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000489
490// Implement isa/cast/dyncast.
491bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
492 return D->getTag() == DW_TAG_variable;
493}
494
495/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
496void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
497 GlobalDesc::ApplyToFields(Visitor);
498 Visitor->Apply(Global);
499}
500
501#ifndef NDEBUG
502void 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";
516}
517#endif
518
519//===----------------------------------------------------------------------===//
520
521SubprogramDesc::SubprogramDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000522 : GlobalDesc(DW_TAG_subprogram) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000523
524// Implement isa/cast/dyncast.
525bool SubprogramDesc::classof(const DebugInfoDesc *D) {
526 return D->getTag() == DW_TAG_subprogram;
527}
528
529/// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
530void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
531 GlobalDesc::ApplyToFields(Visitor);
532}
533
534#ifndef NDEBUG
535void 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";
548}
549#endif
550
551//===----------------------------------------------------------------------===//
552
553BlockDesc::BlockDesc()
Bill Wendlinga28cd122008-07-01 22:08:01 +0000554 : DebugInfoDesc(DW_TAG_lexical_block), Context(NULL) {}
Bill Wendling305635a2008-06-27 00:09:40 +0000555
556// Implement isa/cast/dyncast.
557bool BlockDesc::classof(const DebugInfoDesc *D) {
558 return D->getTag() == DW_TAG_lexical_block;
559}
560
561/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
562void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
563 DebugInfoDesc::ApplyToFields(Visitor);
564
565 Visitor->Apply(Context);
566}
567
568#ifndef NDEBUG
569void BlockDesc::dump() {
570 cerr << getDescString() << " "
571 << "Version(" << getVersion() << "), "
572 << "Tag(" << getTag() << "),"
573 << "Context(" << Context << ")\n";
574}
575#endif