1 //===-- llvm/VMCore/DebugInfoBuilder.cpp - ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the definition of the DebugInfoBuilder class, which is
11 // a helper class used to construct source level debugging information.
13 //===----------------------------------------------------------------------===//
15 #include <llvm/Support/DebugInfoBuilder.h>
16 #include <llvm/DerivedTypes.h>
17 #include <llvm/Constants.h>
18 #include <llvm/GlobalVariable.h>
19 #include <llvm/Module.h>
20 #include <llvm/Support/Dwarf.h>
21 #include <llvm/System/Path.h>
27 //===----------------------------------------------------------------------===//
28 // Debug version -- copied from MachineModuleInfo (for now), in order to avoid
29 // creating a dependency on CodeGen. These declarations really should be moved
30 // to a better place where modules can get at them without being dependent on
33 LLVMDebugVersion = (6 << 16), // Current version of debug information.
34 LLVMDebugVersion5 = (5 << 16), // Constant for version 5.
35 LLVMDebugVersion4 = (4 << 16), // Constant for version 4.
36 LLVMDebugVersionMask = 0xffff0000 // Mask for version number.
39 const char ANCHOR_TYPE_NAME[] = "llvm.dbg.anchor.type";
40 const char COMPILE_UNIT_ANCHOR_NAME[] = "llvm.dbg.compile_units";
41 const char GLOBAL_VAR_ANCHOR_NAME[] = "llvm.dbg.global_variables";
42 const char SUBPROGRAM_ANCHOR_NAME[] = "llvm.dbg.subprograms";
43 const char COMPILE_UNIT_TYPE_NAME[] = "llvm.dbg.compile_unit.type";
44 const char COMPILE_UNIT_NAME[] = "llvm.dbg.compile_unit";
45 const char SUBPROGRAM_NAME[] = "llvm.dbg.subprogram";
46 const char BASICTYPE_NAME[] = "llvm.dbg.basictype";
47 const char DERIVEDTYPE_NAME[] = "llvm.dbg.derivedtype";
49 } // end anonymous namespace
51 DebugInfoBuilder::DebugInfoBuilder() {
52 anyPtrType = PointerType::getUnqual(StructType::get(NULL, NULL));
53 anchorType = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
56 GlobalVariable * DebugInfoBuilder::createAnchor(unsigned anchorTag,
57 const char * anchorName) {
59 std::vector<Constant *> values;
60 values.push_back(ConstantInt::get(Type::Int32Ty, LLVMDebugVersion));
61 values.push_back(ConstantInt::get(Type::Int32Ty, anchorTag));
63 return new GlobalVariable(anchorType, true, GlobalValue::LinkOnceLinkage,
64 ConstantStruct::get(anchorType, values), anchorName, module);
67 // Calculate the size of the specified LLVM type.
68 Constant * DebugInfoBuilder::getSize(const Type * type) {
69 Constant * one = ConstantInt::get(Type::Int32Ty, 1);
70 return ConstantExpr::getPtrToInt(
71 ConstantExpr::getGetElementPtr(
72 ConstantPointerNull::get(PointerType::getUnqual(type)),
73 &one, 1), Type::Int32Ty);
76 Constant * DebugInfoBuilder::getAlignment(const Type * type) {
77 // Calculates the alignment of T using "sizeof({i8, T}) - sizeof(T)"
78 return ConstantExpr::getSub(
79 getSize(StructType::get(Type::Int8Ty, type, NULL)),
83 void DebugInfoBuilder::setModule(Module * m) {
85 module->addTypeName(ANCHOR_TYPE_NAME, anchorType);
87 compileUnitAnchor = module->getGlobalVariable(COMPILE_UNIT_ANCHOR_NAME);
88 if (compileUnitAnchor == NULL) {
90 createAnchor(dwarf::DW_TAG_compile_unit, COMPILE_UNIT_ANCHOR_NAME);
93 globalVariableAnchor = module->getGlobalVariable(GLOBAL_VAR_ANCHOR_NAME);
94 if (globalVariableAnchor == NULL) {
95 globalVariableAnchor =
96 createAnchor(dwarf::DW_TAG_compile_unit, GLOBAL_VAR_ANCHOR_NAME);
99 subprogramAnchor = module->getGlobalVariable(SUBPROGRAM_ANCHOR_NAME);
100 if (subprogramAnchor == NULL) {
102 createAnchor(dwarf::DW_TAG_compile_unit, SUBPROGRAM_ANCHOR_NAME);
105 compileUnit = module->getGlobalVariable(COMPILE_UNIT_NAME);
106 setContext(compileUnit);
109 GlobalVariable * DebugInfoBuilder::createCompileUnitDescriptor(unsigned langId,
110 const sys::Path & srcPath, const std::string & producer) {
112 if (compileUnit == NULL) {
113 std::vector<Constant *> values;
114 values.push_back(ConstantInt::get(
115 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_compile_unit));
117 ConstantExpr::getBitCast(compileUnitAnchor, anyPtrType));
118 values.push_back(ConstantInt::get(Type::Int32Ty, langId));
119 values.push_back(ConstantArray::get(srcPath.getLast()));
120 values.push_back(ConstantArray::get(srcPath.getDirname() + "/"));
121 values.push_back(ConstantArray::get(producer));
123 Constant * structVal = ConstantStruct::get(values, false);
124 compileUnit = new GlobalVariable(structVal->getType(), true,
125 GlobalValue::InternalLinkage, structVal, COMPILE_UNIT_NAME, module);
128 setContext(compileUnit);
132 GlobalVariable * DebugInfoBuilder::createSubProgramDescriptor(
133 const std::string & name,
134 const std::string & qualifiedName,
136 GlobalVariable * typeDesc,
140 assert(compileUnit != NULL);
141 assert(subprogramAnchor != NULL);
143 std::vector<Constant *> values;
144 values.push_back(ConstantInt::get(
145 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_subprogram));
146 values.push_back(ConstantExpr::getBitCast(subprogramAnchor, anyPtrType));
147 values.push_back(ConstantExpr::getBitCast(context, anyPtrType));
148 values.push_back(ConstantArray::get(name));
149 values.push_back(ConstantArray::get(qualifiedName));
150 values.push_back(ConstantArray::get(qualifiedName));
151 values.push_back(ConstantExpr::getBitCast(compileUnit, anyPtrType));
152 values.push_back(ConstantInt::get(Type::Int32Ty, line));
153 values.push_back(typeDesc ?
154 ConstantExpr::getBitCast(typeDesc, anyPtrType) :
155 ConstantPointerNull::get(anyPtrType));
156 values.push_back(ConstantInt::get(Type::Int1Ty, isInternal));
157 values.push_back(ConstantInt::get(Type::Int1Ty, isDefined));
159 Constant * structVal = ConstantStruct::get(values, false);
160 return new GlobalVariable(structVal->getType(), true,
161 GlobalValue::InternalLinkage, structVal, SUBPROGRAM_NAME, module);
164 GlobalVariable * DebugInfoBuilder::createBasicTypeDescriptor(
168 unsigned alignmentInBits,
169 unsigned offsetInBits,
170 unsigned typeEncoding) {
172 std::vector<Constant *> values;
173 values.push_back(ConstantInt::get(
174 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
175 values.push_back(ConstantExpr::getBitCast(context, anyPtrType));
176 values.push_back(ConstantArray::get(name));
177 values.push_back(ConstantExpr::getBitCast(compileUnit, anyPtrType));
178 values.push_back(ConstantInt::get(Type::Int32Ty, line));
179 values.push_back(ConstantInt::get(Type::Int32Ty, sizeInBits));
180 values.push_back(ConstantInt::get(Type::Int32Ty, alignmentInBits));
181 values.push_back(ConstantInt::get(Type::Int32Ty, offsetInBits));
182 values.push_back(ConstantInt::get(Type::Int32Ty, typeEncoding));
184 Constant * structVal = ConstantStruct::get(values, false);
185 return new GlobalVariable(structVal->getType(), true,
186 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
189 GlobalVariable * DebugInfoBuilder::createIntegerTypeDescriptor(
190 std::string & name, const IntegerType * type, bool isSigned) {
192 std::vector<Constant *> values;
193 values.push_back(ConstantInt::get(
194 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
195 values.push_back(ConstantPointerNull::get(anyPtrType));
196 values.push_back(ConstantArray::get(name));
197 values.push_back(ConstantPointerNull::get(anyPtrType));
198 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
199 values.push_back(ConstantInt::get(Type::Int32Ty, type->getBitWidth()));
200 values.push_back(getAlignment(type));
201 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
202 values.push_back(ConstantInt::get(Type::Int32Ty,
203 isSigned ? dwarf::DW_ATE_signed_char : dwarf::DW_ATE_unsigned_char));
205 Constant * structVal = ConstantStruct::get(values, false);
206 return new GlobalVariable(structVal->getType(), true,
207 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
210 GlobalVariable * DebugInfoBuilder::createCharacterTypeDescriptor(
211 std::string & name, const IntegerType * type, bool isSigned) {
213 std::vector<Constant *> values;
214 values.push_back(ConstantInt::get(
215 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
216 values.push_back(ConstantPointerNull::get(anyPtrType));
217 values.push_back(ConstantArray::get(name));
218 values.push_back(ConstantPointerNull::get(anyPtrType));
219 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
220 values.push_back(ConstantInt::get(Type::Int32Ty, type->getBitWidth()));
221 values.push_back(getAlignment(type));
222 values.push_back(ConstantInt::get(Type::Int32Ty, 0/*offsetInBits*/));
223 values.push_back(ConstantInt::get(Type::Int32Ty,
224 isSigned ? dwarf::DW_ATE_signed_char : dwarf::DW_ATE_unsigned_char));
226 Constant * structVal = ConstantStruct::get(values, false);
227 return new GlobalVariable(structVal->getType(), true,
228 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
231 GlobalVariable * DebugInfoBuilder::createFloatTypeDescriptor(
232 std::string & name, const Type * type) {
234 std::vector<Constant *> values;
235 values.push_back(ConstantInt::get(
236 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
237 values.push_back(ConstantPointerNull::get(anyPtrType));
238 values.push_back(ConstantArray::get(name));
239 values.push_back(ConstantPointerNull::get(anyPtrType));
240 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
241 values.push_back(getSize(type));
242 values.push_back(getAlignment(type));
243 values.push_back(ConstantInt::get(Type::Int32Ty, 0/*offsetInBits*/));
244 values.push_back(ConstantInt::get(Type::Int32Ty, dwarf::DW_ATE_float));
246 Constant * structVal = ConstantStruct::get(values, false);
247 return new GlobalVariable(structVal->getType(), true,
248 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
251 GlobalVariable * DebugInfoBuilder::createPointerTypeDescriptor(
253 GlobalVariable * referenceType,
254 const PointerType * type,
257 std::vector<Constant *> values;
258 values.push_back(ConstantInt::get(
259 Type::Int32Ty, dwarf::DW_TAG_pointer_type + LLVMDebugVersion));
261 context ? ConstantExpr::getBitCast(context, anyPtrType) : NULL);
262 values.push_back(ConstantArray::get(name));
264 compileUnit ? ConstantExpr::getBitCast(compileUnit, anyPtrType) : NULL);
265 values.push_back(ConstantInt::get(Type::Int32Ty, line));
266 values.push_back(getSize(type));
267 values.push_back(getAlignment(type));
268 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
269 values.push_back(referenceType);
271 Constant * structVal = ConstantStruct::get(values, false);
272 return new GlobalVariable(structVal->getType(), true,
273 GlobalValue::InternalLinkage, structVal, DERIVEDTYPE_NAME, module);