blob: c87928f44a42eb52b98e8afe663c4fed3f5f7b8e [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) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000226 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000227 Constant *C = CI->getOperand(I++);
228 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000229 if (GV->hasInitializer()) {
230 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
231 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
232 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
233 DebugInfoDesc *DE = DR.Deserialize(GVE);
234 Field.push_back(DE);
235 }
236 } else if (GV->getInitializer()->isNullValue()) {
237 if (const ArrayType *T =
238 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
239 Field.resize(T->getNumElements());
240 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000241 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000242 }
243 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000244};
245
246//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000247/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000248/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000249class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000250private:
251 DISerializer &SR; // Active serializer.
252 std::vector<Constant*> &Elements; // Element accumulator.
253
254public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000255 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
256 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000257 , SR(S)
258 , Elements(E)
259 {}
260
261 /// Apply - Set the value of each of the fields.
262 ///
263 virtual void Apply(int &Field) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000264 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000265 }
266 virtual void Apply(unsigned &Field) {
267 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
268 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000269 virtual void Apply(int64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000270 Elements.push_back(ConstantSInt::get(Type::LongTy, Field));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000271 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000272 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000273 Elements.push_back(ConstantUInt::get(Type::ULongTy, Field));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000274 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000275 virtual void Apply(bool &Field) {
276 Elements.push_back(ConstantBool::get(Field));
277 }
278 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000279 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000280 }
281 virtual void Apply(DebugInfoDesc *&Field) {
282 GlobalVariable *GV = NULL;
283
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000284 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000285 if (Field) GV = SR.Serialize(Field);
286
287 // FIXME - At some point should use specific type.
288 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
289
290 if (GV) {
291 // Set to pointer to global.
292 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
293 } else {
294 // Use NULL.
295 Elements.push_back(ConstantPointerNull::get(EmptyTy));
296 }
297 }
298 virtual void Apply(GlobalVariable *&Field) {
299 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000300 if (Field) {
301 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
302 } else {
303 Elements.push_back(ConstantPointerNull::get(EmptyTy));
304 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000305 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000306 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
307 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
308 unsigned N = Field.size();
309 ArrayType *AT = ArrayType::get(EmptyTy, N);
310 std::vector<Constant *> ArrayElements;
311
312 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000313 if (DebugInfoDesc *Element = Field[i]) {
314 GlobalVariable *GVE = SR.Serialize(Element);
315 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
316 ArrayElements.push_back(cast<Constant>(CE));
317 } else {
318 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
319 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000320 }
321
322 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000323 GlobalVariable *CAGV = new GlobalVariable(AT, true,
324 GlobalValue::InternalLinkage,
325 CA, "llvm.dbg.array",
326 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000327 CAGV->setSection("llvm.metadata");
Jim Laskeyf8913f12006-03-01 17:53:02 +0000328 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000329 Elements.push_back(CAE);
330 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000331};
332
333//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000334/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000335/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000336class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000337private:
338 DISerializer &SR; // Active serializer.
339 std::vector<const Type*> &Fields; // Type accumulator.
340
341public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000342 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
343 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000344 , SR(S)
345 , Fields(F)
346 {}
347
348 /// Apply - Set the value of each of the fields.
349 ///
350 virtual void Apply(int &Field) {
351 Fields.push_back(Type::IntTy);
352 }
353 virtual void Apply(unsigned &Field) {
354 Fields.push_back(Type::UIntTy);
355 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000356 virtual void Apply(int64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000357 Fields.push_back(Type::LongTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000358 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000359 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000360 Fields.push_back(Type::ULongTy);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000361 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000362 virtual void Apply(bool &Field) {
363 Fields.push_back(Type::BoolTy);
364 }
365 virtual void Apply(std::string &Field) {
366 Fields.push_back(SR.getStrPtrType());
367 }
368 virtual void Apply(DebugInfoDesc *&Field) {
369 // FIXME - At some point should use specific type.
370 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
371 Fields.push_back(EmptyTy);
372 }
373 virtual void Apply(GlobalVariable *&Field) {
374 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
375 Fields.push_back(EmptyTy);
376 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000377 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
378 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
379 Fields.push_back(EmptyTy);
380 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000381};
382
383//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000384/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000385/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000386class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000387private:
388 DIVerifier &VR; // Active verifier.
389 bool IsValid; // Validity status.
390 unsigned I; // Current operand index.
391 ConstantStruct *CI; // GlobalVariable constant initializer.
392
393public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000394 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
395 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000396 , VR(V)
397 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000398 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000399 , CI(cast<ConstantStruct>(GV->getInitializer()))
400 {
401 }
402
403 // Accessors.
404 bool isValid() const { return IsValid; }
405
406 /// Apply - Set the value of each of the fields.
407 ///
408 virtual void Apply(int &Field) {
409 Constant *C = CI->getOperand(I++);
410 IsValid = IsValid && isa<ConstantInt>(C);
411 }
412 virtual void Apply(unsigned &Field) {
413 Constant *C = CI->getOperand(I++);
414 IsValid = IsValid && isa<ConstantInt>(C);
415 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000416 virtual void Apply(int64_t &Field) {
417 Constant *C = CI->getOperand(I++);
418 IsValid = IsValid && isa<ConstantInt>(C);
419 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000420 virtual void Apply(uint64_t &Field) {
421 Constant *C = CI->getOperand(I++);
422 IsValid = IsValid && isa<ConstantInt>(C);
423 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000424 virtual void Apply(bool &Field) {
425 Constant *C = CI->getOperand(I++);
426 IsValid = IsValid && isa<ConstantBool>(C);
427 }
428 virtual void Apply(std::string &Field) {
429 Constant *C = CI->getOperand(I++);
Jim Laskey21407982006-03-14 18:37:57 +0000430 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000431 }
432 virtual void Apply(DebugInfoDesc *&Field) {
433 // FIXME - Prepare the correct descriptor.
434 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000435 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000436 }
437 virtual void Apply(GlobalVariable *&Field) {
438 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000439 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000440 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000441 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isGlobalVariable(C);
444 if (!IsValid) return;
445
446 GlobalVariable *GV = getGlobalVariable(C);
447 IsValid = IsValid && GV && GV->hasInitializer();
448 if (!IsValid) return;
449
450 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
451 IsValid = IsValid && CA;
452 if (!IsValid) return;
453
454 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
455 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
456 if (!IsValid) return;
457
458 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
459 VR.Verify(GVE);
460 }
461 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000462};
463
Jim Laskeyce72b172006-02-11 01:01:30 +0000464
Jim Laskey86cbdba2006-02-06 15:33:21 +0000465//===----------------------------------------------------------------------===//
466
Jim Laskeyed4e5662006-06-14 14:45:39 +0000467/// TagFromGlobal - Returns the tag number from a debug info descriptor
468/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000469unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
470 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey7089f452006-06-16 13:14:03 +0000471 return C ? ((unsigned)C->getValue() & ~LLVMDebugVersionMask) :
472 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000473}
474
475/// VersionFromGlobal - Returns the version number from a debug info
476/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
477/// int.
478unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
479 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey7089f452006-06-16 13:14:03 +0000480 return C ? ((unsigned)C->getValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000481 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000482}
483
484/// DescFactory - Create an instance of debug info descriptor based on Tag.
485/// Return NULL if not a recognized Tag.
486DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
487 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000488 case DW_TAG_anchor: return new AnchorDesc();
489 case DW_TAG_compile_unit: return new CompileUnitDesc();
490 case DW_TAG_variable: return new GlobalVariableDesc();
491 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000492 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000493 case DW_TAG_base_type: return new BasicTypeDesc();
494 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000495 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000496 case DW_TAG_reference_type:
497 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000498 case DW_TAG_volatile_type:
499 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000500 case DW_TAG_member:
501 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000502 case DW_TAG_array_type:
503 case DW_TAG_structure_type:
504 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000505 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000506 case DW_TAG_vector_type:
507 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000508 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000509 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000510 case DW_TAG_return_variable:
511 case DW_TAG_arg_variable:
512 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000513 default: break;
514 }
515 return NULL;
516}
517
518/// getLinkage - get linkage appropriate for this type of descriptor.
519///
520GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
521 return GlobalValue::InternalLinkage;
522}
523
524/// ApplyToFields - Target the vistor to the fields of the descriptor.
525///
526void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
527 Visitor->Apply(Tag);
528}
529
530//===----------------------------------------------------------------------===//
531
Jim Laskey9c4447a2006-03-01 20:39:36 +0000532AnchorDesc::AnchorDesc()
533: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000534, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000535{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000536AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000537: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000538, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000539{}
540
541// Implement isa/cast/dyncast.
542bool AnchorDesc::classof(const DebugInfoDesc *D) {
543 return D->getTag() == DW_TAG_anchor;
544}
545
Jim Laskeyce72b172006-02-11 01:01:30 +0000546/// getLinkage - get linkage appropriate for this type of descriptor.
547///
548GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
549 return GlobalValue::LinkOnceLinkage;
550}
551
552/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
553///
554void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
555 DebugInfoDesc::ApplyToFields(Visitor);
556
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000557 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000558}
559
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000560/// getDescString - Return a string used to compose global names and labels. A
561/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000562/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000563/// to the list of names left external in the internalizer.
564/// ExternalNames.insert("llvm.dbg.compile_units");
565/// ExternalNames.insert("llvm.dbg.global_variables");
566/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000567const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000568 switch (AnchorTag) {
569 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
570 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
571 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
572 default: break;
573 }
574
575 assert(0 && "Tag does not have a case for anchor string");
576 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000577}
578
579/// getTypeString - Return a string used to label this descriptors type.
580///
581const char *AnchorDesc::getTypeString() const {
582 return "llvm.dbg.anchor.type";
583}
584
585#ifndef NDEBUG
586void AnchorDesc::dump() {
587 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000588 << "Version(" << getVersion() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000589 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000590 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000591}
592#endif
593
594//===----------------------------------------------------------------------===//
595
596AnchoredDesc::AnchoredDesc(unsigned T)
597: DebugInfoDesc(T)
598, Anchor(NULL)
599{}
600
601/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
602///
603void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
604 DebugInfoDesc::ApplyToFields(Visitor);
605
Jim Laskey7089f452006-06-16 13:14:03 +0000606 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000607}
608
609//===----------------------------------------------------------------------===//
610
611CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000612: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000613, Language(0)
614, FileName("")
615, Directory("")
616, Producer("")
617{}
618
Jim Laskey9c4447a2006-03-01 20:39:36 +0000619// Implement isa/cast/dyncast.
620bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
621 return D->getTag() == DW_TAG_compile_unit;
622}
623
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000624/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
625///
626void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000627 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000628
629 // Handle cases out of sync with compiler.
630 if (getVersion() == 0) {
631 unsigned DebugVersion;
632 Visitor->Apply(DebugVersion);
633 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000634
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000635 Visitor->Apply(Language);
636 Visitor->Apply(FileName);
637 Visitor->Apply(Directory);
638 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000639}
640
Jim Laskeyce72b172006-02-11 01:01:30 +0000641/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000642///
Jim Laskeyce72b172006-02-11 01:01:30 +0000643const char *CompileUnitDesc::getDescString() const {
644 return "llvm.dbg.compile_unit";
645}
646
647/// getTypeString - Return a string used to label this descriptors type.
648///
649const char *CompileUnitDesc::getTypeString() const {
650 return "llvm.dbg.compile_unit.type";
651}
652
653/// getAnchorString - Return a string used to label this descriptor's anchor.
654///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000655const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000656const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000657 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000658}
659
Jim Laskey86cbdba2006-02-06 15:33:21 +0000660#ifndef NDEBUG
661void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000662 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000663 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000664 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000665 << "Anchor(" << getAnchor() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000666 << "Language(" << Language << "), "
667 << "FileName(\"" << FileName << "\"), "
668 << "Directory(\"" << Directory << "\"), "
669 << "Producer(\"" << Producer << "\")\n";
670}
671#endif
672
673//===----------------------------------------------------------------------===//
674
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000675TypeDesc::TypeDesc(unsigned T)
676: DebugInfoDesc(T)
677, Context(NULL)
678, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000679, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000680, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000681, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000682, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000683, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000684, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000685{}
686
Jim Laskey69906002006-02-24 16:46:40 +0000687/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000688///
689void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
690 DebugInfoDesc::ApplyToFields(Visitor);
691
692 Visitor->Apply(Context);
693 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000694 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000695 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000696 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000697 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000698 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000699 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000700}
701
702/// getDescString - Return a string used to compose global names and labels.
703///
704const char *TypeDesc::getDescString() const {
705 return "llvm.dbg.type";
706}
707
708/// getTypeString - Return a string used to label this descriptor's type.
709///
710const char *TypeDesc::getTypeString() const {
711 return "llvm.dbg.type.type";
712}
713
714#ifndef NDEBUG
715void TypeDesc::dump() {
716 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000717 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000718 << "Tag(" << getTag() << "), "
719 << "Context(" << Context << "), "
720 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000721 << "File(" << File << "), "
722 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000723 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000724 << "Align(" << Align << "), "
Jim Laskeye2a78f22006-07-11 15:58:09 +0000725 << "Offset(" << Offset << "), "
726 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000727}
728#endif
729
730//===----------------------------------------------------------------------===//
731
732BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000733: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000734, Encoding(0)
735{}
736
Jim Laskey9c4447a2006-03-01 20:39:36 +0000737// Implement isa/cast/dyncast.
738bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
739 return D->getTag() == DW_TAG_base_type;
740}
741
Jim Laskey69906002006-02-24 16:46:40 +0000742/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000743///
744void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
745 TypeDesc::ApplyToFields(Visitor);
746
747 Visitor->Apply(Encoding);
748}
749
Jim Laskeyf8913f12006-03-01 17:53:02 +0000750/// getDescString - Return a string used to compose global names and labels.
751///
752const char *BasicTypeDesc::getDescString() const {
753 return "llvm.dbg.basictype";
754}
755
756/// getTypeString - Return a string used to label this descriptor's type.
757///
758const char *BasicTypeDesc::getTypeString() const {
759 return "llvm.dbg.basictype.type";
760}
761
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000762#ifndef NDEBUG
763void BasicTypeDesc::dump() {
764 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000765 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000766 << "Tag(" << getTag() << "), "
767 << "Context(" << getContext() << "), "
768 << "Name(\"" << getName() << "\"), "
769 << "Size(" << getSize() << "), "
770 << "Encoding(" << Encoding << ")\n";
771}
772#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000773
Jim Laskey434b40b2006-02-23 22:37:30 +0000774//===----------------------------------------------------------------------===//
775
Jim Laskey69906002006-02-24 16:46:40 +0000776DerivedTypeDesc::DerivedTypeDesc(unsigned T)
777: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000778, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000779{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000780
Jim Laskey9c4447a2006-03-01 20:39:36 +0000781// Implement isa/cast/dyncast.
782bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
783 unsigned T = D->getTag();
784 switch (T) {
785 case DW_TAG_typedef:
786 case DW_TAG_pointer_type:
787 case DW_TAG_reference_type:
788 case DW_TAG_const_type:
789 case DW_TAG_volatile_type:
790 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000791 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000792 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000793 return true;
794 default: break;
795 }
796 return false;
797}
798
Jim Laskey69906002006-02-24 16:46:40 +0000799/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000800///
Jim Laskey69906002006-02-24 16:46:40 +0000801void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000802 TypeDesc::ApplyToFields(Visitor);
803
Jim Laskey7089f452006-06-16 13:14:03 +0000804 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000805}
806
Jim Laskeyf8913f12006-03-01 17:53:02 +0000807/// getDescString - Return a string used to compose global names and labels.
808///
809const char *DerivedTypeDesc::getDescString() const {
810 return "llvm.dbg.derivedtype";
811}
812
813/// getTypeString - Return a string used to label this descriptor's type.
814///
815const char *DerivedTypeDesc::getTypeString() const {
816 return "llvm.dbg.derivedtype.type";
817}
818
Jim Laskey434b40b2006-02-23 22:37:30 +0000819#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000820void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000821 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000822 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000823 << "Tag(" << getTag() << "), "
824 << "Context(" << getContext() << "), "
825 << "Name(\"" << getName() << "\"), "
826 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000827 << "File(" << getFile() << "), "
828 << "Line(" << getLine() << "), "
829 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000830}
831#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000832
833//===----------------------------------------------------------------------===//
834
Jim Laskeyf8913f12006-03-01 17:53:02 +0000835CompositeTypeDesc::CompositeTypeDesc(unsigned T)
836: DerivedTypeDesc(T)
837, Elements()
838{}
839
Jim Laskey9c4447a2006-03-01 20:39:36 +0000840// Implement isa/cast/dyncast.
841bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
842 unsigned T = D->getTag();
843 switch (T) {
844 case DW_TAG_array_type:
845 case DW_TAG_structure_type:
846 case DW_TAG_union_type:
847 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000848 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000849 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000850 return true;
851 default: break;
852 }
853 return false;
854}
855
Jim Laskeyf8913f12006-03-01 17:53:02 +0000856/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
857///
858void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000859 DerivedTypeDesc::ApplyToFields(Visitor);
860
Jim Laskeyf8913f12006-03-01 17:53:02 +0000861 Visitor->Apply(Elements);
862}
863
864/// getDescString - Return a string used to compose global names and labels.
865///
866const char *CompositeTypeDesc::getDescString() const {
867 return "llvm.dbg.compositetype";
868}
869
870/// getTypeString - Return a string used to label this descriptor's type.
871///
872const char *CompositeTypeDesc::getTypeString() const {
873 return "llvm.dbg.compositetype.type";
874}
875
876#ifndef NDEBUG
877void CompositeTypeDesc::dump() {
878 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000879 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000880 << "Tag(" << getTag() << "), "
881 << "Context(" << getContext() << "), "
882 << "Name(\"" << getName() << "\"), "
883 << "Size(" << getSize() << "), "
884 << "File(" << getFile() << "), "
885 << "Line(" << getLine() << "), "
886 << "FromType(" << getFromType() << "), "
887 << "Elements.size(" << Elements.size() << ")\n";
888}
889#endif
890
891//===----------------------------------------------------------------------===//
892
893SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000894: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000895, Lo(0)
896, Hi(0)
897{}
898
Jim Laskey9c4447a2006-03-01 20:39:36 +0000899// Implement isa/cast/dyncast.
900bool SubrangeDesc::classof(const DebugInfoDesc *D) {
901 return D->getTag() == DW_TAG_subrange_type;
902}
903
Jim Laskeyf8913f12006-03-01 17:53:02 +0000904/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
905///
906void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
907 DebugInfoDesc::ApplyToFields(Visitor);
908
909 Visitor->Apply(Lo);
910 Visitor->Apply(Hi);
911}
912
913/// getDescString - Return a string used to compose global names and labels.
914///
915const char *SubrangeDesc::getDescString() const {
916 return "llvm.dbg.subrange";
917}
918
919/// getTypeString - Return a string used to label this descriptor's type.
920///
921const char *SubrangeDesc::getTypeString() const {
922 return "llvm.dbg.subrange.type";
923}
924
925#ifndef NDEBUG
926void SubrangeDesc::dump() {
927 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000928 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000929 << "Tag(" << getTag() << "), "
930 << "Lo(" << Lo << "), "
931 << "Hi(" << Hi << ")\n";
932}
933#endif
934
935//===----------------------------------------------------------------------===//
936
Jim Laskey6a3eb012006-03-01 23:52:37 +0000937EnumeratorDesc::EnumeratorDesc()
938: DebugInfoDesc(DW_TAG_enumerator)
939, Name("")
940, Value(0)
941{}
942
943// Implement isa/cast/dyncast.
944bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
945 return D->getTag() == DW_TAG_enumerator;
946}
947
948/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
949///
950void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
951 DebugInfoDesc::ApplyToFields(Visitor);
952
953 Visitor->Apply(Name);
954 Visitor->Apply(Value);
955}
956
957/// getDescString - Return a string used to compose global names and labels.
958///
959const char *EnumeratorDesc::getDescString() const {
960 return "llvm.dbg.enumerator";
961}
962
963/// getTypeString - Return a string used to label this descriptor's type.
964///
965const char *EnumeratorDesc::getTypeString() const {
966 return "llvm.dbg.enumerator.type";
967}
968
969#ifndef NDEBUG
970void EnumeratorDesc::dump() {
971 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000972 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000973 << "Tag(" << getTag() << "), "
974 << "Name(" << Name << "), "
975 << "Value(" << Value << ")\n";
976}
977#endif
978
979//===----------------------------------------------------------------------===//
980
Jim Laskeyb8509c52006-03-23 18:07:55 +0000981VariableDesc::VariableDesc(unsigned T)
982: DebugInfoDesc(T)
983, Context(NULL)
984, Name("")
985, File(NULL)
986, Line(0)
987, TyDesc(0)
988{}
989
990// Implement isa/cast/dyncast.
991bool VariableDesc::classof(const DebugInfoDesc *D) {
992 unsigned T = D->getTag();
993 switch (T) {
994 case DW_TAG_auto_variable:
995 case DW_TAG_arg_variable:
996 case DW_TAG_return_variable:
997 return true;
998 default: break;
999 }
1000 return false;
1001}
1002
1003/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1004///
1005void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1006 DebugInfoDesc::ApplyToFields(Visitor);
1007
1008 Visitor->Apply(Context);
1009 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001010 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001011 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001012 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001013}
1014
1015/// getDescString - Return a string used to compose global names and labels.
1016///
1017const char *VariableDesc::getDescString() const {
1018 return "llvm.dbg.variable";
1019}
1020
1021/// getTypeString - Return a string used to label this descriptor's type.
1022///
1023const char *VariableDesc::getTypeString() const {
1024 return "llvm.dbg.variable.type";
1025}
1026
1027#ifndef NDEBUG
1028void VariableDesc::dump() {
1029 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001030 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001031 << "Tag(" << getTag() << "), "
1032 << "Context(" << Context << "), "
1033 << "Name(\"" << Name << "\"), "
1034 << "File(" << File << "), "
1035 << "Line(" << Line << "), "
1036 << "TyDesc(" << TyDesc << ")\n";
1037}
1038#endif
1039
1040//===----------------------------------------------------------------------===//
1041
Jim Laskeyce72b172006-02-11 01:01:30 +00001042GlobalDesc::GlobalDesc(unsigned T)
1043: AnchoredDesc(T)
1044, Context(0)
1045, Name("")
Jim Laskeye2a78f22006-07-11 15:58:09 +00001046, DisplayName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001047, File(NULL)
1048, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001049, TyDesc(NULL)
1050, IsStatic(false)
1051, IsDefinition(false)
1052{}
1053
1054/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001055///
Jim Laskeyce72b172006-02-11 01:01:30 +00001056void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1057 AnchoredDesc::ApplyToFields(Visitor);
1058
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001059 Visitor->Apply(Context);
1060 Visitor->Apply(Name);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001061 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
Jim Laskey7089f452006-06-16 13:14:03 +00001062 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001063 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001064 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001065 Visitor->Apply(IsStatic);
1066 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001067}
1068
1069//===----------------------------------------------------------------------===//
1070
1071GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001072: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001073, Global(NULL)
1074{}
1075
Jim Laskey9c4447a2006-03-01 20:39:36 +00001076// Implement isa/cast/dyncast.
1077bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1078 return D->getTag() == DW_TAG_variable;
1079}
1080
Jim Laskeyce72b172006-02-11 01:01:30 +00001081/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1082///
1083void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1084 GlobalDesc::ApplyToFields(Visitor);
1085
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001086 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001087}
1088
Jim Laskeyce72b172006-02-11 01:01:30 +00001089/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001090///
Jim Laskeyce72b172006-02-11 01:01:30 +00001091const char *GlobalVariableDesc::getDescString() const {
1092 return "llvm.dbg.global_variable";
1093}
1094
1095/// getTypeString - Return a string used to label this descriptors type.
1096///
1097const char *GlobalVariableDesc::getTypeString() const {
1098 return "llvm.dbg.global_variable.type";
1099}
1100
1101/// getAnchorString - Return a string used to label this descriptor's anchor.
1102///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001103const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001104const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001105 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001106}
1107
Jim Laskey86cbdba2006-02-06 15:33:21 +00001108#ifndef NDEBUG
1109void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001110 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001111 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001112 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001113 << "Anchor(" << getAnchor() << "), "
1114 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001115 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001116 << "File(" << getFile() << "),"
1117 << "Line(" << getLine() << "),"
Jim Laskey774f8542006-10-13 13:01:34 +00001118 << "Type(" << getType() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001119 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1120 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001121 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001122}
1123#endif
1124
1125//===----------------------------------------------------------------------===//
1126
Jim Laskeyce72b172006-02-11 01:01:30 +00001127SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001128: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001129{}
1130
Jim Laskey9c4447a2006-03-01 20:39:36 +00001131// Implement isa/cast/dyncast.
1132bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1133 return D->getTag() == DW_TAG_subprogram;
1134}
1135
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001136/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001137/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001138void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001139 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001140}
1141
Jim Laskeyce72b172006-02-11 01:01:30 +00001142/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001143///
Jim Laskeyce72b172006-02-11 01:01:30 +00001144const char *SubprogramDesc::getDescString() const {
1145 return "llvm.dbg.subprogram";
1146}
1147
1148/// getTypeString - Return a string used to label this descriptors type.
1149///
1150const char *SubprogramDesc::getTypeString() const {
1151 return "llvm.dbg.subprogram.type";
1152}
1153
1154/// getAnchorString - Return a string used to label this descriptor's anchor.
1155///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001156const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001157const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001158 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001159}
1160
Jim Laskey86cbdba2006-02-06 15:33:21 +00001161#ifndef NDEBUG
1162void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001163 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001164 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001165 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001166 << "Anchor(" << getAnchor() << "), "
1167 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001168 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001169 << "File(" << getFile() << "),"
1170 << "Line(" << getLine() << "),"
Jim Laskey774f8542006-10-13 13:01:34 +00001171 << "Type(" << getType() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001172 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1173 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001174}
1175#endif
1176
Jim Laskey45ccae52006-02-28 20:15:07 +00001177//===----------------------------------------------------------------------===//
1178
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001179BlockDesc::BlockDesc()
1180: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001181, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001182{}
1183
1184// Implement isa/cast/dyncast.
1185bool BlockDesc::classof(const DebugInfoDesc *D) {
1186 return D->getTag() == DW_TAG_lexical_block;
1187}
1188
1189/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1190///
1191void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1192 DebugInfoDesc::ApplyToFields(Visitor);
1193
Jim Laskeyb8509c52006-03-23 18:07:55 +00001194 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001195}
1196
1197/// getDescString - Return a string used to compose global names and labels.
1198///
1199const char *BlockDesc::getDescString() const {
1200 return "llvm.dbg.block";
1201}
1202
1203/// getTypeString - Return a string used to label this descriptors type.
1204///
1205const char *BlockDesc::getTypeString() const {
1206 return "llvm.dbg.block.type";
1207}
1208
1209#ifndef NDEBUG
1210void BlockDesc::dump() {
1211 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001212 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001213 << "Tag(" << getTag() << "),"
1214 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001215}
1216#endif
1217
1218//===----------------------------------------------------------------------===//
1219
Jim Laskey86cbdba2006-02-06 15:33:21 +00001220DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001221 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001222}
1223DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001224 // Handle NULL.
1225 if (!GV) return NULL;
1226
Jim Laskey86cbdba2006-02-06 15:33:21 +00001227 // Check to see if it has been already deserialized.
1228 DebugInfoDesc *&Slot = GlobalDescs[GV];
1229 if (Slot) return Slot;
1230
1231 // Get the Tag from the global.
1232 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1233
Jim Laskey86cbdba2006-02-06 15:33:21 +00001234 // Create an empty instance of the correct sort.
1235 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001236
Jim Laskey21407982006-03-14 18:37:57 +00001237 // If not a user defined descriptor.
1238 if (Slot) {
1239 // Deserialize the fields.
1240 DIDeserializeVisitor DRAM(*this, GV);
1241 DRAM.ApplyToFields(Slot);
1242 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001243
1244 return Slot;
1245}
1246
1247//===----------------------------------------------------------------------===//
1248
1249/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001250///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001251const PointerType *DISerializer::getStrPtrType() {
1252 // If not already defined.
1253 if (!StrPtrTy) {
1254 // Construct the pointer to signed bytes.
1255 StrPtrTy = PointerType::get(Type::SByteTy);
1256 }
1257
1258 return StrPtrTy;
1259}
1260
1261/// getEmptyStructPtrType - Return a "{ }*" type.
1262///
1263const PointerType *DISerializer::getEmptyStructPtrType() {
1264 // If not already defined.
1265 if (!EmptyStructPtrTy) {
1266 // Construct the empty structure type.
1267 const StructType *EmptyStructTy =
1268 StructType::get(std::vector<const Type*>());
1269 // Construct the pointer to empty structure type.
1270 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1271 }
1272
1273 return EmptyStructPtrTy;
1274}
1275
1276/// getTagType - Return the type describing the specified descriptor (via tag.)
1277///
1278const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1279 // Attempt to get the previously defined type.
1280 StructType *&Ty = TagTypes[DD->getTag()];
1281
1282 // If not already defined.
1283 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001284 // Set up fields vector.
1285 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001286 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001287 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001288 GTAM.ApplyToFields(DD);
1289
1290 // Construct structured type.
1291 Ty = StructType::get(Fields);
1292
Jim Laskey86cbdba2006-02-06 15:33:21 +00001293 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001294 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001295 }
1296
1297 return Ty;
1298}
1299
1300/// getString - Construct the string as constant string global.
1301///
Jim Laskeyce72b172006-02-11 01:01:30 +00001302Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001303 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001304 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001305 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001306 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001307 // If empty string then use a sbyte* null instead.
1308 if (String.empty()) {
1309 Slot = ConstantPointerNull::get(getStrPtrType());
1310 } else {
1311 // Construct string as an llvm constant.
1312 Constant *ConstStr = ConstantArray::get(String);
1313 // Otherwise create and return a new string global.
1314 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1315 GlobalVariable::InternalLinkage,
1316 ConstStr, "str", M);
1317 StrGV->setSection("llvm.metadata");
1318 // Convert to generic string pointer.
1319 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1320 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001321 return Slot;
1322
Jim Laskey86cbdba2006-02-06 15:33:21 +00001323}
1324
1325/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1326/// so that it can be serialized to a .bc or .ll file.
1327GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1328 // Check if the DebugInfoDesc is already in the map.
1329 GlobalVariable *&Slot = DescGlobals[DD];
1330
1331 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1332 if (Slot) return Slot;
1333
Jim Laskey86cbdba2006-02-06 15:33:21 +00001334 // Get the type associated with the Tag.
1335 const StructType *Ty = getTagType(DD);
1336
1337 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001338 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1339 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001340 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001341
1342 // Insert new GlobalVariable in DescGlobals map.
1343 Slot = GV;
1344
1345 // Set up elements vector
1346 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001347 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001348 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001349 SRAM.ApplyToFields(DD);
1350
1351 // Set the globals initializer.
1352 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1353
1354 return GV;
1355}
1356
1357//===----------------------------------------------------------------------===//
1358
Jim Laskey86cbdba2006-02-06 15:33:21 +00001359/// Verify - Return true if the GlobalVariable appears to be a valid
1360/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001361bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001362 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001363}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001364bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001365 // NULLs are valid.
1366 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001367
Jim Laskey98e04102006-03-26 22:45:20 +00001368 // Check prior validity.
1369 unsigned &ValiditySlot = Validity[GV];
1370
1371 // If visited before then use old state.
1372 if (ValiditySlot) return ValiditySlot == Valid;
1373
1374 // Assume validity for the time being (recursion.)
1375 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001376
1377 // Make sure the global is internal or link once (anchor.)
1378 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1379 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1380 ValiditySlot = Invalid;
1381 return false;
1382 }
Jim Laskey98e04102006-03-26 22:45:20 +00001383
Jim Laskey86cbdba2006-02-06 15:33:21 +00001384 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001385 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001386
1387 // Check for user defined descriptors.
1388 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001389
Jim Laskey86cbdba2006-02-06 15:33:21 +00001390 // Construct an empty DebugInfoDesc.
1391 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001392
1393 // Allow for user defined descriptors.
1394 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001395
1396 // Get the initializer constant.
1397 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1398
1399 // Get the operand count.
1400 unsigned N = CI->getNumOperands();
1401
1402 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001403 unsigned &CountSlot = Counts[Tag];
1404 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001405 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001406 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001407 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001408 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001409 }
1410
Jim Laskey21407982006-03-14 18:37:57 +00001411 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001412 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001413 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001414 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001415 return false;
1416 }
1417
1418 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001419 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001420 VRAM.ApplyToFields(DD);
1421
1422 // Release empty DebugInfoDesc.
1423 delete DD;
1424
Jim Laskey98e04102006-03-26 22:45:20 +00001425 // If fields are not valid.
1426 if (!VRAM.isValid()) {
1427 ValiditySlot = Invalid;
1428 return false;
1429 }
1430
1431 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001432}
1433
1434//===----------------------------------------------------------------------===//
1435
Jim Laskeyb8509c52006-03-23 18:07:55 +00001436DebugScope::~DebugScope() {
1437 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1438 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1439}
1440
1441//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001442
1443MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001444: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001445, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001446, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001447, Directories()
1448, SourceFiles()
1449, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001450, LabelID(0)
1451, ScopeMap()
1452, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001453, FrameMoves()
1454{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001455MachineDebugInfo::~MachineDebugInfo() {
1456
1457}
1458
Jim Laskeyb2efb852006-01-04 22:28:25 +00001459/// doInitialization - Initialize the debug state for a new module.
1460///
1461bool MachineDebugInfo::doInitialization() {
1462 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001463}
1464
Jim Laskeyb2efb852006-01-04 22:28:25 +00001465/// doFinalization - Tear down the debug state after completion of a module.
1466///
1467bool MachineDebugInfo::doFinalization() {
1468 return false;
1469}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001470
Jim Laskey41886992006-04-07 16:34:46 +00001471/// BeginFunction - Begin gathering function debug information.
1472///
1473void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1474 // Coming soon.
1475}
1476
1477/// MachineDebugInfo::EndFunction - Discard function debug information.
1478///
1479void MachineDebugInfo::EndFunction() {
1480 // Clean up scope information.
1481 if (RootScope) {
1482 delete RootScope;
1483 ScopeMap.clear();
1484 RootScope = NULL;
1485 }
1486
1487 // Clean up frame info.
1488 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1489 FrameMoves.clear();
1490}
1491
Jim Laskeyd96185a2006-02-13 12:50:39 +00001492/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001493///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001494// FIXME - use new Value type when available.
1495DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001496 return DR.Deserialize(V);
1497}
1498
1499/// Verify - Verify that a Value is debug information descriptor.
1500///
1501bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001502 return VR.Verify(V);
1503}
1504
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001505/// AnalyzeModule - Scan the module for global debug information.
1506///
1507void MachineDebugInfo::AnalyzeModule(Module &M) {
1508 SetupCompileUnits(M);
1509}
1510
1511/// SetupCompileUnits - Set up the unique vector of compile units.
1512///
1513void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001514 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001515
Jim Laskey0420f2a2006-02-22 19:02:11 +00001516 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1517 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001518 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001519}
1520
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001521/// getCompileUnits - Return a vector of debug compile units.
1522///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001523const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001524 return CompileUnits;
1525}
1526
Jim Laskey0420f2a2006-02-22 19:02:11 +00001527/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1528/// named GlobalVariable.
1529std::vector<GlobalVariable*>
1530MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1531 const std::string &RootName) {
1532 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001533}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001534
1535/// RecordLabel - Records location information and associates it with a
1536/// debug label. Returns a unique label ID used to generate a label and
1537/// provide correspondence to the source line list.
1538unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1539 unsigned Source) {
1540 unsigned ID = NextLabelID();
1541 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1542 return ID;
1543}
1544
1545/// RecordSource - Register a source file with debug info. Returns an source
1546/// ID.
1547unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1548 const std::string &Source) {
1549 unsigned DirectoryID = Directories.insert(Directory);
1550 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1551}
1552unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1553 return RecordSource(CompileUnit->getDirectory(),
1554 CompileUnit->getFileName());
1555}
1556
1557/// RecordRegionStart - Indicate the start of a region.
1558///
1559unsigned MachineDebugInfo::RecordRegionStart(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 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1565 return ID;
1566}
1567
1568/// RecordRegionEnd - Indicate the end of a region.
1569///
1570unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1571 // FIXME - need to be able to handle split scopes because of bb cloning.
1572 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1573 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1574 unsigned ID = NextLabelID();
1575 Scope->setEndLabelID(ID);
1576 return ID;
1577}
1578
1579/// RecordVariable - Indicate the declaration of a local variable.
1580///
1581void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1582 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1583 DebugScope *Scope = getOrCreateScope(VD->getContext());
1584 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1585 Scope->AddVariable(DV);
1586}
1587
1588/// getOrCreateScope - Returns the scope associated with the given descriptor.
1589///
1590DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1591 DebugScope *&Slot = ScopeMap[ScopeDesc];
1592 if (!Slot) {
1593 // FIXME - breaks down when the context is an inlined function.
1594 DebugInfoDesc *ParentDesc = NULL;
1595 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1596 ParentDesc = Block->getContext();
1597 }
1598 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1599 Slot = new DebugScope(Parent, ScopeDesc);
1600 if (Parent) {
1601 Parent->AddScope(Slot);
1602 } else if (RootScope) {
1603 // FIXME - Add inlined function scopes to the root so we can delete
1604 // them later. Long term, handle inlined functions properly.
1605 RootScope->AddScope(Slot);
1606 } else {
1607 // First function is top level function.
1608 RootScope = Slot;
1609 }
1610 }
1611 return Slot;
1612}
1613
Jim Laskeyb8509c52006-03-23 18:07:55 +00001614