Add DebugInfoBuilder. Patch by Talin!
[oota-llvm.git] / lib / VMCore / DebugInfoBuilder.cpp
1 //===-- llvm/VMCore/DebugInfoBuilder.cpp - ----------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the definition of the DebugInfoBuilder class, which is
11 // a helper class used to construct source level debugging information.
12 //
13 //===----------------------------------------------------------------------===//
14
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>
22
23 namespace llvm {
24     
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
29 // CodeGen.
30 enum {
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.
35 };
36
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";
46
47 DebugInfoBuilder::DebugInfoBuilder() {
48     anyPtrType = PointerType::getUnqual(StructType::get(NULL, NULL));
49     anchorType = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
50 }
51
52 GlobalVariable * DebugInfoBuilder::createAnchor(unsigned anchorTag,
53     const char * anchorName) {
54
55     std::vector<Constant *> values;
56     values.push_back(ConstantInt::get(Type::Int32Ty, LLVMDebugVersion));
57     values.push_back(ConstantInt::get(Type::Int32Ty, anchorTag));
58     
59     return new GlobalVariable(anchorType, true, GlobalValue::LinkOnceLinkage,
60         ConstantStruct::get(anchorType, values), anchorName, module);
61 }
62
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);
70 }
71     
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)),
76         getSize(type));
77 }
78     
79 void DebugInfoBuilder::setModule(Module * m) {
80     module = m;
81     module->addTypeName(ANCHOR_TYPE_NAME, anchorType);
82     
83     compileUnitAnchor = module->getGlobalVariable(COMPILE_UNIT_ANCHOR_NAME);
84     if (compileUnitAnchor == NULL) {
85         compileUnitAnchor =
86             createAnchor(dwarf::DW_TAG_compile_unit, COMPILE_UNIT_ANCHOR_NAME);
87     }
88
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);
93     }
94
95     subprogramAnchor = module->getGlobalVariable(SUBPROGRAM_ANCHOR_NAME);
96     if (subprogramAnchor == NULL) {
97         subprogramAnchor =
98             createAnchor(dwarf::DW_TAG_compile_unit, SUBPROGRAM_ANCHOR_NAME);
99     }
100     
101     compileUnit = module->getGlobalVariable(COMPILE_UNIT_NAME);
102     setContext(compileUnit);
103 }
104     
105 GlobalVariable * DebugInfoBuilder::createCompileUnitDescriptor(unsigned langId,
106     const sys::Path & srcPath, const std::string & producer) {
107
108     if (compileUnit == NULL) {
109         std::vector<Constant *> values;
110         values.push_back(ConstantInt::get(
111             Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_compile_unit));
112         values.push_back(
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));
118     
119         Constant * structVal = ConstantStruct::get(values, false);
120         compileUnit = new GlobalVariable(structVal->getType(), true,
121             GlobalValue::InternalLinkage, structVal, COMPILE_UNIT_NAME, module);
122     }
123
124     setContext(compileUnit);
125     return compileUnit;
126 }
127
128 GlobalVariable * DebugInfoBuilder::createSubProgramDescriptor(
129     const std::string & name,
130     const std::string & qualifiedName,
131     unsigned line,
132     GlobalVariable * typeDesc,
133     bool isInternal,
134     bool isDefined) {
135         
136     assert(compileUnit != NULL);
137     assert(subprogramAnchor != NULL);
138         
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));
154     
155     Constant * structVal = ConstantStruct::get(values, false);
156     return new GlobalVariable(structVal->getType(), true,
157         GlobalValue::InternalLinkage, structVal, SUBPROGRAM_NAME, module);
158 }
159
160 GlobalVariable * DebugInfoBuilder::createBasicTypeDescriptor(
161     std::string & name,
162     unsigned line,
163     unsigned sizeInBits,
164     unsigned alignmentInBits,
165     unsigned offsetInBits,
166     unsigned typeEncoding) {
167
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));
179
180     Constant * structVal = ConstantStruct::get(values, false);
181     return new GlobalVariable(structVal->getType(), true,
182         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
183 }
184
185 GlobalVariable * DebugInfoBuilder::createIntegerTypeDescriptor(
186     std::string & name, const IntegerType * type, bool isSigned) {
187
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));
200
201     Constant * structVal = ConstantStruct::get(values, false);
202     return new GlobalVariable(structVal->getType(), true,
203         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
204 }
205
206 GlobalVariable * DebugInfoBuilder::createCharacterTypeDescriptor(
207     std::string & name, const IntegerType * type, bool isSigned) {
208
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));
221
222     Constant * structVal = ConstantStruct::get(values, false);
223     return new GlobalVariable(structVal->getType(), true,
224         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
225 }
226
227 GlobalVariable * DebugInfoBuilder::createFloatTypeDescriptor(
228     std::string & name, const Type * type) {
229
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));
241
242     Constant * structVal = ConstantStruct::get(values, false);
243     return new GlobalVariable(structVal->getType(), true,
244         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
245 }
246
247 GlobalVariable * DebugInfoBuilder::createPointerTypeDescriptor(
248     std::string & name,
249     GlobalVariable * referenceType,
250     const PointerType * type,
251     unsigned line) {
252
253     std::vector<Constant *> values;
254     values.push_back(ConstantInt::get(
255         Type::Int32Ty, dwarf::DW_TAG_pointer_type + LLVMDebugVersion));
256     values.push_back(
257         context ? ConstantExpr::getBitCast(context, anyPtrType) : NULL);
258     values.push_back(ConstantArray::get(name));
259     values.push_back(
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);
266
267     Constant * structVal = ConstantStruct::get(values, false);
268     return new GlobalVariable(structVal->getType(), true,
269         GlobalValue::InternalLinkage, structVal, DERIVEDTYPE_NAME, module);
270 }
271
272 }