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/Constants.h" 21 #include "llvm/DIBuilder.h" 22 #include "llvm/DataLayout.h" 23 #include "llvm/GlobalVariable.h" 24 #include "llvm/Instructions.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Target/Mangler.h" 28 #include "llvm/Target/TargetFrameLowering.h" 29 #include "llvm/Target/TargetMachine.h" 30 #include "llvm/Target/TargetRegisterInfo.h" 31 32 using namespace llvm; 33 34 /// CompileUnit - Compile unit constructor. 35 CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, AsmPrinter *A, 36 DwarfDebug *DW) 37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) { 38 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); 39 } 40 41 /// ~CompileUnit - Destructor for compile unit. 42 CompileUnit::~CompileUnit() { 43 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) 44 DIEBlocks[j]->~DIEBlock(); 45 } 46 47 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 48 /// information entry. 49 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) { 50 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); 51 return Value; 52 } 53 54 /// getDefaultLowerBound - Return the default lower bound for an array. If the 55 /// DWARF version doesn't handle the language, return -1. 56 int64_t CompileUnit::getDefaultLowerBound() const { 57 switch (Language) { 58 default: 59 break; 60 61 case dwarf::DW_LANG_C89: 62 case dwarf::DW_LANG_C99: 63 case dwarf::DW_LANG_C: 64 case dwarf::DW_LANG_C_plus_plus: 65 case dwarf::DW_LANG_ObjC: 66 case dwarf::DW_LANG_ObjC_plus_plus: 67 return 0; 68 69 case dwarf::DW_LANG_Fortran77: 70 case dwarf::DW_LANG_Fortran90: 71 case dwarf::DW_LANG_Fortran95: 72 return 1; 73 74 // The languages below have valid values only if the DWARF version >= 4. 75 case dwarf::DW_LANG_Java: 76 case dwarf::DW_LANG_Python: 77 case dwarf::DW_LANG_UPC: 78 case dwarf::DW_LANG_D: 79 if (dwarf::DWARF_VERSION >= 4) 80 return 0; 81 break; 82 83 case dwarf::DW_LANG_Ada83: 84 case dwarf::DW_LANG_Ada95: 85 case dwarf::DW_LANG_Cobol74: 86 case dwarf::DW_LANG_Cobol85: 87 case dwarf::DW_LANG_Modula2: 88 case dwarf::DW_LANG_Pascal83: 89 case dwarf::DW_LANG_PLI: 90 if (dwarf::DWARF_VERSION >= 4) 91 return 1; 92 break; 93 } 94 95 return -1; 96 } 97 98 /// addFlag - Add a flag that is true. 99 void CompileUnit::addFlag(DIE *Die, unsigned Attribute) { 100 if (!DD->useDarwinGDBCompat()) 101 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, 102 DIEIntegerOne); 103 else 104 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1); 105 } 106 107 /// addUInt - Add an unsigned integer attribute data and value. 108 /// 109 void CompileUnit::addUInt(DIE *Die, unsigned Attribute, 110 unsigned Form, uint64_t Integer) { 111 if (!Form) Form = DIEInteger::BestForm(false, Integer); 112 DIEValue *Value = Integer == 1 ? 113 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer); 114 Die->addValue(Attribute, Form, Value); 115 } 116 117 /// addSInt - Add an signed integer attribute data and value. 118 /// 119 void CompileUnit::addSInt(DIE *Die, unsigned Attribute, 120 unsigned Form, int64_t Integer) { 121 if (!Form) Form = DIEInteger::BestForm(true, Integer); 122 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); 123 Die->addValue(Attribute, Form, Value); 124 } 125 126 /// addString - Add a string attribute data and value. We always emit a 127 /// reference to the string pool instead of immediate strings so that DIEs have 128 /// more predictable sizes. 129 void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) { 130 MCSymbol *Symb = DD->getStringPoolEntry(String); 131 DIEValue *Value; 132 if (Asm->needsRelocationsForDwarfStringPool()) 133 Value = new (DIEValueAllocator) DIELabel(Symb); 134 else { 135 MCSymbol *StringPool = DD->getStringPool(); 136 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); 137 } 138 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value); 139 } 140 141 /// addLabel - Add a Dwarf label attribute data and value. 142 /// 143 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form, 144 const MCSymbol *Label) { 145 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); 146 Die->addValue(Attribute, Form, Value); 147 } 148 149 /// addDelta - Add a label delta attribute data and value. 150 /// 151 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form, 152 const MCSymbol *Hi, const MCSymbol *Lo) { 153 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 154 Die->addValue(Attribute, Form, Value); 155 } 156 157 /// addDIEEntry - Add a DIE attribute data and value. 158 /// 159 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, 160 DIE *Entry) { 161 Die->addValue(Attribute, Form, createDIEEntry(Entry)); 162 } 163 164 /// addBlock - Add block data. 165 /// 166 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form, 167 DIEBlock *Block) { 168 Block->ComputeSize(Asm); 169 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. 170 Die->addValue(Attribute, Block->BestForm(), Block); 171 } 172 173 /// addSourceLine - Add location information to specified debug information 174 /// entry. 175 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) { 176 // Verify variable. 177 if (!V.Verify()) 178 return; 179 180 unsigned Line = V.getLineNumber(); 181 if (Line == 0) 182 return; 183 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(), 184 V.getContext().getDirectory()); 185 assert(FileID && "Invalid file id"); 186 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 187 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 188 } 189 190 /// addSourceLine - Add location information to specified debug information 191 /// entry. 192 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { 193 // Verify global variable. 194 if (!G.Verify()) 195 return; 196 197 unsigned Line = G.getLineNumber(); 198 if (Line == 0) 199 return; 200 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory()); 201 assert(FileID && "Invalid file id"); 202 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 203 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 204 } 205 206 /// addSourceLine - Add location information to specified debug information 207 /// entry. 208 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) { 209 // Verify subprogram. 210 if (!SP.Verify()) 211 return; 212 213 // If the line number is 0, don't add it. 214 unsigned Line = SP.getLineNumber(); 215 if (Line == 0) 216 return; 217 218 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), 219 SP.getDirectory()); 220 assert(FileID && "Invalid file id"); 221 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 222 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 223 } 224 225 /// addSourceLine - Add location information to specified debug information 226 /// entry. 227 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) { 228 // Verify type. 229 if (!Ty.Verify()) 230 return; 231 232 unsigned Line = Ty.getLineNumber(); 233 if (Line == 0) 234 return; 235 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), 236 Ty.getDirectory()); 237 assert(FileID && "Invalid file id"); 238 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 239 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 240 } 241 242 /// addSourceLine - Add location information to specified debug information 243 /// entry. 244 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) { 245 // Verify type. 246 if (!Ty.Verify()) 247 return; 248 249 unsigned Line = Ty.getLineNumber(); 250 if (Line == 0) 251 return; 252 DIFile File = Ty.getFile(); 253 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(), 254 File.getDirectory()); 255 assert(FileID && "Invalid file id"); 256 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 257 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 258 } 259 260 /// addSourceLine - Add location information to specified debug information 261 /// entry. 262 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) { 263 // Verify namespace. 264 if (!NS.Verify()) 265 return; 266 267 unsigned Line = NS.getLineNumber(); 268 if (Line == 0) 269 return; 270 StringRef FN = NS.getFilename(); 271 272 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory()); 273 assert(FileID && "Invalid file id"); 274 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 275 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 276 } 277 278 /// addVariableAddress - Add DW_AT_location attribute for a 279 /// DbgVariable based on provided MachineLocation. 280 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die, 281 MachineLocation Location) { 282 if (DV->variableHasComplexAddress()) 283 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 284 else if (DV->isBlockByrefVariable()) 285 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 286 else 287 addAddress(Die, dwarf::DW_AT_location, Location); 288 } 289 290 /// addRegisterOp - Add register operand. 291 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) { 292 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 293 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 294 if (DWReg < 32) 295 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); 296 else { 297 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 298 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg); 299 } 300 } 301 302 /// addRegisterOffset - Add register offset. 303 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg, 304 int64_t Offset) { 305 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 306 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 307 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); 308 if (Reg == TRI->getFrameRegister(*Asm->MF)) 309 // If variable offset is based in frame register then use fbreg. 310 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); 311 else if (DWReg < 32) 312 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); 313 else { 314 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 315 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg); 316 } 317 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset); 318 } 319 320 /// addAddress - Add an address attribute to a die based on the location 321 /// provided. 322 void CompileUnit::addAddress(DIE *Die, unsigned Attribute, 323 const MachineLocation &Location) { 324 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 325 326 if (Location.isReg()) 327 addRegisterOp(Block, Location.getReg()); 328 else 329 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 330 331 // Now attach the location information to the DIE. 332 addBlock(Die, Attribute, 0, Block); 333 } 334 335 /// addComplexAddress - Start with the address based on the location provided, 336 /// and generate the DWARF information necessary to find the actual variable 337 /// given the extra address information encoded in the DIVariable, starting from 338 /// the starting location. Add the DWARF information to the die. 339 /// 340 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die, 341 unsigned Attribute, 342 const MachineLocation &Location) { 343 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 344 unsigned N = DV->getNumAddrElements(); 345 unsigned i = 0; 346 if (Location.isReg()) { 347 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) { 348 // If first address element is OpPlus then emit 349 // DW_OP_breg + Offset instead of DW_OP_reg + Offset. 350 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1)); 351 i = 2; 352 } else 353 addRegisterOp(Block, Location.getReg()); 354 } 355 else 356 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 357 358 for (;i < N; ++i) { 359 uint64_t Element = DV->getAddrElement(i); 360 if (Element == DIBuilder::OpPlus) { 361 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 362 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i)); 363 } else if (Element == DIBuilder::OpDeref) { 364 if (!Location.isReg()) 365 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 366 } else llvm_unreachable("unknown DIBuilder Opcode"); 367 } 368 369 // Now attach the location information to the DIE. 370 addBlock(Die, Attribute, 0, Block); 371 } 372 373 /* Byref variables, in Blocks, are declared by the programmer as "SomeType 374 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and 375 gives the variable VarName either the struct, or a pointer to the struct, as 376 its type. This is necessary for various behind-the-scenes things the 377 compiler needs to do with by-reference variables in Blocks. 378 379 However, as far as the original *programmer* is concerned, the variable 380 should still have type 'SomeType', as originally declared. 381 382 The function getBlockByrefType dives into the __Block_byref_x_VarName 383 struct to find the original type of the variable, which is then assigned to 384 the variable's Debug Information Entry as its real type. So far, so good. 385 However now the debugger will expect the variable VarName to have the type 386 SomeType. So we need the location attribute for the variable to be an 387 expression that explains to the debugger how to navigate through the 388 pointers and struct to find the actual variable of type SomeType. 389 390 The following function does just that. We start by getting 391 the "normal" location for the variable. This will be the location 392 of either the struct __Block_byref_x_VarName or the pointer to the 393 struct __Block_byref_x_VarName. 394 395 The struct will look something like: 396 397 struct __Block_byref_x_VarName { 398 ... <various fields> 399 struct __Block_byref_x_VarName *forwarding; 400 ... <various other fields> 401 SomeType VarName; 402 ... <maybe more fields> 403 }; 404 405 If we are given the struct directly (as our starting point) we 406 need to tell the debugger to: 407 408 1). Add the offset of the forwarding field. 409 410 2). Follow that pointer to get the real __Block_byref_x_VarName 411 struct to use (the real one may have been copied onto the heap). 412 413 3). Add the offset for the field VarName, to find the actual variable. 414 415 If we started with a pointer to the struct, then we need to 416 dereference that pointer first, before the other steps. 417 Translating this into DWARF ops, we will need to append the following 418 to the current location description for the variable: 419 420 DW_OP_deref -- optional, if we start with a pointer 421 DW_OP_plus_uconst <forward_fld_offset> 422 DW_OP_deref 423 DW_OP_plus_uconst <varName_fld_offset> 424 425 That is what this function does. */ 426 427 /// addBlockByrefAddress - Start with the address based on the location 428 /// provided, and generate the DWARF information necessary to find the 429 /// actual Block variable (navigating the Block struct) based on the 430 /// starting location. Add the DWARF information to the die. For 431 /// more information, read large comment just above here. 432 /// 433 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die, 434 unsigned Attribute, 435 const MachineLocation &Location) { 436 DIType Ty = DV->getType(); 437 DIType TmpTy = Ty; 438 unsigned Tag = Ty.getTag(); 439 bool isPointer = false; 440 441 StringRef varName = DV->getName(); 442 443 if (Tag == dwarf::DW_TAG_pointer_type) { 444 DIDerivedType DTy = DIDerivedType(Ty); 445 TmpTy = DTy.getTypeDerivedFrom(); 446 isPointer = true; 447 } 448 449 DICompositeType blockStruct = DICompositeType(TmpTy); 450 451 // Find the __forwarding field and the variable field in the __Block_byref 452 // struct. 453 DIArray Fields = blockStruct.getTypeArray(); 454 DIDescriptor varField = DIDescriptor(); 455 DIDescriptor forwardingField = DIDescriptor(); 456 457 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { 458 DIDescriptor Element = Fields.getElement(i); 459 DIDerivedType DT = DIDerivedType(Element); 460 StringRef fieldName = DT.getName(); 461 if (fieldName == "__forwarding") 462 forwardingField = Element; 463 else if (fieldName == varName) 464 varField = Element; 465 } 466 467 // Get the offsets for the forwarding field and the variable field. 468 unsigned forwardingFieldOffset = 469 DIDerivedType(forwardingField).getOffsetInBits() >> 3; 470 unsigned varFieldOffset = 471 DIDerivedType(varField).getOffsetInBits() >> 3; 472 473 // Decode the original location, and use that as the start of the byref 474 // variable's location. 475 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 476 477 if (Location.isReg()) 478 addRegisterOp(Block, Location.getReg()); 479 else 480 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 481 482 // If we started with a pointer to the __Block_byref... struct, then 483 // the first thing we need to do is dereference the pointer (DW_OP_deref). 484 if (isPointer) 485 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 486 487 // Next add the offset for the '__forwarding' field: 488 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in 489 // adding the offset if it's 0. 490 if (forwardingFieldOffset > 0) { 491 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 492 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset); 493 } 494 495 // Now dereference the __forwarding field to get to the real __Block_byref 496 // struct: DW_OP_deref. 497 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 498 499 // Now that we've got the real __Block_byref... struct, add the offset 500 // for the variable's field to get to the location of the actual variable: 501 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. 502 if (varFieldOffset > 0) { 503 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 504 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset); 505 } 506 507 // Now attach the location information to the DIE. 508 addBlock(Die, Attribute, 0, Block); 509 } 510 511 /// isTypeSigned - Return true if the type is signed. 512 static bool isTypeSigned(DIType Ty, int *SizeInBits) { 513 if (Ty.isDerivedType()) 514 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits); 515 if (Ty.isBasicType()) 516 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed 517 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { 518 *SizeInBits = Ty.getSizeInBits(); 519 return true; 520 } 521 return false; 522 } 523 524 /// addConstantValue - Add constant value entry in variable DIE. 525 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO, 526 DIType Ty) { 527 assert(MO.isImm() && "Invalid machine operand!"); 528 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 529 int SizeInBits = -1; 530 bool SignedConstant = isTypeSigned(Ty, &SizeInBits); 531 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata; 532 switch (SizeInBits) { 533 case 8: Form = dwarf::DW_FORM_data1; break; 534 case 16: Form = dwarf::DW_FORM_data2; break; 535 case 32: Form = dwarf::DW_FORM_data4; break; 536 case 64: Form = dwarf::DW_FORM_data8; break; 537 default: break; 538 } 539 SignedConstant ? addSInt(Block, 0, Form, MO.getImm()) 540 : addUInt(Block, 0, Form, MO.getImm()); 541 542 addBlock(Die, dwarf::DW_AT_const_value, 0, Block); 543 return true; 544 } 545 546 /// addConstantFPValue - Add constant value entry in variable DIE. 547 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { 548 assert (MO.isFPImm() && "Invalid machine operand!"); 549 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 550 APFloat FPImm = MO.getFPImm()->getValueAPF(); 551 552 // Get the raw data form of the floating point. 553 const APInt FltVal = FPImm.bitcastToAPInt(); 554 const char *FltPtr = (const char*)FltVal.getRawData(); 555 556 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 557 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 558 int Incr = (LittleEndian ? 1 : -1); 559 int Start = (LittleEndian ? 0 : NumBytes - 1); 560 int Stop = (LittleEndian ? NumBytes : -1); 561 562 // Output the constant to DWARF one byte at a time. 563 for (; Start != Stop; Start += Incr) 564 addUInt(Block, 0, dwarf::DW_FORM_data1, 565 (unsigned char)0xFF & FltPtr[Start]); 566 567 addBlock(Die, dwarf::DW_AT_const_value, 0, Block); 568 return true; 569 } 570 571 /// addConstantValue - Add constant value entry in variable DIE. 572 bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI, 573 bool Unsigned) { 574 unsigned CIBitWidth = CI->getBitWidth(); 575 if (CIBitWidth <= 64) { 576 unsigned form = 0; 577 switch (CIBitWidth) { 578 case 8: form = dwarf::DW_FORM_data1; break; 579 case 16: form = dwarf::DW_FORM_data2; break; 580 case 32: form = dwarf::DW_FORM_data4; break; 581 case 64: form = dwarf::DW_FORM_data8; break; 582 default: 583 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata; 584 } 585 if (Unsigned) 586 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue()); 587 else 588 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue()); 589 return true; 590 } 591 592 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 593 594 // Get the raw data form of the large APInt. 595 const APInt Val = CI->getValue(); 596 const uint64_t *Ptr64 = Val.getRawData(); 597 598 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 599 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 600 601 // Output the constant to DWARF one byte at a time. 602 for (int i = 0; i < NumBytes; i++) { 603 uint8_t c; 604 if (LittleEndian) 605 c = Ptr64[i / 8] >> (8 * (i & 7)); 606 else 607 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); 608 addUInt(Block, 0, dwarf::DW_FORM_data1, c); 609 } 610 611 addBlock(Die, dwarf::DW_AT_const_value, 0, Block); 612 return true; 613 } 614 615 /// addTemplateParams - Add template parameters in buffer. 616 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { 617 // Add template parameters. 618 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { 619 DIDescriptor Element = TParams.getElement(i); 620 if (Element.isTemplateTypeParameter()) 621 Buffer.addChild(getOrCreateTemplateTypeParameterDIE( 622 DITemplateTypeParameter(Element))); 623 else if (Element.isTemplateValueParameter()) 624 Buffer.addChild(getOrCreateTemplateValueParameterDIE( 625 DITemplateValueParameter(Element))); 626 } 627 } 628 629 /// addToContextOwner - Add Die into the list of its context owner's children. 630 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) { 631 if (Context.isType()) { 632 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context)); 633 ContextDIE->addChild(Die); 634 } else if (Context.isNameSpace()) { 635 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context)); 636 ContextDIE->addChild(Die); 637 } else if (Context.isSubprogram()) { 638 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context)); 639 ContextDIE->addChild(Die); 640 } else if (DIE *ContextDIE = getDIE(Context)) 641 ContextDIE->addChild(Die); 642 else 643 addDie(Die); 644 } 645 646 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 647 /// given DIType. 648 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) { 649 DIType Ty(TyNode); 650 if (!Ty.Verify()) 651 return NULL; 652 DIE *TyDIE = getDIE(Ty); 653 if (TyDIE) 654 return TyDIE; 655 656 // Create new type. 657 TyDIE = new DIE(dwarf::DW_TAG_base_type); 658 insertDIE(Ty, TyDIE); 659 if (Ty.isBasicType()) 660 constructTypeDIE(*TyDIE, DIBasicType(Ty)); 661 else if (Ty.isCompositeType()) 662 constructTypeDIE(*TyDIE, DICompositeType(Ty)); 663 else { 664 assert(Ty.isDerivedType() && "Unknown kind of DIType"); 665 constructTypeDIE(*TyDIE, DIDerivedType(Ty)); 666 } 667 // If this is a named finished type then include it in the list of types 668 // for the accelerator tables. 669 if (!Ty.getName().empty() && !Ty.isForwardDecl()) { 670 bool IsImplementation = 0; 671 if (Ty.isCompositeType()) { 672 DICompositeType CT(Ty); 673 // A runtime language of 0 actually means C/C++ and that any 674 // non-negative value is some version of Objective-C/C++. 675 IsImplementation = (CT.getRunTimeLang() == 0) || 676 CT.isObjcClassComplete(); 677 } 678 unsigned Flags = IsImplementation ? 679 DwarfAccelTable::eTypeFlagClassIsImplementation : 0; 680 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags)); 681 } 682 683 addToContextOwner(TyDIE, Ty.getContext()); 684 return TyDIE; 685 } 686 687 /// addType - Add a new type attribute to the specified entity. 688 void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) { 689 if (!Ty.Verify()) 690 return; 691 692 // Check for pre-existence. 693 DIEEntry *Entry = getDIEEntry(Ty); 694 // If it exists then use the existing value. 695 if (Entry) { 696 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry); 697 return; 698 } 699 700 // Construct type. 701 DIE *Buffer = getOrCreateTypeDIE(Ty); 702 703 // Set up proxy. 704 Entry = createDIEEntry(Buffer); 705 insertDIEEntry(Ty, Entry); 706 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry); 707 708 // If this is a complete composite type then include it in the 709 // list of global types. 710 addGlobalType(Ty); 711 } 712 713 /// addGlobalType - Add a new global type to the compile unit. 714 /// 715 void CompileUnit::addGlobalType(DIType Ty) { 716 DIDescriptor Context = Ty.getContext(); 717 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl() 718 && (!Context || Context.isCompileUnit() || Context.isFile() 719 || Context.isNameSpace())) 720 if (DIEEntry *Entry = getDIEEntry(Ty)) 721 GlobalTypes[Ty.getName()] = Entry->getEntry(); 722 } 723 724 /// addPubTypes - Add type for pubtypes section. 725 void CompileUnit::addPubTypes(DISubprogram SP) { 726 DICompositeType SPTy = SP.getType(); 727 unsigned SPTag = SPTy.getTag(); 728 if (SPTag != dwarf::DW_TAG_subroutine_type) 729 return; 730 731 DIArray Args = SPTy.getTypeArray(); 732 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { 733 DIType ATy(Args.getElement(i)); 734 if (!ATy.Verify()) 735 continue; 736 addGlobalType(ATy); 737 } 738 } 739 740 /// constructTypeDIE - Construct basic type die from DIBasicType. 741 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { 742 // Get core information. 743 StringRef Name = BTy.getName(); 744 // Add name if not anonymous or intermediate type. 745 if (!Name.empty()) 746 addString(&Buffer, dwarf::DW_AT_name, Name); 747 748 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) { 749 Buffer.setTag(dwarf::DW_TAG_unspecified_type); 750 // Unspecified types has only name, nothing else. 751 return; 752 } 753 754 Buffer.setTag(dwarf::DW_TAG_base_type); 755 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 756 BTy.getEncoding()); 757 758 uint64_t Size = BTy.getSizeInBits() >> 3; 759 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 760 } 761 762 /// constructTypeDIE - Construct derived type die from DIDerivedType. 763 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { 764 // Get core information. 765 StringRef Name = DTy.getName(); 766 uint64_t Size = DTy.getSizeInBits() >> 3; 767 unsigned Tag = DTy.getTag(); 768 769 // FIXME - Workaround for templates. 770 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; 771 772 Buffer.setTag(Tag); 773 774 // Map to main type, void will not have a type. 775 DIType FromTy = DTy.getTypeDerivedFrom(); 776 addType(&Buffer, FromTy); 777 778 // Add name if not anonymous or intermediate type. 779 if (!Name.empty()) 780 addString(&Buffer, dwarf::DW_AT_name, Name); 781 782 // Add size if non-zero (derived types might be zero-sized.) 783 if (Size && Tag != dwarf::DW_TAG_pointer_type) 784 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 785 786 // Add source line info if available and TyDesc is not a forward declaration. 787 if (!DTy.isForwardDecl()) 788 addSourceLine(&Buffer, DTy); 789 } 790 791 /// constructTypeDIE - Construct type DIE from DICompositeType. 792 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { 793 // Get core information. 794 StringRef Name = CTy.getName(); 795 796 uint64_t Size = CTy.getSizeInBits() >> 3; 797 unsigned Tag = CTy.getTag(); 798 Buffer.setTag(Tag); 799 800 switch (Tag) { 801 case dwarf::DW_TAG_vector_type: 802 case dwarf::DW_TAG_array_type: 803 constructArrayTypeDIE(Buffer, &CTy); 804 break; 805 case dwarf::DW_TAG_enumeration_type: { 806 DIArray Elements = CTy.getTypeArray(); 807 808 // Add enumerators to enumeration type. 809 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 810 DIE *ElemDie = NULL; 811 DIDescriptor Enum(Elements.getElement(i)); 812 if (Enum.isEnumerator()) { 813 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum)); 814 Buffer.addChild(ElemDie); 815 } 816 } 817 DIType DTy = CTy.getTypeDerivedFrom(); 818 if (DTy.Verify()) { 819 addType(&Buffer, DTy); 820 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1); 821 } 822 } 823 break; 824 case dwarf::DW_TAG_subroutine_type: { 825 // Add return type. 826 DIArray Elements = CTy.getTypeArray(); 827 DIDescriptor RTy = Elements.getElement(0); 828 addType(&Buffer, DIType(RTy)); 829 830 bool isPrototyped = true; 831 // Add arguments. 832 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { 833 DIDescriptor Ty = Elements.getElement(i); 834 if (Ty.isUnspecifiedParameter()) { 835 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters); 836 Buffer.addChild(Arg); 837 isPrototyped = false; 838 } else { 839 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 840 addType(Arg, DIType(Ty)); 841 Buffer.addChild(Arg); 842 } 843 } 844 // Add prototype flag if we're dealing with a C language and the 845 // function has been prototyped. 846 if (isPrototyped && 847 (Language == dwarf::DW_LANG_C89 || 848 Language == dwarf::DW_LANG_C99 || 849 Language == dwarf::DW_LANG_ObjC)) 850 addFlag(&Buffer, dwarf::DW_AT_prototyped); 851 } 852 break; 853 case dwarf::DW_TAG_structure_type: 854 case dwarf::DW_TAG_union_type: 855 case dwarf::DW_TAG_class_type: { 856 // Add elements to structure type. 857 DIArray Elements = CTy.getTypeArray(); 858 859 // A forward struct declared type may not have elements available. 860 unsigned N = Elements.getNumElements(); 861 if (N == 0) 862 break; 863 864 // Add elements to structure type. 865 for (unsigned i = 0; i < N; ++i) { 866 DIDescriptor Element = Elements.getElement(i); 867 DIE *ElemDie = NULL; 868 if (Element.isSubprogram()) { 869 DISubprogram SP(Element); 870 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element)); 871 if (SP.isProtected()) 872 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 873 dwarf::DW_ACCESS_protected); 874 else if (SP.isPrivate()) 875 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 876 dwarf::DW_ACCESS_private); 877 else 878 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 879 dwarf::DW_ACCESS_public); 880 if (SP.isExplicit()) 881 addFlag(ElemDie, dwarf::DW_AT_explicit); 882 } 883 else if (Element.isVariable()) { 884 DIVariable DV(Element); 885 ElemDie = new DIE(dwarf::DW_TAG_variable); 886 addString(ElemDie, dwarf::DW_AT_name, DV.getName()); 887 addType(ElemDie, DV.getType()); 888 addFlag(ElemDie, dwarf::DW_AT_declaration); 889 addFlag(ElemDie, dwarf::DW_AT_external); 890 addSourceLine(ElemDie, DV); 891 } else if (Element.isDerivedType()) { 892 DIDerivedType DDTy(Element); 893 if (DDTy.getTag() == dwarf::DW_TAG_friend) { 894 ElemDie = new DIE(dwarf::DW_TAG_friend); 895 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend); 896 } else 897 ElemDie = createMemberDIE(DIDerivedType(Element)); 898 } else if (Element.isObjCProperty()) { 899 DIObjCProperty Property(Element); 900 ElemDie = new DIE(Property.getTag()); 901 StringRef PropertyName = Property.getObjCPropertyName(); 902 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 903 addType(ElemDie, Property.getType()); 904 addSourceLine(ElemDie, Property); 905 StringRef GetterName = Property.getObjCPropertyGetterName(); 906 if (!GetterName.empty()) 907 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 908 StringRef SetterName = Property.getObjCPropertySetterName(); 909 if (!SetterName.empty()) 910 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 911 unsigned PropertyAttributes = 0; 912 if (Property.isReadOnlyObjCProperty()) 913 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; 914 if (Property.isReadWriteObjCProperty()) 915 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; 916 if (Property.isAssignObjCProperty()) 917 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; 918 if (Property.isRetainObjCProperty()) 919 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; 920 if (Property.isCopyObjCProperty()) 921 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; 922 if (Property.isNonAtomicObjCProperty()) 923 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; 924 if (PropertyAttributes) 925 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0, 926 PropertyAttributes); 927 928 DIEEntry *Entry = getDIEEntry(Element); 929 if (!Entry) { 930 Entry = createDIEEntry(ElemDie); 931 insertDIEEntry(Element, Entry); 932 } 933 } else 934 continue; 935 Buffer.addChild(ElemDie); 936 } 937 938 if (CTy.isAppleBlockExtension()) 939 addFlag(&Buffer, dwarf::DW_AT_APPLE_block); 940 941 DICompositeType ContainingType = CTy.getContainingType(); 942 if (DIDescriptor(ContainingType).isCompositeType()) 943 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, 944 getOrCreateTypeDIE(DIType(ContainingType))); 945 else { 946 DIDescriptor Context = CTy.getContext(); 947 addToContextOwner(&Buffer, Context); 948 } 949 950 if (CTy.isObjcClassComplete()) 951 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 952 953 // Add template parameters to a class, structure or union types. 954 // FIXME: The support isn't in the metadata for this yet. 955 if (Tag == dwarf::DW_TAG_class_type || 956 Tag == dwarf::DW_TAG_structure_type || 957 Tag == dwarf::DW_TAG_union_type) 958 addTemplateParams(Buffer, CTy.getTemplateParams()); 959 960 break; 961 } 962 default: 963 break; 964 } 965 966 // Add name if not anonymous or intermediate type. 967 if (!Name.empty()) 968 addString(&Buffer, dwarf::DW_AT_name, Name); 969 970 if (Tag == dwarf::DW_TAG_enumeration_type || 971 Tag == dwarf::DW_TAG_class_type || 972 Tag == dwarf::DW_TAG_structure_type || 973 Tag == dwarf::DW_TAG_union_type) { 974 // Add size if non-zero (derived types might be zero-sized.) 975 // TODO: Do we care about size for enum forward declarations? 976 if (Size) 977 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 978 else if (!CTy.isForwardDecl()) 979 // Add zero size if it is not a forward declaration. 980 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); 981 982 // If we're a forward decl, say so. 983 if (CTy.isForwardDecl()) 984 addFlag(&Buffer, dwarf::DW_AT_declaration); 985 986 // Add source line info if available. 987 if (!CTy.isForwardDecl()) 988 addSourceLine(&Buffer, CTy); 989 990 // No harm in adding the runtime language to the declaration. 991 unsigned RLang = CTy.getRunTimeLang(); 992 if (RLang) 993 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, 994 dwarf::DW_FORM_data1, RLang); 995 } 996 } 997 998 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE 999 /// for the given DITemplateTypeParameter. 1000 DIE * 1001 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) { 1002 DIE *ParamDIE = getDIE(TP); 1003 if (ParamDIE) 1004 return ParamDIE; 1005 1006 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter); 1007 addType(ParamDIE, TP.getType()); 1008 addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); 1009 return ParamDIE; 1010 } 1011 1012 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE 1013 /// for the given DITemplateValueParameter. 1014 DIE * 1015 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){ 1016 DIE *ParamDIE = getDIE(TPV); 1017 if (ParamDIE) 1018 return ParamDIE; 1019 1020 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter); 1021 addType(ParamDIE, TPV.getType()); 1022 if (!TPV.getName().empty()) 1023 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName()); 1024 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, 1025 TPV.getValue()); 1026 return ParamDIE; 1027 } 1028 1029 /// getOrCreateNameSpace - Create a DIE for DINameSpace. 1030 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) { 1031 DIE *NDie = getDIE(NS); 1032 if (NDie) 1033 return NDie; 1034 NDie = new DIE(dwarf::DW_TAG_namespace); 1035 insertDIE(NS, NDie); 1036 if (!NS.getName().empty()) { 1037 addString(NDie, dwarf::DW_AT_name, NS.getName()); 1038 addAccelNamespace(NS.getName(), NDie); 1039 } else 1040 addAccelNamespace("(anonymous namespace)", NDie); 1041 addSourceLine(NDie, NS); 1042 addToContextOwner(NDie, NS.getContext()); 1043 return NDie; 1044 } 1045 1046 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm 1047 /// printer to not emit usual symbol prefix before the symbol name is used then 1048 /// return linkage name after skipping this special LLVM prefix. 1049 static StringRef getRealLinkageName(StringRef LinkageName) { 1050 char One = '\1'; 1051 if (LinkageName.startswith(StringRef(&One, 1))) 1052 return LinkageName.substr(1); 1053 return LinkageName; 1054 } 1055 1056 /// getOrCreateSubprogramDIE - Create new DIE using SP. 1057 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) { 1058 DIE *SPDie = getDIE(SP); 1059 if (SPDie) 1060 return SPDie; 1061 1062 SPDie = new DIE(dwarf::DW_TAG_subprogram); 1063 1064 // DW_TAG_inlined_subroutine may refer to this DIE. 1065 insertDIE(SP, SPDie); 1066 1067 DISubprogram SPDecl = SP.getFunctionDeclaration(); 1068 DIE *DeclDie = NULL; 1069 if (SPDecl.isSubprogram()) { 1070 DeclDie = getOrCreateSubprogramDIE(SPDecl); 1071 } 1072 1073 // Add to context owner. 1074 addToContextOwner(SPDie, SP.getContext()); 1075 1076 // Add function template parameters. 1077 addTemplateParams(*SPDie, SP.getTemplateParams()); 1078 1079 // Unfortunately this code needs to stay here instead of below the 1080 // AT_specification code in order to work around a bug in older 1081 // gdbs that requires the linkage name to resolve multiple template 1082 // functions. 1083 // TODO: Remove this set of code when we get rid of the old gdb 1084 // compatibility. 1085 StringRef LinkageName = SP.getLinkageName(); 1086 if (!LinkageName.empty() && DD->useDarwinGDBCompat()) 1087 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, 1088 getRealLinkageName(LinkageName)); 1089 1090 // If this DIE is going to refer declaration info using AT_specification 1091 // then there is no need to add other attributes. 1092 if (DeclDie) { 1093 // Refer function declaration directly. 1094 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4, 1095 DeclDie); 1096 1097 return SPDie; 1098 } 1099 1100 // Add the linkage name if we have one. 1101 if (!LinkageName.empty() && !DD->useDarwinGDBCompat()) 1102 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, 1103 getRealLinkageName(LinkageName)); 1104 1105 // Constructors and operators for anonymous aggregates do not have names. 1106 if (!SP.getName().empty()) 1107 addString(SPDie, dwarf::DW_AT_name, SP.getName()); 1108 1109 addSourceLine(SPDie, SP); 1110 1111 // Add the prototype if we have a prototype and we have a C like 1112 // language. 1113 if (SP.isPrototyped() && 1114 (Language == dwarf::DW_LANG_C89 || 1115 Language == dwarf::DW_LANG_C99 || 1116 Language == dwarf::DW_LANG_ObjC)) 1117 addFlag(SPDie, dwarf::DW_AT_prototyped); 1118 1119 // Add Return Type. 1120 DICompositeType SPTy = SP.getType(); 1121 DIArray Args = SPTy.getTypeArray(); 1122 unsigned SPTag = SPTy.getTag(); 1123 1124 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type) 1125 addType(SPDie, SPTy); 1126 else 1127 addType(SPDie, DIType(Args.getElement(0))); 1128 1129 unsigned VK = SP.getVirtuality(); 1130 if (VK) { 1131 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 1132 DIEBlock *Block = getDIEBlock(); 1133 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1134 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex()); 1135 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block); 1136 ContainingTypeMap.insert(std::make_pair(SPDie, 1137 SP.getContainingType())); 1138 } 1139 1140 if (!SP.isDefinition()) { 1141 addFlag(SPDie, dwarf::DW_AT_declaration); 1142 1143 // Add arguments. Do not add arguments for subprogram definition. They will 1144 // be handled while processing variables. 1145 DICompositeType SPTy = SP.getType(); 1146 DIArray Args = SPTy.getTypeArray(); 1147 unsigned SPTag = SPTy.getTag(); 1148 1149 if (SPTag == dwarf::DW_TAG_subroutine_type) 1150 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 1151 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 1152 DIType ATy = DIType(Args.getElement(i)); 1153 addType(Arg, ATy); 1154 if (ATy.isArtificial()) 1155 addFlag(Arg, dwarf::DW_AT_artificial); 1156 SPDie->addChild(Arg); 1157 } 1158 } 1159 1160 if (SP.isArtificial()) 1161 addFlag(SPDie, dwarf::DW_AT_artificial); 1162 1163 if (!SP.isLocalToUnit()) 1164 addFlag(SPDie, dwarf::DW_AT_external); 1165 1166 if (SP.isOptimized()) 1167 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 1168 1169 if (unsigned isa = Asm->getISAEncoding()) { 1170 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 1171 } 1172 1173 return SPDie; 1174 } 1175 1176 // Return const expression if value is a GEP to access merged global 1177 // constant. e.g. 1178 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) 1179 static const ConstantExpr *getMergedGlobalExpr(const Value *V) { 1180 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); 1181 if (!CE || CE->getNumOperands() != 3 || 1182 CE->getOpcode() != Instruction::GetElementPtr) 1183 return NULL; 1184 1185 // First operand points to a global struct. 1186 Value *Ptr = CE->getOperand(0); 1187 if (!isa<GlobalValue>(Ptr) || 1188 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) 1189 return NULL; 1190 1191 // Second operand is zero. 1192 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); 1193 if (!CI || !CI->isZero()) 1194 return NULL; 1195 1196 // Third operand is offset. 1197 if (!isa<ConstantInt>(CE->getOperand(2))) 1198 return NULL; 1199 1200 return CE; 1201 } 1202 1203 /// createGlobalVariableDIE - create global variable DIE. 1204 void CompileUnit::createGlobalVariableDIE(const MDNode *N) { 1205 // Check for pre-existence. 1206 if (getDIE(N)) 1207 return; 1208 1209 DIGlobalVariable GV(N); 1210 if (!GV.Verify()) 1211 return; 1212 1213 DIE *VariableDIE = new DIE(GV.getTag()); 1214 // Add to map. 1215 insertDIE(N, VariableDIE); 1216 1217 // Add name. 1218 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); 1219 StringRef LinkageName = GV.getLinkageName(); 1220 bool isGlobalVariable = GV.getGlobal() != NULL; 1221 if (!LinkageName.empty() && isGlobalVariable) 1222 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, 1223 getRealLinkageName(LinkageName)); 1224 // Add type. 1225 DIType GTy = GV.getType(); 1226 addType(VariableDIE, GTy); 1227 1228 // Add scoping info. 1229 if (!GV.isLocalToUnit()) 1230 addFlag(VariableDIE, dwarf::DW_AT_external); 1231 1232 // Add line number info. 1233 addSourceLine(VariableDIE, GV); 1234 // Add to context owner. 1235 DIDescriptor GVContext = GV.getContext(); 1236 addToContextOwner(VariableDIE, GVContext); 1237 // Add location. 1238 bool addToAccelTable = false; 1239 DIE *VariableSpecDIE = NULL; 1240 if (isGlobalVariable) { 1241 addToAccelTable = true; 1242 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 1243 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 1244 addLabel(Block, 0, dwarf::DW_FORM_udata, 1245 Asm->Mang->getSymbol(GV.getGlobal())); 1246 // Do not create specification DIE if context is either compile unit 1247 // or a subprogram. 1248 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && 1249 !GVContext.isFile() && !isSubprogramContext(GVContext)) { 1250 // Create specification DIE. 1251 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable); 1252 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, 1253 dwarf::DW_FORM_ref4, VariableDIE); 1254 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block); 1255 addFlag(VariableDIE, dwarf::DW_AT_declaration); 1256 addDie(VariableSpecDIE); 1257 } else { 1258 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block); 1259 } 1260 } else if (const ConstantInt *CI = 1261 dyn_cast_or_null<ConstantInt>(GV.getConstant())) 1262 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType()); 1263 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) { 1264 addToAccelTable = true; 1265 // GV is a merged global. 1266 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 1267 Value *Ptr = CE->getOperand(0); 1268 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 1269 addLabel(Block, 0, dwarf::DW_FORM_udata, 1270 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr))); 1271 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1272 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end()); 1273 addUInt(Block, 0, dwarf::DW_FORM_udata, 1274 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); 1275 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1276 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block); 1277 } 1278 1279 if (addToAccelTable) { 1280 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; 1281 addAccelName(GV.getName(), AddrDIE); 1282 1283 // If the linkage name is different than the name, go ahead and output 1284 // that as well into the name table. 1285 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) 1286 addAccelName(GV.getLinkageName(), AddrDIE); 1287 } 1288 1289 return; 1290 } 1291 1292 /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 1293 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, 1294 DIE *IndexTy) { 1295 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); 1296 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); 1297 1298 // The LowerBound value defines the lower bounds which is typically zero for 1299 // C/C++. The Count value is the number of elements. Values are 64 bit. If 1300 // Count == -1 then the array is unbounded and we do not emit 1301 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and 1302 // Count == 0, then the array has zero elements in which case we do not emit 1303 // an upper bound. 1304 int64_t LowerBound = SR.getLo(); 1305 int64_t DefaultLowerBound = getDefaultLowerBound(); 1306 int64_t Count = SR.getCount(); 1307 1308 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) 1309 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound); 1310 1311 if (Count != -1 && Count != 0) 1312 // FIXME: An unbounded array should reference the expression that defines 1313 // the array. 1314 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1); 1315 1316 Buffer.addChild(DW_Subrange); 1317 } 1318 1319 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 1320 void CompileUnit::constructArrayTypeDIE(DIE &Buffer, 1321 DICompositeType *CTy) { 1322 Buffer.setTag(dwarf::DW_TAG_array_type); 1323 if (CTy->getTag() == dwarf::DW_TAG_vector_type) 1324 addFlag(&Buffer, dwarf::DW_AT_GNU_vector); 1325 1326 // Emit derived type. 1327 addType(&Buffer, CTy->getTypeDerivedFrom()); 1328 DIArray Elements = CTy->getTypeArray(); 1329 1330 // Get an anonymous type for index type. 1331 DIE *IdxTy = getIndexTyDie(); 1332 if (!IdxTy) { 1333 // Construct an anonymous type for index type. 1334 IdxTy = new DIE(dwarf::DW_TAG_base_type); 1335 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); 1336 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1337 dwarf::DW_ATE_signed); 1338 addDie(IdxTy); 1339 setIndexTyDie(IdxTy); 1340 } 1341 1342 // Add subranges to array type. 1343 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1344 DIDescriptor Element = Elements.getElement(i); 1345 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 1346 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); 1347 } 1348 } 1349 1350 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 1351 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) { 1352 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); 1353 StringRef Name = ETy.getName(); 1354 addString(Enumerator, dwarf::DW_AT_name, Name); 1355 int64_t Value = ETy.getEnumValue(); 1356 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); 1357 return Enumerator; 1358 } 1359 1360 /// constructContainingTypeDIEs - Construct DIEs for types that contain 1361 /// vtables. 1362 void CompileUnit::constructContainingTypeDIEs() { 1363 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(), 1364 CE = ContainingTypeMap.end(); CI != CE; ++CI) { 1365 DIE *SPDie = CI->first; 1366 const MDNode *N = CI->second; 1367 if (!N) continue; 1368 DIE *NDie = getDIE(N); 1369 if (!NDie) continue; 1370 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie); 1371 } 1372 } 1373 1374 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 1375 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) { 1376 StringRef Name = DV->getName(); 1377 if (Name.empty()) 1378 return NULL; 1379 1380 // Translate tag to proper Dwarf tag. 1381 unsigned Tag = DV->getTag(); 1382 1383 // Define variable debug information entry. 1384 DIE *VariableDie = new DIE(Tag); 1385 DbgVariable *AbsVar = DV->getAbstractVariable(); 1386 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL; 1387 if (AbsDIE) 1388 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, 1389 dwarf::DW_FORM_ref4, AbsDIE); 1390 else { 1391 addString(VariableDie, dwarf::DW_AT_name, Name); 1392 addSourceLine(VariableDie, DV->getVariable()); 1393 addType(VariableDie, DV->getType()); 1394 } 1395 1396 if (DV->isArtificial()) 1397 addFlag(VariableDie, dwarf::DW_AT_artificial); 1398 1399 if (isScopeAbstract) { 1400 DV->setDIE(VariableDie); 1401 return VariableDie; 1402 } 1403 1404 // Add variable address. 1405 1406 unsigned Offset = DV->getDotDebugLocOffset(); 1407 if (Offset != ~0U) { 1408 addLabel(VariableDie, dwarf::DW_AT_location, 1409 dwarf::DW_FORM_data4, 1410 Asm->GetTempSymbol("debug_loc", Offset)); 1411 DV->setDIE(VariableDie); 1412 return VariableDie; 1413 } 1414 1415 // Check if variable is described by a DBG_VALUE instruction. 1416 if (const MachineInstr *DVInsn = DV->getMInsn()) { 1417 bool updated = false; 1418 if (DVInsn->getNumOperands() == 3) { 1419 if (DVInsn->getOperand(0).isReg()) { 1420 const MachineOperand RegOp = DVInsn->getOperand(0); 1421 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); 1422 if (DVInsn->getOperand(1).isImm() && 1423 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) { 1424 unsigned FrameReg = 0; 1425 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); 1426 int Offset = 1427 TFI->getFrameIndexReference(*Asm->MF, 1428 DVInsn->getOperand(1).getImm(), 1429 FrameReg); 1430 MachineLocation Location(FrameReg, Offset); 1431 addVariableAddress(DV, VariableDie, Location); 1432 1433 } else if (RegOp.getReg()) 1434 addVariableAddress(DV, VariableDie, 1435 MachineLocation(RegOp.getReg())); 1436 updated = true; 1437 } 1438 else if (DVInsn->getOperand(0).isImm()) 1439 updated = 1440 addConstantValue(VariableDie, DVInsn->getOperand(0), 1441 DV->getType()); 1442 else if (DVInsn->getOperand(0).isFPImm()) 1443 updated = 1444 addConstantFPValue(VariableDie, DVInsn->getOperand(0)); 1445 else if (DVInsn->getOperand(0).isCImm()) 1446 updated = 1447 addConstantValue(VariableDie, 1448 DVInsn->getOperand(0).getCImm(), 1449 DV->getType().isUnsignedDIType()); 1450 } else { 1451 addVariableAddress(DV, VariableDie, 1452 Asm->getDebugValueLocation(DVInsn)); 1453 updated = true; 1454 } 1455 if (!updated) { 1456 // If variableDie is not updated then DBG_VALUE instruction does not 1457 // have valid variable info. 1458 delete VariableDie; 1459 return NULL; 1460 } 1461 DV->setDIE(VariableDie); 1462 return VariableDie; 1463 } else { 1464 // .. else use frame index. 1465 int FI = DV->getFrameIndex(); 1466 if (FI != ~0) { 1467 unsigned FrameReg = 0; 1468 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); 1469 int Offset = 1470 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 1471 MachineLocation Location(FrameReg, Offset); 1472 addVariableAddress(DV, VariableDie, Location); 1473 } 1474 } 1475 1476 DV->setDIE(VariableDie); 1477 return VariableDie; 1478 } 1479 1480 /// createMemberDIE - Create new member DIE. 1481 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) { 1482 DIE *MemberDie = new DIE(DT.getTag()); 1483 StringRef Name = DT.getName(); 1484 if (!Name.empty()) 1485 addString(MemberDie, dwarf::DW_AT_name, Name); 1486 1487 addType(MemberDie, DT.getTypeDerivedFrom()); 1488 1489 addSourceLine(MemberDie, DT); 1490 1491 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); 1492 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1493 1494 uint64_t Size = DT.getSizeInBits(); 1495 uint64_t FieldSize = DT.getOriginalTypeSize(); 1496 1497 if (Size != FieldSize) { 1498 // Handle bitfield. 1499 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); 1500 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); 1501 1502 uint64_t Offset = DT.getOffsetInBits(); 1503 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 1504 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1505 uint64_t FieldOffset = (HiMark - FieldSize); 1506 Offset -= FieldOffset; 1507 1508 // Maybe we need to work from the other end. 1509 if (Asm->getDataLayout().isLittleEndian()) 1510 Offset = FieldSize - (Offset + Size); 1511 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); 1512 1513 // Here WD_AT_data_member_location points to the anonymous 1514 // field that includes this bit field. 1515 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3); 1516 1517 } else 1518 // This is not a bitfield. 1519 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); 1520 1521 if (DT.getTag() == dwarf::DW_TAG_inheritance 1522 && DT.isVirtual()) { 1523 1524 // For C++, virtual base classes are not at fixed offset. Use following 1525 // expression to extract appropriate offset from vtable. 1526 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 1527 1528 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); 1529 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 1530 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1531 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1532 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits()); 1533 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 1534 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1535 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1536 1537 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, 1538 VBaseLocationDie); 1539 } else 1540 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie); 1541 1542 if (DT.isProtected()) 1543 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1544 dwarf::DW_ACCESS_protected); 1545 else if (DT.isPrivate()) 1546 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1547 dwarf::DW_ACCESS_private); 1548 // Otherwise C++ member and base classes are considered public. 1549 else 1550 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1551 dwarf::DW_ACCESS_public); 1552 if (DT.isVirtual()) 1553 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 1554 dwarf::DW_VIRTUALITY_virtual); 1555 1556 // Objective-C properties. 1557 if (MDNode *PNode = DT.getObjCProperty()) 1558 if (DIEEntry *PropertyDie = getDIEEntry(PNode)) 1559 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, 1560 PropertyDie); 1561 1562 // This is only for backward compatibility. 1563 StringRef PropertyName = DT.getObjCPropertyName(); 1564 if (!PropertyName.empty()) { 1565 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 1566 StringRef GetterName = DT.getObjCPropertyGetterName(); 1567 if (!GetterName.empty()) 1568 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 1569 StringRef SetterName = DT.getObjCPropertySetterName(); 1570 if (!SetterName.empty()) 1571 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 1572 unsigned PropertyAttributes = 0; 1573 if (DT.isReadOnlyObjCProperty()) 1574 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; 1575 if (DT.isReadWriteObjCProperty()) 1576 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; 1577 if (DT.isAssignObjCProperty()) 1578 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; 1579 if (DT.isRetainObjCProperty()) 1580 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; 1581 if (DT.isCopyObjCProperty()) 1582 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; 1583 if (DT.isNonAtomicObjCProperty()) 1584 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; 1585 if (PropertyAttributes) 1586 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0, 1587 PropertyAttributes); 1588 } 1589 return MemberDie; 1590 } 1591