blob: 54fb1b923cad78ed1b72bd7e3238905b90ca54a9 [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 Laskeye2a78f22006-07-11 15:58:09 +0000674, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000675{}
676
Jim Laskey69906002006-02-24 16:46:40 +0000677/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000678///
679void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
680 DebugInfoDesc::ApplyToFields(Visitor);
681
682 Visitor->Apply(Context);
683 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000684 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000685 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000686 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000687 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000688 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000689 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000690}
691
692/// getDescString - Return a string used to compose global names and labels.
693///
694const char *TypeDesc::getDescString() const {
695 return "llvm.dbg.type";
696}
697
698/// getTypeString - Return a string used to label this descriptor's type.
699///
700const char *TypeDesc::getTypeString() const {
701 return "llvm.dbg.type.type";
702}
703
704#ifndef NDEBUG
705void TypeDesc::dump() {
706 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000707 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000708 << "Tag(" << getTag() << "), "
709 << "Context(" << Context << "), "
710 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000711 << "File(" << File << "), "
712 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000713 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000714 << "Align(" << Align << "), "
Jim Laskeye2a78f22006-07-11 15:58:09 +0000715 << "Offset(" << Offset << "), "
716 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000717}
718#endif
719
720//===----------------------------------------------------------------------===//
721
722BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000723: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000724, Encoding(0)
725{}
726
Jim Laskey9c4447a2006-03-01 20:39:36 +0000727// Implement isa/cast/dyncast.
728bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
729 return D->getTag() == DW_TAG_base_type;
730}
731
Jim Laskey69906002006-02-24 16:46:40 +0000732/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000733///
734void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
735 TypeDesc::ApplyToFields(Visitor);
736
737 Visitor->Apply(Encoding);
738}
739
Jim Laskeyf8913f12006-03-01 17:53:02 +0000740/// getDescString - Return a string used to compose global names and labels.
741///
742const char *BasicTypeDesc::getDescString() const {
743 return "llvm.dbg.basictype";
744}
745
746/// getTypeString - Return a string used to label this descriptor's type.
747///
748const char *BasicTypeDesc::getTypeString() const {
749 return "llvm.dbg.basictype.type";
750}
751
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000752#ifndef NDEBUG
753void BasicTypeDesc::dump() {
754 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000755 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000756 << "Tag(" << getTag() << "), "
757 << "Context(" << getContext() << "), "
758 << "Name(\"" << getName() << "\"), "
759 << "Size(" << getSize() << "), "
760 << "Encoding(" << Encoding << ")\n";
761}
762#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000763
Jim Laskey434b40b2006-02-23 22:37:30 +0000764//===----------------------------------------------------------------------===//
765
Jim Laskey69906002006-02-24 16:46:40 +0000766DerivedTypeDesc::DerivedTypeDesc(unsigned T)
767: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000768, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000769{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000770
Jim Laskey9c4447a2006-03-01 20:39:36 +0000771// Implement isa/cast/dyncast.
772bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
773 unsigned T = D->getTag();
774 switch (T) {
775 case DW_TAG_typedef:
776 case DW_TAG_pointer_type:
777 case DW_TAG_reference_type:
778 case DW_TAG_const_type:
779 case DW_TAG_volatile_type:
780 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000781 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000782 return true;
783 default: break;
784 }
785 return false;
786}
787
Jim Laskey69906002006-02-24 16:46:40 +0000788/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000789///
Jim Laskey69906002006-02-24 16:46:40 +0000790void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000791 TypeDesc::ApplyToFields(Visitor);
792
Jim Laskey7089f452006-06-16 13:14:03 +0000793 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000794}
795
Jim Laskeyf8913f12006-03-01 17:53:02 +0000796/// getDescString - Return a string used to compose global names and labels.
797///
798const char *DerivedTypeDesc::getDescString() const {
799 return "llvm.dbg.derivedtype";
800}
801
802/// getTypeString - Return a string used to label this descriptor's type.
803///
804const char *DerivedTypeDesc::getTypeString() const {
805 return "llvm.dbg.derivedtype.type";
806}
807
Jim Laskey434b40b2006-02-23 22:37:30 +0000808#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000809void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000810 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000811 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000812 << "Tag(" << getTag() << "), "
813 << "Context(" << getContext() << "), "
814 << "Name(\"" << getName() << "\"), "
815 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000816 << "File(" << getFile() << "), "
817 << "Line(" << getLine() << "), "
818 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000819}
820#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000821
822//===----------------------------------------------------------------------===//
823
Jim Laskeyf8913f12006-03-01 17:53:02 +0000824CompositeTypeDesc::CompositeTypeDesc(unsigned T)
825: DerivedTypeDesc(T)
826, Elements()
827{}
828
Jim Laskey9c4447a2006-03-01 20:39:36 +0000829// Implement isa/cast/dyncast.
830bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
831 unsigned T = D->getTag();
832 switch (T) {
833 case DW_TAG_array_type:
834 case DW_TAG_structure_type:
835 case DW_TAG_union_type:
836 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000837 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000838 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000839 return true;
840 default: break;
841 }
842 return false;
843}
844
Jim Laskeyf8913f12006-03-01 17:53:02 +0000845/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
846///
847void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000848 DerivedTypeDesc::ApplyToFields(Visitor);
849
Jim Laskeyf8913f12006-03-01 17:53:02 +0000850 Visitor->Apply(Elements);
851}
852
853/// getDescString - Return a string used to compose global names and labels.
854///
855const char *CompositeTypeDesc::getDescString() const {
856 return "llvm.dbg.compositetype";
857}
858
859/// getTypeString - Return a string used to label this descriptor's type.
860///
861const char *CompositeTypeDesc::getTypeString() const {
862 return "llvm.dbg.compositetype.type";
863}
864
865#ifndef NDEBUG
866void CompositeTypeDesc::dump() {
867 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000868 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000869 << "Tag(" << getTag() << "), "
870 << "Context(" << getContext() << "), "
871 << "Name(\"" << getName() << "\"), "
872 << "Size(" << getSize() << "), "
873 << "File(" << getFile() << "), "
874 << "Line(" << getLine() << "), "
875 << "FromType(" << getFromType() << "), "
876 << "Elements.size(" << Elements.size() << ")\n";
877}
878#endif
879
880//===----------------------------------------------------------------------===//
881
882SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000883: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000884, Lo(0)
885, Hi(0)
886{}
887
Jim Laskey9c4447a2006-03-01 20:39:36 +0000888// Implement isa/cast/dyncast.
889bool SubrangeDesc::classof(const DebugInfoDesc *D) {
890 return D->getTag() == DW_TAG_subrange_type;
891}
892
Jim Laskeyf8913f12006-03-01 17:53:02 +0000893/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
894///
895void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
896 DebugInfoDesc::ApplyToFields(Visitor);
897
898 Visitor->Apply(Lo);
899 Visitor->Apply(Hi);
900}
901
902/// getDescString - Return a string used to compose global names and labels.
903///
904const char *SubrangeDesc::getDescString() const {
905 return "llvm.dbg.subrange";
906}
907
908/// getTypeString - Return a string used to label this descriptor's type.
909///
910const char *SubrangeDesc::getTypeString() const {
911 return "llvm.dbg.subrange.type";
912}
913
914#ifndef NDEBUG
915void SubrangeDesc::dump() {
916 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000917 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000918 << "Tag(" << getTag() << "), "
919 << "Lo(" << Lo << "), "
920 << "Hi(" << Hi << ")\n";
921}
922#endif
923
924//===----------------------------------------------------------------------===//
925
Jim Laskey6a3eb012006-03-01 23:52:37 +0000926EnumeratorDesc::EnumeratorDesc()
927: DebugInfoDesc(DW_TAG_enumerator)
928, Name("")
929, Value(0)
930{}
931
932// Implement isa/cast/dyncast.
933bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
934 return D->getTag() == DW_TAG_enumerator;
935}
936
937/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
938///
939void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
940 DebugInfoDesc::ApplyToFields(Visitor);
941
942 Visitor->Apply(Name);
943 Visitor->Apply(Value);
944}
945
946/// getDescString - Return a string used to compose global names and labels.
947///
948const char *EnumeratorDesc::getDescString() const {
949 return "llvm.dbg.enumerator";
950}
951
952/// getTypeString - Return a string used to label this descriptor's type.
953///
954const char *EnumeratorDesc::getTypeString() const {
955 return "llvm.dbg.enumerator.type";
956}
957
958#ifndef NDEBUG
959void EnumeratorDesc::dump() {
960 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000961 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000962 << "Tag(" << getTag() << "), "
963 << "Name(" << Name << "), "
964 << "Value(" << Value << ")\n";
965}
966#endif
967
968//===----------------------------------------------------------------------===//
969
Jim Laskeyb8509c52006-03-23 18:07:55 +0000970VariableDesc::VariableDesc(unsigned T)
971: DebugInfoDesc(T)
972, Context(NULL)
973, Name("")
974, File(NULL)
975, Line(0)
976, TyDesc(0)
977{}
978
979// Implement isa/cast/dyncast.
980bool VariableDesc::classof(const DebugInfoDesc *D) {
981 unsigned T = D->getTag();
982 switch (T) {
983 case DW_TAG_auto_variable:
984 case DW_TAG_arg_variable:
985 case DW_TAG_return_variable:
986 return true;
987 default: break;
988 }
989 return false;
990}
991
992/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
993///
994void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
995 DebugInfoDesc::ApplyToFields(Visitor);
996
997 Visitor->Apply(Context);
998 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000999 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001000 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001001 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001002}
1003
1004/// getDescString - Return a string used to compose global names and labels.
1005///
1006const char *VariableDesc::getDescString() const {
1007 return "llvm.dbg.variable";
1008}
1009
1010/// getTypeString - Return a string used to label this descriptor's type.
1011///
1012const char *VariableDesc::getTypeString() const {
1013 return "llvm.dbg.variable.type";
1014}
1015
1016#ifndef NDEBUG
1017void VariableDesc::dump() {
1018 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001019 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001020 << "Tag(" << getTag() << "), "
1021 << "Context(" << Context << "), "
1022 << "Name(\"" << Name << "\"), "
1023 << "File(" << File << "), "
1024 << "Line(" << Line << "), "
1025 << "TyDesc(" << TyDesc << ")\n";
1026}
1027#endif
1028
1029//===----------------------------------------------------------------------===//
1030
Jim Laskeyce72b172006-02-11 01:01:30 +00001031GlobalDesc::GlobalDesc(unsigned T)
1032: AnchoredDesc(T)
1033, Context(0)
1034, Name("")
Jim Laskeye2a78f22006-07-11 15:58:09 +00001035, DisplayName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001036, File(NULL)
1037, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001038, TyDesc(NULL)
1039, IsStatic(false)
1040, IsDefinition(false)
1041{}
1042
1043/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001044///
Jim Laskeyce72b172006-02-11 01:01:30 +00001045void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1046 AnchoredDesc::ApplyToFields(Visitor);
1047
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001048 Visitor->Apply(Context);
1049 Visitor->Apply(Name);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001050 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
Jim Laskey7089f452006-06-16 13:14:03 +00001051 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001052 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001053 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001054 Visitor->Apply(IsStatic);
1055 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001056}
1057
1058//===----------------------------------------------------------------------===//
1059
1060GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001061: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001062, Global(NULL)
1063{}
1064
Jim Laskey9c4447a2006-03-01 20:39:36 +00001065// Implement isa/cast/dyncast.
1066bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1067 return D->getTag() == DW_TAG_variable;
1068}
1069
Jim Laskeyce72b172006-02-11 01:01:30 +00001070/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1071///
1072void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1073 GlobalDesc::ApplyToFields(Visitor);
1074
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001075 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001076}
1077
Jim Laskeyce72b172006-02-11 01:01:30 +00001078/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001079///
Jim Laskeyce72b172006-02-11 01:01:30 +00001080const char *GlobalVariableDesc::getDescString() const {
1081 return "llvm.dbg.global_variable";
1082}
1083
1084/// getTypeString - Return a string used to label this descriptors type.
1085///
1086const char *GlobalVariableDesc::getTypeString() const {
1087 return "llvm.dbg.global_variable.type";
1088}
1089
1090/// getAnchorString - Return a string used to label this descriptor's anchor.
1091///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001092const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001093const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001094 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001095}
1096
Jim Laskey86cbdba2006-02-06 15:33:21 +00001097#ifndef NDEBUG
1098void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001099 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001100 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001101 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001102 << "Anchor(" << getAnchor() << "), "
1103 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001104 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001105 << "File(" << getFile() << "),"
1106 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001107 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001108 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1109 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001110 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001111}
1112#endif
1113
1114//===----------------------------------------------------------------------===//
1115
Jim Laskeyce72b172006-02-11 01:01:30 +00001116SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001117: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001118{}
1119
Jim Laskey9c4447a2006-03-01 20:39:36 +00001120// Implement isa/cast/dyncast.
1121bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1122 return D->getTag() == DW_TAG_subprogram;
1123}
1124
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001125/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001126/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001127void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001128 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001129}
1130
Jim Laskeyce72b172006-02-11 01:01:30 +00001131/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001132///
Jim Laskeyce72b172006-02-11 01:01:30 +00001133const char *SubprogramDesc::getDescString() const {
1134 return "llvm.dbg.subprogram";
1135}
1136
1137/// getTypeString - Return a string used to label this descriptors type.
1138///
1139const char *SubprogramDesc::getTypeString() const {
1140 return "llvm.dbg.subprogram.type";
1141}
1142
1143/// getAnchorString - Return a string used to label this descriptor's anchor.
1144///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001145const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001146const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001147 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001148}
1149
Jim Laskey86cbdba2006-02-06 15:33:21 +00001150#ifndef NDEBUG
1151void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001152 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001153 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001154 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001155 << "Anchor(" << getAnchor() << "), "
1156 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001157 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001158 << "File(" << getFile() << "),"
1159 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001160 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001161 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1162 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001163}
1164#endif
1165
Jim Laskey45ccae52006-02-28 20:15:07 +00001166//===----------------------------------------------------------------------===//
1167
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001168BlockDesc::BlockDesc()
1169: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001170, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001171{}
1172
1173// Implement isa/cast/dyncast.
1174bool BlockDesc::classof(const DebugInfoDesc *D) {
1175 return D->getTag() == DW_TAG_lexical_block;
1176}
1177
1178/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1179///
1180void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1181 DebugInfoDesc::ApplyToFields(Visitor);
1182
Jim Laskeyb8509c52006-03-23 18:07:55 +00001183 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001184}
1185
1186/// getDescString - Return a string used to compose global names and labels.
1187///
1188const char *BlockDesc::getDescString() const {
1189 return "llvm.dbg.block";
1190}
1191
1192/// getTypeString - Return a string used to label this descriptors type.
1193///
1194const char *BlockDesc::getTypeString() const {
1195 return "llvm.dbg.block.type";
1196}
1197
1198#ifndef NDEBUG
1199void BlockDesc::dump() {
1200 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001201 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001202 << "Tag(" << getTag() << "),"
1203 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001204}
1205#endif
1206
1207//===----------------------------------------------------------------------===//
1208
Jim Laskey86cbdba2006-02-06 15:33:21 +00001209DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001210 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001211}
1212DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001213 // Handle NULL.
1214 if (!GV) return NULL;
1215
Jim Laskey86cbdba2006-02-06 15:33:21 +00001216 // Check to see if it has been already deserialized.
1217 DebugInfoDesc *&Slot = GlobalDescs[GV];
1218 if (Slot) return Slot;
1219
1220 // Get the Tag from the global.
1221 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1222
Jim Laskey86cbdba2006-02-06 15:33:21 +00001223 // Create an empty instance of the correct sort.
1224 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001225
Jim Laskey21407982006-03-14 18:37:57 +00001226 // If not a user defined descriptor.
1227 if (Slot) {
1228 // Deserialize the fields.
1229 DIDeserializeVisitor DRAM(*this, GV);
1230 DRAM.ApplyToFields(Slot);
1231 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001232
1233 return Slot;
1234}
1235
1236//===----------------------------------------------------------------------===//
1237
1238/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001239///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001240const PointerType *DISerializer::getStrPtrType() {
1241 // If not already defined.
1242 if (!StrPtrTy) {
1243 // Construct the pointer to signed bytes.
1244 StrPtrTy = PointerType::get(Type::SByteTy);
1245 }
1246
1247 return StrPtrTy;
1248}
1249
1250/// getEmptyStructPtrType - Return a "{ }*" type.
1251///
1252const PointerType *DISerializer::getEmptyStructPtrType() {
1253 // If not already defined.
1254 if (!EmptyStructPtrTy) {
1255 // Construct the empty structure type.
1256 const StructType *EmptyStructTy =
1257 StructType::get(std::vector<const Type*>());
1258 // Construct the pointer to empty structure type.
1259 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1260 }
1261
1262 return EmptyStructPtrTy;
1263}
1264
1265/// getTagType - Return the type describing the specified descriptor (via tag.)
1266///
1267const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1268 // Attempt to get the previously defined type.
1269 StructType *&Ty = TagTypes[DD->getTag()];
1270
1271 // If not already defined.
1272 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001273 // Set up fields vector.
1274 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001275 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001276 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001277 GTAM.ApplyToFields(DD);
1278
1279 // Construct structured type.
1280 Ty = StructType::get(Fields);
1281
Jim Laskey86cbdba2006-02-06 15:33:21 +00001282 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001283 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001284 }
1285
1286 return Ty;
1287}
1288
1289/// getString - Construct the string as constant string global.
1290///
Jim Laskeyce72b172006-02-11 01:01:30 +00001291Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001292 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001293 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001294 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001295 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001296 // If empty string then use a sbyte* null instead.
1297 if (String.empty()) {
1298 Slot = ConstantPointerNull::get(getStrPtrType());
1299 } else {
1300 // Construct string as an llvm constant.
1301 Constant *ConstStr = ConstantArray::get(String);
1302 // Otherwise create and return a new string global.
1303 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1304 GlobalVariable::InternalLinkage,
1305 ConstStr, "str", M);
1306 StrGV->setSection("llvm.metadata");
1307 // Convert to generic string pointer.
1308 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1309 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001310 return Slot;
1311
Jim Laskey86cbdba2006-02-06 15:33:21 +00001312}
1313
1314/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1315/// so that it can be serialized to a .bc or .ll file.
1316GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1317 // Check if the DebugInfoDesc is already in the map.
1318 GlobalVariable *&Slot = DescGlobals[DD];
1319
1320 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1321 if (Slot) return Slot;
1322
Jim Laskey86cbdba2006-02-06 15:33:21 +00001323 // Get the type associated with the Tag.
1324 const StructType *Ty = getTagType(DD);
1325
1326 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001327 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1328 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001329 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001330
1331 // Insert new GlobalVariable in DescGlobals map.
1332 Slot = GV;
1333
1334 // Set up elements vector
1335 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001336 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001337 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001338 SRAM.ApplyToFields(DD);
1339
1340 // Set the globals initializer.
1341 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1342
1343 return GV;
1344}
1345
1346//===----------------------------------------------------------------------===//
1347
Jim Laskey86cbdba2006-02-06 15:33:21 +00001348/// Verify - Return true if the GlobalVariable appears to be a valid
1349/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001350bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001351 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001352}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001353bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001354 // NULLs are valid.
1355 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001356
Jim Laskey98e04102006-03-26 22:45:20 +00001357 // Check prior validity.
1358 unsigned &ValiditySlot = Validity[GV];
1359
1360 // If visited before then use old state.
1361 if (ValiditySlot) return ValiditySlot == Valid;
1362
1363 // Assume validity for the time being (recursion.)
1364 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001365
1366 // Make sure the global is internal or link once (anchor.)
1367 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1368 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1369 ValiditySlot = Invalid;
1370 return false;
1371 }
Jim Laskey98e04102006-03-26 22:45:20 +00001372
Jim Laskey86cbdba2006-02-06 15:33:21 +00001373 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001374 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001375
1376 // Check for user defined descriptors.
1377 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001378
Jim Laskey86cbdba2006-02-06 15:33:21 +00001379 // Construct an empty DebugInfoDesc.
1380 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001381
1382 // Allow for user defined descriptors.
1383 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001384
1385 // Get the initializer constant.
1386 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1387
1388 // Get the operand count.
1389 unsigned N = CI->getNumOperands();
1390
1391 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001392 unsigned &CountSlot = Counts[Tag];
1393 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001395 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001396 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001397 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001398 }
1399
Jim Laskey21407982006-03-14 18:37:57 +00001400 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001401 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001402 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001403 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001404 return false;
1405 }
1406
1407 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001408 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001409 VRAM.ApplyToFields(DD);
1410
1411 // Release empty DebugInfoDesc.
1412 delete DD;
1413
Jim Laskey98e04102006-03-26 22:45:20 +00001414 // If fields are not valid.
1415 if (!VRAM.isValid()) {
1416 ValiditySlot = Invalid;
1417 return false;
1418 }
1419
1420 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001421}
1422
1423//===----------------------------------------------------------------------===//
1424
Jim Laskeyb8509c52006-03-23 18:07:55 +00001425DebugScope::~DebugScope() {
1426 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1427 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1428}
1429
1430//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001431
1432MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001433: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001434, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001435, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001436, Directories()
1437, SourceFiles()
1438, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001439, LabelID(0)
1440, ScopeMap()
1441, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001442, FrameMoves()
1443{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001444MachineDebugInfo::~MachineDebugInfo() {
1445
1446}
1447
Jim Laskeyb2efb852006-01-04 22:28:25 +00001448/// doInitialization - Initialize the debug state for a new module.
1449///
1450bool MachineDebugInfo::doInitialization() {
1451 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001452}
1453
Jim Laskeyb2efb852006-01-04 22:28:25 +00001454/// doFinalization - Tear down the debug state after completion of a module.
1455///
1456bool MachineDebugInfo::doFinalization() {
1457 return false;
1458}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001459
Jim Laskey41886992006-04-07 16:34:46 +00001460/// BeginFunction - Begin gathering function debug information.
1461///
1462void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1463 // Coming soon.
1464}
1465
1466/// MachineDebugInfo::EndFunction - Discard function debug information.
1467///
1468void MachineDebugInfo::EndFunction() {
1469 // Clean up scope information.
1470 if (RootScope) {
1471 delete RootScope;
1472 ScopeMap.clear();
1473 RootScope = NULL;
1474 }
1475
1476 // Clean up frame info.
1477 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1478 FrameMoves.clear();
1479}
1480
Jim Laskeyd96185a2006-02-13 12:50:39 +00001481/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001482///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001483// FIXME - use new Value type when available.
1484DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001485 return DR.Deserialize(V);
1486}
1487
1488/// Verify - Verify that a Value is debug information descriptor.
1489///
1490bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001491 return VR.Verify(V);
1492}
1493
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001494/// AnalyzeModule - Scan the module for global debug information.
1495///
1496void MachineDebugInfo::AnalyzeModule(Module &M) {
1497 SetupCompileUnits(M);
1498}
1499
1500/// SetupCompileUnits - Set up the unique vector of compile units.
1501///
1502void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001503 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001504
Jim Laskey0420f2a2006-02-22 19:02:11 +00001505 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1506 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001507 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001508}
1509
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001510/// getCompileUnits - Return a vector of debug compile units.
1511///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001512const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001513 return CompileUnits;
1514}
1515
Jim Laskey0420f2a2006-02-22 19:02:11 +00001516/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1517/// named GlobalVariable.
1518std::vector<GlobalVariable*>
1519MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1520 const std::string &RootName) {
1521 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001522}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001523
1524/// RecordLabel - Records location information and associates it with a
1525/// debug label. Returns a unique label ID used to generate a label and
1526/// provide correspondence to the source line list.
1527unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1528 unsigned Source) {
1529 unsigned ID = NextLabelID();
1530 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1531 return ID;
1532}
1533
1534/// RecordSource - Register a source file with debug info. Returns an source
1535/// ID.
1536unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1537 const std::string &Source) {
1538 unsigned DirectoryID = Directories.insert(Directory);
1539 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1540}
1541unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1542 return RecordSource(CompileUnit->getDirectory(),
1543 CompileUnit->getFileName());
1544}
1545
1546/// RecordRegionStart - Indicate the start of a region.
1547///
1548unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1549 // FIXME - need to be able to handle split scopes because of bb cloning.
1550 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1551 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1552 unsigned ID = NextLabelID();
1553 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1554 return ID;
1555}
1556
1557/// RecordRegionEnd - Indicate the end of a region.
1558///
1559unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1560 // FIXME - need to be able to handle split scopes because of bb cloning.
1561 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1562 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1563 unsigned ID = NextLabelID();
1564 Scope->setEndLabelID(ID);
1565 return ID;
1566}
1567
1568/// RecordVariable - Indicate the declaration of a local variable.
1569///
1570void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1571 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1572 DebugScope *Scope = getOrCreateScope(VD->getContext());
1573 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1574 Scope->AddVariable(DV);
1575}
1576
1577/// getOrCreateScope - Returns the scope associated with the given descriptor.
1578///
1579DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1580 DebugScope *&Slot = ScopeMap[ScopeDesc];
1581 if (!Slot) {
1582 // FIXME - breaks down when the context is an inlined function.
1583 DebugInfoDesc *ParentDesc = NULL;
1584 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1585 ParentDesc = Block->getContext();
1586 }
1587 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1588 Slot = new DebugScope(Parent, ScopeDesc);
1589 if (Parent) {
1590 Parent->AddScope(Slot);
1591 } else if (RootScope) {
1592 // FIXME - Add inlined function scopes to the root so we can delete
1593 // them later. Long term, handle inlined functions properly.
1594 RootScope->AddScope(Slot);
1595 } else {
1596 // First function is top level function.
1597 RootScope = Slot;
1598 }
1599 }
1600 return Slot;
1601}
1602
Jim Laskeyb8509c52006-03-23 18:07:55 +00001603