blob: fa2f57f0e0c85db5de23906231db7a4a520b1582 [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 Laskey9d0ff8e2006-03-15 19:09:58 +0000500 case DW_TAG_member: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000501 case DW_TAG_array_type:
502 case DW_TAG_structure_type:
503 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000504 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000505 case DW_TAG_vector_type:
506 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000507 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000508 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000509 case DW_TAG_return_variable:
510 case DW_TAG_arg_variable:
511 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000512 default: break;
513 }
514 return NULL;
515}
516
517/// getLinkage - get linkage appropriate for this type of descriptor.
518///
519GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
520 return GlobalValue::InternalLinkage;
521}
522
523/// ApplyToFields - Target the vistor to the fields of the descriptor.
524///
525void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
526 Visitor->Apply(Tag);
527}
528
529//===----------------------------------------------------------------------===//
530
Jim Laskey9c4447a2006-03-01 20:39:36 +0000531AnchorDesc::AnchorDesc()
532: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000533, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000534{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000535AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000536: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000537, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000538{}
539
540// Implement isa/cast/dyncast.
541bool AnchorDesc::classof(const DebugInfoDesc *D) {
542 return D->getTag() == DW_TAG_anchor;
543}
544
Jim Laskeyce72b172006-02-11 01:01:30 +0000545/// getLinkage - get linkage appropriate for this type of descriptor.
546///
547GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
548 return GlobalValue::LinkOnceLinkage;
549}
550
551/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
552///
553void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
554 DebugInfoDesc::ApplyToFields(Visitor);
555
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000556 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000557}
558
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000559/// getDescString - Return a string used to compose global names and labels. A
560/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000561/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000562/// to the list of names left external in the internalizer.
563/// ExternalNames.insert("llvm.dbg.compile_units");
564/// ExternalNames.insert("llvm.dbg.global_variables");
565/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000566const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000567 switch (AnchorTag) {
568 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
569 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
570 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
571 default: break;
572 }
573
574 assert(0 && "Tag does not have a case for anchor string");
575 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000576}
577
578/// getTypeString - Return a string used to label this descriptors type.
579///
580const char *AnchorDesc::getTypeString() const {
581 return "llvm.dbg.anchor.type";
582}
583
584#ifndef NDEBUG
585void AnchorDesc::dump() {
586 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000587 << "Version(" << getVersion() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000588 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000589 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000590}
591#endif
592
593//===----------------------------------------------------------------------===//
594
595AnchoredDesc::AnchoredDesc(unsigned T)
596: DebugInfoDesc(T)
597, Anchor(NULL)
598{}
599
600/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
601///
602void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
603 DebugInfoDesc::ApplyToFields(Visitor);
604
Jim Laskey7089f452006-06-16 13:14:03 +0000605 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000606}
607
608//===----------------------------------------------------------------------===//
609
610CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000611: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000612, Language(0)
613, FileName("")
614, Directory("")
615, Producer("")
616{}
617
Jim Laskey9c4447a2006-03-01 20:39:36 +0000618// Implement isa/cast/dyncast.
619bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
620 return D->getTag() == DW_TAG_compile_unit;
621}
622
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000623/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
624///
625void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000626 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000627
628 // Handle cases out of sync with compiler.
629 if (getVersion() == 0) {
630 unsigned DebugVersion;
631 Visitor->Apply(DebugVersion);
632 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000633
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000634 Visitor->Apply(Language);
635 Visitor->Apply(FileName);
636 Visitor->Apply(Directory);
637 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000638}
639
Jim Laskeyce72b172006-02-11 01:01:30 +0000640/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000641///
Jim Laskeyce72b172006-02-11 01:01:30 +0000642const char *CompileUnitDesc::getDescString() const {
643 return "llvm.dbg.compile_unit";
644}
645
646/// getTypeString - Return a string used to label this descriptors type.
647///
648const char *CompileUnitDesc::getTypeString() const {
649 return "llvm.dbg.compile_unit.type";
650}
651
652/// getAnchorString - Return a string used to label this descriptor's anchor.
653///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000654const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000655const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000656 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000657}
658
Jim Laskey86cbdba2006-02-06 15:33:21 +0000659#ifndef NDEBUG
660void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000661 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000662 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000663 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000664 << "Anchor(" << getAnchor() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000665 << "Language(" << Language << "), "
666 << "FileName(\"" << FileName << "\"), "
667 << "Directory(\"" << Directory << "\"), "
668 << "Producer(\"" << Producer << "\")\n";
669}
670#endif
671
672//===----------------------------------------------------------------------===//
673
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000674TypeDesc::TypeDesc(unsigned T)
675: DebugInfoDesc(T)
676, Context(NULL)
677, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000678, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000679, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000680, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000681, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000682, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000683, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000684{}
685
Jim Laskey69906002006-02-24 16:46:40 +0000686/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000687///
688void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
689 DebugInfoDesc::ApplyToFields(Visitor);
690
691 Visitor->Apply(Context);
692 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000693 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000694 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000695 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000696 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000697 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000698 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000699}
700
701/// getDescString - Return a string used to compose global names and labels.
702///
703const char *TypeDesc::getDescString() const {
704 return "llvm.dbg.type";
705}
706
707/// getTypeString - Return a string used to label this descriptor's type.
708///
709const char *TypeDesc::getTypeString() const {
710 return "llvm.dbg.type.type";
711}
712
713#ifndef NDEBUG
714void TypeDesc::dump() {
715 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000716 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000717 << "Tag(" << getTag() << "), "
718 << "Context(" << Context << "), "
719 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000720 << "File(" << File << "), "
721 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000722 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000723 << "Align(" << Align << "), "
Jim Laskeye2a78f22006-07-11 15:58:09 +0000724 << "Offset(" << Offset << "), "
725 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000726}
727#endif
728
729//===----------------------------------------------------------------------===//
730
731BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000732: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000733, Encoding(0)
734{}
735
Jim Laskey9c4447a2006-03-01 20:39:36 +0000736// Implement isa/cast/dyncast.
737bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
738 return D->getTag() == DW_TAG_base_type;
739}
740
Jim Laskey69906002006-02-24 16:46:40 +0000741/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000742///
743void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
744 TypeDesc::ApplyToFields(Visitor);
745
746 Visitor->Apply(Encoding);
747}
748
Jim Laskeyf8913f12006-03-01 17:53:02 +0000749/// getDescString - Return a string used to compose global names and labels.
750///
751const char *BasicTypeDesc::getDescString() const {
752 return "llvm.dbg.basictype";
753}
754
755/// getTypeString - Return a string used to label this descriptor's type.
756///
757const char *BasicTypeDesc::getTypeString() const {
758 return "llvm.dbg.basictype.type";
759}
760
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000761#ifndef NDEBUG
762void BasicTypeDesc::dump() {
763 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000764 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000765 << "Tag(" << getTag() << "), "
766 << "Context(" << getContext() << "), "
767 << "Name(\"" << getName() << "\"), "
768 << "Size(" << getSize() << "), "
769 << "Encoding(" << Encoding << ")\n";
770}
771#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000772
Jim Laskey434b40b2006-02-23 22:37:30 +0000773//===----------------------------------------------------------------------===//
774
Jim Laskey69906002006-02-24 16:46:40 +0000775DerivedTypeDesc::DerivedTypeDesc(unsigned T)
776: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000777, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000778{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000779
Jim Laskey9c4447a2006-03-01 20:39:36 +0000780// Implement isa/cast/dyncast.
781bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
782 unsigned T = D->getTag();
783 switch (T) {
784 case DW_TAG_typedef:
785 case DW_TAG_pointer_type:
786 case DW_TAG_reference_type:
787 case DW_TAG_const_type:
788 case DW_TAG_volatile_type:
789 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000790 case DW_TAG_member:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000791 return true;
792 default: break;
793 }
794 return false;
795}
796
Jim Laskey69906002006-02-24 16:46:40 +0000797/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000798///
Jim Laskey69906002006-02-24 16:46:40 +0000799void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000800 TypeDesc::ApplyToFields(Visitor);
801
Jim Laskey7089f452006-06-16 13:14:03 +0000802 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000803}
804
Jim Laskeyf8913f12006-03-01 17:53:02 +0000805/// getDescString - Return a string used to compose global names and labels.
806///
807const char *DerivedTypeDesc::getDescString() const {
808 return "llvm.dbg.derivedtype";
809}
810
811/// getTypeString - Return a string used to label this descriptor's type.
812///
813const char *DerivedTypeDesc::getTypeString() const {
814 return "llvm.dbg.derivedtype.type";
815}
816
Jim Laskey434b40b2006-02-23 22:37:30 +0000817#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000818void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000819 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000820 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000821 << "Tag(" << getTag() << "), "
822 << "Context(" << getContext() << "), "
823 << "Name(\"" << getName() << "\"), "
824 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000825 << "File(" << getFile() << "), "
826 << "Line(" << getLine() << "), "
827 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000828}
829#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000830
831//===----------------------------------------------------------------------===//
832
Jim Laskeyf8913f12006-03-01 17:53:02 +0000833CompositeTypeDesc::CompositeTypeDesc(unsigned T)
834: DerivedTypeDesc(T)
835, Elements()
836{}
837
Jim Laskey9c4447a2006-03-01 20:39:36 +0000838// Implement isa/cast/dyncast.
839bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
840 unsigned T = D->getTag();
841 switch (T) {
842 case DW_TAG_array_type:
843 case DW_TAG_structure_type:
844 case DW_TAG_union_type:
845 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000846 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000847 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000848 return true;
849 default: break;
850 }
851 return false;
852}
853
Jim Laskeyf8913f12006-03-01 17:53:02 +0000854/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
855///
856void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000857 DerivedTypeDesc::ApplyToFields(Visitor);
858
Jim Laskeyf8913f12006-03-01 17:53:02 +0000859 Visitor->Apply(Elements);
860}
861
862/// getDescString - Return a string used to compose global names and labels.
863///
864const char *CompositeTypeDesc::getDescString() const {
865 return "llvm.dbg.compositetype";
866}
867
868/// getTypeString - Return a string used to label this descriptor's type.
869///
870const char *CompositeTypeDesc::getTypeString() const {
871 return "llvm.dbg.compositetype.type";
872}
873
874#ifndef NDEBUG
875void CompositeTypeDesc::dump() {
876 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000877 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000878 << "Tag(" << getTag() << "), "
879 << "Context(" << getContext() << "), "
880 << "Name(\"" << getName() << "\"), "
881 << "Size(" << getSize() << "), "
882 << "File(" << getFile() << "), "
883 << "Line(" << getLine() << "), "
884 << "FromType(" << getFromType() << "), "
885 << "Elements.size(" << Elements.size() << ")\n";
886}
887#endif
888
889//===----------------------------------------------------------------------===//
890
891SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000892: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000893, Lo(0)
894, Hi(0)
895{}
896
Jim Laskey9c4447a2006-03-01 20:39:36 +0000897// Implement isa/cast/dyncast.
898bool SubrangeDesc::classof(const DebugInfoDesc *D) {
899 return D->getTag() == DW_TAG_subrange_type;
900}
901
Jim Laskeyf8913f12006-03-01 17:53:02 +0000902/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
903///
904void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
905 DebugInfoDesc::ApplyToFields(Visitor);
906
907 Visitor->Apply(Lo);
908 Visitor->Apply(Hi);
909}
910
911/// getDescString - Return a string used to compose global names and labels.
912///
913const char *SubrangeDesc::getDescString() const {
914 return "llvm.dbg.subrange";
915}
916
917/// getTypeString - Return a string used to label this descriptor's type.
918///
919const char *SubrangeDesc::getTypeString() const {
920 return "llvm.dbg.subrange.type";
921}
922
923#ifndef NDEBUG
924void SubrangeDesc::dump() {
925 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000926 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000927 << "Tag(" << getTag() << "), "
928 << "Lo(" << Lo << "), "
929 << "Hi(" << Hi << ")\n";
930}
931#endif
932
933//===----------------------------------------------------------------------===//
934
Jim Laskey6a3eb012006-03-01 23:52:37 +0000935EnumeratorDesc::EnumeratorDesc()
936: DebugInfoDesc(DW_TAG_enumerator)
937, Name("")
938, Value(0)
939{}
940
941// Implement isa/cast/dyncast.
942bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
943 return D->getTag() == DW_TAG_enumerator;
944}
945
946/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
947///
948void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
949 DebugInfoDesc::ApplyToFields(Visitor);
950
951 Visitor->Apply(Name);
952 Visitor->Apply(Value);
953}
954
955/// getDescString - Return a string used to compose global names and labels.
956///
957const char *EnumeratorDesc::getDescString() const {
958 return "llvm.dbg.enumerator";
959}
960
961/// getTypeString - Return a string used to label this descriptor's type.
962///
963const char *EnumeratorDesc::getTypeString() const {
964 return "llvm.dbg.enumerator.type";
965}
966
967#ifndef NDEBUG
968void EnumeratorDesc::dump() {
969 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000970 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000971 << "Tag(" << getTag() << "), "
972 << "Name(" << Name << "), "
973 << "Value(" << Value << ")\n";
974}
975#endif
976
977//===----------------------------------------------------------------------===//
978
Jim Laskeyb8509c52006-03-23 18:07:55 +0000979VariableDesc::VariableDesc(unsigned T)
980: DebugInfoDesc(T)
981, Context(NULL)
982, Name("")
983, File(NULL)
984, Line(0)
985, TyDesc(0)
986{}
987
988// Implement isa/cast/dyncast.
989bool VariableDesc::classof(const DebugInfoDesc *D) {
990 unsigned T = D->getTag();
991 switch (T) {
992 case DW_TAG_auto_variable:
993 case DW_TAG_arg_variable:
994 case DW_TAG_return_variable:
995 return true;
996 default: break;
997 }
998 return false;
999}
1000
1001/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1002///
1003void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1004 DebugInfoDesc::ApplyToFields(Visitor);
1005
1006 Visitor->Apply(Context);
1007 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001008 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001009 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001010 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001011}
1012
1013/// getDescString - Return a string used to compose global names and labels.
1014///
1015const char *VariableDesc::getDescString() const {
1016 return "llvm.dbg.variable";
1017}
1018
1019/// getTypeString - Return a string used to label this descriptor's type.
1020///
1021const char *VariableDesc::getTypeString() const {
1022 return "llvm.dbg.variable.type";
1023}
1024
1025#ifndef NDEBUG
1026void VariableDesc::dump() {
1027 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001028 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001029 << "Tag(" << getTag() << "), "
1030 << "Context(" << Context << "), "
1031 << "Name(\"" << Name << "\"), "
1032 << "File(" << File << "), "
1033 << "Line(" << Line << "), "
1034 << "TyDesc(" << TyDesc << ")\n";
1035}
1036#endif
1037
1038//===----------------------------------------------------------------------===//
1039
Jim Laskeyce72b172006-02-11 01:01:30 +00001040GlobalDesc::GlobalDesc(unsigned T)
1041: AnchoredDesc(T)
1042, Context(0)
1043, Name("")
Jim Laskeye2a78f22006-07-11 15:58:09 +00001044, DisplayName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001045, File(NULL)
1046, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001047, TyDesc(NULL)
1048, IsStatic(false)
1049, IsDefinition(false)
1050{}
1051
1052/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001053///
Jim Laskeyce72b172006-02-11 01:01:30 +00001054void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1055 AnchoredDesc::ApplyToFields(Visitor);
1056
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001057 Visitor->Apply(Context);
1058 Visitor->Apply(Name);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001059 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
Jim Laskey7089f452006-06-16 13:14:03 +00001060 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001061 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001062 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001063 Visitor->Apply(IsStatic);
1064 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001065}
1066
1067//===----------------------------------------------------------------------===//
1068
1069GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001070: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001071, Global(NULL)
1072{}
1073
Jim Laskey9c4447a2006-03-01 20:39:36 +00001074// Implement isa/cast/dyncast.
1075bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1076 return D->getTag() == DW_TAG_variable;
1077}
1078
Jim Laskeyce72b172006-02-11 01:01:30 +00001079/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1080///
1081void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1082 GlobalDesc::ApplyToFields(Visitor);
1083
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001084 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001085}
1086
Jim Laskeyce72b172006-02-11 01:01:30 +00001087/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001088///
Jim Laskeyce72b172006-02-11 01:01:30 +00001089const char *GlobalVariableDesc::getDescString() const {
1090 return "llvm.dbg.global_variable";
1091}
1092
1093/// getTypeString - Return a string used to label this descriptors type.
1094///
1095const char *GlobalVariableDesc::getTypeString() const {
1096 return "llvm.dbg.global_variable.type";
1097}
1098
1099/// getAnchorString - Return a string used to label this descriptor's anchor.
1100///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001101const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001102const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001103 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001104}
1105
Jim Laskey86cbdba2006-02-06 15:33:21 +00001106#ifndef NDEBUG
1107void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001108 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001109 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001110 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001111 << "Anchor(" << getAnchor() << "), "
1112 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001113 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001114 << "File(" << getFile() << "),"
1115 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001116 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001117 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1118 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001119 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001120}
1121#endif
1122
1123//===----------------------------------------------------------------------===//
1124
Jim Laskeyce72b172006-02-11 01:01:30 +00001125SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001126: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001127{}
1128
Jim Laskey9c4447a2006-03-01 20:39:36 +00001129// Implement isa/cast/dyncast.
1130bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1131 return D->getTag() == DW_TAG_subprogram;
1132}
1133
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001134/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001135/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001136void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001137 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001138}
1139
Jim Laskeyce72b172006-02-11 01:01:30 +00001140/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001141///
Jim Laskeyce72b172006-02-11 01:01:30 +00001142const char *SubprogramDesc::getDescString() const {
1143 return "llvm.dbg.subprogram";
1144}
1145
1146/// getTypeString - Return a string used to label this descriptors type.
1147///
1148const char *SubprogramDesc::getTypeString() const {
1149 return "llvm.dbg.subprogram.type";
1150}
1151
1152/// getAnchorString - Return a string used to label this descriptor's anchor.
1153///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001154const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001155const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001156 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001157}
1158
Jim Laskey86cbdba2006-02-06 15:33:21 +00001159#ifndef NDEBUG
1160void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001161 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001162 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001163 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001164 << "Anchor(" << getAnchor() << "), "
1165 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001166 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001167 << "File(" << getFile() << "),"
1168 << "Line(" << getLine() << "),"
Jim Laskeyb8509c52006-03-23 18:07:55 +00001169 << "Type(\"" << getType() << "\"), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001170 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1171 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001172}
1173#endif
1174
Jim Laskey45ccae52006-02-28 20:15:07 +00001175//===----------------------------------------------------------------------===//
1176
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001177BlockDesc::BlockDesc()
1178: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001179, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001180{}
1181
1182// Implement isa/cast/dyncast.
1183bool BlockDesc::classof(const DebugInfoDesc *D) {
1184 return D->getTag() == DW_TAG_lexical_block;
1185}
1186
1187/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1188///
1189void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1190 DebugInfoDesc::ApplyToFields(Visitor);
1191
Jim Laskeyb8509c52006-03-23 18:07:55 +00001192 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001193}
1194
1195/// getDescString - Return a string used to compose global names and labels.
1196///
1197const char *BlockDesc::getDescString() const {
1198 return "llvm.dbg.block";
1199}
1200
1201/// getTypeString - Return a string used to label this descriptors type.
1202///
1203const char *BlockDesc::getTypeString() const {
1204 return "llvm.dbg.block.type";
1205}
1206
1207#ifndef NDEBUG
1208void BlockDesc::dump() {
1209 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001210 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001211 << "Tag(" << getTag() << "),"
1212 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001213}
1214#endif
1215
1216//===----------------------------------------------------------------------===//
1217
Jim Laskey86cbdba2006-02-06 15:33:21 +00001218DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001219 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001220}
1221DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001222 // Handle NULL.
1223 if (!GV) return NULL;
1224
Jim Laskey86cbdba2006-02-06 15:33:21 +00001225 // Check to see if it has been already deserialized.
1226 DebugInfoDesc *&Slot = GlobalDescs[GV];
1227 if (Slot) return Slot;
1228
1229 // Get the Tag from the global.
1230 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1231
Jim Laskey86cbdba2006-02-06 15:33:21 +00001232 // Create an empty instance of the correct sort.
1233 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001234
Jim Laskey21407982006-03-14 18:37:57 +00001235 // If not a user defined descriptor.
1236 if (Slot) {
1237 // Deserialize the fields.
1238 DIDeserializeVisitor DRAM(*this, GV);
1239 DRAM.ApplyToFields(Slot);
1240 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001241
1242 return Slot;
1243}
1244
1245//===----------------------------------------------------------------------===//
1246
1247/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001248///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001249const PointerType *DISerializer::getStrPtrType() {
1250 // If not already defined.
1251 if (!StrPtrTy) {
1252 // Construct the pointer to signed bytes.
1253 StrPtrTy = PointerType::get(Type::SByteTy);
1254 }
1255
1256 return StrPtrTy;
1257}
1258
1259/// getEmptyStructPtrType - Return a "{ }*" type.
1260///
1261const PointerType *DISerializer::getEmptyStructPtrType() {
1262 // If not already defined.
1263 if (!EmptyStructPtrTy) {
1264 // Construct the empty structure type.
1265 const StructType *EmptyStructTy =
1266 StructType::get(std::vector<const Type*>());
1267 // Construct the pointer to empty structure type.
1268 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1269 }
1270
1271 return EmptyStructPtrTy;
1272}
1273
1274/// getTagType - Return the type describing the specified descriptor (via tag.)
1275///
1276const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1277 // Attempt to get the previously defined type.
1278 StructType *&Ty = TagTypes[DD->getTag()];
1279
1280 // If not already defined.
1281 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001282 // Set up fields vector.
1283 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001284 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001285 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001286 GTAM.ApplyToFields(DD);
1287
1288 // Construct structured type.
1289 Ty = StructType::get(Fields);
1290
Jim Laskey86cbdba2006-02-06 15:33:21 +00001291 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001292 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001293 }
1294
1295 return Ty;
1296}
1297
1298/// getString - Construct the string as constant string global.
1299///
Jim Laskeyce72b172006-02-11 01:01:30 +00001300Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001301 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001302 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001303 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001304 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001305 // If empty string then use a sbyte* null instead.
1306 if (String.empty()) {
1307 Slot = ConstantPointerNull::get(getStrPtrType());
1308 } else {
1309 // Construct string as an llvm constant.
1310 Constant *ConstStr = ConstantArray::get(String);
1311 // Otherwise create and return a new string global.
1312 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1313 GlobalVariable::InternalLinkage,
1314 ConstStr, "str", M);
1315 StrGV->setSection("llvm.metadata");
1316 // Convert to generic string pointer.
1317 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1318 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001319 return Slot;
1320
Jim Laskey86cbdba2006-02-06 15:33:21 +00001321}
1322
1323/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1324/// so that it can be serialized to a .bc or .ll file.
1325GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1326 // Check if the DebugInfoDesc is already in the map.
1327 GlobalVariable *&Slot = DescGlobals[DD];
1328
1329 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1330 if (Slot) return Slot;
1331
Jim Laskey86cbdba2006-02-06 15:33:21 +00001332 // Get the type associated with the Tag.
1333 const StructType *Ty = getTagType(DD);
1334
1335 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001336 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1337 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001338 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001339
1340 // Insert new GlobalVariable in DescGlobals map.
1341 Slot = GV;
1342
1343 // Set up elements vector
1344 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001345 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001346 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001347 SRAM.ApplyToFields(DD);
1348
1349 // Set the globals initializer.
1350 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1351
1352 return GV;
1353}
1354
1355//===----------------------------------------------------------------------===//
1356
Jim Laskey86cbdba2006-02-06 15:33:21 +00001357/// Verify - Return true if the GlobalVariable appears to be a valid
1358/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001359bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001360 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001361}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001362bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001363 // NULLs are valid.
1364 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001365
Jim Laskey98e04102006-03-26 22:45:20 +00001366 // Check prior validity.
1367 unsigned &ValiditySlot = Validity[GV];
1368
1369 // If visited before then use old state.
1370 if (ValiditySlot) return ValiditySlot == Valid;
1371
1372 // Assume validity for the time being (recursion.)
1373 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001374
1375 // Make sure the global is internal or link once (anchor.)
1376 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1377 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1378 ValiditySlot = Invalid;
1379 return false;
1380 }
Jim Laskey98e04102006-03-26 22:45:20 +00001381
Jim Laskey86cbdba2006-02-06 15:33:21 +00001382 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001383 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001384
1385 // Check for user defined descriptors.
1386 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001387
Jim Laskey86cbdba2006-02-06 15:33:21 +00001388 // Construct an empty DebugInfoDesc.
1389 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001390
1391 // Allow for user defined descriptors.
1392 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001393
1394 // Get the initializer constant.
1395 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1396
1397 // Get the operand count.
1398 unsigned N = CI->getNumOperands();
1399
1400 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001401 unsigned &CountSlot = Counts[Tag];
1402 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001403 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001404 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001405 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001406 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001407 }
1408
Jim Laskey21407982006-03-14 18:37:57 +00001409 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001410 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001411 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001412 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001413 return false;
1414 }
1415
1416 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001417 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001418 VRAM.ApplyToFields(DD);
1419
1420 // Release empty DebugInfoDesc.
1421 delete DD;
1422
Jim Laskey98e04102006-03-26 22:45:20 +00001423 // If fields are not valid.
1424 if (!VRAM.isValid()) {
1425 ValiditySlot = Invalid;
1426 return false;
1427 }
1428
1429 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001430}
1431
1432//===----------------------------------------------------------------------===//
1433
Jim Laskeyb8509c52006-03-23 18:07:55 +00001434DebugScope::~DebugScope() {
1435 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1436 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1437}
1438
1439//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001440
1441MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001442: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001443, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001444, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001445, Directories()
1446, SourceFiles()
1447, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001448, LabelID(0)
1449, ScopeMap()
1450, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001451, FrameMoves()
1452{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001453MachineDebugInfo::~MachineDebugInfo() {
1454
1455}
1456
Jim Laskeyb2efb852006-01-04 22:28:25 +00001457/// doInitialization - Initialize the debug state for a new module.
1458///
1459bool MachineDebugInfo::doInitialization() {
1460 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001461}
1462
Jim Laskeyb2efb852006-01-04 22:28:25 +00001463/// doFinalization - Tear down the debug state after completion of a module.
1464///
1465bool MachineDebugInfo::doFinalization() {
1466 return false;
1467}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001468
Jim Laskey41886992006-04-07 16:34:46 +00001469/// BeginFunction - Begin gathering function debug information.
1470///
1471void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1472 // Coming soon.
1473}
1474
1475/// MachineDebugInfo::EndFunction - Discard function debug information.
1476///
1477void MachineDebugInfo::EndFunction() {
1478 // Clean up scope information.
1479 if (RootScope) {
1480 delete RootScope;
1481 ScopeMap.clear();
1482 RootScope = NULL;
1483 }
1484
1485 // Clean up frame info.
1486 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1487 FrameMoves.clear();
1488}
1489
Jim Laskeyd96185a2006-02-13 12:50:39 +00001490/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001491///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001492// FIXME - use new Value type when available.
1493DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001494 return DR.Deserialize(V);
1495}
1496
1497/// Verify - Verify that a Value is debug information descriptor.
1498///
1499bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001500 return VR.Verify(V);
1501}
1502
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001503/// AnalyzeModule - Scan the module for global debug information.
1504///
1505void MachineDebugInfo::AnalyzeModule(Module &M) {
1506 SetupCompileUnits(M);
1507}
1508
1509/// SetupCompileUnits - Set up the unique vector of compile units.
1510///
1511void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001512 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001513
Jim Laskey0420f2a2006-02-22 19:02:11 +00001514 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1515 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001516 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001517}
1518
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001519/// getCompileUnits - Return a vector of debug compile units.
1520///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001521const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001522 return CompileUnits;
1523}
1524
Jim Laskey0420f2a2006-02-22 19:02:11 +00001525/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1526/// named GlobalVariable.
1527std::vector<GlobalVariable*>
1528MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1529 const std::string &RootName) {
1530 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001531}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001532
1533/// RecordLabel - Records location information and associates it with a
1534/// debug label. Returns a unique label ID used to generate a label and
1535/// provide correspondence to the source line list.
1536unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1537 unsigned Source) {
1538 unsigned ID = NextLabelID();
1539 Lines.push_back(new SourceLineInfo(Line, Column, Source, ID));
1540 return ID;
1541}
1542
1543/// RecordSource - Register a source file with debug info. Returns an source
1544/// ID.
1545unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1546 const std::string &Source) {
1547 unsigned DirectoryID = Directories.insert(Directory);
1548 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1549}
1550unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1551 return RecordSource(CompileUnit->getDirectory(),
1552 CompileUnit->getFileName());
1553}
1554
1555/// RecordRegionStart - Indicate the start of a region.
1556///
1557unsigned MachineDebugInfo::RecordRegionStart(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 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1563 return ID;
1564}
1565
1566/// RecordRegionEnd - Indicate the end of a region.
1567///
1568unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1569 // FIXME - need to be able to handle split scopes because of bb cloning.
1570 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1571 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1572 unsigned ID = NextLabelID();
1573 Scope->setEndLabelID(ID);
1574 return ID;
1575}
1576
1577/// RecordVariable - Indicate the declaration of a local variable.
1578///
1579void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1580 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1581 DebugScope *Scope = getOrCreateScope(VD->getContext());
1582 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1583 Scope->AddVariable(DV);
1584}
1585
1586/// getOrCreateScope - Returns the scope associated with the given descriptor.
1587///
1588DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1589 DebugScope *&Slot = ScopeMap[ScopeDesc];
1590 if (!Slot) {
1591 // FIXME - breaks down when the context is an inlined function.
1592 DebugInfoDesc *ParentDesc = NULL;
1593 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1594 ParentDesc = Block->getContext();
1595 }
1596 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1597 Slot = new DebugScope(Parent, ScopeDesc);
1598 if (Parent) {
1599 Parent->AddScope(Slot);
1600 } else if (RootScope) {
1601 // FIXME - Add inlined function scopes to the root so we can delete
1602 // them later. Long term, handle inlined functions properly.
1603 RootScope->AddScope(Slot);
1604 } else {
1605 // First function is top level function.
1606 RootScope = Slot;
1607 }
1608 }
1609 return Slot;
1610}
1611
Jim Laskeyb8509c52006-03-23 18:07:55 +00001612