comparison gen/todebug.cpp @ 946:1714836f2c0b

Mostly rewrite debug info generation in terms of llvm/Analysis/DebugInfo.h. Add getCompilationModule to Dsymbol and fix template compile unit decision code. Runtime compiles with -g again.
author Christian Kamm <kamm incasoftware de>
date Sun, 08 Feb 2009 16:50:22 +0100
parents 01d9ece9982a
children 8a70b4381369
comparison
equal deleted inserted replaced
945:03d7c4aac654 946:1714836f2c0b
21 #define DBG_CAST(X) ( llvm::ConstantExpr::getBitCast(X, DBG_TYPE) ) 21 #define DBG_CAST(X) ( llvm::ConstantExpr::getBitCast(X, DBG_TYPE) )
22 22
23 #define DBG_TAG(X) ( llvm::ConstantExpr::getAdd( DtoConstUint( X ), DtoConstUint( llvm::LLVMDebugVersion ) ) ) 23 #define DBG_TAG(X) ( llvm::ConstantExpr::getAdd( DtoConstUint( X ), DtoConstUint( llvm::LLVMDebugVersion ) ) )
24 24
25 ////////////////////////////////////////////////////////////////////////////////////////////////// 25 //////////////////////////////////////////////////////////////////////////////////////////////////
26
27 /**
28 * Emits a global variable, LLVM Dwarf style.
29 * @param type Type of variable.
30 * @param values Initializers.
31 * @param name Name.
32 * @return The global variable.
33 */
34 static LLGlobalVariable* emitDwarfGlobal(const LLStructType* type, const std::vector<LLConstant*> values, const char* name, bool linkonce=false)
35 {
36 LLConstant* c = llvm::ConstantStruct::get(type, values);
37 LLGlobalValue::LinkageTypes linkage = linkonce ? LLGlobalValue::LinkOnceLinkage : LLGlobalValue::InternalLinkage;
38 LLGlobalVariable* gv = new LLGlobalVariable(type, true, linkage, c, name, gIR->module);
39 gv->setSection("llvm.metadata");
40 return gv;
41 }
42 26
43 /** 27 /**
44 * Emits a global variable, LLVM Dwarf style, only declares. 28 * Emits a global variable, LLVM Dwarf style, only declares.
45 * @param type Type of variable. 29 * @param type Type of variable.
46 * @param name Name. 30 * @param name Name.
54 return gv; 38 return gv;
55 } 39 }
56 40
57 ////////////////////////////////////////////////////////////////////////////////////////////////// 41 //////////////////////////////////////////////////////////////////////////////////////////////////
58 42
59 /** 43 static llvm::DIAnchor getDwarfAnchor(dwarf_constants c)
60 * Emits the Dwarf anchors that are used repeatedly by LLVM debug info. 44 {
61 */
62 static void emitDwarfAnchors()
63 {
64 const llvm::StructType* anchorTy = isaStruct(gIR->module->getTypeByName("llvm.dbg.anchor.type"));
65 std::vector<LLConstant*> vals(2);
66
67 vals[0] = DtoConstUint(llvm::LLVMDebugVersion);
68 vals[1] = DtoConstUint(DW_TAG_compile_unit);
69 gIR->dwarfCUs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.compile_units", true);
70
71 vals[0] = DtoConstUint(llvm::LLVMDebugVersion);
72 vals[1] = DtoConstUint(DW_TAG_variable);
73 gIR->dwarfGVs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.global_variables", true);
74
75 vals[0] = DtoConstUint(llvm::LLVMDebugVersion);
76 vals[1] = DtoConstUint(DW_TAG_subprogram);
77 gIR->dwarfSPs = emitDwarfGlobal(anchorTy, vals, "llvm.dbg.subprograms", true);
78 }
79
80 //////////////////////////////////////////////////////////////////////////////////////////////////
81
82 static LLConstant* getDwarfAnchor(dwarf_constants c)
83 {
84 if (!gIR->dwarfCUs)
85 emitDwarfAnchors();
86 switch (c) 45 switch (c)
87 { 46 {
88 case DW_TAG_compile_unit: 47 case DW_TAG_compile_unit:
89 return gIR->dwarfCUs; 48 return gIR->difactory.GetOrCreateCompileUnitAnchor();
90 case DW_TAG_variable: 49 case DW_TAG_variable:
91 return gIR->dwarfGVs; 50 return gIR->difactory.GetOrCreateGlobalVariableAnchor();
92 case DW_TAG_subprogram: 51 case DW_TAG_subprogram:
93 return gIR->dwarfSPs; 52 return gIR->difactory.GetOrCreateSubprogramAnchor();
94 default: 53 default:
95 assert(0); 54 assert(0);
96 } 55 }
97 } 56 }
98 57
124 83
125 static const llvm::StructType* getDwarfGlobalVariableType() { 84 static const llvm::StructType* getDwarfGlobalVariableType() {
126 return isaStruct(gIR->module->getTypeByName("llvm.dbg.global_variable.type")); 85 return isaStruct(gIR->module->getTypeByName("llvm.dbg.global_variable.type"));
127 } 86 }
128 87
129 ////////////////////////////////////////////////////////////////////////////////////////////////// 88
130 89 //////////////////////////////////////////////////////////////////////////////////////////////////
131 static LLGlobalVariable* dwarfCompileUnit(Module* m) 90
132 { 91 static llvm::DIType dwarfTypeDescription_impl(Type* type, llvm::DICompileUnit cu, const char* c_name);
133 std::vector<LLConstant*> vals(6); 92 static llvm::DIType dwarfTypeDescription(Type* type, llvm::DICompileUnit cu, const char* c_name);
134 vals[0] = DBG_TAG(DW_TAG_compile_unit); 93
135 vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_compile_unit)); 94 //////////////////////////////////////////////////////////////////////////////////////////////////
136 95
137 if (global.params.symdebug == 2) 96 static llvm::DIBasicType dwarfBasicType(Type* type, llvm::DICompileUnit compileUnit)
138 vals[2] = DtoConstUint(DW_LANG_C);
139 else
140 vals[2] = DtoConstUint(DW_LANG_D);
141 vals[3] = DtoConstStringPtr(FileName::name(m->srcfile->name->toChars()), "llvm.metadata");
142 std::string srcpath(FileName::path(m->srcfile->name->toChars()));
143 if (!FileName::absolute(srcpath.c_str())) {
144 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
145 tmp.appendComponent(srcpath);
146 srcpath = tmp.toString();
147 if (!srcpath.empty() && *srcpath.rbegin() != '/' && *srcpath.rbegin() != '\\')
148 srcpath = srcpath + '/';
149 }
150 vals[4] = DtoConstStringPtr(srcpath.c_str(), "llvm.metadata");
151 vals[5] = DtoConstStringPtr("LDC (http://www.dsource.org/projects/ldc)", "llvm.metadata");
152
153 LLGlobalVariable* gv = emitDwarfGlobal(getDwarfCompileUnitType(), vals, "llvm.dbg.compile_unit");
154 m->ir.irModule->dwarfCompileUnit = gv;
155 return gv;
156 }
157
158 //////////////////////////////////////////////////////////////////////////////////////////////////
159
160 static LLGlobalVariable* dwarfSubProgram(llvm::GlobalVariable* emitUnit, llvm::GlobalVariable* defineUnit, const char* prettyname, const char* mangledname, unsigned int linenum, bool isprivate)
161 {
162 std::vector<LLConstant*> vals(11);
163 vals[0] = DBG_TAG(DW_TAG_subprogram);
164 vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_subprogram));
165
166 vals[2] = DBG_CAST(emitUnit);
167 vals[3] = DtoConstStringPtr(prettyname, "llvm.metadata");
168 vals[4] = vals[3];
169 vals[5] = DtoConstStringPtr(mangledname, "llvm.metadata");
170 vals[6] = DBG_CAST(defineUnit);
171 vals[7] = DtoConstUint(linenum);
172 vals[8] = DBG_NULL;
173 vals[9] = DtoConstBool(isprivate);
174 vals[10] = DtoConstBool(emitUnit == defineUnit);
175
176 Logger::println("emitting subprogram global");
177
178 return emitDwarfGlobal(getDwarfSubProgramType(), vals, "llvm.dbg.subprogram");
179 }
180
181 /*
182 static LLGlobalVariable* dwarfSubProgram(FuncDeclaration* fd, llvm::GlobalVariable* compileUnit)
183 {
184 std::vector<LLConstant*> vals(11);
185 vals[0] = DBG_TAG(DW_TAG_subprogram);
186 vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_subprogram));
187
188 vals[2] = DBG_CAST(compileUnit);
189 vals[3] = DtoConstStringPtr(fd->toPrettyChars(), "llvm.metadata");
190 vals[4] = vals[3];
191 vals[5] = DtoConstStringPtr(fd->mangle(), "llvm.metadata");
192 vals[6] = DBG_CAST( DtoDwarfCompileUnit(fd->getModule()) );
193 vals[7] = DtoConstUint(fd->loc.linnum);
194 vals[8] = DBG_NULL;
195 vals[9] = DtoConstBool(fd->protection == PROTprivate);
196 vals[10] = DtoConstBool(fd->getModule() == gIR->dmodule);
197
198 Logger::println("emitting subprogram global");
199
200 return emitDwarfGlobal(getDwarfSubProgramType(), vals, "llvm.dbg.subprogram");
201 }*/
202
203 //////////////////////////////////////////////////////////////////////////////////////////////////
204
205 static LLGlobalVariable* dwarfTypeDescription_impl(Type* type, LLGlobalVariable* cu, const char* c_name);
206 static LLGlobalVariable* dwarfTypeDescription(Type* type, LLGlobalVariable* cu, const char* c_name);
207
208 //////////////////////////////////////////////////////////////////////////////////////////////////
209
210 static LLGlobalVariable* dwarfBasicType(Type* type, llvm::GlobalVariable* compileUnit)
211 { 97 {
212 Type* t = type->toBasetype(); 98 Type* t = type->toBasetype();
213
214 const LLType* T = DtoType(type); 99 const LLType* T = DtoType(type);
215 100
216 std::vector<LLConstant*> vals(10); 101 // find encoding
217
218 // tag
219 vals[0] = DBG_TAG(DW_TAG_base_type);
220
221 // context
222 vals[1] = DBG_CAST(compileUnit);
223
224 // name
225 vals[2] = DtoConstStringPtr(type->toChars(), "llvm.metadata");
226
227 // compile unit where defined
228 vals[3] = DBG_NULL;
229
230 // line number where defined
231 vals[4] = DtoConstInt(0);
232
233 // size in bits
234 vals[5] = LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false);
235
236 // alignment in bits
237 vals[6] = LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false);
238
239 // offset in bits
240 vals[7] = LLConstantInt::get(LLType::Int64Ty, 0, false);
241
242 // FIXME: dont know what this is
243 vals[8] = DtoConstUint(0);
244
245 // dwarf type
246 unsigned id; 102 unsigned id;
247 if (t->isintegral()) 103 if (t->isintegral())
248 { 104 {
249 if (type->isunsigned()) 105 if (type->isunsigned())
250 id = DW_ATE_unsigned; 106 id = DW_ATE_unsigned;
257 } 113 }
258 else 114 else
259 { 115 {
260 assert(0 && "unsupported basictype for debug info"); 116 assert(0 && "unsupported basictype for debug info");
261 } 117 }
262 vals[9] = DtoConstUint(id); 118
263 119 return gIR->difactory.CreateBasicType(
264 return emitDwarfGlobal(getDwarfBasicTypeType(), vals, "llvm.dbg.basictype"); 120 compileUnit, // context
265 } 121 type->toChars(), // name
266 122 llvm::DICompileUnit(NULL), // compile unit
267 ////////////////////////////////////////////////////////////////////////////////////////////////// 123 0, // line number
268 124 getTypeBitSize(T), // size (bits)
269 static LLGlobalVariable* dwarfDerivedType(Type* type, llvm::GlobalVariable* compileUnit) 125 getABITypeAlign(T)*8, // align (bits)
126 0, // offset (bits)
127 //FIXME: need flags?
128 0, // flags
129 id // encoding
130 );
131 }
132
133 //////////////////////////////////////////////////////////////////////////////////////////////////
134
135 static llvm::DIDerivedType dwarfDerivedType(Type* type, llvm::DICompileUnit compileUnit)
270 { 136 {
271 const LLType* T = DtoType(type); 137 const LLType* T = DtoType(type);
272 Type* t = type->toBasetype(); 138 Type* t = type->toBasetype();
273 139
274 // defaults 140 assert(t->ty == Tpointer && "unsupported derivedtype for debug info, only pointers allowed");
275 LLConstant* name = getNullPtr(getVoidPtrType()); 141
276 142 // find base type
277 // find tag 143 llvm::DIType basetype;
278 unsigned tag;
279 if (t->ty == Tpointer)
280 {
281 tag = DW_TAG_pointer_type;
282 }
283 else
284 {
285 assert(0 && "unsupported derivedtype for debug info");
286 }
287
288 std::vector<LLConstant*> vals(10);
289
290 // tag
291 vals[0] = DBG_TAG(tag);
292
293 // context
294 vals[1] = DBG_CAST(compileUnit);
295
296 // name
297 vals[2] = name;
298
299 // compile unit where defined
300 vals[3] = DBG_NULL;
301
302 // line number where defined
303 vals[4] = DtoConstInt(0);
304
305 // size in bits
306 vals[5] = LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false);
307
308 // alignment in bits
309 vals[6] = LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false);
310
311 // offset in bits
312 vals[7] = LLConstantInt::get(LLType::Int64Ty, 0, false);
313
314 // FIXME: dont know what this is
315 vals[8] = DtoConstUint(0);
316
317 // base type
318 Type* nt = t->nextOf(); 144 Type* nt = t->nextOf();
319 LLGlobalVariable* nTD = dwarfTypeDescription_impl(nt, compileUnit, NULL); 145 basetype = dwarfTypeDescription_impl(nt, compileUnit, NULL);
320 if (nt->ty == Tvoid || !nTD) 146 if (nt->ty == Tvoid)
321 vals[9] = DBG_NULL; 147 basetype = llvm::DIType(NULL);
322 else 148
323 vals[9] = DBG_CAST(nTD); 149 return gIR->difactory.CreateDerivedType(
324 150 DW_TAG_pointer_type, // tag
325 return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype"); 151 compileUnit, // context
326 } 152 "", // name
327 153 llvm::DICompileUnit(NULL), // compile unit
328 ////////////////////////////////////////////////////////////////////////////////////////////////// 154 0, // line number
329 155 getTypeBitSize(T), // size (bits)
330 static LLGlobalVariable* dwarfMemberType(unsigned linnum, Type* type, LLGlobalVariable* compileUnit, LLGlobalVariable* definedCU, const char* c_name, unsigned offset) 156 getABITypeAlign(T)*8, // align (bits)
157 0, // offset (bits)
158 //FIXME: need flags?
159 0, // flags
160 basetype // derived from
161 );
162 }
163
164 //////////////////////////////////////////////////////////////////////////////////////////////////
165
166 static llvm::DIDerivedType dwarfMemberType(unsigned linnum, Type* type, llvm::DICompileUnit compileUnit, llvm::DICompileUnit definedCU, const char* c_name, unsigned offset)
331 { 167 {
332 const LLType* T = DtoType(type); 168 const LLType* T = DtoType(type);
333 Type* t = type->toBasetype(); 169 Type* t = type->toBasetype();
334 170
335 // defaults 171 // find base type
336 LLConstant* name; 172 llvm::DIType basetype;
337 if (c_name) 173 basetype = dwarfTypeDescription(t, compileUnit, NULL);
338 name = DtoConstStringPtr(c_name, "llvm.metadata"); 174 if (t->ty == Tvoid)
339 else 175 basetype = llvm::DIType(NULL);
340 name = getNullPtr(getVoidPtrType()); 176
341 177 return gIR->difactory.CreateDerivedType(
342 std::vector<LLConstant*> vals(10); 178 DW_TAG_member, // tag
343 179 compileUnit, // context
344 // tag 180 c_name, // name
345 vals[0] = DBG_TAG(DW_TAG_member); 181 definedCU, // compile unit
346 182 linnum, // line number
347 // context 183 getTypeBitSize(T), // size (bits)
348 vals[1] = DBG_CAST(compileUnit); 184 getABITypeAlign(T)*8, // align (bits)
349 185 offset*8, // offset (bits)
350 // name 186 //FIXME: need flags?
351 vals[2] = name; 187 0, // flags
352 188 basetype // derived from
353 // compile unit where defined 189 );
354 if (definedCU) 190 }
355 vals[3] = DBG_CAST(definedCU); 191
356 else 192 //////////////////////////////////////////////////////////////////////////////////////////////////
357 vals[3] = DBG_NULL; 193
358 194 //FIXME: This does not use llvm's DIFactory as it can't
359 // line number where defined 195 // handle recursive types properly.
360 vals[4] = DtoConstInt(linnum); 196 static llvm::DICompositeType dwarfCompositeType(Type* type, llvm::DICompileUnit compileUnit)
361
362 // size in bits
363 vals[5] = LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false);
364
365 // alignment in bits
366 vals[6] = LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false);
367
368 // offset in bits
369 vals[7] = LLConstantInt::get(LLType::Int64Ty, offset*8, false);
370
371 // FIXME: dont know what this is
372 vals[8] = DtoConstUint(0);
373
374 // base type
375 LLGlobalVariable* nTD = dwarfTypeDescription(t, compileUnit, NULL);
376 if (t->ty == Tvoid || !nTD)
377 vals[9] = DBG_NULL;
378 else
379 vals[9] = DBG_CAST(nTD);
380
381 return emitDwarfGlobal(getDwarfDerivedTypeType(), vals, "llvm.dbg.derivedtype");
382 }
383
384 //////////////////////////////////////////////////////////////////////////////////////////////////
385
386 static LLGlobalVariable* dwarfCompositeType(Type* type, llvm::GlobalVariable* compileUnit)
387 { 197 {
388 const LLType* T = DtoType(type); 198 const LLType* T = DtoType(type);
389 Type* t = type->toBasetype(); 199 Type* t = type->toBasetype();
390 200
391 // defaults 201 // defaults
392 LLConstant* name = getNullPtr(getVoidPtrType()); 202 LLConstant* name = getNullPtr(getVoidPtrType());
393 LLGlobalVariable* members = NULL; 203 LLGlobalVariable* members = NULL;
394 unsigned linnum = 0; 204 unsigned linnum = 0;
395 LLGlobalVariable* definedCU = NULL; 205 llvm::DICompileUnit definedCU;
396 206
397 // prepare tag and members 207 // prepare tag and members
398 unsigned tag; 208 unsigned tag;
399 209
400 // declare final global variable 210 // declare final global variable
403 // dynamic array 213 // dynamic array
404 if (t->ty == Tarray) 214 if (t->ty == Tarray)
405 { 215 {
406 tag = DW_TAG_structure_type; 216 tag = DW_TAG_structure_type;
407 217
408 LLGlobalVariable* len = dwarfMemberType(0, Type::tsize_t, compileUnit, NULL, "length", 0); 218 LLGlobalVariable* len = dwarfMemberType(0, Type::tsize_t, compileUnit, llvm::DICompileUnit(NULL), "length", 0).getGV();
409 assert(len); 219 assert(len);
410 LLGlobalVariable* ptr = dwarfMemberType(0, t->nextOf()->pointerTo(), compileUnit, NULL, "ptr", global.params.is64bit?8:4); 220 LLGlobalVariable* ptr = dwarfMemberType(0, t->nextOf()->pointerTo(), compileUnit, llvm::DICompileUnit(NULL), "ptr", global.params.is64bit?8:4).getGV();
411 assert(ptr); 221 assert(ptr);
412 222
413 const LLArrayType* at = LLArrayType::get(DBG_TYPE, 2); 223 const LLArrayType* at = LLArrayType::get(DBG_TYPE, 2);
414 224
415 std::vector<LLConstant*> elems(2); 225 std::vector<LLConstant*> elems(2);
440 assert(sd); 250 assert(sd);
441 251
442 // if we don't know the aggregate's size, we don't know enough about it 252 // if we don't know the aggregate's size, we don't know enough about it
443 // to provide debug info. probably a forward-declared struct? 253 // to provide debug info. probably a forward-declared struct?
444 if (sd->sizeok == 0) 254 if (sd->sizeok == 0)
445 return NULL; 255 return llvm::DICompositeType(NULL);
446 256
447 IrStruct* ir = sd->ir.irStruct; 257 IrStruct* ir = sd->ir.irStruct;
448 assert(ir); 258 assert(ir);
449 if (ir->dwarfComposite) 259 if (!ir->diCompositeType.isNull())
450 return ir->dwarfComposite; 260 return ir->diCompositeType;
451 261
452 // set to handle recursive types properly 262 // set to handle recursive types properly
453 gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype"); 263 gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype");
454 ir->dwarfComposite = gv; 264 // set bogus initializer to satisfy asserts in DICompositeType constructor
265 gv->setInitializer(LLConstant::getNullValue(getDwarfCompositeTypeType()));
266 ir->diCompositeType = llvm::DICompositeType(gv);
455 267
456 tag = DW_TAG_structure_type; 268 tag = DW_TAG_structure_type;
457 269
458 name = DtoConstStringPtr(sd->toChars(), "llvm.metadata"); 270 name = DtoConstStringPtr(sd->toChars(), "llvm.metadata");
459 linnum = sd->loc.linnum; 271 linnum = sd->loc.linnum;
460 definedCU = DtoDwarfCompileUnit(sd->getModule()); 272 definedCU = DtoDwarfCompileUnit(sd->getCompilationModule());
461 273
462 std::vector<LLConstant*> elems; 274 std::vector<LLConstant*> elems;
463 if (!ir->aggrdecl->isInterfaceDeclaration()) // plain interfaces don't have one 275 if (!ir->aggrdecl->isInterfaceDeclaration()) // plain interfaces don't have one
464 { 276 {
465 std::vector<VarDeclaration*>& arr = ir->varDecls; 277 std::vector<VarDeclaration*>& arr = ir->varDecls;
468 for (int k=0; k<narr; k++) 280 for (int k=0; k<narr; k++)
469 { 281 {
470 VarDeclaration* vd = arr[k]; 282 VarDeclaration* vd = arr[k];
471 assert(vd); 283 assert(vd);
472 284
473 LLGlobalVariable* ptr = dwarfMemberType(vd->loc.linnum, vd->type, compileUnit, definedCU, vd->toChars(), vd->offset); 285 LLGlobalVariable* ptr = dwarfMemberType(vd->loc.linnum, vd->type, compileUnit, definedCU, vd->toChars(), vd->offset).getGV();
474 elems.push_back(DBG_CAST(ptr)); 286 elems.push_back(DBG_CAST(ptr));
475 } 287 }
476 } 288 }
477 289
478 const LLArrayType* at = LLArrayType::get(DBG_TYPE, elems.size()); 290 const LLArrayType* at = LLArrayType::get(DBG_TYPE, elems.size());
491 303
492 // tag 304 // tag
493 vals[0] = DBG_TAG(tag); 305 vals[0] = DBG_TAG(tag);
494 306
495 // context 307 // context
496 vals[1] = DBG_CAST(compileUnit); 308 vals[1] = DBG_CAST(compileUnit.getGV());
497 309
498 // name 310 // name
499 vals[2] = name; 311 vals[2] = name;
500 312
501 // compile unit where defined 313 // compile unit where defined
502 if (definedCU) 314 if (definedCU.getGV())
503 vals[3] = DBG_CAST(definedCU); 315 vals[3] = DBG_CAST(definedCU.getGV());
504 else 316 else
505 vals[3] = DBG_NULL; 317 vals[3] = DBG_NULL;
506 318
507 // line number where defined 319 // line number where defined
508 vals[4] = DtoConstInt(linnum); 320 vals[4] = DtoConstInt(linnum);
532 if (!gv) 344 if (!gv)
533 gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype"); 345 gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype");
534 LLConstant* initia = LLConstantStruct::get(getDwarfCompositeTypeType(), vals); 346 LLConstant* initia = LLConstantStruct::get(getDwarfCompositeTypeType(), vals);
535 gv->setInitializer(initia); 347 gv->setInitializer(initia);
536 348
537 return gv; 349 return llvm::DICompositeType(gv);
538 } 350 }
539 351
540 ////////////////////////////////////////////////////////////////////////////////////////////////// 352 //////////////////////////////////////////////////////////////////////////////////////////////////
541 353
542 static LLGlobalVariable* dwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 354 static llvm::DIGlobalVariable dwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd)
543 { 355 {
544 assert(vd->isDataseg()); 356 assert(vd->isDataseg());
545 LLGlobalVariable* compileUnit = DtoDwarfCompileUnit(gIR->dmodule); 357 llvm::DICompileUnit compileUnit = DtoDwarfCompileUnit(gIR->dmodule);
546 358
547 std::vector<LLConstant*> vals(12); 359 return gIR->difactory.CreateGlobalVariable(
548 vals[0] = DBG_TAG(DW_TAG_variable); 360 compileUnit, // context
549 vals[1] = DBG_CAST(getDwarfAnchor(DW_TAG_variable)); 361 vd->mangle(), // name
550 362 vd->toPrettyChars(), // displayname
551 vals[2] = DBG_CAST(compileUnit); 363 vd->toChars(), // linkage name
552 364 DtoDwarfCompileUnit(vd->getCompilationModule()), // compile unit
553 vals[3] = DtoConstStringPtr(vd->mangle(), "llvm.metadata"); 365 vd->loc.linnum, // line num
554 vals[4] = DtoConstStringPtr(vd->toPrettyChars(), "llvm.metadata"); 366 dwarfTypeDescription_impl(vd->type, compileUnit, NULL), // type
555 vals[5] = DtoConstStringPtr(vd->toChars(), "llvm.metadata"); 367 vd->protection == PROTprivate, // is local to unit
556 368 vd->getCompilationModule() == gIR->dmodule, // is definition
557 vals[6] = DBG_CAST(DtoDwarfCompileUnit(vd->getModule())); 369 ll // value
558 vals[7] = DtoConstUint(vd->loc.linnum); 370 );
559 371 }
560 LLGlobalVariable* TY = dwarfTypeDescription_impl(vd->type, compileUnit, NULL); 372
561 vals[8] = TY ? DBG_CAST(TY) : DBG_NULL; 373 //////////////////////////////////////////////////////////////////////////////////////////////////
562 vals[9] = DtoConstBool(vd->protection == PROTprivate); 374
563 vals[10] = DtoConstBool(vd->getModule() == gIR->dmodule); 375 static llvm::DIVariable dwarfVariable(VarDeclaration* vd, llvm::DIType type)
564
565 vals[11] = DBG_CAST(ll);
566
567 return emitDwarfGlobal(getDwarfGlobalVariableType(), vals, "llvm.dbg.global_variable");
568 }
569
570 //////////////////////////////////////////////////////////////////////////////////////////////////
571
572 static LLGlobalVariable* dwarfVariable(VarDeclaration* vd, LLGlobalVariable* typeDescr)
573 { 376 {
574 assert(!vd->isDataseg() && "static variable"); 377 assert(!vd->isDataseg() && "static variable");
575 378
576 unsigned tag; 379 unsigned tag;
577 if (vd->isParameter()) 380 if (vd->isParameter())
578 tag = DW_TAG_arg_variable; 381 tag = DW_TAG_arg_variable;
579 else 382 else
580 tag = DW_TAG_auto_variable; 383 tag = DW_TAG_auto_variable;
581 384
582 std::vector<LLConstant*> vals(6); 385 return gIR->difactory.CreateVariable(
583 // tag 386 tag, // tag
584 vals[0] = DBG_TAG(tag); 387 gIR->func()->diSubprogram, // context
585 // context 388 vd->toChars(), // name
586 vals[1] = DBG_CAST(gIR->func()->dwarfSubProg); 389 DtoDwarfCompileUnit(vd->getCompilationModule()), // compile unit
587 // name 390 vd->loc.linnum, // line num
588 vals[2] = DtoConstStringPtr(vd->toChars(), "llvm.metadata"); 391 type // type
589 // compile unit where defined 392 );
590 vals[3] = DBG_CAST(DtoDwarfCompileUnit(vd->getModule())); 393 }
591 // line number where defined 394
592 vals[4] = DtoConstUint(vd->loc.linnum); 395 //////////////////////////////////////////////////////////////////////////////////////////////////
593 // type descriptor 396
594 vals[5] = DBG_CAST(typeDescr); 397 static void dwarfDeclare(LLValue* var, llvm::DIVariable divar)
595 398 {
596 return emitDwarfGlobal(getDwarfVariableType(), vals, "llvm.dbg.variable"); 399 gIR->difactory.InsertDeclare(var, divar, gIR->scopebb());
597 } 400 }
598 401
599 ////////////////////////////////////////////////////////////////////////////////////////////////// 402 //////////////////////////////////////////////////////////////////////////////////////////////////
600 403
601 static void dwarfDeclare(LLValue* var, LLGlobalVariable* varDescr) 404 static llvm::DIType dwarfTypeDescription_impl(Type* type, llvm::DICompileUnit cu, const char* c_name)
602 {
603 LLSmallVector<LLValue*,2> args(2);
604 args[0] = DtoBitCast(var, DBG_TYPE);
605 args[1] = DBG_CAST(varDescr);
606 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.declare"), args.begin(), args.end());
607 }
608
609 //////////////////////////////////////////////////////////////////////////////////////////////////
610
611 static LLGlobalVariable* dwarfTypeDescription_impl(Type* type, LLGlobalVariable* cu, const char* c_name)
612 { 405 {
613 Type* t = type->toBasetype(); 406 Type* t = type->toBasetype();
614 if (t->ty == Tvoid) 407 if (t->ty == Tvoid)
615 return NULL; 408 return llvm::DIType(NULL);
616 else if (t->isintegral() || t->isfloating()) 409 else if (t->isintegral() || t->isfloating())
617 return dwarfBasicType(type, cu); 410 return dwarfBasicType(type, cu);
618 else if (t->ty == Tpointer) 411 else if (t->ty == Tpointer)
619 return dwarfDerivedType(type, cu); 412 return dwarfDerivedType(type, cu);
620 else if (t->ty == Tarray || t->ty == Tstruct || t->ty == Tclass) 413 else if (t->ty == Tarray || t->ty == Tstruct || t->ty == Tclass)
621 return dwarfCompositeType(type, cu); 414 return dwarfCompositeType(type, cu);
622 415
623 return NULL; 416 return llvm::DIType(NULL);
624 } 417 }
625 418
626 static LLGlobalVariable* dwarfTypeDescription(Type* type, LLGlobalVariable* cu, const char* c_name) 419 static llvm::DIType dwarfTypeDescription(Type* type, llvm::DICompileUnit cu, const char* c_name)
627 { 420 {
628 Type* t = type->toBasetype(); 421 Type* t = type->toBasetype();
629 if (t->ty == Tclass) 422 if (t->ty == Tclass)
630 return dwarfTypeDescription_impl(type->pointerTo(), cu, c_name); 423 return dwarfTypeDescription_impl(type->pointerTo(), cu, c_name);
631 else 424 else
638 { 431 {
639 Logger::println("D to dwarf local variable"); 432 Logger::println("D to dwarf local variable");
640 LOG_SCOPE; 433 LOG_SCOPE;
641 434
642 // get compile units 435 // get compile units
643 LLGlobalVariable* thisCU = DtoDwarfCompileUnit(gIR->dmodule); 436 llvm::DICompileUnit thisCU = DtoDwarfCompileUnit(gIR->dmodule);
644 LLGlobalVariable* varCU = thisCU; 437 llvm::DICompileUnit varCU = thisCU;
645 if (vd->getModule() != gIR->dmodule) 438 if (vd->getCompilationModule() != gIR->dmodule)
646 varCU = DtoDwarfCompileUnit(vd->getModule()); 439 varCU = DtoDwarfCompileUnit(vd->getCompilationModule());
647 440
648 // get type description 441 // get type description
649 Type* t = vd->type->toBasetype(); 442 llvm::DIType TD = dwarfTypeDescription(vd->type, thisCU, NULL);
650 LLGlobalVariable* TD = dwarfTypeDescription(vd->type, thisCU, NULL); 443 if (TD.isNull())
651 if (TD == NULL)
652 return; // unsupported 444 return; // unsupported
653 445
654 // get variable description 446 // get variable description
655 LLGlobalVariable* VD; 447 llvm::DIVariable VD = dwarfVariable(vd, TD);
656 VD = dwarfVariable(vd, TD);
657 448
658 // declare 449 // declare
659 dwarfDeclare(ll, VD); 450 dwarfDeclare(ll, VD);
660 } 451 }
661 452
662 ////////////////////////////////////////////////////////////////////////////////////////////////// 453 //////////////////////////////////////////////////////////////////////////////////////////////////
663 454
664 LLGlobalVariable* DtoDwarfCompileUnit(Module* m) 455 llvm::DICompileUnit DtoDwarfCompileUnit(Module* m)
665 { 456 {
666 Logger::println("D to dwarf compile_unit"); 457 Logger::println("D to dwarf compile_unit");
667 LOG_SCOPE; 458 LOG_SCOPE;
668 459
669 // we might be generating for an import 460 // we might be generating for an import
670 if (!m->ir.irModule) 461 if (!m->ir.irModule)
671 m->ir.irModule = new IrModule(m, m->srcfile->toChars()); 462 m->ir.irModule = new IrModule(m, m->srcfile->toChars());
672 else if (m->ir.irModule->dwarfCompileUnit) 463 else if (!m->ir.irModule->diCompileUnit.isNull())
673 { 464 {
674 if (m->ir.irModule->dwarfCompileUnit->getParent() == gIR->module) 465 assert (m->ir.irModule->diCompileUnit.getGV()->getParent() == gIR->module
675 return m->ir.irModule->dwarfCompileUnit; 466 && "debug info compile unit belongs to incorrect llvm module!");
676 } 467 return m->ir.irModule->diCompileUnit;
677 468 }
678 LLGlobalVariable* gv = dwarfCompileUnit(m); 469
679 m->ir.irModule->dwarfCompileUnit = gv; 470 // prepare srcpath
680 return gv; 471 std::string srcpath(FileName::path(m->srcfile->name->toChars()));
681 } 472 if (!FileName::absolute(srcpath.c_str())) {
682 473 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory();
683 ////////////////////////////////////////////////////////////////////////////////////////////////// 474 tmp.appendComponent(srcpath);
684 475 srcpath = tmp.toString();
685 LLGlobalVariable* DtoDwarfSubProgram(FuncDeclaration* fd) 476 if (!srcpath.empty() && *srcpath.rbegin() != '/' && *srcpath.rbegin() != '\\')
477 srcpath = srcpath + '/';
478 }
479
480 // make compile unit
481 m->ir.irModule->diCompileUnit = gIR->difactory.CreateCompileUnit(
482 global.params.symdebug == 2 ? DW_LANG_C : DW_LANG_D,
483 m->srcfile->name->toChars(),
484 srcpath,
485 "LDC (http://www.dsource.org/projects/ldc)",
486 //FIXME: What do these two mean?
487 false, // isMain,
488 false // isOptimized
489 );
490
491 return m->ir.irModule->diCompileUnit;
492 }
493
494 //////////////////////////////////////////////////////////////////////////////////////////////////
495
496 llvm::DISubprogram DtoDwarfSubProgram(FuncDeclaration* fd)
686 { 497 {
687 Logger::println("D to dwarf subprogram"); 498 Logger::println("D to dwarf subprogram");
688 LOG_SCOPE; 499 LOG_SCOPE;
689 500
501 llvm::DICompileUnit context = DtoDwarfCompileUnit(gIR->dmodule);
502 llvm::DICompileUnit definition = DtoDwarfCompileUnit(fd->getCompilationModule());
503
690 // FIXME: duplicates ? 504 // FIXME: duplicates ?
691 return dwarfSubProgram( 505 return gIR->difactory.CreateSubprogram(
692 DtoDwarfCompileUnit(gIR->dmodule), 506 context, // context
693 DtoDwarfCompileUnit(fd->getModule()), 507 fd->toPrettyChars(), // name
694 fd->toPrettyChars(), fd->mangle(), 508 fd->toPrettyChars(), // display name
695 fd->loc.linnum, 509 fd->mangle(), // linkage name
696 fd->protection == PROTprivate); 510 definition, // compile unit
697 } 511 fd->loc.linnum, // line no
698 512 //FIXME: what's this type for?
699 ////////////////////////////////////////////////////////////////////////////////////////////////// 513 llvm::DIType(NULL), // type
700 514 fd->protection == PROTprivate, // is local to unit
701 LLGlobalVariable* DtoDwarfSubProgramInternal(const char* prettyname, const char* mangledname) 515 context.getGV() == definition.getGV() // isdefinition
516 );
517 }
518
519 //////////////////////////////////////////////////////////////////////////////////////////////////
520
521 llvm::DISubprogram DtoDwarfSubProgramInternal(const char* prettyname, const char* mangledname)
702 { 522 {
703 Logger::println("D to dwarf subprogram"); 523 Logger::println("D to dwarf subprogram");
704 LOG_SCOPE; 524 LOG_SCOPE;
705 525
526 llvm::DICompileUnit context = DtoDwarfCompileUnit(gIR->dmodule);
527
706 // FIXME: duplicates ? 528 // FIXME: duplicates ?
707 return dwarfSubProgram( 529 return gIR->difactory.CreateSubprogram(
708 DtoDwarfCompileUnit(gIR->dmodule), 530 context, // context
709 DtoDwarfCompileUnit(gIR->dmodule), 531 prettyname, // name
710 prettyname, mangledname, 532 prettyname, // display name
711 0, 533 mangledname, // linkage name
712 true); 534 context, // compile unit
713 } 535 0, // line no
714 536 //FIXME: what's this type for?
715 ////////////////////////////////////////////////////////////////////////////////////////////////// 537 llvm::DIType(NULL), // type
716 538 true, // is local to unit
717 LLGlobalVariable* DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd) 539 true // isdefinition
540 );
541 }
542
543 //////////////////////////////////////////////////////////////////////////////////////////////////
544
545 llvm::DIGlobalVariable DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd)
718 { 546 {
719 Logger::println("D to dwarf global_variable"); 547 Logger::println("D to dwarf global_variable");
720 LOG_SCOPE; 548 LOG_SCOPE;
721 549
722 // FIXME: duplicates ? 550 // FIXME: duplicates ?
728 void DtoDwarfFuncStart(FuncDeclaration* fd) 556 void DtoDwarfFuncStart(FuncDeclaration* fd)
729 { 557 {
730 Logger::println("D to dwarf funcstart"); 558 Logger::println("D to dwarf funcstart");
731 LOG_SCOPE; 559 LOG_SCOPE;
732 560
733 assert(fd->ir.irFunc->dwarfSubProg); 561 assert(!fd->ir.irFunc->diSubprogram.isNull());
734 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(fd->ir.irFunc->dwarfSubProg)); 562 gIR->difactory.InsertSubprogramStart(fd->ir.irFunc->diSubprogram, gIR->scopebb());
735 } 563 }
736 564
737 ////////////////////////////////////////////////////////////////////////////////////////////////// 565 //////////////////////////////////////////////////////////////////////////////////////////////////
738 566
739 void DtoDwarfFuncEnd(FuncDeclaration* fd) 567 void DtoDwarfFuncEnd(FuncDeclaration* fd)
740 { 568 {
741 Logger::println("D to dwarf funcend"); 569 Logger::println("D to dwarf funcend");
742 LOG_SCOPE; 570 LOG_SCOPE;
743 571
744 assert(fd->ir.irFunc->dwarfSubProg); 572 assert(!fd->ir.irFunc->diSubprogram.isNull());
745 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(fd->ir.irFunc->dwarfSubProg)); 573 gIR->difactory.InsertRegionEnd(fd->ir.irFunc->diSubprogram, gIR->scopebb());
746 } 574 }
747 575
748 ////////////////////////////////////////////////////////////////////////////////////////////////// 576 //////////////////////////////////////////////////////////////////////////////////////////////////
749 577
750 void DtoDwarfStopPoint(unsigned ln) 578 void DtoDwarfStopPoint(unsigned ln)
751 { 579 {
752 Logger::println("D to dwarf stoppoint at line %u", ln); 580 Logger::println("D to dwarf stoppoint at line %u", ln);
753 LOG_SCOPE; 581 LOG_SCOPE;
754 582
755 LLSmallVector<LLValue*,3> args(3); 583 gIR->difactory.InsertStopPoint(
756 args[0] = DtoConstUint(ln); 584 DtoDwarfCompileUnit(gIR->func()->decl->getCompilationModule()), // compile unit
757 args[1] = DtoConstUint(0); 585 ln, // line no
758 FuncDeclaration* fd = gIR->func()->decl; 586 0, // col no
759 args[2] = DBG_CAST(DtoDwarfCompileUnit(fd->getModule())); 587 gIR->scopebb()
760 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.stoppoint"), args.begin(), args.end()); 588 );
761 } 589 }