blob: 1652881268f399a89ec980175dfee2f71a7791a7 [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 Laskey434b40b2006-02-23 22:37:30 +0000433 case DI_TAG_typedef: return new TypedefDesc();
Jim Laskeyce72b172006-02-11 01:01:30 +0000434 default: break;
435 }
436 return NULL;
437}
438
439/// getLinkage - get linkage appropriate for this type of descriptor.
440///
441GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
442 return GlobalValue::InternalLinkage;
443}
444
445/// ApplyToFields - Target the vistor to the fields of the descriptor.
446///
447void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
448 Visitor->Apply(Tag);
449}
450
451//===----------------------------------------------------------------------===//
452
453/// getLinkage - get linkage appropriate for this type of descriptor.
454///
455GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
456 return GlobalValue::LinkOnceLinkage;
457}
458
459/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
460///
461void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
462 DebugInfoDesc::ApplyToFields(Visitor);
463
464 Visitor->Apply(Name);
465}
466
467/// getDescString - Return a string used to compose global names and labels.
468///
469const char *AnchorDesc::getDescString() const {
470 return Name.c_str();
471}
472
473/// getTypeString - Return a string used to label this descriptors type.
474///
475const char *AnchorDesc::getTypeString() const {
476 return "llvm.dbg.anchor.type";
477}
478
479#ifndef NDEBUG
480void AnchorDesc::dump() {
481 std::cerr << getDescString() << " "
482 << "Tag(" << getTag() << "), "
483 << "Name(" << Name << ")\n";
484}
485#endif
486
487//===----------------------------------------------------------------------===//
488
489AnchoredDesc::AnchoredDesc(unsigned T)
490: DebugInfoDesc(T)
491, Anchor(NULL)
492{}
493
494/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
495///
496void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
497 DebugInfoDesc::ApplyToFields(Visitor);
498
499 Visitor->Apply((DebugInfoDesc *&)Anchor);
500}
501
502//===----------------------------------------------------------------------===//
503
504CompileUnitDesc::CompileUnitDesc()
505: AnchoredDesc(DI_TAG_compile_unit)
506, DebugVersion(LLVMDebugVersion)
507, Language(0)
508, FileName("")
509, Directory("")
510, Producer("")
511{}
512
Jim Laskey86cbdba2006-02-06 15:33:21 +0000513/// DebugVersionFromGlobal - Returns the version number from a compile unit
514/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000515unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000516 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000517 return C ? (unsigned)C->getValue() : (unsigned)DIInvalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000518}
519
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000520/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
521///
522void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000523 AnchoredDesc::ApplyToFields(Visitor);
524
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000525 Visitor->Apply(DebugVersion);
526 Visitor->Apply(Language);
527 Visitor->Apply(FileName);
528 Visitor->Apply(Directory);
529 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000530}
531
Jim Laskeyce72b172006-02-11 01:01:30 +0000532/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000533///
Jim Laskeyce72b172006-02-11 01:01:30 +0000534const char *CompileUnitDesc::getDescString() const {
535 return "llvm.dbg.compile_unit";
536}
537
538/// getTypeString - Return a string used to label this descriptors type.
539///
540const char *CompileUnitDesc::getTypeString() const {
541 return "llvm.dbg.compile_unit.type";
542}
543
544/// getAnchorString - Return a string used to label this descriptor's anchor.
545///
546const char *CompileUnitDesc::getAnchorString() const {
547 return "llvm.dbg.compile_units";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000548}
549
Jim Laskey86cbdba2006-02-06 15:33:21 +0000550#ifndef NDEBUG
551void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000552 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000553 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000554 << "Anchor(" << getAnchor() << "), "
555 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000556 << "Language(" << Language << "), "
557 << "FileName(\"" << FileName << "\"), "
558 << "Directory(\"" << Directory << "\"), "
559 << "Producer(\"" << Producer << "\")\n";
560}
561#endif
562
563//===----------------------------------------------------------------------===//
564
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000565TypeDesc::TypeDesc(unsigned T)
566: DebugInfoDesc(T)
567, Context(NULL)
568, Name("")
569, Size(0)
570{}
571
572/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
573///
574void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
575 DebugInfoDesc::ApplyToFields(Visitor);
576
577 Visitor->Apply(Context);
578 Visitor->Apply(Name);
579 Visitor->Apply(Size);
580}
581
582/// getDescString - Return a string used to compose global names and labels.
583///
584const char *TypeDesc::getDescString() const {
585 return "llvm.dbg.type";
586}
587
588/// getTypeString - Return a string used to label this descriptor's type.
589///
590const char *TypeDesc::getTypeString() const {
591 return "llvm.dbg.type.type";
592}
593
594#ifndef NDEBUG
595void TypeDesc::dump() {
596 std::cerr << getDescString() << " "
597 << "Tag(" << getTag() << "), "
598 << "Context(" << Context << "), "
599 << "Name(\"" << Name << "\"), "
600 << "Size(" << Size << ")\n";
601}
602#endif
603
604//===----------------------------------------------------------------------===//
605
606BasicTypeDesc::BasicTypeDesc()
607: TypeDesc(DI_TAG_basictype)
608, Encoding(0)
609{}
610
611/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
612///
613void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
614 TypeDesc::ApplyToFields(Visitor);
615
616 Visitor->Apply(Encoding);
617}
618
619#ifndef NDEBUG
620void BasicTypeDesc::dump() {
621 std::cerr << getDescString() << " "
622 << "Tag(" << getTag() << "), "
623 << "Context(" << getContext() << "), "
624 << "Name(\"" << getName() << "\"), "
625 << "Size(" << getSize() << "), "
626 << "Encoding(" << Encoding << ")\n";
627}
628#endif
Jim Laskey434b40b2006-02-23 22:37:30 +0000629//===----------------------------------------------------------------------===//
630
631TypedefDesc::TypedefDesc()
632: TypeDesc(DI_TAG_typedef)
633, FromType(NULL)
634, File(NULL)
635, Line(0)
636{}
637
638/// ApplyToFields - Target the visitor to the fields of the TypedefDesc.
639///
640void TypedefDesc::ApplyToFields(DIVisitor *Visitor) {
641 TypeDesc::ApplyToFields(Visitor);
642
643 Visitor->Apply((DebugInfoDesc *&)FromType);
644 Visitor->Apply((DebugInfoDesc *&)File);
645 Visitor->Apply(Line);
646}
647
648#ifndef NDEBUG
649void TypedefDesc::dump() {
650 std::cerr << getDescString() << " "
651 << "Tag(" << getTag() << "), "
652 << "Context(" << getContext() << "), "
653 << "Name(\"" << getName() << "\"), "
654 << "Size(" << getSize() << "), "
655 << "FromType(" << FromType << "), "
656 << "File(" << File << "), "
657 << "Line(" << Line << ")\n";
658}
659#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000660
661//===----------------------------------------------------------------------===//
662
Jim Laskeyce72b172006-02-11 01:01:30 +0000663GlobalDesc::GlobalDesc(unsigned T)
664: AnchoredDesc(T)
665, Context(0)
666, Name("")
667, TyDesc(NULL)
668, IsStatic(false)
669, IsDefinition(false)
670{}
671
672/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000673///
Jim Laskeyce72b172006-02-11 01:01:30 +0000674void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
675 AnchoredDesc::ApplyToFields(Visitor);
676
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000677 Visitor->Apply(Context);
678 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000679 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000680 Visitor->Apply(IsStatic);
681 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000682}
683
684//===----------------------------------------------------------------------===//
685
686GlobalVariableDesc::GlobalVariableDesc()
687: GlobalDesc(DI_TAG_global_variable)
688, Global(NULL)
689{}
690
691/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
692///
693void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
694 GlobalDesc::ApplyToFields(Visitor);
695
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000696 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000697 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000698}
699
Jim Laskeyce72b172006-02-11 01:01:30 +0000700/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000701///
Jim Laskeyce72b172006-02-11 01:01:30 +0000702const char *GlobalVariableDesc::getDescString() const {
703 return "llvm.dbg.global_variable";
704}
705
706/// getTypeString - Return a string used to label this descriptors type.
707///
708const char *GlobalVariableDesc::getTypeString() const {
709 return "llvm.dbg.global_variable.type";
710}
711
712/// getAnchorString - Return a string used to label this descriptor's anchor.
713///
714const char *GlobalVariableDesc::getAnchorString() const {
715 return "llvm.dbg.global_variables";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000716}
717
Jim Laskey86cbdba2006-02-06 15:33:21 +0000718#ifndef NDEBUG
719void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000720 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000721 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000722 << "Anchor(" << getAnchor() << "), "
723 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000724 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000725 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
726 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +0000727 << "Global(" << Global << "), "
728 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000729}
730#endif
731
732//===----------------------------------------------------------------------===//
733
Jim Laskeyce72b172006-02-11 01:01:30 +0000734SubprogramDesc::SubprogramDesc()
735: GlobalDesc(DI_TAG_subprogram)
736{}
737
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000738/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +0000739/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000740void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000741 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000742}
743
Jim Laskeyce72b172006-02-11 01:01:30 +0000744/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000745///
Jim Laskeyce72b172006-02-11 01:01:30 +0000746const char *SubprogramDesc::getDescString() const {
747 return "llvm.dbg.subprogram";
748}
749
750/// getTypeString - Return a string used to label this descriptors type.
751///
752const char *SubprogramDesc::getTypeString() const {
753 return "llvm.dbg.subprogram.type";
754}
755
756/// getAnchorString - Return a string used to label this descriptor's anchor.
757///
758const char *SubprogramDesc::getAnchorString() const {
759 return "llvm.dbg.subprograms";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000760}
761
Jim Laskey86cbdba2006-02-06 15:33:21 +0000762#ifndef NDEBUG
763void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000764 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000765 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000766 << "Anchor(" << getAnchor() << "), "
767 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000768 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000769 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
770 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000771}
772#endif
773
Jim Laskey86cbdba2006-02-06 15:33:21 +0000774DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000775 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000776}
777DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000778 // Handle NULL.
779 if (!GV) return NULL;
780
Jim Laskey86cbdba2006-02-06 15:33:21 +0000781 // Check to see if it has been already deserialized.
782 DebugInfoDesc *&Slot = GlobalDescs[GV];
783 if (Slot) return Slot;
784
785 // Get the Tag from the global.
786 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
787
788 // Get the debug version if a compile unit.
789 if (Tag == DI_TAG_compile_unit) {
790 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
791 }
792
793 // Create an empty instance of the correct sort.
794 Slot = DebugInfoDesc::DescFactory(Tag);
795 assert(Slot && "Unknown Tag");
796
797 // Deserialize the fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000798 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000799 DRAM.ApplyToFields(Slot);
800
801 return Slot;
802}
803
804//===----------------------------------------------------------------------===//
805
806/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000807///
Jim Laskey86cbdba2006-02-06 15:33:21 +0000808const PointerType *DISerializer::getStrPtrType() {
809 // If not already defined.
810 if (!StrPtrTy) {
811 // Construct the pointer to signed bytes.
812 StrPtrTy = PointerType::get(Type::SByteTy);
813 }
814
815 return StrPtrTy;
816}
817
818/// getEmptyStructPtrType - Return a "{ }*" type.
819///
820const PointerType *DISerializer::getEmptyStructPtrType() {
821 // If not already defined.
822 if (!EmptyStructPtrTy) {
823 // Construct the empty structure type.
824 const StructType *EmptyStructTy =
825 StructType::get(std::vector<const Type*>());
826 // Construct the pointer to empty structure type.
827 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
828 }
829
830 return EmptyStructPtrTy;
831}
832
833/// getTagType - Return the type describing the specified descriptor (via tag.)
834///
835const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
836 // Attempt to get the previously defined type.
837 StructType *&Ty = TagTypes[DD->getTag()];
838
839 // If not already defined.
840 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000841 // Set up fields vector.
842 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +0000843 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000844 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000845 GTAM.ApplyToFields(DD);
846
847 // Construct structured type.
848 Ty = StructType::get(Fields);
849
Jim Laskey86cbdba2006-02-06 15:33:21 +0000850 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +0000851 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000852 }
853
854 return Ty;
855}
856
857/// getString - Construct the string as constant string global.
858///
Jim Laskeyce72b172006-02-11 01:01:30 +0000859Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000860 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +0000861 Constant *&Slot = StringCache[String];
862 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000863 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +0000864 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000865 Constant *ConstStr = ConstantArray::get(String);
866 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +0000867 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
868 GlobalVariable::InternalLinkage,
869 ConstStr, "str", M);
870 // Convert to generic string pointer.
871 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
872 return Slot;
873
Jim Laskey86cbdba2006-02-06 15:33:21 +0000874}
875
876/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
877/// so that it can be serialized to a .bc or .ll file.
878GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
879 // Check if the DebugInfoDesc is already in the map.
880 GlobalVariable *&Slot = DescGlobals[DD];
881
882 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
883 if (Slot) return Slot;
884
Jim Laskey86cbdba2006-02-06 15:33:21 +0000885 // Get the type associated with the Tag.
886 const StructType *Ty = getTagType(DD);
887
888 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +0000889 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
890 NULL, DD->getDescString(), M);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000891
892 // Insert new GlobalVariable in DescGlobals map.
893 Slot = GV;
894
895 // Set up elements vector
896 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +0000897 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000898 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000899 SRAM.ApplyToFields(DD);
900
901 // Set the globals initializer.
902 GV->setInitializer(ConstantStruct::get(Ty, Elements));
903
904 return GV;
905}
906
907//===----------------------------------------------------------------------===//
908
909/// markVisited - Return true if the GlobalVariable hase been "seen" before.
910/// Mark visited otherwise.
911bool DIVerifier::markVisited(GlobalVariable *GV) {
912 // Check if the GlobalVariable is already in the Visited set.
913 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
914
915 // See if GlobalVariable exists.
916 bool Exists = VI != Visited.end() && *VI == GV;
917
918 // Insert in set.
919 if (!Exists) Visited.insert(VI, GV);
920
921 return Exists;
922}
923
924/// Verify - Return true if the GlobalVariable appears to be a valid
925/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +0000926bool DIVerifier::Verify(Value *V) {
927 return Verify(getGlobalVariable(V));
928}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000929bool DIVerifier::Verify(GlobalVariable *GV) {
930 // Check if seen before.
931 if (markVisited(GV)) return true;
932
933 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000934 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000935 if (Tag == DIInvalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000936
937 // If a compile unit we need the debug version.
938 if (Tag == DI_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000939 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskeyf60c2412006-02-06 21:54:05 +0000940 if (DebugVersion == DIInvalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000941 }
942
943 // Construct an empty DebugInfoDesc.
944 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
945 if (!DD) return false;
946
947 // Get the initializer constant.
948 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
949
950 // Get the operand count.
951 unsigned N = CI->getNumOperands();
952
953 // Get the field count.
954 unsigned &Slot = Counts[Tag];
955 if (!Slot) {
956 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000957 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000958 CTAM.ApplyToFields(DD);
959 Slot = CTAM.getCount();
960 }
961
962 // Field count must equal operand count.
963 if (Slot != N) {
964 delete DD;
965 return false;
966 }
967
968 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000969 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000970 VRAM.ApplyToFields(DD);
971
972 // Release empty DebugInfoDesc.
973 delete DD;
974
975 // Return result of field tests.
976 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000977}
978
979//===----------------------------------------------------------------------===//
980
981
982MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +0000983: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000984, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000985, Directories()
986, SourceFiles()
987, Lines()
988{
989
990}
991MachineDebugInfo::~MachineDebugInfo() {
992
993}
994
Jim Laskeyb2efb852006-01-04 22:28:25 +0000995/// doInitialization - Initialize the debug state for a new module.
996///
997bool MachineDebugInfo::doInitialization() {
998 return false;
Jim Laskey6af56812006-01-04 13:36:38 +0000999}
1000
Jim Laskeyb2efb852006-01-04 22:28:25 +00001001/// doFinalization - Tear down the debug state after completion of a module.
1002///
1003bool MachineDebugInfo::doFinalization() {
1004 return false;
1005}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001006
Jim Laskeyd96185a2006-02-13 12:50:39 +00001007/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001008///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001009// FIXME - use new Value type when available.
1010DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001011 return DR.Deserialize(V);
1012}
1013
1014/// Verify - Verify that a Value is debug information descriptor.
1015///
1016bool MachineDebugInfo::Verify(Value *V) {
1017 DIVerifier VR;
1018 return VR.Verify(V);
1019}
1020
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001021/// AnalyzeModule - Scan the module for global debug information.
1022///
1023void MachineDebugInfo::AnalyzeModule(Module &M) {
1024 SetupCompileUnits(M);
1025}
1026
1027/// SetupCompileUnits - Set up the unique vector of compile units.
1028///
1029void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001030 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001031
Jim Laskey0420f2a2006-02-22 19:02:11 +00001032 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1033 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001034 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001035}
1036
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001037/// getCompileUnits - Return a vector of debug compile units.
1038///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001039const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001040 return CompileUnits;
1041}
1042
Jim Laskey0420f2a2006-02-22 19:02:11 +00001043/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1044/// named GlobalVariable.
1045std::vector<GlobalVariable*>
1046MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1047 const std::string &RootName) {
1048 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001049}