blob: a3013e9608ec7ad446290e52cbcb9ec1e02ac78c [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 Laskeyed4e5662006-06-14 14:45:39 +0000462 return C ? ((unsigned)C->getValue() & tag_mask) : (unsigned)DW_TAG_invalid;
463}
464
465/// VersionFromGlobal - Returns the version number from a debug info
466/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
467/// int.
468unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
469 ConstantUInt *C = getUIntOperand(GV, 0);
470 return C ? ((unsigned)C->getValue() >> version_shift) :
471 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000472}
473
474/// DescFactory - Create an instance of debug info descriptor based on Tag.
475/// Return NULL if not a recognized Tag.
476DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
477 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000478 case DW_TAG_anchor: return new AnchorDesc();
479 case DW_TAG_compile_unit: return new CompileUnitDesc();
480 case DW_TAG_variable: return new GlobalVariableDesc();
481 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000482 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000483 case DW_TAG_base_type: return new BasicTypeDesc();
484 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000485 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000486 case DW_TAG_reference_type:
487 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000488 case DW_TAG_volatile_type:
489 case DW_TAG_restrict_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000490 case DW_TAG_member: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000491 case DW_TAG_array_type:
492 case DW_TAG_structure_type:
493 case DW_TAG_union_type:
494 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
495 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000496 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000497 case DW_TAG_return_variable:
498 case DW_TAG_arg_variable:
499 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000500 default: break;
501 }
502 return NULL;
503}
504
505/// getLinkage - get linkage appropriate for this type of descriptor.
506///
507GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
508 return GlobalValue::InternalLinkage;
509}
510
511/// ApplyToFields - Target the vistor to the fields of the descriptor.
512///
513void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
514 Visitor->Apply(Tag);
515}
516
517//===----------------------------------------------------------------------===//
518
Jim Laskey9c4447a2006-03-01 20:39:36 +0000519AnchorDesc::AnchorDesc()
520: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000521, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000522{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000523AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000524: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000525, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000526{}
527
528// Implement isa/cast/dyncast.
529bool AnchorDesc::classof(const DebugInfoDesc *D) {
530 return D->getTag() == DW_TAG_anchor;
531}
532
Jim Laskeyce72b172006-02-11 01:01:30 +0000533/// getLinkage - get linkage appropriate for this type of descriptor.
534///
535GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
536 return GlobalValue::LinkOnceLinkage;
537}
538
539/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
540///
541void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
542 DebugInfoDesc::ApplyToFields(Visitor);
543
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000544 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000545}
546
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000547/// getDescString - Return a string used to compose global names and labels. A
548/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000549/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000550/// to the list of names left external in the internalizer.
551/// ExternalNames.insert("llvm.dbg.compile_units");
552/// ExternalNames.insert("llvm.dbg.global_variables");
553/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000554const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000555 switch (AnchorTag) {
556 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
557 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
558 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
559 default: break;
560 }
561
562 assert(0 && "Tag does not have a case for anchor string");
563 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000564}
565
566/// getTypeString - Return a string used to label this descriptors type.
567///
568const char *AnchorDesc::getTypeString() const {
569 return "llvm.dbg.anchor.type";
570}
571
572#ifndef NDEBUG
573void AnchorDesc::dump() {
574 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000575 << "Version(" << getVersion() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000576 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000577 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000578}
579#endif
580
581//===----------------------------------------------------------------------===//
582
583AnchoredDesc::AnchoredDesc(unsigned T)
584: DebugInfoDesc(T)
585, Anchor(NULL)
586{}
587
588/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
589///
590void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
591 DebugInfoDesc::ApplyToFields(Visitor);
592
Reid Spencer60d07ee2006-04-13 18:29:58 +0000593 DebugInfoDesc *Tmp = Anchor;
594 Visitor->Apply(Tmp);
595 Anchor = (AnchorDesc*)Tmp;
Jim Laskeyce72b172006-02-11 01:01:30 +0000596}
597
598//===----------------------------------------------------------------------===//
599
600CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000601: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000602, Language(0)
603, FileName("")
604, Directory("")
605, Producer("")
606{}
607
Jim Laskey9c4447a2006-03-01 20:39:36 +0000608// Implement isa/cast/dyncast.
609bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
610 return D->getTag() == DW_TAG_compile_unit;
611}
612
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000613/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
614///
615void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000616 AnchoredDesc::ApplyToFields(Visitor);
617
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000618 Visitor->Apply(Language);
619 Visitor->Apply(FileName);
620 Visitor->Apply(Directory);
621 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000622}
623
Jim Laskeyce72b172006-02-11 01:01:30 +0000624/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000625///
Jim Laskeyce72b172006-02-11 01:01:30 +0000626const char *CompileUnitDesc::getDescString() const {
627 return "llvm.dbg.compile_unit";
628}
629
630/// getTypeString - Return a string used to label this descriptors type.
631///
632const char *CompileUnitDesc::getTypeString() const {
633 return "llvm.dbg.compile_unit.type";
634}
635
636/// getAnchorString - Return a string used to label this descriptor's anchor.
637///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000638const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000639const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000640 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000641}
642
Jim Laskey86cbdba2006-02-06 15:33:21 +0000643#ifndef NDEBUG
644void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000645 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000646 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000647 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000648 << "Anchor(" << getAnchor() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000649 << "Language(" << Language << "), "
650 << "FileName(\"" << FileName << "\"), "
651 << "Directory(\"" << Directory << "\"), "
652 << "Producer(\"" << Producer << "\")\n";
653}
654#endif
655
656//===----------------------------------------------------------------------===//
657
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000658TypeDesc::TypeDesc(unsigned T)
659: DebugInfoDesc(T)
660, Context(NULL)
661, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000662, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000663, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000664, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000665, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000666, Offset(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000667{}
668
Jim Laskey69906002006-02-24 16:46:40 +0000669/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000670///
671void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
672 DebugInfoDesc::ApplyToFields(Visitor);
673
674 Visitor->Apply(Context);
675 Visitor->Apply(Name);
Reid Spencer60d07ee2006-04-13 18:29:58 +0000676 DebugInfoDesc* Tmp = File;
677 Visitor->Apply(Tmp);
678 File = (CompileUnitDesc*)Tmp;
Jim Laskey69906002006-02-24 16:46:40 +0000679 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000680 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000681 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000682 Visitor->Apply(Offset);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000683}
684
685/// getDescString - Return a string used to compose global names and labels.
686///
687const char *TypeDesc::getDescString() const {
688 return "llvm.dbg.type";
689}
690
691/// getTypeString - Return a string used to label this descriptor's type.
692///
693const char *TypeDesc::getTypeString() const {
694 return "llvm.dbg.type.type";
695}
696
697#ifndef NDEBUG
698void TypeDesc::dump() {
699 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000700 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701 << "Tag(" << getTag() << "), "
702 << "Context(" << Context << "), "
703 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000704 << "File(" << File << "), "
705 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000706 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000707 << "Align(" << Align << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000708 << "Offset(" << Offset << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000709}
710#endif
711
712//===----------------------------------------------------------------------===//
713
714BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000715: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000716, Encoding(0)
717{}
718
Jim Laskey9c4447a2006-03-01 20:39:36 +0000719// Implement isa/cast/dyncast.
720bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
721 return D->getTag() == DW_TAG_base_type;
722}
723
Jim Laskey69906002006-02-24 16:46:40 +0000724/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000725///
726void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
727 TypeDesc::ApplyToFields(Visitor);
728
729 Visitor->Apply(Encoding);
730}
731
Jim Laskeyf8913f12006-03-01 17:53:02 +0000732/// getDescString - Return a string used to compose global names and labels.
733///
734const char *BasicTypeDesc::getDescString() const {
735 return "llvm.dbg.basictype";
736}
737
738/// getTypeString - Return a string used to label this descriptor's type.
739///
740const char *BasicTypeDesc::getTypeString() const {
741 return "llvm.dbg.basictype.type";
742}
743
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000744#ifndef NDEBUG
745void BasicTypeDesc::dump() {
746 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000747 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000748 << "Tag(" << getTag() << "), "
749 << "Context(" << getContext() << "), "
750 << "Name(\"" << getName() << "\"), "
751 << "Size(" << getSize() << "), "
752 << "Encoding(" << Encoding << ")\n";
753}
754#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000755
Jim Laskey434b40b2006-02-23 22:37:30 +0000756//===----------------------------------------------------------------------===//
757
Jim Laskey69906002006-02-24 16:46:40 +0000758DerivedTypeDesc::DerivedTypeDesc(unsigned T)
759: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000760, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000761{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000762
Jim Laskey9c4447a2006-03-01 20:39:36 +0000763// Implement isa/cast/dyncast.
764bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
765 unsigned T = D->getTag();
766 switch (T) {
767 case DW_TAG_typedef:
768 case DW_TAG_pointer_type:
769 case DW_TAG_reference_type:
770 case DW_TAG_const_type:
771 case DW_TAG_volatile_type:
772 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000773 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000774 return true;
775 default: break;
776 }
777 return false;
778}
779
Jim Laskey69906002006-02-24 16:46:40 +0000780/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000781///
Jim Laskey69906002006-02-24 16:46:40 +0000782void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000783 TypeDesc::ApplyToFields(Visitor);
784
Reid Spencer60d07ee2006-04-13 18:29:58 +0000785 DebugInfoDesc* Tmp = FromType;
786 Visitor->Apply(Tmp);
787 FromType = (TypeDesc*)Tmp;
Jim Laskey434b40b2006-02-23 22:37:30 +0000788}
789
Jim Laskeyf8913f12006-03-01 17:53:02 +0000790/// getDescString - Return a string used to compose global names and labels.
791///
792const char *DerivedTypeDesc::getDescString() const {
793 return "llvm.dbg.derivedtype";
794}
795
796/// getTypeString - Return a string used to label this descriptor's type.
797///
798const char *DerivedTypeDesc::getTypeString() const {
799 return "llvm.dbg.derivedtype.type";
800}
801
Jim Laskey434b40b2006-02-23 22:37:30 +0000802#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000803void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000804 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000805 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000806 << "Tag(" << getTag() << "), "
807 << "Context(" << getContext() << "), "
808 << "Name(\"" << getName() << "\"), "
809 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000810 << "File(" << getFile() << "), "
811 << "Line(" << getLine() << "), "
812 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000813}
814#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000815
816//===----------------------------------------------------------------------===//
817
Jim Laskeyf8913f12006-03-01 17:53:02 +0000818CompositeTypeDesc::CompositeTypeDesc(unsigned T)
819: DerivedTypeDesc(T)
Jim Laskeyf8a01a92006-06-15 20:51:43 +0000820, IsVector(false)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000821, Elements()
822{}
823
Jim Laskey9c4447a2006-03-01 20:39:36 +0000824// Implement isa/cast/dyncast.
825bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
826 unsigned T = D->getTag();
827 switch (T) {
828 case DW_TAG_array_type:
829 case DW_TAG_structure_type:
830 case DW_TAG_union_type:
831 case DW_TAG_enumeration_type:
832 return true;
833 default: break;
834 }
835 return false;
836}
837
Jim Laskeyf8913f12006-03-01 17:53:02 +0000838/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
839///
840void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
841 DerivedTypeDesc::ApplyToFields(Visitor);
842
Jim Laskeyf8a01a92006-06-15 20:51:43 +0000843 Visitor->Apply(IsVector);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000844 Visitor->Apply(Elements);
845}
846
847/// getDescString - Return a string used to compose global names and labels.
848///
849const char *CompositeTypeDesc::getDescString() const {
850 return "llvm.dbg.compositetype";
851}
852
853/// getTypeString - Return a string used to label this descriptor's type.
854///
855const char *CompositeTypeDesc::getTypeString() const {
856 return "llvm.dbg.compositetype.type";
857}
858
859#ifndef NDEBUG
860void CompositeTypeDesc::dump() {
861 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000862 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000863 << "Tag(" << getTag() << "), "
864 << "Context(" << getContext() << "), "
865 << "Name(\"" << getName() << "\"), "
866 << "Size(" << getSize() << "), "
867 << "File(" << getFile() << "), "
868 << "Line(" << getLine() << "), "
869 << "FromType(" << getFromType() << "), "
870 << "Elements.size(" << Elements.size() << ")\n";
871}
872#endif
873
874//===----------------------------------------------------------------------===//
875
876SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000877: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000878, Lo(0)
879, Hi(0)
880{}
881
Jim Laskey9c4447a2006-03-01 20:39:36 +0000882// Implement isa/cast/dyncast.
883bool SubrangeDesc::classof(const DebugInfoDesc *D) {
884 return D->getTag() == DW_TAG_subrange_type;
885}
886
Jim Laskeyf8913f12006-03-01 17:53:02 +0000887/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
888///
889void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
890 DebugInfoDesc::ApplyToFields(Visitor);
891
892 Visitor->Apply(Lo);
893 Visitor->Apply(Hi);
894}
895
896/// getDescString - Return a string used to compose global names and labels.
897///
898const char *SubrangeDesc::getDescString() const {
899 return "llvm.dbg.subrange";
900}
901
902/// getTypeString - Return a string used to label this descriptor's type.
903///
904const char *SubrangeDesc::getTypeString() const {
905 return "llvm.dbg.subrange.type";
906}
907
908#ifndef NDEBUG
909void SubrangeDesc::dump() {
910 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000911 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000912 << "Tag(" << getTag() << "), "
913 << "Lo(" << Lo << "), "
914 << "Hi(" << Hi << ")\n";
915}
916#endif
917
918//===----------------------------------------------------------------------===//
919
Jim Laskey6a3eb012006-03-01 23:52:37 +0000920EnumeratorDesc::EnumeratorDesc()
921: DebugInfoDesc(DW_TAG_enumerator)
922, Name("")
923, Value(0)
924{}
925
926// Implement isa/cast/dyncast.
927bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
928 return D->getTag() == DW_TAG_enumerator;
929}
930
931/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
932///
933void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
934 DebugInfoDesc::ApplyToFields(Visitor);
935
936 Visitor->Apply(Name);
937 Visitor->Apply(Value);
938}
939
940/// getDescString - Return a string used to compose global names and labels.
941///
942const char *EnumeratorDesc::getDescString() const {
943 return "llvm.dbg.enumerator";
944}
945
946/// getTypeString - Return a string used to label this descriptor's type.
947///
948const char *EnumeratorDesc::getTypeString() const {
949 return "llvm.dbg.enumerator.type";
950}
951
952#ifndef NDEBUG
953void EnumeratorDesc::dump() {
954 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000955 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000956 << "Tag(" << getTag() << "), "
957 << "Name(" << Name << "), "
958 << "Value(" << Value << ")\n";
959}
960#endif
961
962//===----------------------------------------------------------------------===//
963
Jim Laskeyb8509c52006-03-23 18:07:55 +0000964VariableDesc::VariableDesc(unsigned T)
965: DebugInfoDesc(T)
966, Context(NULL)
967, Name("")
968, File(NULL)
969, Line(0)
970, TyDesc(0)
971{}
972
973// Implement isa/cast/dyncast.
974bool VariableDesc::classof(const DebugInfoDesc *D) {
975 unsigned T = D->getTag();
976 switch (T) {
977 case DW_TAG_auto_variable:
978 case DW_TAG_arg_variable:
979 case DW_TAG_return_variable:
980 return true;
981 default: break;
982 }
983 return false;
984}
985
986/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
987///
988void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
989 DebugInfoDesc::ApplyToFields(Visitor);
990
991 Visitor->Apply(Context);
992 Visitor->Apply(Name);
Reid Spencer60d07ee2006-04-13 18:29:58 +0000993 DebugInfoDesc* Tmp1 = File;
994 Visitor->Apply(Tmp1);
995 File = (CompileUnitDesc*)Tmp1;
Jim Laskeyb8509c52006-03-23 18:07:55 +0000996 Visitor->Apply(Line);
Reid Spencer60d07ee2006-04-13 18:29:58 +0000997 DebugInfoDesc* Tmp2 = TyDesc;
998 Visitor->Apply(Tmp2);
999 TyDesc = (TypeDesc*)Tmp2;
Jim Laskeyb8509c52006-03-23 18:07:55 +00001000}
1001
1002/// getDescString - Return a string used to compose global names and labels.
1003///
1004const char *VariableDesc::getDescString() const {
1005 return "llvm.dbg.variable";
1006}
1007
1008/// getTypeString - Return a string used to label this descriptor's type.
1009///
1010const char *VariableDesc::getTypeString() const {
1011 return "llvm.dbg.variable.type";
1012}
1013
1014#ifndef NDEBUG
1015void VariableDesc::dump() {
1016 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001017 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001018 << "Tag(" << getTag() << "), "
1019 << "Context(" << Context << "), "
1020 << "Name(\"" << Name << "\"), "
1021 << "File(" << File << "), "
1022 << "Line(" << Line << "), "
1023 << "TyDesc(" << TyDesc << ")\n";
1024}
1025#endif
1026
1027//===----------------------------------------------------------------------===//
1028
Jim Laskeyce72b172006-02-11 01:01:30 +00001029GlobalDesc::GlobalDesc(unsigned T)
1030: AnchoredDesc(T)
1031, Context(0)
1032, Name("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001033, File(NULL)
1034, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001035, TyDesc(NULL)
1036, IsStatic(false)
1037, IsDefinition(false)
1038{}
1039
1040/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001041///
Jim Laskeyce72b172006-02-11 01:01:30 +00001042void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1043 AnchoredDesc::ApplyToFields(Visitor);
1044
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001045 Visitor->Apply(Context);
1046 Visitor->Apply(Name);
Reid Spencer60d07ee2006-04-13 18:29:58 +00001047 DebugInfoDesc* Tmp1 = File;
1048 Visitor->Apply(Tmp1);
1049 File = (CompileUnitDesc*)Tmp1;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001050 Visitor->Apply(Line);
Reid Spencer60d07ee2006-04-13 18:29:58 +00001051 DebugInfoDesc* Tmp2 = TyDesc;
1052 Visitor->Apply(Tmp2);
1053 TyDesc = (TypeDesc*)Tmp2;
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 Laskey9d0ff8e2006-03-15 19:09:58 +00001104 << "File(" << getFile() << "),"
1105 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001106 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001107 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1108 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001109 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001110}
1111#endif
1112
1113//===----------------------------------------------------------------------===//
1114
Jim Laskeyce72b172006-02-11 01:01:30 +00001115SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001116: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001117{}
1118
Jim Laskey9c4447a2006-03-01 20:39:36 +00001119// Implement isa/cast/dyncast.
1120bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1121 return D->getTag() == DW_TAG_subprogram;
1122}
1123
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001124/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001125/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001126void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001127 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001128}
1129
Jim Laskeyce72b172006-02-11 01:01:30 +00001130/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001131///
Jim Laskeyce72b172006-02-11 01:01:30 +00001132const char *SubprogramDesc::getDescString() const {
1133 return "llvm.dbg.subprogram";
1134}
1135
1136/// getTypeString - Return a string used to label this descriptors type.
1137///
1138const char *SubprogramDesc::getTypeString() const {
1139 return "llvm.dbg.subprogram.type";
1140}
1141
1142/// getAnchorString - Return a string used to label this descriptor's anchor.
1143///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001144const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001145const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001146 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001147}
1148
Jim Laskey86cbdba2006-02-06 15:33:21 +00001149#ifndef NDEBUG
1150void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001151 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001152 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001153 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001154 << "Anchor(" << getAnchor() << "), "
1155 << "Name(\"" << getName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001156 << "File(" << getFile() << "),"
1157 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001158 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001159 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1160 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001161}
1162#endif
1163
Jim Laskey45ccae52006-02-28 20:15:07 +00001164//===----------------------------------------------------------------------===//
1165
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001166BlockDesc::BlockDesc()
1167: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001168, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001169{}
1170
1171// Implement isa/cast/dyncast.
1172bool BlockDesc::classof(const DebugInfoDesc *D) {
1173 return D->getTag() == DW_TAG_lexical_block;
1174}
1175
1176/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1177///
1178void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1179 DebugInfoDesc::ApplyToFields(Visitor);
1180
Jim Laskeyb8509c52006-03-23 18:07:55 +00001181 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001182}
1183
1184/// getDescString - Return a string used to compose global names and labels.
1185///
1186const char *BlockDesc::getDescString() const {
1187 return "llvm.dbg.block";
1188}
1189
1190/// getTypeString - Return a string used to label this descriptors type.
1191///
1192const char *BlockDesc::getTypeString() const {
1193 return "llvm.dbg.block.type";
1194}
1195
1196#ifndef NDEBUG
1197void BlockDesc::dump() {
1198 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001199 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001200 << "Tag(" << getTag() << "),"
1201 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001202}
1203#endif
1204
1205//===----------------------------------------------------------------------===//
1206
Jim Laskey86cbdba2006-02-06 15:33:21 +00001207DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001208 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001209}
1210DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001211 // Handle NULL.
1212 if (!GV) return NULL;
1213
Jim Laskey86cbdba2006-02-06 15:33:21 +00001214 // Check to see if it has been already deserialized.
1215 DebugInfoDesc *&Slot = GlobalDescs[GV];
1216 if (Slot) return Slot;
1217
1218 // Get the Tag from the global.
1219 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1220
Jim Laskey86cbdba2006-02-06 15:33:21 +00001221 // Create an empty instance of the correct sort.
1222 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001223
Jim Laskey21407982006-03-14 18:37:57 +00001224 // If not a user defined descriptor.
1225 if (Slot) {
1226 // Deserialize the fields.
1227 DIDeserializeVisitor DRAM(*this, GV);
1228 DRAM.ApplyToFields(Slot);
1229 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001230
1231 return Slot;
1232}
1233
1234//===----------------------------------------------------------------------===//
1235
1236/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001237///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001238const PointerType *DISerializer::getStrPtrType() {
1239 // If not already defined.
1240 if (!StrPtrTy) {
1241 // Construct the pointer to signed bytes.
1242 StrPtrTy = PointerType::get(Type::SByteTy);
1243 }
1244
1245 return StrPtrTy;
1246}
1247
1248/// getEmptyStructPtrType - Return a "{ }*" type.
1249///
1250const PointerType *DISerializer::getEmptyStructPtrType() {
1251 // If not already defined.
1252 if (!EmptyStructPtrTy) {
1253 // Construct the empty structure type.
1254 const StructType *EmptyStructTy =
1255 StructType::get(std::vector<const Type*>());
1256 // Construct the pointer to empty structure type.
1257 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1258 }
1259
1260 return EmptyStructPtrTy;
1261}
1262
1263/// getTagType - Return the type describing the specified descriptor (via tag.)
1264///
1265const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1266 // Attempt to get the previously defined type.
1267 StructType *&Ty = TagTypes[DD->getTag()];
1268
1269 // If not already defined.
1270 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001271 // Set up fields vector.
1272 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001273 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001274 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001275 GTAM.ApplyToFields(DD);
1276
1277 // Construct structured type.
1278 Ty = StructType::get(Fields);
1279
Jim Laskey86cbdba2006-02-06 15:33:21 +00001280 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001281 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001282 }
1283
1284 return Ty;
1285}
1286
1287/// getString - Construct the string as constant string global.
1288///
Jim Laskeyce72b172006-02-11 01:01:30 +00001289Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001290 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001291 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001292 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001293 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001294 // If empty string then use a sbyte* null instead.
1295 if (String.empty()) {
1296 Slot = ConstantPointerNull::get(getStrPtrType());
1297 } else {
1298 // Construct string as an llvm constant.
1299 Constant *ConstStr = ConstantArray::get(String);
1300 // Otherwise create and return a new string global.
1301 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1302 GlobalVariable::InternalLinkage,
1303 ConstStr, "str", M);
1304 StrGV->setSection("llvm.metadata");
1305 // Convert to generic string pointer.
1306 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1307 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001308 return Slot;
1309
Jim Laskey86cbdba2006-02-06 15:33:21 +00001310}
1311
1312/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1313/// so that it can be serialized to a .bc or .ll file.
1314GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1315 // Check if the DebugInfoDesc is already in the map.
1316 GlobalVariable *&Slot = DescGlobals[DD];
1317
1318 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1319 if (Slot) return Slot;
1320
Jim Laskey86cbdba2006-02-06 15:33:21 +00001321 // Get the type associated with the Tag.
1322 const StructType *Ty = getTagType(DD);
1323
1324 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001325 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1326 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001327 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001328
1329 // Insert new GlobalVariable in DescGlobals map.
1330 Slot = GV;
1331
1332 // Set up elements vector
1333 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001334 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001335 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001336 SRAM.ApplyToFields(DD);
1337
1338 // Set the globals initializer.
1339 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1340
1341 return GV;
1342}
1343
1344//===----------------------------------------------------------------------===//
1345
Jim Laskey86cbdba2006-02-06 15:33:21 +00001346/// Verify - Return true if the GlobalVariable appears to be a valid
1347/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001348bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001349 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001350}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001351bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001352 // NULLs are valid.
1353 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001354
Jim Laskey98e04102006-03-26 22:45:20 +00001355 // Check prior validity.
1356 unsigned &ValiditySlot = Validity[GV];
1357
1358 // If visited before then use old state.
1359 if (ValiditySlot) return ValiditySlot == Valid;
1360
1361 // Assume validity for the time being (recursion.)
1362 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001363
1364 // Make sure the global is internal or link once (anchor.)
1365 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1366 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1367 ValiditySlot = Invalid;
1368 return false;
1369 }
Jim Laskey98e04102006-03-26 22:45:20 +00001370
Jim Laskey86cbdba2006-02-06 15:33:21 +00001371 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001372 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001373
1374 // Check for user defined descriptors.
1375 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001376
Jim Laskey86cbdba2006-02-06 15:33:21 +00001377 // Construct an empty DebugInfoDesc.
1378 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001379
1380 // Allow for user defined descriptors.
1381 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001382
1383 // Get the initializer constant.
1384 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1385
1386 // Get the operand count.
1387 unsigned N = CI->getNumOperands();
1388
1389 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001390 unsigned &CountSlot = Counts[Tag];
1391 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001392 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001393 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001395 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001396 }
1397
Jim Laskey21407982006-03-14 18:37:57 +00001398 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001399 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001400 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001401 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001402 return false;
1403 }
1404
1405 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001406 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001407 VRAM.ApplyToFields(DD);
1408
1409 // Release empty DebugInfoDesc.
1410 delete DD;
1411
Jim Laskey98e04102006-03-26 22:45:20 +00001412 // If fields are not valid.
1413 if (!VRAM.isValid()) {
1414 ValiditySlot = Invalid;
1415 return false;
1416 }
1417
1418 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001419}
1420
1421//===----------------------------------------------------------------------===//
1422
Jim Laskeyb8509c52006-03-23 18:07:55 +00001423DebugScope::~DebugScope() {
1424 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1425 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1426}
1427
1428//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001429
1430MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001431: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001432, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001433, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001434, Directories()
1435, SourceFiles()
1436, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001437, LabelID(0)
1438, ScopeMap()
1439, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001440, FrameMoves()
1441{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001442MachineDebugInfo::~MachineDebugInfo() {
1443
1444}
1445
Jim Laskeyb2efb852006-01-04 22:28:25 +00001446/// doInitialization - Initialize the debug state for a new module.
1447///
1448bool MachineDebugInfo::doInitialization() {
1449 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001450}
1451
Jim Laskeyb2efb852006-01-04 22:28:25 +00001452/// doFinalization - Tear down the debug state after completion of a module.
1453///
1454bool MachineDebugInfo::doFinalization() {
1455 return false;
1456}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001457
Jim Laskey41886992006-04-07 16:34:46 +00001458/// BeginFunction - Begin gathering function debug information.
1459///
1460void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1461 // Coming soon.
1462}
1463
1464/// MachineDebugInfo::EndFunction - Discard function debug information.
1465///
1466void MachineDebugInfo::EndFunction() {
1467 // Clean up scope information.
1468 if (RootScope) {
1469 delete RootScope;
1470 ScopeMap.clear();
1471 RootScope = NULL;
1472 }
1473
1474 // Clean up frame info.
1475 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1476 FrameMoves.clear();
1477}
1478
Jim Laskeyd96185a2006-02-13 12:50:39 +00001479/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001480///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001481// FIXME - use new Value type when available.
1482DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001483 return DR.Deserialize(V);
1484}
1485
1486/// Verify - Verify that a Value is debug information descriptor.
1487///
1488bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001489 return VR.Verify(V);
1490}
1491
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001492/// AnalyzeModule - Scan the module for global debug information.
1493///
1494void MachineDebugInfo::AnalyzeModule(Module &M) {
1495 SetupCompileUnits(M);
1496}
1497
1498/// SetupCompileUnits - Set up the unique vector of compile units.
1499///
1500void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001501 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001502
Jim Laskey0420f2a2006-02-22 19:02:11 +00001503 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1504 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001505 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001506}
1507
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001508/// getCompileUnits - Return a vector of debug compile units.
1509///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001510const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001511 return CompileUnits;
1512}
1513
Jim Laskey0420f2a2006-02-22 19:02:11 +00001514/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1515/// named GlobalVariable.
1516std::vector<GlobalVariable*>
1517MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1518 const std::string &RootName) {
1519 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001520}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001521
1522/// RecordLabel - Records location information and associates it with a
1523/// debug label. Returns a unique label ID used to generate a label and
1524/// provide correspondence to the source line list.
1525unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1526 unsigned Source) {
1527 unsigned ID = NextLabelID();
1528 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1529 return ID;
1530}
1531
1532/// RecordSource - Register a source file with debug info. Returns an source
1533/// ID.
1534unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1535 const std::string &Source) {
1536 unsigned DirectoryID = Directories.insert(Directory);
1537 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1538}
1539unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1540 return RecordSource(CompileUnit->getDirectory(),
1541 CompileUnit->getFileName());
1542}
1543
1544/// RecordRegionStart - Indicate the start of a region.
1545///
1546unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1547 // FIXME - need to be able to handle split scopes because of bb cloning.
1548 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1549 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1550 unsigned ID = NextLabelID();
1551 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1552 return ID;
1553}
1554
1555/// RecordRegionEnd - Indicate the end of a region.
1556///
1557unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1558 // FIXME - need to be able to handle split scopes because of bb cloning.
1559 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1560 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1561 unsigned ID = NextLabelID();
1562 Scope->setEndLabelID(ID);
1563 return ID;
1564}
1565
1566/// RecordVariable - Indicate the declaration of a local variable.
1567///
1568void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1569 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1570 DebugScope *Scope = getOrCreateScope(VD->getContext());
1571 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1572 Scope->AddVariable(DV);
1573}
1574
1575/// getOrCreateScope - Returns the scope associated with the given descriptor.
1576///
1577DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1578 DebugScope *&Slot = ScopeMap[ScopeDesc];
1579 if (!Slot) {
1580 // FIXME - breaks down when the context is an inlined function.
1581 DebugInfoDesc *ParentDesc = NULL;
1582 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1583 ParentDesc = Block->getContext();
1584 }
1585 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1586 Slot = new DebugScope(Parent, ScopeDesc);
1587 if (Parent) {
1588 Parent->AddScope(Slot);
1589 } else if (RootScope) {
1590 // FIXME - Add inlined function scopes to the root so we can delete
1591 // them later. Long term, handle inlined functions properly.
1592 RootScope->AddScope(Slot);
1593 } else {
1594 // First function is top level function.
1595 RootScope = Slot;
1596 }
1597 }
1598 return Slot;
1599}
1600
Jim Laskeyb8509c52006-03-23 18:07:55 +00001601