blob: 46163a777e69e3a8fdea185c56d2930a54e113a4 [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 Laskeyce72b172006-02-11 01:01:30 +0000519 default: break;
520 }
521 return NULL;
522}
523
524/// getLinkage - get linkage appropriate for this type of descriptor.
525///
526GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
527 return GlobalValue::InternalLinkage;
528}
529
530/// ApplyToFields - Target the vistor to the fields of the descriptor.
531///
532void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
533 Visitor->Apply(Tag);
534}
535
536//===----------------------------------------------------------------------===//
537
Jim Laskey9c4447a2006-03-01 20:39:36 +0000538AnchorDesc::AnchorDesc()
539: DebugInfoDesc(DW_TAG_anchor)
540, Name("")
541{}
542AnchorDesc::AnchorDesc(const std::string &N)
543: DebugInfoDesc(DW_TAG_anchor)
544, Name(N)
545{}
546
547// Implement isa/cast/dyncast.
548bool AnchorDesc::classof(const DebugInfoDesc *D) {
549 return D->getTag() == DW_TAG_anchor;
550}
551
Jim Laskeyce72b172006-02-11 01:01:30 +0000552/// getLinkage - get linkage appropriate for this type of descriptor.
553///
554GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
555 return GlobalValue::LinkOnceLinkage;
556}
557
558/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
559///
560void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
561 DebugInfoDesc::ApplyToFields(Visitor);
562
563 Visitor->Apply(Name);
564}
565
566/// getDescString - Return a string used to compose global names and labels.
567///
568const char *AnchorDesc::getDescString() const {
569 return Name.c_str();
570}
571
572/// getTypeString - Return a string used to label this descriptors type.
573///
574const char *AnchorDesc::getTypeString() const {
575 return "llvm.dbg.anchor.type";
576}
577
578#ifndef NDEBUG
579void AnchorDesc::dump() {
580 std::cerr << getDescString() << " "
581 << "Tag(" << getTag() << "), "
582 << "Name(" << Name << ")\n";
583}
584#endif
585
586//===----------------------------------------------------------------------===//
587
588AnchoredDesc::AnchoredDesc(unsigned T)
589: DebugInfoDesc(T)
590, Anchor(NULL)
591{}
592
593/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
594///
595void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
596 DebugInfoDesc::ApplyToFields(Visitor);
597
598 Visitor->Apply((DebugInfoDesc *&)Anchor);
599}
600
601//===----------------------------------------------------------------------===//
602
603CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000604: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000605, DebugVersion(LLVMDebugVersion)
606, Language(0)
607, FileName("")
608, Directory("")
609, Producer("")
610{}
611
Jim Laskey9c4447a2006-03-01 20:39:36 +0000612// Implement isa/cast/dyncast.
613bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
614 return D->getTag() == DW_TAG_compile_unit;
615}
616
Jim Laskey86cbdba2006-02-06 15:33:21 +0000617/// DebugVersionFromGlobal - Returns the version number from a compile unit
618/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000619unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000620 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000621 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000622}
623
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000624/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
625///
626void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000627 AnchoredDesc::ApplyToFields(Visitor);
628
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000629 Visitor->Apply(DebugVersion);
630 Visitor->Apply(Language);
631 Visitor->Apply(FileName);
632 Visitor->Apply(Directory);
633 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000634}
635
Jim Laskeyce72b172006-02-11 01:01:30 +0000636/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000637///
Jim Laskeyce72b172006-02-11 01:01:30 +0000638const char *CompileUnitDesc::getDescString() const {
639 return "llvm.dbg.compile_unit";
640}
641
642/// getTypeString - Return a string used to label this descriptors type.
643///
644const char *CompileUnitDesc::getTypeString() const {
645 return "llvm.dbg.compile_unit.type";
646}
647
648/// getAnchorString - Return a string used to label this descriptor's anchor.
649///
650const char *CompileUnitDesc::getAnchorString() const {
651 return "llvm.dbg.compile_units";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000652}
653
Jim Laskey86cbdba2006-02-06 15:33:21 +0000654#ifndef NDEBUG
655void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000656 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000657 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000658 << "Anchor(" << getAnchor() << "), "
659 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000660 << "Language(" << Language << "), "
661 << "FileName(\"" << FileName << "\"), "
662 << "Directory(\"" << Directory << "\"), "
663 << "Producer(\"" << Producer << "\")\n";
664}
665#endif
666
667//===----------------------------------------------------------------------===//
668
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000669TypeDesc::TypeDesc(unsigned T)
670: DebugInfoDesc(T)
671, Context(NULL)
672, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000673, File(NULL)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000674, Size(0)
675{}
676
Jim Laskey69906002006-02-24 16:46:40 +0000677/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000678///
679void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
680 DebugInfoDesc::ApplyToFields(Visitor);
681
682 Visitor->Apply(Context);
683 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000684 Visitor->Apply((DebugInfoDesc *&)File);
685 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000686 Visitor->Apply(Size);
687}
688
689/// getDescString - Return a string used to compose global names and labels.
690///
691const char *TypeDesc::getDescString() const {
692 return "llvm.dbg.type";
693}
694
695/// getTypeString - Return a string used to label this descriptor's type.
696///
697const char *TypeDesc::getTypeString() const {
698 return "llvm.dbg.type.type";
699}
700
701#ifndef NDEBUG
702void TypeDesc::dump() {
703 std::cerr << getDescString() << " "
704 << "Tag(" << getTag() << "), "
705 << "Context(" << Context << "), "
706 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000707 << "File(" << File << "), "
708 << "Line(" << Line << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000709 << "Size(" << Size << ")\n";
710}
711#endif
712
713//===----------------------------------------------------------------------===//
714
715BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000716: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000717, Encoding(0)
718{}
719
Jim Laskey9c4447a2006-03-01 20:39:36 +0000720// Implement isa/cast/dyncast.
721bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
722 return D->getTag() == DW_TAG_base_type;
723}
724
Jim Laskey69906002006-02-24 16:46:40 +0000725/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000726///
727void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
728 TypeDesc::ApplyToFields(Visitor);
729
730 Visitor->Apply(Encoding);
731}
732
Jim Laskeyf8913f12006-03-01 17:53:02 +0000733/// getDescString - Return a string used to compose global names and labels.
734///
735const char *BasicTypeDesc::getDescString() const {
736 return "llvm.dbg.basictype";
737}
738
739/// getTypeString - Return a string used to label this descriptor's type.
740///
741const char *BasicTypeDesc::getTypeString() const {
742 return "llvm.dbg.basictype.type";
743}
744
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000745#ifndef NDEBUG
746void BasicTypeDesc::dump() {
747 std::cerr << getDescString() << " "
748 << "Tag(" << getTag() << "), "
749 << "Context(" << getContext() << "), "
750 << "Name(\"" << getName() << "\"), "
751 << "Size(" << getSize() << "), "
752 << "Encoding(" << Encoding << ")\n";
753}
754#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000755
Jim Laskey434b40b2006-02-23 22:37:30 +0000756//===----------------------------------------------------------------------===//
757
Jim Laskey69906002006-02-24 16:46:40 +0000758DerivedTypeDesc::DerivedTypeDesc(unsigned T)
759: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000760, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000761{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000762
Jim Laskey9c4447a2006-03-01 20:39:36 +0000763// Implement isa/cast/dyncast.
764bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
765 unsigned T = D->getTag();
766 switch (T) {
767 case DW_TAG_typedef:
768 case DW_TAG_pointer_type:
769 case DW_TAG_reference_type:
770 case DW_TAG_const_type:
771 case DW_TAG_volatile_type:
772 case DW_TAG_restrict_type:
773 return true;
774 default: break;
775 }
776 return false;
777}
778
Jim Laskey69906002006-02-24 16:46:40 +0000779/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000780///
Jim Laskey69906002006-02-24 16:46:40 +0000781void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000782 TypeDesc::ApplyToFields(Visitor);
783
784 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000785}
786
Jim Laskeyf8913f12006-03-01 17:53:02 +0000787/// getDescString - Return a string used to compose global names and labels.
788///
789const char *DerivedTypeDesc::getDescString() const {
790 return "llvm.dbg.derivedtype";
791}
792
793/// getTypeString - Return a string used to label this descriptor's type.
794///
795const char *DerivedTypeDesc::getTypeString() const {
796 return "llvm.dbg.derivedtype.type";
797}
798
Jim Laskey434b40b2006-02-23 22:37:30 +0000799#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000800void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000801 std::cerr << getDescString() << " "
802 << "Tag(" << getTag() << "), "
803 << "Context(" << getContext() << "), "
804 << "Name(\"" << getName() << "\"), "
805 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000806 << "File(" << getFile() << "), "
807 << "Line(" << getLine() << "), "
808 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000809}
810#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000811
812//===----------------------------------------------------------------------===//
813
Jim Laskeyf8913f12006-03-01 17:53:02 +0000814CompositeTypeDesc::CompositeTypeDesc(unsigned T)
815: DerivedTypeDesc(T)
816, Elements()
817{}
818
Jim Laskey9c4447a2006-03-01 20:39:36 +0000819// Implement isa/cast/dyncast.
820bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
821 unsigned T = D->getTag();
822 switch (T) {
823 case DW_TAG_array_type:
824 case DW_TAG_structure_type:
825 case DW_TAG_union_type:
826 case DW_TAG_enumeration_type:
827 return true;
828 default: break;
829 }
830 return false;
831}
832
Jim Laskeyf8913f12006-03-01 17:53:02 +0000833/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
834///
835void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
836 DerivedTypeDesc::ApplyToFields(Visitor);
837
838 Visitor->Apply(Elements);
839}
840
841/// getDescString - Return a string used to compose global names and labels.
842///
843const char *CompositeTypeDesc::getDescString() const {
844 return "llvm.dbg.compositetype";
845}
846
847/// getTypeString - Return a string used to label this descriptor's type.
848///
849const char *CompositeTypeDesc::getTypeString() const {
850 return "llvm.dbg.compositetype.type";
851}
852
853#ifndef NDEBUG
854void CompositeTypeDesc::dump() {
855 std::cerr << getDescString() << " "
856 << "Tag(" << getTag() << "), "
857 << "Context(" << getContext() << "), "
858 << "Name(\"" << getName() << "\"), "
859 << "Size(" << getSize() << "), "
860 << "File(" << getFile() << "), "
861 << "Line(" << getLine() << "), "
862 << "FromType(" << getFromType() << "), "
863 << "Elements.size(" << Elements.size() << ")\n";
864}
865#endif
866
867//===----------------------------------------------------------------------===//
868
869SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000870: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000871, Lo(0)
872, Hi(0)
873{}
874
Jim Laskey9c4447a2006-03-01 20:39:36 +0000875// Implement isa/cast/dyncast.
876bool SubrangeDesc::classof(const DebugInfoDesc *D) {
877 return D->getTag() == DW_TAG_subrange_type;
878}
879
Jim Laskeyf8913f12006-03-01 17:53:02 +0000880/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
881///
882void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
883 DebugInfoDesc::ApplyToFields(Visitor);
884
885 Visitor->Apply(Lo);
886 Visitor->Apply(Hi);
887}
888
889/// getDescString - Return a string used to compose global names and labels.
890///
891const char *SubrangeDesc::getDescString() const {
892 return "llvm.dbg.subrange";
893}
894
895/// getTypeString - Return a string used to label this descriptor's type.
896///
897const char *SubrangeDesc::getTypeString() const {
898 return "llvm.dbg.subrange.type";
899}
900
901#ifndef NDEBUG
902void SubrangeDesc::dump() {
903 std::cerr << getDescString() << " "
904 << "Tag(" << getTag() << "), "
905 << "Lo(" << Lo << "), "
906 << "Hi(" << Hi << ")\n";
907}
908#endif
909
910//===----------------------------------------------------------------------===//
911
Jim Laskeyce72b172006-02-11 01:01:30 +0000912GlobalDesc::GlobalDesc(unsigned T)
913: AnchoredDesc(T)
914, Context(0)
915, Name("")
916, TyDesc(NULL)
917, IsStatic(false)
918, IsDefinition(false)
919{}
920
921/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000922///
Jim Laskeyce72b172006-02-11 01:01:30 +0000923void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
924 AnchoredDesc::ApplyToFields(Visitor);
925
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000926 Visitor->Apply(Context);
927 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000928 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000929 Visitor->Apply(IsStatic);
930 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000931}
932
933//===----------------------------------------------------------------------===//
934
935GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000936: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +0000937, Global(NULL)
938{}
939
Jim Laskey9c4447a2006-03-01 20:39:36 +0000940// Implement isa/cast/dyncast.
941bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
942 return D->getTag() == DW_TAG_variable;
943}
944
Jim Laskeyce72b172006-02-11 01:01:30 +0000945/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
946///
947void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
948 GlobalDesc::ApplyToFields(Visitor);
949
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000950 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000951 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000952}
953
Jim Laskeyce72b172006-02-11 01:01:30 +0000954/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000955///
Jim Laskeyce72b172006-02-11 01:01:30 +0000956const char *GlobalVariableDesc::getDescString() const {
957 return "llvm.dbg.global_variable";
958}
959
960/// getTypeString - Return a string used to label this descriptors type.
961///
962const char *GlobalVariableDesc::getTypeString() const {
963 return "llvm.dbg.global_variable.type";
964}
965
966/// getAnchorString - Return a string used to label this descriptor's anchor.
967///
968const char *GlobalVariableDesc::getAnchorString() const {
969 return "llvm.dbg.global_variables";
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000970}
971
Jim Laskey86cbdba2006-02-06 15:33:21 +0000972#ifndef NDEBUG
973void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000974 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000975 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000976 << "Anchor(" << getAnchor() << "), "
977 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000978 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000979 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
980 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +0000981 << "Global(" << Global << "), "
982 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000983}
984#endif
985
986//===----------------------------------------------------------------------===//
987
Jim Laskeyce72b172006-02-11 01:01:30 +0000988SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000989: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +0000990{}
991
Jim Laskey9c4447a2006-03-01 20:39:36 +0000992// Implement isa/cast/dyncast.
993bool SubprogramDesc::classof(const DebugInfoDesc *D) {
994 return D->getTag() == DW_TAG_subprogram;
995}
996
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000997/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +0000998/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000999void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001000 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001001}
1002
Jim Laskeyce72b172006-02-11 01:01:30 +00001003/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001004///
Jim Laskeyce72b172006-02-11 01:01:30 +00001005const char *SubprogramDesc::getDescString() const {
1006 return "llvm.dbg.subprogram";
1007}
1008
1009/// getTypeString - Return a string used to label this descriptors type.
1010///
1011const char *SubprogramDesc::getTypeString() const {
1012 return "llvm.dbg.subprogram.type";
1013}
1014
1015/// getAnchorString - Return a string used to label this descriptor's anchor.
1016///
1017const char *SubprogramDesc::getAnchorString() const {
1018 return "llvm.dbg.subprograms";
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001019}
1020
Jim Laskey86cbdba2006-02-06 15:33:21 +00001021#ifndef NDEBUG
1022void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001023 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001024 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001025 << "Anchor(" << getAnchor() << "), "
1026 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001027 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001028 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1029 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001030}
1031#endif
1032
Jim Laskey45ccae52006-02-28 20:15:07 +00001033//===----------------------------------------------------------------------===//
1034
Jim Laskey86cbdba2006-02-06 15:33:21 +00001035DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001036 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001037}
1038DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001039 // Handle NULL.
1040 if (!GV) return NULL;
1041
Jim Laskey86cbdba2006-02-06 15:33:21 +00001042 // Check to see if it has been already deserialized.
1043 DebugInfoDesc *&Slot = GlobalDescs[GV];
1044 if (Slot) return Slot;
1045
1046 // Get the Tag from the global.
1047 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1048
1049 // Get the debug version if a compile unit.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001050 if (Tag == DW_TAG_compile_unit) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001051 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1052 }
1053
1054 // Create an empty instance of the correct sort.
1055 Slot = DebugInfoDesc::DescFactory(Tag);
1056 assert(Slot && "Unknown Tag");
1057
1058 // Deserialize the fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001059 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001060 DRAM.ApplyToFields(Slot);
1061
1062 return Slot;
1063}
1064
1065//===----------------------------------------------------------------------===//
1066
1067/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001068///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001069const PointerType *DISerializer::getStrPtrType() {
1070 // If not already defined.
1071 if (!StrPtrTy) {
1072 // Construct the pointer to signed bytes.
1073 StrPtrTy = PointerType::get(Type::SByteTy);
1074 }
1075
1076 return StrPtrTy;
1077}
1078
1079/// getEmptyStructPtrType - Return a "{ }*" type.
1080///
1081const PointerType *DISerializer::getEmptyStructPtrType() {
1082 // If not already defined.
1083 if (!EmptyStructPtrTy) {
1084 // Construct the empty structure type.
1085 const StructType *EmptyStructTy =
1086 StructType::get(std::vector<const Type*>());
1087 // Construct the pointer to empty structure type.
1088 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1089 }
1090
1091 return EmptyStructPtrTy;
1092}
1093
1094/// getTagType - Return the type describing the specified descriptor (via tag.)
1095///
1096const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1097 // Attempt to get the previously defined type.
1098 StructType *&Ty = TagTypes[DD->getTag()];
1099
1100 // If not already defined.
1101 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001102 // Set up fields vector.
1103 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001104 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001105 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001106 GTAM.ApplyToFields(DD);
1107
1108 // Construct structured type.
1109 Ty = StructType::get(Fields);
1110
Jim Laskey86cbdba2006-02-06 15:33:21 +00001111 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001112 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001113 }
1114
1115 return Ty;
1116}
1117
1118/// getString - Construct the string as constant string global.
1119///
Jim Laskeyce72b172006-02-11 01:01:30 +00001120Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001121 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001122 Constant *&Slot = StringCache[String];
1123 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001124 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +00001125 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001126 Constant *ConstStr = ConstantArray::get(String);
1127 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +00001128 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1129 GlobalVariable::InternalLinkage,
1130 ConstStr, "str", M);
1131 // Convert to generic string pointer.
1132 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1133 return Slot;
1134
Jim Laskey86cbdba2006-02-06 15:33:21 +00001135}
1136
1137/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1138/// so that it can be serialized to a .bc or .ll file.
1139GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1140 // Check if the DebugInfoDesc is already in the map.
1141 GlobalVariable *&Slot = DescGlobals[DD];
1142
1143 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1144 if (Slot) return Slot;
1145
Jim Laskey86cbdba2006-02-06 15:33:21 +00001146 // Get the type associated with the Tag.
1147 const StructType *Ty = getTagType(DD);
1148
1149 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001150 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1151 NULL, DD->getDescString(), M);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001152
1153 // Insert new GlobalVariable in DescGlobals map.
1154 Slot = GV;
1155
1156 // Set up elements vector
1157 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001158 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001159 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001160 SRAM.ApplyToFields(DD);
1161
1162 // Set the globals initializer.
1163 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1164
1165 return GV;
1166}
1167
1168//===----------------------------------------------------------------------===//
1169
1170/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1171/// Mark visited otherwise.
1172bool DIVerifier::markVisited(GlobalVariable *GV) {
1173 // Check if the GlobalVariable is already in the Visited set.
1174 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1175
1176 // See if GlobalVariable exists.
1177 bool Exists = VI != Visited.end() && *VI == GV;
1178
1179 // Insert in set.
1180 if (!Exists) Visited.insert(VI, GV);
1181
1182 return Exists;
1183}
1184
1185/// Verify - Return true if the GlobalVariable appears to be a valid
1186/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001187bool DIVerifier::Verify(Value *V) {
1188 return Verify(getGlobalVariable(V));
1189}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001190bool DIVerifier::Verify(GlobalVariable *GV) {
1191 // Check if seen before.
1192 if (markVisited(GV)) return true;
1193
1194 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001195 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001196 if (Tag == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001197
1198 // If a compile unit we need the debug version.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001199 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001200 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001201 if (DebugVersion == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001202 }
1203
1204 // Construct an empty DebugInfoDesc.
1205 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1206 if (!DD) return false;
1207
1208 // Get the initializer constant.
1209 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1210
1211 // Get the operand count.
1212 unsigned N = CI->getNumOperands();
1213
1214 // Get the field count.
1215 unsigned &Slot = Counts[Tag];
1216 if (!Slot) {
1217 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001218 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001219 CTAM.ApplyToFields(DD);
1220 Slot = CTAM.getCount();
1221 }
1222
1223 // Field count must equal operand count.
1224 if (Slot != N) {
1225 delete DD;
1226 return false;
1227 }
1228
1229 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001230 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001231 VRAM.ApplyToFields(DD);
1232
1233 // Release empty DebugInfoDesc.
1234 delete DD;
1235
1236 // Return result of field tests.
1237 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001238}
1239
1240//===----------------------------------------------------------------------===//
1241
1242
1243MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001244: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001245, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001246, Directories()
1247, SourceFiles()
1248, Lines()
1249{
1250
1251}
1252MachineDebugInfo::~MachineDebugInfo() {
1253
1254}
1255
Jim Laskeyb2efb852006-01-04 22:28:25 +00001256/// doInitialization - Initialize the debug state for a new module.
1257///
1258bool MachineDebugInfo::doInitialization() {
1259 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001260}
1261
Jim Laskeyb2efb852006-01-04 22:28:25 +00001262/// doFinalization - Tear down the debug state after completion of a module.
1263///
1264bool MachineDebugInfo::doFinalization() {
1265 return false;
1266}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001267
Jim Laskeyd96185a2006-02-13 12:50:39 +00001268/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001269///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001270// FIXME - use new Value type when available.
1271DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001272 return DR.Deserialize(V);
1273}
1274
1275/// Verify - Verify that a Value is debug information descriptor.
1276///
1277bool MachineDebugInfo::Verify(Value *V) {
1278 DIVerifier VR;
1279 return VR.Verify(V);
1280}
1281
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001282/// AnalyzeModule - Scan the module for global debug information.
1283///
1284void MachineDebugInfo::AnalyzeModule(Module &M) {
1285 SetupCompileUnits(M);
1286}
1287
1288/// SetupCompileUnits - Set up the unique vector of compile units.
1289///
1290void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001291 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001292
Jim Laskey0420f2a2006-02-22 19:02:11 +00001293 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1294 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001295 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001296}
1297
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001298/// getCompileUnits - Return a vector of debug compile units.
1299///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001300const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001301 return CompileUnits;
1302}
1303
Jim Laskey0420f2a2006-02-22 19:02:11 +00001304/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1305/// named GlobalVariable.
1306std::vector<GlobalVariable*>
1307MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1308 const std::string &RootName) {
1309 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001310}