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