comparison gen/todebug.cpp @ 252:e3355ce5444b trunk

[svn r269] Fixed dwarf debug info for structs.
author lindquist
date Thu, 12 Jun 2008 16:58:26 +0200
parents fc9c1a0eabbd
children 23d0d9855cad
comparison
equal deleted inserted replaced
251:2f2d7c843e5d 252:e3355ce5444b
32 */ 32 */
33 static LLGlobalVariable* emitDwarfGlobal(const LLStructType* type, const std::vector<LLConstant*> values, const char* name, bool linkonce=false) 33 static LLGlobalVariable* emitDwarfGlobal(const LLStructType* type, const std::vector<LLConstant*> values, const char* name, bool linkonce=false)
34 { 34 {
35 LLConstant* c = llvm::ConstantStruct::get(type, values); 35 LLConstant* c = llvm::ConstantStruct::get(type, values);
36 LLGlobalValue::LinkageTypes linkage = linkonce ? LLGlobalValue::LinkOnceLinkage : LLGlobalValue::InternalLinkage; 36 LLGlobalValue::LinkageTypes linkage = linkonce ? LLGlobalValue::LinkOnceLinkage : LLGlobalValue::InternalLinkage;
37 LLGlobalVariable* gv = new LLGlobalVariable(c->getType(), true, linkage, c, name, gIR->module); 37 LLGlobalVariable* gv = new LLGlobalVariable(type, true, linkage, c, name, gIR->module);
38 gv->setSection("llvm.metadata"); 38 gv->setSection("llvm.metadata");
39 return gv; 39 return gv;
40 } 40 }
41 41
42 ////////////////////////////////////////////////////////////////////////////////////////////////// 42 /**
43 43 * Emits a global variable, LLVM Dwarf style, only declares.
44 static llvm::GlobalVariable* dbg_compile_units = 0; 44 * @param type Type of variable.
45 static llvm::GlobalVariable* dbg_global_variables = 0; 45 * @param name Name.
46 static llvm::GlobalVariable* dbg_subprograms = 0; 46 * @return The global variable.
47 */
48 static LLGlobalVariable* emitDwarfGlobalDecl(const LLStructType* type, const char* name, bool linkonce=false)
49 {
50 LLGlobalValue::LinkageTypes linkage = linkonce ? LLGlobalValue::LinkOnceLinkage : LLGlobalValue::InternalLinkage;
51 LLGlobalVariable* gv = new LLGlobalVariable(type, true, linkage, NULL, name, gIR->module);
52 gv->setSection("llvm.metadata");
53 return gv;
54 }
55
56 //////////////////////////////////////////////////////////////////////////////////////////////////
47 57
48 /** 58 /**
49 * Emits the Dwarf anchors that are used repeatedly by LLVM debug info. 59 * Emits the Dwarf anchors that are used repeatedly by LLVM debug info.
50 */ 60 */
51 static void emitDwarfAnchors() 61 static void emitDwarfAnchors()
52 { 62 {
53 const llvm::StructType* anchorTy = isaStruct(gIR->module->getTypeByName("llvm.dbg.anchor.type")); 63 const llvm::StructType* anchorTy = isaStruct(gIR->module->getTypeByName("llvm.dbg.anchor.type"));
54 std::vector<LLConstant*> vals(2); 64 std::vector<LLConstant*> vals(2);
55 65
56 if (!gIR->module->getNamedGlobal("llvm.dbg.compile_units")) { 66 vals[0] = DtoConstUint(llvm::LLVMDebugVersion);
57 vals[0] = DtoConstUint(llvm::LLVMDebugVersion); 67 vals[1] = DtoConstUint(DW_TAG_compile_unit);
58 vals[1] = DtoConstUint(DW_TAG_compile_unit); 68 gIR->dwarfCUs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.compile_units", true);
59 dbg_compile_units = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.compile_units", true); 69
60 } 70 vals[0] = DtoConstUint(llvm::LLVMDebugVersion);
61 if (!gIR->module->getNamedGlobal("llvm.dbg.global_variables")) { 71 vals[1] = DtoConstUint(DW_TAG_variable);
62 vals[0] = DtoConstUint(llvm::LLVMDebugVersion); 72 gIR->dwarfGVs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.global_variables", true);
63 vals[1] = DtoConstUint(DW_TAG_variable); 73
64 dbg_global_variables = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.global_variables", true); 74 vals[0] = DtoConstUint(llvm::LLVMDebugVersion);
65 } 75 vals[1] = DtoConstUint(DW_TAG_subprogram);
66 if (!gIR->module->getNamedGlobal("llvm.dbg.subprograms")) { 76 gIR->dwarfSPs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.subprograms", true);
67 vals[0] = DtoConstUint(llvm::LLVMDebugVersion); 77 }
68 vals[1] = DtoConstUint(DW_TAG_subprogram); 78
69 dbg_subprograms = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.subprograms", true); 79 //////////////////////////////////////////////////////////////////////////////////////////////////
70 }
71 }
72 80
73 static LLConstant* getDwarfAnchor(dwarf_constants c) 81 static LLConstant* getDwarfAnchor(dwarf_constants c)
74 { 82 {
75 emitDwarfAnchors(); 83 if (!gIR->dwarfCUs)
84 emitDwarfAnchors();
76 switch (c) 85 switch (c)
77 { 86 {
78 case DW_TAG_compile_unit: 87 case DW_TAG_compile_unit:
79 return dbg_compile_units; 88 return gIR->dwarfCUs;
80 case DW_TAG_variable: 89 case DW_TAG_variable:
81 return dbg_global_variables; 90 return gIR->dwarfGVs;
82 case DW_TAG_subprogram: 91 case DW_TAG_subprogram:
83 return dbg_subprograms; 92 return gIR->dwarfSPs;
84 default: 93 default:
85 assert(0); 94 assert(0);
86 } 95 }
87 } 96 }
88 97
161 return emitDwarfGlobal(getDwarfSubProgramType(), vals, "llvm.dbg.subprogram"); 170 return emitDwarfGlobal(getDwarfSubProgramType(), vals, "llvm.dbg.subprogram");
162 } 171 }
163 172
164 ////////////////////////////////////////////////////////////////////////////////////////////////// 173 //////////////////////////////////////////////////////////////////////////////////////////////////
165 174
166 static LLGlobalVariable* dwarfTypeDescription(Loc loc, Type* type, LLGlobalVariable* cu, const char* c_name); 175 static LLGlobalVariable* dwarfTypeDescription(Type* type, LLGlobalVariable* cu, const char* c_name);
167 176
168 ////////////////////////////////////////////////////////////////////////////////////////////////// 177 //////////////////////////////////////////////////////////////////////////////////////////////////
169 178
170 static LLGlobalVariable* dwarfBasicType(Type* type, llvm::GlobalVariable* compileUnit) 179 static LLGlobalVariable* dwarfBasicType(Type* type, llvm::GlobalVariable* compileUnit)
171 { 180 {
224 return emitDwarfGlobal(getDwarfBasicTypeType(), vals, "llvm.dbg.basictype"); 233 return emitDwarfGlobal(getDwarfBasicTypeType(), vals, "llvm.dbg.basictype");
225 } 234 }
226 235
227 ////////////////////////////////////////////////////////////////////////////////////////////////// 236 //////////////////////////////////////////////////////////////////////////////////////////////////
228 237
229 static LLGlobalVariable* dwarfDerivedType(Loc loc, Type* type, llvm::GlobalVariable* compileUnit) 238 static LLGlobalVariable* dwarfDerivedType(Type* type, llvm::GlobalVariable* compileUnit)
230 { 239 {
231 const LLType* T = DtoType(type); 240 const LLType* T = DtoType(type);
232 Type* t = DtoDType(type); 241 Type* t = DtoDType(type);
233 242
234 // defaults 243 // defaults
274 // FIXME: dont know what this is 283 // FIXME: dont know what this is
275 vals.push_back(DtoConstUint(0)); 284 vals.push_back(DtoConstUint(0));
276 285
277 // base type 286 // base type
278 Type* nt = t->nextOf(); 287 Type* nt = t->nextOf();
279 LLGlobalVariable* nTD = dwarfTypeDescription(loc, nt, compileUnit, NULL); 288 LLGlobalVariable* nTD = dwarfTypeDescription(nt, compileUnit, NULL);
280 if (nt->ty == Tvoid || !nTD) 289 if (nt->ty == Tvoid || !nTD)
281 vals.push_back(DBG_NULL); 290 vals.push_back(DBG_NULL);
282 else 291 else
283 vals.push_back(DBG_CAST(nTD)); 292 vals.push_back(DBG_CAST(nTD));
284 293
285 return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype"); 294 return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype");
286 } 295 }
287 296
288 ////////////////////////////////////////////////////////////////////////////////////////////////// 297 //////////////////////////////////////////////////////////////////////////////////////////////////
289 298
290 static LLGlobalVariable* dwarfMemberType(Loc loc, Type* type, llvm::GlobalVariable* compileUnit, const char* c_name, unsigned offset) 299 static LLGlobalVariable* dwarfMemberType(unsigned linnum, Type* type, LLGlobalVariable* compileUnit, LLGlobalVariable* definedCU, const char* c_name, unsigned offset)
291 { 300 {
292 const LLType* T = DtoType(type); 301 const LLType* T = DtoType(type);
293 Type* t = DtoDType(type); 302 Type* t = DtoDType(type);
294 303
295 // defaults 304 // defaults
309 318
310 // name 319 // name
311 vals.push_back(name); 320 vals.push_back(name);
312 321
313 // compile unit where defined 322 // compile unit where defined
314 vals.push_back(DBG_NULL); 323 if (definedCU)
324 vals.push_back(DBG_CAST(definedCU));
325 else
326 vals.push_back(DBG_NULL);
315 327
316 // line number where defined 328 // line number where defined
317 vals.push_back(DtoConstInt(0)); 329 vals.push_back(DtoConstInt(linnum));
318 330
319 // size in bits 331 // size in bits
320 vals.push_back(LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false)); 332 vals.push_back(LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false));
321 333
322 // alignment in bits 334 // alignment in bits
327 339
328 // FIXME: dont know what this is 340 // FIXME: dont know what this is
329 vals.push_back(DtoConstUint(0)); 341 vals.push_back(DtoConstUint(0));
330 342
331 // base type 343 // base type
332 LLGlobalVariable* nTD = dwarfTypeDescription(loc, t, compileUnit, NULL); 344 LLGlobalVariable* nTD = dwarfTypeDescription(t, compileUnit, NULL);
333 if (t->ty == Tvoid || !nTD) 345 if (t->ty == Tvoid || !nTD)
334 vals.push_back(DBG_NULL); 346 vals.push_back(DBG_NULL);
335 else 347 else
336 vals.push_back(DBG_CAST(nTD)); 348 vals.push_back(DBG_CAST(nTD));
337 349
338 return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype"); 350 return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype");
339 } 351 }
340 352
341 ////////////////////////////////////////////////////////////////////////////////////////////////// 353 //////////////////////////////////////////////////////////////////////////////////////////////////
342 354
343 static LLGlobalVariable* dwarfCompositeType(Loc loc, Type* type, llvm::GlobalVariable* compileUnit) 355 static LLGlobalVariable* dwarfCompositeType(Type* type, llvm::GlobalVariable* compileUnit)
344 { 356 {
345 const LLType* T = DtoType(type); 357 const LLType* T = DtoType(type);
346 Type* t = DtoDType(type); 358 Type* t = DtoDType(type);
347 359
348 // defaults 360 // defaults
349 LLConstant* name = getNullPtr(getVoidPtrType()); 361 LLConstant* name = getNullPtr(getVoidPtrType());
350 LLGlobalVariable* members = NULL; 362 LLGlobalVariable* members = NULL;
363 unsigned linnum = 0;
364 LLGlobalVariable* definedCU = NULL;
351 365
352 // prepare tag and members 366 // prepare tag and members
353 unsigned tag; 367 unsigned tag;
368
369 // declare final global variable
370 LLGlobalVariable* gv = NULL;
371
372 // dynamic array
354 if (t->ty == Tarray) 373 if (t->ty == Tarray)
355 { 374 {
356 tag = DW_TAG_structure_type; 375 tag = DW_TAG_structure_type;
357 376
358 LLGlobalVariable* len = dwarfMemberType(loc, Type::tsize_t, compileUnit, "length", 0); 377 LLGlobalVariable* len = dwarfMemberType(0, Type::tsize_t, compileUnit, NULL, "length", 0);
359 assert(len); 378 assert(len);
360 LLGlobalVariable* ptr = dwarfMemberType(loc, t->nextOf()->pointerTo(), compileUnit, "ptr", global.params.is64bit?8:4); 379 LLGlobalVariable* ptr = dwarfMemberType(0, t->nextOf()->pointerTo(), compileUnit, NULL, "ptr", global.params.is64bit?8:4);
361 assert(ptr); 380 assert(ptr);
362 381
363 const LLArrayType* at = LLArrayType::get(DBG_TYPE, 2); 382 const LLArrayType* at = LLArrayType::get(DBG_TYPE, 2);
364 383
365 std::vector<LLConstant*> elems; 384 std::vector<LLConstant*> elems;
366 elems.push_back(DBG_CAST(len)); 385 elems.push_back(DBG_CAST(len));
367 elems.push_back(DBG_CAST(ptr)); 386 elems.push_back(DBG_CAST(ptr));
368 387
369 // elems[0]->dump();
370 // elems[1]->dump();
371 // at->dump();
372
373 LLConstant* ca = LLConstantArray::get(at, elems); 388 LLConstant* ca = LLConstantArray::get(at, elems);
374 members = new LLGlobalVariable(ca->getType(), true, LLGlobalValue::InternalLinkage, ca, ".array", gIR->module); 389 members = new LLGlobalVariable(ca->getType(), true, LLGlobalValue::InternalLinkage, ca, ".array", gIR->module);
375 members->setSection("llvm.metadata"); 390 members->setSection("llvm.metadata");
376 } 391
392 name = DtoConstStringPtr(t->toChars(), "llvm.metadata");
393 }
394
395 // struct
396 else if (t->ty == Tstruct)
397 {
398 TypeStruct* ts = (TypeStruct*)t;
399 StructDeclaration* sd = ts->sym;
400 assert(sd);
401
402 IrStruct* ir = sd->ir.irStruct;
403 assert(ir);
404 if (ir->dwarfComposite)
405 return ir->dwarfComposite;
406
407 // set to handle recursive types properly
408 gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype");
409 ir->dwarfComposite = gv;
410
411 tag = DW_TAG_structure_type;
412
413 name = DtoConstStringPtr(sd->toChars(), "llvm.metadata");
414 linnum = sd->loc.linnum;
415 definedCU = DtoDwarfCompileUnit(sd->getModule());
416
417 std::vector<LLConstant*> elems;
418 for (IrStruct::OffsetMap::iterator i=ir->offsets.begin(); i!=ir->offsets.end(); ++i)
419 {
420 unsigned offset = i->first;
421 IrStruct::Offset& o = i->second;
422
423 LLGlobalVariable* ptr = dwarfMemberType(o.var->loc.linnum, o.var->type, compileUnit, definedCU, o.var->toChars(), offset);
424 elems.push_back(DBG_CAST(ptr));
425 }
426
427 const LLArrayType* at = LLArrayType::get(DBG_TYPE, elems.size());
428 LLConstant* ca = LLConstantArray::get(at, elems);
429 members = new LLGlobalVariable(ca->getType(), true, LLGlobalValue::InternalLinkage, ca, ".array", gIR->module);
430 members->setSection("llvm.metadata");
431 }
432
433 // unsupported composite type
377 else 434 else
378 { 435 {
379 assert(0 && "unsupported compositetype for debug info"); 436 assert(0 && "unsupported compositetype for debug info");
380 } 437 }
381 438
389 446
390 // name 447 // name
391 vals.push_back(name); 448 vals.push_back(name);
392 449
393 // compile unit where defined 450 // compile unit where defined
394 vals.push_back(DBG_NULL); 451 if (definedCU)
452 vals.push_back(DBG_CAST(definedCU));
453 else
454 vals.push_back(DBG_NULL);
395 455
396 // line number where defined 456 // line number where defined
397 vals.push_back(DtoConstInt(0)); 457 vals.push_back(DtoConstInt(linnum));
398 458
399 // size in bits 459 // size in bits
400 vals.push_back(LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false)); 460 vals.push_back(LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false));
401 461
402 // alignment in bits 462 // alignment in bits
415 if (members) 475 if (members)
416 vals.push_back(DBG_CAST(members)); 476 vals.push_back(DBG_CAST(members));
417 else 477 else
418 vals.push_back(DBG_NULL); 478 vals.push_back(DBG_NULL);
419 479
420 return emitDwarfGlobal(getDwarfCompositeTypeType(), vals, "llvm.dbg.compositetype"); 480 // set initializer
481 if (!gv)
482 gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype");
483 LLConstant* initia = LLConstantStruct::get(getDwarfCompositeTypeType(), vals);
484 gv->setInitializer(initia);
485
486 return gv;
421 } 487 }
422 488
423 ////////////////////////////////////////////////////////////////////////////////////////////////// 489 //////////////////////////////////////////////////////////////////////////////////////////////////
424 490
425 static LLGlobalVariable* dwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 491 static LLGlobalVariable* dwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd)
438 vals.push_back(DtoConstStringPtr(vd->toChars(), "llvm.metadata")); 504 vals.push_back(DtoConstStringPtr(vd->toChars(), "llvm.metadata"));
439 505
440 vals.push_back(DBG_CAST(DtoDwarfCompileUnit(vd->getModule()))); 506 vals.push_back(DBG_CAST(DtoDwarfCompileUnit(vd->getModule())));
441 vals.push_back(DtoConstUint(vd->loc.linnum)); 507 vals.push_back(DtoConstUint(vd->loc.linnum));
442 508
443 LLGlobalVariable* TY = dwarfTypeDescription(vd->loc, vd->type, compileUnit, NULL); 509 LLGlobalVariable* TY = dwarfTypeDescription(vd->type, compileUnit, NULL);
444 vals.push_back(TY ? DBG_CAST(TY) : DBG_NULL); 510 vals.push_back(TY ? DBG_CAST(TY) : DBG_NULL);
445 vals.push_back(DtoConstBool(vd->protection == PROTprivate)); 511 vals.push_back(DtoConstBool(vd->protection == PROTprivate));
446 vals.push_back(DtoConstBool(vd->getModule() == gIR->dmodule)); 512 vals.push_back(DtoConstBool(vd->getModule() == gIR->dmodule));
447 513
448 vals.push_back(DBG_CAST(ll)); 514 vals.push_back(DBG_CAST(ll));
449 515
450 return emitDwarfGlobal(getDwarfGlobalVariableType(), vals, "llvm.dbg.global_variable"); 516 return emitDwarfGlobal(getDwarfGlobalVariableType(), vals, "llvm.dbg.global_variable");
451 } 517 }
518
519 //////////////////////////////////////////////////////////////////////////////////////////////////
452 520
453 static LLGlobalVariable* dwarfVariable(VarDeclaration* vd, LLGlobalVariable* typeDescr) 521 static LLGlobalVariable* dwarfVariable(VarDeclaration* vd, LLGlobalVariable* typeDescr)
454 { 522 {
455 assert(!vd->isDataseg() && "static variable"); 523 assert(!vd->isDataseg() && "static variable");
456 524
487 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.declare"), args.begin(), args.end()); 555 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.declare"), args.begin(), args.end());
488 } 556 }
489 557
490 ////////////////////////////////////////////////////////////////////////////////////////////////// 558 //////////////////////////////////////////////////////////////////////////////////////////////////
491 559
492 static LLGlobalVariable* dwarfTypeDescription(Loc loc, Type* type, LLGlobalVariable* cu, const char* c_name) 560 static LLGlobalVariable* dwarfTypeDescription(Type* type, LLGlobalVariable* cu, const char* c_name)
493 { 561 {
494 Type* t = type->toBasetype(); 562 Type* t = type->toBasetype();
495 if (t->ty == Tvoid) 563 if (t->ty == Tvoid)
496 return NULL; 564 return NULL;
497 else if (t->isintegral() || t->isfloating()) 565 else if (t->isintegral() || t->isfloating())
498 return dwarfBasicType(type, cu); 566 return dwarfBasicType(type, cu);
499 else if (t->ty == Tpointer) 567 else if (t->ty == Tpointer)
500 return dwarfDerivedType(loc, type, cu); 568 return dwarfDerivedType(type, cu);
501 else if (t->ty == Tarray) 569 else if (t->ty == Tarray || t->ty == Tstruct)
502 return dwarfCompositeType(loc, type, cu); 570 return dwarfCompositeType(type, cu);
503 571
504 if (global.params.warnings)
505 warning("%s: unsupported type for debug info: %s", loc.toChars(), type->toChars());
506 return NULL; 572 return NULL;
507 } 573 }
508 574
509 ////////////////////////////////////////////////////////////////////////////////////////////////// 575 //////////////////////////////////////////////////////////////////////////////////////////////////
510 576
516 if (vd->getModule() != gIR->dmodule) 582 if (vd->getModule() != gIR->dmodule)
517 varCU = DtoDwarfCompileUnit(vd->getModule()); 583 varCU = DtoDwarfCompileUnit(vd->getModule());
518 584
519 // get type description 585 // get type description
520 Type* t = vd->type->toBasetype(); 586 Type* t = vd->type->toBasetype();
521 LLGlobalVariable* TD = dwarfTypeDescription(vd->loc, vd->type, thisCU, NULL); 587 LLGlobalVariable* TD = dwarfTypeDescription(vd->type, thisCU, NULL);
522 if (TD == NULL) 588 if (TD == NULL)
523 return; // unsupported 589 return; // unsupported
524 590
525 // get variable description 591 // get variable description
526 LLGlobalVariable* VD; 592 LLGlobalVariable* VD;