blob: 72da0a0406042429c0bc3a50a7966c894a025dc1 [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 Laskeyf4afdd92006-02-23 16:58:18 +0000200 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000201 virtual void Apply(bool &Field) { ++Count; }
202 virtual void Apply(std::string &Field) { ++Count; }
203 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
204 virtual void Apply(GlobalVariable *&Field) { ++Count; }
205};
206
207//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000208/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
209/// supplied DebugInfoDesc.
210class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000211private:
212 DIDeserializer &DR; // Active deserializer.
213 unsigned I; // Current operand index.
214 ConstantStruct *CI; // GlobalVariable constant initializer.
215
216public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000217 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
218 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000219 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000220 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000221 , CI(cast<ConstantStruct>(GV->getInitializer()))
222 {}
223
224 /// Apply - Set the value of each of the fields.
225 ///
226 virtual void Apply(int &Field) {
227 Constant *C = CI->getOperand(I++);
228 Field = cast<ConstantSInt>(C)->getValue();
229 }
230 virtual void Apply(unsigned &Field) {
231 Constant *C = CI->getOperand(I++);
232 Field = cast<ConstantUInt>(C)->getValue();
233 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000234 virtual void Apply(uint64_t &Field) {
235 Constant *C = CI->getOperand(I++);
236 Field = cast<ConstantUInt>(C)->getValue();
237 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000238 virtual void Apply(bool &Field) {
239 Constant *C = CI->getOperand(I++);
240 Field = cast<ConstantBool>(C)->getValue();
241 }
242 virtual void Apply(std::string &Field) {
243 Constant *C = CI->getOperand(I++);
244 Field = getStringValue(C);
245 }
246 virtual void Apply(DebugInfoDesc *&Field) {
247 Constant *C = CI->getOperand(I++);
248 Field = DR.Deserialize(C);
249 }
250 virtual void Apply(GlobalVariable *&Field) {
251 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000252 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000253 }
254};
255
256//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000257/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000258/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000259class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000260private:
261 DISerializer &SR; // Active serializer.
262 std::vector<Constant*> &Elements; // Element accumulator.
263
264public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000265 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
266 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000267 , SR(S)
268 , Elements(E)
269 {}
270
271 /// Apply - Set the value of each of the fields.
272 ///
273 virtual void Apply(int &Field) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000274 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000275 }
276 virtual void Apply(unsigned &Field) {
277 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
278 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000279 virtual void Apply(uint64_t &Field) {
280 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
281 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000282 virtual void Apply(bool &Field) {
283 Elements.push_back(ConstantBool::get(Field));
284 }
285 virtual void Apply(std::string &Field) {
286 Elements.push_back(SR.getString(Field));
287 }
288 virtual void Apply(DebugInfoDesc *&Field) {
289 GlobalVariable *GV = NULL;
290
291 // If non-NULL the convert to global.
292 if (Field) GV = SR.Serialize(Field);
293
294 // FIXME - At some point should use specific type.
295 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
296
297 if (GV) {
298 // Set to pointer to global.
299 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
300 } else {
301 // Use NULL.
302 Elements.push_back(ConstantPointerNull::get(EmptyTy));
303 }
304 }
305 virtual void Apply(GlobalVariable *&Field) {
306 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000307 if (Field) {
308 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
309 } else {
310 Elements.push_back(ConstantPointerNull::get(EmptyTy));
311 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000312 }
313};
314
315//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000316/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000317/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000318class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000319private:
320 DISerializer &SR; // Active serializer.
321 std::vector<const Type*> &Fields; // Type accumulator.
322
323public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000324 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
325 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000326 , SR(S)
327 , Fields(F)
328 {}
329
330 /// Apply - Set the value of each of the fields.
331 ///
332 virtual void Apply(int &Field) {
333 Fields.push_back(Type::IntTy);
334 }
335 virtual void Apply(unsigned &Field) {
336 Fields.push_back(Type::UIntTy);
337 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000338 virtual void Apply(uint64_t &Field) {
339 Fields.push_back(Type::UIntTy);
340 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000341 virtual void Apply(bool &Field) {
342 Fields.push_back(Type::BoolTy);
343 }
344 virtual void Apply(std::string &Field) {
345 Fields.push_back(SR.getStrPtrType());
346 }
347 virtual void Apply(DebugInfoDesc *&Field) {
348 // FIXME - At some point should use specific type.
349 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
350 Fields.push_back(EmptyTy);
351 }
352 virtual void Apply(GlobalVariable *&Field) {
353 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
354 Fields.push_back(EmptyTy);
355 }
356};
357
358//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000359/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000360/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000361class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000362private:
363 DIVerifier &VR; // Active verifier.
364 bool IsValid; // Validity status.
365 unsigned I; // Current operand index.
366 ConstantStruct *CI; // GlobalVariable constant initializer.
367
368public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000369 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
370 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000371 , VR(V)
372 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000373 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000374 , CI(cast<ConstantStruct>(GV->getInitializer()))
375 {
376 }
377
378 // Accessors.
379 bool isValid() const { return IsValid; }
380
381 /// Apply - Set the value of each of the fields.
382 ///
383 virtual void Apply(int &Field) {
384 Constant *C = CI->getOperand(I++);
385 IsValid = IsValid && isa<ConstantInt>(C);
386 }
387 virtual void Apply(unsigned &Field) {
388 Constant *C = CI->getOperand(I++);
389 IsValid = IsValid && isa<ConstantInt>(C);
390 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000391 virtual void Apply(uint64_t &Field) {
392 Constant *C = CI->getOperand(I++);
393 IsValid = IsValid && isa<ConstantInt>(C);
394 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000395 virtual void Apply(bool &Field) {
396 Constant *C = CI->getOperand(I++);
397 IsValid = IsValid && isa<ConstantBool>(C);
398 }
399 virtual void Apply(std::string &Field) {
400 Constant *C = CI->getOperand(I++);
401 IsValid = IsValid && isStringValue(C);
402 }
403 virtual void Apply(DebugInfoDesc *&Field) {
404 // FIXME - Prepare the correct descriptor.
405 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000406 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000407 }
408 virtual void Apply(GlobalVariable *&Field) {
409 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000410 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000411 }
412};
413
Jim Laskeyce72b172006-02-11 01:01:30 +0000414
Jim Laskey86cbdba2006-02-06 15:33:21 +0000415//===----------------------------------------------------------------------===//
416
Jim Laskeyce72b172006-02-11 01:01:30 +0000417/// TagFromGlobal - Returns the Tag number from a debug info descriptor
418/// GlobalVariable.
419unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
420 ConstantUInt *C = getUIntOperand(GV, 0);
421 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
422}
423
424/// DescFactory - Create an instance of debug info descriptor based on Tag.
425/// Return NULL if not a recognized Tag.
426DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
427 switch (Tag) {
428 case DI_TAG_anchor: return new AnchorDesc();
429 case DI_TAG_compile_unit: return new CompileUnitDesc();
430 case DI_TAG_global_variable: return new GlobalVariableDesc();
431 case DI_TAG_subprogram: return new SubprogramDesc();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000432 case DI_TAG_basictype: return new BasicTypeDesc();
Jim Laskey69906002006-02-24 16:46:40 +0000433 case DI_TAG_typedef: return new DerivedTypeDesc(DI_TAG_typedef);
434 case DI_TAG_pointer: return new DerivedTypeDesc(DI_TAG_pointer);
435 case DI_TAG_reference: return new DerivedTypeDesc(DI_TAG_reference);
Jim Laskeyce72b172006-02-11 01:01:30 +0000436 default: break;
437 }
438 return NULL;
439}
440
441/// getLinkage - get linkage appropriate for this type of descriptor.
442///
443GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
444 return GlobalValue::InternalLinkage;
445}
446
447/// ApplyToFields - Target the vistor to the fields of the descriptor.
448///
449void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
450 Visitor->Apply(Tag);
451}
452
453//===----------------------------------------------------------------------===//
454
455/// getLinkage - get linkage appropriate for this type of descriptor.
456///
457GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
458 return GlobalValue::LinkOnceLinkage;
459}
460
461/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
462///
463void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
464 DebugInfoDesc::ApplyToFields(Visitor);
465
466 Visitor->Apply(Name);
467}
468
469/// getDescString - Return a string used to compose global names and labels.
470///
471const char *AnchorDesc::getDescString() const {
472 return Name.c_str();
473}
474
475/// getTypeString - Return a string used to label this descriptors type.
476///
477const char *AnchorDesc::getTypeString() const {
478 return "llvm.dbg.anchor.type";
479}
480
481#ifndef NDEBUG
482void AnchorDesc::dump() {
483 std::cerr << getDescString() << " "
484 << "Tag(" << getTag() << "), "
485 << "Name(" << Name << ")\n";
486}
487#endif
488
489//===----------------------------------------------------------------------===//
490
491AnchoredDesc::AnchoredDesc(unsigned T)
492: DebugInfoDesc(T)
493, Anchor(NULL)
494{}
495
496/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
497///
498void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
499 DebugInfoDesc::ApplyToFields(Visitor);
500
501 Visitor->Apply((DebugInfoDesc *&)Anchor);
502}
503
504//===----------------------------------------------------------------------===//
505
506CompileUnitDesc::CompileUnitDesc()
507: AnchoredDesc(DI_TAG_compile_unit)
508, DebugVersion(LLVMDebugVersion)
509, Language(0)
510, FileName("")
511, Directory("")
512, Producer("")
513{}
514
Jim Laskey86cbdba2006-02-06 15:33:21 +0000515/// DebugVersionFromGlobal - Returns the version number from a compile unit
516/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000517unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000518 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000519 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000520}
521
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000522/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
523///
524void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000525 AnchoredDesc::ApplyToFields(Visitor);
526
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000527 Visitor->Apply(DebugVersion);
528 Visitor->Apply(Language);
529 Visitor->Apply(FileName);
530 Visitor->Apply(Directory);
531 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000532}
533
Jim Laskeyce72b172006-02-11 01:01:30 +0000534/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000535///
Jim Laskeyce72b172006-02-11 01:01:30 +0000536const char *CompileUnitDesc::getDescString() const {
537 return "llvm.dbg.compile_unit";
538}
539
540/// getTypeString - Return a string used to label this descriptors type.
541///
542const char *CompileUnitDesc::getTypeString() const {
543 return "llvm.dbg.compile_unit.type";
544}
545
546/// getAnchorString - Return a string used to label this descriptor's anchor.
547///
548const char *CompileUnitDesc::getAnchorString() const {
549 return "llvm.dbg.compile_units";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000550}
551
Jim Laskey86cbdba2006-02-06 15:33:21 +0000552#ifndef NDEBUG
553void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000554 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000555 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000556 << "Anchor(" << getAnchor() << "), "
557 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000558 << "Language(" << Language << "), "
559 << "FileName(\"" << FileName << "\"), "
560 << "Directory(\"" << Directory << "\"), "
561 << "Producer(\"" << Producer << "\")\n";
562}
563#endif
564
565//===----------------------------------------------------------------------===//
566
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000567TypeDesc::TypeDesc(unsigned T)
568: DebugInfoDesc(T)
569, Context(NULL)
570, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000571, File(NULL)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000572, Size(0)
573{}
574
Jim Laskey69906002006-02-24 16:46:40 +0000575/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000576///
577void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
578 DebugInfoDesc::ApplyToFields(Visitor);
579
580 Visitor->Apply(Context);
581 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000582 Visitor->Apply((DebugInfoDesc *&)File);
583 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000584 Visitor->Apply(Size);
585}
586
587/// getDescString - Return a string used to compose global names and labels.
588///
589const char *TypeDesc::getDescString() const {
590 return "llvm.dbg.type";
591}
592
593/// getTypeString - Return a string used to label this descriptor's type.
594///
595const char *TypeDesc::getTypeString() const {
596 return "llvm.dbg.type.type";
597}
598
599#ifndef NDEBUG
600void TypeDesc::dump() {
601 std::cerr << getDescString() << " "
602 << "Tag(" << getTag() << "), "
603 << "Context(" << Context << "), "
604 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000605 << "File(" << File << "), "
606 << "Line(" << Line << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000607 << "Size(" << Size << ")\n";
608}
609#endif
610
611//===----------------------------------------------------------------------===//
612
613BasicTypeDesc::BasicTypeDesc()
614: TypeDesc(DI_TAG_basictype)
615, Encoding(0)
616{}
617
Jim Laskey69906002006-02-24 16:46:40 +0000618/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000619///
620void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
621 TypeDesc::ApplyToFields(Visitor);
622
623 Visitor->Apply(Encoding);
624}
625
626#ifndef NDEBUG
627void BasicTypeDesc::dump() {
628 std::cerr << getDescString() << " "
629 << "Tag(" << getTag() << "), "
630 << "Context(" << getContext() << "), "
631 << "Name(\"" << getName() << "\"), "
632 << "Size(" << getSize() << "), "
633 << "Encoding(" << Encoding << ")\n";
634}
635#endif
Jim Laskey434b40b2006-02-23 22:37:30 +0000636//===----------------------------------------------------------------------===//
637
Jim Laskey69906002006-02-24 16:46:40 +0000638DerivedTypeDesc::DerivedTypeDesc(unsigned T)
639: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000640, FromType(NULL)
Jim Laskey69906002006-02-24 16:46:40 +0000641{
642 assert((T == DI_TAG_typedef || T == DI_TAG_pointer || T == DI_TAG_reference)&&
643 "Unknown derived type.");
644}
Jim Laskey434b40b2006-02-23 22:37:30 +0000645
Jim Laskey69906002006-02-24 16:46:40 +0000646/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000647///
Jim Laskey69906002006-02-24 16:46:40 +0000648void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000649 TypeDesc::ApplyToFields(Visitor);
650
651 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000652}
653
654#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000655void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000656 std::cerr << getDescString() << " "
657 << "Tag(" << getTag() << "), "
658 << "Context(" << getContext() << "), "
659 << "Name(\"" << getName() << "\"), "
660 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000661 << "File(" << getFile() << "), "
662 << "Line(" << getLine() << "), "
663 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000664}
665#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000666
667//===----------------------------------------------------------------------===//
668
Jim Laskeyce72b172006-02-11 01:01:30 +0000669GlobalDesc::GlobalDesc(unsigned T)
670: AnchoredDesc(T)
671, Context(0)
672, Name("")
673, TyDesc(NULL)
674, IsStatic(false)
675, IsDefinition(false)
676{}
677
678/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000679///
Jim Laskeyce72b172006-02-11 01:01:30 +0000680void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
681 AnchoredDesc::ApplyToFields(Visitor);
682
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000683 Visitor->Apply(Context);
684 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000685 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000686 Visitor->Apply(IsStatic);
687 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000688}
689
690//===----------------------------------------------------------------------===//
691
692GlobalVariableDesc::GlobalVariableDesc()
693: GlobalDesc(DI_TAG_global_variable)
694, Global(NULL)
695{}
696
697/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
698///
699void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
700 GlobalDesc::ApplyToFields(Visitor);
701
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000702 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000703 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000704}
705
Jim Laskeyce72b172006-02-11 01:01:30 +0000706/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000707///
Jim Laskeyce72b172006-02-11 01:01:30 +0000708const char *GlobalVariableDesc::getDescString() const {
709 return "llvm.dbg.global_variable";
710}
711
712/// getTypeString - Return a string used to label this descriptors type.
713///
714const char *GlobalVariableDesc::getTypeString() const {
715 return "llvm.dbg.global_variable.type";
716}
717
718/// getAnchorString - Return a string used to label this descriptor's anchor.
719///
720const char *GlobalVariableDesc::getAnchorString() const {
721 return "llvm.dbg.global_variables";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000722}
723
Jim Laskey86cbdba2006-02-06 15:33:21 +0000724#ifndef NDEBUG
725void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000726 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000727 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000728 << "Anchor(" << getAnchor() << "), "
729 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000730 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000731 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
732 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +0000733 << "Global(" << Global << "), "
734 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000735}
736#endif
737
738//===----------------------------------------------------------------------===//
739
Jim Laskeyce72b172006-02-11 01:01:30 +0000740SubprogramDesc::SubprogramDesc()
741: GlobalDesc(DI_TAG_subprogram)
742{}
743
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000744/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +0000745/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000746void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000747 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000748}
749
Jim Laskeyce72b172006-02-11 01:01:30 +0000750/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000751///
Jim Laskeyce72b172006-02-11 01:01:30 +0000752const char *SubprogramDesc::getDescString() const {
753 return "llvm.dbg.subprogram";
754}
755
756/// getTypeString - Return a string used to label this descriptors type.
757///
758const char *SubprogramDesc::getTypeString() const {
759 return "llvm.dbg.subprogram.type";
760}
761
762/// getAnchorString - Return a string used to label this descriptor's anchor.
763///
764const char *SubprogramDesc::getAnchorString() const {
765 return "llvm.dbg.subprograms";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000766}
767
Jim Laskey86cbdba2006-02-06 15:33:21 +0000768#ifndef NDEBUG
769void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000770 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000771 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000772 << "Anchor(" << getAnchor() << "), "
773 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000774 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000775 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
776 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000777}
778#endif
779
Jim Laskey86cbdba2006-02-06 15:33:21 +0000780DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000781 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000782}
783DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000784 // Handle NULL.
785 if (!GV) return NULL;
786
Jim Laskey86cbdba2006-02-06 15:33:21 +0000787 // Check to see if it has been already deserialized.
788 DebugInfoDesc *&Slot = GlobalDescs[GV];
789 if (Slot) return Slot;
790
791 // Get the Tag from the global.
792 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
793
794 // Get the debug version if a compile unit.
795 if (Tag == DI_TAG_compile_unit) {
796 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
797 }
798
799 // Create an empty instance of the correct sort.
800 Slot = DebugInfoDesc::DescFactory(Tag);
801 assert(Slot && "Unknown Tag");
802
803 // Deserialize the fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000804 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000805 DRAM.ApplyToFields(Slot);
806
807 return Slot;
808}
809
810//===----------------------------------------------------------------------===//
811
812/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000813///
Jim Laskey86cbdba2006-02-06 15:33:21 +0000814const PointerType *DISerializer::getStrPtrType() {
815 // If not already defined.
816 if (!StrPtrTy) {
817 // Construct the pointer to signed bytes.
818 StrPtrTy = PointerType::get(Type::SByteTy);
819 }
820
821 return StrPtrTy;
822}
823
824/// getEmptyStructPtrType - Return a "{ }*" type.
825///
826const PointerType *DISerializer::getEmptyStructPtrType() {
827 // If not already defined.
828 if (!EmptyStructPtrTy) {
829 // Construct the empty structure type.
830 const StructType *EmptyStructTy =
831 StructType::get(std::vector<const Type*>());
832 // Construct the pointer to empty structure type.
833 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
834 }
835
836 return EmptyStructPtrTy;
837}
838
839/// getTagType - Return the type describing the specified descriptor (via tag.)
840///
841const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
842 // Attempt to get the previously defined type.
843 StructType *&Ty = TagTypes[DD->getTag()];
844
845 // If not already defined.
846 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000847 // Set up fields vector.
848 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +0000849 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000850 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000851 GTAM.ApplyToFields(DD);
852
853 // Construct structured type.
854 Ty = StructType::get(Fields);
855
Jim Laskey86cbdba2006-02-06 15:33:21 +0000856 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +0000857 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000858 }
859
860 return Ty;
861}
862
863/// getString - Construct the string as constant string global.
864///
Jim Laskeyce72b172006-02-11 01:01:30 +0000865Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000866 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +0000867 Constant *&Slot = StringCache[String];
868 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000869 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +0000870 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000871 Constant *ConstStr = ConstantArray::get(String);
872 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +0000873 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
874 GlobalVariable::InternalLinkage,
875 ConstStr, "str", M);
876 // Convert to generic string pointer.
877 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
878 return Slot;
879
Jim Laskey86cbdba2006-02-06 15:33:21 +0000880}
881
882/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
883/// so that it can be serialized to a .bc or .ll file.
884GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
885 // Check if the DebugInfoDesc is already in the map.
886 GlobalVariable *&Slot = DescGlobals[DD];
887
888 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
889 if (Slot) return Slot;
890
Jim Laskey86cbdba2006-02-06 15:33:21 +0000891 // Get the type associated with the Tag.
892 const StructType *Ty = getTagType(DD);
893
894 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +0000895 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
896 NULL, DD->getDescString(), M);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000897
898 // Insert new GlobalVariable in DescGlobals map.
899 Slot = GV;
900
901 // Set up elements vector
902 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +0000903 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000904 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000905 SRAM.ApplyToFields(DD);
906
907 // Set the globals initializer.
908 GV->setInitializer(ConstantStruct::get(Ty, Elements));
909
910 return GV;
911}
912
913//===----------------------------------------------------------------------===//
914
915/// markVisited - Return true if the GlobalVariable hase been "seen" before.
916/// Mark visited otherwise.
917bool DIVerifier::markVisited(GlobalVariable *GV) {
918 // Check if the GlobalVariable is already in the Visited set.
919 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
920
921 // See if GlobalVariable exists.
922 bool Exists = VI != Visited.end() && *VI == GV;
923
924 // Insert in set.
925 if (!Exists) Visited.insert(VI, GV);
926
927 return Exists;
928}
929
930/// Verify - Return true if the GlobalVariable appears to be a valid
931/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +0000932bool DIVerifier::Verify(Value *V) {
933 return Verify(getGlobalVariable(V));
934}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000935bool DIVerifier::Verify(GlobalVariable *GV) {
936 // Check if seen before.
937 if (markVisited(GV)) return true;
938
939 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000940 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000941 if (Tag == DIInvalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000942
943 // If a compile unit we need the debug version.
944 if (Tag == DI_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000945 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000946 if (DebugVersion == DIInvalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000947 }
948
949 // Construct an empty DebugInfoDesc.
950 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
951 if (!DD) return false;
952
953 // Get the initializer constant.
954 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
955
956 // Get the operand count.
957 unsigned N = CI->getNumOperands();
958
959 // Get the field count.
960 unsigned &Slot = Counts[Tag];
961 if (!Slot) {
962 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000963 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000964 CTAM.ApplyToFields(DD);
965 Slot = CTAM.getCount();
966 }
967
968 // Field count must equal operand count.
969 if (Slot != N) {
970 delete DD;
971 return false;
972 }
973
974 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000975 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000976 VRAM.ApplyToFields(DD);
977
978 // Release empty DebugInfoDesc.
979 delete DD;
980
981 // Return result of field tests.
982 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000983}
984
985//===----------------------------------------------------------------------===//
986
987
988MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +0000989: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000990, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000991, Directories()
992, SourceFiles()
993, Lines()
994{
995
996}
997MachineDebugInfo::~MachineDebugInfo() {
998
999}
1000
Jim Laskeyb2efb852006-01-04 22:28:25 +00001001/// doInitialization - Initialize the debug state for a new module.
1002///
1003bool MachineDebugInfo::doInitialization() {
1004 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001005}
1006
Jim Laskeyb2efb852006-01-04 22:28:25 +00001007/// doFinalization - Tear down the debug state after completion of a module.
1008///
1009bool MachineDebugInfo::doFinalization() {
1010 return false;
1011}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001012
Jim Laskeyd96185a2006-02-13 12:50:39 +00001013/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001014///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001015// FIXME - use new Value type when available.
1016DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001017 return DR.Deserialize(V);
1018}
1019
1020/// Verify - Verify that a Value is debug information descriptor.
1021///
1022bool MachineDebugInfo::Verify(Value *V) {
1023 DIVerifier VR;
1024 return VR.Verify(V);
1025}
1026
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001027/// AnalyzeModule - Scan the module for global debug information.
1028///
1029void MachineDebugInfo::AnalyzeModule(Module &M) {
1030 SetupCompileUnits(M);
1031}
1032
1033/// SetupCompileUnits - Set up the unique vector of compile units.
1034///
1035void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001036 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001037
Jim Laskey0420f2a2006-02-22 19:02:11 +00001038 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1039 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001040 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001041}
1042
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001043/// getCompileUnits - Return a vector of debug compile units.
1044///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001045const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001046 return CompileUnits;
1047}
1048
Jim Laskey0420f2a2006-02-22 19:02:11 +00001049/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1050/// named GlobalVariable.
1051std::vector<GlobalVariable*>
1052MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1053 const std::string &RootName) {
1054 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001055}