blob: 74f827acbfb7abf4e04eb5ba6e83a2d7cddae1c5 [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);
Jim Laskeye8c3e3b2006-03-07 20:53:47 +000056 FieldTypes.push_back(Type::UIntTy);
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
Jim Laskey86cbdba2006-02-06 15:33:21 +000070/// isStringValue - Return true if the given value can be coerced to a string.
71///
72static bool isStringValue(Value *V) {
73 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
74 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
75 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
76 return Init->isString();
77 }
78 } else if (Constant *C = dyn_cast<Constant>(V)) {
79 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
80 return isStringValue(GV);
81 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
82 if (CE->getOpcode() == Instruction::GetElementPtr) {
83 if (CE->getNumOperands() == 3 &&
84 cast<Constant>(CE->getOperand(1))->isNullValue() &&
85 isa<ConstantInt>(CE->getOperand(2))) {
86 return isStringValue(CE->getOperand(0));
87 }
88 }
89 }
90 }
91 return false;
92}
93
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000094/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000095///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000096static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000097 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
98 return GV;
99 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000100 if (CE->getOpcode() == Instruction::Cast) {
101 return dyn_cast<GlobalVariable>(CE->getOperand(0));
102 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000103 }
104 return NULL;
105}
106
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000107/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000108/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000109static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000110 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
111 return true;
112 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
113 if (CE->getOpcode() == Instruction::Cast) {
114 return isa<GlobalVariable>(CE->getOperand(0));
115 }
116 }
117 return false;
118}
119
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000120/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000121///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000122static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000123 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000124 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000125
Jim Laskey86cbdba2006-02-06 15:33:21 +0000126 // Get the initializer constant.
127 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000128 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000129
Jim Laskey86cbdba2006-02-06 15:33:21 +0000130 // Check if there is at least i + 1 operands.
131 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000132 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000133
Jim Laskey86cbdba2006-02-06 15:33:21 +0000134 // Check constant.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000135 return dyn_cast<ConstantUInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000136}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000137//===----------------------------------------------------------------------===//
138
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000139/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000140/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000141void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000142 DD->ApplyToFields(this);
143}
144
145//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000146/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
147/// the supplied DebugInfoDesc.
148class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000149private:
150 unsigned Count; // Running count of fields.
151
152public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000153 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000154
155 // Accessors.
156 unsigned getCount() const { return Count; }
157
158 /// Apply - Count each of the fields.
159 ///
160 virtual void Apply(int &Field) { ++Count; }
161 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000162 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000163 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000164 virtual void Apply(bool &Field) { ++Count; }
165 virtual void Apply(std::string &Field) { ++Count; }
166 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
167 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000168 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
169 ++Count;
170 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000171};
172
173//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000174/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
175/// supplied DebugInfoDesc.
176class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000177private:
178 DIDeserializer &DR; // Active deserializer.
179 unsigned I; // Current operand index.
180 ConstantStruct *CI; // GlobalVariable constant initializer.
181
182public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000183 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
184 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000185 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000186 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000187 , CI(cast<ConstantStruct>(GV->getInitializer()))
188 {}
189
190 /// Apply - Set the value of each of the fields.
191 ///
192 virtual void Apply(int &Field) {
193 Constant *C = CI->getOperand(I++);
194 Field = cast<ConstantSInt>(C)->getValue();
195 }
196 virtual void Apply(unsigned &Field) {
197 Constant *C = CI->getOperand(I++);
198 Field = cast<ConstantUInt>(C)->getValue();
199 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000200 virtual void Apply(int64_t &Field) {
201 Constant *C = CI->getOperand(I++);
202 Field = cast<ConstantSInt>(C)->getValue();
203 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000204 virtual void Apply(uint64_t &Field) {
205 Constant *C = CI->getOperand(I++);
206 Field = cast<ConstantUInt>(C)->getValue();
207 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000208 virtual void Apply(bool &Field) {
209 Constant *C = CI->getOperand(I++);
210 Field = cast<ConstantBool>(C)->getValue();
211 }
212 virtual void Apply(std::string &Field) {
213 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000214 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000215 }
216 virtual void Apply(DebugInfoDesc *&Field) {
217 Constant *C = CI->getOperand(I++);
218 Field = DR.Deserialize(C);
219 }
220 virtual void Apply(GlobalVariable *&Field) {
221 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000222 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000223 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000224 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
225 Constant *C = CI->getOperand(I++);
226 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskey45ccae52006-02-28 20:15:07 +0000227 Field.resize(0);
Jim Laskey2b0e3092006-03-08 02:07:02 +0000228 // Have to be able to deal with the empty array case (zero initializer)
229 if (!GV->hasInitializer()) return;
230 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
231 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
232 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
233 DebugInfoDesc *DE = DR.Deserialize(GVE);
234 Field.push_back(DE);
235 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000236 }
237 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000238};
239
240//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000241/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000242/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000243class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000244private:
245 DISerializer &SR; // Active serializer.
246 std::vector<Constant*> &Elements; // Element accumulator.
247
248public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000249 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
250 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000251 , SR(S)
252 , Elements(E)
253 {}
254
255 /// Apply - Set the value of each of the fields.
256 ///
257 virtual void Apply(int &Field) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000258 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000259 }
260 virtual void Apply(unsigned &Field) {
261 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
262 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000263 virtual void Apply(int64_t &Field) {
264 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
265 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000266 virtual void Apply(uint64_t &Field) {
267 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
268 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000269 virtual void Apply(bool &Field) {
270 Elements.push_back(ConstantBool::get(Field));
271 }
272 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000273 if (Field.empty()) {
274 Elements.push_back(NULL);
275 } else {
276 Elements.push_back(SR.getString(Field));
277 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000278 }
279 virtual void Apply(DebugInfoDesc *&Field) {
280 GlobalVariable *GV = NULL;
281
282 // If non-NULL the convert to global.
283 if (Field) GV = SR.Serialize(Field);
284
285 // FIXME - At some point should use specific type.
286 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
287
288 if (GV) {
289 // Set to pointer to global.
290 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
291 } else {
292 // Use NULL.
293 Elements.push_back(ConstantPointerNull::get(EmptyTy));
294 }
295 }
296 virtual void Apply(GlobalVariable *&Field) {
297 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000298 if (Field) {
299 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
300 } else {
301 Elements.push_back(ConstantPointerNull::get(EmptyTy));
302 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000303 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000304 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
305 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
306 unsigned N = Field.size();
307 ArrayType *AT = ArrayType::get(EmptyTy, N);
308 std::vector<Constant *> ArrayElements;
309
310 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
311 GlobalVariable *GVE = SR.Serialize(Field[i]);
312 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
313 ArrayElements.push_back(cast<Constant>(CE));
314 }
315
316 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000317 GlobalVariable *CAGV = new GlobalVariable(AT, true,
318 GlobalValue::InternalLinkage,
319 CA, "llvm.dbg.array",
320 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000321 CAGV->setSection("llvm.metadata");
Jim Laskeyf8913f12006-03-01 17:53:02 +0000322 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000323 Elements.push_back(CAE);
324 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000325};
326
327//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000328/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000329/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000330class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000331private:
332 DISerializer &SR; // Active serializer.
333 std::vector<const Type*> &Fields; // Type accumulator.
334
335public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000336 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
337 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000338 , SR(S)
339 , Fields(F)
340 {}
341
342 /// Apply - Set the value of each of the fields.
343 ///
344 virtual void Apply(int &Field) {
345 Fields.push_back(Type::IntTy);
346 }
347 virtual void Apply(unsigned &Field) {
348 Fields.push_back(Type::UIntTy);
349 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000350 virtual void Apply(int64_t &Field) {
351 Fields.push_back(Type::IntTy);
352 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000353 virtual void Apply(uint64_t &Field) {
354 Fields.push_back(Type::UIntTy);
355 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000356 virtual void Apply(bool &Field) {
357 Fields.push_back(Type::BoolTy);
358 }
359 virtual void Apply(std::string &Field) {
360 Fields.push_back(SR.getStrPtrType());
361 }
362 virtual void Apply(DebugInfoDesc *&Field) {
363 // FIXME - At some point should use specific type.
364 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
365 Fields.push_back(EmptyTy);
366 }
367 virtual void Apply(GlobalVariable *&Field) {
368 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
369 Fields.push_back(EmptyTy);
370 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000371 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
372 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
373 Fields.push_back(EmptyTy);
374 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000375};
376
377//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000378/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000379/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000380class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000381private:
382 DIVerifier &VR; // Active verifier.
383 bool IsValid; // Validity status.
384 unsigned I; // Current operand index.
385 ConstantStruct *CI; // GlobalVariable constant initializer.
386
387public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000388 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
389 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000390 , VR(V)
391 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000392 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000393 , CI(cast<ConstantStruct>(GV->getInitializer()))
394 {
395 }
396
397 // Accessors.
398 bool isValid() const { return IsValid; }
399
400 /// Apply - Set the value of each of the fields.
401 ///
402 virtual void Apply(int &Field) {
403 Constant *C = CI->getOperand(I++);
404 IsValid = IsValid && isa<ConstantInt>(C);
405 }
406 virtual void Apply(unsigned &Field) {
407 Constant *C = CI->getOperand(I++);
408 IsValid = IsValid && isa<ConstantInt>(C);
409 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000410 virtual void Apply(int64_t &Field) {
411 Constant *C = CI->getOperand(I++);
412 IsValid = IsValid && isa<ConstantInt>(C);
413 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000414 virtual void Apply(uint64_t &Field) {
415 Constant *C = CI->getOperand(I++);
416 IsValid = IsValid && isa<ConstantInt>(C);
417 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000418 virtual void Apply(bool &Field) {
419 Constant *C = CI->getOperand(I++);
420 IsValid = IsValid && isa<ConstantBool>(C);
421 }
422 virtual void Apply(std::string &Field) {
423 Constant *C = CI->getOperand(I++);
Jim Laskey21407982006-03-14 18:37:57 +0000424 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000425 }
426 virtual void Apply(DebugInfoDesc *&Field) {
427 // FIXME - Prepare the correct descriptor.
428 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000429 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000430 }
431 virtual void Apply(GlobalVariable *&Field) {
432 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000433 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000434 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000435 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
436 Constant *C = CI->getOperand(I++);
437 IsValid = IsValid && isGlobalVariable(C);
438 if (!IsValid) return;
439
440 GlobalVariable *GV = getGlobalVariable(C);
441 IsValid = IsValid && GV && GV->hasInitializer();
442 if (!IsValid) return;
443
444 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
445 IsValid = IsValid && CA;
446 if (!IsValid) return;
447
448 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
449 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
450 if (!IsValid) return;
451
452 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
453 VR.Verify(GVE);
454 }
455 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000456};
457
Jim Laskeyce72b172006-02-11 01:01:30 +0000458
Jim Laskey86cbdba2006-02-06 15:33:21 +0000459//===----------------------------------------------------------------------===//
460
Jim Laskeyce72b172006-02-11 01:01:30 +0000461/// TagFromGlobal - Returns the Tag number from a debug info descriptor
462/// GlobalVariable.
463unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
464 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000465 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000466}
467
468/// DescFactory - Create an instance of debug info descriptor based on Tag.
469/// Return NULL if not a recognized Tag.
470DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
471 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000472 case DW_TAG_anchor: return new AnchorDesc();
473 case DW_TAG_compile_unit: return new CompileUnitDesc();
474 case DW_TAG_variable: return new GlobalVariableDesc();
475 case DW_TAG_subprogram: return new SubprogramDesc();
476 case DW_TAG_base_type: return new BasicTypeDesc();
477 case DW_TAG_typedef:
478 case DW_TAG_pointer_type:
479 case DW_TAG_reference_type:
480 case DW_TAG_const_type:
481 case DW_TAG_volatile_type:
482 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
483 case DW_TAG_array_type:
484 case DW_TAG_structure_type:
485 case DW_TAG_union_type:
486 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
487 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskeyf01e5472006-03-03 15:06:57 +0000488 case DW_TAG_member: return new DerivedTypeDesc(DW_TAG_member);
Jim Laskey6a3eb012006-03-01 23:52:37 +0000489 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyce72b172006-02-11 01:01:30 +0000490 default: break;
491 }
492 return NULL;
493}
494
495/// getLinkage - get linkage appropriate for this type of descriptor.
496///
497GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
498 return GlobalValue::InternalLinkage;
499}
500
501/// ApplyToFields - Target the vistor to the fields of the descriptor.
502///
503void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
504 Visitor->Apply(Tag);
505}
506
507//===----------------------------------------------------------------------===//
508
Jim Laskey9c4447a2006-03-01 20:39:36 +0000509AnchorDesc::AnchorDesc()
510: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000511, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000512{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000513AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000514: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000515, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000516{}
517
518// Implement isa/cast/dyncast.
519bool AnchorDesc::classof(const DebugInfoDesc *D) {
520 return D->getTag() == DW_TAG_anchor;
521}
522
Jim Laskeyce72b172006-02-11 01:01:30 +0000523/// getLinkage - get linkage appropriate for this type of descriptor.
524///
525GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
526 return GlobalValue::LinkOnceLinkage;
527}
528
529/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
530///
531void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
532 DebugInfoDesc::ApplyToFields(Visitor);
533
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000534 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000535}
536
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000537/// getDescString - Return a string used to compose global names and labels. A
538/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000539/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000540/// to the list of names left external in the internalizer.
541/// ExternalNames.insert("llvm.dbg.compile_units");
542/// ExternalNames.insert("llvm.dbg.global_variables");
543/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000544const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000545 switch (AnchorTag) {
546 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
547 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
548 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
549 default: break;
550 }
551
552 assert(0 && "Tag does not have a case for anchor string");
553 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000554}
555
556/// getTypeString - Return a string used to label this descriptors type.
557///
558const char *AnchorDesc::getTypeString() const {
559 return "llvm.dbg.anchor.type";
560}
561
562#ifndef NDEBUG
563void AnchorDesc::dump() {
564 std::cerr << getDescString() << " "
565 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000566 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000567}
568#endif
569
570//===----------------------------------------------------------------------===//
571
572AnchoredDesc::AnchoredDesc(unsigned T)
573: DebugInfoDesc(T)
574, Anchor(NULL)
575{}
576
577/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
578///
579void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
580 DebugInfoDesc::ApplyToFields(Visitor);
581
582 Visitor->Apply((DebugInfoDesc *&)Anchor);
583}
584
585//===----------------------------------------------------------------------===//
586
587CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000588: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000589, DebugVersion(LLVMDebugVersion)
590, Language(0)
591, FileName("")
592, Directory("")
593, Producer("")
594{}
595
Jim Laskey9c4447a2006-03-01 20:39:36 +0000596// Implement isa/cast/dyncast.
597bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
598 return D->getTag() == DW_TAG_compile_unit;
599}
600
Jim Laskey86cbdba2006-02-06 15:33:21 +0000601/// DebugVersionFromGlobal - Returns the version number from a compile unit
602/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000603unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000604 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000605 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000606}
607
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000608/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
609///
610void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000611 AnchoredDesc::ApplyToFields(Visitor);
612
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000613 Visitor->Apply(DebugVersion);
614 Visitor->Apply(Language);
615 Visitor->Apply(FileName);
616 Visitor->Apply(Directory);
617 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000618}
619
Jim Laskeyce72b172006-02-11 01:01:30 +0000620/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000621///
Jim Laskeyce72b172006-02-11 01:01:30 +0000622const char *CompileUnitDesc::getDescString() const {
623 return "llvm.dbg.compile_unit";
624}
625
626/// getTypeString - Return a string used to label this descriptors type.
627///
628const char *CompileUnitDesc::getTypeString() const {
629 return "llvm.dbg.compile_unit.type";
630}
631
632/// getAnchorString - Return a string used to label this descriptor's anchor.
633///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000634const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000635const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000636 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000637}
638
Jim Laskey86cbdba2006-02-06 15:33:21 +0000639#ifndef NDEBUG
640void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000641 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000642 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000643 << "Anchor(" << getAnchor() << "), "
644 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000645 << "Language(" << Language << "), "
646 << "FileName(\"" << FileName << "\"), "
647 << "Directory(\"" << Directory << "\"), "
648 << "Producer(\"" << Producer << "\")\n";
649}
650#endif
651
652//===----------------------------------------------------------------------===//
653
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000654TypeDesc::TypeDesc(unsigned T)
655: DebugInfoDesc(T)
656, Context(NULL)
657, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000658, File(NULL)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000659, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000660, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000661, Offset(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000662{}
663
Jim Laskey69906002006-02-24 16:46:40 +0000664/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000665///
666void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
667 DebugInfoDesc::ApplyToFields(Visitor);
668
669 Visitor->Apply(Context);
670 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000671 Visitor->Apply((DebugInfoDesc *&)File);
672 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000673 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000674 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000675 Visitor->Apply(Offset);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000676}
677
678/// getDescString - Return a string used to compose global names and labels.
679///
680const char *TypeDesc::getDescString() const {
681 return "llvm.dbg.type";
682}
683
684/// getTypeString - Return a string used to label this descriptor's type.
685///
686const char *TypeDesc::getTypeString() const {
687 return "llvm.dbg.type.type";
688}
689
690#ifndef NDEBUG
691void TypeDesc::dump() {
692 std::cerr << getDescString() << " "
693 << "Tag(" << getTag() << "), "
694 << "Context(" << Context << "), "
695 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000696 << "File(" << File << "), "
697 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000698 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000699 << "Align(" << Align << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000700 << "Offset(" << Offset << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701}
702#endif
703
704//===----------------------------------------------------------------------===//
705
706BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000707: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000708, Encoding(0)
709{}
710
Jim Laskey9c4447a2006-03-01 20:39:36 +0000711// Implement isa/cast/dyncast.
712bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
713 return D->getTag() == DW_TAG_base_type;
714}
715
Jim Laskey69906002006-02-24 16:46:40 +0000716/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000717///
718void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
719 TypeDesc::ApplyToFields(Visitor);
720
721 Visitor->Apply(Encoding);
722}
723
Jim Laskeyf8913f12006-03-01 17:53:02 +0000724/// getDescString - Return a string used to compose global names and labels.
725///
726const char *BasicTypeDesc::getDescString() const {
727 return "llvm.dbg.basictype";
728}
729
730/// getTypeString - Return a string used to label this descriptor's type.
731///
732const char *BasicTypeDesc::getTypeString() const {
733 return "llvm.dbg.basictype.type";
734}
735
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000736#ifndef NDEBUG
737void BasicTypeDesc::dump() {
738 std::cerr << getDescString() << " "
739 << "Tag(" << getTag() << "), "
740 << "Context(" << getContext() << "), "
741 << "Name(\"" << getName() << "\"), "
742 << "Size(" << getSize() << "), "
743 << "Encoding(" << Encoding << ")\n";
744}
745#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000746
Jim Laskey434b40b2006-02-23 22:37:30 +0000747//===----------------------------------------------------------------------===//
748
Jim Laskey69906002006-02-24 16:46:40 +0000749DerivedTypeDesc::DerivedTypeDesc(unsigned T)
750: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000751, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000752{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000753
Jim Laskey9c4447a2006-03-01 20:39:36 +0000754// Implement isa/cast/dyncast.
755bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
756 unsigned T = D->getTag();
757 switch (T) {
758 case DW_TAG_typedef:
759 case DW_TAG_pointer_type:
760 case DW_TAG_reference_type:
761 case DW_TAG_const_type:
762 case DW_TAG_volatile_type:
763 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000764 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000765 return true;
766 default: break;
767 }
768 return false;
769}
770
Jim Laskey69906002006-02-24 16:46:40 +0000771/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000772///
Jim Laskey69906002006-02-24 16:46:40 +0000773void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000774 TypeDesc::ApplyToFields(Visitor);
775
776 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000777}
778
Jim Laskeyf8913f12006-03-01 17:53:02 +0000779/// getDescString - Return a string used to compose global names and labels.
780///
781const char *DerivedTypeDesc::getDescString() const {
782 return "llvm.dbg.derivedtype";
783}
784
785/// getTypeString - Return a string used to label this descriptor's type.
786///
787const char *DerivedTypeDesc::getTypeString() const {
788 return "llvm.dbg.derivedtype.type";
789}
790
Jim Laskey434b40b2006-02-23 22:37:30 +0000791#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000792void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000793 std::cerr << getDescString() << " "
794 << "Tag(" << getTag() << "), "
795 << "Context(" << getContext() << "), "
796 << "Name(\"" << getName() << "\"), "
797 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000798 << "File(" << getFile() << "), "
799 << "Line(" << getLine() << "), "
800 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000801}
802#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000803
804//===----------------------------------------------------------------------===//
805
Jim Laskeyf8913f12006-03-01 17:53:02 +0000806CompositeTypeDesc::CompositeTypeDesc(unsigned T)
807: DerivedTypeDesc(T)
808, Elements()
809{}
810
Jim Laskey9c4447a2006-03-01 20:39:36 +0000811// Implement isa/cast/dyncast.
812bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
813 unsigned T = D->getTag();
814 switch (T) {
815 case DW_TAG_array_type:
816 case DW_TAG_structure_type:
817 case DW_TAG_union_type:
818 case DW_TAG_enumeration_type:
819 return true;
820 default: break;
821 }
822 return false;
823}
824
Jim Laskeyf8913f12006-03-01 17:53:02 +0000825/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
826///
827void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
828 DerivedTypeDesc::ApplyToFields(Visitor);
829
830 Visitor->Apply(Elements);
831}
832
833/// getDescString - Return a string used to compose global names and labels.
834///
835const char *CompositeTypeDesc::getDescString() const {
836 return "llvm.dbg.compositetype";
837}
838
839/// getTypeString - Return a string used to label this descriptor's type.
840///
841const char *CompositeTypeDesc::getTypeString() const {
842 return "llvm.dbg.compositetype.type";
843}
844
845#ifndef NDEBUG
846void CompositeTypeDesc::dump() {
847 std::cerr << getDescString() << " "
848 << "Tag(" << getTag() << "), "
849 << "Context(" << getContext() << "), "
850 << "Name(\"" << getName() << "\"), "
851 << "Size(" << getSize() << "), "
852 << "File(" << getFile() << "), "
853 << "Line(" << getLine() << "), "
854 << "FromType(" << getFromType() << "), "
855 << "Elements.size(" << Elements.size() << ")\n";
856}
857#endif
858
859//===----------------------------------------------------------------------===//
860
861SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000862: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000863, Lo(0)
864, Hi(0)
865{}
866
Jim Laskey9c4447a2006-03-01 20:39:36 +0000867// Implement isa/cast/dyncast.
868bool SubrangeDesc::classof(const DebugInfoDesc *D) {
869 return D->getTag() == DW_TAG_subrange_type;
870}
871
Jim Laskeyf8913f12006-03-01 17:53:02 +0000872/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
873///
874void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
875 DebugInfoDesc::ApplyToFields(Visitor);
876
877 Visitor->Apply(Lo);
878 Visitor->Apply(Hi);
879}
880
881/// getDescString - Return a string used to compose global names and labels.
882///
883const char *SubrangeDesc::getDescString() const {
884 return "llvm.dbg.subrange";
885}
886
887/// getTypeString - Return a string used to label this descriptor's type.
888///
889const char *SubrangeDesc::getTypeString() const {
890 return "llvm.dbg.subrange.type";
891}
892
893#ifndef NDEBUG
894void SubrangeDesc::dump() {
895 std::cerr << getDescString() << " "
896 << "Tag(" << getTag() << "), "
897 << "Lo(" << Lo << "), "
898 << "Hi(" << Hi << ")\n";
899}
900#endif
901
902//===----------------------------------------------------------------------===//
903
Jim Laskey6a3eb012006-03-01 23:52:37 +0000904EnumeratorDesc::EnumeratorDesc()
905: DebugInfoDesc(DW_TAG_enumerator)
906, Name("")
907, Value(0)
908{}
909
910// Implement isa/cast/dyncast.
911bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
912 return D->getTag() == DW_TAG_enumerator;
913}
914
915/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
916///
917void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
918 DebugInfoDesc::ApplyToFields(Visitor);
919
920 Visitor->Apply(Name);
921 Visitor->Apply(Value);
922}
923
924/// getDescString - Return a string used to compose global names and labels.
925///
926const char *EnumeratorDesc::getDescString() const {
927 return "llvm.dbg.enumerator";
928}
929
930/// getTypeString - Return a string used to label this descriptor's type.
931///
932const char *EnumeratorDesc::getTypeString() const {
933 return "llvm.dbg.enumerator.type";
934}
935
936#ifndef NDEBUG
937void EnumeratorDesc::dump() {
938 std::cerr << getDescString() << " "
939 << "Tag(" << getTag() << "), "
940 << "Name(" << Name << "), "
941 << "Value(" << Value << ")\n";
942}
943#endif
944
945//===----------------------------------------------------------------------===//
946
Jim Laskeyce72b172006-02-11 01:01:30 +0000947GlobalDesc::GlobalDesc(unsigned T)
948: AnchoredDesc(T)
949, Context(0)
950, Name("")
951, TyDesc(NULL)
952, IsStatic(false)
953, IsDefinition(false)
954{}
955
956/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000957///
Jim Laskeyce72b172006-02-11 01:01:30 +0000958void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
959 AnchoredDesc::ApplyToFields(Visitor);
960
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000961 Visitor->Apply(Context);
962 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000963 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000964 Visitor->Apply(IsStatic);
965 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000966}
967
968//===----------------------------------------------------------------------===//
969
970GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000971: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +0000972, Global(NULL)
973{}
974
Jim Laskey9c4447a2006-03-01 20:39:36 +0000975// Implement isa/cast/dyncast.
976bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
977 return D->getTag() == DW_TAG_variable;
978}
979
Jim Laskeyce72b172006-02-11 01:01:30 +0000980/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
981///
982void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
983 GlobalDesc::ApplyToFields(Visitor);
984
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000985 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000986 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000987}
988
Jim Laskeyce72b172006-02-11 01:01:30 +0000989/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000990///
Jim Laskeyce72b172006-02-11 01:01:30 +0000991const char *GlobalVariableDesc::getDescString() const {
992 return "llvm.dbg.global_variable";
993}
994
995/// getTypeString - Return a string used to label this descriptors type.
996///
997const char *GlobalVariableDesc::getTypeString() const {
998 return "llvm.dbg.global_variable.type";
999}
1000
1001/// getAnchorString - Return a string used to label this descriptor's anchor.
1002///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001003const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001004const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001005 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001006}
1007
Jim Laskey86cbdba2006-02-06 15:33:21 +00001008#ifndef NDEBUG
1009void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001010 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001011 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001012 << "Anchor(" << getAnchor() << "), "
1013 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001014 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001015 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1016 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +00001017 << "Global(" << Global << "), "
1018 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001019}
1020#endif
1021
1022//===----------------------------------------------------------------------===//
1023
Jim Laskeyce72b172006-02-11 01:01:30 +00001024SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001025: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001026{}
1027
Jim Laskey9c4447a2006-03-01 20:39:36 +00001028// Implement isa/cast/dyncast.
1029bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1030 return D->getTag() == DW_TAG_subprogram;
1031}
1032
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001033/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001034/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001035void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001036 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001037}
1038
Jim Laskeyce72b172006-02-11 01:01:30 +00001039/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001040///
Jim Laskeyce72b172006-02-11 01:01:30 +00001041const char *SubprogramDesc::getDescString() const {
1042 return "llvm.dbg.subprogram";
1043}
1044
1045/// getTypeString - Return a string used to label this descriptors type.
1046///
1047const char *SubprogramDesc::getTypeString() const {
1048 return "llvm.dbg.subprogram.type";
1049}
1050
1051/// getAnchorString - Return a string used to label this descriptor's anchor.
1052///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001053const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001054const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001055 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001056}
1057
Jim Laskey86cbdba2006-02-06 15:33:21 +00001058#ifndef NDEBUG
1059void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001060 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001061 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001062 << "Anchor(" << getAnchor() << "), "
1063 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001064 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001065 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1066 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001067}
1068#endif
1069
Jim Laskey45ccae52006-02-28 20:15:07 +00001070//===----------------------------------------------------------------------===//
1071
Jim Laskey86cbdba2006-02-06 15:33:21 +00001072DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001073 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001074}
1075DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001076 // Handle NULL.
1077 if (!GV) return NULL;
1078
Jim Laskey86cbdba2006-02-06 15:33:21 +00001079 // Check to see if it has been already deserialized.
1080 DebugInfoDesc *&Slot = GlobalDescs[GV];
1081 if (Slot) return Slot;
1082
1083 // Get the Tag from the global.
1084 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1085
1086 // Get the debug version if a compile unit.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001087 if (Tag == DW_TAG_compile_unit) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001088 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1089 }
1090
1091 // Create an empty instance of the correct sort.
1092 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001093
Jim Laskey21407982006-03-14 18:37:57 +00001094 // If not a user defined descriptor.
1095 if (Slot) {
1096 // Deserialize the fields.
1097 DIDeserializeVisitor DRAM(*this, GV);
1098 DRAM.ApplyToFields(Slot);
1099 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001100
1101 return Slot;
1102}
1103
1104//===----------------------------------------------------------------------===//
1105
1106/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001107///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001108const PointerType *DISerializer::getStrPtrType() {
1109 // If not already defined.
1110 if (!StrPtrTy) {
1111 // Construct the pointer to signed bytes.
1112 StrPtrTy = PointerType::get(Type::SByteTy);
1113 }
1114
1115 return StrPtrTy;
1116}
1117
1118/// getEmptyStructPtrType - Return a "{ }*" type.
1119///
1120const PointerType *DISerializer::getEmptyStructPtrType() {
1121 // If not already defined.
1122 if (!EmptyStructPtrTy) {
1123 // Construct the empty structure type.
1124 const StructType *EmptyStructTy =
1125 StructType::get(std::vector<const Type*>());
1126 // Construct the pointer to empty structure type.
1127 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1128 }
1129
1130 return EmptyStructPtrTy;
1131}
1132
1133/// getTagType - Return the type describing the specified descriptor (via tag.)
1134///
1135const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1136 // Attempt to get the previously defined type.
1137 StructType *&Ty = TagTypes[DD->getTag()];
1138
1139 // If not already defined.
1140 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001141 // Set up fields vector.
1142 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001143 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001144 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001145 GTAM.ApplyToFields(DD);
1146
1147 // Construct structured type.
1148 Ty = StructType::get(Fields);
1149
Jim Laskey86cbdba2006-02-06 15:33:21 +00001150 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001151 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001152 }
1153
1154 return Ty;
1155}
1156
1157/// getString - Construct the string as constant string global.
1158///
Jim Laskeyce72b172006-02-11 01:01:30 +00001159Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001160 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001161 Constant *&Slot = StringCache[String];
1162 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001163 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +00001164 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001165 Constant *ConstStr = ConstantArray::get(String);
1166 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +00001167 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1168 GlobalVariable::InternalLinkage,
1169 ConstStr, "str", M);
Jim Laskey78098112006-03-07 22:00:35 +00001170 StrGV->setSection("llvm.metadata");
Jim Laskeyce72b172006-02-11 01:01:30 +00001171 // Convert to generic string pointer.
1172 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1173 return Slot;
1174
Jim Laskey86cbdba2006-02-06 15:33:21 +00001175}
1176
1177/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1178/// so that it can be serialized to a .bc or .ll file.
1179GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1180 // Check if the DebugInfoDesc is already in the map.
1181 GlobalVariable *&Slot = DescGlobals[DD];
1182
1183 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1184 if (Slot) return Slot;
1185
Jim Laskey86cbdba2006-02-06 15:33:21 +00001186 // Get the type associated with the Tag.
1187 const StructType *Ty = getTagType(DD);
1188
1189 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001190 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1191 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001192 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001193
1194 // Insert new GlobalVariable in DescGlobals map.
1195 Slot = GV;
1196
1197 // Set up elements vector
1198 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001199 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001200 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001201 SRAM.ApplyToFields(DD);
1202
1203 // Set the globals initializer.
1204 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1205
1206 return GV;
1207}
1208
1209//===----------------------------------------------------------------------===//
1210
1211/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1212/// Mark visited otherwise.
1213bool DIVerifier::markVisited(GlobalVariable *GV) {
1214 // Check if the GlobalVariable is already in the Visited set.
1215 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1216
1217 // See if GlobalVariable exists.
1218 bool Exists = VI != Visited.end() && *VI == GV;
1219
1220 // Insert in set.
1221 if (!Exists) Visited.insert(VI, GV);
1222
1223 return Exists;
1224}
1225
1226/// Verify - Return true if the GlobalVariable appears to be a valid
1227/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001228bool DIVerifier::Verify(Value *V) {
1229 return Verify(getGlobalVariable(V));
1230}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001231bool DIVerifier::Verify(GlobalVariable *GV) {
1232 // Check if seen before.
1233 if (markVisited(GV)) return true;
1234
1235 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001236 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001237 if (Tag == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001238
1239 // If a compile unit we need the debug version.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001240 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001241 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001242 if (DebugVersion == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001243 }
1244
1245 // Construct an empty DebugInfoDesc.
1246 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001247
1248 // Allow for user defined descriptors.
1249 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001250
1251 // Get the initializer constant.
1252 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1253
1254 // Get the operand count.
1255 unsigned N = CI->getNumOperands();
1256
1257 // Get the field count.
1258 unsigned &Slot = Counts[Tag];
1259 if (!Slot) {
1260 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001261 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001262 CTAM.ApplyToFields(DD);
1263 Slot = CTAM.getCount();
1264 }
1265
Jim Laskey21407982006-03-14 18:37:57 +00001266 // Field count must be at most equal operand count.
1267 if (Slot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001268 delete DD;
1269 return false;
1270 }
1271
1272 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001273 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001274 VRAM.ApplyToFields(DD);
1275
1276 // Release empty DebugInfoDesc.
1277 delete DD;
1278
1279 // Return result of field tests.
1280 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001281}
1282
1283//===----------------------------------------------------------------------===//
1284
1285
1286MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001287: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001288, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001289, Directories()
1290, SourceFiles()
1291, Lines()
1292{
1293
1294}
1295MachineDebugInfo::~MachineDebugInfo() {
1296
1297}
1298
Jim Laskeyb2efb852006-01-04 22:28:25 +00001299/// doInitialization - Initialize the debug state for a new module.
1300///
1301bool MachineDebugInfo::doInitialization() {
1302 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001303}
1304
Jim Laskeyb2efb852006-01-04 22:28:25 +00001305/// doFinalization - Tear down the debug state after completion of a module.
1306///
1307bool MachineDebugInfo::doFinalization() {
1308 return false;
1309}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001310
Jim Laskeyd96185a2006-02-13 12:50:39 +00001311/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001312///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001313// FIXME - use new Value type when available.
1314DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001315 return DR.Deserialize(V);
1316}
1317
1318/// Verify - Verify that a Value is debug information descriptor.
1319///
1320bool MachineDebugInfo::Verify(Value *V) {
1321 DIVerifier VR;
1322 return VR.Verify(V);
1323}
1324
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001325/// AnalyzeModule - Scan the module for global debug information.
1326///
1327void MachineDebugInfo::AnalyzeModule(Module &M) {
1328 SetupCompileUnits(M);
1329}
1330
1331/// SetupCompileUnits - Set up the unique vector of compile units.
1332///
1333void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001334 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001335
Jim Laskey0420f2a2006-02-22 19:02:11 +00001336 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1337 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001338 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001339}
1340
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001341/// getCompileUnits - Return a vector of debug compile units.
1342///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001343const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001344 return CompileUnits;
1345}
1346
Jim Laskey0420f2a2006-02-22 19:02:11 +00001347/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1348/// named GlobalVariable.
1349std::vector<GlobalVariable*>
1350MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1351 const std::string &RootName) {
1352 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001353}