blob: c5309417055fded22a8392589ba1328789bab150 [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;
Jim Laskey9c4447a2006-03-01 20:39:36 +000023using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000024
25// Handle the Pass registration stuff necessary to use TargetData's.
26namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000027 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
28}
Jim Laskey063e7652006-01-17 17:31:53 +000029
Jim Laskeyb3e789a2006-01-26 20:21:46 +000030//===----------------------------------------------------------------------===//
31
Jim Laskey86cbdba2006-02-06 15:33:21 +000032/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000033/// specified value in their initializer somewhere.
34static void
35getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
36 // Scan though value users.
37 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
38 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000039 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000040 Result.push_back(GV);
41 } else if (Constant *C = dyn_cast<Constant>(*I)) {
42 // If the user is a constant variable then scan its users
43 getGlobalVariablesUsing(C, Result);
44 }
45 }
46}
47
Jim Laskey86cbdba2006-02-06 15:33:21 +000048/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
49/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000050static std::vector<GlobalVariable*>
51getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000052 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000053
54 std::vector<const Type*> FieldTypes;
55 FieldTypes.push_back(Type::UIntTy);
56 FieldTypes.push_back(PointerType::get(Type::SByteTy));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000057
Jim Laskey86cbdba2006-02-06 15:33:21 +000058 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000059 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000060 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000061
62 // If present and linkonce then scan for users.
63 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
64 getGlobalVariablesUsing(UseRoot, Result);
65 }
66
67 return Result;
68}
69
70/// getStringValue - Turn an LLVM constant pointer that eventually points to a
71/// global into a string value. Return an empty string if we can't do it.
72///
Chris Lattner22760af2006-01-27 17:31:30 +000073static const std::string getStringValue(Value *V, unsigned Offset = 0) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000074 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 if (Init->isString()) {
78 std::string Result = Init->getAsString();
79 if (Offset < Result.size()) {
80 // If we are pointing INTO The string, erase the beginning...
81 Result.erase(Result.begin(), Result.begin()+Offset);
82
83 // Take off the null terminator, and any string fragments after it.
84 std::string::size_type NullPos = Result.find_first_of((char)0);
85 if (NullPos != std::string::npos)
86 Result.erase(Result.begin()+NullPos, Result.end());
87 return Result;
88 }
89 }
90 }
91 } else if (Constant *C = dyn_cast<Constant>(V)) {
92 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
93 return getStringValue(GV, Offset);
94 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
95 if (CE->getOpcode() == Instruction::GetElementPtr) {
96 // Turn a gep into the specified offset.
97 if (CE->getNumOperands() == 3 &&
98 cast<Constant>(CE->getOperand(1))->isNullValue() &&
99 isa<ConstantInt>(CE->getOperand(2))) {
100 return getStringValue(CE->getOperand(0),
101 Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
102 }
103 }
104 }
105 }
106 return "";
107}
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000108
Jim Laskey86cbdba2006-02-06 15:33:21 +0000109/// isStringValue - Return true if the given value can be coerced to a string.
110///
111static bool isStringValue(Value *V) {
112 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
113 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
114 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
115 return Init->isString();
116 }
117 } else if (Constant *C = dyn_cast<Constant>(V)) {
118 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
119 return isStringValue(GV);
120 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
121 if (CE->getOpcode() == Instruction::GetElementPtr) {
122 if (CE->getNumOperands() == 3 &&
123 cast<Constant>(CE->getOperand(1))->isNullValue() &&
124 isa<ConstantInt>(CE->getOperand(2))) {
125 return isStringValue(CE->getOperand(0));
126 }
127 }
128 }
129 }
130 return false;
131}
132
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000133/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000134///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000135static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000136 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
137 return GV;
138 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000139 if (CE->getOpcode() == Instruction::Cast) {
140 return dyn_cast<GlobalVariable>(CE->getOperand(0));
141 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000142 }
143 return NULL;
144}
145
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000146/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000147/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000148static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000149 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
150 return true;
151 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
152 if (CE->getOpcode() == Instruction::Cast) {
153 return isa<GlobalVariable>(CE->getOperand(0));
154 }
155 }
156 return false;
157}
158
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000159/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000160///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000161static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000162 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000163 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000164
Jim Laskey86cbdba2006-02-06 15:33:21 +0000165 // Get the initializer constant.
166 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000167 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000168
Jim Laskey86cbdba2006-02-06 15:33:21 +0000169 // Check if there is at least i + 1 operands.
170 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000171 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000172
Jim Laskey86cbdba2006-02-06 15:33:21 +0000173 // Check constant.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000174 return dyn_cast<ConstantUInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000175}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000176//===----------------------------------------------------------------------===//
177
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000178/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000179/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000180void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000181 DD->ApplyToFields(this);
182}
183
184//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000185/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
186/// the supplied DebugInfoDesc.
187class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000188private:
189 unsigned Count; // Running count of fields.
190
191public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000192 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000193
194 // Accessors.
195 unsigned getCount() const { return Count; }
196
197 /// Apply - Count each of the fields.
198 ///
199 virtual void Apply(int &Field) { ++Count; }
200 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000201 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000202 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000203 virtual void Apply(bool &Field) { ++Count; }
204 virtual void Apply(std::string &Field) { ++Count; }
205 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
206 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000207 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
208 ++Count;
209 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000210};
211
212//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000213/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
214/// supplied DebugInfoDesc.
215class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000216private:
217 DIDeserializer &DR; // Active deserializer.
218 unsigned I; // Current operand index.
219 ConstantStruct *CI; // GlobalVariable constant initializer.
220
221public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000222 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
223 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000224 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000225 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000226 , CI(cast<ConstantStruct>(GV->getInitializer()))
227 {}
228
229 /// Apply - Set the value of each of the fields.
230 ///
231 virtual void Apply(int &Field) {
232 Constant *C = CI->getOperand(I++);
233 Field = cast<ConstantSInt>(C)->getValue();
234 }
235 virtual void Apply(unsigned &Field) {
236 Constant *C = CI->getOperand(I++);
237 Field = cast<ConstantUInt>(C)->getValue();
238 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000239 virtual void Apply(int64_t &Field) {
240 Constant *C = CI->getOperand(I++);
241 Field = cast<ConstantSInt>(C)->getValue();
242 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000243 virtual void Apply(uint64_t &Field) {
244 Constant *C = CI->getOperand(I++);
245 Field = cast<ConstantUInt>(C)->getValue();
246 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000247 virtual void Apply(bool &Field) {
248 Constant *C = CI->getOperand(I++);
249 Field = cast<ConstantBool>(C)->getValue();
250 }
251 virtual void Apply(std::string &Field) {
252 Constant *C = CI->getOperand(I++);
253 Field = getStringValue(C);
254 }
255 virtual void Apply(DebugInfoDesc *&Field) {
256 Constant *C = CI->getOperand(I++);
257 Field = DR.Deserialize(C);
258 }
259 virtual void Apply(GlobalVariable *&Field) {
260 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000261 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000262 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000263 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
264 Constant *C = CI->getOperand(I++);
265 GlobalVariable *GV = getGlobalVariable(C);
266 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
267 Field.resize(0);
268 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
269 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
270 DebugInfoDesc *DE = DR.Deserialize(GVE);
271 Field.push_back(DE);
272 }
273 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000274};
275
276//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000277/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000278/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000279class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000280private:
281 DISerializer &SR; // Active serializer.
282 std::vector<Constant*> &Elements; // Element accumulator.
283
284public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000285 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
286 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000287 , SR(S)
288 , Elements(E)
289 {}
290
291 /// Apply - Set the value of each of the fields.
292 ///
293 virtual void Apply(int &Field) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000294 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000295 }
296 virtual void Apply(unsigned &Field) {
297 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
298 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000299 virtual void Apply(int64_t &Field) {
300 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
301 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000302 virtual void Apply(uint64_t &Field) {
303 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
304 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000305 virtual void Apply(bool &Field) {
306 Elements.push_back(ConstantBool::get(Field));
307 }
308 virtual void Apply(std::string &Field) {
309 Elements.push_back(SR.getString(Field));
310 }
311 virtual void Apply(DebugInfoDesc *&Field) {
312 GlobalVariable *GV = NULL;
313
314 // If non-NULL the convert to global.
315 if (Field) GV = SR.Serialize(Field);
316
317 // FIXME - At some point should use specific type.
318 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
319
320 if (GV) {
321 // Set to pointer to global.
322 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
323 } else {
324 // Use NULL.
325 Elements.push_back(ConstantPointerNull::get(EmptyTy));
326 }
327 }
328 virtual void Apply(GlobalVariable *&Field) {
329 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000330 if (Field) {
331 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
332 } else {
333 Elements.push_back(ConstantPointerNull::get(EmptyTy));
334 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000335 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000336 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
337 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
338 unsigned N = Field.size();
339 ArrayType *AT = ArrayType::get(EmptyTy, N);
340 std::vector<Constant *> ArrayElements;
341
342 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
343 GlobalVariable *GVE = SR.Serialize(Field[i]);
344 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
345 ArrayElements.push_back(cast<Constant>(CE));
346 }
347
348 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000349 GlobalVariable *CAGV = new GlobalVariable(AT, true,
350 GlobalValue::InternalLinkage,
351 CA, "llvm.dbg.array",
352 SR.getModule());
353 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000354 Elements.push_back(CAE);
355 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000356};
357
358//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000359/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000360/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000361class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000362private:
363 DISerializer &SR; // Active serializer.
364 std::vector<const Type*> &Fields; // Type accumulator.
365
366public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000367 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
368 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000369 , SR(S)
370 , Fields(F)
371 {}
372
373 /// Apply - Set the value of each of the fields.
374 ///
375 virtual void Apply(int &Field) {
376 Fields.push_back(Type::IntTy);
377 }
378 virtual void Apply(unsigned &Field) {
379 Fields.push_back(Type::UIntTy);
380 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000381 virtual void Apply(int64_t &Field) {
382 Fields.push_back(Type::IntTy);
383 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000384 virtual void Apply(uint64_t &Field) {
385 Fields.push_back(Type::UIntTy);
386 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000387 virtual void Apply(bool &Field) {
388 Fields.push_back(Type::BoolTy);
389 }
390 virtual void Apply(std::string &Field) {
391 Fields.push_back(SR.getStrPtrType());
392 }
393 virtual void Apply(DebugInfoDesc *&Field) {
394 // FIXME - At some point should use specific type.
395 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396 Fields.push_back(EmptyTy);
397 }
398 virtual void Apply(GlobalVariable *&Field) {
399 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
400 Fields.push_back(EmptyTy);
401 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000402 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
403 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
404 Fields.push_back(EmptyTy);
405 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000406};
407
408//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000409/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000410/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000411class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000412private:
413 DIVerifier &VR; // Active verifier.
414 bool IsValid; // Validity status.
415 unsigned I; // Current operand index.
416 ConstantStruct *CI; // GlobalVariable constant initializer.
417
418public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000419 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
420 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000421 , VR(V)
422 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000423 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000424 , CI(cast<ConstantStruct>(GV->getInitializer()))
425 {
426 }
427
428 // Accessors.
429 bool isValid() const { return IsValid; }
430
431 /// Apply - Set the value of each of the fields.
432 ///
433 virtual void Apply(int &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isa<ConstantInt>(C);
436 }
437 virtual void Apply(unsigned &Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isa<ConstantInt>(C);
440 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000441 virtual void Apply(int64_t &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isa<ConstantInt>(C);
444 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000445 virtual void Apply(uint64_t &Field) {
446 Constant *C = CI->getOperand(I++);
447 IsValid = IsValid && isa<ConstantInt>(C);
448 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000449 virtual void Apply(bool &Field) {
450 Constant *C = CI->getOperand(I++);
451 IsValid = IsValid && isa<ConstantBool>(C);
452 }
453 virtual void Apply(std::string &Field) {
454 Constant *C = CI->getOperand(I++);
455 IsValid = IsValid && isStringValue(C);
456 }
457 virtual void Apply(DebugInfoDesc *&Field) {
458 // FIXME - Prepare the correct descriptor.
459 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000460 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000461 }
462 virtual void Apply(GlobalVariable *&Field) {
463 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000464 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000465 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000466 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
467 Constant *C = CI->getOperand(I++);
468 IsValid = IsValid && isGlobalVariable(C);
469 if (!IsValid) return;
470
471 GlobalVariable *GV = getGlobalVariable(C);
472 IsValid = IsValid && GV && GV->hasInitializer();
473 if (!IsValid) return;
474
475 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
476 IsValid = IsValid && CA;
477 if (!IsValid) return;
478
479 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
480 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
481 if (!IsValid) return;
482
483 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
484 VR.Verify(GVE);
485 }
486 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000487};
488
Jim Laskeyce72b172006-02-11 01:01:30 +0000489
Jim Laskey86cbdba2006-02-06 15:33:21 +0000490//===----------------------------------------------------------------------===//
491
Jim Laskeyce72b172006-02-11 01:01:30 +0000492/// TagFromGlobal - Returns the Tag number from a debug info descriptor
493/// GlobalVariable.
494unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
495 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000496 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000497}
498
499/// DescFactory - Create an instance of debug info descriptor based on Tag.
500/// Return NULL if not a recognized Tag.
501DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
502 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000503 case DW_TAG_anchor: return new AnchorDesc();
504 case DW_TAG_compile_unit: return new CompileUnitDesc();
505 case DW_TAG_variable: return new GlobalVariableDesc();
506 case DW_TAG_subprogram: return new SubprogramDesc();
507 case DW_TAG_base_type: return new BasicTypeDesc();
508 case DW_TAG_typedef:
509 case DW_TAG_pointer_type:
510 case DW_TAG_reference_type:
511 case DW_TAG_const_type:
512 case DW_TAG_volatile_type:
513 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
514 case DW_TAG_array_type:
515 case DW_TAG_structure_type:
516 case DW_TAG_union_type:
517 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
518 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000519 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyce72b172006-02-11 01:01:30 +0000520 default: break;
521 }
522 return NULL;
523}
524
525/// getLinkage - get linkage appropriate for this type of descriptor.
526///
527GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
528 return GlobalValue::InternalLinkage;
529}
530
531/// ApplyToFields - Target the vistor to the fields of the descriptor.
532///
533void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
534 Visitor->Apply(Tag);
535}
536
537//===----------------------------------------------------------------------===//
538
Jim Laskey9c4447a2006-03-01 20:39:36 +0000539AnchorDesc::AnchorDesc()
540: DebugInfoDesc(DW_TAG_anchor)
541, Name("")
542{}
543AnchorDesc::AnchorDesc(const std::string &N)
544: DebugInfoDesc(DW_TAG_anchor)
545, Name(N)
546{}
547
548// Implement isa/cast/dyncast.
549bool AnchorDesc::classof(const DebugInfoDesc *D) {
550 return D->getTag() == DW_TAG_anchor;
551}
552
Jim Laskeyce72b172006-02-11 01:01:30 +0000553/// getLinkage - get linkage appropriate for this type of descriptor.
554///
555GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
556 return GlobalValue::LinkOnceLinkage;
557}
558
559/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
560///
561void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
562 DebugInfoDesc::ApplyToFields(Visitor);
563
564 Visitor->Apply(Name);
565}
566
567/// getDescString - Return a string used to compose global names and labels.
568///
569const char *AnchorDesc::getDescString() const {
570 return Name.c_str();
571}
572
573/// getTypeString - Return a string used to label this descriptors type.
574///
575const char *AnchorDesc::getTypeString() const {
576 return "llvm.dbg.anchor.type";
577}
578
579#ifndef NDEBUG
580void AnchorDesc::dump() {
581 std::cerr << getDescString() << " "
582 << "Tag(" << getTag() << "), "
583 << "Name(" << Name << ")\n";
584}
585#endif
586
587//===----------------------------------------------------------------------===//
588
589AnchoredDesc::AnchoredDesc(unsigned T)
590: DebugInfoDesc(T)
591, Anchor(NULL)
592{}
593
594/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
595///
596void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
597 DebugInfoDesc::ApplyToFields(Visitor);
598
599 Visitor->Apply((DebugInfoDesc *&)Anchor);
600}
601
602//===----------------------------------------------------------------------===//
603
604CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000605: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000606, DebugVersion(LLVMDebugVersion)
607, Language(0)
608, FileName("")
609, Directory("")
610, Producer("")
611{}
612
Jim Laskey9c4447a2006-03-01 20:39:36 +0000613// Implement isa/cast/dyncast.
614bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
615 return D->getTag() == DW_TAG_compile_unit;
616}
617
Jim Laskey86cbdba2006-02-06 15:33:21 +0000618/// DebugVersionFromGlobal - Returns the version number from a compile unit
619/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000620unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000621 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000622 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000623}
624
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000625/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
626///
627void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000628 AnchoredDesc::ApplyToFields(Visitor);
629
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000630 Visitor->Apply(DebugVersion);
631 Visitor->Apply(Language);
632 Visitor->Apply(FileName);
633 Visitor->Apply(Directory);
634 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000635}
636
Jim Laskeyce72b172006-02-11 01:01:30 +0000637/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000638///
Jim Laskeyce72b172006-02-11 01:01:30 +0000639const char *CompileUnitDesc::getDescString() const {
640 return "llvm.dbg.compile_unit";
641}
642
643/// getTypeString - Return a string used to label this descriptors type.
644///
645const char *CompileUnitDesc::getTypeString() const {
646 return "llvm.dbg.compile_unit.type";
647}
648
649/// getAnchorString - Return a string used to label this descriptor's anchor.
650///
651const char *CompileUnitDesc::getAnchorString() const {
652 return "llvm.dbg.compile_units";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000653}
654
Jim Laskey86cbdba2006-02-06 15:33:21 +0000655#ifndef NDEBUG
656void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000657 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000658 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000659 << "Anchor(" << getAnchor() << "), "
660 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000661 << "Language(" << Language << "), "
662 << "FileName(\"" << FileName << "\"), "
663 << "Directory(\"" << Directory << "\"), "
664 << "Producer(\"" << Producer << "\")\n";
665}
666#endif
667
668//===----------------------------------------------------------------------===//
669
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000670TypeDesc::TypeDesc(unsigned T)
671: DebugInfoDesc(T)
672, Context(NULL)
673, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000674, File(NULL)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000675, Size(0)
676{}
677
Jim Laskey69906002006-02-24 16:46:40 +0000678/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000679///
680void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
681 DebugInfoDesc::ApplyToFields(Visitor);
682
683 Visitor->Apply(Context);
684 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000685 Visitor->Apply((DebugInfoDesc *&)File);
686 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000687 Visitor->Apply(Size);
688}
689
690/// getDescString - Return a string used to compose global names and labels.
691///
692const char *TypeDesc::getDescString() const {
693 return "llvm.dbg.type";
694}
695
696/// getTypeString - Return a string used to label this descriptor's type.
697///
698const char *TypeDesc::getTypeString() const {
699 return "llvm.dbg.type.type";
700}
701
702#ifndef NDEBUG
703void TypeDesc::dump() {
704 std::cerr << getDescString() << " "
705 << "Tag(" << getTag() << "), "
706 << "Context(" << Context << "), "
707 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000708 << "File(" << File << "), "
709 << "Line(" << Line << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000710 << "Size(" << Size << ")\n";
711}
712#endif
713
714//===----------------------------------------------------------------------===//
715
716BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000717: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000718, Encoding(0)
719{}
720
Jim Laskey9c4447a2006-03-01 20:39:36 +0000721// Implement isa/cast/dyncast.
722bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
723 return D->getTag() == DW_TAG_base_type;
724}
725
Jim Laskey69906002006-02-24 16:46:40 +0000726/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000727///
728void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
729 TypeDesc::ApplyToFields(Visitor);
730
731 Visitor->Apply(Encoding);
732}
733
Jim Laskeyf8913f12006-03-01 17:53:02 +0000734/// getDescString - Return a string used to compose global names and labels.
735///
736const char *BasicTypeDesc::getDescString() const {
737 return "llvm.dbg.basictype";
738}
739
740/// getTypeString - Return a string used to label this descriptor's type.
741///
742const char *BasicTypeDesc::getTypeString() const {
743 return "llvm.dbg.basictype.type";
744}
745
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000746#ifndef NDEBUG
747void BasicTypeDesc::dump() {
748 std::cerr << getDescString() << " "
749 << "Tag(" << getTag() << "), "
750 << "Context(" << getContext() << "), "
751 << "Name(\"" << getName() << "\"), "
752 << "Size(" << getSize() << "), "
753 << "Encoding(" << Encoding << ")\n";
754}
755#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000756
Jim Laskey434b40b2006-02-23 22:37:30 +0000757//===----------------------------------------------------------------------===//
758
Jim Laskey69906002006-02-24 16:46:40 +0000759DerivedTypeDesc::DerivedTypeDesc(unsigned T)
760: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000761, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000762{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000763
Jim Laskey9c4447a2006-03-01 20:39:36 +0000764// Implement isa/cast/dyncast.
765bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
766 unsigned T = D->getTag();
767 switch (T) {
768 case DW_TAG_typedef:
769 case DW_TAG_pointer_type:
770 case DW_TAG_reference_type:
771 case DW_TAG_const_type:
772 case DW_TAG_volatile_type:
773 case DW_TAG_restrict_type:
774 return true;
775 default: break;
776 }
777 return false;
778}
779
Jim Laskey69906002006-02-24 16:46:40 +0000780/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000781///
Jim Laskey69906002006-02-24 16:46:40 +0000782void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000783 TypeDesc::ApplyToFields(Visitor);
784
785 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000786}
787
Jim Laskeyf8913f12006-03-01 17:53:02 +0000788/// getDescString - Return a string used to compose global names and labels.
789///
790const char *DerivedTypeDesc::getDescString() const {
791 return "llvm.dbg.derivedtype";
792}
793
794/// getTypeString - Return a string used to label this descriptor's type.
795///
796const char *DerivedTypeDesc::getTypeString() const {
797 return "llvm.dbg.derivedtype.type";
798}
799
Jim Laskey434b40b2006-02-23 22:37:30 +0000800#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000801void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000802 std::cerr << getDescString() << " "
803 << "Tag(" << getTag() << "), "
804 << "Context(" << getContext() << "), "
805 << "Name(\"" << getName() << "\"), "
806 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000807 << "File(" << getFile() << "), "
808 << "Line(" << getLine() << "), "
809 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000810}
811#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000812
813//===----------------------------------------------------------------------===//
814
Jim Laskeyf8913f12006-03-01 17:53:02 +0000815CompositeTypeDesc::CompositeTypeDesc(unsigned T)
816: DerivedTypeDesc(T)
817, Elements()
818{}
819
Jim Laskey9c4447a2006-03-01 20:39:36 +0000820// Implement isa/cast/dyncast.
821bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
822 unsigned T = D->getTag();
823 switch (T) {
824 case DW_TAG_array_type:
825 case DW_TAG_structure_type:
826 case DW_TAG_union_type:
827 case DW_TAG_enumeration_type:
828 return true;
829 default: break;
830 }
831 return false;
832}
833
Jim Laskeyf8913f12006-03-01 17:53:02 +0000834/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
835///
836void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
837 DerivedTypeDesc::ApplyToFields(Visitor);
838
839 Visitor->Apply(Elements);
840}
841
842/// getDescString - Return a string used to compose global names and labels.
843///
844const char *CompositeTypeDesc::getDescString() const {
845 return "llvm.dbg.compositetype";
846}
847
848/// getTypeString - Return a string used to label this descriptor's type.
849///
850const char *CompositeTypeDesc::getTypeString() const {
851 return "llvm.dbg.compositetype.type";
852}
853
854#ifndef NDEBUG
855void CompositeTypeDesc::dump() {
856 std::cerr << getDescString() << " "
857 << "Tag(" << getTag() << "), "
858 << "Context(" << getContext() << "), "
859 << "Name(\"" << getName() << "\"), "
860 << "Size(" << getSize() << "), "
861 << "File(" << getFile() << "), "
862 << "Line(" << getLine() << "), "
863 << "FromType(" << getFromType() << "), "
864 << "Elements.size(" << Elements.size() << ")\n";
865}
866#endif
867
868//===----------------------------------------------------------------------===//
869
870SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000871: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000872, Lo(0)
873, Hi(0)
874{}
875
Jim Laskey9c4447a2006-03-01 20:39:36 +0000876// Implement isa/cast/dyncast.
877bool SubrangeDesc::classof(const DebugInfoDesc *D) {
878 return D->getTag() == DW_TAG_subrange_type;
879}
880
Jim Laskeyf8913f12006-03-01 17:53:02 +0000881/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
882///
883void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
884 DebugInfoDesc::ApplyToFields(Visitor);
885
886 Visitor->Apply(Lo);
887 Visitor->Apply(Hi);
888}
889
890/// getDescString - Return a string used to compose global names and labels.
891///
892const char *SubrangeDesc::getDescString() const {
893 return "llvm.dbg.subrange";
894}
895
896/// getTypeString - Return a string used to label this descriptor's type.
897///
898const char *SubrangeDesc::getTypeString() const {
899 return "llvm.dbg.subrange.type";
900}
901
902#ifndef NDEBUG
903void SubrangeDesc::dump() {
904 std::cerr << getDescString() << " "
905 << "Tag(" << getTag() << "), "
906 << "Lo(" << Lo << "), "
907 << "Hi(" << Hi << ")\n";
908}
909#endif
910
911//===----------------------------------------------------------------------===//
912
Jim Laskey6a3eb012006-03-01 23:52:37 +0000913EnumeratorDesc::EnumeratorDesc()
914: DebugInfoDesc(DW_TAG_enumerator)
915, Name("")
916, Value(0)
917{}
918
919// Implement isa/cast/dyncast.
920bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
921 return D->getTag() == DW_TAG_enumerator;
922}
923
924/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
925///
926void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
927 DebugInfoDesc::ApplyToFields(Visitor);
928
929 Visitor->Apply(Name);
930 Visitor->Apply(Value);
931}
932
933/// getDescString - Return a string used to compose global names and labels.
934///
935const char *EnumeratorDesc::getDescString() const {
936 return "llvm.dbg.enumerator";
937}
938
939/// getTypeString - Return a string used to label this descriptor's type.
940///
941const char *EnumeratorDesc::getTypeString() const {
942 return "llvm.dbg.enumerator.type";
943}
944
945#ifndef NDEBUG
946void EnumeratorDesc::dump() {
947 std::cerr << getDescString() << " "
948 << "Tag(" << getTag() << "), "
949 << "Name(" << Name << "), "
950 << "Value(" << Value << ")\n";
951}
952#endif
953
954//===----------------------------------------------------------------------===//
955
Jim Laskeyce72b172006-02-11 01:01:30 +0000956GlobalDesc::GlobalDesc(unsigned T)
957: AnchoredDesc(T)
958, Context(0)
959, Name("")
960, TyDesc(NULL)
961, IsStatic(false)
962, IsDefinition(false)
963{}
964
965/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000966///
Jim Laskeyce72b172006-02-11 01:01:30 +0000967void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
968 AnchoredDesc::ApplyToFields(Visitor);
969
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000970 Visitor->Apply(Context);
971 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000972 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000973 Visitor->Apply(IsStatic);
974 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000975}
976
977//===----------------------------------------------------------------------===//
978
979GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000980: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +0000981, Global(NULL)
982{}
983
Jim Laskey9c4447a2006-03-01 20:39:36 +0000984// Implement isa/cast/dyncast.
985bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
986 return D->getTag() == DW_TAG_variable;
987}
988
Jim Laskeyce72b172006-02-11 01:01:30 +0000989/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
990///
991void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
992 GlobalDesc::ApplyToFields(Visitor);
993
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000994 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000995 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000996}
997
Jim Laskeyce72b172006-02-11 01:01:30 +0000998/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000999///
Jim Laskeyce72b172006-02-11 01:01:30 +00001000const char *GlobalVariableDesc::getDescString() const {
1001 return "llvm.dbg.global_variable";
1002}
1003
1004/// getTypeString - Return a string used to label this descriptors type.
1005///
1006const char *GlobalVariableDesc::getTypeString() const {
1007 return "llvm.dbg.global_variable.type";
1008}
1009
1010/// getAnchorString - Return a string used to label this descriptor's anchor.
1011///
1012const char *GlobalVariableDesc::getAnchorString() const {
1013 return "llvm.dbg.global_variables";
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001014}
1015
Jim Laskey86cbdba2006-02-06 15:33:21 +00001016#ifndef NDEBUG
1017void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001018 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001019 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001020 << "Anchor(" << getAnchor() << "), "
1021 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001022 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001023 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1024 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +00001025 << "Global(" << Global << "), "
1026 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001027}
1028#endif
1029
1030//===----------------------------------------------------------------------===//
1031
Jim Laskeyce72b172006-02-11 01:01:30 +00001032SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001033: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001034{}
1035
Jim Laskey9c4447a2006-03-01 20:39:36 +00001036// Implement isa/cast/dyncast.
1037bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1038 return D->getTag() == DW_TAG_subprogram;
1039}
1040
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001041/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001042/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001043void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001044 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001045}
1046
Jim Laskeyce72b172006-02-11 01:01:30 +00001047/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001048///
Jim Laskeyce72b172006-02-11 01:01:30 +00001049const char *SubprogramDesc::getDescString() const {
1050 return "llvm.dbg.subprogram";
1051}
1052
1053/// getTypeString - Return a string used to label this descriptors type.
1054///
1055const char *SubprogramDesc::getTypeString() const {
1056 return "llvm.dbg.subprogram.type";
1057}
1058
1059/// getAnchorString - Return a string used to label this descriptor's anchor.
1060///
1061const char *SubprogramDesc::getAnchorString() const {
1062 return "llvm.dbg.subprograms";
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001063}
1064
Jim Laskey86cbdba2006-02-06 15:33:21 +00001065#ifndef NDEBUG
1066void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001067 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001068 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001069 << "Anchor(" << getAnchor() << "), "
1070 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001071 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001072 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1073 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001074}
1075#endif
1076
Jim Laskey45ccae52006-02-28 20:15:07 +00001077//===----------------------------------------------------------------------===//
1078
Jim Laskey86cbdba2006-02-06 15:33:21 +00001079DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001080 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001081}
1082DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001083 // Handle NULL.
1084 if (!GV) return NULL;
1085
Jim Laskey86cbdba2006-02-06 15:33:21 +00001086 // Check to see if it has been already deserialized.
1087 DebugInfoDesc *&Slot = GlobalDescs[GV];
1088 if (Slot) return Slot;
1089
1090 // Get the Tag from the global.
1091 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1092
1093 // Get the debug version if a compile unit.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001094 if (Tag == DW_TAG_compile_unit) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001095 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1096 }
1097
1098 // Create an empty instance of the correct sort.
1099 Slot = DebugInfoDesc::DescFactory(Tag);
1100 assert(Slot && "Unknown Tag");
1101
1102 // Deserialize the fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001103 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001104 DRAM.ApplyToFields(Slot);
1105
1106 return Slot;
1107}
1108
1109//===----------------------------------------------------------------------===//
1110
1111/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001112///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001113const PointerType *DISerializer::getStrPtrType() {
1114 // If not already defined.
1115 if (!StrPtrTy) {
1116 // Construct the pointer to signed bytes.
1117 StrPtrTy = PointerType::get(Type::SByteTy);
1118 }
1119
1120 return StrPtrTy;
1121}
1122
1123/// getEmptyStructPtrType - Return a "{ }*" type.
1124///
1125const PointerType *DISerializer::getEmptyStructPtrType() {
1126 // If not already defined.
1127 if (!EmptyStructPtrTy) {
1128 // Construct the empty structure type.
1129 const StructType *EmptyStructTy =
1130 StructType::get(std::vector<const Type*>());
1131 // Construct the pointer to empty structure type.
1132 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1133 }
1134
1135 return EmptyStructPtrTy;
1136}
1137
1138/// getTagType - Return the type describing the specified descriptor (via tag.)
1139///
1140const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1141 // Attempt to get the previously defined type.
1142 StructType *&Ty = TagTypes[DD->getTag()];
1143
1144 // If not already defined.
1145 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001146 // Set up fields vector.
1147 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001148 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001149 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001150 GTAM.ApplyToFields(DD);
1151
1152 // Construct structured type.
1153 Ty = StructType::get(Fields);
1154
Jim Laskey86cbdba2006-02-06 15:33:21 +00001155 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001156 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001157 }
1158
1159 return Ty;
1160}
1161
1162/// getString - Construct the string as constant string global.
1163///
Jim Laskeyce72b172006-02-11 01:01:30 +00001164Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001165 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001166 Constant *&Slot = StringCache[String];
1167 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001168 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +00001169 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001170 Constant *ConstStr = ConstantArray::get(String);
1171 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +00001172 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1173 GlobalVariable::InternalLinkage,
1174 ConstStr, "str", M);
1175 // Convert to generic string pointer.
1176 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1177 return Slot;
1178
Jim Laskey86cbdba2006-02-06 15:33:21 +00001179}
1180
1181/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1182/// so that it can be serialized to a .bc or .ll file.
1183GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1184 // Check if the DebugInfoDesc is already in the map.
1185 GlobalVariable *&Slot = DescGlobals[DD];
1186
1187 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1188 if (Slot) return Slot;
1189
Jim Laskey86cbdba2006-02-06 15:33:21 +00001190 // Get the type associated with the Tag.
1191 const StructType *Ty = getTagType(DD);
1192
1193 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001194 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1195 NULL, DD->getDescString(), M);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001196
1197 // Insert new GlobalVariable in DescGlobals map.
1198 Slot = GV;
1199
1200 // Set up elements vector
1201 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001202 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001203 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001204 SRAM.ApplyToFields(DD);
1205
1206 // Set the globals initializer.
1207 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1208
1209 return GV;
1210}
1211
1212//===----------------------------------------------------------------------===//
1213
1214/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1215/// Mark visited otherwise.
1216bool DIVerifier::markVisited(GlobalVariable *GV) {
1217 // Check if the GlobalVariable is already in the Visited set.
1218 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1219
1220 // See if GlobalVariable exists.
1221 bool Exists = VI != Visited.end() && *VI == GV;
1222
1223 // Insert in set.
1224 if (!Exists) Visited.insert(VI, GV);
1225
1226 return Exists;
1227}
1228
1229/// Verify - Return true if the GlobalVariable appears to be a valid
1230/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001231bool DIVerifier::Verify(Value *V) {
1232 return Verify(getGlobalVariable(V));
1233}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001234bool DIVerifier::Verify(GlobalVariable *GV) {
1235 // Check if seen before.
1236 if (markVisited(GV)) return true;
1237
1238 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001239 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001240 if (Tag == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001241
1242 // If a compile unit we need the debug version.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001243 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001244 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001245 if (DebugVersion == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001246 }
1247
1248 // Construct an empty DebugInfoDesc.
1249 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1250 if (!DD) return false;
1251
1252 // Get the initializer constant.
1253 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1254
1255 // Get the operand count.
1256 unsigned N = CI->getNumOperands();
1257
1258 // Get the field count.
1259 unsigned &Slot = Counts[Tag];
1260 if (!Slot) {
1261 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001262 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001263 CTAM.ApplyToFields(DD);
1264 Slot = CTAM.getCount();
1265 }
1266
1267 // Field count must equal operand count.
1268 if (Slot != N) {
1269 delete DD;
1270 return false;
1271 }
1272
1273 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001274 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001275 VRAM.ApplyToFields(DD);
1276
1277 // Release empty DebugInfoDesc.
1278 delete DD;
1279
1280 // Return result of field tests.
1281 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001282}
1283
1284//===----------------------------------------------------------------------===//
1285
1286
1287MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001288: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001289, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001290, Directories()
1291, SourceFiles()
1292, Lines()
1293{
1294
1295}
1296MachineDebugInfo::~MachineDebugInfo() {
1297
1298}
1299
Jim Laskeyb2efb852006-01-04 22:28:25 +00001300/// doInitialization - Initialize the debug state for a new module.
1301///
1302bool MachineDebugInfo::doInitialization() {
1303 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001304}
1305
Jim Laskeyb2efb852006-01-04 22:28:25 +00001306/// doFinalization - Tear down the debug state after completion of a module.
1307///
1308bool MachineDebugInfo::doFinalization() {
1309 return false;
1310}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001311
Jim Laskeyd96185a2006-02-13 12:50:39 +00001312/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001313///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001314// FIXME - use new Value type when available.
1315DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001316 return DR.Deserialize(V);
1317}
1318
1319/// Verify - Verify that a Value is debug information descriptor.
1320///
1321bool MachineDebugInfo::Verify(Value *V) {
1322 DIVerifier VR;
1323 return VR.Verify(V);
1324}
1325
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001326/// AnalyzeModule - Scan the module for global debug information.
1327///
1328void MachineDebugInfo::AnalyzeModule(Module &M) {
1329 SetupCompileUnits(M);
1330}
1331
1332/// SetupCompileUnits - Set up the unique vector of compile units.
1333///
1334void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001335 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001336
Jim Laskey0420f2a2006-02-22 19:02:11 +00001337 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1338 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001339 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001340}
1341
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001342/// getCompileUnits - Return a vector of debug compile units.
1343///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001344const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001345 return CompileUnits;
1346}
1347
Jim Laskey0420f2a2006-02-22 19:02:11 +00001348/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1349/// named GlobalVariable.
1350std::vector<GlobalVariable*>
1351MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1352 const std::string &RootName) {
1353 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001354}