Use stripPointerCasts when checking for AllocaInsts for the stackprotector intrinsic.
[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 using namespace llvm;
24
25 namespace {
26     
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
31 // CodeGen.
32 enum {
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.
37 };
38
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";
48
49 } // end anonymous namespace
50
51 DebugInfoBuilder::DebugInfoBuilder() {
52     anyPtrType = PointerType::getUnqual(StructType::get(NULL, NULL));
53     anchorType = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL);
54 }
55
56 GlobalVariable * DebugInfoBuilder::createAnchor(unsigned anchorTag,
57     const char * anchorName) {
58
59     std::vector<Constant *> values;
60     values.push_back(ConstantInt::get(Type::Int32Ty, LLVMDebugVersion));
61     values.push_back(ConstantInt::get(Type::Int32Ty, anchorTag));
62     
63     return new GlobalVariable(anchorType, true, GlobalValue::LinkOnceLinkage,
64         ConstantStruct::get(anchorType, values), anchorName, module);
65 }
66
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);
74 }
75     
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)),
80         getSize(type));
81 }
82     
83 void DebugInfoBuilder::setModule(Module * m) {
84     module = m;
85     module->addTypeName(ANCHOR_TYPE_NAME, anchorType);
86     
87     compileUnitAnchor = module->getGlobalVariable(COMPILE_UNIT_ANCHOR_NAME);
88     if (compileUnitAnchor == NULL) {
89         compileUnitAnchor =
90             createAnchor(dwarf::DW_TAG_compile_unit, COMPILE_UNIT_ANCHOR_NAME);
91     }
92
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);
97     }
98
99     subprogramAnchor = module->getGlobalVariable(SUBPROGRAM_ANCHOR_NAME);
100     if (subprogramAnchor == NULL) {
101         subprogramAnchor =
102             createAnchor(dwarf::DW_TAG_compile_unit, SUBPROGRAM_ANCHOR_NAME);
103     }
104     
105     compileUnit = module->getGlobalVariable(COMPILE_UNIT_NAME);
106     setContext(compileUnit);
107 }
108     
109 GlobalVariable * DebugInfoBuilder::createCompileUnitDescriptor(unsigned langId,
110     const sys::Path & srcPath, const std::string & producer) {
111
112     if (compileUnit == NULL) {
113         std::vector<Constant *> values;
114         values.push_back(ConstantInt::get(
115             Type::Int32Ty, LLVMDebugVersion + dwarf::DW_TAG_compile_unit));
116         values.push_back(
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));
122     
123         Constant * structVal = ConstantStruct::get(values, false);
124         compileUnit = new GlobalVariable(structVal->getType(), true,
125             GlobalValue::InternalLinkage, structVal, COMPILE_UNIT_NAME, module);
126     }
127
128     setContext(compileUnit);
129     return compileUnit;
130 }
131
132 GlobalVariable * DebugInfoBuilder::createSubProgramDescriptor(
133     const std::string & name,
134     const std::string & qualifiedName,
135     unsigned line,
136     GlobalVariable * typeDesc,
137     bool isInternal,
138     bool isDefined) {
139         
140     assert(compileUnit != NULL);
141     assert(subprogramAnchor != NULL);
142         
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));
158     
159     Constant * structVal = ConstantStruct::get(values, false);
160     return new GlobalVariable(structVal->getType(), true,
161         GlobalValue::InternalLinkage, structVal, SUBPROGRAM_NAME, module);
162 }
163
164 GlobalVariable * DebugInfoBuilder::createBasicTypeDescriptor(
165     std::string & name,
166     unsigned line,
167     unsigned sizeInBits,
168     unsigned alignmentInBits,
169     unsigned offsetInBits,
170     unsigned typeEncoding) {
171
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));
183
184     Constant * structVal = ConstantStruct::get(values, false);
185     return new GlobalVariable(structVal->getType(), true,
186         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
187 }
188
189 GlobalVariable * DebugInfoBuilder::createIntegerTypeDescriptor(
190     std::string & name, const IntegerType * type, bool isSigned) {
191
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));
204
205     Constant * structVal = ConstantStruct::get(values, false);
206     return new GlobalVariable(structVal->getType(), true,
207         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
208 }
209
210 GlobalVariable * DebugInfoBuilder::createCharacterTypeDescriptor(
211     std::string & name, const IntegerType * type, bool isSigned) {
212
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));
225
226     Constant * structVal = ConstantStruct::get(values, false);
227     return new GlobalVariable(structVal->getType(), true,
228         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
229 }
230
231 GlobalVariable * DebugInfoBuilder::createFloatTypeDescriptor(
232     std::string & name, const Type * type) {
233
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));
245
246     Constant * structVal = ConstantStruct::get(values, false);
247     return new GlobalVariable(structVal->getType(), true,
248         GlobalValue::InternalLinkage, structVal, BASICTYPE_NAME, module);
249 }
250
251 GlobalVariable * DebugInfoBuilder::createPointerTypeDescriptor(
252     std::string & name,
253     GlobalVariable * referenceType,
254     const PointerType * type,
255     unsigned line) {
256
257     std::vector<Constant *> values;
258     values.push_back(ConstantInt::get(
259         Type::Int32Ty, dwarf::DW_TAG_pointer_type + LLVMDebugVersion));
260     values.push_back(
261         context ? ConstantExpr::getBitCast(context, anyPtrType) : NULL);
262     values.push_back(ConstantArray::get(name));
263     values.push_back(
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);
270
271     Constant * structVal = ConstantStruct::get(values, false);
272     return new GlobalVariable(structVal->getType(), true,
273         GlobalValue::InternalLinkage, structVal, DERIVEDTYPE_NAME, module);
274 }