blob: d3b6ecb4699efee899863d23a0a22b8b70d2126c [file] [log] [blame]
Jim Laskey6af56812006-01-04 13:36:38 +00001//===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by James M. Laskey and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Jim Laskey6af56812006-01-04 13:36:38 +00009
10#include "llvm/CodeGen/MachineDebugInfo.h"
11
Jim Laskeyb3e789a2006-01-26 20:21:46 +000012#include "llvm/Constants.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
14#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey41886992006-04-07 16:34:46 +000015#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000016#include "llvm/Target/TargetInstrInfo.h"
17#include "llvm/Target/TargetMachine.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000018#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000019#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000020#include "llvm/Intrinsics.h"
21#include "llvm/Instructions.h"
22#include "llvm/Module.h"
23#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000024#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000025using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000026using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000027
28// Handle the Pass registration stuff necessary to use TargetData's.
29namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000030 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
31}
Jim Laskey063e7652006-01-17 17:31:53 +000032
Jim Laskeyb3e789a2006-01-26 20:21:46 +000033//===----------------------------------------------------------------------===//
34
Jim Laskey86cbdba2006-02-06 15:33:21 +000035/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000036/// specified value in their initializer somewhere.
37static void
38getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
39 // Scan though value users.
40 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
41 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000042 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000043 Result.push_back(GV);
44 } else if (Constant *C = dyn_cast<Constant>(*I)) {
45 // If the user is a constant variable then scan its users
46 getGlobalVariablesUsing(C, Result);
47 }
48 }
49}
50
Jim Laskey86cbdba2006-02-06 15:33:21 +000051/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
52/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000053static std::vector<GlobalVariable*>
54getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000055 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000056
57 std::vector<const Type*> FieldTypes;
58 FieldTypes.push_back(Type::UIntTy);
Jim Laskeye8c3e3b2006-03-07 20:53:47 +000059 FieldTypes.push_back(Type::UIntTy);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000060
Jim Laskey86cbdba2006-02-06 15:33:21 +000061 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000063 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000064
65 // If present and linkonce then scan for users.
66 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
67 getGlobalVariablesUsing(UseRoot, Result);
68 }
69
70 return Result;
71}
72
Jim Laskey86cbdba2006-02-06 15:33:21 +000073/// isStringValue - Return true if the given value can be coerced to a string.
74///
75static bool isStringValue(Value *V) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
77 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
78 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
79 return Init->isString();
80 }
81 } else if (Constant *C = dyn_cast<Constant>(V)) {
82 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
83 return isStringValue(GV);
84 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
85 if (CE->getOpcode() == Instruction::GetElementPtr) {
86 if (CE->getNumOperands() == 3 &&
87 cast<Constant>(CE->getOperand(1))->isNullValue() &&
88 isa<ConstantInt>(CE->getOperand(2))) {
89 return isStringValue(CE->getOperand(0));
90 }
91 }
92 }
93 }
94 return false;
95}
96
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000097/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000098///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000099static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000100 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
101 return GV;
102 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000103 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000104 return dyn_cast<GlobalVariable>(CE->getOperand(0));
105 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000106 }
107 return NULL;
108}
109
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000110/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000111/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000112static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000113 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
114 return true;
115 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000116 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000117 return isa<GlobalVariable>(CE->getOperand(0));
118 }
119 }
120 return false;
121}
122
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000123/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000124///
Reid Spencerb83eb642006-10-20 07:07:24 +0000125static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000126 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000127 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000128
Jim Laskey86cbdba2006-02-06 15:33:21 +0000129 // Get the initializer constant.
130 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000131 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000132
Jim Laskey86cbdba2006-02-06 15:33:21 +0000133 // Check if there is at least i + 1 operands.
134 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000135 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000136
Jim Laskey86cbdba2006-02-06 15:33:21 +0000137 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000138 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000139}
Reid Spencerb83eb642006-10-20 07:07:24 +0000140
Jim Laskey86cbdba2006-02-06 15:33:21 +0000141//===----------------------------------------------------------------------===//
142
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000143/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000144/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000145void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000146 DD->ApplyToFields(this);
147}
148
149//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000150/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
151/// the supplied DebugInfoDesc.
152class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000153private:
154 unsigned Count; // Running count of fields.
155
156public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000157 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000158
159 // Accessors.
160 unsigned getCount() const { return Count; }
161
162 /// Apply - Count each of the fields.
163 ///
164 virtual void Apply(int &Field) { ++Count; }
165 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000166 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000167 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000168 virtual void Apply(bool &Field) { ++Count; }
169 virtual void Apply(std::string &Field) { ++Count; }
170 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
171 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000172 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
173 ++Count;
174 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000175};
176
177//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000178/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
179/// supplied DebugInfoDesc.
180class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000181private:
182 DIDeserializer &DR; // Active deserializer.
183 unsigned I; // Current operand index.
184 ConstantStruct *CI; // GlobalVariable constant initializer.
185
186public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000187 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
188 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000189 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000190 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000191 , CI(cast<ConstantStruct>(GV->getInitializer()))
192 {}
193
194 /// Apply - Set the value of each of the fields.
195 ///
196 virtual void Apply(int &Field) {
197 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000198 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000199 }
200 virtual void Apply(unsigned &Field) {
201 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000202 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000203 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000204 virtual void Apply(int64_t &Field) {
205 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000206 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000207 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000208 virtual void Apply(uint64_t &Field) {
209 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000210 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000211 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000212 virtual void Apply(bool &Field) {
213 Constant *C = CI->getOperand(I++);
214 Field = cast<ConstantBool>(C)->getValue();
215 }
216 virtual void Apply(std::string &Field) {
217 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000218 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000219 }
220 virtual void Apply(DebugInfoDesc *&Field) {
221 Constant *C = CI->getOperand(I++);
222 Field = DR.Deserialize(C);
223 }
224 virtual void Apply(GlobalVariable *&Field) {
225 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000226 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000227 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000228 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000229 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000230 Constant *C = CI->getOperand(I++);
231 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000232 if (GV->hasInitializer()) {
233 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
234 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
235 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
236 DebugInfoDesc *DE = DR.Deserialize(GVE);
237 Field.push_back(DE);
238 }
239 } else if (GV->getInitializer()->isNullValue()) {
240 if (const ArrayType *T =
241 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
242 Field.resize(T->getNumElements());
243 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000244 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000245 }
246 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000247};
248
249//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000250/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000251/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000252class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000253private:
254 DISerializer &SR; // Active serializer.
255 std::vector<Constant*> &Elements; // Element accumulator.
256
257public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000258 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
259 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000260 , SR(S)
261 , Elements(E)
262 {}
263
264 /// Apply - Set the value of each of the fields.
265 ///
266 virtual void Apply(int &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000267 Elements.push_back(ConstantInt::get(Type::IntTy, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000268 }
269 virtual void Apply(unsigned &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000270 Elements.push_back(ConstantInt::get(Type::UIntTy, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000271 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000272 virtual void Apply(int64_t &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000273 Elements.push_back(ConstantInt::get(Type::LongTy, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000274 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000275 virtual void Apply(uint64_t &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000276 Elements.push_back(ConstantInt::get(Type::ULongTy, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000277 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000278 virtual void Apply(bool &Field) {
279 Elements.push_back(ConstantBool::get(Field));
280 }
281 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000282 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000283 }
284 virtual void Apply(DebugInfoDesc *&Field) {
285 GlobalVariable *GV = NULL;
286
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000287 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000288 if (Field) GV = SR.Serialize(Field);
289
290 // FIXME - At some point should use specific type.
291 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
292
293 if (GV) {
294 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000295 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000296 } else {
297 // Use NULL.
298 Elements.push_back(ConstantPointerNull::get(EmptyTy));
299 }
300 }
301 virtual void Apply(GlobalVariable *&Field) {
302 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000303 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000304 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000305 } else {
306 Elements.push_back(ConstantPointerNull::get(EmptyTy));
307 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000308 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000309 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
310 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
311 unsigned N = Field.size();
312 ArrayType *AT = ArrayType::get(EmptyTy, N);
313 std::vector<Constant *> ArrayElements;
314
315 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000316 if (DebugInfoDesc *Element = Field[i]) {
317 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000318 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000319 ArrayElements.push_back(cast<Constant>(CE));
320 } else {
321 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
322 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000323 }
324
325 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000326 GlobalVariable *CAGV = new GlobalVariable(AT, true,
327 GlobalValue::InternalLinkage,
328 CA, "llvm.dbg.array",
329 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000330 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000331 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000332 Elements.push_back(CAE);
333 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000334};
335
336//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000337/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000338/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000339class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000340private:
341 DISerializer &SR; // Active serializer.
342 std::vector<const Type*> &Fields; // Type accumulator.
343
344public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000345 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
346 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000347 , SR(S)
348 , Fields(F)
349 {}
350
351 /// Apply - Set the value of each of the fields.
352 ///
353 virtual void Apply(int &Field) {
354 Fields.push_back(Type::IntTy);
355 }
356 virtual void Apply(unsigned &Field) {
357 Fields.push_back(Type::UIntTy);
358 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000359 virtual void Apply(int64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000360 Fields.push_back(Type::LongTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000361 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000362 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000363 Fields.push_back(Type::ULongTy);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000364 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000365 virtual void Apply(bool &Field) {
366 Fields.push_back(Type::BoolTy);
367 }
368 virtual void Apply(std::string &Field) {
369 Fields.push_back(SR.getStrPtrType());
370 }
371 virtual void Apply(DebugInfoDesc *&Field) {
372 // FIXME - At some point should use specific type.
373 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
374 Fields.push_back(EmptyTy);
375 }
376 virtual void Apply(GlobalVariable *&Field) {
377 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
378 Fields.push_back(EmptyTy);
379 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000380 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
381 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
382 Fields.push_back(EmptyTy);
383 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000384};
385
386//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000387/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000388/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000389class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000390private:
391 DIVerifier &VR; // Active verifier.
392 bool IsValid; // Validity status.
393 unsigned I; // Current operand index.
394 ConstantStruct *CI; // GlobalVariable constant initializer.
395
396public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000397 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
398 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000399 , VR(V)
400 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000401 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000402 , CI(cast<ConstantStruct>(GV->getInitializer()))
403 {
404 }
405
406 // Accessors.
407 bool isValid() const { return IsValid; }
408
409 /// Apply - Set the value of each of the fields.
410 ///
411 virtual void Apply(int &Field) {
412 Constant *C = CI->getOperand(I++);
413 IsValid = IsValid && isa<ConstantInt>(C);
414 }
415 virtual void Apply(unsigned &Field) {
416 Constant *C = CI->getOperand(I++);
417 IsValid = IsValid && isa<ConstantInt>(C);
418 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000419 virtual void Apply(int64_t &Field) {
420 Constant *C = CI->getOperand(I++);
421 IsValid = IsValid && isa<ConstantInt>(C);
422 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000423 virtual void Apply(uint64_t &Field) {
424 Constant *C = CI->getOperand(I++);
425 IsValid = IsValid && isa<ConstantInt>(C);
426 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000427 virtual void Apply(bool &Field) {
428 Constant *C = CI->getOperand(I++);
429 IsValid = IsValid && isa<ConstantBool>(C);
430 }
431 virtual void Apply(std::string &Field) {
432 Constant *C = CI->getOperand(I++);
Jim Laskey21407982006-03-14 18:37:57 +0000433 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000434 }
435 virtual void Apply(DebugInfoDesc *&Field) {
436 // FIXME - Prepare the correct descriptor.
437 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000438 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000439 }
440 virtual void Apply(GlobalVariable *&Field) {
441 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000442 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000443 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000444 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
445 Constant *C = CI->getOperand(I++);
446 IsValid = IsValid && isGlobalVariable(C);
447 if (!IsValid) return;
448
449 GlobalVariable *GV = getGlobalVariable(C);
450 IsValid = IsValid && GV && GV->hasInitializer();
451 if (!IsValid) return;
452
453 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
454 IsValid = IsValid && CA;
455 if (!IsValid) return;
456
457 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
458 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
459 if (!IsValid) return;
460
461 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
462 VR.Verify(GVE);
463 }
464 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000465};
466
Jim Laskeyce72b172006-02-11 01:01:30 +0000467
Jim Laskey86cbdba2006-02-06 15:33:21 +0000468//===----------------------------------------------------------------------===//
469
Jim Laskeyed4e5662006-06-14 14:45:39 +0000470/// TagFromGlobal - Returns the tag number from a debug info descriptor
471/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000472unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000473 ConstantInt *C = getUIntOperand(GV, 0);
474 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000475 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000476}
477
478/// VersionFromGlobal - Returns the version number from a debug info
479/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
480/// int.
481unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000482 ConstantInt *C = getUIntOperand(GV, 0);
483 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000484 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000485}
486
487/// DescFactory - Create an instance of debug info descriptor based on Tag.
488/// Return NULL if not a recognized Tag.
489DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
490 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000491 case DW_TAG_anchor: return new AnchorDesc();
492 case DW_TAG_compile_unit: return new CompileUnitDesc();
493 case DW_TAG_variable: return new GlobalVariableDesc();
494 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000495 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000496 case DW_TAG_base_type: return new BasicTypeDesc();
497 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000498 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000499 case DW_TAG_reference_type:
500 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000501 case DW_TAG_volatile_type:
502 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000503 case DW_TAG_member:
504 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000505 case DW_TAG_array_type:
506 case DW_TAG_structure_type:
507 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000508 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000509 case DW_TAG_vector_type:
510 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000511 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000512 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000513 case DW_TAG_return_variable:
514 case DW_TAG_arg_variable:
515 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000516 default: break;
517 }
518 return NULL;
519}
520
521/// getLinkage - get linkage appropriate for this type of descriptor.
522///
523GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
524 return GlobalValue::InternalLinkage;
525}
526
527/// ApplyToFields - Target the vistor to the fields of the descriptor.
528///
529void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
530 Visitor->Apply(Tag);
531}
532
533//===----------------------------------------------------------------------===//
534
Jim Laskey9c4447a2006-03-01 20:39:36 +0000535AnchorDesc::AnchorDesc()
536: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000537, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000538{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000539AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000540: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000541, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000542{}
543
544// Implement isa/cast/dyncast.
545bool AnchorDesc::classof(const DebugInfoDesc *D) {
546 return D->getTag() == DW_TAG_anchor;
547}
548
Jim Laskeyce72b172006-02-11 01:01:30 +0000549/// getLinkage - get linkage appropriate for this type of descriptor.
550///
551GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
552 return GlobalValue::LinkOnceLinkage;
553}
554
555/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
556///
557void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
558 DebugInfoDesc::ApplyToFields(Visitor);
559
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000560 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000561}
562
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000563/// getDescString - Return a string used to compose global names and labels. A
564/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000565/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000566/// to the list of names left external in the internalizer.
567/// ExternalNames.insert("llvm.dbg.compile_units");
568/// ExternalNames.insert("llvm.dbg.global_variables");
569/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000570const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000571 switch (AnchorTag) {
572 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
573 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
574 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
575 default: break;
576 }
577
578 assert(0 && "Tag does not have a case for anchor string");
579 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000580}
581
582/// getTypeString - Return a string used to label this descriptors type.
583///
584const char *AnchorDesc::getTypeString() const {
585 return "llvm.dbg.anchor.type";
586}
587
588#ifndef NDEBUG
589void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000590 cerr << getDescString() << " "
591 << "Version(" << getVersion() << "), "
592 << "Tag(" << getTag() << "), "
593 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000594}
595#endif
596
597//===----------------------------------------------------------------------===//
598
599AnchoredDesc::AnchoredDesc(unsigned T)
600: DebugInfoDesc(T)
601, Anchor(NULL)
602{}
603
604/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
605///
606void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
607 DebugInfoDesc::ApplyToFields(Visitor);
608
Jim Laskey7089f452006-06-16 13:14:03 +0000609 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000610}
611
612//===----------------------------------------------------------------------===//
613
614CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000615: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000616, Language(0)
617, FileName("")
618, Directory("")
619, Producer("")
620{}
621
Jim Laskey9c4447a2006-03-01 20:39:36 +0000622// Implement isa/cast/dyncast.
623bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
624 return D->getTag() == DW_TAG_compile_unit;
625}
626
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000627/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
628///
629void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000630 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000631
632 // Handle cases out of sync with compiler.
633 if (getVersion() == 0) {
634 unsigned DebugVersion;
635 Visitor->Apply(DebugVersion);
636 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000637
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000638 Visitor->Apply(Language);
639 Visitor->Apply(FileName);
640 Visitor->Apply(Directory);
641 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000642}
643
Jim Laskeyce72b172006-02-11 01:01:30 +0000644/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000645///
Jim Laskeyce72b172006-02-11 01:01:30 +0000646const char *CompileUnitDesc::getDescString() const {
647 return "llvm.dbg.compile_unit";
648}
649
650/// getTypeString - Return a string used to label this descriptors type.
651///
652const char *CompileUnitDesc::getTypeString() const {
653 return "llvm.dbg.compile_unit.type";
654}
655
656/// getAnchorString - Return a string used to label this descriptor's anchor.
657///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000658const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000659const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000660 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000661}
662
Jim Laskey86cbdba2006-02-06 15:33:21 +0000663#ifndef NDEBUG
664void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000665 cerr << getDescString() << " "
666 << "Version(" << getVersion() << "), "
667 << "Tag(" << getTag() << "), "
668 << "Anchor(" << getAnchor() << "), "
669 << "Language(" << Language << "), "
670 << "FileName(\"" << FileName << "\"), "
671 << "Directory(\"" << Directory << "\"), "
672 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000673}
674#endif
675
676//===----------------------------------------------------------------------===//
677
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000678TypeDesc::TypeDesc(unsigned T)
679: DebugInfoDesc(T)
680, Context(NULL)
681, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000682, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000683, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000684, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000685, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000686, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000687, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000688{}
689
Jim Laskey69906002006-02-24 16:46:40 +0000690/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000691///
692void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
693 DebugInfoDesc::ApplyToFields(Visitor);
694
695 Visitor->Apply(Context);
696 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000697 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000698 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000699 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000700 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000701 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000702 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000703}
704
705/// getDescString - Return a string used to compose global names and labels.
706///
707const char *TypeDesc::getDescString() const {
708 return "llvm.dbg.type";
709}
710
711/// getTypeString - Return a string used to label this descriptor's type.
712///
713const char *TypeDesc::getTypeString() const {
714 return "llvm.dbg.type.type";
715}
716
717#ifndef NDEBUG
718void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000719 cerr << getDescString() << " "
720 << "Version(" << getVersion() << "), "
721 << "Tag(" << getTag() << "), "
722 << "Context(" << Context << "), "
723 << "Name(\"" << Name << "\"), "
724 << "File(" << File << "), "
725 << "Line(" << Line << "), "
726 << "Size(" << Size << "), "
727 << "Align(" << Align << "), "
728 << "Offset(" << Offset << "), "
729 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000730}
731#endif
732
733//===----------------------------------------------------------------------===//
734
735BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000736: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000737, Encoding(0)
738{}
739
Jim Laskey9c4447a2006-03-01 20:39:36 +0000740// Implement isa/cast/dyncast.
741bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
742 return D->getTag() == DW_TAG_base_type;
743}
744
Jim Laskey69906002006-02-24 16:46:40 +0000745/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000746///
747void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
748 TypeDesc::ApplyToFields(Visitor);
749
750 Visitor->Apply(Encoding);
751}
752
Jim Laskeyf8913f12006-03-01 17:53:02 +0000753/// getDescString - Return a string used to compose global names and labels.
754///
755const char *BasicTypeDesc::getDescString() const {
756 return "llvm.dbg.basictype";
757}
758
759/// getTypeString - Return a string used to label this descriptor's type.
760///
761const char *BasicTypeDesc::getTypeString() const {
762 return "llvm.dbg.basictype.type";
763}
764
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000765#ifndef NDEBUG
766void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000767 cerr << getDescString() << " "
768 << "Version(" << getVersion() << "), "
769 << "Tag(" << getTag() << "), "
770 << "Context(" << getContext() << "), "
771 << "Name(\"" << getName() << "\"), "
772 << "Size(" << getSize() << "), "
773 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000774}
775#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000776
Jim Laskey434b40b2006-02-23 22:37:30 +0000777//===----------------------------------------------------------------------===//
778
Jim Laskey69906002006-02-24 16:46:40 +0000779DerivedTypeDesc::DerivedTypeDesc(unsigned T)
780: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000781, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000782{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000783
Jim Laskey9c4447a2006-03-01 20:39:36 +0000784// Implement isa/cast/dyncast.
785bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
786 unsigned T = D->getTag();
787 switch (T) {
788 case DW_TAG_typedef:
789 case DW_TAG_pointer_type:
790 case DW_TAG_reference_type:
791 case DW_TAG_const_type:
792 case DW_TAG_volatile_type:
793 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000794 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000795 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000796 return true;
797 default: break;
798 }
799 return false;
800}
801
Jim Laskey69906002006-02-24 16:46:40 +0000802/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000803///
Jim Laskey69906002006-02-24 16:46:40 +0000804void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000805 TypeDesc::ApplyToFields(Visitor);
806
Jim Laskey7089f452006-06-16 13:14:03 +0000807 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000808}
809
Jim Laskeyf8913f12006-03-01 17:53:02 +0000810/// getDescString - Return a string used to compose global names and labels.
811///
812const char *DerivedTypeDesc::getDescString() const {
813 return "llvm.dbg.derivedtype";
814}
815
816/// getTypeString - Return a string used to label this descriptor's type.
817///
818const char *DerivedTypeDesc::getTypeString() const {
819 return "llvm.dbg.derivedtype.type";
820}
821
Jim Laskey434b40b2006-02-23 22:37:30 +0000822#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000823void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000824 cerr << getDescString() << " "
825 << "Version(" << getVersion() << "), "
826 << "Tag(" << getTag() << "), "
827 << "Context(" << getContext() << "), "
828 << "Name(\"" << getName() << "\"), "
829 << "Size(" << getSize() << "), "
830 << "File(" << getFile() << "), "
831 << "Line(" << getLine() << "), "
832 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000833}
834#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000835
836//===----------------------------------------------------------------------===//
837
Jim Laskeyf8913f12006-03-01 17:53:02 +0000838CompositeTypeDesc::CompositeTypeDesc(unsigned T)
839: DerivedTypeDesc(T)
840, Elements()
841{}
842
Jim Laskey9c4447a2006-03-01 20:39:36 +0000843// Implement isa/cast/dyncast.
844bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
845 unsigned T = D->getTag();
846 switch (T) {
847 case DW_TAG_array_type:
848 case DW_TAG_structure_type:
849 case DW_TAG_union_type:
850 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000851 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000852 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000853 return true;
854 default: break;
855 }
856 return false;
857}
858
Jim Laskeyf8913f12006-03-01 17:53:02 +0000859/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
860///
861void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000862 DerivedTypeDesc::ApplyToFields(Visitor);
863
Jim Laskeyf8913f12006-03-01 17:53:02 +0000864 Visitor->Apply(Elements);
865}
866
867/// getDescString - Return a string used to compose global names and labels.
868///
869const char *CompositeTypeDesc::getDescString() const {
870 return "llvm.dbg.compositetype";
871}
872
873/// getTypeString - Return a string used to label this descriptor's type.
874///
875const char *CompositeTypeDesc::getTypeString() const {
876 return "llvm.dbg.compositetype.type";
877}
878
879#ifndef NDEBUG
880void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000881 cerr << getDescString() << " "
882 << "Version(" << getVersion() << "), "
883 << "Tag(" << getTag() << "), "
884 << "Context(" << getContext() << "), "
885 << "Name(\"" << getName() << "\"), "
886 << "Size(" << getSize() << "), "
887 << "File(" << getFile() << "), "
888 << "Line(" << getLine() << "), "
889 << "FromType(" << getFromType() << "), "
890 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000891}
892#endif
893
894//===----------------------------------------------------------------------===//
895
896SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000897: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000898, Lo(0)
899, Hi(0)
900{}
901
Jim Laskey9c4447a2006-03-01 20:39:36 +0000902// Implement isa/cast/dyncast.
903bool SubrangeDesc::classof(const DebugInfoDesc *D) {
904 return D->getTag() == DW_TAG_subrange_type;
905}
906
Jim Laskeyf8913f12006-03-01 17:53:02 +0000907/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
908///
909void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
910 DebugInfoDesc::ApplyToFields(Visitor);
911
912 Visitor->Apply(Lo);
913 Visitor->Apply(Hi);
914}
915
916/// getDescString - Return a string used to compose global names and labels.
917///
918const char *SubrangeDesc::getDescString() const {
919 return "llvm.dbg.subrange";
920}
921
922/// getTypeString - Return a string used to label this descriptor's type.
923///
924const char *SubrangeDesc::getTypeString() const {
925 return "llvm.dbg.subrange.type";
926}
927
928#ifndef NDEBUG
929void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000930 cerr << getDescString() << " "
931 << "Version(" << getVersion() << "), "
932 << "Tag(" << getTag() << "), "
933 << "Lo(" << Lo << "), "
934 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000935}
936#endif
937
938//===----------------------------------------------------------------------===//
939
Jim Laskey6a3eb012006-03-01 23:52:37 +0000940EnumeratorDesc::EnumeratorDesc()
941: DebugInfoDesc(DW_TAG_enumerator)
942, Name("")
943, Value(0)
944{}
945
946// Implement isa/cast/dyncast.
947bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
948 return D->getTag() == DW_TAG_enumerator;
949}
950
951/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
952///
953void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
954 DebugInfoDesc::ApplyToFields(Visitor);
955
956 Visitor->Apply(Name);
957 Visitor->Apply(Value);
958}
959
960/// getDescString - Return a string used to compose global names and labels.
961///
962const char *EnumeratorDesc::getDescString() const {
963 return "llvm.dbg.enumerator";
964}
965
966/// getTypeString - Return a string used to label this descriptor's type.
967///
968const char *EnumeratorDesc::getTypeString() const {
969 return "llvm.dbg.enumerator.type";
970}
971
972#ifndef NDEBUG
973void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000974 cerr << getDescString() << " "
975 << "Version(" << getVersion() << "), "
976 << "Tag(" << getTag() << "), "
977 << "Name(" << Name << "), "
978 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +0000979}
980#endif
981
982//===----------------------------------------------------------------------===//
983
Jim Laskeyb8509c52006-03-23 18:07:55 +0000984VariableDesc::VariableDesc(unsigned T)
985: DebugInfoDesc(T)
986, Context(NULL)
987, Name("")
988, File(NULL)
989, Line(0)
990, TyDesc(0)
991{}
992
993// Implement isa/cast/dyncast.
994bool VariableDesc::classof(const DebugInfoDesc *D) {
995 unsigned T = D->getTag();
996 switch (T) {
997 case DW_TAG_auto_variable:
998 case DW_TAG_arg_variable:
999 case DW_TAG_return_variable:
1000 return true;
1001 default: break;
1002 }
1003 return false;
1004}
1005
1006/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1007///
1008void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1009 DebugInfoDesc::ApplyToFields(Visitor);
1010
1011 Visitor->Apply(Context);
1012 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001013 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001014 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001015 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001016}
1017
1018/// getDescString - Return a string used to compose global names and labels.
1019///
1020const char *VariableDesc::getDescString() const {
1021 return "llvm.dbg.variable";
1022}
1023
1024/// getTypeString - Return a string used to label this descriptor's type.
1025///
1026const char *VariableDesc::getTypeString() const {
1027 return "llvm.dbg.variable.type";
1028}
1029
1030#ifndef NDEBUG
1031void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001032 cerr << getDescString() << " "
1033 << "Version(" << getVersion() << "), "
1034 << "Tag(" << getTag() << "), "
1035 << "Context(" << Context << "), "
1036 << "Name(\"" << Name << "\"), "
1037 << "File(" << File << "), "
1038 << "Line(" << Line << "), "
1039 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001040}
1041#endif
1042
1043//===----------------------------------------------------------------------===//
1044
Jim Laskeyce72b172006-02-11 01:01:30 +00001045GlobalDesc::GlobalDesc(unsigned T)
1046: AnchoredDesc(T)
1047, Context(0)
1048, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001049, FullName("")
1050, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001051, File(NULL)
1052, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001053, TyDesc(NULL)
1054, IsStatic(false)
1055, IsDefinition(false)
1056{}
1057
1058/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001059///
Jim Laskeyce72b172006-02-11 01:01:30 +00001060void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1061 AnchoredDesc::ApplyToFields(Visitor);
1062
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001063 Visitor->Apply(Context);
1064 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001065 Visitor->Apply(FullName);
1066 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001067 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001068 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001069 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001070 Visitor->Apply(IsStatic);
1071 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001072}
1073
1074//===----------------------------------------------------------------------===//
1075
1076GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001077: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001078, Global(NULL)
1079{}
1080
Jim Laskey9c4447a2006-03-01 20:39:36 +00001081// Implement isa/cast/dyncast.
1082bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1083 return D->getTag() == DW_TAG_variable;
1084}
1085
Jim Laskeyce72b172006-02-11 01:01:30 +00001086/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1087///
1088void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1089 GlobalDesc::ApplyToFields(Visitor);
1090
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001091 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001092}
1093
Jim Laskeyce72b172006-02-11 01:01:30 +00001094/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001095///
Jim Laskeyce72b172006-02-11 01:01:30 +00001096const char *GlobalVariableDesc::getDescString() const {
1097 return "llvm.dbg.global_variable";
1098}
1099
1100/// getTypeString - Return a string used to label this descriptors type.
1101///
1102const char *GlobalVariableDesc::getTypeString() const {
1103 return "llvm.dbg.global_variable.type";
1104}
1105
1106/// getAnchorString - Return a string used to label this descriptor's anchor.
1107///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001108const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001109const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001110 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001111}
1112
Jim Laskey86cbdba2006-02-06 15:33:21 +00001113#ifndef NDEBUG
1114void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001115 cerr << getDescString() << " "
1116 << "Version(" << getVersion() << "), "
1117 << "Tag(" << getTag() << "), "
1118 << "Anchor(" << getAnchor() << "), "
1119 << "Name(\"" << getName() << "\"), "
1120 << "FullName(\"" << getFullName() << "\"), "
1121 << "LinkageName(\"" << getLinkageName() << "\"), "
1122 << "File(" << getFile() << "),"
1123 << "Line(" << getLine() << "),"
1124 << "Type(" << getType() << "), "
1125 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1126 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1127 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001128}
1129#endif
1130
1131//===----------------------------------------------------------------------===//
1132
Jim Laskeyce72b172006-02-11 01:01:30 +00001133SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001134: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001135{}
1136
Jim Laskey9c4447a2006-03-01 20:39:36 +00001137// Implement isa/cast/dyncast.
1138bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1139 return D->getTag() == DW_TAG_subprogram;
1140}
1141
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001142/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001143/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001144void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001145 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001146}
1147
Jim Laskeyce72b172006-02-11 01:01:30 +00001148/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001149///
Jim Laskeyce72b172006-02-11 01:01:30 +00001150const char *SubprogramDesc::getDescString() const {
1151 return "llvm.dbg.subprogram";
1152}
1153
1154/// getTypeString - Return a string used to label this descriptors type.
1155///
1156const char *SubprogramDesc::getTypeString() const {
1157 return "llvm.dbg.subprogram.type";
1158}
1159
1160/// getAnchorString - Return a string used to label this descriptor's anchor.
1161///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001162const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001163const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001164 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001165}
1166
Jim Laskey86cbdba2006-02-06 15:33:21 +00001167#ifndef NDEBUG
1168void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001169 cerr << getDescString() << " "
1170 << "Version(" << getVersion() << "), "
1171 << "Tag(" << getTag() << "), "
1172 << "Anchor(" << getAnchor() << "), "
1173 << "Name(\"" << getName() << "\"), "
1174 << "FullName(\"" << getFullName() << "\"), "
1175 << "LinkageName(\"" << getLinkageName() << "\"), "
1176 << "File(" << getFile() << "),"
1177 << "Line(" << getLine() << "),"
1178 << "Type(" << getType() << "), "
1179 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1180 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001181}
1182#endif
1183
Jim Laskey45ccae52006-02-28 20:15:07 +00001184//===----------------------------------------------------------------------===//
1185
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001186BlockDesc::BlockDesc()
1187: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001188, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001189{}
1190
1191// Implement isa/cast/dyncast.
1192bool BlockDesc::classof(const DebugInfoDesc *D) {
1193 return D->getTag() == DW_TAG_lexical_block;
1194}
1195
1196/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1197///
1198void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1199 DebugInfoDesc::ApplyToFields(Visitor);
1200
Jim Laskeyb8509c52006-03-23 18:07:55 +00001201 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001202}
1203
1204/// getDescString - Return a string used to compose global names and labels.
1205///
1206const char *BlockDesc::getDescString() const {
1207 return "llvm.dbg.block";
1208}
1209
1210/// getTypeString - Return a string used to label this descriptors type.
1211///
1212const char *BlockDesc::getTypeString() const {
1213 return "llvm.dbg.block.type";
1214}
1215
1216#ifndef NDEBUG
1217void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001218 cerr << getDescString() << " "
1219 << "Version(" << getVersion() << "), "
1220 << "Tag(" << getTag() << "),"
1221 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001222}
1223#endif
1224
1225//===----------------------------------------------------------------------===//
1226
Jim Laskey86cbdba2006-02-06 15:33:21 +00001227DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001228 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001229}
1230DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001231 // Handle NULL.
1232 if (!GV) return NULL;
1233
Jim Laskey86cbdba2006-02-06 15:33:21 +00001234 // Check to see if it has been already deserialized.
1235 DebugInfoDesc *&Slot = GlobalDescs[GV];
1236 if (Slot) return Slot;
1237
1238 // Get the Tag from the global.
1239 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1240
Jim Laskey86cbdba2006-02-06 15:33:21 +00001241 // Create an empty instance of the correct sort.
1242 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001243
Jim Laskey21407982006-03-14 18:37:57 +00001244 // If not a user defined descriptor.
1245 if (Slot) {
1246 // Deserialize the fields.
1247 DIDeserializeVisitor DRAM(*this, GV);
1248 DRAM.ApplyToFields(Slot);
1249 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001250
1251 return Slot;
1252}
1253
1254//===----------------------------------------------------------------------===//
1255
1256/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001257///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001258const PointerType *DISerializer::getStrPtrType() {
1259 // If not already defined.
1260 if (!StrPtrTy) {
1261 // Construct the pointer to signed bytes.
1262 StrPtrTy = PointerType::get(Type::SByteTy);
1263 }
1264
1265 return StrPtrTy;
1266}
1267
1268/// getEmptyStructPtrType - Return a "{ }*" type.
1269///
1270const PointerType *DISerializer::getEmptyStructPtrType() {
1271 // If not already defined.
1272 if (!EmptyStructPtrTy) {
1273 // Construct the empty structure type.
1274 const StructType *EmptyStructTy =
1275 StructType::get(std::vector<const Type*>());
1276 // Construct the pointer to empty structure type.
1277 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1278 }
1279
1280 return EmptyStructPtrTy;
1281}
1282
1283/// getTagType - Return the type describing the specified descriptor (via tag.)
1284///
1285const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1286 // Attempt to get the previously defined type.
1287 StructType *&Ty = TagTypes[DD->getTag()];
1288
1289 // If not already defined.
1290 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001291 // Set up fields vector.
1292 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001293 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001294 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001295 GTAM.ApplyToFields(DD);
1296
1297 // Construct structured type.
1298 Ty = StructType::get(Fields);
1299
Jim Laskey86cbdba2006-02-06 15:33:21 +00001300 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001301 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001302 }
1303
1304 return Ty;
1305}
1306
1307/// getString - Construct the string as constant string global.
1308///
Jim Laskeyce72b172006-02-11 01:01:30 +00001309Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001310 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001311 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001312 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001313 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001314 // If empty string then use a sbyte* null instead.
1315 if (String.empty()) {
1316 Slot = ConstantPointerNull::get(getStrPtrType());
1317 } else {
1318 // Construct string as an llvm constant.
1319 Constant *ConstStr = ConstantArray::get(String);
1320 // Otherwise create and return a new string global.
1321 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1322 GlobalVariable::InternalLinkage,
1323 ConstStr, "str", M);
1324 StrGV->setSection("llvm.metadata");
1325 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001326 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001327 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001328 return Slot;
1329
Jim Laskey86cbdba2006-02-06 15:33:21 +00001330}
1331
1332/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1333/// so that it can be serialized to a .bc or .ll file.
1334GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1335 // Check if the DebugInfoDesc is already in the map.
1336 GlobalVariable *&Slot = DescGlobals[DD];
1337
1338 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1339 if (Slot) return Slot;
1340
Jim Laskey86cbdba2006-02-06 15:33:21 +00001341 // Get the type associated with the Tag.
1342 const StructType *Ty = getTagType(DD);
1343
1344 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001345 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1346 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001347 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001348
1349 // Insert new GlobalVariable in DescGlobals map.
1350 Slot = GV;
1351
1352 // Set up elements vector
1353 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001354 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001355 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001356 SRAM.ApplyToFields(DD);
1357
1358 // Set the globals initializer.
1359 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1360
1361 return GV;
1362}
1363
1364//===----------------------------------------------------------------------===//
1365
Jim Laskey86cbdba2006-02-06 15:33:21 +00001366/// Verify - Return true if the GlobalVariable appears to be a valid
1367/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001368bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001369 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001370}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001371bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001372 // NULLs are valid.
1373 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001374
Jim Laskey98e04102006-03-26 22:45:20 +00001375 // Check prior validity.
1376 unsigned &ValiditySlot = Validity[GV];
1377
1378 // If visited before then use old state.
1379 if (ValiditySlot) return ValiditySlot == Valid;
1380
1381 // Assume validity for the time being (recursion.)
1382 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001383
1384 // Make sure the global is internal or link once (anchor.)
1385 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1386 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1387 ValiditySlot = Invalid;
1388 return false;
1389 }
Jim Laskey98e04102006-03-26 22:45:20 +00001390
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001391 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001392 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001393
1394 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001395 if (Tag == DW_TAG_invalid) {
1396 ValiditySlot = Valid;
1397 return true;
1398 }
1399
1400 // Get the Version.
1401 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1402
1403 // Check for version mismatch.
1404 if (Version != LLVMDebugVersion) {
1405 ValiditySlot = Invalid;
1406 return false;
1407 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001408
Jim Laskey86cbdba2006-02-06 15:33:21 +00001409 // Construct an empty DebugInfoDesc.
1410 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001411
1412 // Allow for user defined descriptors.
1413 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001414
1415 // Get the initializer constant.
1416 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1417
1418 // Get the operand count.
1419 unsigned N = CI->getNumOperands();
1420
1421 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001422 unsigned &CountSlot = Counts[Tag];
1423 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001424 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001425 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001426 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001427 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001428 }
1429
Jim Laskey21407982006-03-14 18:37:57 +00001430 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001431 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001432 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001433 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001434 return false;
1435 }
1436
1437 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001438 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001439 VRAM.ApplyToFields(DD);
1440
1441 // Release empty DebugInfoDesc.
1442 delete DD;
1443
Jim Laskey98e04102006-03-26 22:45:20 +00001444 // If fields are not valid.
1445 if (!VRAM.isValid()) {
1446 ValiditySlot = Invalid;
1447 return false;
1448 }
1449
1450 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001451}
1452
1453//===----------------------------------------------------------------------===//
1454
Jim Laskeyb8509c52006-03-23 18:07:55 +00001455DebugScope::~DebugScope() {
1456 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1457 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1458}
1459
1460//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001461
1462MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001463: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001464, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001465, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001466, Directories()
1467, SourceFiles()
1468, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001469, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001470, ScopeMap()
1471, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001472, FrameMoves()
1473{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001474MachineDebugInfo::~MachineDebugInfo() {
1475
1476}
1477
Jim Laskeyb2efb852006-01-04 22:28:25 +00001478/// doInitialization - Initialize the debug state for a new module.
1479///
1480bool MachineDebugInfo::doInitialization() {
1481 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001482}
1483
Jim Laskeyb2efb852006-01-04 22:28:25 +00001484/// doFinalization - Tear down the debug state after completion of a module.
1485///
1486bool MachineDebugInfo::doFinalization() {
1487 return false;
1488}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001489
Jim Laskey41886992006-04-07 16:34:46 +00001490/// BeginFunction - Begin gathering function debug information.
1491///
1492void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1493 // Coming soon.
1494}
1495
1496/// MachineDebugInfo::EndFunction - Discard function debug information.
1497///
1498void MachineDebugInfo::EndFunction() {
1499 // Clean up scope information.
1500 if (RootScope) {
1501 delete RootScope;
1502 ScopeMap.clear();
1503 RootScope = NULL;
1504 }
1505
1506 // Clean up frame info.
1507 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1508 FrameMoves.clear();
1509}
1510
Jim Laskeyd96185a2006-02-13 12:50:39 +00001511/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001512///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001513// FIXME - use new Value type when available.
1514DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001515 return DR.Deserialize(V);
1516}
1517
1518/// Verify - Verify that a Value is debug information descriptor.
1519///
1520bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001521 return VR.Verify(V);
1522}
1523
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001524/// AnalyzeModule - Scan the module for global debug information.
1525///
1526void MachineDebugInfo::AnalyzeModule(Module &M) {
1527 SetupCompileUnits(M);
1528}
1529
1530/// SetupCompileUnits - Set up the unique vector of compile units.
1531///
1532void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001533 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001534
Jim Laskey0420f2a2006-02-22 19:02:11 +00001535 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1536 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001537 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001538}
1539
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001540/// getCompileUnits - Return a vector of debug compile units.
1541///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001542const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001543 return CompileUnits;
1544}
1545
Jim Laskey0420f2a2006-02-22 19:02:11 +00001546/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1547/// named GlobalVariable.
1548std::vector<GlobalVariable*>
1549MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1550 const std::string &RootName) {
1551 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001552}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001553
1554/// RecordLabel - Records location information and associates it with a
1555/// debug label. Returns a unique label ID used to generate a label and
1556/// provide correspondence to the source line list.
1557unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1558 unsigned Source) {
1559 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001560 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001561 return ID;
1562}
1563
1564/// RecordSource - Register a source file with debug info. Returns an source
1565/// ID.
1566unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1567 const std::string &Source) {
1568 unsigned DirectoryID = Directories.insert(Directory);
1569 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1570}
1571unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1572 return RecordSource(CompileUnit->getDirectory(),
1573 CompileUnit->getFileName());
1574}
1575
1576/// RecordRegionStart - Indicate the start of a region.
1577///
1578unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1579 // FIXME - need to be able to handle split scopes because of bb cloning.
1580 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1581 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1582 unsigned ID = NextLabelID();
1583 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1584 return ID;
1585}
1586
1587/// RecordRegionEnd - Indicate the end of a region.
1588///
1589unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1590 // FIXME - need to be able to handle split scopes because of bb cloning.
1591 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1592 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1593 unsigned ID = NextLabelID();
1594 Scope->setEndLabelID(ID);
1595 return ID;
1596}
1597
1598/// RecordVariable - Indicate the declaration of a local variable.
1599///
1600void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1601 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1602 DebugScope *Scope = getOrCreateScope(VD->getContext());
1603 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1604 Scope->AddVariable(DV);
1605}
1606
1607/// getOrCreateScope - Returns the scope associated with the given descriptor.
1608///
1609DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1610 DebugScope *&Slot = ScopeMap[ScopeDesc];
1611 if (!Slot) {
1612 // FIXME - breaks down when the context is an inlined function.
1613 DebugInfoDesc *ParentDesc = NULL;
1614 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1615 ParentDesc = Block->getContext();
1616 }
1617 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1618 Slot = new DebugScope(Parent, ScopeDesc);
1619 if (Parent) {
1620 Parent->AddScope(Slot);
1621 } else if (RootScope) {
1622 // FIXME - Add inlined function scopes to the root so we can delete
1623 // them later. Long term, handle inlined functions properly.
1624 RootScope->AddScope(Slot);
1625 } else {
1626 // First function is top level function.
1627 RootScope = Slot;
1628 }
1629 }
1630 return Slot;
1631}
1632
Jim Laskey9d4209f2006-11-07 19:33:46 +00001633//===----------------------------------------------------------------------===//
1634/// DebugLabelFolding pass - This pass prunes out redundant debug labels. This
1635/// allows a debug emitter to determine if the range of two labels is empty,
1636/// by seeing if the labels map to the same reduced label.
1637
1638namespace llvm {
1639
1640struct DebugLabelFolder : public MachineFunctionPass {
1641 virtual bool runOnMachineFunction(MachineFunction &MF);
1642 virtual const char *getPassName() const { return "Debug Label Folder"; }
1643};
1644
1645bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1646 // Get machine debug info.
1647 MachineDebugInfo *MDI = getAnalysisToUpdate<MachineDebugInfo>();
1648 if (!MDI) return false;
1649 // Get target instruction info.
1650 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1651 if (!TII) return false;
1652 // Get target version of the debug label opcode.
1653 unsigned DWARF_LABELOpc = TII->getDWARF_LABELOpcode();
1654 if (!DWARF_LABELOpc) return false;
1655
1656 // Track if change is made.
1657 bool MadeChange = false;
1658 // No prior label to begin.
1659 unsigned PriorLabel = 0;
1660
1661 // Iterate through basic blocks.
1662 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1663 BB != E; ++BB) {
1664 // Iterate through instructions.
1665 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1666 // Is it a debug label.
1667 if ((unsigned)I->getOpcode() == DWARF_LABELOpc) {
1668 // The label ID # is always operand #0, an immediate.
1669 unsigned NextLabel = I->getOperand(0).getImm();
1670
1671 // If there was an immediate prior label.
1672 if (PriorLabel) {
1673 // Remap the current label to prior label.
1674 MDI->RemapLabel(NextLabel, PriorLabel);
1675 // Delete the current label.
1676 I = BB->erase(I);
1677 // Indicate a change has been made.
1678 MadeChange = true;
1679 continue;
1680 } else {
1681 // Start a new round.
1682 PriorLabel = NextLabel;
1683 }
1684 } else {
1685 // No consecutive labels.
1686 PriorLabel = 0;
1687 }
1688
1689 ++I;
1690 }
1691 }
1692
1693 return MadeChange;
1694}
1695
1696FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1697
1698}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001699