DIE.h revision 9f765d178aec597dce6cc16b0d08db159498c87e
1//===--- lib/CodeGen/DIE.h - DWARF Info Entries -----------------*- 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// Data structures for DWARF info entries. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef CODEGEN_ASMPRINTER_DIE_H__ 15#define CODEGEN_ASMPRINTER_DIE_H__ 16 17#include "DwarfLabel.h" 18#include "llvm/ADT/FoldingSet.h" 19#include "llvm/ADT/SmallVector.h" 20#include "llvm/Support/Compiler.h" 21#include "llvm/Support/Dwarf.h" 22#include "llvm/Support/raw_ostream.h" 23#include <iosfwd> 24 25namespace llvm { 26 class AsmPrinter; 27 class Dwarf; 28 class TargetData; 29 30 //===--------------------------------------------------------------------===// 31 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a 32 /// Dwarf abbreviation. 33 class VISIBILITY_HIDDEN DIEAbbrevData { 34 /// Attribute - Dwarf attribute code. 35 /// 36 unsigned Attribute; 37 38 /// Form - Dwarf form code. 39 /// 40 unsigned Form; 41 public: 42 DIEAbbrevData(unsigned A, unsigned F) : Attribute(A), Form(F) {} 43 44 // Accessors. 45 unsigned getAttribute() const { return Attribute; } 46 unsigned getForm() const { return Form; } 47 48 /// Profile - Used to gather unique data for the abbreviation folding set. 49 /// 50 void Profile(FoldingSetNodeID &ID) const; 51 }; 52 53 //===--------------------------------------------------------------------===// 54 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug 55 /// information object. 56 class VISIBILITY_HIDDEN DIEAbbrev : public FoldingSetNode { 57 /// Tag - Dwarf tag code. 58 /// 59 unsigned Tag; 60 61 /// Unique number for node. 62 /// 63 unsigned Number; 64 65 /// ChildrenFlag - Dwarf children flag. 66 /// 67 unsigned ChildrenFlag; 68 69 /// Data - Raw data bytes for abbreviation. 70 /// 71 SmallVector<DIEAbbrevData, 8> Data; 72 public: 73 DIEAbbrev(unsigned T, unsigned C) : Tag(T), ChildrenFlag(C), Data() {} 74 virtual ~DIEAbbrev() {} 75 76 // Accessors. 77 unsigned getTag() const { return Tag; } 78 unsigned getNumber() const { return Number; } 79 unsigned getChildrenFlag() const { return ChildrenFlag; } 80 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; } 81 void setTag(unsigned T) { Tag = T; } 82 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; } 83 void setNumber(unsigned N) { Number = N; } 84 85 /// AddAttribute - Adds another set of attribute information to the 86 /// abbreviation. 87 void AddAttribute(unsigned Attribute, unsigned Form) { 88 Data.push_back(DIEAbbrevData(Attribute, Form)); 89 } 90 91 /// AddFirstAttribute - Adds a set of attribute information to the front 92 /// of the abbreviation. 93 void AddFirstAttribute(unsigned Attribute, unsigned Form) { 94 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form)); 95 } 96 97 /// Profile - Used to gather unique data for the abbreviation folding set. 98 /// 99 void Profile(FoldingSetNodeID &ID) const; 100 101 /// Emit - Print the abbreviation using the specified asm printer. 102 /// 103 void Emit(const AsmPrinter *Asm) const; 104 105#ifndef NDEBUG 106 void print(std::ostream *O) { 107 if (O) print(*O); 108 } 109 void print(std::ostream &O); 110 void dump(); 111#endif 112 }; 113 114 //===--------------------------------------------------------------------===// 115 /// DIE - A structured debug information entry. Has an abbreviation which 116 /// describes it's organization. 117 class CompileUnit; 118 class DIEValue; 119 120 class VISIBILITY_HIDDEN DIE : public FoldingSetNode { 121 protected: 122 /// Abbrev - Buffer for constructing abbreviation. 123 /// 124 DIEAbbrev Abbrev; 125 126 /// Offset - Offset in debug info section. 127 /// 128 unsigned Offset; 129 130 /// Size - Size of instance + children. 131 /// 132 unsigned Size; 133 134 /// Children DIEs. 135 /// 136 std::vector<DIE *> Children; 137 138 /// Attributes values. 139 /// 140 SmallVector<DIEValue*, 32> Values; 141 142 /// Abstract compile unit. 143 CompileUnit *AbstractCU; 144 145 // Private data for print() 146 mutable unsigned IndentCount; 147 public: 148 explicit DIE(unsigned Tag) 149 : Abbrev(Tag, dwarf::DW_CHILDREN_no), Offset(0), 150 Size(0), IndentCount(0) {} 151 virtual ~DIE(); 152 153 // Accessors. 154 DIEAbbrev &getAbbrev() { return Abbrev; } 155 unsigned getAbbrevNumber() const { return Abbrev.getNumber(); } 156 unsigned getTag() const { return Abbrev.getTag(); } 157 unsigned getOffset() const { return Offset; } 158 unsigned getSize() const { return Size; } 159 const std::vector<DIE *> &getChildren() const { return Children; } 160 SmallVector<DIEValue*, 32> &getValues() { return Values; } 161 CompileUnit *getAbstractCompileUnit() const { return AbstractCU; } 162 163 void setTag(unsigned Tag) { Abbrev.setTag(Tag); } 164 void setOffset(unsigned O) { Offset = O; } 165 void setSize(unsigned S) { Size = S; } 166 void setAbstractCompileUnit(CompileUnit *CU) { AbstractCU = CU; } 167 168 /// AddValue - Add a value and attributes to a DIE. 169 /// 170 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) { 171 Abbrev.AddAttribute(Attribute, Form); 172 Values.push_back(Value); 173 } 174 175 /// SiblingOffset - Return the offset of the debug information entry's 176 /// sibling. 177 unsigned SiblingOffset() const { return Offset + Size; } 178 179 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE. 180 /// 181 void AddSiblingOffset(); 182 183 /// AddChild - Add a child to the DIE. 184 /// 185 void AddChild(DIE *Child) { 186 Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes); 187 Children.push_back(Child); 188 } 189 190 /// Detach - Detaches objects connected to it after copying. 191 /// 192 void Detach() { 193 Children.clear(); 194 } 195 196 /// Profile - Used to gather unique data for the value folding set. 197 /// 198 void Profile(FoldingSetNodeID &ID) ; 199 200#ifndef NDEBUG 201 void print(std::ostream *O, unsigned IncIndent = 0) { 202 if (O) print(*O, IncIndent); 203 } 204 void print(std::ostream &O, unsigned IncIndent = 0); 205 void dump(); 206#endif 207 }; 208 209 //===--------------------------------------------------------------------===// 210 /// DIEValue - A debug information entry value. 211 /// 212 class VISIBILITY_HIDDEN DIEValue : public FoldingSetNode { 213 public: 214 enum { 215 isInteger, 216 isString, 217 isLabel, 218 isAsIsLabel, 219 isSectionOffset, 220 isDelta, 221 isEntry, 222 isBlock 223 }; 224 protected: 225 /// Type - Type of data stored in the value. 226 /// 227 unsigned Type; 228 public: 229 explicit DIEValue(unsigned T) : Type(T) {} 230 virtual ~DIEValue() {} 231 232 // Accessors 233 unsigned getType() const { return Type; } 234 235 /// EmitValue - Emit value via the Dwarf writer. 236 /// 237 virtual void EmitValue(Dwarf *D, unsigned Form) const = 0; 238 239 /// SizeOf - Return the size of a value in bytes. 240 /// 241 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const = 0; 242 243 /// Profile - Used to gather unique data for the value folding set. 244 /// 245 virtual void Profile(FoldingSetNodeID &ID) = 0; 246 247 // Implement isa/cast/dyncast. 248 static bool classof(const DIEValue *) { return true; } 249 250#ifndef NDEBUG 251 void print(std::ostream *O) { 252 if (O) print(*O); 253 } 254 virtual void print(std::ostream &O) = 0; 255 void dump(); 256#endif 257 }; 258 259 //===--------------------------------------------------------------------===// 260 /// DIEInteger - An integer value DIE. 261 /// 262 class VISIBILITY_HIDDEN DIEInteger : public DIEValue { 263 uint64_t Integer; 264 public: 265 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {} 266 267 /// BestForm - Choose the best form for integer. 268 /// 269 static unsigned BestForm(bool IsSigned, uint64_t Int) { 270 if (IsSigned) { 271 if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1; 272 if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2; 273 if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4; 274 } else { 275 if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1; 276 if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2; 277 if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4; 278 } 279 return dwarf::DW_FORM_data8; 280 } 281 282 /// EmitValue - Emit integer of appropriate size. 283 /// 284 virtual void EmitValue(Dwarf *D, unsigned Form) const; 285 286 /// SizeOf - Determine size of integer value in bytes. 287 /// 288 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const; 289 290 /// Profile - Used to gather unique data for the value folding set. 291 /// 292 static void Profile(FoldingSetNodeID &ID, unsigned Int); 293 virtual void Profile(FoldingSetNodeID &ID); 294 295 // Implement isa/cast/dyncast. 296 static bool classof(const DIEInteger *) { return true; } 297 static bool classof(const DIEValue *I) { return I->getType() == isInteger; } 298 299#ifndef NDEBUG 300 virtual void print(std::ostream &O); 301#endif 302 }; 303 304 //===--------------------------------------------------------------------===// 305 /// DIEString - A string value DIE. 306 /// 307 class VISIBILITY_HIDDEN DIEString : public DIEValue { 308 const std::string Str; 309 public: 310 explicit DIEString(const std::string &S) : DIEValue(isString), Str(S) {} 311 312 /// EmitValue - Emit string value. 313 /// 314 virtual void EmitValue(Dwarf *D, unsigned Form) const; 315 316 /// SizeOf - Determine size of string value in bytes. 317 /// 318 virtual unsigned SizeOf(const TargetData *, unsigned /*Form*/) const { 319 return Str.size() + sizeof(char); // sizeof('\0'); 320 } 321 322 /// Profile - Used to gather unique data for the value folding set. 323 /// 324 static void Profile(FoldingSetNodeID &ID, const std::string &Str); 325 virtual void Profile(FoldingSetNodeID &ID); 326 327 // Implement isa/cast/dyncast. 328 static bool classof(const DIEString *) { return true; } 329 static bool classof(const DIEValue *S) { return S->getType() == isString; } 330 331#ifndef NDEBUG 332 virtual void print(std::ostream &O); 333#endif 334 }; 335 336 //===--------------------------------------------------------------------===// 337 /// DIEDwarfLabel - A Dwarf internal label expression DIE. 338 // 339 class VISIBILITY_HIDDEN DIEDwarfLabel : public DIEValue { 340 const DWLabel Label; 341 public: 342 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {} 343 344 /// EmitValue - Emit label value. 345 /// 346 virtual void EmitValue(Dwarf *D, unsigned Form) const; 347 348 /// SizeOf - Determine size of label value in bytes. 349 /// 350 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const; 351 352 /// Profile - Used to gather unique data for the value folding set. 353 /// 354 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label); 355 virtual void Profile(FoldingSetNodeID &ID); 356 357 // Implement isa/cast/dyncast. 358 static bool classof(const DIEDwarfLabel *) { return true; } 359 static bool classof(const DIEValue *L) { return L->getType() == isLabel; } 360 361#ifndef NDEBUG 362 virtual void print(std::ostream &O); 363#endif 364 }; 365 366 //===--------------------------------------------------------------------===// 367 /// DIEObjectLabel - A label to an object in code or data. 368 // 369 class VISIBILITY_HIDDEN DIEObjectLabel : public DIEValue { 370 const std::string Label; 371 public: 372 explicit DIEObjectLabel(const std::string &L) 373 : DIEValue(isAsIsLabel), Label(L) {} 374 375 /// EmitValue - Emit label value. 376 /// 377 virtual void EmitValue(Dwarf *D, unsigned Form) const; 378 379 /// SizeOf - Determine size of label value in bytes. 380 /// 381 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const; 382 383 /// Profile - Used to gather unique data for the value folding set. 384 /// 385 static void Profile(FoldingSetNodeID &ID, const std::string &Label); 386 virtual void Profile(FoldingSetNodeID &ID); 387 388 // Implement isa/cast/dyncast. 389 static bool classof(const DIEObjectLabel *) { return true; } 390 static bool classof(const DIEValue *L) { 391 return L->getType() == isAsIsLabel; 392 } 393 394#ifndef NDEBUG 395 virtual void print(std::ostream &O); 396#endif 397 }; 398 399 //===--------------------------------------------------------------------===// 400 /// DIESectionOffset - A section offset DIE. 401 /// 402 class VISIBILITY_HIDDEN DIESectionOffset : public DIEValue { 403 const DWLabel Label; 404 const DWLabel Section; 405 bool IsEH : 1; 406 bool UseSet : 1; 407 public: 408 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec, 409 bool isEH = false, bool useSet = true) 410 : DIEValue(isSectionOffset), Label(Lab), Section(Sec), 411 IsEH(isEH), UseSet(useSet) {} 412 413 /// EmitValue - Emit section offset. 414 /// 415 virtual void EmitValue(Dwarf *D, unsigned Form) const; 416 417 /// SizeOf - Determine size of section offset value in bytes. 418 /// 419 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const; 420 421 /// Profile - Used to gather unique data for the value folding set. 422 /// 423 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label, 424 const DWLabel &Section); 425 virtual void Profile(FoldingSetNodeID &ID); 426 427 // Implement isa/cast/dyncast. 428 static bool classof(const DIESectionOffset *) { return true; } 429 static bool classof(const DIEValue *D) { 430 return D->getType() == isSectionOffset; 431 } 432 433#ifndef NDEBUG 434 virtual void print(std::ostream &O); 435#endif 436 }; 437 438 //===--------------------------------------------------------------------===// 439 /// DIEDelta - A simple label difference DIE. 440 /// 441 class VISIBILITY_HIDDEN DIEDelta : public DIEValue { 442 const DWLabel LabelHi; 443 const DWLabel LabelLo; 444 public: 445 DIEDelta(const DWLabel &Hi, const DWLabel &Lo) 446 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {} 447 448 /// EmitValue - Emit delta value. 449 /// 450 virtual void EmitValue(Dwarf *D, unsigned Form) const; 451 452 /// SizeOf - Determine size of delta value in bytes. 453 /// 454 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const; 455 456 /// Profile - Used to gather unique data for the value folding set. 457 /// 458 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi, 459 const DWLabel &LabelLo); 460 virtual void Profile(FoldingSetNodeID &ID); 461 462 // Implement isa/cast/dyncast. 463 static bool classof(const DIEDelta *) { return true; } 464 static bool classof(const DIEValue *D) { return D->getType() == isDelta; } 465 466#ifndef NDEBUG 467 virtual void print(std::ostream &O); 468#endif 469 }; 470 471 //===--------------------------------------------------------------------===// 472 /// DIEntry - A pointer to another debug information entry. An instance of 473 /// this class can also be used as a proxy for a debug information entry not 474 /// yet defined (ie. types.) 475 class VISIBILITY_HIDDEN DIEEntry : public DIEValue { 476 DIE *Entry; 477 public: 478 explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {} 479 480 DIE *getEntry() const { return Entry; } 481 void setEntry(DIE *E) { Entry = E; } 482 483 /// EmitValue - Emit debug information entry offset. 484 /// 485 virtual void EmitValue(Dwarf *D, unsigned Form) const; 486 487 /// SizeOf - Determine size of debug information entry in bytes. 488 /// 489 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const { 490 return sizeof(int32_t); 491 } 492 493 /// Profile - Used to gather unique data for the value folding set. 494 /// 495 static void Profile(FoldingSetNodeID &ID, DIE *Entry); 496 virtual void Profile(FoldingSetNodeID &ID); 497 498 // Implement isa/cast/dyncast. 499 static bool classof(const DIEEntry *) { return true; } 500 static bool classof(const DIEValue *E) { return E->getType() == isEntry; } 501 502#ifndef NDEBUG 503 virtual void print(std::ostream &O); 504#endif 505 }; 506 507 //===--------------------------------------------------------------------===// 508 /// DIEBlock - A block of values. Primarily used for location expressions. 509 // 510 class VISIBILITY_HIDDEN DIEBlock : public DIEValue, public DIE { 511 unsigned Size; // Size in bytes excluding size header. 512 public: 513 DIEBlock() 514 : DIEValue(isBlock), DIE(0), Size(0) {} 515 virtual ~DIEBlock() {} 516 517 /// ComputeSize - calculate the size of the block. 518 /// 519 unsigned ComputeSize(const TargetData *TD); 520 521 /// BestForm - Choose the best form for data. 522 /// 523 unsigned BestForm() const { 524 if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1; 525 if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2; 526 if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4; 527 return dwarf::DW_FORM_block; 528 } 529 530 /// EmitValue - Emit block data. 531 /// 532 virtual void EmitValue(Dwarf *D, unsigned Form) const; 533 534 /// SizeOf - Determine size of block data in bytes. 535 /// 536 virtual unsigned SizeOf(const TargetData *TD, unsigned Form) const; 537 538 /// Profile - Used to gather unique data for the value folding set. 539 /// 540 virtual void Profile(FoldingSetNodeID &ID); 541 542 // Implement isa/cast/dyncast. 543 static bool classof(const DIEBlock *) { return true; } 544 static bool classof(const DIEValue *E) { return E->getType() == isBlock; } 545 546#ifndef NDEBUG 547 virtual void print(std::ostream &O); 548#endif 549 }; 550 551} // end llvm namespace 552 553#endif 554