1 //===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===// 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 contains support for constructing a dwarf compile unit. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #define DEBUG_TYPE "dwarfdebug" 15 16 #include "DwarfCompileUnit.h" 17 #include "DwarfAccelTable.h" 18 #include "DwarfDebug.h" 19 #include "llvm/ADT/APFloat.h" 20 #include "llvm/DIBuilder.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/DataLayout.h" 23 #include "llvm/IR/GlobalVariable.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/Target/Mangler.h" 26 #include "llvm/Target/TargetFrameLowering.h" 27 #include "llvm/Target/TargetMachine.h" 28 #include "llvm/Target/TargetLoweringObjectFile.h" 29 #include "llvm/Target/TargetRegisterInfo.h" 30 31 using namespace llvm; 32 33 /// CompileUnit - Compile unit constructor. 34 CompileUnit::CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A, 35 DwarfDebug *DW, DwarfUnits *DWU) 36 : UniqueID(UID), Node(N), CUDie(D), Asm(A), DD(DW), DU(DWU), IndexTyDie(0), 37 DebugInfoOffset(0) { 38 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); 39 insertDIE(N, D); 40 } 41 42 /// ~CompileUnit - Destructor for compile unit. 43 CompileUnit::~CompileUnit() { 44 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) 45 DIEBlocks[j]->~DIEBlock(); 46 } 47 48 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 49 /// information entry. 50 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) { 51 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); 52 return Value; 53 } 54 55 /// getDefaultLowerBound - Return the default lower bound for an array. If the 56 /// DWARF version doesn't handle the language, return -1. 57 int64_t CompileUnit::getDefaultLowerBound() const { 58 switch (DICompileUnit(Node).getLanguage()) { 59 default: 60 break; 61 62 case dwarf::DW_LANG_C89: 63 case dwarf::DW_LANG_C99: 64 case dwarf::DW_LANG_C: 65 case dwarf::DW_LANG_C_plus_plus: 66 case dwarf::DW_LANG_ObjC: 67 case dwarf::DW_LANG_ObjC_plus_plus: 68 return 0; 69 70 case dwarf::DW_LANG_Fortran77: 71 case dwarf::DW_LANG_Fortran90: 72 case dwarf::DW_LANG_Fortran95: 73 return 1; 74 75 // The languages below have valid values only if the DWARF version >= 4. 76 case dwarf::DW_LANG_Java: 77 case dwarf::DW_LANG_Python: 78 case dwarf::DW_LANG_UPC: 79 case dwarf::DW_LANG_D: 80 if (dwarf::DWARF_VERSION >= 4) 81 return 0; 82 break; 83 84 case dwarf::DW_LANG_Ada83: 85 case dwarf::DW_LANG_Ada95: 86 case dwarf::DW_LANG_Cobol74: 87 case dwarf::DW_LANG_Cobol85: 88 case dwarf::DW_LANG_Modula2: 89 case dwarf::DW_LANG_Pascal83: 90 case dwarf::DW_LANG_PLI: 91 if (dwarf::DWARF_VERSION >= 4) 92 return 1; 93 break; 94 } 95 96 return -1; 97 } 98 99 /// addFlag - Add a flag that is true. 100 void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) { 101 if (DD->getDwarfVersion() >= 4) 102 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne); 103 else 104 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne); 105 } 106 107 /// addUInt - Add an unsigned integer attribute data and value. 108 /// 109 void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute, 110 Optional<dwarf::Form> Form, uint64_t Integer) { 111 if (!Form) 112 Form = DIEInteger::BestForm(false, Integer); 113 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator) 114 DIEInteger(Integer); 115 Die->addValue(Attribute, *Form, Value); 116 } 117 118 void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) { 119 addUInt(Block, (dwarf::Attribute)0, Form, Integer); 120 } 121 122 /// addSInt - Add an signed integer attribute data and value. 123 /// 124 void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute, 125 Optional<dwarf::Form> Form, int64_t Integer) { 126 if (!Form) 127 Form = DIEInteger::BestForm(true, Integer); 128 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); 129 Die->addValue(Attribute, *Form, Value); 130 } 131 132 void CompileUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, 133 int64_t Integer) { 134 addSInt(Die, (dwarf::Attribute)0, Form, Integer); 135 } 136 137 /// addString - Add a string attribute data and value. We always emit a 138 /// reference to the string pool instead of immediate strings so that DIEs have 139 /// more predictable sizes. In the case of split dwarf we emit an index 140 /// into another table which gets us the static offset into the string 141 /// table. 142 void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute, StringRef String) { 143 DIEValue *Value; 144 dwarf::Form Form; 145 if (!DD->useSplitDwarf()) { 146 MCSymbol *Symb = DU->getStringPoolEntry(String); 147 if (Asm->needsRelocationsForDwarfStringPool()) 148 Value = new (DIEValueAllocator) DIELabel(Symb); 149 else { 150 MCSymbol *StringPool = DU->getStringPoolSym(); 151 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); 152 } 153 Form = dwarf::DW_FORM_strp; 154 } else { 155 unsigned idx = DU->getStringPoolIndex(String); 156 Value = new (DIEValueAllocator) DIEInteger(idx); 157 Form = dwarf::DW_FORM_GNU_str_index; 158 } 159 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); 160 Die->addValue(Attribute, Form, Str); 161 } 162 163 /// addLocalString - Add a string attribute data and value. This is guaranteed 164 /// to be in the local string pool instead of indirected. 165 void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute, 166 StringRef String) { 167 MCSymbol *Symb = DU->getStringPoolEntry(String); 168 DIEValue *Value; 169 if (Asm->needsRelocationsForDwarfStringPool()) 170 Value = new (DIEValueAllocator) DIELabel(Symb); 171 else { 172 MCSymbol *StringPool = DU->getStringPoolSym(); 173 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); 174 } 175 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value); 176 } 177 178 /// addExpr - Add a Dwarf expression attribute data and value. 179 /// 180 void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) { 181 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr); 182 Die->addValue((dwarf::Attribute)0, Form, Value); 183 } 184 185 /// addLabel - Add a Dwarf label attribute data and value. 186 /// 187 void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, 188 const MCSymbol *Label) { 189 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); 190 Die->addValue(Attribute, Form, Value); 191 } 192 193 void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form, 194 const MCSymbol *Label) { 195 addLabel(Die, (dwarf::Attribute)0, Form, Label); 196 } 197 198 /// addLabelAddress - Add a dwarf label attribute data and value using 199 /// DW_FORM_addr or DW_FORM_GNU_addr_index. 200 /// 201 void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute, 202 MCSymbol *Label) { 203 if (Label) 204 DD->addArangeLabel(SymbolCU(this, Label)); 205 206 if (!DD->useSplitDwarf()) { 207 if (Label != NULL) { 208 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); 209 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); 210 } else { 211 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0); 212 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); 213 } 214 } else { 215 unsigned idx = DU->getAddrPoolIndex(Label); 216 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); 217 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); 218 } 219 } 220 221 /// addOpAddress - Add a dwarf op address data and value using the 222 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. 223 /// 224 void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) { 225 DD->addArangeLabel(SymbolCU(this, Sym)); 226 if (!DD->useSplitDwarf()) { 227 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 228 addLabel(Die, dwarf::DW_FORM_udata, Sym); 229 } else { 230 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); 231 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym)); 232 } 233 } 234 235 /// addDelta - Add a label delta attribute data and value. 236 /// 237 void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, 238 const MCSymbol *Hi, const MCSymbol *Lo) { 239 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 240 Die->addValue(Attribute, Form, Value); 241 } 242 243 /// addDIEEntry - Add a DIE attribute data and value. 244 /// 245 void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) { 246 // We currently only use ref4. 247 Die->addValue(Attribute, dwarf::DW_FORM_ref4, createDIEEntry(Entry)); 248 } 249 250 /// Create a DIE with the given Tag, add the DIE to its parent, and 251 /// call insertDIE if MD is not null. 252 DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const MDNode *MD) { 253 DIE *Die = new DIE(Tag); 254 Parent.addChild(Die); 255 if (MD) 256 insertDIE(MD, Die); 257 return Die; 258 } 259 260 /// addBlock - Add block data. 261 /// 262 void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, 263 DIEBlock *Block) { 264 Block->ComputeSize(Asm); 265 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. 266 Die->addValue(Attribute, Block->BestForm(), Block); 267 } 268 269 /// addSourceLine - Add location information to specified debug information 270 /// entry. 271 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) { 272 // Verify variable. 273 if (!V.isVariable()) 274 return; 275 276 unsigned Line = V.getLineNumber(); 277 if (Line == 0) 278 return; 279 unsigned FileID = 280 DD->getOrCreateSourceID(V.getContext().getFilename(), 281 V.getContext().getDirectory(), getUniqueID()); 282 assert(FileID && "Invalid file id"); 283 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 284 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 285 } 286 287 /// addSourceLine - Add location information to specified debug information 288 /// entry. 289 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { 290 // Verify global variable. 291 if (!G.isGlobalVariable()) 292 return; 293 294 unsigned Line = G.getLineNumber(); 295 if (Line == 0) 296 return; 297 unsigned FileID = 298 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID()); 299 assert(FileID && "Invalid file id"); 300 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 301 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 302 } 303 304 /// addSourceLine - Add location information to specified debug information 305 /// entry. 306 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) { 307 // Verify subprogram. 308 if (!SP.isSubprogram()) 309 return; 310 311 // If the line number is 0, don't add it. 312 unsigned Line = SP.getLineNumber(); 313 if (Line == 0) 314 return; 315 316 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(), 317 getUniqueID()); 318 assert(FileID && "Invalid file id"); 319 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 320 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 321 } 322 323 /// addSourceLine - Add location information to specified debug information 324 /// entry. 325 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) { 326 // Verify type. 327 if (!Ty.isType()) 328 return; 329 330 unsigned Line = Ty.getLineNumber(); 331 if (Line == 0) 332 return; 333 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(), 334 getUniqueID()); 335 assert(FileID && "Invalid file id"); 336 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 337 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 338 } 339 340 /// addSourceLine - Add location information to specified debug information 341 /// entry. 342 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) { 343 // Verify type. 344 if (!Ty.isObjCProperty()) 345 return; 346 347 unsigned Line = Ty.getLineNumber(); 348 if (Line == 0) 349 return; 350 DIFile File = Ty.getFile(); 351 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(), 352 File.getDirectory(), getUniqueID()); 353 assert(FileID && "Invalid file id"); 354 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 355 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 356 } 357 358 /// addSourceLine - Add location information to specified debug information 359 /// entry. 360 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) { 361 // Verify namespace. 362 if (!NS.Verify()) 363 return; 364 365 unsigned Line = NS.getLineNumber(); 366 if (Line == 0) 367 return; 368 StringRef FN = NS.getFilename(); 369 370 unsigned FileID = 371 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID()); 372 assert(FileID && "Invalid file id"); 373 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 374 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 375 } 376 377 /// addVariableAddress - Add DW_AT_location attribute for a 378 /// DbgVariable based on provided MachineLocation. 379 void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die, 380 MachineLocation Location) { 381 if (DV.variableHasComplexAddress()) 382 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 383 else if (DV.isBlockByrefVariable()) 384 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 385 else 386 addAddress(Die, dwarf::DW_AT_location, Location, 387 DV.getVariable().isIndirect()); 388 } 389 390 /// addRegisterOp - Add register operand. 391 void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) { 392 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 393 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 394 if (DWReg < 32) 395 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); 396 else { 397 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 398 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); 399 } 400 } 401 402 /// addRegisterOffset - Add register offset. 403 void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg, 404 int64_t Offset) { 405 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 406 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 407 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); 408 if (Reg == TRI->getFrameRegister(*Asm->MF)) 409 // If variable offset is based in frame register then use fbreg. 410 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); 411 else if (DWReg < 32) 412 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); 413 else { 414 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 415 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); 416 } 417 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset); 418 } 419 420 /// addAddress - Add an address attribute to a die based on the location 421 /// provided. 422 void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute, 423 const MachineLocation &Location, bool Indirect) { 424 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 425 426 if (Location.isReg() && !Indirect) 427 addRegisterOp(Block, Location.getReg()); 428 else { 429 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 430 if (Indirect && !Location.isReg()) { 431 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 432 } 433 } 434 435 // Now attach the location information to the DIE. 436 addBlock(Die, Attribute, Block); 437 } 438 439 /// addComplexAddress - Start with the address based on the location provided, 440 /// and generate the DWARF information necessary to find the actual variable 441 /// given the extra address information encoded in the DIVariable, starting from 442 /// the starting location. Add the DWARF information to the die. 443 /// 444 void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die, 445 dwarf::Attribute Attribute, 446 const MachineLocation &Location) { 447 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 448 unsigned N = DV.getNumAddrElements(); 449 unsigned i = 0; 450 if (Location.isReg()) { 451 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { 452 // If first address element is OpPlus then emit 453 // DW_OP_breg + Offset instead of DW_OP_reg + Offset. 454 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1)); 455 i = 2; 456 } else 457 addRegisterOp(Block, Location.getReg()); 458 } else 459 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 460 461 for (; i < N; ++i) { 462 uint64_t Element = DV.getAddrElement(i); 463 if (Element == DIBuilder::OpPlus) { 464 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 465 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i)); 466 } else if (Element == DIBuilder::OpDeref) { 467 if (!Location.isReg()) 468 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 469 } else 470 llvm_unreachable("unknown DIBuilder Opcode"); 471 } 472 473 // Now attach the location information to the DIE. 474 addBlock(Die, Attribute, Block); 475 } 476 477 /* Byref variables, in Blocks, are declared by the programmer as "SomeType 478 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and 479 gives the variable VarName either the struct, or a pointer to the struct, as 480 its type. This is necessary for various behind-the-scenes things the 481 compiler needs to do with by-reference variables in Blocks. 482 483 However, as far as the original *programmer* is concerned, the variable 484 should still have type 'SomeType', as originally declared. 485 486 The function getBlockByrefType dives into the __Block_byref_x_VarName 487 struct to find the original type of the variable, which is then assigned to 488 the variable's Debug Information Entry as its real type. So far, so good. 489 However now the debugger will expect the variable VarName to have the type 490 SomeType. So we need the location attribute for the variable to be an 491 expression that explains to the debugger how to navigate through the 492 pointers and struct to find the actual variable of type SomeType. 493 494 The following function does just that. We start by getting 495 the "normal" location for the variable. This will be the location 496 of either the struct __Block_byref_x_VarName or the pointer to the 497 struct __Block_byref_x_VarName. 498 499 The struct will look something like: 500 501 struct __Block_byref_x_VarName { 502 ... <various fields> 503 struct __Block_byref_x_VarName *forwarding; 504 ... <various other fields> 505 SomeType VarName; 506 ... <maybe more fields> 507 }; 508 509 If we are given the struct directly (as our starting point) we 510 need to tell the debugger to: 511 512 1). Add the offset of the forwarding field. 513 514 2). Follow that pointer to get the real __Block_byref_x_VarName 515 struct to use (the real one may have been copied onto the heap). 516 517 3). Add the offset for the field VarName, to find the actual variable. 518 519 If we started with a pointer to the struct, then we need to 520 dereference that pointer first, before the other steps. 521 Translating this into DWARF ops, we will need to append the following 522 to the current location description for the variable: 523 524 DW_OP_deref -- optional, if we start with a pointer 525 DW_OP_plus_uconst <forward_fld_offset> 526 DW_OP_deref 527 DW_OP_plus_uconst <varName_fld_offset> 528 529 That is what this function does. */ 530 531 /// addBlockByrefAddress - Start with the address based on the location 532 /// provided, and generate the DWARF information necessary to find the 533 /// actual Block variable (navigating the Block struct) based on the 534 /// starting location. Add the DWARF information to the die. For 535 /// more information, read large comment just above here. 536 /// 537 void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die, 538 dwarf::Attribute Attribute, 539 const MachineLocation &Location) { 540 DIType Ty = DV.getType(); 541 DIType TmpTy = Ty; 542 uint16_t Tag = Ty.getTag(); 543 bool isPointer = false; 544 545 StringRef varName = DV.getName(); 546 547 if (Tag == dwarf::DW_TAG_pointer_type) { 548 DIDerivedType DTy = DIDerivedType(Ty); 549 TmpTy = resolve(DTy.getTypeDerivedFrom()); 550 isPointer = true; 551 } 552 553 DICompositeType blockStruct = DICompositeType(TmpTy); 554 555 // Find the __forwarding field and the variable field in the __Block_byref 556 // struct. 557 DIArray Fields = blockStruct.getTypeArray(); 558 DIDescriptor varField = DIDescriptor(); 559 DIDescriptor forwardingField = DIDescriptor(); 560 561 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { 562 DIDescriptor Element = Fields.getElement(i); 563 DIDerivedType DT = DIDerivedType(Element); 564 StringRef fieldName = DT.getName(); 565 if (fieldName == "__forwarding") 566 forwardingField = Element; 567 else if (fieldName == varName) 568 varField = Element; 569 } 570 571 // Get the offsets for the forwarding field and the variable field. 572 unsigned forwardingFieldOffset = 573 DIDerivedType(forwardingField).getOffsetInBits() >> 3; 574 unsigned varFieldOffset = DIDerivedType(varField).getOffsetInBits() >> 3; 575 576 // Decode the original location, and use that as the start of the byref 577 // variable's location. 578 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 579 580 if (Location.isReg()) 581 addRegisterOp(Block, Location.getReg()); 582 else 583 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 584 585 // If we started with a pointer to the __Block_byref... struct, then 586 // the first thing we need to do is dereference the pointer (DW_OP_deref). 587 if (isPointer) 588 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 589 590 // Next add the offset for the '__forwarding' field: 591 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in 592 // adding the offset if it's 0. 593 if (forwardingFieldOffset > 0) { 594 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 595 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset); 596 } 597 598 // Now dereference the __forwarding field to get to the real __Block_byref 599 // struct: DW_OP_deref. 600 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 601 602 // Now that we've got the real __Block_byref... struct, add the offset 603 // for the variable's field to get to the location of the actual variable: 604 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. 605 if (varFieldOffset > 0) { 606 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 607 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset); 608 } 609 610 // Now attach the location information to the DIE. 611 addBlock(Die, Attribute, Block); 612 } 613 614 /// isTypeSigned - Return true if the type is signed. 615 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) { 616 if (Ty.isDerivedType()) 617 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()), 618 SizeInBits); 619 if (Ty.isBasicType()) 620 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed || 621 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { 622 *SizeInBits = Ty.getSizeInBits(); 623 return true; 624 } 625 return false; 626 } 627 628 /// Return true if type encoding is unsigned. 629 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) { 630 DIDerivedType DTy(Ty); 631 if (DTy.isDerivedType()) 632 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom())); 633 634 DIBasicType BTy(Ty); 635 if (BTy.isBasicType()) { 636 unsigned Encoding = BTy.getEncoding(); 637 if (Encoding == dwarf::DW_ATE_unsigned || 638 Encoding == dwarf::DW_ATE_unsigned_char || 639 Encoding == dwarf::DW_ATE_boolean) 640 return true; 641 } 642 return false; 643 } 644 645 /// If this type is derived from a base type then return base type size. 646 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) { 647 unsigned Tag = Ty.getTag(); 648 649 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 650 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 651 Tag != dwarf::DW_TAG_restrict_type) 652 return Ty.getSizeInBits(); 653 654 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom()); 655 656 // If this type is not derived from any type then take conservative approach. 657 if (!BaseType.isValid()) 658 return Ty.getSizeInBits(); 659 660 // If this is a derived type, go ahead and get the base type, unless it's a 661 // reference then it's just the size of the field. Pointer types have no need 662 // of this since they're a different type of qualification on the type. 663 if (BaseType.getTag() == dwarf::DW_TAG_reference_type || 664 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) 665 return Ty.getSizeInBits(); 666 667 if (BaseType.isDerivedType()) 668 return getBaseTypeSize(DD, DIDerivedType(BaseType)); 669 670 return BaseType.getSizeInBits(); 671 } 672 673 /// addConstantValue - Add constant value entry in variable DIE. 674 void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO, 675 DIType Ty) { 676 // FIXME: This is a bit conservative/simple - it emits negative values at 677 // their maximum bit width which is a bit unfortunate (& doesn't prefer 678 // udata/sdata over dataN as suggested by the DWARF spec) 679 assert(MO.isImm() && "Invalid machine operand!"); 680 int SizeInBits = -1; 681 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits); 682 dwarf::Form Form; 683 684 // If we're a signed constant definitely use sdata. 685 if (SignedConstant) { 686 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm()); 687 return; 688 } 689 690 // Else use data for now unless it's larger than we can deal with. 691 switch (SizeInBits) { 692 case 8: 693 Form = dwarf::DW_FORM_data1; 694 break; 695 case 16: 696 Form = dwarf::DW_FORM_data2; 697 break; 698 case 32: 699 Form = dwarf::DW_FORM_data4; 700 break; 701 case 64: 702 Form = dwarf::DW_FORM_data8; 703 break; 704 default: 705 Form = dwarf::DW_FORM_udata; 706 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); 707 return; 708 } 709 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); 710 } 711 712 /// addConstantFPValue - Add constant value entry in variable DIE. 713 void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { 714 assert(MO.isFPImm() && "Invalid machine operand!"); 715 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 716 APFloat FPImm = MO.getFPImm()->getValueAPF(); 717 718 // Get the raw data form of the floating point. 719 const APInt FltVal = FPImm.bitcastToAPInt(); 720 const char *FltPtr = (const char *)FltVal.getRawData(); 721 722 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 723 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 724 int Incr = (LittleEndian ? 1 : -1); 725 int Start = (LittleEndian ? 0 : NumBytes - 1); 726 int Stop = (LittleEndian ? NumBytes : -1); 727 728 // Output the constant to DWARF one byte at a time. 729 for (; Start != Stop; Start += Incr) 730 addUInt(Block, dwarf::DW_FORM_data1, 731 (unsigned char)0xFF & FltPtr[Start]); 732 733 addBlock(Die, dwarf::DW_AT_const_value, Block); 734 } 735 736 /// addConstantFPValue - Add constant value entry in variable DIE. 737 void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) { 738 // Pass this down to addConstantValue as an unsigned bag of bits. 739 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); 740 } 741 742 /// addConstantValue - Add constant value entry in variable DIE. 743 void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI, 744 bool Unsigned) { 745 addConstantValue(Die, CI->getValue(), Unsigned); 746 } 747 748 // addConstantValue - Add constant value entry in variable DIE. 749 void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) { 750 unsigned CIBitWidth = Val.getBitWidth(); 751 if (CIBitWidth <= 64) { 752 // If we're a signed constant definitely use sdata. 753 if (!Unsigned) { 754 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, 755 Val.getSExtValue()); 756 return; 757 } 758 759 // Else use data for now unless it's larger than we can deal with. 760 dwarf::Form Form; 761 switch (CIBitWidth) { 762 case 8: 763 Form = dwarf::DW_FORM_data1; 764 break; 765 case 16: 766 Form = dwarf::DW_FORM_data2; 767 break; 768 case 32: 769 Form = dwarf::DW_FORM_data4; 770 break; 771 case 64: 772 Form = dwarf::DW_FORM_data8; 773 break; 774 default: 775 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, 776 Val.getZExtValue()); 777 return; 778 } 779 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue()); 780 return; 781 } 782 783 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 784 785 // Get the raw data form of the large APInt. 786 const uint64_t *Ptr64 = Val.getRawData(); 787 788 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 789 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 790 791 // Output the constant to DWARF one byte at a time. 792 for (int i = 0; i < NumBytes; i++) { 793 uint8_t c; 794 if (LittleEndian) 795 c = Ptr64[i / 8] >> (8 * (i & 7)); 796 else 797 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); 798 addUInt(Block, dwarf::DW_FORM_data1, c); 799 } 800 801 addBlock(Die, dwarf::DW_AT_const_value, Block); 802 } 803 804 /// addTemplateParams - Add template parameters into buffer. 805 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { 806 // Add template parameters. 807 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { 808 DIDescriptor Element = TParams.getElement(i); 809 if (Element.isTemplateTypeParameter()) 810 constructTemplateTypeParameterDIE(Buffer, 811 DITemplateTypeParameter(Element)); 812 else if (Element.isTemplateValueParameter()) 813 constructTemplateValueParameterDIE(Buffer, 814 DITemplateValueParameter(Element)); 815 } 816 } 817 818 /// getOrCreateContextDIE - Get context owner's DIE. 819 DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) { 820 if (Context.isType()) 821 return getOrCreateTypeDIE(DIType(Context)); 822 else if (Context.isNameSpace()) 823 return getOrCreateNameSpace(DINameSpace(Context)); 824 else if (Context.isSubprogram()) 825 return getOrCreateSubprogramDIE(DISubprogram(Context)); 826 else 827 return getDIE(Context); 828 } 829 830 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 831 /// given DIType. 832 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) { 833 DIType Ty(TyNode); 834 if (!Ty.isType()) 835 return NULL; 836 837 // Construct the context before querying for the existence of the DIE in case 838 // such construction creates the DIE. 839 DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext())); 840 if (!ContextDIE) 841 ContextDIE = CUDie.get(); 842 843 DIE *TyDIE = getDIE(Ty); 844 if (TyDIE) 845 return TyDIE; 846 847 // Create new type. 848 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); 849 850 if (Ty.isBasicType()) 851 constructTypeDIE(*TyDIE, DIBasicType(Ty)); 852 else if (Ty.isCompositeType()) 853 constructTypeDIE(*TyDIE, DICompositeType(Ty)); 854 else { 855 assert(Ty.isDerivedType() && "Unknown kind of DIType"); 856 constructTypeDIE(*TyDIE, DIDerivedType(Ty)); 857 } 858 // If this is a named finished type then include it in the list of types 859 // for the accelerator tables. 860 if (!Ty.getName().empty() && !Ty.isForwardDecl()) { 861 bool IsImplementation = 0; 862 if (Ty.isCompositeType()) { 863 DICompositeType CT(Ty); 864 // A runtime language of 0 actually means C/C++ and that any 865 // non-negative value is some version of Objective-C/C++. 866 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete(); 867 } 868 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; 869 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags)); 870 } 871 872 return TyDIE; 873 } 874 875 /// addType - Add a new type attribute to the specified entity. 876 void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) { 877 assert(Ty && "Trying to add a type that doesn't exist?"); 878 879 // Check for pre-existence. 880 DIEEntry *Entry = getDIEEntry(Ty); 881 // If it exists then use the existing value. 882 if (Entry) { 883 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry); 884 return; 885 } 886 887 // Construct type. 888 DIE *Buffer = getOrCreateTypeDIE(Ty); 889 890 // Set up proxy. 891 Entry = createDIEEntry(Buffer); 892 insertDIEEntry(Ty, Entry); 893 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry); 894 895 // If this is a complete composite type then include it in the 896 // list of global types. 897 addGlobalType(Ty); 898 } 899 900 // Accelerator table mutators - add each name along with its companion 901 // DIE to the proper table while ensuring that the name that we're going 902 // to reference is in the string table. We do this since the names we 903 // add may not only be identical to the names in the DIE. 904 void CompileUnit::addAccelName(StringRef Name, DIE *Die) { 905 DU->getStringPoolEntry(Name); 906 std::vector<DIE *> &DIEs = AccelNames[Name]; 907 DIEs.push_back(Die); 908 } 909 910 void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) { 911 DU->getStringPoolEntry(Name); 912 std::vector<DIE *> &DIEs = AccelObjC[Name]; 913 DIEs.push_back(Die); 914 } 915 916 void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) { 917 DU->getStringPoolEntry(Name); 918 std::vector<DIE *> &DIEs = AccelNamespace[Name]; 919 DIEs.push_back(Die); 920 } 921 922 void CompileUnit::addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die) { 923 DU->getStringPoolEntry(Name); 924 std::vector<std::pair<DIE *, unsigned> > &DIEs = AccelTypes[Name]; 925 DIEs.push_back(Die); 926 } 927 928 /// addGlobalName - Add a new global name to the compile unit. 929 void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) { 930 std::string FullName = getParentContextString(Context) + Name.str(); 931 GlobalNames[FullName] = Die; 932 } 933 934 /// addGlobalType - Add a new global type to the compile unit. 935 /// 936 void CompileUnit::addGlobalType(DIType Ty) { 937 DIScope Context = resolve(Ty.getContext()); 938 if (!Ty.getName().empty() && !Ty.isForwardDecl() && 939 (!Context || Context.isCompileUnit() || Context.isFile() || 940 Context.isNameSpace())) 941 if (DIEEntry *Entry = getDIEEntry(Ty)) { 942 std::string FullName = 943 getParentContextString(Context) + Ty.getName().str(); 944 GlobalTypes[FullName] = Entry->getEntry(); 945 } 946 } 947 948 /// getParentContextString - Walks the metadata parent chain in a language 949 /// specific manner (using the compile unit language) and returns 950 /// it as a string. This is done at the metadata level because DIEs may 951 /// not currently have been added to the parent context and walking the 952 /// DIEs looking for names is more expensive than walking the metadata. 953 std::string CompileUnit::getParentContextString(DIScope Context) const { 954 if (!Context) 955 return ""; 956 957 // FIXME: Decide whether to implement this for non-C++ languages. 958 if (getLanguage() != dwarf::DW_LANG_C_plus_plus) 959 return ""; 960 961 std::string CS; 962 SmallVector<DIScope, 1> Parents; 963 while (!Context.isCompileUnit()) { 964 Parents.push_back(Context); 965 if (Context.getContext()) 966 Context = resolve(Context.getContext()); 967 else 968 // Structure, etc types will have a NULL context if they're at the top 969 // level. 970 break; 971 } 972 973 // Reverse iterate over our list to go from the outermost construct to the 974 // innermost. 975 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(), 976 E = Parents.rend(); 977 I != E; ++I) { 978 DIScope Ctx = *I; 979 StringRef Name = Ctx.getName(); 980 if (!Name.empty()) { 981 CS += Name; 982 CS += "::"; 983 } 984 } 985 return CS; 986 } 987 988 /// addPubTypes - Add subprogram argument types for pubtypes section. 989 void CompileUnit::addPubTypes(DISubprogram SP) { 990 DICompositeType SPTy = SP.getType(); 991 uint16_t SPTag = SPTy.getTag(); 992 if (SPTag != dwarf::DW_TAG_subroutine_type) 993 return; 994 995 DIArray Args = SPTy.getTypeArray(); 996 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { 997 DIType ATy(Args.getElement(i)); 998 if (!ATy.isType()) 999 continue; 1000 addGlobalType(ATy); 1001 } 1002 } 1003 1004 /// constructTypeDIE - Construct basic type die from DIBasicType. 1005 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { 1006 // Get core information. 1007 StringRef Name = BTy.getName(); 1008 // Add name if not anonymous or intermediate type. 1009 if (!Name.empty()) 1010 addString(&Buffer, dwarf::DW_AT_name, Name); 1011 1012 // An unspecified type only has a name attribute. 1013 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) 1014 return; 1015 1016 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1017 BTy.getEncoding()); 1018 1019 uint64_t Size = BTy.getSizeInBits() >> 3; 1020 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); 1021 } 1022 1023 /// constructTypeDIE - Construct derived type die from DIDerivedType. 1024 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { 1025 // Get core information. 1026 StringRef Name = DTy.getName(); 1027 uint64_t Size = DTy.getSizeInBits() >> 3; 1028 uint16_t Tag = Buffer.getTag(); 1029 1030 // Map to main type, void will not have a type. 1031 DIType FromTy = resolve(DTy.getTypeDerivedFrom()); 1032 if (FromTy) 1033 addType(&Buffer, FromTy); 1034 1035 // Add name if not anonymous or intermediate type. 1036 if (!Name.empty()) 1037 addString(&Buffer, dwarf::DW_AT_name, Name); 1038 1039 // Add size if non-zero (derived types might be zero-sized.) 1040 if (Size && Tag != dwarf::DW_TAG_pointer_type) 1041 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); 1042 1043 if (Tag == dwarf::DW_TAG_ptr_to_member_type) 1044 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, 1045 getOrCreateTypeDIE(resolve(DTy.getClassType()))); 1046 // Add source line info if available and TyDesc is not a forward declaration. 1047 if (!DTy.isForwardDecl()) 1048 addSourceLine(&Buffer, DTy); 1049 } 1050 1051 /// Return true if the type is appropriately scoped to be contained inside 1052 /// its own type unit. 1053 static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) { 1054 DIScope Parent = DD->resolve(Ty.getContext()); 1055 while (Parent) { 1056 // Don't generate a hash for anything scoped inside a function. 1057 if (Parent.isSubprogram()) 1058 return false; 1059 Parent = DD->resolve(Parent.getContext()); 1060 } 1061 return true; 1062 } 1063 1064 /// Return true if the type should be split out into a type unit. 1065 static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) { 1066 uint16_t Tag = CTy.getTag(); 1067 1068 switch (Tag) { 1069 case dwarf::DW_TAG_structure_type: 1070 case dwarf::DW_TAG_union_type: 1071 case dwarf::DW_TAG_enumeration_type: 1072 case dwarf::DW_TAG_class_type: 1073 // If this is a class, structure, union, or enumeration type 1074 // that is a definition (not a declaration), and not scoped 1075 // inside a function then separate this out as a type unit. 1076 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD); 1077 default: 1078 return false; 1079 } 1080 } 1081 1082 /// constructTypeDIE - Construct type DIE from DICompositeType. 1083 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { 1084 // Get core information. 1085 StringRef Name = CTy.getName(); 1086 1087 uint64_t Size = CTy.getSizeInBits() >> 3; 1088 uint16_t Tag = Buffer.getTag(); 1089 1090 switch (Tag) { 1091 case dwarf::DW_TAG_array_type: 1092 constructArrayTypeDIE(Buffer, &CTy); 1093 break; 1094 case dwarf::DW_TAG_enumeration_type: { 1095 DIArray Elements = CTy.getTypeArray(); 1096 1097 // Add enumerators to enumeration type. 1098 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1099 DIDescriptor Enum(Elements.getElement(i)); 1100 if (Enum.isEnumerator()) 1101 constructEnumTypeDIE(Buffer, DIEnumerator(Enum)); 1102 } 1103 DIType DTy = resolve(CTy.getTypeDerivedFrom()); 1104 if (DTy) { 1105 addType(&Buffer, DTy); 1106 addFlag(&Buffer, dwarf::DW_AT_enum_class); 1107 } 1108 } break; 1109 case dwarf::DW_TAG_subroutine_type: { 1110 // Add return type. A void return won't have a type. 1111 DIArray Elements = CTy.getTypeArray(); 1112 DIDescriptor RTy = Elements.getElement(0); 1113 if (RTy) 1114 addType(&Buffer, DIType(RTy)); 1115 1116 bool isPrototyped = true; 1117 // Add arguments. 1118 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { 1119 DIDescriptor Ty = Elements.getElement(i); 1120 if (Ty.isUnspecifiedParameter()) { 1121 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); 1122 isPrototyped = false; 1123 } else { 1124 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); 1125 addType(Arg, DIType(Ty)); 1126 if (DIType(Ty).isArtificial()) 1127 addFlag(Arg, dwarf::DW_AT_artificial); 1128 } 1129 } 1130 // Add prototype flag if we're dealing with a C language and the 1131 // function has been prototyped. 1132 uint16_t Language = DICompileUnit(Node).getLanguage(); 1133 if (isPrototyped && 1134 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1135 Language == dwarf::DW_LANG_ObjC)) 1136 addFlag(&Buffer, dwarf::DW_AT_prototyped); 1137 } break; 1138 case dwarf::DW_TAG_structure_type: 1139 case dwarf::DW_TAG_union_type: 1140 case dwarf::DW_TAG_class_type: { 1141 // Add elements to structure type. 1142 DIArray Elements = CTy.getTypeArray(); 1143 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1144 DIDescriptor Element = Elements.getElement(i); 1145 DIE *ElemDie = NULL; 1146 if (Element.isSubprogram()) { 1147 DISubprogram SP(Element); 1148 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element)); 1149 if (SP.isProtected()) 1150 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1151 dwarf::DW_ACCESS_protected); 1152 else if (SP.isPrivate()) 1153 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1154 dwarf::DW_ACCESS_private); 1155 else 1156 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1157 dwarf::DW_ACCESS_public); 1158 if (SP.isExplicit()) 1159 addFlag(ElemDie, dwarf::DW_AT_explicit); 1160 } else if (Element.isDerivedType()) { 1161 DIDerivedType DDTy(Element); 1162 if (DDTy.getTag() == dwarf::DW_TAG_friend) { 1163 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); 1164 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()), 1165 dwarf::DW_AT_friend); 1166 } else if (DDTy.isStaticMember()) { 1167 getOrCreateStaticMemberDIE(DDTy); 1168 } else { 1169 constructMemberDIE(Buffer, DDTy); 1170 } 1171 } else if (Element.isObjCProperty()) { 1172 DIObjCProperty Property(Element); 1173 ElemDie = createAndAddDIE(Property.getTag(), Buffer); 1174 StringRef PropertyName = Property.getObjCPropertyName(); 1175 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 1176 addType(ElemDie, Property.getType()); 1177 addSourceLine(ElemDie, Property); 1178 StringRef GetterName = Property.getObjCPropertyGetterName(); 1179 if (!GetterName.empty()) 1180 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 1181 StringRef SetterName = Property.getObjCPropertySetterName(); 1182 if (!SetterName.empty()) 1183 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 1184 unsigned PropertyAttributes = 0; 1185 if (Property.isReadOnlyObjCProperty()) 1186 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; 1187 if (Property.isReadWriteObjCProperty()) 1188 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; 1189 if (Property.isAssignObjCProperty()) 1190 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; 1191 if (Property.isRetainObjCProperty()) 1192 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; 1193 if (Property.isCopyObjCProperty()) 1194 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; 1195 if (Property.isNonAtomicObjCProperty()) 1196 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; 1197 if (PropertyAttributes) 1198 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, 1199 PropertyAttributes); 1200 1201 DIEEntry *Entry = getDIEEntry(Element); 1202 if (!Entry) { 1203 Entry = createDIEEntry(ElemDie); 1204 insertDIEEntry(Element, Entry); 1205 } 1206 } else 1207 continue; 1208 } 1209 1210 if (CTy.isAppleBlockExtension()) 1211 addFlag(&Buffer, dwarf::DW_AT_APPLE_block); 1212 1213 DICompositeType ContainingType(resolve(CTy.getContainingType())); 1214 if (DIDescriptor(ContainingType).isCompositeType()) 1215 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, 1216 getOrCreateTypeDIE(DIType(ContainingType))); 1217 1218 if (CTy.isObjcClassComplete()) 1219 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 1220 1221 // Add template parameters to a class, structure or union types. 1222 // FIXME: The support isn't in the metadata for this yet. 1223 if (Tag == dwarf::DW_TAG_class_type || 1224 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 1225 addTemplateParams(Buffer, CTy.getTemplateParams()); 1226 1227 break; 1228 } 1229 default: 1230 break; 1231 } 1232 1233 // Add name if not anonymous or intermediate type. 1234 if (!Name.empty()) 1235 addString(&Buffer, dwarf::DW_AT_name, Name); 1236 1237 if (Tag == dwarf::DW_TAG_enumeration_type || 1238 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || 1239 Tag == dwarf::DW_TAG_union_type) { 1240 // Add size if non-zero (derived types might be zero-sized.) 1241 // TODO: Do we care about size for enum forward declarations? 1242 if (Size) 1243 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); 1244 else if (!CTy.isForwardDecl()) 1245 // Add zero size if it is not a forward declaration. 1246 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0); 1247 1248 // If we're a forward decl, say so. 1249 if (CTy.isForwardDecl()) 1250 addFlag(&Buffer, dwarf::DW_AT_declaration); 1251 1252 // Add source line info if available. 1253 if (!CTy.isForwardDecl()) 1254 addSourceLine(&Buffer, CTy); 1255 1256 // No harm in adding the runtime language to the declaration. 1257 unsigned RLang = CTy.getRunTimeLang(); 1258 if (RLang) 1259 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, 1260 RLang); 1261 } 1262 // If this is a type applicable to a type unit it then add it to the 1263 // list of types we'll compute a hash for later. 1264 if (shouldCreateTypeUnit(CTy, DD)) 1265 DD->addTypeUnitType(&Buffer); 1266 } 1267 1268 /// constructTemplateTypeParameterDIE - Construct new DIE for the given 1269 /// DITemplateTypeParameter. 1270 void 1271 CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer, 1272 DITemplateTypeParameter TP) { 1273 DIE *ParamDIE = 1274 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); 1275 // Add the type if it exists, it could be void and therefore no type. 1276 if (TP.getType()) 1277 addType(ParamDIE, resolve(TP.getType())); 1278 if (!TP.getName().empty()) 1279 addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); 1280 } 1281 1282 /// constructTemplateValueParameterDIE - Construct new DIE for the given 1283 /// DITemplateValueParameter. 1284 void 1285 CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer, 1286 DITemplateValueParameter VP) { 1287 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer); 1288 1289 // Add the type if there is one, template template and template parameter 1290 // packs will not have a type. 1291 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter) 1292 addType(ParamDIE, resolve(VP.getType())); 1293 if (!VP.getName().empty()) 1294 addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); 1295 if (Value *Val = VP.getValue()) { 1296 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) 1297 addConstantValue(ParamDIE, CI, 1298 isUnsignedDIType(DD, resolve(VP.getType()))); 1299 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) { 1300 // For declaration non-type template parameters (such as global values and 1301 // functions) 1302 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 1303 addOpAddress(Block, Asm->getSymbol(GV)); 1304 // Emit DW_OP_stack_value to use the address as the immediate value of the 1305 // parameter, rather than a pointer to it. 1306 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 1307 addBlock(ParamDIE, dwarf::DW_AT_location, Block); 1308 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) { 1309 assert(isa<MDString>(Val)); 1310 addString(ParamDIE, dwarf::DW_AT_GNU_template_name, 1311 cast<MDString>(Val)->getString()); 1312 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 1313 assert(isa<MDNode>(Val)); 1314 DIArray A(cast<MDNode>(Val)); 1315 addTemplateParams(*ParamDIE, A); 1316 } 1317 } 1318 } 1319 1320 /// getOrCreateNameSpace - Create a DIE for DINameSpace. 1321 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) { 1322 // Construct the context before querying for the existence of the DIE in case 1323 // such construction creates the DIE. 1324 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext()); 1325 if (!ContextDIE) 1326 // If the context is null, DIE should belong to the CU we call construct 1327 // function on. 1328 ContextDIE = CUDie.get(); 1329 1330 DIE *NDie = getDIE(NS); 1331 if (NDie) 1332 return NDie; 1333 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); 1334 1335 if (!NS.getName().empty()) { 1336 addString(NDie, dwarf::DW_AT_name, NS.getName()); 1337 addAccelNamespace(NS.getName(), NDie); 1338 addGlobalName(NS.getName(), NDie, NS.getContext()); 1339 } else 1340 addAccelNamespace("(anonymous namespace)", NDie); 1341 addSourceLine(NDie, NS); 1342 return NDie; 1343 } 1344 1345 /// getOrCreateSubprogramDIE - Create new DIE using SP. 1346 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) { 1347 // Construct the context before querying for the existence of the DIE in case 1348 // such construction creates the DIE (as is the case for member function 1349 // declarations). 1350 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext())); 1351 if (!ContextDIE) 1352 ContextDIE = CUDie.get(); 1353 1354 DIE *SPDie = getDIE(SP); 1355 if (SPDie) 1356 return SPDie; 1357 1358 DISubprogram SPDecl = SP.getFunctionDeclaration(); 1359 if (SPDecl.isSubprogram()) 1360 // Add subprogram definitions to the CU die directly. 1361 ContextDIE = CUDie.get(); 1362 1363 // DW_TAG_inlined_subroutine may refer to this DIE. 1364 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); 1365 1366 DIE *DeclDie = NULL; 1367 if (SPDecl.isSubprogram()) 1368 DeclDie = getOrCreateSubprogramDIE(SPDecl); 1369 1370 // Add function template parameters. 1371 addTemplateParams(*SPDie, SP.getTemplateParams()); 1372 1373 // If this DIE is going to refer declaration info using AT_specification 1374 // then there is no need to add other attributes. 1375 if (DeclDie) { 1376 // Refer function declaration directly. 1377 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie); 1378 1379 return SPDie; 1380 } 1381 1382 // Add the linkage name if we have one. 1383 StringRef LinkageName = SP.getLinkageName(); 1384 if (!LinkageName.empty()) 1385 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, 1386 GlobalValue::getRealLinkageName(LinkageName)); 1387 1388 // Constructors and operators for anonymous aggregates do not have names. 1389 if (!SP.getName().empty()) 1390 addString(SPDie, dwarf::DW_AT_name, SP.getName()); 1391 1392 addSourceLine(SPDie, SP); 1393 1394 // Add the prototype if we have a prototype and we have a C like 1395 // language. 1396 uint16_t Language = DICompileUnit(Node).getLanguage(); 1397 if (SP.isPrototyped() && 1398 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1399 Language == dwarf::DW_LANG_ObjC)) 1400 addFlag(SPDie, dwarf::DW_AT_prototyped); 1401 1402 DICompositeType SPTy = SP.getType(); 1403 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type && 1404 "the type of a subprogram should be a subroutine"); 1405 1406 DIArray Args = SPTy.getTypeArray(); 1407 // Add a return type. If this is a type like a C/C++ void type we don't add a 1408 // return type. 1409 if (Args.getElement(0)) 1410 addType(SPDie, DIType(Args.getElement(0))); 1411 1412 unsigned VK = SP.getVirtuality(); 1413 if (VK) { 1414 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 1415 DIEBlock *Block = getDIEBlock(); 1416 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1417 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex()); 1418 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); 1419 ContainingTypeMap.insert(std::make_pair(SPDie, 1420 resolve(SP.getContainingType()))); 1421 } 1422 1423 if (!SP.isDefinition()) { 1424 addFlag(SPDie, dwarf::DW_AT_declaration); 1425 1426 // Add arguments. Do not add arguments for subprogram definition. They will 1427 // be handled while processing variables. 1428 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 1429 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie); 1430 DIType ATy = DIType(Args.getElement(i)); 1431 addType(Arg, ATy); 1432 if (ATy.isArtificial()) 1433 addFlag(Arg, dwarf::DW_AT_artificial); 1434 } 1435 } 1436 1437 if (SP.isArtificial()) 1438 addFlag(SPDie, dwarf::DW_AT_artificial); 1439 1440 if (!SP.isLocalToUnit()) 1441 addFlag(SPDie, dwarf::DW_AT_external); 1442 1443 if (SP.isOptimized()) 1444 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 1445 1446 if (unsigned isa = Asm->getISAEncoding()) { 1447 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 1448 } 1449 1450 return SPDie; 1451 } 1452 1453 // Return const expression if value is a GEP to access merged global 1454 // constant. e.g. 1455 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) 1456 static const ConstantExpr *getMergedGlobalExpr(const Value *V) { 1457 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); 1458 if (!CE || CE->getNumOperands() != 3 || 1459 CE->getOpcode() != Instruction::GetElementPtr) 1460 return NULL; 1461 1462 // First operand points to a global struct. 1463 Value *Ptr = CE->getOperand(0); 1464 if (!isa<GlobalValue>(Ptr) || 1465 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) 1466 return NULL; 1467 1468 // Second operand is zero. 1469 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); 1470 if (!CI || !CI->isZero()) 1471 return NULL; 1472 1473 // Third operand is offset. 1474 if (!isa<ConstantInt>(CE->getOperand(2))) 1475 return NULL; 1476 1477 return CE; 1478 } 1479 1480 /// createGlobalVariableDIE - create global variable DIE. 1481 void CompileUnit::createGlobalVariableDIE(const MDNode *N) { 1482 // Check for pre-existence. 1483 if (getDIE(N)) 1484 return; 1485 1486 DIGlobalVariable GV(N); 1487 if (!GV.isGlobalVariable()) 1488 return; 1489 1490 DIScope GVContext = GV.getContext(); 1491 DIType GTy = GV.getType(); 1492 1493 // If this is a static data member definition, some attributes belong 1494 // to the declaration DIE. 1495 DIE *VariableDIE = NULL; 1496 bool IsStaticMember = false; 1497 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); 1498 if (SDMDecl.Verify()) { 1499 assert(SDMDecl.isStaticMember() && "Expected static member decl"); 1500 // We need the declaration DIE that is in the static member's class. 1501 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); 1502 IsStaticMember = true; 1503 } 1504 1505 // If this is not a static data member definition, create the variable 1506 // DIE and add the initial set of attributes to it. 1507 if (!VariableDIE) { 1508 // Construct the context before querying for the existence of the DIE in 1509 // case such construction creates the DIE. 1510 DIE *ContextDIE = getOrCreateContextDIE(GVContext); 1511 if (!ContextDIE) 1512 ContextDIE = CUDie.get(); 1513 1514 // Add to map. 1515 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, N); 1516 1517 // Add name and type. 1518 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); 1519 addType(VariableDIE, GTy); 1520 1521 // Add scoping info. 1522 if (!GV.isLocalToUnit()) 1523 addFlag(VariableDIE, dwarf::DW_AT_external); 1524 1525 // Add line number info. 1526 addSourceLine(VariableDIE, GV); 1527 } 1528 1529 // Add location. 1530 bool addToAccelTable = false; 1531 DIE *VariableSpecDIE = NULL; 1532 bool isGlobalVariable = GV.getGlobal() != NULL; 1533 if (isGlobalVariable) { 1534 addToAccelTable = true; 1535 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 1536 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); 1537 if (GV.getGlobal()->isThreadLocal()) { 1538 // FIXME: Make this work with -gsplit-dwarf. 1539 unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 1540 assert((PointerSize == 4 || PointerSize == 8) && 1541 "Add support for other sizes if necessary"); 1542 const MCExpr *Expr = 1543 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym); 1544 // Based on GCC's support for TLS: 1545 if (!DD->useSplitDwarf()) { 1546 // 1) Start with a constNu of the appropriate pointer size 1547 addUInt(Block, dwarf::DW_FORM_data1, 1548 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); 1549 // 2) containing the (relocated) offset of the TLS variable 1550 // within the module's TLS block. 1551 addExpr(Block, dwarf::DW_FORM_udata, Expr); 1552 } else { 1553 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 1554 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr)); 1555 } 1556 // 3) followed by a custom OP to make the debugger do a TLS lookup. 1557 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); 1558 } else 1559 addOpAddress(Block, Sym); 1560 // Do not create specification DIE if context is either compile unit 1561 // or a subprogram. 1562 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && 1563 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { 1564 // Create specification DIE. 1565 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie.get()); 1566 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE); 1567 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block); 1568 // A static member's declaration is already flagged as such. 1569 if (!SDMDecl.Verify()) 1570 addFlag(VariableDIE, dwarf::DW_AT_declaration); 1571 } else { 1572 addBlock(VariableDIE, dwarf::DW_AT_location, Block); 1573 } 1574 // Add the linkage name. 1575 StringRef LinkageName = GV.getLinkageName(); 1576 if (!LinkageName.empty()) 1577 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: 1578 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and 1579 // TAG_variable. 1580 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE 1581 : VariableDIE, 1582 dwarf::DW_AT_MIPS_linkage_name, 1583 GlobalValue::getRealLinkageName(LinkageName)); 1584 } else if (const ConstantInt *CI = 1585 dyn_cast_or_null<ConstantInt>(GV.getConstant())) { 1586 // AT_const_value was added when the static member was created. To avoid 1587 // emitting AT_const_value multiple times, we only add AT_const_value when 1588 // it is not a static member. 1589 if (!IsStaticMember) 1590 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy)); 1591 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) { 1592 addToAccelTable = true; 1593 // GV is a merged global. 1594 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 1595 Value *Ptr = CE->getOperand(0); 1596 addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr))); 1597 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1598 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); 1599 addUInt(Block, dwarf::DW_FORM_udata, 1600 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); 1601 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1602 addBlock(VariableDIE, dwarf::DW_AT_location, Block); 1603 } 1604 1605 if (addToAccelTable) { 1606 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; 1607 addAccelName(GV.getName(), AddrDIE); 1608 1609 // If the linkage name is different than the name, go ahead and output 1610 // that as well into the name table. 1611 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) 1612 addAccelName(GV.getLinkageName(), AddrDIE); 1613 } 1614 1615 if (!GV.isLocalToUnit()) 1616 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE, 1617 GV.getContext()); 1618 } 1619 1620 /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 1621 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, 1622 DIE *IndexTy) { 1623 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); 1624 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy); 1625 1626 // The LowerBound value defines the lower bounds which is typically zero for 1627 // C/C++. The Count value is the number of elements. Values are 64 bit. If 1628 // Count == -1 then the array is unbounded and we do not emit 1629 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and 1630 // Count == 0, then the array has zero elements in which case we do not emit 1631 // an upper bound. 1632 int64_t LowerBound = SR.getLo(); 1633 int64_t DefaultLowerBound = getDefaultLowerBound(); 1634 int64_t Count = SR.getCount(); 1635 1636 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) 1637 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); 1638 1639 if (Count != -1 && Count != 0) 1640 // FIXME: An unbounded array should reference the expression that defines 1641 // the array. 1642 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, LowerBound + Count - 1); 1643 } 1644 1645 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 1646 void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType *CTy) { 1647 if (CTy->isVector()) 1648 addFlag(&Buffer, dwarf::DW_AT_GNU_vector); 1649 1650 // Emit the element type. 1651 addType(&Buffer, resolve(CTy->getTypeDerivedFrom())); 1652 1653 // Get an anonymous type for index type. 1654 // FIXME: This type should be passed down from the front end 1655 // as different languages may have different sizes for indexes. 1656 DIE *IdxTy = getIndexTyDie(); 1657 if (!IdxTy) { 1658 // Construct an anonymous type for index type. 1659 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get()); 1660 addString(IdxTy, dwarf::DW_AT_name, "int"); 1661 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t)); 1662 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1663 dwarf::DW_ATE_signed); 1664 setIndexTyDie(IdxTy); 1665 } 1666 1667 // Add subranges to array type. 1668 DIArray Elements = CTy->getTypeArray(); 1669 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1670 DIDescriptor Element = Elements.getElement(i); 1671 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 1672 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); 1673 } 1674 } 1675 1676 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 1677 void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DIEnumerator ETy) { 1678 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); 1679 StringRef Name = ETy.getName(); 1680 addString(Enumerator, dwarf::DW_AT_name, Name); 1681 int64_t Value = ETy.getEnumValue(); 1682 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); 1683 } 1684 1685 /// constructContainingTypeDIEs - Construct DIEs for types that contain 1686 /// vtables. 1687 void CompileUnit::constructContainingTypeDIEs() { 1688 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(), 1689 CE = ContainingTypeMap.end(); 1690 CI != CE; ++CI) { 1691 DIE *SPDie = CI->first; 1692 const MDNode *N = CI->second; 1693 if (!N) 1694 continue; 1695 DIE *NDie = getDIE(N); 1696 if (!NDie) 1697 continue; 1698 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie); 1699 } 1700 } 1701 1702 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 1703 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) { 1704 StringRef Name = DV->getName(); 1705 1706 // Define variable debug information entry. 1707 DIE *VariableDie = new DIE(DV->getTag()); 1708 DbgVariable *AbsVar = DV->getAbstractVariable(); 1709 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL; 1710 if (AbsDIE) 1711 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE); 1712 else { 1713 if (!Name.empty()) 1714 addString(VariableDie, dwarf::DW_AT_name, Name); 1715 addSourceLine(VariableDie, DV->getVariable()); 1716 addType(VariableDie, DV->getType()); 1717 } 1718 1719 if (DV->isArtificial()) 1720 addFlag(VariableDie, dwarf::DW_AT_artificial); 1721 1722 if (isScopeAbstract) { 1723 DV->setDIE(VariableDie); 1724 return VariableDie; 1725 } 1726 1727 // Add variable address. 1728 1729 unsigned Offset = DV->getDotDebugLocOffset(); 1730 if (Offset != ~0U) { 1731 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4, 1732 Asm->GetTempSymbol("debug_loc", Offset)); 1733 DV->setDIE(VariableDie); 1734 return VariableDie; 1735 } 1736 1737 // Check if variable is described by a DBG_VALUE instruction. 1738 if (const MachineInstr *DVInsn = DV->getMInsn()) { 1739 assert(DVInsn->getNumOperands() == 3); 1740 if (DVInsn->getOperand(0).isReg()) { 1741 const MachineOperand RegOp = DVInsn->getOperand(0); 1742 // If the second operand is an immediate, this is an indirect value. 1743 if (DVInsn->getOperand(1).isImm()) { 1744 MachineLocation Location(RegOp.getReg(), 1745 DVInsn->getOperand(1).getImm()); 1746 addVariableAddress(*DV, VariableDie, Location); 1747 } else if (RegOp.getReg()) 1748 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg())); 1749 } else if (DVInsn->getOperand(0).isImm()) 1750 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType()); 1751 else if (DVInsn->getOperand(0).isFPImm()) 1752 addConstantFPValue(VariableDie, DVInsn->getOperand(0)); 1753 else if (DVInsn->getOperand(0).isCImm()) 1754 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(), 1755 isUnsignedDIType(DD, DV->getType())); 1756 1757 DV->setDIE(VariableDie); 1758 return VariableDie; 1759 } else { 1760 // .. else use frame index. 1761 int FI = DV->getFrameIndex(); 1762 if (FI != ~0) { 1763 unsigned FrameReg = 0; 1764 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); 1765 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 1766 MachineLocation Location(FrameReg, Offset); 1767 addVariableAddress(*DV, VariableDie, Location); 1768 } 1769 } 1770 1771 DV->setDIE(VariableDie); 1772 return VariableDie; 1773 } 1774 1775 /// constructMemberDIE - Construct member DIE from DIDerivedType. 1776 void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) { 1777 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer); 1778 StringRef Name = DT.getName(); 1779 if (!Name.empty()) 1780 addString(MemberDie, dwarf::DW_AT_name, Name); 1781 1782 addType(MemberDie, resolve(DT.getTypeDerivedFrom())); 1783 1784 addSourceLine(MemberDie, DT); 1785 1786 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); 1787 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1788 1789 uint64_t Size = DT.getSizeInBits(); 1790 uint64_t FieldSize = getBaseTypeSize(DD, DT); 1791 1792 if (Size != FieldSize) { 1793 // Handle bitfield. 1794 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, 1795 getBaseTypeSize(DD, DT) >> 3); 1796 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits()); 1797 1798 uint64_t Offset = DT.getOffsetInBits(); 1799 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 1800 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1801 uint64_t FieldOffset = (HiMark - FieldSize); 1802 Offset -= FieldOffset; 1803 1804 // Maybe we need to work from the other end. 1805 if (Asm->getDataLayout().isLittleEndian()) 1806 Offset = FieldSize - (Offset + Size); 1807 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); 1808 1809 // Here WD_AT_data_member_location points to the anonymous 1810 // field that includes this bit field. 1811 addUInt(MemLocationDie, dwarf::DW_FORM_udata, FieldOffset >> 3); 1812 1813 } else 1814 // This is not a bitfield. 1815 addUInt(MemLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); 1816 1817 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) { 1818 1819 // For C++, virtual base classes are not at fixed offset. Use following 1820 // expression to extract appropriate offset from vtable. 1821 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 1822 1823 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); 1824 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 1825 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1826 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1827 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits()); 1828 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 1829 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1830 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1831 1832 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); 1833 } else 1834 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); 1835 1836 if (DT.isProtected()) 1837 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1838 dwarf::DW_ACCESS_protected); 1839 else if (DT.isPrivate()) 1840 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1841 dwarf::DW_ACCESS_private); 1842 // Otherwise C++ member and base classes are considered public. 1843 else 1844 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1845 dwarf::DW_ACCESS_public); 1846 if (DT.isVirtual()) 1847 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 1848 dwarf::DW_VIRTUALITY_virtual); 1849 1850 // Objective-C properties. 1851 if (MDNode *PNode = DT.getObjCProperty()) 1852 if (DIEEntry *PropertyDie = getDIEEntry(PNode)) 1853 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, 1854 PropertyDie); 1855 1856 if (DT.isArtificial()) 1857 addFlag(MemberDie, dwarf::DW_AT_artificial); 1858 } 1859 1860 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member. 1861 DIE *CompileUnit::getOrCreateStaticMemberDIE(const DIDerivedType DT) { 1862 if (!DT.Verify()) 1863 return NULL; 1864 1865 // Construct the context before querying for the existence of the DIE in case 1866 // such construction creates the DIE. 1867 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext())); 1868 assert(ContextDIE && "Static member should belong to a non-CU context."); 1869 1870 DIE *StaticMemberDIE = getDIE(DT); 1871 if (StaticMemberDIE) 1872 return StaticMemberDIE; 1873 1874 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT); 1875 1876 DIType Ty = resolve(DT.getTypeDerivedFrom()); 1877 1878 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); 1879 addType(StaticMemberDIE, Ty); 1880 addSourceLine(StaticMemberDIE, DT); 1881 addFlag(StaticMemberDIE, dwarf::DW_AT_external); 1882 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); 1883 1884 // FIXME: We could omit private if the parent is a class_type, and 1885 // public if the parent is something else. 1886 if (DT.isProtected()) 1887 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1888 dwarf::DW_ACCESS_protected); 1889 else if (DT.isPrivate()) 1890 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1891 dwarf::DW_ACCESS_private); 1892 else 1893 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1894 dwarf::DW_ACCESS_public); 1895 1896 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant())) 1897 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty)); 1898 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant())) 1899 addConstantFPValue(StaticMemberDIE, CFP); 1900 1901 return StaticMemberDIE; 1902 } 1903