blob: fa755f1923e9f14eb62a81e8357d33bd6151fbdf [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) {
273 Elements.push_back(SR.getString(Field));
274 }
275 virtual void Apply(DebugInfoDesc *&Field) {
276 GlobalVariable *GV = NULL;
277
278 // If non-NULL the convert to global.
279 if (Field) GV = SR.Serialize(Field);
280
281 // FIXME - At some point should use specific type.
282 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
283
284 if (GV) {
285 // Set to pointer to global.
286 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
287 } else {
288 // Use NULL.
289 Elements.push_back(ConstantPointerNull::get(EmptyTy));
290 }
291 }
292 virtual void Apply(GlobalVariable *&Field) {
293 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000294 if (Field) {
295 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
296 } else {
297 Elements.push_back(ConstantPointerNull::get(EmptyTy));
298 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000299 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000300 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
301 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
302 unsigned N = Field.size();
303 ArrayType *AT = ArrayType::get(EmptyTy, N);
304 std::vector<Constant *> ArrayElements;
305
306 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
307 GlobalVariable *GVE = SR.Serialize(Field[i]);
308 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
309 ArrayElements.push_back(cast<Constant>(CE));
310 }
311
312 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000313 GlobalVariable *CAGV = new GlobalVariable(AT, true,
314 GlobalValue::InternalLinkage,
315 CA, "llvm.dbg.array",
316 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000317 CAGV->setSection("llvm.metadata");
Jim Laskeyf8913f12006-03-01 17:53:02 +0000318 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000319 Elements.push_back(CAE);
320 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000321};
322
323//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000324/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000325/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000326class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000327private:
328 DISerializer &SR; // Active serializer.
329 std::vector<const Type*> &Fields; // Type accumulator.
330
331public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000332 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
333 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000334 , SR(S)
335 , Fields(F)
336 {}
337
338 /// Apply - Set the value of each of the fields.
339 ///
340 virtual void Apply(int &Field) {
341 Fields.push_back(Type::IntTy);
342 }
343 virtual void Apply(unsigned &Field) {
344 Fields.push_back(Type::UIntTy);
345 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000346 virtual void Apply(int64_t &Field) {
347 Fields.push_back(Type::IntTy);
348 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000349 virtual void Apply(uint64_t &Field) {
350 Fields.push_back(Type::UIntTy);
351 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000352 virtual void Apply(bool &Field) {
353 Fields.push_back(Type::BoolTy);
354 }
355 virtual void Apply(std::string &Field) {
356 Fields.push_back(SR.getStrPtrType());
357 }
358 virtual void Apply(DebugInfoDesc *&Field) {
359 // FIXME - At some point should use specific type.
360 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
361 Fields.push_back(EmptyTy);
362 }
363 virtual void Apply(GlobalVariable *&Field) {
364 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
365 Fields.push_back(EmptyTy);
366 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000367 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
368 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
369 Fields.push_back(EmptyTy);
370 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000371};
372
373//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000374/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000375/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000376class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000377private:
378 DIVerifier &VR; // Active verifier.
379 bool IsValid; // Validity status.
380 unsigned I; // Current operand index.
381 ConstantStruct *CI; // GlobalVariable constant initializer.
382
383public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000384 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
385 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000386 , VR(V)
387 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000388 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000389 , CI(cast<ConstantStruct>(GV->getInitializer()))
390 {
391 }
392
393 // Accessors.
394 bool isValid() const { return IsValid; }
395
396 /// Apply - Set the value of each of the fields.
397 ///
398 virtual void Apply(int &Field) {
399 Constant *C = CI->getOperand(I++);
400 IsValid = IsValid && isa<ConstantInt>(C);
401 }
402 virtual void Apply(unsigned &Field) {
403 Constant *C = CI->getOperand(I++);
404 IsValid = IsValid && isa<ConstantInt>(C);
405 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000406 virtual void Apply(int64_t &Field) {
407 Constant *C = CI->getOperand(I++);
408 IsValid = IsValid && isa<ConstantInt>(C);
409 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000410 virtual void Apply(uint64_t &Field) {
411 Constant *C = CI->getOperand(I++);
412 IsValid = IsValid && isa<ConstantInt>(C);
413 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000414 virtual void Apply(bool &Field) {
415 Constant *C = CI->getOperand(I++);
416 IsValid = IsValid && isa<ConstantBool>(C);
417 }
418 virtual void Apply(std::string &Field) {
419 Constant *C = CI->getOperand(I++);
420 IsValid = IsValid && isStringValue(C);
421 }
422 virtual void Apply(DebugInfoDesc *&Field) {
423 // FIXME - Prepare the correct descriptor.
424 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000425 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000426 }
427 virtual void Apply(GlobalVariable *&Field) {
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 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000431 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
432 Constant *C = CI->getOperand(I++);
433 IsValid = IsValid && isGlobalVariable(C);
434 if (!IsValid) return;
435
436 GlobalVariable *GV = getGlobalVariable(C);
437 IsValid = IsValid && GV && GV->hasInitializer();
438 if (!IsValid) return;
439
440 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
441 IsValid = IsValid && CA;
442 if (!IsValid) return;
443
444 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
445 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
446 if (!IsValid) return;
447
448 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
449 VR.Verify(GVE);
450 }
451 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000452};
453
Jim Laskeyce72b172006-02-11 01:01:30 +0000454
Jim Laskey86cbdba2006-02-06 15:33:21 +0000455//===----------------------------------------------------------------------===//
456
Jim Laskeyce72b172006-02-11 01:01:30 +0000457/// TagFromGlobal - Returns the Tag number from a debug info descriptor
458/// GlobalVariable.
459unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
460 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000461 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000462}
463
464/// DescFactory - Create an instance of debug info descriptor based on Tag.
465/// Return NULL if not a recognized Tag.
466DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
467 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000468 case DW_TAG_anchor: return new AnchorDesc();
469 case DW_TAG_compile_unit: return new CompileUnitDesc();
470 case DW_TAG_variable: return new GlobalVariableDesc();
471 case DW_TAG_subprogram: return new SubprogramDesc();
472 case DW_TAG_base_type: return new BasicTypeDesc();
473 case DW_TAG_typedef:
474 case DW_TAG_pointer_type:
475 case DW_TAG_reference_type:
476 case DW_TAG_const_type:
477 case DW_TAG_volatile_type:
478 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
479 case DW_TAG_array_type:
480 case DW_TAG_structure_type:
481 case DW_TAG_union_type:
482 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
483 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskeyf01e5472006-03-03 15:06:57 +0000484 case DW_TAG_member: return new DerivedTypeDesc(DW_TAG_member);
Jim Laskey6a3eb012006-03-01 23:52:37 +0000485 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyce72b172006-02-11 01:01:30 +0000486 default: break;
487 }
488 return NULL;
489}
490
491/// getLinkage - get linkage appropriate for this type of descriptor.
492///
493GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
494 return GlobalValue::InternalLinkage;
495}
496
497/// ApplyToFields - Target the vistor to the fields of the descriptor.
498///
499void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
500 Visitor->Apply(Tag);
501}
502
503//===----------------------------------------------------------------------===//
504
Jim Laskey9c4447a2006-03-01 20:39:36 +0000505AnchorDesc::AnchorDesc()
506: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000507, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000508{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000509AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000510: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000511, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000512{}
513
514// Implement isa/cast/dyncast.
515bool AnchorDesc::classof(const DebugInfoDesc *D) {
516 return D->getTag() == DW_TAG_anchor;
517}
518
Jim Laskeyce72b172006-02-11 01:01:30 +0000519/// getLinkage - get linkage appropriate for this type of descriptor.
520///
521GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
522 return GlobalValue::LinkOnceLinkage;
523}
524
525/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
526///
527void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
528 DebugInfoDesc::ApplyToFields(Visitor);
529
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000530 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000531}
532
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000533/// getDescString - Return a string used to compose global names and labels. A
534/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000535/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000536/// to the list of names left external in the internalizer.
537/// ExternalNames.insert("llvm.dbg.compile_units");
538/// ExternalNames.insert("llvm.dbg.global_variables");
539/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000540const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000541 switch (AnchorTag) {
542 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
543 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
544 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
545 default: break;
546 }
547
548 assert(0 && "Tag does not have a case for anchor string");
549 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000550}
551
552/// getTypeString - Return a string used to label this descriptors type.
553///
554const char *AnchorDesc::getTypeString() const {
555 return "llvm.dbg.anchor.type";
556}
557
558#ifndef NDEBUG
559void AnchorDesc::dump() {
560 std::cerr << getDescString() << " "
561 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000562 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000563}
564#endif
565
566//===----------------------------------------------------------------------===//
567
568AnchoredDesc::AnchoredDesc(unsigned T)
569: DebugInfoDesc(T)
570, Anchor(NULL)
571{}
572
573/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
574///
575void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
576 DebugInfoDesc::ApplyToFields(Visitor);
577
578 Visitor->Apply((DebugInfoDesc *&)Anchor);
579}
580
581//===----------------------------------------------------------------------===//
582
583CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000584: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000585, DebugVersion(LLVMDebugVersion)
586, Language(0)
587, FileName("")
588, Directory("")
589, Producer("")
590{}
591
Jim Laskey9c4447a2006-03-01 20:39:36 +0000592// Implement isa/cast/dyncast.
593bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
594 return D->getTag() == DW_TAG_compile_unit;
595}
596
Jim Laskey86cbdba2006-02-06 15:33:21 +0000597/// DebugVersionFromGlobal - Returns the version number from a compile unit
598/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000599unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000600 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000601 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000602}
603
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000604/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
605///
606void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000607 AnchoredDesc::ApplyToFields(Visitor);
608
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000609 Visitor->Apply(DebugVersion);
610 Visitor->Apply(Language);
611 Visitor->Apply(FileName);
612 Visitor->Apply(Directory);
613 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000614}
615
Jim Laskeyce72b172006-02-11 01:01:30 +0000616/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000617///
Jim Laskeyce72b172006-02-11 01:01:30 +0000618const char *CompileUnitDesc::getDescString() const {
619 return "llvm.dbg.compile_unit";
620}
621
622/// getTypeString - Return a string used to label this descriptors type.
623///
624const char *CompileUnitDesc::getTypeString() const {
625 return "llvm.dbg.compile_unit.type";
626}
627
628/// getAnchorString - Return a string used to label this descriptor's anchor.
629///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000630const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000631const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000632 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000633}
634
Jim Laskey86cbdba2006-02-06 15:33:21 +0000635#ifndef NDEBUG
636void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000637 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000638 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000639 << "Anchor(" << getAnchor() << "), "
640 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000641 << "Language(" << Language << "), "
642 << "FileName(\"" << FileName << "\"), "
643 << "Directory(\"" << Directory << "\"), "
644 << "Producer(\"" << Producer << "\")\n";
645}
646#endif
647
648//===----------------------------------------------------------------------===//
649
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000650TypeDesc::TypeDesc(unsigned T)
651: DebugInfoDesc(T)
652, Context(NULL)
653, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000654, File(NULL)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000655, Size(0)
Jim Laskey54689c22006-03-09 13:28:47 +0000656, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000657, Offset(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000658{}
659
Jim Laskey69906002006-02-24 16:46:40 +0000660/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000661///
662void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
663 DebugInfoDesc::ApplyToFields(Visitor);
664
665 Visitor->Apply(Context);
666 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000667 Visitor->Apply((DebugInfoDesc *&)File);
668 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000669 Visitor->Apply(Size);
Jim Laskey54689c22006-03-09 13:28:47 +0000670 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000671 Visitor->Apply(Offset);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000672}
673
674/// getDescString - Return a string used to compose global names and labels.
675///
676const char *TypeDesc::getDescString() const {
677 return "llvm.dbg.type";
678}
679
680/// getTypeString - Return a string used to label this descriptor's type.
681///
682const char *TypeDesc::getTypeString() const {
683 return "llvm.dbg.type.type";
684}
685
686#ifndef NDEBUG
687void TypeDesc::dump() {
688 std::cerr << getDescString() << " "
689 << "Tag(" << getTag() << "), "
690 << "Context(" << Context << "), "
691 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000692 << "File(" << File << "), "
693 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000694 << "Size(" << Size << "), "
Jim Laskey54689c22006-03-09 13:28:47 +0000695 << "Align(" << Align << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000696 << "Offset(" << Offset << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000697}
698#endif
699
700//===----------------------------------------------------------------------===//
701
702BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000703: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000704, Encoding(0)
705{}
706
Jim Laskey9c4447a2006-03-01 20:39:36 +0000707// Implement isa/cast/dyncast.
708bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
709 return D->getTag() == DW_TAG_base_type;
710}
711
Jim Laskey69906002006-02-24 16:46:40 +0000712/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000713///
714void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
715 TypeDesc::ApplyToFields(Visitor);
716
717 Visitor->Apply(Encoding);
718}
719
Jim Laskeyf8913f12006-03-01 17:53:02 +0000720/// getDescString - Return a string used to compose global names and labels.
721///
722const char *BasicTypeDesc::getDescString() const {
723 return "llvm.dbg.basictype";
724}
725
726/// getTypeString - Return a string used to label this descriptor's type.
727///
728const char *BasicTypeDesc::getTypeString() const {
729 return "llvm.dbg.basictype.type";
730}
731
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000732#ifndef NDEBUG
733void BasicTypeDesc::dump() {
734 std::cerr << getDescString() << " "
735 << "Tag(" << getTag() << "), "
736 << "Context(" << getContext() << "), "
737 << "Name(\"" << getName() << "\"), "
738 << "Size(" << getSize() << "), "
739 << "Encoding(" << Encoding << ")\n";
740}
741#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000742
Jim Laskey434b40b2006-02-23 22:37:30 +0000743//===----------------------------------------------------------------------===//
744
Jim Laskey69906002006-02-24 16:46:40 +0000745DerivedTypeDesc::DerivedTypeDesc(unsigned T)
746: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000747, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000748{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000749
Jim Laskey9c4447a2006-03-01 20:39:36 +0000750// Implement isa/cast/dyncast.
751bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
752 unsigned T = D->getTag();
753 switch (T) {
754 case DW_TAG_typedef:
755 case DW_TAG_pointer_type:
756 case DW_TAG_reference_type:
757 case DW_TAG_const_type:
758 case DW_TAG_volatile_type:
759 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000760 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000761 return true;
762 default: break;
763 }
764 return false;
765}
766
Jim Laskey69906002006-02-24 16:46:40 +0000767/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000768///
Jim Laskey69906002006-02-24 16:46:40 +0000769void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000770 TypeDesc::ApplyToFields(Visitor);
771
772 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000773}
774
Jim Laskeyf8913f12006-03-01 17:53:02 +0000775/// getDescString - Return a string used to compose global names and labels.
776///
777const char *DerivedTypeDesc::getDescString() const {
778 return "llvm.dbg.derivedtype";
779}
780
781/// getTypeString - Return a string used to label this descriptor's type.
782///
783const char *DerivedTypeDesc::getTypeString() const {
784 return "llvm.dbg.derivedtype.type";
785}
786
Jim Laskey434b40b2006-02-23 22:37:30 +0000787#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000788void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000789 std::cerr << getDescString() << " "
790 << "Tag(" << getTag() << "), "
791 << "Context(" << getContext() << "), "
792 << "Name(\"" << getName() << "\"), "
793 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000794 << "File(" << getFile() << "), "
795 << "Line(" << getLine() << "), "
796 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000797}
798#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000799
800//===----------------------------------------------------------------------===//
801
Jim Laskeyf8913f12006-03-01 17:53:02 +0000802CompositeTypeDesc::CompositeTypeDesc(unsigned T)
803: DerivedTypeDesc(T)
804, Elements()
805{}
806
Jim Laskey9c4447a2006-03-01 20:39:36 +0000807// Implement isa/cast/dyncast.
808bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
809 unsigned T = D->getTag();
810 switch (T) {
811 case DW_TAG_array_type:
812 case DW_TAG_structure_type:
813 case DW_TAG_union_type:
814 case DW_TAG_enumeration_type:
815 return true;
816 default: break;
817 }
818 return false;
819}
820
Jim Laskeyf8913f12006-03-01 17:53:02 +0000821/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
822///
823void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
824 DerivedTypeDesc::ApplyToFields(Visitor);
825
826 Visitor->Apply(Elements);
827}
828
829/// getDescString - Return a string used to compose global names and labels.
830///
831const char *CompositeTypeDesc::getDescString() const {
832 return "llvm.dbg.compositetype";
833}
834
835/// getTypeString - Return a string used to label this descriptor's type.
836///
837const char *CompositeTypeDesc::getTypeString() const {
838 return "llvm.dbg.compositetype.type";
839}
840
841#ifndef NDEBUG
842void CompositeTypeDesc::dump() {
843 std::cerr << getDescString() << " "
844 << "Tag(" << getTag() << "), "
845 << "Context(" << getContext() << "), "
846 << "Name(\"" << getName() << "\"), "
847 << "Size(" << getSize() << "), "
848 << "File(" << getFile() << "), "
849 << "Line(" << getLine() << "), "
850 << "FromType(" << getFromType() << "), "
851 << "Elements.size(" << Elements.size() << ")\n";
852}
853#endif
854
855//===----------------------------------------------------------------------===//
856
857SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000858: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000859, Lo(0)
860, Hi(0)
861{}
862
Jim Laskey9c4447a2006-03-01 20:39:36 +0000863// Implement isa/cast/dyncast.
864bool SubrangeDesc::classof(const DebugInfoDesc *D) {
865 return D->getTag() == DW_TAG_subrange_type;
866}
867
Jim Laskeyf8913f12006-03-01 17:53:02 +0000868/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
869///
870void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
871 DebugInfoDesc::ApplyToFields(Visitor);
872
873 Visitor->Apply(Lo);
874 Visitor->Apply(Hi);
875}
876
877/// getDescString - Return a string used to compose global names and labels.
878///
879const char *SubrangeDesc::getDescString() const {
880 return "llvm.dbg.subrange";
881}
882
883/// getTypeString - Return a string used to label this descriptor's type.
884///
885const char *SubrangeDesc::getTypeString() const {
886 return "llvm.dbg.subrange.type";
887}
888
889#ifndef NDEBUG
890void SubrangeDesc::dump() {
891 std::cerr << getDescString() << " "
892 << "Tag(" << getTag() << "), "
893 << "Lo(" << Lo << "), "
894 << "Hi(" << Hi << ")\n";
895}
896#endif
897
898//===----------------------------------------------------------------------===//
899
Jim Laskey6a3eb012006-03-01 23:52:37 +0000900EnumeratorDesc::EnumeratorDesc()
901: DebugInfoDesc(DW_TAG_enumerator)
902, Name("")
903, Value(0)
904{}
905
906// Implement isa/cast/dyncast.
907bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
908 return D->getTag() == DW_TAG_enumerator;
909}
910
911/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
912///
913void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
914 DebugInfoDesc::ApplyToFields(Visitor);
915
916 Visitor->Apply(Name);
917 Visitor->Apply(Value);
918}
919
920/// getDescString - Return a string used to compose global names and labels.
921///
922const char *EnumeratorDesc::getDescString() const {
923 return "llvm.dbg.enumerator";
924}
925
926/// getTypeString - Return a string used to label this descriptor's type.
927///
928const char *EnumeratorDesc::getTypeString() const {
929 return "llvm.dbg.enumerator.type";
930}
931
932#ifndef NDEBUG
933void EnumeratorDesc::dump() {
934 std::cerr << getDescString() << " "
935 << "Tag(" << getTag() << "), "
936 << "Name(" << Name << "), "
937 << "Value(" << Value << ")\n";
938}
939#endif
940
941//===----------------------------------------------------------------------===//
942
Jim Laskeyce72b172006-02-11 01:01:30 +0000943GlobalDesc::GlobalDesc(unsigned T)
944: AnchoredDesc(T)
945, Context(0)
946, Name("")
947, TyDesc(NULL)
948, IsStatic(false)
949, IsDefinition(false)
950{}
951
952/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000953///
Jim Laskeyce72b172006-02-11 01:01:30 +0000954void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
955 AnchoredDesc::ApplyToFields(Visitor);
956
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000957 Visitor->Apply(Context);
958 Visitor->Apply(Name);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000959 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000960 Visitor->Apply(IsStatic);
961 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +0000962}
963
964//===----------------------------------------------------------------------===//
965
966GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000967: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +0000968, Global(NULL)
969{}
970
Jim Laskey9c4447a2006-03-01 20:39:36 +0000971// Implement isa/cast/dyncast.
972bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
973 return D->getTag() == DW_TAG_variable;
974}
975
Jim Laskeyce72b172006-02-11 01:01:30 +0000976/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
977///
978void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
979 GlobalDesc::ApplyToFields(Visitor);
980
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000981 Visitor->Apply(Global);
Jim Laskey0420f2a2006-02-22 19:02:11 +0000982 Visitor->Apply(Line);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000983}
984
Jim Laskeyce72b172006-02-11 01:01:30 +0000985/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000986///
Jim Laskeyce72b172006-02-11 01:01:30 +0000987const char *GlobalVariableDesc::getDescString() const {
988 return "llvm.dbg.global_variable";
989}
990
991/// getTypeString - Return a string used to label this descriptors type.
992///
993const char *GlobalVariableDesc::getTypeString() const {
994 return "llvm.dbg.global_variable.type";
995}
996
997/// getAnchorString - Return a string used to label this descriptor's anchor.
998///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000999const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001000const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001001 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001002}
1003
Jim Laskey86cbdba2006-02-06 15:33:21 +00001004#ifndef NDEBUG
1005void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001006 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001007 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001008 << "Anchor(" << getAnchor() << "), "
1009 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001010 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001011 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1012 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey0420f2a2006-02-22 19:02:11 +00001013 << "Global(" << Global << "), "
1014 << "Line(" << Line << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001015}
1016#endif
1017
1018//===----------------------------------------------------------------------===//
1019
Jim Laskeyce72b172006-02-11 01:01:30 +00001020SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001021: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001022{}
1023
Jim Laskey9c4447a2006-03-01 20:39:36 +00001024// Implement isa/cast/dyncast.
1025bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1026 return D->getTag() == DW_TAG_subprogram;
1027}
1028
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001029/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001030/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001031void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001032 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001033}
1034
Jim Laskeyce72b172006-02-11 01:01:30 +00001035/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001036///
Jim Laskeyce72b172006-02-11 01:01:30 +00001037const char *SubprogramDesc::getDescString() const {
1038 return "llvm.dbg.subprogram";
1039}
1040
1041/// getTypeString - Return a string used to label this descriptors type.
1042///
1043const char *SubprogramDesc::getTypeString() const {
1044 return "llvm.dbg.subprogram.type";
1045}
1046
1047/// getAnchorString - Return a string used to label this descriptor's anchor.
1048///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001049const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001050const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001051 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001052}
1053
Jim Laskey86cbdba2006-02-06 15:33:21 +00001054#ifndef NDEBUG
1055void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001056 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001057 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001058 << "Anchor(" << getAnchor() << "), "
1059 << "Name(\"" << getName() << "\"), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001060 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001061 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1062 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001063}
1064#endif
1065
Jim Laskey45ccae52006-02-28 20:15:07 +00001066//===----------------------------------------------------------------------===//
1067
Jim Laskey86cbdba2006-02-06 15:33:21 +00001068DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001069 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001070}
1071DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001072 // Handle NULL.
1073 if (!GV) return NULL;
1074
Jim Laskey86cbdba2006-02-06 15:33:21 +00001075 // Check to see if it has been already deserialized.
1076 DebugInfoDesc *&Slot = GlobalDescs[GV];
1077 if (Slot) return Slot;
1078
1079 // Get the Tag from the global.
1080 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1081
1082 // Get the debug version if a compile unit.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001083 if (Tag == DW_TAG_compile_unit) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001084 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1085 }
1086
1087 // Create an empty instance of the correct sort.
1088 Slot = DebugInfoDesc::DescFactory(Tag);
1089 assert(Slot && "Unknown Tag");
1090
1091 // Deserialize the fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001092 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001093 DRAM.ApplyToFields(Slot);
1094
1095 return Slot;
1096}
1097
1098//===----------------------------------------------------------------------===//
1099
1100/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001101///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001102const PointerType *DISerializer::getStrPtrType() {
1103 // If not already defined.
1104 if (!StrPtrTy) {
1105 // Construct the pointer to signed bytes.
1106 StrPtrTy = PointerType::get(Type::SByteTy);
1107 }
1108
1109 return StrPtrTy;
1110}
1111
1112/// getEmptyStructPtrType - Return a "{ }*" type.
1113///
1114const PointerType *DISerializer::getEmptyStructPtrType() {
1115 // If not already defined.
1116 if (!EmptyStructPtrTy) {
1117 // Construct the empty structure type.
1118 const StructType *EmptyStructTy =
1119 StructType::get(std::vector<const Type*>());
1120 // Construct the pointer to empty structure type.
1121 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1122 }
1123
1124 return EmptyStructPtrTy;
1125}
1126
1127/// getTagType - Return the type describing the specified descriptor (via tag.)
1128///
1129const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1130 // Attempt to get the previously defined type.
1131 StructType *&Ty = TagTypes[DD->getTag()];
1132
1133 // If not already defined.
1134 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001135 // Set up fields vector.
1136 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001137 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001138 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001139 GTAM.ApplyToFields(DD);
1140
1141 // Construct structured type.
1142 Ty = StructType::get(Fields);
1143
Jim Laskey86cbdba2006-02-06 15:33:21 +00001144 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001145 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001146 }
1147
1148 return Ty;
1149}
1150
1151/// getString - Construct the string as constant string global.
1152///
Jim Laskeyce72b172006-02-11 01:01:30 +00001153Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001154 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001155 Constant *&Slot = StringCache[String];
1156 // return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001157 if (Slot) return Slot;
Jim Laskeyce72b172006-02-11 01:01:30 +00001158 // Construct string as an llvm constant.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001159 Constant *ConstStr = ConstantArray::get(String);
1160 // Otherwise create and return a new string global.
Jim Laskeyce72b172006-02-11 01:01:30 +00001161 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1162 GlobalVariable::InternalLinkage,
1163 ConstStr, "str", M);
Jim Laskey78098112006-03-07 22:00:35 +00001164 StrGV->setSection("llvm.metadata");
Jim Laskeyce72b172006-02-11 01:01:30 +00001165 // Convert to generic string pointer.
1166 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1167 return Slot;
1168
Jim Laskey86cbdba2006-02-06 15:33:21 +00001169}
1170
1171/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1172/// so that it can be serialized to a .bc or .ll file.
1173GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1174 // Check if the DebugInfoDesc is already in the map.
1175 GlobalVariable *&Slot = DescGlobals[DD];
1176
1177 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1178 if (Slot) return Slot;
1179
Jim Laskey86cbdba2006-02-06 15:33:21 +00001180 // Get the type associated with the Tag.
1181 const StructType *Ty = getTagType(DD);
1182
1183 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001184 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1185 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001186 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001187
1188 // Insert new GlobalVariable in DescGlobals map.
1189 Slot = GV;
1190
1191 // Set up elements vector
1192 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001193 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001194 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001195 SRAM.ApplyToFields(DD);
1196
1197 // Set the globals initializer.
1198 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1199
1200 return GV;
1201}
1202
1203//===----------------------------------------------------------------------===//
1204
1205/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1206/// Mark visited otherwise.
1207bool DIVerifier::markVisited(GlobalVariable *GV) {
1208 // Check if the GlobalVariable is already in the Visited set.
1209 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1210
1211 // See if GlobalVariable exists.
1212 bool Exists = VI != Visited.end() && *VI == GV;
1213
1214 // Insert in set.
1215 if (!Exists) Visited.insert(VI, GV);
1216
1217 return Exists;
1218}
1219
1220/// Verify - Return true if the GlobalVariable appears to be a valid
1221/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001222bool DIVerifier::Verify(Value *V) {
1223 return Verify(getGlobalVariable(V));
1224}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001225bool DIVerifier::Verify(GlobalVariable *GV) {
1226 // Check if seen before.
1227 if (markVisited(GV)) return true;
1228
1229 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001230 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001231 if (Tag == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001232
1233 // If a compile unit we need the debug version.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001234 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001235 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskey9c4447a2006-03-01 20:39:36 +00001236 if (DebugVersion == DW_TAG_invalid) return false;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001237 }
1238
1239 // Construct an empty DebugInfoDesc.
1240 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1241 if (!DD) return false;
1242
1243 // Get the initializer constant.
1244 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1245
1246 // Get the operand count.
1247 unsigned N = CI->getNumOperands();
1248
1249 // Get the field count.
1250 unsigned &Slot = Counts[Tag];
1251 if (!Slot) {
1252 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001253 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001254 CTAM.ApplyToFields(DD);
1255 Slot = CTAM.getCount();
1256 }
1257
1258 // Field count must equal operand count.
1259 if (Slot != N) {
1260 delete DD;
1261 return false;
1262 }
1263
1264 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001265 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001266 VRAM.ApplyToFields(DD);
1267
1268 // Release empty DebugInfoDesc.
1269 delete DD;
1270
1271 // Return result of field tests.
1272 return VRAM.isValid();
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001273}
1274
1275//===----------------------------------------------------------------------===//
1276
1277
1278MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001279: DR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001280, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001281, Directories()
1282, SourceFiles()
1283, Lines()
1284{
1285
1286}
1287MachineDebugInfo::~MachineDebugInfo() {
1288
1289}
1290
Jim Laskeyb2efb852006-01-04 22:28:25 +00001291/// doInitialization - Initialize the debug state for a new module.
1292///
1293bool MachineDebugInfo::doInitialization() {
1294 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001295}
1296
Jim Laskeyb2efb852006-01-04 22:28:25 +00001297/// doFinalization - Tear down the debug state after completion of a module.
1298///
1299bool MachineDebugInfo::doFinalization() {
1300 return false;
1301}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001302
Jim Laskeyd96185a2006-02-13 12:50:39 +00001303/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001304///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001305// FIXME - use new Value type when available.
1306DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001307 return DR.Deserialize(V);
1308}
1309
1310/// Verify - Verify that a Value is debug information descriptor.
1311///
1312bool MachineDebugInfo::Verify(Value *V) {
1313 DIVerifier VR;
1314 return VR.Verify(V);
1315}
1316
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001317/// AnalyzeModule - Scan the module for global debug information.
1318///
1319void MachineDebugInfo::AnalyzeModule(Module &M) {
1320 SetupCompileUnits(M);
1321}
1322
1323/// SetupCompileUnits - Set up the unique vector of compile units.
1324///
1325void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001326 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001327
Jim Laskey0420f2a2006-02-22 19:02:11 +00001328 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1329 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001330 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001331}
1332
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001333/// getCompileUnits - Return a vector of debug compile units.
1334///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001335const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001336 return CompileUnits;
1337}
1338
Jim Laskey0420f2a2006-02-22 19:02:11 +00001339/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1340/// named GlobalVariable.
1341std::vector<GlobalVariable*>
1342MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1343 const std::string &RootName) {
1344 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001345}