blob: 98d07d3e2f889796794918d954505d5fa9d56742 [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"
13#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000014#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000015#include "llvm/Intrinsics.h"
16#include "llvm/Instructions.h"
17#include "llvm/Module.h"
18#include "llvm/Support/Dwarf.h"
19
Jim Laskey86cbdba2006-02-06 15:33:21 +000020#include <iostream>
21
Jim Laskey6af56812006-01-04 13:36:38 +000022using namespace llvm;
23
24// Handle the Pass registration stuff necessary to use TargetData's.
25namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000026 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
27}
Jim Laskey063e7652006-01-17 17:31:53 +000028
Jim Laskeyb3e789a2006-01-26 20:21:46 +000029//===----------------------------------------------------------------------===//
30
Jim Laskey86cbdba2006-02-06 15:33:21 +000031/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000032/// specified value in their initializer somewhere.
33static void
34getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
35 // Scan though value users.
36 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
37 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000038 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000039 Result.push_back(GV);
40 } else if (Constant *C = dyn_cast<Constant>(*I)) {
41 // If the user is a constant variable then scan its users
42 getGlobalVariablesUsing(C, Result);
43 }
44 }
45}
46
Jim Laskey86cbdba2006-02-06 15:33:21 +000047/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
48/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000049static std::vector<GlobalVariable*>
50getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000051 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000052
53 std::vector<const Type*> FieldTypes;
54 FieldTypes.push_back(Type::UIntTy);
55 FieldTypes.push_back(PointerType::get(Type::SByteTy));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000056
Jim Laskey86cbdba2006-02-06 15:33:21 +000057 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000058 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000059 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000060
61 // If present and linkonce then scan for users.
62 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
63 getGlobalVariablesUsing(UseRoot, Result);
64 }
65
66 return Result;
67}
68
69/// getStringValue - Turn an LLVM constant pointer that eventually points to a
70/// global into a string value. Return an empty string if we can't do it.
71///
Chris Lattner22760af2006-01-27 17:31:30 +000072static const std::string getStringValue(Value *V, unsigned Offset = 0) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000073 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
74 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
75 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
76 if (Init->isString()) {
77 std::string Result = Init->getAsString();
78 if (Offset < Result.size()) {
79 // If we are pointing INTO The string, erase the beginning...
80 Result.erase(Result.begin(), Result.begin()+Offset);
81
82 // Take off the null terminator, and any string fragments after it.
83 std::string::size_type NullPos = Result.find_first_of((char)0);
84 if (NullPos != std::string::npos)
85 Result.erase(Result.begin()+NullPos, Result.end());
86 return Result;
87 }
88 }
89 }
90 } else if (Constant *C = dyn_cast<Constant>(V)) {
91 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
92 return getStringValue(GV, Offset);
93 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
94 if (CE->getOpcode() == Instruction::GetElementPtr) {
95 // Turn a gep into the specified offset.
96 if (CE->getNumOperands() == 3 &&
97 cast<Constant>(CE->getOperand(1))->isNullValue() &&
98 isa<ConstantInt>(CE->getOperand(2))) {
99 return getStringValue(CE->getOperand(0),
100 Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
101 }
102 }
103 }
104 }
105 return "";
106}
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000107
Jim Laskey86cbdba2006-02-06 15:33:21 +0000108/// isStringValue - Return true if the given value can be coerced to a string.
109///
110static bool isStringValue(Value *V) {
111 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
112 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
113 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
114 return Init->isString();
115 }
116 } else if (Constant *C = dyn_cast<Constant>(V)) {
117 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
118 return isStringValue(GV);
119 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
120 if (CE->getOpcode() == Instruction::GetElementPtr) {
121 if (CE->getNumOperands() == 3 &&
122 cast<Constant>(CE->getOperand(1))->isNullValue() &&
123 isa<ConstantInt>(CE->getOperand(2))) {
124 return isStringValue(CE->getOperand(0));
125 }
126 }
127 }
128 }
129 return false;
130}
131
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000132/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000133///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000134static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000135 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
136 return GV;
137 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000138 if (CE->getOpcode() == Instruction::Cast) {
139 return dyn_cast<GlobalVariable>(CE->getOperand(0));
140 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000141 }
142 return NULL;
143}
144
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000145/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000146/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000147static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000148 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
149 return true;
150 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
151 if (CE->getOpcode() == Instruction::Cast) {
152 return isa<GlobalVariable>(CE->getOperand(0));
153 }
154 }
155 return false;
156}
157
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000158/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000159///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000160static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000161 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000162 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000163
Jim Laskey86cbdba2006-02-06 15:33:21 +0000164 // Get the initializer constant.
165 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000166 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000167
Jim Laskey86cbdba2006-02-06 15:33:21 +0000168 // Check if there is at least i + 1 operands.
169 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000170 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000171
Jim Laskey86cbdba2006-02-06 15:33:21 +0000172 // Check constant.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000173 return dyn_cast<ConstantUInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000174}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000175//===----------------------------------------------------------------------===//
176
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000177/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000178/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000179void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000180 DD->ApplyToFields(this);
181}
182
183//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000184/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
185/// the supplied DebugInfoDesc.
186class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000187private:
188 unsigned Count; // Running count of fields.
189
190public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000191 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000192
193 // Accessors.
194 unsigned getCount() const { return Count; }
195
196 /// Apply - Count each of the fields.
197 ///
198 virtual void Apply(int &Field) { ++Count; }
199 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000200 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000201 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000202 virtual void Apply(bool &Field) { ++Count; }
203 virtual void Apply(std::string &Field) { ++Count; }
204 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
205 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000206 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
207 ++Count;
208 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000209};
210
211//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000212/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
213/// supplied DebugInfoDesc.
214class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000215private:
216 DIDeserializer &DR; // Active deserializer.
217 unsigned I; // Current operand index.
218 ConstantStruct *CI; // GlobalVariable constant initializer.
219
220public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000221 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
222 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000223 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000224 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000225 , CI(cast<ConstantStruct>(GV->getInitializer()))
226 {}
227
228 /// Apply - Set the value of each of the fields.
229 ///
230 virtual void Apply(int &Field) {
231 Constant *C = CI->getOperand(I++);
232 Field = cast<ConstantSInt>(C)->getValue();
233 }
234 virtual void Apply(unsigned &Field) {
235 Constant *C = CI->getOperand(I++);
236 Field = cast<ConstantUInt>(C)->getValue();
237 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000238 virtual void Apply(int64_t &Field) {
239 Constant *C = CI->getOperand(I++);
240 Field = cast<ConstantSInt>(C)->getValue();
241 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000242 virtual void Apply(uint64_t &Field) {
243 Constant *C = CI->getOperand(I++);
244 Field = cast<ConstantUInt>(C)->getValue();
245 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000246 virtual void Apply(bool &Field) {
247 Constant *C = CI->getOperand(I++);
248 Field = cast<ConstantBool>(C)->getValue();
249 }
250 virtual void Apply(std::string &Field) {
251 Constant *C = CI->getOperand(I++);
252 Field = getStringValue(C);
253 }
254 virtual void Apply(DebugInfoDesc *&Field) {
255 Constant *C = CI->getOperand(I++);
256 Field = DR.Deserialize(C);
257 }
258 virtual void Apply(GlobalVariable *&Field) {
259 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000260 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000261 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000262 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
263 Constant *C = CI->getOperand(I++);
264 GlobalVariable *GV = getGlobalVariable(C);
265 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
266 Field.resize(0);
267 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
268 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
269 DebugInfoDesc *DE = DR.Deserialize(GVE);
270 Field.push_back(DE);
271 }
272 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000273};
274
275//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000276/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000277/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000278class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000279private:
280 DISerializer &SR; // Active serializer.
281 std::vector<Constant*> &Elements; // Element accumulator.
282
283public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000284 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
285 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000286 , SR(S)
287 , Elements(E)
288 {}
289
290 /// Apply - Set the value of each of the fields.
291 ///
292 virtual void Apply(int &Field) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000293 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000294 }
295 virtual void Apply(unsigned &Field) {
296 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
297 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000298 virtual void Apply(int64_t &Field) {
299 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
300 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000301 virtual void Apply(uint64_t &Field) {
302 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
303 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000304 virtual void Apply(bool &Field) {
305 Elements.push_back(ConstantBool::get(Field));
306 }
307 virtual void Apply(std::string &Field) {
308 Elements.push_back(SR.getString(Field));
309 }
310 virtual void Apply(DebugInfoDesc *&Field) {
311 GlobalVariable *GV = NULL;
312
313 // If non-NULL the convert to global.
314 if (Field) GV = SR.Serialize(Field);
315
316 // FIXME - At some point should use specific type.
317 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
318
319 if (GV) {
320 // Set to pointer to global.
321 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
322 } else {
323 // Use NULL.
324 Elements.push_back(ConstantPointerNull::get(EmptyTy));
325 }
326 }
327 virtual void Apply(GlobalVariable *&Field) {
328 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000329 if (Field) {
330 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
331 } else {
332 Elements.push_back(ConstantPointerNull::get(EmptyTy));
333 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000334 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000335 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
336 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
337 unsigned N = Field.size();
338 ArrayType *AT = ArrayType::get(EmptyTy, N);
339 std::vector<Constant *> ArrayElements;
340
341 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
342 GlobalVariable *GVE = SR.Serialize(Field[i]);
343 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
344 ArrayElements.push_back(cast<Constant>(CE));
345 }
346
347 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000348 GlobalVariable *CAGV = new GlobalVariable(AT, true,
349 GlobalValue::InternalLinkage,
350 CA, "llvm.dbg.array",
351 SR.getModule());
352 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000353 Elements.push_back(CAE);
354 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000355};
356
357//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000358/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000359/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000360class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000361private:
362 DISerializer &SR; // Active serializer.
363 std::vector<const Type*> &Fields; // Type accumulator.
364
365public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000366 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
367 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000368 , SR(S)
369 , Fields(F)
370 {}
371
372 /// Apply - Set the value of each of the fields.
373 ///
374 virtual void Apply(int &Field) {
375 Fields.push_back(Type::IntTy);
376 }
377 virtual void Apply(unsigned &Field) {
378 Fields.push_back(Type::UIntTy);
379 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000380 virtual void Apply(int64_t &Field) {
381 Fields.push_back(Type::IntTy);
382 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000383 virtual void Apply(uint64_t &Field) {
384 Fields.push_back(Type::UIntTy);
385 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000386 virtual void Apply(bool &Field) {
387 Fields.push_back(Type::BoolTy);
388 }
389 virtual void Apply(std::string &Field) {
390 Fields.push_back(SR.getStrPtrType());
391 }
392 virtual void Apply(DebugInfoDesc *&Field) {
393 // FIXME - At some point should use specific type.
394 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
395 Fields.push_back(EmptyTy);
396 }
397 virtual void Apply(GlobalVariable *&Field) {
398 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
399 Fields.push_back(EmptyTy);
400 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000401 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
402 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
403 Fields.push_back(EmptyTy);
404 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000405};
406
407//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000408/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000409/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000410class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000411private:
412 DIVerifier &VR; // Active verifier.
413 bool IsValid; // Validity status.
414 unsigned I; // Current operand index.
415 ConstantStruct *CI; // GlobalVariable constant initializer.
416
417public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000418 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
419 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000420 , VR(V)
421 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000422 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000423 , CI(cast<ConstantStruct>(GV->getInitializer()))
424 {
425 }
426
427 // Accessors.
428 bool isValid() const { return IsValid; }
429
430 /// Apply - Set the value of each of the fields.
431 ///
432 virtual void Apply(int &Field) {
433 Constant *C = CI->getOperand(I++);
434 IsValid = IsValid && isa<ConstantInt>(C);
435 }
436 virtual void Apply(unsigned &Field) {
437 Constant *C = CI->getOperand(I++);
438 IsValid = IsValid && isa<ConstantInt>(C);
439 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000440 virtual void Apply(int64_t &Field) {
441 Constant *C = CI->getOperand(I++);
442 IsValid = IsValid && isa<ConstantInt>(C);
443 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000444 virtual void Apply(uint64_t &Field) {
445 Constant *C = CI->getOperand(I++);
446 IsValid = IsValid && isa<ConstantInt>(C);
447 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000448 virtual void Apply(bool &Field) {
449 Constant *C = CI->getOperand(I++);
450 IsValid = IsValid && isa<ConstantBool>(C);
451 }
452 virtual void Apply(std::string &Field) {
453 Constant *C = CI->getOperand(I++);
454 IsValid = IsValid && isStringValue(C);
455 }
456 virtual void Apply(DebugInfoDesc *&Field) {
457 // FIXME - Prepare the correct descriptor.
458 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000459 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000460 }
461 virtual void Apply(GlobalVariable *&Field) {
462 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000463 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000464 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000465 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
466 Constant *C = CI->getOperand(I++);
467 IsValid = IsValid && isGlobalVariable(C);
468 if (!IsValid) return;
469
470 GlobalVariable *GV = getGlobalVariable(C);
471 IsValid = IsValid && GV && GV->hasInitializer();
472 if (!IsValid) return;
473
474 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
475 IsValid = IsValid && CA;
476 if (!IsValid) return;
477
478 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
479 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
480 if (!IsValid) return;
481
482 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
483 VR.Verify(GVE);
484 }
485 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000486};
487
Jim Laskeyce72b172006-02-11 01:01:30 +0000488
Jim Laskey86cbdba2006-02-06 15:33:21 +0000489//===----------------------------------------------------------------------===//
490
Jim Laskeyce72b172006-02-11 01:01:30 +0000491/// TagFromGlobal - Returns the Tag number from a debug info descriptor
492/// GlobalVariable.
493unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
494 ConstantUInt *C = getUIntOperand(GV, 0);
495 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
496}
497
498/// DescFactory - Create an instance of debug info descriptor based on Tag.
499/// Return NULL if not a recognized Tag.
500DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
501 switch (Tag) {
502 case DI_TAG_anchor: return new AnchorDesc();
503 case DI_TAG_compile_unit: return new CompileUnitDesc();
504 case DI_TAG_global_variable: return new GlobalVariableDesc();
505 case DI_TAG_subprogram: return new SubprogramDesc();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000506 case DI_TAG_basictype: return new BasicTypeDesc();
Jim Laskey45ccae52006-02-28 20:15:07 +0000507 case DI_TAG_typedef:
508 case DI_TAG_pointer:
509 case DI_TAG_reference:
510 case DI_TAG_const:
511 case DI_TAG_volatile:
512 case DI_TAG_restrict: return new DerivedTypeDesc(Tag);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000513 case DI_TAG_array:
514 case DI_TAG_struct:
515 case DI_TAG_union:
516 case DI_TAG_enum: return new CompositeTypeDesc(Tag);
517 case DI_TAG_subrange: return new SubrangeDesc();
Jim Laskeyce72b172006-02-11 01:01:30 +0000518 default: break;
519 }
520 return NULL;
521}
522
523/// getLinkage - get linkage appropriate for this type of descriptor.
524///
525GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
526 return GlobalValue::InternalLinkage;
527}
528
529/// ApplyToFields - Target the vistor to the fields of the descriptor.
530///
531void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
532 Visitor->Apply(Tag);
533}
534
535//===----------------------------------------------------------------------===//
536
537/// getLinkage - get linkage appropriate for this type of descriptor.
538///
539GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
540 return GlobalValue::LinkOnceLinkage;
541}
542
543/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
544///
545void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
546 DebugInfoDesc::ApplyToFields(Visitor);
547
548 Visitor->Apply(Name);
549}
550
551/// getDescString - Return a string used to compose global names and labels.
552///
553const char *AnchorDesc::getDescString() const {
554 return Name.c_str();
555}
556
557/// getTypeString - Return a string used to label this descriptors type.
558///
559const char *AnchorDesc::getTypeString() const {
560 return "llvm.dbg.anchor.type";
561}
562
563#ifndef NDEBUG
564void AnchorDesc::dump() {
565 std::cerr << getDescString() << " "
566 << "Tag(" << getTag() << "), "
567 << "Name(" << Name << ")\n";
568}
569#endif
570
571//===----------------------------------------------------------------------===//
572
573AnchoredDesc::AnchoredDesc(unsigned T)
574: DebugInfoDesc(T)
575, Anchor(NULL)
576{}
577
578/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
579///
580void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
581 DebugInfoDesc::ApplyToFields(Visitor);
582
583 Visitor->Apply((DebugInfoDesc *&)Anchor);
584}
585
586//===----------------------------------------------------------------------===//
587
588CompileUnitDesc::CompileUnitDesc()
589: AnchoredDesc(DI_TAG_compile_unit)
590, DebugVersion(LLVMDebugVersion)
591, Language(0)
592, FileName("")
593, Directory("")
594, Producer("")
595{}
596
Jim Laskey86cbdba2006-02-06 15:33:21 +0000597/// DebugVersionFromGlobal - Returns the version number from a compile unit
598/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000599unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000600 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000601 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000602}
603
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000604/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
605///
606void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000607 AnchoredDesc::ApplyToFields(Visitor);
608
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000609 Visitor->Apply(DebugVersion);
610 Visitor->Apply(Language);
611 Visitor->Apply(FileName);
612 Visitor->Apply(Directory);
613 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000614}
615
Jim Laskeyce72b172006-02-11 01:01:30 +0000616/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000617///
Jim Laskeyce72b172006-02-11 01:01:30 +0000618const char *CompileUnitDesc::getDescString() const {
619 return "llvm.dbg.compile_unit";
620}
621
622/// getTypeString - Return a string used to label this descriptors type.
623///
624const char *CompileUnitDesc::getTypeString() const {
625 return "llvm.dbg.compile_unit.type";
626}
627
628/// getAnchorString - Return a string used to label this descriptor's anchor.
629///
630const char *CompileUnitDesc::getAnchorString() const {
631 return "llvm.dbg.compile_units";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000632}
633
Jim Laskey86cbdba2006-02-06 15:33:21 +0000634#ifndef NDEBUG
635void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000636 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000637 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000638 << "Anchor(" << getAnchor() << "), "
639 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000640 << "Language(" << Language << "), "
641 << "FileName(\"" << FileName << "\"), "
642 << "Directory(\"" << Directory << "\"), "
643 << "Producer(\"" << Producer << "\")\n";
644}
645#endif
646
647//===----------------------------------------------------------------------===//
648
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000649TypeDesc::TypeDesc(unsigned T)
650: DebugInfoDesc(T)
651, Context(NULL)
652, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000653, File(NULL)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000654, Size(0)
655{}
656
Jim Laskey69906002006-02-24 16:46:40 +0000657/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000658///
659void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
660 DebugInfoDesc::ApplyToFields(Visitor);
661
662 Visitor->Apply(Context);
663 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000664 Visitor->Apply((DebugInfoDesc *&)File);
665 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000666 Visitor->Apply(Size);
667}
668
669/// getDescString - Return a string used to compose global names and labels.
670///
671const char *TypeDesc::getDescString() const {
672 return "llvm.dbg.type";
673}
674
675/// getTypeString - Return a string used to label this descriptor's type.
676///
677const char *TypeDesc::getTypeString() const {
678 return "llvm.dbg.type.type";
679}
680
681#ifndef NDEBUG
682void TypeDesc::dump() {
683 std::cerr << getDescString() << " "
684 << "Tag(" << getTag() << "), "
685 << "Context(" << Context << "), "
686 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000687 << "File(" << File << "), "
688 << "Line(" << Line << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000689 << "Size(" << Size << ")\n";
690}
691#endif
692
693//===----------------------------------------------------------------------===//
694
695BasicTypeDesc::BasicTypeDesc()
696: TypeDesc(DI_TAG_basictype)
697, Encoding(0)
698{}
699
Jim Laskey69906002006-02-24 16:46:40 +0000700/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701///
702void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
703 TypeDesc::ApplyToFields(Visitor);
704
705 Visitor->Apply(Encoding);
706}
707
Jim Laskeyf8913f12006-03-01 17:53:02 +0000708/// getDescString - Return a string used to compose global names and labels.
709///
710const char *BasicTypeDesc::getDescString() const {
711 return "llvm.dbg.basictype";
712}
713
714/// getTypeString - Return a string used to label this descriptor's type.
715///
716const char *BasicTypeDesc::getTypeString() const {
717 return "llvm.dbg.basictype.type";
718}
719
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000720#ifndef NDEBUG
721void BasicTypeDesc::dump() {
722 std::cerr << getDescString() << " "
723 << "Tag(" << getTag() << "), "
724 << "Context(" << getContext() << "), "
725 << "Name(\"" << getName() << "\"), "
726 << "Size(" << getSize() << "), "
727 << "Encoding(" << Encoding << ")\n";
728}
729#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000730
Jim Laskey434b40b2006-02-23 22:37:30 +0000731//===----------------------------------------------------------------------===//
732
Jim Laskey69906002006-02-24 16:46:40 +0000733DerivedTypeDesc::DerivedTypeDesc(unsigned T)
734: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000735, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000736{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000737
Jim Laskey69906002006-02-24 16:46:40 +0000738/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000739///
Jim Laskey69906002006-02-24 16:46:40 +0000740void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000741 TypeDesc::ApplyToFields(Visitor);
742
743 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000744}
745
Jim Laskeyf8913f12006-03-01 17:53:02 +0000746/// getDescString - Return a string used to compose global names and labels.
747///
748const char *DerivedTypeDesc::getDescString() const {
749 return "llvm.dbg.derivedtype";
750}
751
752/// getTypeString - Return a string used to label this descriptor's type.
753///
754const char *DerivedTypeDesc::getTypeString() const {
755 return "llvm.dbg.derivedtype.type";
756}
757
Jim Laskey434b40b2006-02-23 22:37:30 +0000758#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000759void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000760 std::cerr << getDescString() << " "
761 << "Tag(" << getTag() << "), "
762 << "Context(" << getContext() << "), "
763 << "Name(\"" << getName() << "\"), "
764 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000765 << "File(" << getFile() << "), "
766 << "Line(" << getLine() << "), "
767 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000768}
769#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000770
771//===----------------------------------------------------------------------===//
772
Jim Laskeyf8913f12006-03-01 17:53:02 +0000773CompositeTypeDesc::CompositeTypeDesc(unsigned T)
774: DerivedTypeDesc(T)
775, Elements()
776{}
777
778/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
779///
780void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
781 DerivedTypeDesc::ApplyToFields(Visitor);
782
783 Visitor->Apply(Elements);
784}
785
786/// getDescString - Return a string used to compose global names and labels.
787///
788const char *CompositeTypeDesc::getDescString() const {
789 return "llvm.dbg.compositetype";
790}
791
792/// getTypeString - Return a string used to label this descriptor's type.
793///
794const char *CompositeTypeDesc::getTypeString() const {
795 return "llvm.dbg.compositetype.type";
796}
797
798#ifndef NDEBUG
799void CompositeTypeDesc::dump() {
800 std::cerr << getDescString() << " "
801 << "Tag(" << getTag() << "), "
802 << "Context(" << getContext() << "), "
803 << "Name(\"" << getName() << "\"), "
804 << "Size(" << getSize() << "), "
805 << "File(" << getFile() << "), "
806 << "Line(" << getLine() << "), "
807 << "FromType(" << getFromType() << "), "
808 << "Elements.size(" << Elements.size() << ")\n";
809}
810#endif
811
812//===----------------------------------------------------------------------===//
813
814SubrangeDesc::SubrangeDesc()
815: DebugInfoDesc(DI_TAG_subrange)
816, Lo(0)
817, Hi(0)
818{}
819
820/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
821///
822void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
823 DebugInfoDesc::ApplyToFields(Visitor);
824
825 Visitor->Apply(Lo);
826 Visitor->Apply(Hi);
827}
828
829/// getDescString - Return a string used to compose global names and labels.
830///
831const char *SubrangeDesc::getDescString() const {
832 return "llvm.dbg.subrange";
833}
834
835/// getTypeString - Return a string used to label this descriptor's type.
836///
837const char *SubrangeDesc::getTypeString() const {
838 return "llvm.dbg.subrange.type";
839}
840
841#ifndef NDEBUG
842void SubrangeDesc::dump() {
843 std::cerr << getDescString() << " "
844 << "Tag(" << getTag() << "), "
845 << "Lo(" << Lo << "), "
846 << "Hi(" << Hi << ")\n";
847}
848#endif
849
850//===----------------------------------------------------------------------===//
851
Jim Laskeyce72b172006-02-11 01:01:30 +0000852GlobalDesc::GlobalDesc(unsigned T)
853: AnchoredDesc(T)
854, Context(0)
855, Name("")
856, TyDesc(NULL)
857, IsStatic(false)
858, IsDefinition(false)
859{}
860
861/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000862///
Jim Laskeyce72b172006-02-11 01:01:30 +0000863void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
864 AnchoredDesc::ApplyToFields(Visitor);
865
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000866 Visitor->Apply(Context);
867 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000868 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000869 Visitor->Apply(IsStatic);
870 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000871}
872
873//===----------------------------------------------------------------------===//
874
875GlobalVariableDesc::GlobalVariableDesc()
876: GlobalDesc(DI_TAG_global_variable)
877, Global(NULL)
878{}
879
880/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
881///
882void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
883 GlobalDesc::ApplyToFields(Visitor);
884
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000885 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000886 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000887}
888
Jim Laskeyce72b172006-02-11 01:01:30 +0000889/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000890///
Jim Laskeyce72b172006-02-11 01:01:30 +0000891const char *GlobalVariableDesc::getDescString() const {
892 return "llvm.dbg.global_variable";
893}
894
895/// getTypeString - Return a string used to label this descriptors type.
896///
897const char *GlobalVariableDesc::getTypeString() const {
898 return "llvm.dbg.global_variable.type";
899}
900
901/// getAnchorString - Return a string used to label this descriptor's anchor.
902///
903const char *GlobalVariableDesc::getAnchorString() const {
904 return "llvm.dbg.global_variables";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000905}
906
Jim Laskey86cbdba2006-02-06 15:33:21 +0000907#ifndef NDEBUG
908void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000909 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000910 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000911 << "Anchor(" << getAnchor() << "), "
912 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000913 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000914 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
915 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +0000916 << "Global(" << Global << "), "
917 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000918}
919#endif
920
921//===----------------------------------------------------------------------===//
922
Jim Laskeyce72b172006-02-11 01:01:30 +0000923SubprogramDesc::SubprogramDesc()
924: GlobalDesc(DI_TAG_subprogram)
925{}
926
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000927/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +0000928/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000929void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000930 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000931}
932
Jim Laskeyce72b172006-02-11 01:01:30 +0000933/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000934///
Jim Laskeyce72b172006-02-11 01:01:30 +0000935const char *SubprogramDesc::getDescString() const {
936 return "llvm.dbg.subprogram";
937}
938
939/// getTypeString - Return a string used to label this descriptors type.
940///
941const char *SubprogramDesc::getTypeString() const {
942 return "llvm.dbg.subprogram.type";
943}
944
945/// getAnchorString - Return a string used to label this descriptor's anchor.
946///
947const char *SubprogramDesc::getAnchorString() const {
948 return "llvm.dbg.subprograms";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000949}
950
Jim Laskey86cbdba2006-02-06 15:33:21 +0000951#ifndef NDEBUG
952void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000953 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000954 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000955 << "Anchor(" << getAnchor() << "), "
956 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000957 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000958 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
959 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000960}
961#endif
962
Jim Laskey45ccae52006-02-28 20:15:07 +0000963//===----------------------------------------------------------------------===//
964
Jim Laskey86cbdba2006-02-06 15:33:21 +0000965DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000966 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000967}
968DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000969 // Handle NULL.
970 if (!GV) return NULL;
971
Jim Laskey86cbdba2006-02-06 15:33:21 +0000972 // Check to see if it has been already deserialized.
973 DebugInfoDesc *&Slot = GlobalDescs[GV];
974 if (Slot) return Slot;
975
976 // Get the Tag from the global.
977 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
978
979 // Get the debug version if a compile unit.
980 if (Tag == DI_TAG_compile_unit) {
981 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
982 }
983
984 // Create an empty instance of the correct sort.
985 Slot = DebugInfoDesc::DescFactory(Tag);
986 assert(Slot && "Unknown Tag");
987
988 // Deserialize the fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000989 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000990 DRAM.ApplyToFields(Slot);
991
992 return Slot;
993}
994
995//===----------------------------------------------------------------------===//
996
997/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000998///
Jim Laskey86cbdba2006-02-06 15:33:21 +0000999const PointerType *DISerializer::getStrPtrType() {
1000 // If not already defined.
1001 if (!StrPtrTy) {
1002 // Construct the pointer to signed bytes.
1003 StrPtrTy = PointerType::get(Type::SByteTy);
1004 }
1005
1006 return StrPtrTy;
1007}
1008
1009/// getEmptyStructPtrType - Return a "{ }*" type.
1010///
1011const PointerType *DISerializer::getEmptyStructPtrType() {
1012 // If not already defined.
1013 if (!EmptyStructPtrTy) {
1014 // Construct the empty structure type.
1015 const StructType *EmptyStructTy =
1016 StructType::get(std::vector<const Type*>());
1017 // Construct the pointer to empty structure type.
1018 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1019 }
1020
1021 return EmptyStructPtrTy;
1022}
1023
1024/// getTagType - Return the type describing the specified descriptor (via tag.)
1025///
1026const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1027 // Attempt to get the previously defined type.
1028 StructType *&Ty = TagTypes[DD->getTag()];
1029
1030 // If not already defined.
1031 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001032 // Set up fields vector.
1033 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001034 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001035 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001036 GTAM.ApplyToFields(DD);
1037
1038 // Construct structured type.
1039 Ty = StructType::get(Fields);
1040
Jim Laskey86cbdba2006-02-06 15:33:21 +00001041 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001042 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001043 }
1044
1045 return Ty;
1046}
1047
1048/// getString - Construct the string as constant string global.
1049///
Jim Laskeyce72b172006-02-11 01:01:30 +00001050Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001051 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001052 Constant *&Slot = StringCache[String];
1053 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001054 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +00001055 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001056 Constant *ConstStr = ConstantArray::get(String);
1057 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +00001058 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1059 GlobalVariable::InternalLinkage,
1060 ConstStr, "str", M);
1061 // Convert to generic string pointer.
1062 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1063 return Slot;
1064
Jim Laskey86cbdba2006-02-06 15:33:21 +00001065}
1066
1067/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1068/// so that it can be serialized to a .bc or .ll file.
1069GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1070 // Check if the DebugInfoDesc is already in the map.
1071 GlobalVariable *&Slot = DescGlobals[DD];
1072
1073 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1074 if (Slot) return Slot;
1075
Jim Laskey86cbdba2006-02-06 15:33:21 +00001076 // Get the type associated with the Tag.
1077 const StructType *Ty = getTagType(DD);
1078
1079 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001080 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1081 NULL, DD->getDescString(), M);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001082
1083 // Insert new GlobalVariable in DescGlobals map.
1084 Slot = GV;
1085
1086 // Set up elements vector
1087 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001088 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001089 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001090 SRAM.ApplyToFields(DD);
1091
1092 // Set the globals initializer.
1093 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1094
1095 return GV;
1096}
1097
1098//===----------------------------------------------------------------------===//
1099
1100/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1101/// Mark visited otherwise.
1102bool DIVerifier::markVisited(GlobalVariable *GV) {
1103 // Check if the GlobalVariable is already in the Visited set.
1104 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1105
1106 // See if GlobalVariable exists.
1107 bool Exists = VI != Visited.end() && *VI == GV;
1108
1109 // Insert in set.
1110 if (!Exists) Visited.insert(VI, GV);
1111
1112 return Exists;
1113}
1114
1115/// Verify - Return true if the GlobalVariable appears to be a valid
1116/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001117bool DIVerifier::Verify(Value *V) {
1118 return Verify(getGlobalVariable(V));
1119}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001120bool DIVerifier::Verify(GlobalVariable *GV) {
1121 // Check if seen before.
1122 if (markVisited(GV)) return true;
1123
1124 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001125 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyf60c2412006-02-06 21:54:05 +00001126 if (Tag == DIInvalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001127
1128 // If a compile unit we need the debug version.
1129 if (Tag == DI_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001130 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskeyf60c2412006-02-06 21:54:05 +00001131 if (DebugVersion == DIInvalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001132 }
1133
1134 // Construct an empty DebugInfoDesc.
1135 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1136 if (!DD) return false;
1137
1138 // Get the initializer constant.
1139 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1140
1141 // Get the operand count.
1142 unsigned N = CI->getNumOperands();
1143
1144 // Get the field count.
1145 unsigned &Slot = Counts[Tag];
1146 if (!Slot) {
1147 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001148 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001149 CTAM.ApplyToFields(DD);
1150 Slot = CTAM.getCount();
1151 }
1152
1153 // Field count must equal operand count.
1154 if (Slot != N) {
1155 delete DD;
1156 return false;
1157 }
1158
1159 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001160 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001161 VRAM.ApplyToFields(DD);
1162
1163 // Release empty DebugInfoDesc.
1164 delete DD;
1165
1166 // Return result of field tests.
1167 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001168}
1169
1170//===----------------------------------------------------------------------===//
1171
1172
1173MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001174: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001175, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001176, Directories()
1177, SourceFiles()
1178, Lines()
1179{
1180
1181}
1182MachineDebugInfo::~MachineDebugInfo() {
1183
1184}
1185
Jim Laskeyb2efb852006-01-04 22:28:25 +00001186/// doInitialization - Initialize the debug state for a new module.
1187///
1188bool MachineDebugInfo::doInitialization() {
1189 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001190}
1191
Jim Laskeyb2efb852006-01-04 22:28:25 +00001192/// doFinalization - Tear down the debug state after completion of a module.
1193///
1194bool MachineDebugInfo::doFinalization() {
1195 return false;
1196}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001197
Jim Laskeyd96185a2006-02-13 12:50:39 +00001198/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001199///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001200// FIXME - use new Value type when available.
1201DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001202 return DR.Deserialize(V);
1203}
1204
1205/// Verify - Verify that a Value is debug information descriptor.
1206///
1207bool MachineDebugInfo::Verify(Value *V) {
1208 DIVerifier VR;
1209 return VR.Verify(V);
1210}
1211
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001212/// AnalyzeModule - Scan the module for global debug information.
1213///
1214void MachineDebugInfo::AnalyzeModule(Module &M) {
1215 SetupCompileUnits(M);
1216}
1217
1218/// SetupCompileUnits - Set up the unique vector of compile units.
1219///
1220void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001221 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001222
Jim Laskey0420f2a2006-02-22 19:02:11 +00001223 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1224 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001225 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001226}
1227
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001228/// getCompileUnits - Return a vector of debug compile units.
1229///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001230const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001231 return CompileUnits;
1232}
1233
Jim Laskey0420f2a2006-02-22 19:02:11 +00001234/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1235/// named GlobalVariable.
1236std::vector<GlobalVariable*>
1237MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1238 const std::string &RootName) {
1239 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001240}