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>
25 //===----------------------------------------------------------------------===//
26 // Debug version -- copied from MachineModuleInfo (for now), in order to avoid
27 // creating a dependency on CodeGen. These declarations really should be moved
28 // to a better place where modules can get at them without being dependent on
31 LLVMDebugVersion = (6 << 16), // Current version of debug information.
32 LLVMDebugVersion5 = (5 << 16), // Constant for version 5.
33 LLVMDebugVersion4 = (4 << 16), // Constant for version 4.
34 LLVMDebugVersionMask = 0xffff0000 // Mask for version number.
37 const char ANCHOR_TYPE_NAME[] = "llvm.dbg.anchor.type";
38 const char COMPILE_UNIT_ANCHOR_NAME[] = "llvm.dbg.compile_units";
39 const char GLOBAL_VAR_ANCHOR_NAME[] = "llvm.dbg.global_variables";
40 const char SUBPROGRAM_ANCHOR_NAME[] = "llvm.dbg.subprograms";
41 const char COMPILE_UNIT_TYPE_NAME[] = "llvm.dbg.compile_unit.type";
42 const char COMPILE_UNIT_NAME[] = "llvm.dbg.compile_unit";
43 const char SUBPROGRAM_NAME[] = "llvm.dbg.subprogram";
44 const char BASICTYPE_NAME[] = "llvm.dbg.basictype";
45 const char DERIVEDTYPE_NAME[] = "llvm.dbg.derivedtype";
47 DebugInfoBuilder::DebugInfoBuilder() {
48 anyPtrType = PointerType::getUnqual(StructType::get(NULL, NULL));
49 anchorType = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
52 GlobalVariable * DebugInfoBuilder::createAnchor(unsigned anchorTag,
53 const char * anchorName) {
55 std::vector<Constant *> values;
56 values.push_back(ConstantInt::get(Type::Int32Ty, LLVMDebugVersion));
57 values.push_back(ConstantInt::get(Type::Int32Ty, anchorTag));
59 return new GlobalVariable(anchorType, true, GlobalValue::LinkOnceLinkage,
60 ConstantStruct::get(anchorType, values), anchorName, module);
63 // Calculate the size of the specified LLVM type.
64 Constant * DebugInfoBuilder::getSize(const Type * type) {
65 Constant * one = ConstantInt::get(Type::Int32Ty, 1);
66 return ConstantExpr::getPtrToInt(
67 ConstantExpr::getGetElementPtr(
68 ConstantPointerNull::get(PointerType::getUnqual(type)),
69 &one, 1), Type::Int32Ty);
72 Constant * DebugInfoBuilder::getAlignment(const Type * type) {
73 // Calculates the alignment of T using "sizeof({i8, T}) - sizeof(T)"
74 return ConstantExpr::getSub(
75 getSize(StructType::get(Type::Int8Ty, type, NULL)),
79 void DebugInfoBuilder::setModule(Module * m) {
81 module->addTypeName(ANCHOR_TYPE_NAME, anchorType);
83 compileUnitAnchor = module->getGlobalVariable(COMPILE_UNIT_ANCHOR_NAME);
84 if (compileUnitAnchor == NULL) {
86 createAnchor(dwarf::DW_TAG_compile_unit, COMPILE_UNIT_ANCHOR_NAME);
89 globalVariableAnchor = module->getGlobalVariable(GLOBAL_VAR_ANCHOR_NAME);
90 if (globalVariableAnchor == NULL) {
91 globalVariableAnchor =
92 createAnchor(dwarf::DW_TAG_compile_unit, GLOBAL_VAR_ANCHOR_NAME);
95 subprogramAnchor = module->getGlobalVariable(SUBPROGRAM_ANCHOR_NAME);
96 if (subprogramAnchor == NULL) {
98 createAnchor(dwarf::DW_TAG_compile_unit, SUBPROGRAM_ANCHOR_NAME);
101 compileUnit = module->getGlobalVariable(COMPILE_UNIT_NAME);
102 setContext(compileUnit);
105 GlobalVariable * DebugInfoBuilder::createCompileUnitDescriptor(unsigned langId,
106 const sys::Path & srcPath, const std::string & producer) {
108 if (compileUnit == NULL) {
109 std::vector<Constant *> values;
110 values.push_back(ConstantInt::get(
111 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_compile_unit));
113 ConstantExpr::getBitCast(compileUnitAnchor, anyPtrType));
114 values.push_back(ConstantInt::get(Type::Int32Ty, langId));
115 values.push_back(ConstantArray::get(srcPath.getLast()));
116 values.push_back(ConstantArray::get(srcPath.getDirname() + "/"));
117 values.push_back(ConstantArray::get(producer));
119 Constant * structVal = ConstantStruct::get(values, false);
120 compileUnit = new GlobalVariable(structVal->getType(), true,
121 GlobalValue::InternalLinkage, structVal, COMPILE_UNIT_NAME, module);
124 setContext(compileUnit);
128 GlobalVariable * DebugInfoBuilder::createSubProgramDescriptor(
129 const std::string & name,
130 const std::string & qualifiedName,
132 GlobalVariable * typeDesc,
136 assert(compileUnit != NULL);
137 assert(subprogramAnchor != NULL);
139 std::vector<Constant *> values;
140 values.push_back(ConstantInt::get(
141 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_subprogram));
142 values.push_back(ConstantExpr::getBitCast(subprogramAnchor, anyPtrType));
143 values.push_back(ConstantExpr::getBitCast(context, anyPtrType));
144 values.push_back(ConstantArray::get(name));
145 values.push_back(ConstantArray::get(qualifiedName));
146 values.push_back(ConstantArray::get(qualifiedName));
147 values.push_back(ConstantExpr::getBitCast(compileUnit, anyPtrType));
148 values.push_back(ConstantInt::get(Type::Int32Ty, line));
149 values.push_back(typeDesc ?
150 ConstantExpr::getBitCast(typeDesc, anyPtrType) :
151 ConstantPointerNull::get(anyPtrType));
152 values.push_back(ConstantInt::get(Type::Int1Ty, isInternal));
153 values.push_back(ConstantInt::get(Type::Int1Ty, isDefined));
155 Constant * structVal = ConstantStruct::get(values, false);
156 return new GlobalVariable(structVal->getType(), true,
157 GlobalValue::InternalLinkage, structVal, SUBPROGRAM_NAME, module);
160 GlobalVariable * DebugInfoBuilder::createBasicTypeDescriptor(
164 unsigned alignmentInBits,
165 unsigned offsetInBits,
166 unsigned typeEncoding) {
168 std::vector<Constant *> values;
169 values.push_back(ConstantInt::get(
170 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
171 values.push_back(ConstantExpr::getBitCast(context, anyPtrType));
172 values.push_back(ConstantArray::get(name));
173 values.push_back(ConstantExpr::getBitCast(compileUnit, anyPtrType));
174 values.push_back(ConstantInt::get(Type::Int32Ty, line));
175 values.push_back(ConstantInt::get(Type::Int32Ty, sizeInBits));
176 values.push_back(ConstantInt::get(Type::Int32Ty, alignmentInBits));
177 values.push_back(ConstantInt::get(Type::Int32Ty, offsetInBits));
178 values.push_back(ConstantInt::get(Type::Int32Ty, typeEncoding));
180 Constant * structVal = ConstantStruct::get(values, false);
181 return new GlobalVariable(structVal->getType(), true,
182 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
185 GlobalVariable * DebugInfoBuilder::createIntegerTypeDescriptor(
186 std::string & name, const IntegerType * type, bool isSigned) {
188 std::vector<Constant *> values;
189 values.push_back(ConstantInt::get(
190 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
191 values.push_back(ConstantPointerNull::get(anyPtrType));
192 values.push_back(ConstantArray::get(name));
193 values.push_back(ConstantPointerNull::get(anyPtrType));
194 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
195 values.push_back(ConstantInt::get(Type::Int32Ty, type->getBitWidth()));
196 values.push_back(getAlignment(type));
197 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
198 values.push_back(ConstantInt::get(Type::Int32Ty,
199 isSigned ? dwarf::DW_ATE_signed_char : dwarf::DW_ATE_unsigned_char));
201 Constant * structVal = ConstantStruct::get(values, false);
202 return new GlobalVariable(structVal->getType(), true,
203 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
206 GlobalVariable * DebugInfoBuilder::createCharacterTypeDescriptor(
207 std::string & name, const IntegerType * type, bool isSigned) {
209 std::vector<Constant *> values;
210 values.push_back(ConstantInt::get(
211 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
212 values.push_back(ConstantPointerNull::get(anyPtrType));
213 values.push_back(ConstantArray::get(name));
214 values.push_back(ConstantPointerNull::get(anyPtrType));
215 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
216 values.push_back(ConstantInt::get(Type::Int32Ty, type->getBitWidth()));
217 values.push_back(getAlignment(type));
218 values.push_back(ConstantInt::get(Type::Int32Ty, 0/*offsetInBits*/));
219 values.push_back(ConstantInt::get(Type::Int32Ty,
220 isSigned ? dwarf::DW_ATE_signed_char : dwarf::DW_ATE_unsigned_char));
222 Constant * structVal = ConstantStruct::get(values, false);
223 return new GlobalVariable(structVal->getType(), true,
224 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
227 GlobalVariable * DebugInfoBuilder::createFloatTypeDescriptor(
228 std::string & name, const Type * type) {
230 std::vector<Constant *> values;
231 values.push_back(ConstantInt::get(
232 Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_base_type));
233 values.push_back(ConstantPointerNull::get(anyPtrType));
234 values.push_back(ConstantArray::get(name));
235 values.push_back(ConstantPointerNull::get(anyPtrType));
236 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
237 values.push_back(getSize(type));
238 values.push_back(getAlignment(type));
239 values.push_back(ConstantInt::get(Type::Int32Ty, 0/*offsetInBits*/));
240 values.push_back(ConstantInt::get(Type::Int32Ty, dwarf::DW_ATE_float));
242 Constant * structVal = ConstantStruct::get(values, false);
243 return new GlobalVariable(structVal->getType(), true,
244 GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
247 GlobalVariable * DebugInfoBuilder::createPointerTypeDescriptor(
249 GlobalVariable * referenceType,
250 const PointerType * type,
253 std::vector<Constant *> values;
254 values.push_back(ConstantInt::get(
255 Type::Int32Ty, dwarf::DW_TAG_pointer_type + LLVMDebugVersion));
257 context ? ConstantExpr::getBitCast(context, anyPtrType) : NULL);
258 values.push_back(ConstantArray::get(name));
260 compileUnit ? ConstantExpr::getBitCast(compileUnit, anyPtrType) : NULL);
261 values.push_back(ConstantInt::get(Type::Int32Ty, line));
262 values.push_back(getSize(type));
263 values.push_back(getAlignment(type));
264 values.push_back(ConstantInt::get(Type::Int32Ty, 0));
265 values.push_back(referenceType);
267 Constant * structVal = ConstantStruct::get(values, false);
268 return new GlobalVariable(structVal->getType(), true,
269 GlobalValue::InternalLinkage, structVal, DERIVEDTYPE_NAME, module);