blob: 2e7e8c0d9f12b4b72da54f5ab7f10eb9b85ab700 [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"
Jim Laskey41886992006-04-07 16:34:46 +000013#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000014#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000015#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000016#include "llvm/Intrinsics.h"
17#include "llvm/Instructions.h"
18#include "llvm/Module.h"
19#include "llvm/Support/Dwarf.h"
20
Jim Laskey86cbdba2006-02-06 15:33:21 +000021#include <iostream>
22
Jim Laskey6af56812006-01-04 13:36:38 +000023using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000024using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000025
26// Handle the Pass registration stuff necessary to use TargetData's.
27namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000028 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
29}
Jim Laskey063e7652006-01-17 17:31:53 +000030
Jim Laskeyb3e789a2006-01-26 20:21:46 +000031//===----------------------------------------------------------------------===//
32
Jim Laskey86cbdba2006-02-06 15:33:21 +000033/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000034/// specified value in their initializer somewhere.
35static void
36getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
37 // Scan though value users.
38 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
39 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000040 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000041 Result.push_back(GV);
42 } else if (Constant *C = dyn_cast<Constant>(*I)) {
43 // If the user is a constant variable then scan its users
44 getGlobalVariablesUsing(C, Result);
45 }
46 }
47}
48
Jim Laskey86cbdba2006-02-06 15:33:21 +000049/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
50/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000051static std::vector<GlobalVariable*>
52getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000053 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000054
55 std::vector<const Type*> FieldTypes;
56 FieldTypes.push_back(Type::UIntTy);
Jim Laskeye8c3e3b2006-03-07 20:53:47 +000057 FieldTypes.push_back(Type::UIntTy);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000058
Jim Laskey86cbdba2006-02-06 15:33:21 +000059 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000060 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000061 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062
63 // If present and linkonce then scan for users.
64 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
65 getGlobalVariablesUsing(UseRoot, Result);
66 }
67
68 return Result;
69}
70
Jim Laskey86cbdba2006-02-06 15:33:21 +000071/// isStringValue - Return true if the given value can be coerced to a string.
72///
73static bool isStringValue(Value *V) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 return Init->isString();
78 }
79 } else if (Constant *C = dyn_cast<Constant>(V)) {
80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
81 return isStringValue(GV);
82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 if (CE->getOpcode() == Instruction::GetElementPtr) {
84 if (CE->getNumOperands() == 3 &&
85 cast<Constant>(CE->getOperand(1))->isNullValue() &&
86 isa<ConstantInt>(CE->getOperand(2))) {
87 return isStringValue(CE->getOperand(0));
88 }
89 }
90 }
91 }
92 return false;
93}
94
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000095/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000096///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000097static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000098 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
99 return GV;
100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000101 if (CE->getOpcode() == Instruction::Cast) {
102 return dyn_cast<GlobalVariable>(CE->getOperand(0));
103 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000104 }
105 return NULL;
106}
107
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000108/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000109/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000110static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000111 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
112 return true;
113 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
114 if (CE->getOpcode() == Instruction::Cast) {
115 return isa<GlobalVariable>(CE->getOperand(0));
116 }
117 }
118 return false;
119}
120
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000121/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000122///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000123static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000124 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000125 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000126
Jim Laskey86cbdba2006-02-06 15:33:21 +0000127 // Get the initializer constant.
128 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000129 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000130
Jim Laskey86cbdba2006-02-06 15:33:21 +0000131 // Check if there is at least i + 1 operands.
132 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000133 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000134
Jim Laskey86cbdba2006-02-06 15:33:21 +0000135 // Check constant.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000136 return dyn_cast<ConstantUInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000137}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000138//===----------------------------------------------------------------------===//
139
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000140/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000141/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000142void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000143 DD->ApplyToFields(this);
144}
145
146//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000147/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
148/// the supplied DebugInfoDesc.
149class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000150private:
151 unsigned Count; // Running count of fields.
152
153public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000154 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000155
156 // Accessors.
157 unsigned getCount() const { return Count; }
158
159 /// Apply - Count each of the fields.
160 ///
161 virtual void Apply(int &Field) { ++Count; }
162 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000163 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000164 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000165 virtual void Apply(bool &Field) { ++Count; }
166 virtual void Apply(std::string &Field) { ++Count; }
167 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
168 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000169 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
170 ++Count;
171 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000172};
173
174//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000175/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
176/// supplied DebugInfoDesc.
177class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000178private:
179 DIDeserializer &DR; // Active deserializer.
180 unsigned I; // Current operand index.
181 ConstantStruct *CI; // GlobalVariable constant initializer.
182
183public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000184 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
185 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000186 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000187 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000188 , CI(cast<ConstantStruct>(GV->getInitializer()))
189 {}
190
191 /// Apply - Set the value of each of the fields.
192 ///
193 virtual void Apply(int &Field) {
194 Constant *C = CI->getOperand(I++);
195 Field = cast<ConstantSInt>(C)->getValue();
196 }
197 virtual void Apply(unsigned &Field) {
198 Constant *C = CI->getOperand(I++);
199 Field = cast<ConstantUInt>(C)->getValue();
200 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000201 virtual void Apply(int64_t &Field) {
202 Constant *C = CI->getOperand(I++);
203 Field = cast<ConstantSInt>(C)->getValue();
204 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000205 virtual void Apply(uint64_t &Field) {
206 Constant *C = CI->getOperand(I++);
207 Field = cast<ConstantUInt>(C)->getValue();
208 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000209 virtual void Apply(bool &Field) {
210 Constant *C = CI->getOperand(I++);
211 Field = cast<ConstantBool>(C)->getValue();
212 }
213 virtual void Apply(std::string &Field) {
214 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000215 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000216 }
217 virtual void Apply(DebugInfoDesc *&Field) {
218 Constant *C = CI->getOperand(I++);
219 Field = DR.Deserialize(C);
220 }
221 virtual void Apply(GlobalVariable *&Field) {
222 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000223 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000224 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000225 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
226 Constant *C = CI->getOperand(I++);
227 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskey45ccae52006-02-28 20:15:07 +0000228 Field.resize(0);
Jim Laskey2b0e3092006-03-08 02:07:02 +0000229 // Have to be able to deal with the empty array case (zero initializer)
230 if (!GV->hasInitializer()) return;
231 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
232 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
233 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
234 DebugInfoDesc *DE = DR.Deserialize(GVE);
235 Field.push_back(DE);
236 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000237 }
238 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000239};
240
241//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000242/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000243/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000244class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000245private:
246 DISerializer &SR; // Active serializer.
247 std::vector<Constant*> &Elements; // Element accumulator.
248
249public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000250 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
251 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000252 , SR(S)
253 , Elements(E)
254 {}
255
256 /// Apply - Set the value of each of the fields.
257 ///
258 virtual void Apply(int &Field) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000259 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000260 }
261 virtual void Apply(unsigned &Field) {
262 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
263 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000264 virtual void Apply(int64_t &Field) {
265 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
266 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000267 virtual void Apply(uint64_t &Field) {
268 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
269 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000270 virtual void Apply(bool &Field) {
271 Elements.push_back(ConstantBool::get(Field));
272 }
273 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000274 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000275 }
276 virtual void Apply(DebugInfoDesc *&Field) {
277 GlobalVariable *GV = NULL;
278
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000279 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000280 if (Field) GV = SR.Serialize(Field);
281
282 // FIXME - At some point should use specific type.
283 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
284
285 if (GV) {
286 // Set to pointer to global.
287 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
288 } else {
289 // Use NULL.
290 Elements.push_back(ConstantPointerNull::get(EmptyTy));
291 }
292 }
293 virtual void Apply(GlobalVariable *&Field) {
294 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000295 if (Field) {
296 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
297 } else {
298 Elements.push_back(ConstantPointerNull::get(EmptyTy));
299 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000300 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000301 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
302 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
303 unsigned N = Field.size();
304 ArrayType *AT = ArrayType::get(EmptyTy, N);
305 std::vector<Constant *> ArrayElements;
306
307 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
308 GlobalVariable *GVE = SR.Serialize(Field[i]);
309 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
310 ArrayElements.push_back(cast<Constant>(CE));
311 }
312
313 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000314 GlobalVariable *CAGV = new GlobalVariable(AT, true,
315 GlobalValue::InternalLinkage,
316 CA, "llvm.dbg.array",
317 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000318 CAGV->setSection("llvm.metadata");
Jim Laskeyf8913f12006-03-01 17:53:02 +0000319 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000320 Elements.push_back(CAE);
321 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000322};
323
324//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000325/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000326/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000327class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000328private:
329 DISerializer &SR; // Active serializer.
330 std::vector<const Type*> &Fields; // Type accumulator.
331
332public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000333 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
334 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000335 , SR(S)
336 , Fields(F)
337 {}
338
339 /// Apply - Set the value of each of the fields.
340 ///
341 virtual void Apply(int &Field) {
342 Fields.push_back(Type::IntTy);
343 }
344 virtual void Apply(unsigned &Field) {
345 Fields.push_back(Type::UIntTy);
346 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000347 virtual void Apply(int64_t &Field) {
348 Fields.push_back(Type::IntTy);
349 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000350 virtual void Apply(uint64_t &Field) {
351 Fields.push_back(Type::UIntTy);
352 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000353 virtual void Apply(bool &Field) {
354 Fields.push_back(Type::BoolTy);
355 }
356 virtual void Apply(std::string &Field) {
357 Fields.push_back(SR.getStrPtrType());
358 }
359 virtual void Apply(DebugInfoDesc *&Field) {
360 // FIXME - At some point should use specific type.
361 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
362 Fields.push_back(EmptyTy);
363 }
364 virtual void Apply(GlobalVariable *&Field) {
365 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
366 Fields.push_back(EmptyTy);
367 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000368 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
369 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
370 Fields.push_back(EmptyTy);
371 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000372};
373
374//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000375/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000376/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000377class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000378private:
379 DIVerifier &VR; // Active verifier.
380 bool IsValid; // Validity status.
381 unsigned I; // Current operand index.
382 ConstantStruct *CI; // GlobalVariable constant initializer.
383
384public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000385 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
386 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000387 , VR(V)
388 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000389 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000390 , CI(cast<ConstantStruct>(GV->getInitializer()))
391 {
392 }
393
394 // Accessors.
395 bool isValid() const { return IsValid; }
396
397 /// Apply - Set the value of each of the fields.
398 ///
399 virtual void Apply(int &Field) {
400 Constant *C = CI->getOperand(I++);
401 IsValid = IsValid && isa<ConstantInt>(C);
402 }
403 virtual void Apply(unsigned &Field) {
404 Constant *C = CI->getOperand(I++);
405 IsValid = IsValid && isa<ConstantInt>(C);
406 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000407 virtual void Apply(int64_t &Field) {
408 Constant *C = CI->getOperand(I++);
409 IsValid = IsValid && isa<ConstantInt>(C);
410 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000411 virtual void Apply(uint64_t &Field) {
412 Constant *C = CI->getOperand(I++);
413 IsValid = IsValid && isa<ConstantInt>(C);
414 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000415 virtual void Apply(bool &Field) {
416 Constant *C = CI->getOperand(I++);
417 IsValid = IsValid && isa<ConstantBool>(C);
418 }
419 virtual void Apply(std::string &Field) {
420 Constant *C = CI->getOperand(I++);
Jim Laskey21407982006-03-14 18:37:57 +0000421 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000422 }
423 virtual void Apply(DebugInfoDesc *&Field) {
424 // FIXME - Prepare the correct descriptor.
425 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000426 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000427 }
428 virtual void Apply(GlobalVariable *&Field) {
429 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000430 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000431 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000432 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
433 Constant *C = CI->getOperand(I++);
434 IsValid = IsValid && isGlobalVariable(C);
435 if (!IsValid) return;
436
437 GlobalVariable *GV = getGlobalVariable(C);
438 IsValid = IsValid && GV && GV->hasInitializer();
439 if (!IsValid) return;
440
441 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
442 IsValid = IsValid && CA;
443 if (!IsValid) return;
444
445 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
446 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
447 if (!IsValid) return;
448
449 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
450 VR.Verify(GVE);
451 }
452 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000453};
454
Jim Laskeyce72b172006-02-11 01:01:30 +0000455
Jim Laskey86cbdba2006-02-06 15:33:21 +0000456//===----------------------------------------------------------------------===//
457
Jim Laskeyce72b172006-02-11 01:01:30 +0000458/// TagFromGlobal - Returns the Tag number from a debug info descriptor
459/// GlobalVariable.
460unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
461 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000462 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000463}
464
465/// DescFactory - Create an instance of debug info descriptor based on Tag.
466/// Return NULL if not a recognized Tag.
467DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
468 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000469 case DW_TAG_anchor: return new AnchorDesc();
470 case DW_TAG_compile_unit: return new CompileUnitDesc();
471 case DW_TAG_variable: return new GlobalVariableDesc();
472 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000473 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000474 case DW_TAG_base_type: return new BasicTypeDesc();
475 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000476 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000477 case DW_TAG_reference_type:
478 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000479 case DW_TAG_volatile_type:
480 case DW_TAG_restrict_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000481 case DW_TAG_member: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000482 case DW_TAG_array_type:
483 case DW_TAG_structure_type:
484 case DW_TAG_union_type:
485 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
486 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000487 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000488 case DW_TAG_return_variable:
489 case DW_TAG_arg_variable:
490 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000491 default: break;
492 }
493 return NULL;
494}
495
496/// getLinkage - get linkage appropriate for this type of descriptor.
497///
498GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
499 return GlobalValue::InternalLinkage;
500}
501
502/// ApplyToFields - Target the vistor to the fields of the descriptor.
503///
504void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
505 Visitor->Apply(Tag);
506}
507
508//===----------------------------------------------------------------------===//
509
Jim Laskey9c4447a2006-03-01 20:39:36 +0000510AnchorDesc::AnchorDesc()
511: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000512, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000513{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000514AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000515: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000516, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000517{}
518
519// Implement isa/cast/dyncast.
520bool AnchorDesc::classof(const DebugInfoDesc *D) {
521 return D->getTag() == DW_TAG_anchor;
522}
523
Jim Laskeyce72b172006-02-11 01:01:30 +0000524/// getLinkage - get linkage appropriate for this type of descriptor.
525///
526GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
527 return GlobalValue::LinkOnceLinkage;
528}
529
530/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
531///
532void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
533 DebugInfoDesc::ApplyToFields(Visitor);
534
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000535 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000536}
537
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000538/// getDescString - Return a string used to compose global names and labels. A
539/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000540/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000541/// to the list of names left external in the internalizer.
542/// ExternalNames.insert("llvm.dbg.compile_units");
543/// ExternalNames.insert("llvm.dbg.global_variables");
544/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000545const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000546 switch (AnchorTag) {
547 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
548 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
549 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
550 default: break;
551 }
552
553 assert(0 && "Tag does not have a case for anchor string");
554 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000555}
556
557/// getTypeString - Return a string used to label this descriptors type.
558///
559const char *AnchorDesc::getTypeString() const {
560 return "llvm.dbg.anchor.type";
561}
562
563#ifndef NDEBUG
564void AnchorDesc::dump() {
565 std::cerr << getDescString() << " "
566 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000567 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000568}
569#endif
570
571//===----------------------------------------------------------------------===//
572
573AnchoredDesc::AnchoredDesc(unsigned T)
574: DebugInfoDesc(T)
575, Anchor(NULL)
576{}
577
578/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
579///
580void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
581 DebugInfoDesc::ApplyToFields(Visitor);
582
583 Visitor->Apply((DebugInfoDesc *&)Anchor);
584}
585
586//===----------------------------------------------------------------------===//
587
588CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000589: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000590, DebugVersion(LLVMDebugVersion)
591, Language(0)
592, FileName("")
593, Directory("")
594, Producer("")
595{}
596
Jim Laskey9c4447a2006-03-01 20:39:36 +0000597// Implement isa/cast/dyncast.
598bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
599 return D->getTag() == DW_TAG_compile_unit;
600}
601
Jim Laskey86cbdba2006-02-06 15:33:21 +0000602/// DebugVersionFromGlobal - Returns the version number from a compile unit
603/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000604unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000605 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000606 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000607}
608
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000609/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
610///
611void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000612 AnchoredDesc::ApplyToFields(Visitor);
613
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000614 Visitor->Apply(DebugVersion);
615 Visitor->Apply(Language);
616 Visitor->Apply(FileName);
617 Visitor->Apply(Directory);
618 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000619}
620
Jim Laskeyce72b172006-02-11 01:01:30 +0000621/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000622///
Jim Laskeyce72b172006-02-11 01:01:30 +0000623const char *CompileUnitDesc::getDescString() const {
624 return "llvm.dbg.compile_unit";
625}
626
627/// getTypeString - Return a string used to label this descriptors type.
628///
629const char *CompileUnitDesc::getTypeString() const {
630 return "llvm.dbg.compile_unit.type";
631}
632
633/// getAnchorString - Return a string used to label this descriptor's anchor.
634///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000635const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000636const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000637 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000638}
639
Jim Laskey86cbdba2006-02-06 15:33:21 +0000640#ifndef NDEBUG
641void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000642 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000643 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000644 << "Anchor(" << getAnchor() << "), "
645 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000646 << "Language(" << Language << "), "
647 << "FileName(\"" << FileName << "\"), "
648 << "Directory(\"" << Directory << "\"), "
649 << "Producer(\"" << Producer << "\")\n";
650}
651#endif
652
653//===----------------------------------------------------------------------===//
654
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000655TypeDesc::TypeDesc(unsigned T)
656: DebugInfoDesc(T)
657, Context(NULL)
658, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000659, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000660, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000661, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000662, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000663, Offset(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000664{}
665
Jim Laskey69906002006-02-24 16:46:40 +0000666/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000667///
668void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
669 DebugInfoDesc::ApplyToFields(Visitor);
670
671 Visitor->Apply(Context);
672 Visitor->Apply(Name);
Jim Laskey69906002006-02-24 16:46:40 +0000673 Visitor->Apply((DebugInfoDesc *&)File);
674 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000675 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000676 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000677 Visitor->Apply(Offset);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000678}
679
680/// getDescString - Return a string used to compose global names and labels.
681///
682const char *TypeDesc::getDescString() const {
683 return "llvm.dbg.type";
684}
685
686/// getTypeString - Return a string used to label this descriptor's type.
687///
688const char *TypeDesc::getTypeString() const {
689 return "llvm.dbg.type.type";
690}
691
692#ifndef NDEBUG
693void TypeDesc::dump() {
694 std::cerr << getDescString() << " "
695 << "Tag(" << getTag() << "), "
696 << "Context(" << Context << "), "
697 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000698 << "File(" << File << "), "
699 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000700 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000701 << "Align(" << Align << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000702 << "Offset(" << Offset << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000703}
704#endif
705
706//===----------------------------------------------------------------------===//
707
708BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000709: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000710, Encoding(0)
711{}
712
Jim Laskey9c4447a2006-03-01 20:39:36 +0000713// Implement isa/cast/dyncast.
714bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
715 return D->getTag() == DW_TAG_base_type;
716}
717
Jim Laskey69906002006-02-24 16:46:40 +0000718/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000719///
720void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
721 TypeDesc::ApplyToFields(Visitor);
722
723 Visitor->Apply(Encoding);
724}
725
Jim Laskeyf8913f12006-03-01 17:53:02 +0000726/// getDescString - Return a string used to compose global names and labels.
727///
728const char *BasicTypeDesc::getDescString() const {
729 return "llvm.dbg.basictype";
730}
731
732/// getTypeString - Return a string used to label this descriptor's type.
733///
734const char *BasicTypeDesc::getTypeString() const {
735 return "llvm.dbg.basictype.type";
736}
737
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000738#ifndef NDEBUG
739void BasicTypeDesc::dump() {
740 std::cerr << getDescString() << " "
741 << "Tag(" << getTag() << "), "
742 << "Context(" << getContext() << "), "
743 << "Name(\"" << getName() << "\"), "
744 << "Size(" << getSize() << "), "
745 << "Encoding(" << Encoding << ")\n";
746}
747#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000748
Jim Laskey434b40b2006-02-23 22:37:30 +0000749//===----------------------------------------------------------------------===//
750
Jim Laskey69906002006-02-24 16:46:40 +0000751DerivedTypeDesc::DerivedTypeDesc(unsigned T)
752: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000753, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000754{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000755
Jim Laskey9c4447a2006-03-01 20:39:36 +0000756// Implement isa/cast/dyncast.
757bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
758 unsigned T = D->getTag();
759 switch (T) {
760 case DW_TAG_typedef:
761 case DW_TAG_pointer_type:
762 case DW_TAG_reference_type:
763 case DW_TAG_const_type:
764 case DW_TAG_volatile_type:
765 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000766 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000767 return true;
768 default: break;
769 }
770 return false;
771}
772
Jim Laskey69906002006-02-24 16:46:40 +0000773/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000774///
Jim Laskey69906002006-02-24 16:46:40 +0000775void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000776 TypeDesc::ApplyToFields(Visitor);
777
778 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000779}
780
Jim Laskeyf8913f12006-03-01 17:53:02 +0000781/// getDescString - Return a string used to compose global names and labels.
782///
783const char *DerivedTypeDesc::getDescString() const {
784 return "llvm.dbg.derivedtype";
785}
786
787/// getTypeString - Return a string used to label this descriptor's type.
788///
789const char *DerivedTypeDesc::getTypeString() const {
790 return "llvm.dbg.derivedtype.type";
791}
792
Jim Laskey434b40b2006-02-23 22:37:30 +0000793#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000794void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000795 std::cerr << getDescString() << " "
796 << "Tag(" << getTag() << "), "
797 << "Context(" << getContext() << "), "
798 << "Name(\"" << getName() << "\"), "
799 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000800 << "File(" << getFile() << "), "
801 << "Line(" << getLine() << "), "
802 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000803}
804#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000805
806//===----------------------------------------------------------------------===//
807
Jim Laskeyf8913f12006-03-01 17:53:02 +0000808CompositeTypeDesc::CompositeTypeDesc(unsigned T)
809: DerivedTypeDesc(T)
810, Elements()
811{}
812
Jim Laskey9c4447a2006-03-01 20:39:36 +0000813// Implement isa/cast/dyncast.
814bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
815 unsigned T = D->getTag();
816 switch (T) {
817 case DW_TAG_array_type:
818 case DW_TAG_structure_type:
819 case DW_TAG_union_type:
820 case DW_TAG_enumeration_type:
821 return true;
822 default: break;
823 }
824 return false;
825}
826
Jim Laskeyf8913f12006-03-01 17:53:02 +0000827/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
828///
829void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
830 DerivedTypeDesc::ApplyToFields(Visitor);
831
832 Visitor->Apply(Elements);
833}
834
835/// getDescString - Return a string used to compose global names and labels.
836///
837const char *CompositeTypeDesc::getDescString() const {
838 return "llvm.dbg.compositetype";
839}
840
841/// getTypeString - Return a string used to label this descriptor's type.
842///
843const char *CompositeTypeDesc::getTypeString() const {
844 return "llvm.dbg.compositetype.type";
845}
846
847#ifndef NDEBUG
848void CompositeTypeDesc::dump() {
849 std::cerr << getDescString() << " "
850 << "Tag(" << getTag() << "), "
851 << "Context(" << getContext() << "), "
852 << "Name(\"" << getName() << "\"), "
853 << "Size(" << getSize() << "), "
854 << "File(" << getFile() << "), "
855 << "Line(" << getLine() << "), "
856 << "FromType(" << getFromType() << "), "
857 << "Elements.size(" << Elements.size() << ")\n";
858}
859#endif
860
861//===----------------------------------------------------------------------===//
862
863SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000864: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000865, Lo(0)
866, Hi(0)
867{}
868
Jim Laskey9c4447a2006-03-01 20:39:36 +0000869// Implement isa/cast/dyncast.
870bool SubrangeDesc::classof(const DebugInfoDesc *D) {
871 return D->getTag() == DW_TAG_subrange_type;
872}
873
Jim Laskeyf8913f12006-03-01 17:53:02 +0000874/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
875///
876void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
877 DebugInfoDesc::ApplyToFields(Visitor);
878
879 Visitor->Apply(Lo);
880 Visitor->Apply(Hi);
881}
882
883/// getDescString - Return a string used to compose global names and labels.
884///
885const char *SubrangeDesc::getDescString() const {
886 return "llvm.dbg.subrange";
887}
888
889/// getTypeString - Return a string used to label this descriptor's type.
890///
891const char *SubrangeDesc::getTypeString() const {
892 return "llvm.dbg.subrange.type";
893}
894
895#ifndef NDEBUG
896void SubrangeDesc::dump() {
897 std::cerr << getDescString() << " "
898 << "Tag(" << getTag() << "), "
899 << "Lo(" << Lo << "), "
900 << "Hi(" << Hi << ")\n";
901}
902#endif
903
904//===----------------------------------------------------------------------===//
905
Jim Laskey6a3eb012006-03-01 23:52:37 +0000906EnumeratorDesc::EnumeratorDesc()
907: DebugInfoDesc(DW_TAG_enumerator)
908, Name("")
909, Value(0)
910{}
911
912// Implement isa/cast/dyncast.
913bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
914 return D->getTag() == DW_TAG_enumerator;
915}
916
917/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
918///
919void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
920 DebugInfoDesc::ApplyToFields(Visitor);
921
922 Visitor->Apply(Name);
923 Visitor->Apply(Value);
924}
925
926/// getDescString - Return a string used to compose global names and labels.
927///
928const char *EnumeratorDesc::getDescString() const {
929 return "llvm.dbg.enumerator";
930}
931
932/// getTypeString - Return a string used to label this descriptor's type.
933///
934const char *EnumeratorDesc::getTypeString() const {
935 return "llvm.dbg.enumerator.type";
936}
937
938#ifndef NDEBUG
939void EnumeratorDesc::dump() {
940 std::cerr << getDescString() << " "
941 << "Tag(" << getTag() << "), "
942 << "Name(" << Name << "), "
943 << "Value(" << Value << ")\n";
944}
945#endif
946
947//===----------------------------------------------------------------------===//
948
Jim Laskeyb8509c52006-03-23 18:07:55 +0000949VariableDesc::VariableDesc(unsigned T)
950: DebugInfoDesc(T)
951, Context(NULL)
952, Name("")
953, File(NULL)
954, Line(0)
955, TyDesc(0)
956{}
957
958// Implement isa/cast/dyncast.
959bool VariableDesc::classof(const DebugInfoDesc *D) {
960 unsigned T = D->getTag();
961 switch (T) {
962 case DW_TAG_auto_variable:
963 case DW_TAG_arg_variable:
964 case DW_TAG_return_variable:
965 return true;
966 default: break;
967 }
968 return false;
969}
970
971/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
972///
973void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
974 DebugInfoDesc::ApplyToFields(Visitor);
975
976 Visitor->Apply(Context);
977 Visitor->Apply(Name);
978 Visitor->Apply((DebugInfoDesc *&)File);
979 Visitor->Apply(Line);
980 Visitor->Apply((DebugInfoDesc *&)TyDesc);
981}
982
983/// getDescString - Return a string used to compose global names and labels.
984///
985const char *VariableDesc::getDescString() const {
986 return "llvm.dbg.variable";
987}
988
989/// getTypeString - Return a string used to label this descriptor's type.
990///
991const char *VariableDesc::getTypeString() const {
992 return "llvm.dbg.variable.type";
993}
994
995#ifndef NDEBUG
996void VariableDesc::dump() {
997 std::cerr << getDescString() << " "
998 << "Tag(" << getTag() << "), "
999 << "Context(" << Context << "), "
1000 << "Name(\"" << Name << "\"), "
1001 << "File(" << File << "), "
1002 << "Line(" << Line << "), "
1003 << "TyDesc(" << TyDesc << ")\n";
1004}
1005#endif
1006
1007//===----------------------------------------------------------------------===//
1008
Jim Laskeyce72b172006-02-11 01:01:30 +00001009GlobalDesc::GlobalDesc(unsigned T)
1010: AnchoredDesc(T)
1011, Context(0)
1012, Name("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001013, File(NULL)
1014, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001015, TyDesc(NULL)
1016, IsStatic(false)
1017, IsDefinition(false)
1018{}
1019
1020/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001021///
Jim Laskeyce72b172006-02-11 01:01:30 +00001022void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1023 AnchoredDesc::ApplyToFields(Visitor);
1024
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001025 Visitor->Apply(Context);
1026 Visitor->Apply(Name);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001027 Visitor->Apply((DebugInfoDesc *&)File);
1028 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001029 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001030 Visitor->Apply(IsStatic);
1031 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001032}
1033
1034//===----------------------------------------------------------------------===//
1035
1036GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001037: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001038, Global(NULL)
1039{}
1040
Jim Laskey9c4447a2006-03-01 20:39:36 +00001041// Implement isa/cast/dyncast.
1042bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1043 return D->getTag() == DW_TAG_variable;
1044}
1045
Jim Laskeyce72b172006-02-11 01:01:30 +00001046/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1047///
1048void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1049 GlobalDesc::ApplyToFields(Visitor);
1050
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001051 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001052}
1053
Jim Laskeyce72b172006-02-11 01:01:30 +00001054/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001055///
Jim Laskeyce72b172006-02-11 01:01:30 +00001056const char *GlobalVariableDesc::getDescString() const {
1057 return "llvm.dbg.global_variable";
1058}
1059
1060/// getTypeString - Return a string used to label this descriptors type.
1061///
1062const char *GlobalVariableDesc::getTypeString() const {
1063 return "llvm.dbg.global_variable.type";
1064}
1065
1066/// getAnchorString - Return a string used to label this descriptor's anchor.
1067///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001068const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001069const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001070 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001071}
1072
Jim Laskey86cbdba2006-02-06 15:33:21 +00001073#ifndef NDEBUG
1074void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001075 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001076 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001077 << "Anchor(" << getAnchor() << "), "
1078 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001079 << "File(" << getFile() << "),"
1080 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001081 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001082 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1083 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001084 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001085}
1086#endif
1087
1088//===----------------------------------------------------------------------===//
1089
Jim Laskeyce72b172006-02-11 01:01:30 +00001090SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001091: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001092{}
1093
Jim Laskey9c4447a2006-03-01 20:39:36 +00001094// Implement isa/cast/dyncast.
1095bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1096 return D->getTag() == DW_TAG_subprogram;
1097}
1098
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001099/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001100/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001101void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001102 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001103}
1104
Jim Laskeyce72b172006-02-11 01:01:30 +00001105/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001106///
Jim Laskeyce72b172006-02-11 01:01:30 +00001107const char *SubprogramDesc::getDescString() const {
1108 return "llvm.dbg.subprogram";
1109}
1110
1111/// getTypeString - Return a string used to label this descriptors type.
1112///
1113const char *SubprogramDesc::getTypeString() const {
1114 return "llvm.dbg.subprogram.type";
1115}
1116
1117/// getAnchorString - Return a string used to label this descriptor's anchor.
1118///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001119const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001120const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001121 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001122}
1123
Jim Laskey86cbdba2006-02-06 15:33:21 +00001124#ifndef NDEBUG
1125void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001126 std::cerr << getDescString() << " "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001127 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001128 << "Anchor(" << getAnchor() << "), "
1129 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001130 << "File(" << getFile() << "),"
1131 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001132 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001133 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1134 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001135}
1136#endif
1137
Jim Laskey45ccae52006-02-28 20:15:07 +00001138//===----------------------------------------------------------------------===//
1139
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001140BlockDesc::BlockDesc()
1141: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001142, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001143{}
1144
1145// Implement isa/cast/dyncast.
1146bool BlockDesc::classof(const DebugInfoDesc *D) {
1147 return D->getTag() == DW_TAG_lexical_block;
1148}
1149
1150/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1151///
1152void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1153 DebugInfoDesc::ApplyToFields(Visitor);
1154
Jim Laskeyb8509c52006-03-23 18:07:55 +00001155 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001156}
1157
1158/// getDescString - Return a string used to compose global names and labels.
1159///
1160const char *BlockDesc::getDescString() const {
1161 return "llvm.dbg.block";
1162}
1163
1164/// getTypeString - Return a string used to label this descriptors type.
1165///
1166const char *BlockDesc::getTypeString() const {
1167 return "llvm.dbg.block.type";
1168}
1169
1170#ifndef NDEBUG
1171void BlockDesc::dump() {
1172 std::cerr << getDescString() << " "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001173 << "Tag(" << getTag() << "),"
1174 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001175}
1176#endif
1177
1178//===----------------------------------------------------------------------===//
1179
Jim Laskey86cbdba2006-02-06 15:33:21 +00001180DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001181 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001182}
1183DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001184 // Handle NULL.
1185 if (!GV) return NULL;
1186
Jim Laskey86cbdba2006-02-06 15:33:21 +00001187 // Check to see if it has been already deserialized.
1188 DebugInfoDesc *&Slot = GlobalDescs[GV];
1189 if (Slot) return Slot;
1190
1191 // Get the Tag from the global.
1192 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1193
1194 // Get the debug version if a compile unit.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001195 if (Tag == DW_TAG_compile_unit) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001196 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1197 }
1198
1199 // Create an empty instance of the correct sort.
1200 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001201
Jim Laskey21407982006-03-14 18:37:57 +00001202 // If not a user defined descriptor.
1203 if (Slot) {
1204 // Deserialize the fields.
1205 DIDeserializeVisitor DRAM(*this, GV);
1206 DRAM.ApplyToFields(Slot);
1207 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001208
1209 return Slot;
1210}
1211
1212//===----------------------------------------------------------------------===//
1213
1214/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001215///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001216const PointerType *DISerializer::getStrPtrType() {
1217 // If not already defined.
1218 if (!StrPtrTy) {
1219 // Construct the pointer to signed bytes.
1220 StrPtrTy = PointerType::get(Type::SByteTy);
1221 }
1222
1223 return StrPtrTy;
1224}
1225
1226/// getEmptyStructPtrType - Return a "{ }*" type.
1227///
1228const PointerType *DISerializer::getEmptyStructPtrType() {
1229 // If not already defined.
1230 if (!EmptyStructPtrTy) {
1231 // Construct the empty structure type.
1232 const StructType *EmptyStructTy =
1233 StructType::get(std::vector<const Type*>());
1234 // Construct the pointer to empty structure type.
1235 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1236 }
1237
1238 return EmptyStructPtrTy;
1239}
1240
1241/// getTagType - Return the type describing the specified descriptor (via tag.)
1242///
1243const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1244 // Attempt to get the previously defined type.
1245 StructType *&Ty = TagTypes[DD->getTag()];
1246
1247 // If not already defined.
1248 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001249 // Set up fields vector.
1250 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001251 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001252 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001253 GTAM.ApplyToFields(DD);
1254
1255 // Construct structured type.
1256 Ty = StructType::get(Fields);
1257
Jim Laskey86cbdba2006-02-06 15:33:21 +00001258 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001259 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001260 }
1261
1262 return Ty;
1263}
1264
1265/// getString - Construct the string as constant string global.
1266///
Jim Laskeyce72b172006-02-11 01:01:30 +00001267Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001268 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001269 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001270 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001271 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001272 // If empty string then use a sbyte* null instead.
1273 if (String.empty()) {
1274 Slot = ConstantPointerNull::get(getStrPtrType());
1275 } else {
1276 // Construct string as an llvm constant.
1277 Constant *ConstStr = ConstantArray::get(String);
1278 // Otherwise create and return a new string global.
1279 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1280 GlobalVariable::InternalLinkage,
1281 ConstStr, "str", M);
1282 StrGV->setSection("llvm.metadata");
1283 // Convert to generic string pointer.
1284 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1285 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001286 return Slot;
1287
Jim Laskey86cbdba2006-02-06 15:33:21 +00001288}
1289
1290/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1291/// so that it can be serialized to a .bc or .ll file.
1292GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1293 // Check if the DebugInfoDesc is already in the map.
1294 GlobalVariable *&Slot = DescGlobals[DD];
1295
1296 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1297 if (Slot) return Slot;
1298
Jim Laskey86cbdba2006-02-06 15:33:21 +00001299 // Get the type associated with the Tag.
1300 const StructType *Ty = getTagType(DD);
1301
1302 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001303 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1304 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001305 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001306
1307 // Insert new GlobalVariable in DescGlobals map.
1308 Slot = GV;
1309
1310 // Set up elements vector
1311 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001312 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001313 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001314 SRAM.ApplyToFields(DD);
1315
1316 // Set the globals initializer.
1317 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1318
1319 return GV;
1320}
1321
1322//===----------------------------------------------------------------------===//
1323
Jim Laskey86cbdba2006-02-06 15:33:21 +00001324/// Verify - Return true if the GlobalVariable appears to be a valid
1325/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001326bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001327 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001328}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001329bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001330 // NULLs are valid.
1331 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001332
Jim Laskey98e04102006-03-26 22:45:20 +00001333 // Check prior validity.
1334 unsigned &ValiditySlot = Validity[GV];
1335
1336 // If visited before then use old state.
1337 if (ValiditySlot) return ValiditySlot == Valid;
1338
1339 // Assume validity for the time being (recursion.)
1340 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001341
1342 // Make sure the global is internal or link once (anchor.)
1343 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1344 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1345 ValiditySlot = Invalid;
1346 return false;
1347 }
Jim Laskey98e04102006-03-26 22:45:20 +00001348
Jim Laskey86cbdba2006-02-06 15:33:21 +00001349 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001350 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001351
1352 // Check for user defined descriptors.
1353 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001354
1355 // If a compile unit we need the debug version.
Jim Laskey9c4447a2006-03-01 20:39:36 +00001356 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001357 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001358 // FIXME - In the short term, changes are too drastic to continue.
Jim Laskey98e04102006-03-26 22:45:20 +00001359 if (DebugVersion != LLVMDebugVersion) {
1360 ValiditySlot = Invalid;
1361 return false;
1362 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001363 }
Jim Laskeyb8509c52006-03-23 18:07:55 +00001364
Jim Laskey86cbdba2006-02-06 15:33:21 +00001365 // Construct an empty DebugInfoDesc.
1366 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001367
1368 // Allow for user defined descriptors.
1369 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001370
1371 // Get the initializer constant.
1372 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1373
1374 // Get the operand count.
1375 unsigned N = CI->getNumOperands();
1376
1377 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001378 unsigned &CountSlot = Counts[Tag];
1379 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001380 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001381 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001382 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001383 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001384 }
1385
Jim Laskey21407982006-03-14 18:37:57 +00001386 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001387 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001388 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001389 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001390 return false;
1391 }
1392
1393 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001394 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001395 VRAM.ApplyToFields(DD);
1396
1397 // Release empty DebugInfoDesc.
1398 delete DD;
1399
Jim Laskey98e04102006-03-26 22:45:20 +00001400 // If fields are not valid.
1401 if (!VRAM.isValid()) {
1402 ValiditySlot = Invalid;
1403 return false;
1404 }
1405
1406 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001407}
1408
1409//===----------------------------------------------------------------------===//
1410
Jim Laskeyb8509c52006-03-23 18:07:55 +00001411DebugScope::~DebugScope() {
1412 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1413 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1414}
1415
1416//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001417
1418MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001419: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001420, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001421, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001422, Directories()
1423, SourceFiles()
1424, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001425, LabelID(0)
1426, ScopeMap()
1427, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001428, FrameMoves()
1429{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001430MachineDebugInfo::~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 Laskey41886992006-04-07 16:34:46 +00001446/// BeginFunction - Begin gathering function debug information.
1447///
1448void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1449 // Coming soon.
1450}
1451
1452/// MachineDebugInfo::EndFunction - Discard function debug information.
1453///
1454void MachineDebugInfo::EndFunction() {
1455 // Clean up scope information.
1456 if (RootScope) {
1457 delete RootScope;
1458 ScopeMap.clear();
1459 RootScope = NULL;
1460 }
1461
1462 // Clean up frame info.
1463 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1464 FrameMoves.clear();
1465}
1466
Jim Laskeyd96185a2006-02-13 12:50:39 +00001467/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001468///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001469// FIXME - use new Value type when available.
1470DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001471 return DR.Deserialize(V);
1472}
1473
1474/// Verify - Verify that a Value is debug information descriptor.
1475///
1476bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001477 return VR.Verify(V);
1478}
1479
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001480/// AnalyzeModule - Scan the module for global debug information.
1481///
1482void MachineDebugInfo::AnalyzeModule(Module &M) {
1483 SetupCompileUnits(M);
1484}
1485
1486/// SetupCompileUnits - Set up the unique vector of compile units.
1487///
1488void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001489 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001490
Jim Laskey0420f2a2006-02-22 19:02:11 +00001491 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1492 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001493 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001494}
1495
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001496/// getCompileUnits - Return a vector of debug compile units.
1497///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001498const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001499 return CompileUnits;
1500}
1501
Jim Laskey0420f2a2006-02-22 19:02:11 +00001502/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1503/// named GlobalVariable.
1504std::vector<GlobalVariable*>
1505MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1506 const std::string &RootName) {
1507 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001508}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001509
1510/// RecordLabel - Records location information and associates it with a
1511/// debug label. Returns a unique label ID used to generate a label and
1512/// provide correspondence to the source line list.
1513unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1514 unsigned Source) {
1515 unsigned ID = NextLabelID();
1516 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1517 return ID;
1518}
1519
1520/// RecordSource - Register a source file with debug info. Returns an source
1521/// ID.
1522unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1523 const std::string &Source) {
1524 unsigned DirectoryID = Directories.insert(Directory);
1525 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1526}
1527unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1528 return RecordSource(CompileUnit->getDirectory(),
1529 CompileUnit->getFileName());
1530}
1531
1532/// RecordRegionStart - Indicate the start of a region.
1533///
1534unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1535 // FIXME - need to be able to handle split scopes because of bb cloning.
1536 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1537 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1538 unsigned ID = NextLabelID();
1539 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1540 return ID;
1541}
1542
1543/// RecordRegionEnd - Indicate the end of a region.
1544///
1545unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1546 // FIXME - need to be able to handle split scopes because of bb cloning.
1547 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1548 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1549 unsigned ID = NextLabelID();
1550 Scope->setEndLabelID(ID);
1551 return ID;
1552}
1553
1554/// RecordVariable - Indicate the declaration of a local variable.
1555///
1556void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1557 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1558 DebugScope *Scope = getOrCreateScope(VD->getContext());
1559 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1560 Scope->AddVariable(DV);
1561}
1562
1563/// getOrCreateScope - Returns the scope associated with the given descriptor.
1564///
1565DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1566 DebugScope *&Slot = ScopeMap[ScopeDesc];
1567 if (!Slot) {
1568 // FIXME - breaks down when the context is an inlined function.
1569 DebugInfoDesc *ParentDesc = NULL;
1570 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1571 ParentDesc = Block->getContext();
1572 }
1573 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1574 Slot = new DebugScope(Parent, ScopeDesc);
1575 if (Parent) {
1576 Parent->AddScope(Slot);
1577 } else if (RootScope) {
1578 // FIXME - Add inlined function scopes to the root so we can delete
1579 // them later. Long term, handle inlined functions properly.
1580 RootScope->AddScope(Slot);
1581 } else {
1582 // First function is top level function.
1583 RootScope = Slot;
1584 }
1585 }
1586 return Slot;
1587}
1588
Jim Laskeyb8509c52006-03-23 18:07:55 +00001589