blob: c1aac50573a64aa7a3d292cde8b976a6e52a0e30 [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) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000265 Elements.push_back(ConstantSInt::get(Type::LongTy, Field));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000266 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000267 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000268 Elements.push_back(ConstantUInt::get(Type::ULongTy, Field));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000269 }
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) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000348 Fields.push_back(Type::LongTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000349 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000350 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000351 Fields.push_back(Type::ULongTy);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000352 }
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 Laskeyed4e5662006-06-14 14:45:39 +0000458/// TagFromGlobal - Returns the tag number from a debug info descriptor
459/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000460unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
461 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey7089f452006-06-16 13:14:03 +0000462 return C ? ((unsigned)C->getValue() & ~LLVMDebugVersionMask) :
463 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000464}
465
466/// VersionFromGlobal - Returns the version number from a debug info
467/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
468/// int.
469unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
470 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey7089f452006-06-16 13:14:03 +0000471 return C ? ((unsigned)C->getValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000472 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000473}
474
475/// DescFactory - Create an instance of debug info descriptor based on Tag.
476/// Return NULL if not a recognized Tag.
477DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
478 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000479 case DW_TAG_anchor: return new AnchorDesc();
480 case DW_TAG_compile_unit: return new CompileUnitDesc();
481 case DW_TAG_variable: return new GlobalVariableDesc();
482 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000483 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000484 case DW_TAG_base_type: return new BasicTypeDesc();
485 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000486 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000487 case DW_TAG_reference_type:
488 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000489 case DW_TAG_volatile_type:
490 case DW_TAG_restrict_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000491 case DW_TAG_member: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000492 case DW_TAG_array_type:
493 case DW_TAG_structure_type:
494 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000495 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000496 case DW_TAG_vector_type:
497 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000498 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000499 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000500 case DW_TAG_return_variable:
501 case DW_TAG_arg_variable:
502 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000503 default: break;
504 }
505 return NULL;
506}
507
508/// getLinkage - get linkage appropriate for this type of descriptor.
509///
510GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
511 return GlobalValue::InternalLinkage;
512}
513
514/// ApplyToFields - Target the vistor to the fields of the descriptor.
515///
516void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
517 Visitor->Apply(Tag);
518}
519
520//===----------------------------------------------------------------------===//
521
Jim Laskey9c4447a2006-03-01 20:39:36 +0000522AnchorDesc::AnchorDesc()
523: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000524, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000525{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000526AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000527: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000528, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000529{}
530
531// Implement isa/cast/dyncast.
532bool AnchorDesc::classof(const DebugInfoDesc *D) {
533 return D->getTag() == DW_TAG_anchor;
534}
535
Jim Laskeyce72b172006-02-11 01:01:30 +0000536/// getLinkage - get linkage appropriate for this type of descriptor.
537///
538GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
539 return GlobalValue::LinkOnceLinkage;
540}
541
542/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
543///
544void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
545 DebugInfoDesc::ApplyToFields(Visitor);
546
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000547 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000548}
549
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000550/// getDescString - Return a string used to compose global names and labels. A
551/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000552/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000553/// to the list of names left external in the internalizer.
554/// ExternalNames.insert("llvm.dbg.compile_units");
555/// ExternalNames.insert("llvm.dbg.global_variables");
556/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000557const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000558 switch (AnchorTag) {
559 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
560 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
561 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
562 default: break;
563 }
564
565 assert(0 && "Tag does not have a case for anchor string");
566 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000567}
568
569/// getTypeString - Return a string used to label this descriptors type.
570///
571const char *AnchorDesc::getTypeString() const {
572 return "llvm.dbg.anchor.type";
573}
574
575#ifndef NDEBUG
576void AnchorDesc::dump() {
577 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000578 << "Version(" << getVersion() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000579 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000580 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000581}
582#endif
583
584//===----------------------------------------------------------------------===//
585
586AnchoredDesc::AnchoredDesc(unsigned T)
587: DebugInfoDesc(T)
588, Anchor(NULL)
589{}
590
591/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
592///
593void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
594 DebugInfoDesc::ApplyToFields(Visitor);
595
Jim Laskey7089f452006-06-16 13:14:03 +0000596 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000597}
598
599//===----------------------------------------------------------------------===//
600
601CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000602: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000603, Language(0)
604, FileName("")
605, Directory("")
606, Producer("")
607{}
608
Jim Laskey9c4447a2006-03-01 20:39:36 +0000609// Implement isa/cast/dyncast.
610bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
611 return D->getTag() == DW_TAG_compile_unit;
612}
613
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000614/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
615///
616void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000617 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000618
619 // Handle cases out of sync with compiler.
620 if (getVersion() == 0) {
621 unsigned DebugVersion;
622 Visitor->Apply(DebugVersion);
623 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000624
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000625 Visitor->Apply(Language);
626 Visitor->Apply(FileName);
627 Visitor->Apply(Directory);
628 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000629}
630
Jim Laskeyce72b172006-02-11 01:01:30 +0000631/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000632///
Jim Laskeyce72b172006-02-11 01:01:30 +0000633const char *CompileUnitDesc::getDescString() const {
634 return "llvm.dbg.compile_unit";
635}
636
637/// getTypeString - Return a string used to label this descriptors type.
638///
639const char *CompileUnitDesc::getTypeString() const {
640 return "llvm.dbg.compile_unit.type";
641}
642
643/// getAnchorString - Return a string used to label this descriptor's anchor.
644///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000645const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000646const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000647 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000648}
649
Jim Laskey86cbdba2006-02-06 15:33:21 +0000650#ifndef NDEBUG
651void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000652 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000653 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000654 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000655 << "Anchor(" << getAnchor() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000656 << "Language(" << Language << "), "
657 << "FileName(\"" << FileName << "\"), "
658 << "Directory(\"" << Directory << "\"), "
659 << "Producer(\"" << Producer << "\")\n";
660}
661#endif
662
663//===----------------------------------------------------------------------===//
664
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000665TypeDesc::TypeDesc(unsigned T)
666: DebugInfoDesc(T)
667, Context(NULL)
668, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000669, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000670, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000671, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000672, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000673, Offset(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000674{}
675
Jim Laskey69906002006-02-24 16:46:40 +0000676/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000677///
678void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
679 DebugInfoDesc::ApplyToFields(Visitor);
680
681 Visitor->Apply(Context);
682 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000683 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000684 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000685 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000686 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000687 Visitor->Apply(Offset);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000688}
689
690/// getDescString - Return a string used to compose global names and labels.
691///
692const char *TypeDesc::getDescString() const {
693 return "llvm.dbg.type";
694}
695
696/// getTypeString - Return a string used to label this descriptor's type.
697///
698const char *TypeDesc::getTypeString() const {
699 return "llvm.dbg.type.type";
700}
701
702#ifndef NDEBUG
703void TypeDesc::dump() {
704 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000705 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000706 << "Tag(" << getTag() << "), "
707 << "Context(" << Context << "), "
708 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000709 << "File(" << File << "), "
710 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000711 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000712 << "Align(" << Align << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000713 << "Offset(" << Offset << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000714}
715#endif
716
717//===----------------------------------------------------------------------===//
718
719BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000720: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000721, Encoding(0)
722{}
723
Jim Laskey9c4447a2006-03-01 20:39:36 +0000724// Implement isa/cast/dyncast.
725bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
726 return D->getTag() == DW_TAG_base_type;
727}
728
Jim Laskey69906002006-02-24 16:46:40 +0000729/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000730///
731void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
732 TypeDesc::ApplyToFields(Visitor);
733
734 Visitor->Apply(Encoding);
735}
736
Jim Laskeyf8913f12006-03-01 17:53:02 +0000737/// getDescString - Return a string used to compose global names and labels.
738///
739const char *BasicTypeDesc::getDescString() const {
740 return "llvm.dbg.basictype";
741}
742
743/// getTypeString - Return a string used to label this descriptor's type.
744///
745const char *BasicTypeDesc::getTypeString() const {
746 return "llvm.dbg.basictype.type";
747}
748
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000749#ifndef NDEBUG
750void BasicTypeDesc::dump() {
751 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000752 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000753 << "Tag(" << getTag() << "), "
754 << "Context(" << getContext() << "), "
755 << "Name(\"" << getName() << "\"), "
756 << "Size(" << getSize() << "), "
757 << "Encoding(" << Encoding << ")\n";
758}
759#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000760
Jim Laskey434b40b2006-02-23 22:37:30 +0000761//===----------------------------------------------------------------------===//
762
Jim Laskey69906002006-02-24 16:46:40 +0000763DerivedTypeDesc::DerivedTypeDesc(unsigned T)
764: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000765, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000766{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000767
Jim Laskey9c4447a2006-03-01 20:39:36 +0000768// Implement isa/cast/dyncast.
769bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
770 unsigned T = D->getTag();
771 switch (T) {
772 case DW_TAG_typedef:
773 case DW_TAG_pointer_type:
774 case DW_TAG_reference_type:
775 case DW_TAG_const_type:
776 case DW_TAG_volatile_type:
777 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000778 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000779 return true;
780 default: break;
781 }
782 return false;
783}
784
Jim Laskey69906002006-02-24 16:46:40 +0000785/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000786///
Jim Laskey69906002006-02-24 16:46:40 +0000787void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000788 TypeDesc::ApplyToFields(Visitor);
789
Jim Laskey7089f452006-06-16 13:14:03 +0000790 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000791}
792
Jim Laskeyf8913f12006-03-01 17:53:02 +0000793/// getDescString - Return a string used to compose global names and labels.
794///
795const char *DerivedTypeDesc::getDescString() const {
796 return "llvm.dbg.derivedtype";
797}
798
799/// getTypeString - Return a string used to label this descriptor's type.
800///
801const char *DerivedTypeDesc::getTypeString() const {
802 return "llvm.dbg.derivedtype.type";
803}
804
Jim Laskey434b40b2006-02-23 22:37:30 +0000805#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000806void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000807 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000808 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000809 << "Tag(" << getTag() << "), "
810 << "Context(" << getContext() << "), "
811 << "Name(\"" << getName() << "\"), "
812 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000813 << "File(" << getFile() << "), "
814 << "Line(" << getLine() << "), "
815 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000816}
817#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000818
819//===----------------------------------------------------------------------===//
820
Jim Laskeyf8913f12006-03-01 17:53:02 +0000821CompositeTypeDesc::CompositeTypeDesc(unsigned T)
822: DerivedTypeDesc(T)
823, Elements()
824{}
825
Jim Laskey9c4447a2006-03-01 20:39:36 +0000826// Implement isa/cast/dyncast.
827bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
828 unsigned T = D->getTag();
829 switch (T) {
830 case DW_TAG_array_type:
831 case DW_TAG_structure_type:
832 case DW_TAG_union_type:
833 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000834 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000835 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000836 return true;
837 default: break;
838 }
839 return false;
840}
841
Jim Laskeyf8913f12006-03-01 17:53:02 +0000842/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
843///
844void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000845 DerivedTypeDesc::ApplyToFields(Visitor);
846
Jim Laskeyf8913f12006-03-01 17:53:02 +0000847 Visitor->Apply(Elements);
848}
849
850/// getDescString - Return a string used to compose global names and labels.
851///
852const char *CompositeTypeDesc::getDescString() const {
853 return "llvm.dbg.compositetype";
854}
855
856/// getTypeString - Return a string used to label this descriptor's type.
857///
858const char *CompositeTypeDesc::getTypeString() const {
859 return "llvm.dbg.compositetype.type";
860}
861
862#ifndef NDEBUG
863void CompositeTypeDesc::dump() {
864 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000865 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000866 << "Tag(" << getTag() << "), "
867 << "Context(" << getContext() << "), "
868 << "Name(\"" << getName() << "\"), "
869 << "Size(" << getSize() << "), "
870 << "File(" << getFile() << "), "
871 << "Line(" << getLine() << "), "
872 << "FromType(" << getFromType() << "), "
873 << "Elements.size(" << Elements.size() << ")\n";
874}
875#endif
876
877//===----------------------------------------------------------------------===//
878
879SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000880: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000881, Lo(0)
882, Hi(0)
883{}
884
Jim Laskey9c4447a2006-03-01 20:39:36 +0000885// Implement isa/cast/dyncast.
886bool SubrangeDesc::classof(const DebugInfoDesc *D) {
887 return D->getTag() == DW_TAG_subrange_type;
888}
889
Jim Laskeyf8913f12006-03-01 17:53:02 +0000890/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
891///
892void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
893 DebugInfoDesc::ApplyToFields(Visitor);
894
895 Visitor->Apply(Lo);
896 Visitor->Apply(Hi);
897}
898
899/// getDescString - Return a string used to compose global names and labels.
900///
901const char *SubrangeDesc::getDescString() const {
902 return "llvm.dbg.subrange";
903}
904
905/// getTypeString - Return a string used to label this descriptor's type.
906///
907const char *SubrangeDesc::getTypeString() const {
908 return "llvm.dbg.subrange.type";
909}
910
911#ifndef NDEBUG
912void SubrangeDesc::dump() {
913 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000914 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000915 << "Tag(" << getTag() << "), "
916 << "Lo(" << Lo << "), "
917 << "Hi(" << Hi << ")\n";
918}
919#endif
920
921//===----------------------------------------------------------------------===//
922
Jim Laskey6a3eb012006-03-01 23:52:37 +0000923EnumeratorDesc::EnumeratorDesc()
924: DebugInfoDesc(DW_TAG_enumerator)
925, Name("")
926, Value(0)
927{}
928
929// Implement isa/cast/dyncast.
930bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
931 return D->getTag() == DW_TAG_enumerator;
932}
933
934/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
935///
936void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
937 DebugInfoDesc::ApplyToFields(Visitor);
938
939 Visitor->Apply(Name);
940 Visitor->Apply(Value);
941}
942
943/// getDescString - Return a string used to compose global names and labels.
944///
945const char *EnumeratorDesc::getDescString() const {
946 return "llvm.dbg.enumerator";
947}
948
949/// getTypeString - Return a string used to label this descriptor's type.
950///
951const char *EnumeratorDesc::getTypeString() const {
952 return "llvm.dbg.enumerator.type";
953}
954
955#ifndef NDEBUG
956void EnumeratorDesc::dump() {
957 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000958 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000959 << "Tag(" << getTag() << "), "
960 << "Name(" << Name << "), "
961 << "Value(" << Value << ")\n";
962}
963#endif
964
965//===----------------------------------------------------------------------===//
966
Jim Laskeyb8509c52006-03-23 18:07:55 +0000967VariableDesc::VariableDesc(unsigned T)
968: DebugInfoDesc(T)
969, Context(NULL)
970, Name("")
971, File(NULL)
972, Line(0)
973, TyDesc(0)
974{}
975
976// Implement isa/cast/dyncast.
977bool VariableDesc::classof(const DebugInfoDesc *D) {
978 unsigned T = D->getTag();
979 switch (T) {
980 case DW_TAG_auto_variable:
981 case DW_TAG_arg_variable:
982 case DW_TAG_return_variable:
983 return true;
984 default: break;
985 }
986 return false;
987}
988
989/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
990///
991void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
992 DebugInfoDesc::ApplyToFields(Visitor);
993
994 Visitor->Apply(Context);
995 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000996 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000997 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +0000998 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +0000999}
1000
1001/// getDescString - Return a string used to compose global names and labels.
1002///
1003const char *VariableDesc::getDescString() const {
1004 return "llvm.dbg.variable";
1005}
1006
1007/// getTypeString - Return a string used to label this descriptor's type.
1008///
1009const char *VariableDesc::getTypeString() const {
1010 return "llvm.dbg.variable.type";
1011}
1012
1013#ifndef NDEBUG
1014void VariableDesc::dump() {
1015 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001016 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001017 << "Tag(" << getTag() << "), "
1018 << "Context(" << Context << "), "
1019 << "Name(\"" << Name << "\"), "
1020 << "File(" << File << "), "
1021 << "Line(" << Line << "), "
1022 << "TyDesc(" << TyDesc << ")\n";
1023}
1024#endif
1025
1026//===----------------------------------------------------------------------===//
1027
Jim Laskeyce72b172006-02-11 01:01:30 +00001028GlobalDesc::GlobalDesc(unsigned T)
1029: AnchoredDesc(T)
1030, Context(0)
1031, Name("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001032, File(NULL)
1033, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001034, TyDesc(NULL)
1035, IsStatic(false)
1036, IsDefinition(false)
1037{}
1038
1039/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001040///
Jim Laskeyce72b172006-02-11 01:01:30 +00001041void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1042 AnchoredDesc::ApplyToFields(Visitor);
1043
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001044 Visitor->Apply(Context);
1045 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001046 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001047 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001048 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001049 Visitor->Apply(IsStatic);
1050 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001051}
1052
1053//===----------------------------------------------------------------------===//
1054
1055GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001056: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001057, Global(NULL)
1058{}
1059
Jim Laskey9c4447a2006-03-01 20:39:36 +00001060// Implement isa/cast/dyncast.
1061bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1062 return D->getTag() == DW_TAG_variable;
1063}
1064
Jim Laskeyce72b172006-02-11 01:01:30 +00001065/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1066///
1067void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1068 GlobalDesc::ApplyToFields(Visitor);
1069
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001070 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001071}
1072
Jim Laskeyce72b172006-02-11 01:01:30 +00001073/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001074///
Jim Laskeyce72b172006-02-11 01:01:30 +00001075const char *GlobalVariableDesc::getDescString() const {
1076 return "llvm.dbg.global_variable";
1077}
1078
1079/// getTypeString - Return a string used to label this descriptors type.
1080///
1081const char *GlobalVariableDesc::getTypeString() const {
1082 return "llvm.dbg.global_variable.type";
1083}
1084
1085/// getAnchorString - Return a string used to label this descriptor's anchor.
1086///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001087const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001088const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001089 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001090}
1091
Jim Laskey86cbdba2006-02-06 15:33:21 +00001092#ifndef NDEBUG
1093void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001094 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001095 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001096 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001097 << "Anchor(" << getAnchor() << "), "
1098 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001099 << "File(" << getFile() << "),"
1100 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001101 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001102 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1103 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001104 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001105}
1106#endif
1107
1108//===----------------------------------------------------------------------===//
1109
Jim Laskeyce72b172006-02-11 01:01:30 +00001110SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001111: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001112{}
1113
Jim Laskey9c4447a2006-03-01 20:39:36 +00001114// Implement isa/cast/dyncast.
1115bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1116 return D->getTag() == DW_TAG_subprogram;
1117}
1118
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001119/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001120/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001121void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001122 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001123}
1124
Jim Laskeyce72b172006-02-11 01:01:30 +00001125/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001126///
Jim Laskeyce72b172006-02-11 01:01:30 +00001127const char *SubprogramDesc::getDescString() const {
1128 return "llvm.dbg.subprogram";
1129}
1130
1131/// getTypeString - Return a string used to label this descriptors type.
1132///
1133const char *SubprogramDesc::getTypeString() const {
1134 return "llvm.dbg.subprogram.type";
1135}
1136
1137/// getAnchorString - Return a string used to label this descriptor's anchor.
1138///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001139const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001140const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001141 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001142}
1143
Jim Laskey86cbdba2006-02-06 15:33:21 +00001144#ifndef NDEBUG
1145void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001146 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001147 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001148 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001149 << "Anchor(" << getAnchor() << "), "
1150 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001151 << "File(" << getFile() << "),"
1152 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001153 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001154 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1155 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001156}
1157#endif
1158
Jim Laskey45ccae52006-02-28 20:15:07 +00001159//===----------------------------------------------------------------------===//
1160
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001161BlockDesc::BlockDesc()
1162: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001163, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001164{}
1165
1166// Implement isa/cast/dyncast.
1167bool BlockDesc::classof(const DebugInfoDesc *D) {
1168 return D->getTag() == DW_TAG_lexical_block;
1169}
1170
1171/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1172///
1173void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1174 DebugInfoDesc::ApplyToFields(Visitor);
1175
Jim Laskeyb8509c52006-03-23 18:07:55 +00001176 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001177}
1178
1179/// getDescString - Return a string used to compose global names and labels.
1180///
1181const char *BlockDesc::getDescString() const {
1182 return "llvm.dbg.block";
1183}
1184
1185/// getTypeString - Return a string used to label this descriptors type.
1186///
1187const char *BlockDesc::getTypeString() const {
1188 return "llvm.dbg.block.type";
1189}
1190
1191#ifndef NDEBUG
1192void BlockDesc::dump() {
1193 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001194 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001195 << "Tag(" << getTag() << "),"
1196 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001197}
1198#endif
1199
1200//===----------------------------------------------------------------------===//
1201
Jim Laskey86cbdba2006-02-06 15:33:21 +00001202DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001203 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001204}
1205DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001206 // Handle NULL.
1207 if (!GV) return NULL;
1208
Jim Laskey86cbdba2006-02-06 15:33:21 +00001209 // Check to see if it has been already deserialized.
1210 DebugInfoDesc *&Slot = GlobalDescs[GV];
1211 if (Slot) return Slot;
1212
1213 // Get the Tag from the global.
1214 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1215
Jim Laskey86cbdba2006-02-06 15:33:21 +00001216 // Create an empty instance of the correct sort.
1217 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001218
Jim Laskey21407982006-03-14 18:37:57 +00001219 // If not a user defined descriptor.
1220 if (Slot) {
1221 // Deserialize the fields.
1222 DIDeserializeVisitor DRAM(*this, GV);
1223 DRAM.ApplyToFields(Slot);
1224 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001225
1226 return Slot;
1227}
1228
1229//===----------------------------------------------------------------------===//
1230
1231/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001232///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001233const PointerType *DISerializer::getStrPtrType() {
1234 // If not already defined.
1235 if (!StrPtrTy) {
1236 // Construct the pointer to signed bytes.
1237 StrPtrTy = PointerType::get(Type::SByteTy);
1238 }
1239
1240 return StrPtrTy;
1241}
1242
1243/// getEmptyStructPtrType - Return a "{ }*" type.
1244///
1245const PointerType *DISerializer::getEmptyStructPtrType() {
1246 // If not already defined.
1247 if (!EmptyStructPtrTy) {
1248 // Construct the empty structure type.
1249 const StructType *EmptyStructTy =
1250 StructType::get(std::vector<const Type*>());
1251 // Construct the pointer to empty structure type.
1252 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1253 }
1254
1255 return EmptyStructPtrTy;
1256}
1257
1258/// getTagType - Return the type describing the specified descriptor (via tag.)
1259///
1260const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1261 // Attempt to get the previously defined type.
1262 StructType *&Ty = TagTypes[DD->getTag()];
1263
1264 // If not already defined.
1265 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001266 // Set up fields vector.
1267 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001268 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001269 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001270 GTAM.ApplyToFields(DD);
1271
1272 // Construct structured type.
1273 Ty = StructType::get(Fields);
1274
Jim Laskey86cbdba2006-02-06 15:33:21 +00001275 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001276 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001277 }
1278
1279 return Ty;
1280}
1281
1282/// getString - Construct the string as constant string global.
1283///
Jim Laskeyce72b172006-02-11 01:01:30 +00001284Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001285 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001286 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001287 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001288 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001289 // If empty string then use a sbyte* null instead.
1290 if (String.empty()) {
1291 Slot = ConstantPointerNull::get(getStrPtrType());
1292 } else {
1293 // Construct string as an llvm constant.
1294 Constant *ConstStr = ConstantArray::get(String);
1295 // Otherwise create and return a new string global.
1296 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1297 GlobalVariable::InternalLinkage,
1298 ConstStr, "str", M);
1299 StrGV->setSection("llvm.metadata");
1300 // Convert to generic string pointer.
1301 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1302 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001303 return Slot;
1304
Jim Laskey86cbdba2006-02-06 15:33:21 +00001305}
1306
1307/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1308/// so that it can be serialized to a .bc or .ll file.
1309GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1310 // Check if the DebugInfoDesc is already in the map.
1311 GlobalVariable *&Slot = DescGlobals[DD];
1312
1313 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1314 if (Slot) return Slot;
1315
Jim Laskey86cbdba2006-02-06 15:33:21 +00001316 // Get the type associated with the Tag.
1317 const StructType *Ty = getTagType(DD);
1318
1319 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001320 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1321 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001322 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001323
1324 // Insert new GlobalVariable in DescGlobals map.
1325 Slot = GV;
1326
1327 // Set up elements vector
1328 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001329 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001330 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001331 SRAM.ApplyToFields(DD);
1332
1333 // Set the globals initializer.
1334 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1335
1336 return GV;
1337}
1338
1339//===----------------------------------------------------------------------===//
1340
Jim Laskey86cbdba2006-02-06 15:33:21 +00001341/// Verify - Return true if the GlobalVariable appears to be a valid
1342/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001343bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001344 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001345}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001346bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001347 // NULLs are valid.
1348 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001349
Jim Laskey98e04102006-03-26 22:45:20 +00001350 // Check prior validity.
1351 unsigned &ValiditySlot = Validity[GV];
1352
1353 // If visited before then use old state.
1354 if (ValiditySlot) return ValiditySlot == Valid;
1355
1356 // Assume validity for the time being (recursion.)
1357 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001358
1359 // Make sure the global is internal or link once (anchor.)
1360 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1361 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1362 ValiditySlot = Invalid;
1363 return false;
1364 }
Jim Laskey98e04102006-03-26 22:45:20 +00001365
Jim Laskey86cbdba2006-02-06 15:33:21 +00001366 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001367 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001368
1369 // Check for user defined descriptors.
1370 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001371
Jim Laskey86cbdba2006-02-06 15:33:21 +00001372 // Construct an empty DebugInfoDesc.
1373 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001374
1375 // Allow for user defined descriptors.
1376 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001377
1378 // Get the initializer constant.
1379 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1380
1381 // Get the operand count.
1382 unsigned N = CI->getNumOperands();
1383
1384 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001385 unsigned &CountSlot = Counts[Tag];
1386 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001387 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001388 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001389 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001390 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001391 }
1392
Jim Laskey21407982006-03-14 18:37:57 +00001393 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001394 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001395 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001396 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001397 return false;
1398 }
1399
1400 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001401 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001402 VRAM.ApplyToFields(DD);
1403
1404 // Release empty DebugInfoDesc.
1405 delete DD;
1406
Jim Laskey98e04102006-03-26 22:45:20 +00001407 // If fields are not valid.
1408 if (!VRAM.isValid()) {
1409 ValiditySlot = Invalid;
1410 return false;
1411 }
1412
1413 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001414}
1415
1416//===----------------------------------------------------------------------===//
1417
Jim Laskeyb8509c52006-03-23 18:07:55 +00001418DebugScope::~DebugScope() {
1419 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1420 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1421}
1422
1423//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001424
1425MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001426: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001427, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001428, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001429, Directories()
1430, SourceFiles()
1431, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001432, LabelID(0)
1433, ScopeMap()
1434, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001435, FrameMoves()
1436{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001437MachineDebugInfo::~MachineDebugInfo() {
1438
1439}
1440
Jim Laskeyb2efb852006-01-04 22:28:25 +00001441/// doInitialization - Initialize the debug state for a new module.
1442///
1443bool MachineDebugInfo::doInitialization() {
1444 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001445}
1446
Jim Laskeyb2efb852006-01-04 22:28:25 +00001447/// doFinalization - Tear down the debug state after completion of a module.
1448///
1449bool MachineDebugInfo::doFinalization() {
1450 return false;
1451}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001452
Jim Laskey41886992006-04-07 16:34:46 +00001453/// BeginFunction - Begin gathering function debug information.
1454///
1455void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1456 // Coming soon.
1457}
1458
1459/// MachineDebugInfo::EndFunction - Discard function debug information.
1460///
1461void MachineDebugInfo::EndFunction() {
1462 // Clean up scope information.
1463 if (RootScope) {
1464 delete RootScope;
1465 ScopeMap.clear();
1466 RootScope = NULL;
1467 }
1468
1469 // Clean up frame info.
1470 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1471 FrameMoves.clear();
1472}
1473
Jim Laskeyd96185a2006-02-13 12:50:39 +00001474/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001475///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001476// FIXME - use new Value type when available.
1477DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001478 return DR.Deserialize(V);
1479}
1480
1481/// Verify - Verify that a Value is debug information descriptor.
1482///
1483bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001484 return VR.Verify(V);
1485}
1486
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001487/// AnalyzeModule - Scan the module for global debug information.
1488///
1489void MachineDebugInfo::AnalyzeModule(Module &M) {
1490 SetupCompileUnits(M);
1491}
1492
1493/// SetupCompileUnits - Set up the unique vector of compile units.
1494///
1495void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001496 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001497
Jim Laskey0420f2a2006-02-22 19:02:11 +00001498 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1499 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001500 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001501}
1502
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001503/// getCompileUnits - Return a vector of debug compile units.
1504///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001505const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001506 return CompileUnits;
1507}
1508
Jim Laskey0420f2a2006-02-22 19:02:11 +00001509/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1510/// named GlobalVariable.
1511std::vector<GlobalVariable*>
1512MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1513 const std::string &RootName) {
1514 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001515}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001516
1517/// RecordLabel - Records location information and associates it with a
1518/// debug label. Returns a unique label ID used to generate a label and
1519/// provide correspondence to the source line list.
1520unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1521 unsigned Source) {
1522 unsigned ID = NextLabelID();
1523 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1524 return ID;
1525}
1526
1527/// RecordSource - Register a source file with debug info. Returns an source
1528/// ID.
1529unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1530 const std::string &Source) {
1531 unsigned DirectoryID = Directories.insert(Directory);
1532 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1533}
1534unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1535 return RecordSource(CompileUnit->getDirectory(),
1536 CompileUnit->getFileName());
1537}
1538
1539/// RecordRegionStart - Indicate the start of a region.
1540///
1541unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1542 // FIXME - need to be able to handle split scopes because of bb cloning.
1543 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1544 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1545 unsigned ID = NextLabelID();
1546 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1547 return ID;
1548}
1549
1550/// RecordRegionEnd - Indicate the end of a region.
1551///
1552unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1553 // FIXME - need to be able to handle split scopes because of bb cloning.
1554 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1555 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1556 unsigned ID = NextLabelID();
1557 Scope->setEndLabelID(ID);
1558 return ID;
1559}
1560
1561/// RecordVariable - Indicate the declaration of a local variable.
1562///
1563void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1564 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1565 DebugScope *Scope = getOrCreateScope(VD->getContext());
1566 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1567 Scope->AddVariable(DV);
1568}
1569
1570/// getOrCreateScope - Returns the scope associated with the given descriptor.
1571///
1572DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1573 DebugScope *&Slot = ScopeMap[ScopeDesc];
1574 if (!Slot) {
1575 // FIXME - breaks down when the context is an inlined function.
1576 DebugInfoDesc *ParentDesc = NULL;
1577 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1578 ParentDesc = Block->getContext();
1579 }
1580 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1581 Slot = new DebugScope(Parent, ScopeDesc);
1582 if (Parent) {
1583 Parent->AddScope(Slot);
1584 } else if (RootScope) {
1585 // FIXME - Add inlined function scopes to the root so we can delete
1586 // them later. Long term, handle inlined functions properly.
1587 RootScope->AddScope(Slot);
1588 } else {
1589 // First function is top level function.
1590 RootScope = Slot;
1591 }
1592 }
1593 return Slot;
1594}
1595
Jim Laskeyb8509c52006-03-23 18:07:55 +00001596