comparison gen/todebug.cpp @ 244:a95056b3c996 trunk

[svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB. Did a lot of smaller cleans up here and there. Replaced more llvm::Foo with LLFoo for common stuff. Split up tollvm.cpp.
author lindquist
date Mon, 09 Jun 2008 09:37:08 +0200
parents 0806379a5eca
children d61ce72c39ab
comparison
equal deleted inserted replaced
243:4d006f7b2ada 244:a95056b3c996
20 return llvm::PointerType::get(t, 0); 20 return llvm::PointerType::get(t, 0);
21 } 21 }
22 22
23 static const llvm::PointerType* dbgArrTy() 23 static const llvm::PointerType* dbgArrTy()
24 { 24 {
25 std::vector<const LLType*> t; 25 return ptrTy(llvm::StructType::get(NULL,NULL));
26 return ptrTy(llvm::StructType::get(t));
27 } 26 }
28 27
29 static LLConstant* dbgToArrTy(LLConstant* c) 28 static LLConstant* dbgToArrTy(LLConstant* c)
30 { 29 {
31 Logger::cout() << "casting: " << *c << '\n';
32 return llvm::ConstantExpr::getBitCast(c, dbgArrTy()); 30 return llvm::ConstantExpr::getBitCast(c, dbgArrTy());
33 } 31 }
34 32
35 #define Ty(X) llvm::Type::X 33 #define Ty(X) LLType::X
36 34
37 ////////////////////////////////////////////////////////////////////////////////////////////////// 35 //////////////////////////////////////////////////////////////////////////////////////////////////
38 36
39 static llvm::GlobalVariable* dbg_compile_units = 0; 37 static llvm::GlobalVariable* dbg_compile_units = 0;
40 static llvm::GlobalVariable* dbg_global_variables = 0; 38 static llvm::GlobalVariable* dbg_global_variables = 0;
46 %llvm.dbg.anchor.type = type { 44 %llvm.dbg.anchor.type = type {
47 uint, ;; Tag = 0 + LLVMDebugVersion 45 uint, ;; Tag = 0 + LLVMDebugVersion
48 uint ;; Tag of descriptors grouped by the anchor 46 uint ;; Tag of descriptors grouped by the anchor
49 } 47 }
50 */ 48 */
51 std::vector<const LLType*> elems(2, Ty(Int32Ty)); 49
52 const llvm::StructType* t = isaStruct(gIR->module->getTypeByName("llvm.dbg.anchor.type")); 50 const llvm::StructType* t = isaStruct(gIR->module->getTypeByName("llvm.dbg.anchor.type"));
53 51
54 /* 52 /*
55 %llvm.dbg.compile_units = linkonce constant %llvm.dbg.anchor.type { uint 0, uint 17 } ;; DW_TAG_compile_unit 53 %llvm.dbg.compile_units = linkonce constant %llvm.dbg.anchor.type { uint 0, uint 17 } ;; DW_TAG_compile_unit
56 %llvm.dbg.global_variables = linkonce constant %llvm.dbg.anchor.type { uint 0, uint 52 } ;; DW_TAG_variable 54 %llvm.dbg.global_variables = linkonce constant %llvm.dbg.anchor.type { uint 0, uint 52 } ;; DW_TAG_variable
152 return gv; 150 return gv;
153 } 151 }
154 152
155 ////////////////////////////////////////////////////////////////////////////////////////////////// 153 //////////////////////////////////////////////////////////////////////////////////////////////////
156 154
157 llvm::GlobalVariable* DtoDwarfSubProgram(FuncDeclaration* fd, llvm::GlobalVariable* compileUnit) 155 LLGlobalVariable* DtoDwarfSubProgram(FuncDeclaration* fd, llvm::GlobalVariable* compileUnit)
158 { 156 {
159 std::vector<LLConstant*> vals; 157 std::vector<LLConstant*> vals;
160 vals.push_back(llvm::ConstantExpr::getAdd( 158 vals.push_back(llvm::ConstantExpr::getAdd(
161 DtoConstUint(DW_TAG_subprogram), 159 DtoConstUint(DW_TAG_subprogram),
162 DtoConstUint(llvm::LLVMDebugVersion))); 160 DtoConstUint(llvm::LLVMDebugVersion)));
201 args.push_back(DtoConstUint(0)); 199 args.push_back(DtoConstUint(0));
202 FuncDeclaration* fd = gIR->func()->decl; 200 FuncDeclaration* fd = gIR->func()->decl;
203 args.push_back(dbgToArrTy(DtoDwarfCompileUnit(fd->getModule()))); 201 args.push_back(dbgToArrTy(DtoDwarfCompileUnit(fd->getModule())));
204 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.stoppoint"), args.begin(), args.end()); 202 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.stoppoint"), args.begin(), args.end());
205 } 203 }
204
205 //////////////////////////////////////////////////////////////////////////////////////////////////
206
207 const llvm::StructType* GetDwarfBasicTypeType() {
208 return isaStruct(gIR->module->getTypeByName("llvm.dbg.basictype.type"));
209 }
210
211 //////////////////////////////////////////////////////////////////////////////////////////////////
212
213 LLGlobalVariable* DtoDwarfBasicType(Type* type, llvm::GlobalVariable* compileUnit)
214 {
215 const LLType* T = DtoType(type);
216
217 std::vector<LLConstant*> vals;
218 // tag
219 vals.push_back(llvm::ConstantExpr::getAdd(
220 DtoConstUint(DW_TAG_base_type),
221 DtoConstUint(llvm::LLVMDebugVersion)));
222 // context
223 vals.push_back(dbgToArrTy(compileUnit));
224 // name
225 vals.push_back(DtoConstStringPtr(type->toChars(), "llvm.metadata"));
226 // compile unit where defined
227 vals.push_back(getNullPtr(dbgArrTy()));
228 // line number where defined
229 vals.push_back(DtoConstInt(0));
230 // size in bits
231 vals.push_back(LLConstantInt::get(LLType::Int64Ty, getTypeBitSize(T), false));
232 // alignment in bits
233 vals.push_back(LLConstantInt::get(LLType::Int64Ty, getABITypeAlign(T)*8, false));
234 // offset in bits
235 vals.push_back(LLConstantInt::get(LLType::Int64Ty, 0, false));
236 // FIXME: dont know what this is
237 vals.push_back(DtoConstUint(0));
238 // dwarf type
239 unsigned id;
240 if (type->isintegral())
241 {
242 if (type->isunsigned())
243 id = llvm::dwarf::DW_ATE_unsigned;
244 else
245 id = llvm::dwarf::DW_ATE_signed;
246 }
247 else if (type->isfloating())
248 {
249 id = llvm::dwarf::DW_ATE_float;
250 }
251 else
252 {
253 assert(0 && "unsupported basictype for debug info");
254 }
255 vals.push_back(DtoConstUint(id));
256
257 LLConstant* c = llvm::ConstantStruct::get(GetDwarfBasicTypeType(), vals);
258 LLGlobalVariable* gv = new LLGlobalVariable(c->getType(), true, LLGlobalValue::InternalLinkage, c, "llvm.dbg.basictype", gIR->module);
259 gv->setSection("llvm.metadata");
260 return gv;
261 }
262
263 //////////////////////////////////////////////////////////////////////////////////////////////////
264
265 const llvm::StructType* GetDwarfVariableType() {
266 return isaStruct(gIR->module->getTypeByName("llvm.dbg.variable.type"));
267 }
268
269 //////////////////////////////////////////////////////////////////////////////////////////////////
270
271 LLGlobalVariable* DtoDwarfVariable(VarDeclaration* vd, LLGlobalVariable* typeDescr)
272 {
273 unsigned tag;
274 if (vd->isParameter())
275 tag = DW_TAG_arg_variable;
276 else if (vd->isCodeseg())
277 assert(0 && "a static variable");
278 else
279 tag = DW_TAG_auto_variable;
280
281 std::vector<LLConstant*> vals;
282 // tag
283 vals.push_back(llvm::ConstantExpr::getAdd(
284 DtoConstUint(tag),
285 DtoConstUint(llvm::LLVMDebugVersion)));
286 // context
287 vals.push_back(dbgToArrTy(gIR->func()->dwarfSubProg));
288 // name
289 vals.push_back(DtoConstStringPtr(vd->toChars(), "llvm.metadata"));
290 // compile unit where defined
291 vals.push_back(dbgToArrTy(DtoDwarfCompileUnit(vd->getModule())));
292 // line number where defined
293 vals.push_back(DtoConstUint(vd->loc.linnum));
294 // type descriptor
295 vals.push_back(dbgToArrTy(typeDescr));
296
297 LLConstant* c = llvm::ConstantStruct::get(GetDwarfVariableType(), vals);
298 LLGlobalVariable* gv = new LLGlobalVariable(c->getType(), true, LLGlobalValue::InternalLinkage, c, "llvm.dbg.variable", gIR->module);
299 gv->setSection("llvm.metadata");
300 return gv;
301 }
302
303 //////////////////////////////////////////////////////////////////////////////////////////////////
304
305 void DtoDwarfDeclare(LLValue* var, LLGlobalVariable* varDescr)
306 {
307 LLSmallVector<LLValue*,2> args;
308 args.push_back(DtoBitCast(var, dbgArrTy()));
309 args.push_back(dbgToArrTy(varDescr));
310 gIR->ir->CreateCall(gIR->module->getFunction("llvm.dbg.declare"), args.begin(), args.end());
311 }