blob: 9876e652a830750a703c05364466175855c634b2 [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 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000274 }
275 virtual void Apply(DebugInfoDesc *&Field) {
276 GlobalVariable *GV = NULL;
277
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000278 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000279 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++);
Jim Laskey21407982006-03-14 18:37:57 +0000420 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000421 }
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();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000472 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000473 case DW_TAG_base_type: return new BasicTypeDesc();
474 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000475 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000476 case DW_TAG_reference_type:
477 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000478 case DW_TAG_volatile_type:
479 case DW_TAG_restrict_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000480 case DW_TAG_member: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000481 case DW_TAG_array_type:
482 case DW_TAG_structure_type:
483 case DW_TAG_union_type:
484 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
485 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000486 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000487 case DW_TAG_return_variable:
488 case DW_TAG_arg_variable:
489 case DW_TAG_auto_variable: return new VariableDesc(Tag);
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 Laskeyb8509c52006-03-23 18:07:55 +0000659, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000660, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000661, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000662, Offset(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000663{}
664
Jim Laskey69906002006-02-24 16:46:40 +0000665/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000666///
667void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
668 DebugInfoDesc::ApplyToFields(Visitor);
669
670 Visitor->Apply(Context);
671 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000672 Visitor->Apply((DebugInfoDesc *&)File);
673 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000674 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000675 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000676 Visitor->Apply(Offset);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000677}
678
679/// getDescString - Return a string used to compose global names and labels.
680///
681const char *TypeDesc::getDescString() const {
682 return "llvm.dbg.type";
683}
684
685/// getTypeString - Return a string used to label this descriptor's type.
686///
687const char *TypeDesc::getTypeString() const {
688 return "llvm.dbg.type.type";
689}
690
691#ifndef NDEBUG
692void TypeDesc::dump() {
693 std::cerr << getDescString() << " "
694 << "Tag(" << getTag() << "), "
695 << "Context(" << Context << "), "
696 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000697 << "File(" << File << "), "
698 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000699 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000700 << "Align(" << Align << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000701 << "Offset(" << Offset << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000702}
703#endif
704
705//===----------------------------------------------------------------------===//
706
707BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000708: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000709, Encoding(0)
710{}
711
Jim Laskey9c4447a2006-03-01 20:39:36 +0000712// Implement isa/cast/dyncast.
713bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
714 return D->getTag() == DW_TAG_base_type;
715}
716
Jim Laskey69906002006-02-24 16:46:40 +0000717/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000718///
719void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
720 TypeDesc::ApplyToFields(Visitor);
721
722 Visitor->Apply(Encoding);
723}
724
Jim Laskeyf8913f12006-03-01 17:53:02 +0000725/// getDescString - Return a string used to compose global names and labels.
726///
727const char *BasicTypeDesc::getDescString() const {
728 return "llvm.dbg.basictype";
729}
730
731/// getTypeString - Return a string used to label this descriptor's type.
732///
733const char *BasicTypeDesc::getTypeString() const {
734 return "llvm.dbg.basictype.type";
735}
736
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000737#ifndef NDEBUG
738void BasicTypeDesc::dump() {
739 std::cerr << getDescString() << " "
740 << "Tag(" << getTag() << "), "
741 << "Context(" << getContext() << "), "
742 << "Name(\"" << getName() << "\"), "
743 << "Size(" << getSize() << "), "
744 << "Encoding(" << Encoding << ")\n";
745}
746#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000747
Jim Laskey434b40b2006-02-23 22:37:30 +0000748//===----------------------------------------------------------------------===//
749
Jim Laskey69906002006-02-24 16:46:40 +0000750DerivedTypeDesc::DerivedTypeDesc(unsigned T)
751: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000752, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000753{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000754
Jim Laskey9c4447a2006-03-01 20:39:36 +0000755// Implement isa/cast/dyncast.
756bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
757 unsigned T = D->getTag();
758 switch (T) {
759 case DW_TAG_typedef:
760 case DW_TAG_pointer_type:
761 case DW_TAG_reference_type:
762 case DW_TAG_const_type:
763 case DW_TAG_volatile_type:
764 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000765 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000766 return true;
767 default: break;
768 }
769 return false;
770}
771
Jim Laskey69906002006-02-24 16:46:40 +0000772/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000773///
Jim Laskey69906002006-02-24 16:46:40 +0000774void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000775 TypeDesc::ApplyToFields(Visitor);
776
777 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000778}
779
Jim Laskeyf8913f12006-03-01 17:53:02 +0000780/// getDescString - Return a string used to compose global names and labels.
781///
782const char *DerivedTypeDesc::getDescString() const {
783 return "llvm.dbg.derivedtype";
784}
785
786/// getTypeString - Return a string used to label this descriptor's type.
787///
788const char *DerivedTypeDesc::getTypeString() const {
789 return "llvm.dbg.derivedtype.type";
790}
791
Jim Laskey434b40b2006-02-23 22:37:30 +0000792#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000793void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000794 std::cerr << getDescString() << " "
795 << "Tag(" << getTag() << "), "
796 << "Context(" << getContext() << "), "
797 << "Name(\"" << getName() << "\"), "
798 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000799 << "File(" << getFile() << "), "
800 << "Line(" << getLine() << "), "
801 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000802}
803#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000804
805//===----------------------------------------------------------------------===//
806
Jim Laskeyf8913f12006-03-01 17:53:02 +0000807CompositeTypeDesc::CompositeTypeDesc(unsigned T)
808: DerivedTypeDesc(T)
809, Elements()
810{}
811
Jim Laskey9c4447a2006-03-01 20:39:36 +0000812// Implement isa/cast/dyncast.
813bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
814 unsigned T = D->getTag();
815 switch (T) {
816 case DW_TAG_array_type:
817 case DW_TAG_structure_type:
818 case DW_TAG_union_type:
819 case DW_TAG_enumeration_type:
820 return true;
821 default: break;
822 }
823 return false;
824}
825
Jim Laskeyf8913f12006-03-01 17:53:02 +0000826/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
827///
828void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
829 DerivedTypeDesc::ApplyToFields(Visitor);
830
831 Visitor->Apply(Elements);
832}
833
834/// getDescString - Return a string used to compose global names and labels.
835///
836const char *CompositeTypeDesc::getDescString() const {
837 return "llvm.dbg.compositetype";
838}
839
840/// getTypeString - Return a string used to label this descriptor's type.
841///
842const char *CompositeTypeDesc::getTypeString() const {
843 return "llvm.dbg.compositetype.type";
844}
845
846#ifndef NDEBUG
847void CompositeTypeDesc::dump() {
848 std::cerr << getDescString() << " "
849 << "Tag(" << getTag() << "), "
850 << "Context(" << getContext() << "), "
851 << "Name(\"" << getName() << "\"), "
852 << "Size(" << getSize() << "), "
853 << "File(" << getFile() << "), "
854 << "Line(" << getLine() << "), "
855 << "FromType(" << getFromType() << "), "
856 << "Elements.size(" << Elements.size() << ")\n";
857}
858#endif
859
860//===----------------------------------------------------------------------===//
861
862SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000863: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000864, Lo(0)
865, Hi(0)
866{}
867
Jim Laskey9c4447a2006-03-01 20:39:36 +0000868// Implement isa/cast/dyncast.
869bool SubrangeDesc::classof(const DebugInfoDesc *D) {
870 return D->getTag() == DW_TAG_subrange_type;
871}
872
Jim Laskeyf8913f12006-03-01 17:53:02 +0000873/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
874///
875void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
876 DebugInfoDesc::ApplyToFields(Visitor);
877
878 Visitor->Apply(Lo);
879 Visitor->Apply(Hi);
880}
881
882/// getDescString - Return a string used to compose global names and labels.
883///
884const char *SubrangeDesc::getDescString() const {
885 return "llvm.dbg.subrange";
886}
887
888/// getTypeString - Return a string used to label this descriptor's type.
889///
890const char *SubrangeDesc::getTypeString() const {
891 return "llvm.dbg.subrange.type";
892}
893
894#ifndef NDEBUG
895void SubrangeDesc::dump() {
896 std::cerr << getDescString() << " "
897 << "Tag(" << getTag() << "), "
898 << "Lo(" << Lo << "), "
899 << "Hi(" << Hi << ")\n";
900}
901#endif
902
903//===----------------------------------------------------------------------===//
904
Jim Laskey6a3eb012006-03-01 23:52:37 +0000905EnumeratorDesc::EnumeratorDesc()
906: DebugInfoDesc(DW_TAG_enumerator)
907, Name("")
908, Value(0)
909{}
910
911// Implement isa/cast/dyncast.
912bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
913 return D->getTag() == DW_TAG_enumerator;
914}
915
916/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
917///
918void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
919 DebugInfoDesc::ApplyToFields(Visitor);
920
921 Visitor->Apply(Name);
922 Visitor->Apply(Value);
923}
924
925/// getDescString - Return a string used to compose global names and labels.
926///
927const char *EnumeratorDesc::getDescString() const {
928 return "llvm.dbg.enumerator";
929}
930
931/// getTypeString - Return a string used to label this descriptor's type.
932///
933const char *EnumeratorDesc::getTypeString() const {
934 return "llvm.dbg.enumerator.type";
935}
936
937#ifndef NDEBUG
938void EnumeratorDesc::dump() {
939 std::cerr << getDescString() << " "
940 << "Tag(" << getTag() << "), "
941 << "Name(" << Name << "), "
942 << "Value(" << Value << ")\n";
943}
944#endif
945
946//===----------------------------------------------------------------------===//
947
Jim Laskeyb8509c52006-03-23 18:07:55 +0000948VariableDesc::VariableDesc(unsigned T)
949: DebugInfoDesc(T)
950, Context(NULL)
951, Name("")
952, File(NULL)
953, Line(0)
954, TyDesc(0)
955{}
956
957// Implement isa/cast/dyncast.
958bool VariableDesc::classof(const DebugInfoDesc *D) {
959 unsigned T = D->getTag();
960 switch (T) {
961 case DW_TAG_auto_variable:
962 case DW_TAG_arg_variable:
963 case DW_TAG_return_variable:
964 return true;
965 default: break;
966 }
967 return false;
968}
969
970/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
971///
972void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
973 DebugInfoDesc::ApplyToFields(Visitor);
974
975 Visitor->Apply(Context);
976 Visitor->Apply(Name);
977 Visitor->Apply((DebugInfoDesc *&)File);
978 Visitor->Apply(Line);
979 Visitor->Apply((DebugInfoDesc *&)TyDesc);
980}
981
982/// getDescString - Return a string used to compose global names and labels.
983///
984const char *VariableDesc::getDescString() const {
985 return "llvm.dbg.variable";
986}
987
988/// getTypeString - Return a string used to label this descriptor's type.
989///
990const char *VariableDesc::getTypeString() const {
991 return "llvm.dbg.variable.type";
992}
993
994#ifndef NDEBUG
995void VariableDesc::dump() {
996 std::cerr << getDescString() << " "
997 << "Tag(" << getTag() << "), "
998 << "Context(" << Context << "), "
999 << "Name(\"" << Name << "\"), "
1000 << "File(" << File << "), "
1001 << "Line(" << Line << "), "
1002 << "TyDesc(" << TyDesc << ")\n";
1003}
1004#endif
1005
1006//===----------------------------------------------------------------------===//
1007
Jim Laskeyce72b172006-02-11 01:01:30 +00001008GlobalDesc::GlobalDesc(unsigned T)
1009: AnchoredDesc(T)
1010, Context(0)
1011, Name("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001012, File(NULL)
1013, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001014, TyDesc(NULL)
1015, IsStatic(false)
1016, IsDefinition(false)
1017{}
1018
1019/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001020///
Jim Laskeyce72b172006-02-11 01:01:30 +00001021void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1022 AnchoredDesc::ApplyToFields(Visitor);
1023
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001024 Visitor->Apply(Context);
1025 Visitor->Apply(Name);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001026 Visitor->Apply((DebugInfoDesc *&)File);
1027 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001028 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001029 Visitor->Apply(IsStatic);
1030 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001031}
1032
1033//===----------------------------------------------------------------------===//
1034
1035GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001036: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001037, Global(NULL)
1038{}
1039
Jim Laskey9c4447a2006-03-01 20:39:36 +00001040// Implement isa/cast/dyncast.
1041bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1042 return D->getTag() == DW_TAG_variable;
1043}
1044
Jim Laskeyce72b172006-02-11 01:01:30 +00001045/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1046///
1047void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1048 GlobalDesc::ApplyToFields(Visitor);
1049
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001050 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001051}
1052
Jim Laskeyce72b172006-02-11 01:01:30 +00001053/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001054///
Jim Laskeyce72b172006-02-11 01:01:30 +00001055const char *GlobalVariableDesc::getDescString() const {
1056 return "llvm.dbg.global_variable";
1057}
1058
1059/// getTypeString - Return a string used to label this descriptors type.
1060///
1061const char *GlobalVariableDesc::getTypeString() const {
1062 return "llvm.dbg.global_variable.type";
1063}
1064
1065/// getAnchorString - Return a string used to label this descriptor's anchor.
1066///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001067const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001068const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001069 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001070}
1071
Jim Laskey86cbdba2006-02-06 15:33:21 +00001072#ifndef NDEBUG
1073void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001074 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001075 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001076 << "Anchor(" << getAnchor() << "), "
1077 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001078 << "File(" << getFile() << "),"
1079 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001080 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001081 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1082 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001083 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001084}
1085#endif
1086
1087//===----------------------------------------------------------------------===//
1088
Jim Laskeyce72b172006-02-11 01:01:30 +00001089SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001090: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001091{}
1092
Jim Laskey9c4447a2006-03-01 20:39:36 +00001093// Implement isa/cast/dyncast.
1094bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1095 return D->getTag() == DW_TAG_subprogram;
1096}
1097
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001098/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001099/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001100void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001101 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001102}
1103
Jim Laskeyce72b172006-02-11 01:01:30 +00001104/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001105///
Jim Laskeyce72b172006-02-11 01:01:30 +00001106const char *SubprogramDesc::getDescString() const {
1107 return "llvm.dbg.subprogram";
1108}
1109
1110/// getTypeString - Return a string used to label this descriptors type.
1111///
1112const char *SubprogramDesc::getTypeString() const {
1113 return "llvm.dbg.subprogram.type";
1114}
1115
1116/// getAnchorString - Return a string used to label this descriptor's anchor.
1117///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001118const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001119const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001120 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001121}
1122
Jim Laskey86cbdba2006-02-06 15:33:21 +00001123#ifndef NDEBUG
1124void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001125 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001126 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001127 << "Anchor(" << getAnchor() << "), "
1128 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001129 << "File(" << getFile() << "),"
1130 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001131 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001132 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1133 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001134}
1135#endif
1136
Jim Laskey45ccae52006-02-28 20:15:07 +00001137//===----------------------------------------------------------------------===//
1138
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001139BlockDesc::BlockDesc()
1140: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001141, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001142{}
1143
1144// Implement isa/cast/dyncast.
1145bool BlockDesc::classof(const DebugInfoDesc *D) {
1146 return D->getTag() == DW_TAG_lexical_block;
1147}
1148
1149/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1150///
1151void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1152 DebugInfoDesc::ApplyToFields(Visitor);
1153
Jim Laskeyb8509c52006-03-23 18:07:55 +00001154 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001155}
1156
1157/// getDescString - Return a string used to compose global names and labels.
1158///
1159const char *BlockDesc::getDescString() const {
1160 return "llvm.dbg.block";
1161}
1162
1163/// getTypeString - Return a string used to label this descriptors type.
1164///
1165const char *BlockDesc::getTypeString() const {
1166 return "llvm.dbg.block.type";
1167}
1168
1169#ifndef NDEBUG
1170void BlockDesc::dump() {
1171 std::cerr << getDescString() << " "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001172 << "Tag(" << getTag() << "),"
1173 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001174}
1175#endif
1176
1177//===----------------------------------------------------------------------===//
1178
Jim Laskey86cbdba2006-02-06 15:33:21 +00001179DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001180 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001181}
1182DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001183 // Handle NULL.
1184 if (!GV) return NULL;
1185
Jim Laskey86cbdba2006-02-06 15:33:21 +00001186 // Check to see if it has been already deserialized.
1187 DebugInfoDesc *&Slot = GlobalDescs[GV];
1188 if (Slot) return Slot;
1189
1190 // Get the Tag from the global.
1191 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1192
1193 // Get the debug version if a compile unit.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001194 if (Tag == DW_TAG_compile_unit) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001195 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1196 }
1197
1198 // Create an empty instance of the correct sort.
1199 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001200
Jim Laskey21407982006-03-14 18:37:57 +00001201 // If not a user defined descriptor.
1202 if (Slot) {
1203 // Deserialize the fields.
1204 DIDeserializeVisitor DRAM(*this, GV);
1205 DRAM.ApplyToFields(Slot);
1206 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001207
1208 return Slot;
1209}
1210
1211//===----------------------------------------------------------------------===//
1212
1213/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001214///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001215const PointerType *DISerializer::getStrPtrType() {
1216 // If not already defined.
1217 if (!StrPtrTy) {
1218 // Construct the pointer to signed bytes.
1219 StrPtrTy = PointerType::get(Type::SByteTy);
1220 }
1221
1222 return StrPtrTy;
1223}
1224
1225/// getEmptyStructPtrType - Return a "{ }*" type.
1226///
1227const PointerType *DISerializer::getEmptyStructPtrType() {
1228 // If not already defined.
1229 if (!EmptyStructPtrTy) {
1230 // Construct the empty structure type.
1231 const StructType *EmptyStructTy =
1232 StructType::get(std::vector<const Type*>());
1233 // Construct the pointer to empty structure type.
1234 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1235 }
1236
1237 return EmptyStructPtrTy;
1238}
1239
1240/// getTagType - Return the type describing the specified descriptor (via tag.)
1241///
1242const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1243 // Attempt to get the previously defined type.
1244 StructType *&Ty = TagTypes[DD->getTag()];
1245
1246 // If not already defined.
1247 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001248 // Set up fields vector.
1249 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001250 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001251 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001252 GTAM.ApplyToFields(DD);
1253
1254 // Construct structured type.
1255 Ty = StructType::get(Fields);
1256
Jim Laskey86cbdba2006-02-06 15:33:21 +00001257 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001258 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001259 }
1260
1261 return Ty;
1262}
1263
1264/// getString - Construct the string as constant string global.
1265///
Jim Laskeyce72b172006-02-11 01:01:30 +00001266Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001267 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001268 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001269 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001270 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001271 // If empty string then use a sbyte* null instead.
1272 if (String.empty()) {
1273 Slot = ConstantPointerNull::get(getStrPtrType());
1274 } else {
1275 // Construct string as an llvm constant.
1276 Constant *ConstStr = ConstantArray::get(String);
1277 // Otherwise create and return a new string global.
1278 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1279 GlobalVariable::InternalLinkage,
1280 ConstStr, "str", M);
1281 StrGV->setSection("llvm.metadata");
1282 // Convert to generic string pointer.
1283 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1284 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001285 return Slot;
1286
Jim Laskey86cbdba2006-02-06 15:33:21 +00001287}
1288
1289/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1290/// so that it can be serialized to a .bc or .ll file.
1291GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1292 // Check if the DebugInfoDesc is already in the map.
1293 GlobalVariable *&Slot = DescGlobals[DD];
1294
1295 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1296 if (Slot) return Slot;
1297
Jim Laskey86cbdba2006-02-06 15:33:21 +00001298 // Get the type associated with the Tag.
1299 const StructType *Ty = getTagType(DD);
1300
1301 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001302 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1303 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001304 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001305
1306 // Insert new GlobalVariable in DescGlobals map.
1307 Slot = GV;
1308
1309 // Set up elements vector
1310 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001311 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001312 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001313 SRAM.ApplyToFields(DD);
1314
1315 // Set the globals initializer.
1316 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1317
1318 return GV;
1319}
1320
1321//===----------------------------------------------------------------------===//
1322
Jim Laskey86cbdba2006-02-06 15:33:21 +00001323/// Verify - Return true if the GlobalVariable appears to be a valid
1324/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001325bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001326 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001327}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001328bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001329 // NULLs are valid.
1330 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001331
Jim Laskey98e04102006-03-26 22:45:20 +00001332 // Check prior validity.
1333 unsigned &ValiditySlot = Validity[GV];
1334
1335 // If visited before then use old state.
1336 if (ValiditySlot) return ValiditySlot == Valid;
1337
1338 // Assume validity for the time being (recursion.)
1339 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001340
1341 // Make sure the global is internal or link once (anchor.)
1342 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1343 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1344 ValiditySlot = Invalid;
1345 return false;
1346 }
Jim Laskey98e04102006-03-26 22:45:20 +00001347
Jim Laskey86cbdba2006-02-06 15:33:21 +00001348 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001349 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001350
1351 // Check for user defined descriptors.
1352 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001353
1354 // If a compile unit we need the debug version.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001355 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001356 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001357 // FIXME - In the short term, changes are too drastic to continue.
Jim Laskey98e04102006-03-26 22:45:20 +00001358 if (DebugVersion != LLVMDebugVersion) {
1359 ValiditySlot = Invalid;
1360 return false;
1361 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001362 }
Jim Laskeyb8509c52006-03-23 18:07:55 +00001363
Jim Laskey86cbdba2006-02-06 15:33:21 +00001364 // Construct an empty DebugInfoDesc.
1365 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001366
1367 // Allow for user defined descriptors.
1368 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001369
1370 // Get the initializer constant.
1371 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1372
1373 // Get the operand count.
1374 unsigned N = CI->getNumOperands();
1375
1376 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001377 unsigned &CountSlot = Counts[Tag];
1378 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001379 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001380 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001381 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001382 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001383 }
1384
Jim Laskey21407982006-03-14 18:37:57 +00001385 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001386 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001387 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001388 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001389 return false;
1390 }
1391
1392 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001393 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394 VRAM.ApplyToFields(DD);
1395
1396 // Release empty DebugInfoDesc.
1397 delete DD;
1398
Jim Laskey98e04102006-03-26 22:45:20 +00001399 // If fields are not valid.
1400 if (!VRAM.isValid()) {
1401 ValiditySlot = Invalid;
1402 return false;
1403 }
1404
1405 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001406}
1407
1408//===----------------------------------------------------------------------===//
1409
Jim Laskeyb8509c52006-03-23 18:07:55 +00001410DebugScope::~DebugScope() {
1411 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1412 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1413}
1414
1415//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001416
1417MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001418: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001419, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001420, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001421, Directories()
1422, SourceFiles()
1423, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001424, LabelID(0)
1425, ScopeMap()
1426, RootScope(NULL)
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001427{
1428
1429}
1430MachineDebugInfo::~MachineDebugInfo() {
1431
1432}
1433
Jim Laskeyb2efb852006-01-04 22:28:25 +00001434/// doInitialization - Initialize the debug state for a new module.
1435///
1436bool MachineDebugInfo::doInitialization() {
1437 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001438}
1439
Jim Laskeyb2efb852006-01-04 22:28:25 +00001440/// doFinalization - Tear down the debug state after completion of a module.
1441///
1442bool MachineDebugInfo::doFinalization() {
1443 return false;
1444}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001445
Jim Laskeyd96185a2006-02-13 12:50:39 +00001446/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001447///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001448// FIXME - use new Value type when available.
1449DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001450 return DR.Deserialize(V);
1451}
1452
1453/// Verify - Verify that a Value is debug information descriptor.
1454///
1455bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001456 return VR.Verify(V);
1457}
1458
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001459/// AnalyzeModule - Scan the module for global debug information.
1460///
1461void MachineDebugInfo::AnalyzeModule(Module &M) {
1462 SetupCompileUnits(M);
1463}
1464
1465/// SetupCompileUnits - Set up the unique vector of compile units.
1466///
1467void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001468 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001469
Jim Laskey0420f2a2006-02-22 19:02:11 +00001470 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1471 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001472 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001473}
1474
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001475/// getCompileUnits - Return a vector of debug compile units.
1476///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001477const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001478 return CompileUnits;
1479}
1480
Jim Laskey0420f2a2006-02-22 19:02:11 +00001481/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1482/// named GlobalVariable.
1483std::vector<GlobalVariable*>
1484MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1485 const std::string &RootName) {
1486 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001487}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001488
1489/// RecordLabel - Records location information and associates it with a
1490/// debug label. Returns a unique label ID used to generate a label and
1491/// provide correspondence to the source line list.
1492unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1493 unsigned Source) {
1494 unsigned ID = NextLabelID();
1495 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1496 return ID;
1497}
1498
1499/// RecordSource - Register a source file with debug info. Returns an source
1500/// ID.
1501unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1502 const std::string &Source) {
1503 unsigned DirectoryID = Directories.insert(Directory);
1504 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1505}
1506unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1507 return RecordSource(CompileUnit->getDirectory(),
1508 CompileUnit->getFileName());
1509}
1510
1511/// RecordRegionStart - Indicate the start of a region.
1512///
1513unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1514 // FIXME - need to be able to handle split scopes because of bb cloning.
1515 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1516 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1517 unsigned ID = NextLabelID();
1518 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1519 return ID;
1520}
1521
1522/// RecordRegionEnd - Indicate the end of a region.
1523///
1524unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1525 // FIXME - need to be able to handle split scopes because of bb cloning.
1526 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1527 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1528 unsigned ID = NextLabelID();
1529 Scope->setEndLabelID(ID);
1530 return ID;
1531}
1532
1533/// RecordVariable - Indicate the declaration of a local variable.
1534///
1535void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1536 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1537 DebugScope *Scope = getOrCreateScope(VD->getContext());
1538 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1539 Scope->AddVariable(DV);
1540}
1541
1542/// getOrCreateScope - Returns the scope associated with the given descriptor.
1543///
1544DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1545 DebugScope *&Slot = ScopeMap[ScopeDesc];
1546 if (!Slot) {
1547 // FIXME - breaks down when the context is an inlined function.
1548 DebugInfoDesc *ParentDesc = NULL;
1549 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1550 ParentDesc = Block->getContext();
1551 }
1552 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1553 Slot = new DebugScope(Parent, ScopeDesc);
1554 if (Parent) {
1555 Parent->AddScope(Slot);
1556 } else if (RootScope) {
1557 // FIXME - Add inlined function scopes to the root so we can delete
1558 // them later. Long term, handle inlined functions properly.
1559 RootScope->AddScope(Slot);
1560 } else {
1561 // First function is top level function.
1562 RootScope = Slot;
1563 }
1564 }
1565 return Slot;
1566}
1567
1568/// ClearScopes - Delete the scope and variable info after a function is
1569/// completed.
1570void MachineDebugInfo::ClearScopes() {
1571 if (RootScope) {
1572 delete RootScope;
1573 ScopeMap.clear();
1574 RootScope = NULL;
1575 }
1576}
1577
1578