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