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 writing dwarf compile unit. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #define DEBUG_TYPE "dwarfdebug" 15 16 #include "DwarfCompileUnit.h" 17 #include "DwarfDebug.h" 18 #include "llvm/Constants.h" 19 #include "llvm/Analysis/DIBuilder.h" 20 #include "llvm/Target/TargetData.h" 21 #include "llvm/Target/TargetFrameLowering.h" 22 #include "llvm/Target/TargetMachine.h" 23 #include "llvm/Target/TargetRegisterInfo.h" 24 #include "llvm/ADT/APFloat.h" 25 #include "llvm/Support/ErrorHandling.h" 26 27 using namespace llvm; 28 29 /// CompileUnit - Compile unit constructor. 30 CompileUnit::CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW) 31 : ID(I), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) { 32 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); 33 } 34 35 /// ~CompileUnit - Destructor for compile unit. 36 CompileUnit::~CompileUnit() { 37 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) 38 DIEBlocks[j]->~DIEBlock(); 39 } 40 41 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 42 /// information entry. 43 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) { 44 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); 45 return Value; 46 } 47 48 /// addUInt - Add an unsigned integer attribute data and value. 49 /// 50 void CompileUnit::addUInt(DIE *Die, unsigned Attribute, 51 unsigned Form, uint64_t Integer) { 52 if (!Form) Form = DIEInteger::BestForm(false, Integer); 53 DIEValue *Value = Integer == 1 ? 54 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer); 55 Die->addValue(Attribute, Form, Value); 56 } 57 58 /// addSInt - Add an signed integer attribute data and value. 59 /// 60 void CompileUnit::addSInt(DIE *Die, unsigned Attribute, 61 unsigned Form, int64_t Integer) { 62 if (!Form) Form = DIEInteger::BestForm(true, Integer); 63 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); 64 Die->addValue(Attribute, Form, Value); 65 } 66 67 /// addString - Add a string attribute data and value. DIEString only 68 /// keeps string reference. 69 void CompileUnit::addString(DIE *Die, unsigned Attribute, unsigned Form, 70 StringRef String) { 71 DIEValue *Value = new (DIEValueAllocator) DIEString(String); 72 Die->addValue(Attribute, Form, Value); 73 } 74 75 /// addLabel - Add a Dwarf label attribute data and value. 76 /// 77 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form, 78 const MCSymbol *Label) { 79 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); 80 Die->addValue(Attribute, Form, Value); 81 } 82 83 /// addDelta - Add a label delta attribute data and value. 84 /// 85 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form, 86 const MCSymbol *Hi, const MCSymbol *Lo) { 87 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 88 Die->addValue(Attribute, Form, Value); 89 } 90 91 /// addDIEEntry - Add a DIE attribute data and value. 92 /// 93 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, 94 DIE *Entry) { 95 Die->addValue(Attribute, Form, createDIEEntry(Entry)); 96 } 97 98 99 /// addBlock - Add block data. 100 /// 101 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form, 102 DIEBlock *Block) { 103 Block->ComputeSize(Asm); 104 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. 105 Die->addValue(Attribute, Block->BestForm(), Block); 106 } 107 108 /// addSourceLine - Add location information to specified debug information 109 /// entry. 110 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) { 111 // Verify variable. 112 if (!V.Verify()) 113 return; 114 115 unsigned Line = V.getLineNumber(); 116 if (Line == 0) 117 return; 118 unsigned FileID = DD->GetOrCreateSourceID(V.getContext().getFilename(), 119 V.getContext().getDirectory()); 120 assert(FileID && "Invalid file id"); 121 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 122 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 123 } 124 125 /// addSourceLine - Add location information to specified debug information 126 /// entry. 127 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { 128 // Verify global variable. 129 if (!G.Verify()) 130 return; 131 132 unsigned Line = G.getLineNumber(); 133 if (Line == 0) 134 return; 135 unsigned FileID = DD->GetOrCreateSourceID(G.getContext().getFilename(), 136 G.getContext().getDirectory()); 137 assert(FileID && "Invalid file id"); 138 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 139 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 140 } 141 142 /// addSourceLine - Add location information to specified debug information 143 /// entry. 144 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) { 145 // Verify subprogram. 146 if (!SP.Verify()) 147 return; 148 // If the line number is 0, don't add it. 149 if (SP.getLineNumber() == 0) 150 return; 151 152 unsigned Line = SP.getLineNumber(); 153 if (!SP.getContext().Verify()) 154 return; 155 unsigned FileID = DD->GetOrCreateSourceID(SP.getFilename(), SP.getDirectory()); 156 assert(FileID && "Invalid file id"); 157 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 158 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 159 } 160 161 /// addSourceLine - Add location information to specified debug information 162 /// entry. 163 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) { 164 // Verify type. 165 if (!Ty.Verify()) 166 return; 167 168 unsigned Line = Ty.getLineNumber(); 169 if (Line == 0 || !Ty.getContext().Verify()) 170 return; 171 unsigned FileID = DD->GetOrCreateSourceID(Ty.getFilename(), Ty.getDirectory()); 172 assert(FileID && "Invalid file id"); 173 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 174 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 175 } 176 177 /// addSourceLine - Add location information to specified debug information 178 /// entry. 179 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) { 180 // Verify namespace. 181 if (!NS.Verify()) 182 return; 183 184 unsigned Line = NS.getLineNumber(); 185 if (Line == 0) 186 return; 187 StringRef FN = NS.getFilename(); 188 189 unsigned FileID = DD->GetOrCreateSourceID(FN, NS.getDirectory()); 190 assert(FileID && "Invalid file id"); 191 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); 192 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); 193 } 194 195 /// addVariableAddress - Add DW_AT_location attribute for a 196 /// DbgVariable based on provided MachineLocation. 197 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die, 198 MachineLocation Location) { 199 if (DV->variableHasComplexAddress()) 200 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 201 else if (DV->isBlockByrefVariable()) 202 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 203 else 204 addAddress(Die, dwarf::DW_AT_location, Location); 205 } 206 207 /// addRegisterOp - Add register operand. 208 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) { 209 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 210 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 211 if (DWReg < 32) 212 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); 213 else { 214 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 215 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg); 216 } 217 } 218 219 /// addRegisterOffset - Add register offset. 220 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg, 221 int64_t Offset) { 222 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 223 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 224 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); 225 if (Reg == TRI->getFrameRegister(*Asm->MF)) 226 // If variable offset is based in frame register then use fbreg. 227 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); 228 else if (DWReg < 32) 229 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); 230 else { 231 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 232 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg); 233 } 234 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset); 235 } 236 237 /// addAddress - Add an address attribute to a die based on the location 238 /// provided. 239 void CompileUnit::addAddress(DIE *Die, unsigned Attribute, 240 const MachineLocation &Location) { 241 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 242 243 if (Location.isReg()) 244 addRegisterOp(Block, Location.getReg()); 245 else 246 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 247 248 // Now attach the location information to the DIE. 249 addBlock(Die, Attribute, 0, Block); 250 } 251 252 /// addComplexAddress - Start with the address based on the location provided, 253 /// and generate the DWARF information necessary to find the actual variable 254 /// given the extra address information encoded in the DIVariable, starting from 255 /// the starting location. Add the DWARF information to the die. 256 /// 257 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die, 258 unsigned Attribute, 259 const MachineLocation &Location) { 260 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 261 unsigned N = DV->getNumAddrElements(); 262 unsigned i = 0; 263 if (Location.isReg()) { 264 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) { 265 // If first address element is OpPlus then emit 266 // DW_OP_breg + Offset instead of DW_OP_reg + Offset. 267 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1)); 268 i = 2; 269 } else 270 addRegisterOp(Block, Location.getReg()); 271 } 272 else 273 addRegisterOffset(Block, Location.getReg(), Location.getOffset()); 274 275 for (;i < N; ++i) { 276 uint64_t Element = DV->getAddrElement(i); 277 if (Element == DIBuilder::OpPlus) { 278 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 279 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i)); 280 } else if (Element == DIBuilder::OpDeref) { 281 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 282 } else llvm_unreachable("unknown DIBuilder Opcode"); 283 } 284 285 // Now attach the location information to the DIE. 286 addBlock(Die, Attribute, 0, Block); 287 } 288 289 /* Byref variables, in Blocks, are declared by the programmer as "SomeType 290 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and 291 gives the variable VarName either the struct, or a pointer to the struct, as 292 its type. This is necessary for various behind-the-scenes things the 293 compiler needs to do with by-reference variables in Blocks. 294 295 However, as far as the original *programmer* is concerned, the variable 296 should still have type 'SomeType', as originally declared. 297 298 The function getBlockByrefType dives into the __Block_byref_x_VarName 299 struct to find the original type of the variable, which is then assigned to 300 the variable's Debug Information Entry as its real type. So far, so good. 301 However now the debugger will expect the variable VarName to have the type 302 SomeType. So we need the location attribute for the variable to be an 303 expression that explains to the debugger how to navigate through the 304 pointers and struct to find the actual variable of type SomeType. 305 306 The following function does just that. We start by getting 307 the "normal" location for the variable. This will be the location 308 of either the struct __Block_byref_x_VarName or the pointer to the 309 struct __Block_byref_x_VarName. 310 311 The struct will look something like: 312 313 struct __Block_byref_x_VarName { 314 ... <various fields> 315 struct __Block_byref_x_VarName *forwarding; 316 ... <various other fields> 317 SomeType VarName; 318 ... <maybe more fields> 319 }; 320 321 If we are given the struct directly (as our starting point) we 322 need to tell the debugger to: 323 324 1). Add the offset of the forwarding field. 325 326 2). Follow that pointer to get the real __Block_byref_x_VarName 327 struct to use (the real one may have been copied onto the heap). 328 329 3). Add the offset for the field VarName, to find the actual variable. 330 331 If we started with a pointer to the struct, then we need to 332 dereference that pointer first, before the other steps. 333 Translating this into DWARF ops, we will need to append the following 334 to the current location description for the variable: 335 336 DW_OP_deref -- optional, if we start with a pointer 337 DW_OP_plus_uconst <forward_fld_offset> 338 DW_OP_deref 339 DW_OP_plus_uconst <varName_fld_offset> 340 341 That is what this function does. */ 342 343 /// addBlockByrefAddress - Start with the address based on the location 344 /// provided, and generate the DWARF information necessary to find the 345 /// actual Block variable (navigating the Block struct) based on the 346 /// starting location. Add the DWARF information to the die. For 347 /// more information, read large comment just above here. 348 /// 349 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die, 350 unsigned Attribute, 351 const MachineLocation &Location) { 352 DIType Ty = DV->getType(); 353 DIType TmpTy = Ty; 354 unsigned Tag = Ty.getTag(); 355 bool isPointer = false; 356 357 StringRef varName = DV->getName(); 358 359 if (Tag == dwarf::DW_TAG_pointer_type) { 360 DIDerivedType DTy = DIDerivedType(Ty); 361 TmpTy = DTy.getTypeDerivedFrom(); 362 isPointer = true; 363 } 364 365 DICompositeType blockStruct = DICompositeType(TmpTy); 366 367 // Find the __forwarding field and the variable field in the __Block_byref 368 // struct. 369 DIArray Fields = blockStruct.getTypeArray(); 370 DIDescriptor varField = DIDescriptor(); 371 DIDescriptor forwardingField = DIDescriptor(); 372 373 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { 374 DIDescriptor Element = Fields.getElement(i); 375 DIDerivedType DT = DIDerivedType(Element); 376 StringRef fieldName = DT.getName(); 377 if (fieldName == "__forwarding") 378 forwardingField = Element; 379 else if (fieldName == varName) 380 varField = Element; 381 } 382 383 // Get the offsets for the forwarding field and the variable field. 384 unsigned forwardingFieldOffset = 385 DIDerivedType(forwardingField).getOffsetInBits() >> 3; 386 unsigned varFieldOffset = 387 DIDerivedType(varField).getOffsetInBits() >> 3; 388 389 // Decode the original location, and use that as the start of the byref 390 // variable's location. 391 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 392 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); 393 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 394 395 if (Location.isReg()) { 396 if (Reg < 32) 397 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); 398 else { 399 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 400 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 401 } 402 } else { 403 if (Reg < 32) 404 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); 405 else { 406 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 407 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); 408 } 409 410 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); 411 } 412 413 // If we started with a pointer to the __Block_byref... struct, then 414 // the first thing we need to do is dereference the pointer (DW_OP_deref). 415 if (isPointer) 416 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 417 418 // Next add the offset for the '__forwarding' field: 419 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in 420 // adding the offset if it's 0. 421 if (forwardingFieldOffset > 0) { 422 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 423 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset); 424 } 425 426 // Now dereference the __forwarding field to get to the real __Block_byref 427 // struct: DW_OP_deref. 428 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 429 430 // Now that we've got the real __Block_byref... struct, add the offset 431 // for the variable's field to get to the location of the actual variable: 432 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. 433 if (varFieldOffset > 0) { 434 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 435 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset); 436 } 437 438 // Now attach the location information to the DIE. 439 addBlock(Die, Attribute, 0, Block); 440 } 441 442 /// addConstantValue - Add constant value entry in variable DIE. 443 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO) { 444 assert (MO.isImm() && "Invalid machine operand!"); 445 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 446 unsigned Imm = MO.getImm(); 447 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm); 448 addBlock(Die, dwarf::DW_AT_const_value, 0, Block); 449 return true; 450 } 451 452 /// addConstantFPValue - Add constant value entry in variable DIE. 453 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { 454 assert (MO.isFPImm() && "Invalid machine operand!"); 455 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 456 APFloat FPImm = MO.getFPImm()->getValueAPF(); 457 458 // Get the raw data form of the floating point. 459 const APInt FltVal = FPImm.bitcastToAPInt(); 460 const char *FltPtr = (const char*)FltVal.getRawData(); 461 462 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 463 bool LittleEndian = Asm->getTargetData().isLittleEndian(); 464 int Incr = (LittleEndian ? 1 : -1); 465 int Start = (LittleEndian ? 0 : NumBytes - 1); 466 int Stop = (LittleEndian ? NumBytes : -1); 467 468 // Output the constant to DWARF one byte at a time. 469 for (; Start != Stop; Start += Incr) 470 addUInt(Block, 0, dwarf::DW_FORM_data1, 471 (unsigned char)0xFF & FltPtr[Start]); 472 473 addBlock(Die, dwarf::DW_AT_const_value, 0, Block); 474 return true; 475 } 476 477 /// addConstantValue - Add constant value entry in variable DIE. 478 bool CompileUnit::addConstantValue(DIE *Die, ConstantInt *CI, 479 bool Unsigned) { 480 if (CI->getBitWidth() <= 64) { 481 if (Unsigned) 482 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, 483 CI->getZExtValue()); 484 else 485 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, 486 CI->getSExtValue()); 487 return true; 488 } 489 490 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 491 492 // Get the raw data form of the large APInt. 493 const APInt Val = CI->getValue(); 494 const char *Ptr = (const char*)Val.getRawData(); 495 496 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 497 bool LittleEndian = Asm->getTargetData().isLittleEndian(); 498 int Incr = (LittleEndian ? 1 : -1); 499 int Start = (LittleEndian ? 0 : NumBytes - 1); 500 int Stop = (LittleEndian ? NumBytes : -1); 501 502 // Output the constant to DWARF one byte at a time. 503 for (; Start != Stop; Start += Incr) 504 addUInt(Block, 0, dwarf::DW_FORM_data1, 505 (unsigned char)0xFF & Ptr[Start]); 506 507 addBlock(Die, dwarf::DW_AT_const_value, 0, Block); 508 return true; 509 } 510 511 /// addTemplateParams - Add template parameters in buffer. 512 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { 513 // Add template parameters. 514 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { 515 DIDescriptor Element = TParams.getElement(i); 516 if (Element.isTemplateTypeParameter()) 517 Buffer.addChild(getOrCreateTemplateTypeParameterDIE( 518 DITemplateTypeParameter(Element))); 519 else if (Element.isTemplateValueParameter()) 520 Buffer.addChild(getOrCreateTemplateValueParameterDIE( 521 DITemplateValueParameter(Element))); 522 } 523 524 } 525 /// addToContextOwner - Add Die into the list of its context owner's children. 526 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) { 527 if (Context.isType()) { 528 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context)); 529 ContextDIE->addChild(Die); 530 } else if (Context.isNameSpace()) { 531 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context)); 532 ContextDIE->addChild(Die); 533 } else if (Context.isSubprogram()) { 534 DIE *ContextDIE = DD->createSubprogramDIE(DISubprogram(Context)); 535 ContextDIE->addChild(Die); 536 } else if (DIE *ContextDIE = getDIE(Context)) 537 ContextDIE->addChild(Die); 538 else 539 addDie(Die); 540 } 541 542 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 543 /// given DIType. 544 DIE *CompileUnit::getOrCreateTypeDIE(DIType Ty) { 545 DIE *TyDIE = getDIE(Ty); 546 if (TyDIE) 547 return TyDIE; 548 549 // Create new type. 550 TyDIE = new DIE(dwarf::DW_TAG_base_type); 551 insertDIE(Ty, TyDIE); 552 if (Ty.isBasicType()) 553 constructTypeDIE(*TyDIE, DIBasicType(Ty)); 554 else if (Ty.isCompositeType()) 555 constructTypeDIE(*TyDIE, DICompositeType(Ty)); 556 else { 557 assert(Ty.isDerivedType() && "Unknown kind of DIType"); 558 constructTypeDIE(*TyDIE, DIDerivedType(Ty)); 559 } 560 561 addToContextOwner(TyDIE, Ty.getContext()); 562 return TyDIE; 563 } 564 565 /// addType - Add a new type attribute to the specified entity. 566 void CompileUnit::addType(DIE *Entity, DIType Ty) { 567 if (!Ty.Verify()) 568 return; 569 570 // Check for pre-existence. 571 DIEEntry *Entry = getDIEEntry(Ty); 572 // If it exists then use the existing value. 573 if (Entry) { 574 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry); 575 return; 576 } 577 578 // Construct type. 579 DIE *Buffer = getOrCreateTypeDIE(Ty); 580 581 // Set up proxy. 582 Entry = createDIEEntry(Buffer); 583 insertDIEEntry(Ty, Entry); 584 585 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry); 586 } 587 588 /// constructTypeDIE - Construct basic type die from DIBasicType. 589 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { 590 // Get core information. 591 StringRef Name = BTy.getName(); 592 Buffer.setTag(dwarf::DW_TAG_base_type); 593 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 594 BTy.getEncoding()); 595 596 // Add name if not anonymous or intermediate type. 597 if (!Name.empty()) 598 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 599 uint64_t Size = BTy.getSizeInBits() >> 3; 600 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 601 } 602 603 /// constructTypeDIE - Construct derived type die from DIDerivedType. 604 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { 605 // Get core information. 606 StringRef Name = DTy.getName(); 607 uint64_t Size = DTy.getSizeInBits() >> 3; 608 unsigned Tag = DTy.getTag(); 609 610 // FIXME - Workaround for templates. 611 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; 612 613 Buffer.setTag(Tag); 614 615 // Map to main type, void will not have a type. 616 DIType FromTy = DTy.getTypeDerivedFrom(); 617 addType(&Buffer, FromTy); 618 619 // Add name if not anonymous or intermediate type. 620 if (!Name.empty()) 621 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 622 623 // Add size if non-zero (derived types might be zero-sized.) 624 if (Size) 625 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 626 627 // Add source line info if available and TyDesc is not a forward declaration. 628 if (!DTy.isForwardDecl()) 629 addSourceLine(&Buffer, DTy); 630 } 631 632 /// constructTypeDIE - Construct type DIE from DICompositeType. 633 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { 634 // Get core information. 635 StringRef Name = CTy.getName(); 636 637 uint64_t Size = CTy.getSizeInBits() >> 3; 638 unsigned Tag = CTy.getTag(); 639 Buffer.setTag(Tag); 640 641 switch (Tag) { 642 case dwarf::DW_TAG_vector_type: 643 case dwarf::DW_TAG_array_type: 644 constructArrayTypeDIE(Buffer, &CTy); 645 break; 646 case dwarf::DW_TAG_enumeration_type: { 647 DIArray Elements = CTy.getTypeArray(); 648 649 // Add enumerators to enumeration type. 650 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 651 DIE *ElemDie = NULL; 652 DIDescriptor Enum(Elements.getElement(i)); 653 if (Enum.isEnumerator()) { 654 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum)); 655 Buffer.addChild(ElemDie); 656 } 657 } 658 } 659 break; 660 case dwarf::DW_TAG_subroutine_type: { 661 // Add return type. 662 DIArray Elements = CTy.getTypeArray(); 663 DIDescriptor RTy = Elements.getElement(0); 664 addType(&Buffer, DIType(RTy)); 665 666 bool isPrototyped = true; 667 // Add arguments. 668 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { 669 DIDescriptor Ty = Elements.getElement(i); 670 if (Ty.isUnspecifiedParameter()) { 671 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters); 672 Buffer.addChild(Arg); 673 isPrototyped = false; 674 } else { 675 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); 676 addType(Arg, DIType(Ty)); 677 Buffer.addChild(Arg); 678 } 679 } 680 // Add prototype flag. 681 if (isPrototyped) 682 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); 683 } 684 break; 685 case dwarf::DW_TAG_structure_type: 686 case dwarf::DW_TAG_union_type: 687 case dwarf::DW_TAG_class_type: { 688 // Add elements to structure type. 689 DIArray Elements = CTy.getTypeArray(); 690 691 // A forward struct declared type may not have elements available. 692 unsigned N = Elements.getNumElements(); 693 if (N == 0) 694 break; 695 696 // Add elements to structure type. 697 for (unsigned i = 0; i < N; ++i) { 698 DIDescriptor Element = Elements.getElement(i); 699 DIE *ElemDie = NULL; 700 if (Element.isSubprogram()) { 701 DISubprogram SP(Element); 702 ElemDie = DD->createSubprogramDIE(DISubprogram(Element)); 703 if (SP.isProtected()) 704 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, 705 dwarf::DW_ACCESS_protected); 706 else if (SP.isPrivate()) 707 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, 708 dwarf::DW_ACCESS_private); 709 else 710 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, 711 dwarf::DW_ACCESS_public); 712 if (SP.isExplicit()) 713 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1); 714 } 715 else if (Element.isVariable()) { 716 DIVariable DV(Element); 717 ElemDie = new DIE(dwarf::DW_TAG_variable); 718 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, 719 DV.getName()); 720 addType(ElemDie, DV.getType()); 721 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 722 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); 723 addSourceLine(ElemDie, DV); 724 } else if (Element.isDerivedType()) 725 ElemDie = createMemberDIE(DIDerivedType(Element)); 726 else 727 continue; 728 Buffer.addChild(ElemDie); 729 } 730 731 if (CTy.isAppleBlockExtension()) 732 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1); 733 734 unsigned RLang = CTy.getRunTimeLang(); 735 if (RLang) 736 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, 737 dwarf::DW_FORM_data1, RLang); 738 739 DICompositeType ContainingType = CTy.getContainingType(); 740 if (DIDescriptor(ContainingType).isCompositeType()) 741 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, 742 getOrCreateTypeDIE(DIType(ContainingType))); 743 else { 744 DIDescriptor Context = CTy.getContext(); 745 addToContextOwner(&Buffer, Context); 746 } 747 748 if (Tag == dwarf::DW_TAG_class_type) 749 addTemplateParams(Buffer, CTy.getTemplateParams()); 750 751 break; 752 } 753 default: 754 break; 755 } 756 757 // Add name if not anonymous or intermediate type. 758 if (!Name.empty()) 759 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 760 761 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type 762 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 763 { 764 // Add size if non-zero (derived types might be zero-sized.) 765 if (Size) 766 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); 767 else { 768 // Add zero size if it is not a forward declaration. 769 if (CTy.isForwardDecl()) 770 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); 771 else 772 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); 773 } 774 775 // Add source line info if available. 776 if (!CTy.isForwardDecl()) 777 addSourceLine(&Buffer, CTy); 778 } 779 } 780 781 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE 782 /// for the given DITemplateTypeParameter. 783 DIE * 784 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) { 785 DIE *ParamDIE = getDIE(TP); 786 if (ParamDIE) 787 return ParamDIE; 788 789 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter); 790 addType(ParamDIE, TP.getType()); 791 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName()); 792 return ParamDIE; 793 } 794 795 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE 796 /// for the given DITemplateValueParameter. 797 DIE * 798 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) { 799 DIE *ParamDIE = getDIE(TPV); 800 if (ParamDIE) 801 return ParamDIE; 802 803 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter); 804 addType(ParamDIE, TPV.getType()); 805 if (!TPV.getName().empty()) 806 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName()); 807 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, 808 TPV.getValue()); 809 return ParamDIE; 810 } 811 812 /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 813 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){ 814 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); 815 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); 816 int64_t L = SR.getLo(); 817 int64_t H = SR.getHi(); 818 819 // The L value defines the lower bounds which is typically zero for C/C++. The 820 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size 821 // of the array. If L > H then do not emit DW_AT_lower_bound and 822 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the 823 // array has one element and in such case do not emit lower bound. 824 825 if (L > H) { 826 Buffer.addChild(DW_Subrange); 827 return; 828 } 829 if (L) 830 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L); 831 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H); 832 Buffer.addChild(DW_Subrange); 833 } 834 835 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 836 void CompileUnit::constructArrayTypeDIE(DIE &Buffer, 837 DICompositeType *CTy) { 838 Buffer.setTag(dwarf::DW_TAG_array_type); 839 if (CTy->getTag() == dwarf::DW_TAG_vector_type) 840 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1); 841 842 // Emit derived type. 843 addType(&Buffer, CTy->getTypeDerivedFrom()); 844 DIArray Elements = CTy->getTypeArray(); 845 846 // Get an anonymous type for index type. 847 DIE *IdxTy = getIndexTyDie(); 848 if (!IdxTy) { 849 // Construct an anonymous type for index type. 850 IdxTy = new DIE(dwarf::DW_TAG_base_type); 851 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); 852 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 853 dwarf::DW_ATE_signed); 854 addDie(IdxTy); 855 setIndexTyDie(IdxTy); 856 } 857 858 // Add subranges to array type. 859 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 860 DIDescriptor Element = Elements.getElement(i); 861 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 862 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); 863 } 864 } 865 866 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. 867 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) { 868 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); 869 StringRef Name = ETy.getName(); 870 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 871 int64_t Value = ETy.getEnumValue(); 872 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); 873 return Enumerator; 874 } 875 876 /// createMemberDIE - Create new member DIE. 877 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) { 878 DIE *MemberDie = new DIE(DT.getTag()); 879 StringRef Name = DT.getName(); 880 if (!Name.empty()) 881 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); 882 883 addType(MemberDie, DT.getTypeDerivedFrom()); 884 885 addSourceLine(MemberDie, DT); 886 887 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); 888 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 889 890 uint64_t Size = DT.getSizeInBits(); 891 uint64_t FieldSize = DT.getOriginalTypeSize(); 892 893 if (Size != FieldSize) { 894 // Handle bitfield. 895 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); 896 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); 897 898 uint64_t Offset = DT.getOffsetInBits(); 899 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 900 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 901 uint64_t FieldOffset = (HiMark - FieldSize); 902 Offset -= FieldOffset; 903 904 // Maybe we need to work from the other end. 905 if (Asm->getTargetData().isLittleEndian()) 906 Offset = FieldSize - (Offset + Size); 907 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); 908 909 // Here WD_AT_data_member_location points to the anonymous 910 // field that includes this bit field. 911 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3); 912 913 } else 914 // This is not a bitfield. 915 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); 916 917 if (DT.getTag() == dwarf::DW_TAG_inheritance 918 && DT.isVirtual()) { 919 920 // For C++, virtual base classes are not at fixed offset. Use following 921 // expression to extract appropriate offset from vtable. 922 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 923 924 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); 925 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 926 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 927 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 928 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits()); 929 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 930 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 931 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 932 933 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, 934 VBaseLocationDie); 935 } else 936 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie); 937 938 if (DT.isProtected()) 939 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, 940 dwarf::DW_ACCESS_protected); 941 else if (DT.isPrivate()) 942 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, 943 dwarf::DW_ACCESS_private); 944 // Otherwise C++ member and base classes are considered public. 945 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus) 946 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, 947 dwarf::DW_ACCESS_public); 948 if (DT.isVirtual()) 949 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, 950 dwarf::DW_VIRTUALITY_virtual); 951 952 // Objective-C properties. 953 StringRef PropertyName = DT.getObjCPropertyName(); 954 if (!PropertyName.empty()) { 955 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, dwarf::DW_FORM_string, 956 PropertyName); 957 StringRef GetterName = DT.getObjCPropertyGetterName(); 958 if (!GetterName.empty()) 959 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, 960 dwarf::DW_FORM_string, GetterName); 961 StringRef SetterName = DT.getObjCPropertySetterName(); 962 if (!SetterName.empty()) 963 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, 964 dwarf::DW_FORM_string, SetterName); 965 unsigned PropertyAttributes = 0; 966 if (DT.isReadOnlyObjCProperty()) 967 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; 968 if (DT.isReadWriteObjCProperty()) 969 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; 970 if (DT.isAssignObjCProperty()) 971 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; 972 if (DT.isRetainObjCProperty()) 973 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; 974 if (DT.isCopyObjCProperty()) 975 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; 976 if (DT.isNonAtomicObjCProperty()) 977 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; 978 if (PropertyAttributes) 979 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0, 980 PropertyAttributes); 981 } 982 return MemberDie; 983 } 984