blob: ba5bf03b96f98c2f48d036053cb24115cf2dd3b3 [file] [log] [blame]
Jim Laskey44317392006-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 Laskey44317392006-01-04 13:36:38 +00009
10#include "llvm/CodeGen/MachineDebugInfo.h"
11
Jim Laskey0bbdc552006-01-26 20:21:46 +000012#include "llvm/Constants.h"
13#include "llvm/DerivedTypes.h"
Jim Laskey85263232006-02-06 15:33:21 +000014#include "llvm/GlobalVariable.h"
Jim Laskey0bbdc552006-01-26 20:21:46 +000015#include "llvm/Intrinsics.h"
16#include "llvm/Instructions.h"
17#include "llvm/Module.h"
18#include "llvm/Support/Dwarf.h"
19
Jim Laskey85263232006-02-06 15:33:21 +000020#include <iostream>
21
Jim Laskey44317392006-01-04 13:36:38 +000022using namespace llvm;
Jim Laskey4e71db12006-03-01 20:39:36 +000023using namespace llvm::dwarf;
Jim Laskey44317392006-01-04 13:36:38 +000024
25// Handle the Pass registration stuff necessary to use TargetData's.
26namespace {
Jim Laskey219d5592006-01-04 22:28:25 +000027 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
28}
Jim Laskeyb9966022006-01-17 17:31:53 +000029
Jim Laskey0bbdc552006-01-26 20:21:46 +000030//===----------------------------------------------------------------------===//
31
Jim Laskey85263232006-02-06 15:33:21 +000032/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskey0bbdc552006-01-26 20:21:46 +000033/// specified value in their initializer somewhere.
34static void
35getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
36 // Scan though value users.
37 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
38 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey85263232006-02-06 15:33:21 +000039 // If the user is a GlobalVariable then add to result.
Jim Laskey0bbdc552006-01-26 20:21:46 +000040 Result.push_back(GV);
41 } else if (Constant *C = dyn_cast<Constant>(*I)) {
42 // If the user is a constant variable then scan its users
43 getGlobalVariablesUsing(C, Result);
44 }
45 }
46}
47
Jim Laskey85263232006-02-06 15:33:21 +000048/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
49/// named GlobalVariable.
Jim Laskey0bbdc552006-01-26 20:21:46 +000050static std::vector<GlobalVariable*>
51getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey85263232006-02-06 15:33:21 +000052 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskey5995d012006-02-11 01:01:30 +000053
54 std::vector<const Type*> FieldTypes;
55 FieldTypes.push_back(Type::UIntTy);
Jim Laskey69effa22006-03-07 20:53:47 +000056 FieldTypes.push_back(Type::UIntTy);
Jim Laskey0bbdc552006-01-26 20:21:46 +000057
Jim Laskey85263232006-02-06 15:33:21 +000058 // Get the GlobalVariable root.
Jim Laskey0bbdc552006-01-26 20:21:46 +000059 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskey5995d012006-02-11 01:01:30 +000060 StructType::get(FieldTypes));
Jim Laskey0bbdc552006-01-26 20:21:46 +000061
62 // If present and linkonce then scan for users.
63 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
64 getGlobalVariablesUsing(UseRoot, Result);
65 }
66
67 return Result;
68}
69
70/// getStringValue - Turn an LLVM constant pointer that eventually points to a
71/// global into a string value. Return an empty string if we can't do it.
72///
Chris Lattnerecd7e612006-01-27 17:31:30 +000073static const std::string getStringValue(Value *V, unsigned Offset = 0) {
Jim Laskey0bbdc552006-01-26 20:21:46 +000074 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 if (Init->isString()) {
78 std::string Result = Init->getAsString();
79 if (Offset < Result.size()) {
80 // If we are pointing INTO The string, erase the beginning...
81 Result.erase(Result.begin(), Result.begin()+Offset);
82
83 // Take off the null terminator, and any string fragments after it.
84 std::string::size_type NullPos = Result.find_first_of((char)0);
85 if (NullPos != std::string::npos)
86 Result.erase(Result.begin()+NullPos, Result.end());
87 return Result;
88 }
89 }
90 }
91 } else if (Constant *C = dyn_cast<Constant>(V)) {
92 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
93 return getStringValue(GV, Offset);
94 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
95 if (CE->getOpcode() == Instruction::GetElementPtr) {
96 // Turn a gep into the specified offset.
97 if (CE->getNumOperands() == 3 &&
98 cast<Constant>(CE->getOperand(1))->isNullValue() &&
99 isa<ConstantInt>(CE->getOperand(2))) {
100 return getStringValue(CE->getOperand(0),
101 Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
102 }
103 }
104 }
105 }
106 return "";
107}
Jim Laskeyf98fc842006-01-27 15:20:54 +0000108
Jim Laskey85263232006-02-06 15:33:21 +0000109/// isStringValue - Return true if the given value can be coerced to a string.
110///
111static bool isStringValue(Value *V) {
112 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
113 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
114 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
115 return Init->isString();
116 }
117 } else if (Constant *C = dyn_cast<Constant>(V)) {
118 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
119 return isStringValue(GV);
120 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
121 if (CE->getOpcode() == Instruction::GetElementPtr) {
122 if (CE->getNumOperands() == 3 &&
123 cast<Constant>(CE->getOperand(1))->isNullValue() &&
124 isa<ConstantInt>(CE->getOperand(2))) {
125 return isStringValue(CE->getOperand(0));
126 }
127 }
128 }
129 }
130 return false;
131}
132
Jim Laskeyb643ff52006-02-06 19:12:02 +0000133/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyf98fc842006-01-27 15:20:54 +0000134///
Jim Laskeyb643ff52006-02-06 19:12:02 +0000135static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyf98fc842006-01-27 15:20:54 +0000136 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
137 return GV;
138 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Jim Laskey85263232006-02-06 15:33:21 +0000139 if (CE->getOpcode() == Instruction::Cast) {
140 return dyn_cast<GlobalVariable>(CE->getOperand(0));
141 }
Jim Laskeyf98fc842006-01-27 15:20:54 +0000142 }
143 return NULL;
144}
145
Jim Laskeyb643ff52006-02-06 19:12:02 +0000146/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey85263232006-02-06 15:33:21 +0000147/// GlobalVariable.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000148static bool isGlobalVariable(Value *V) {
Jim Laskey85263232006-02-06 15:33:21 +0000149 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
150 return true;
151 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
152 if (CE->getOpcode() == Instruction::Cast) {
153 return isa<GlobalVariable>(CE->getOperand(0));
154 }
155 }
156 return false;
157}
158
Jim Laskeyb643ff52006-02-06 19:12:02 +0000159/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey85263232006-02-06 15:33:21 +0000160///
Jim Laskeyb643ff52006-02-06 19:12:02 +0000161static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey85263232006-02-06 15:33:21 +0000162 // Make sure the GlobalVariable has an initializer.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000163 if (!GV->hasInitializer()) return NULL;
Jim Laskey219d5592006-01-04 22:28:25 +0000164
Jim Laskey85263232006-02-06 15:33:21 +0000165 // Get the initializer constant.
166 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyb643ff52006-02-06 19:12:02 +0000167 if (!CI) return NULL;
Jim Laskey0bbdc552006-01-26 20:21:46 +0000168
Jim Laskey85263232006-02-06 15:33:21 +0000169 // Check if there is at least i + 1 operands.
170 unsigned N = CI->getNumOperands();
Jim Laskeyb643ff52006-02-06 19:12:02 +0000171 if (i >= N) return NULL;
Jim Laskey0bbdc552006-01-26 20:21:46 +0000172
Jim Laskey85263232006-02-06 15:33:21 +0000173 // Check constant.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000174 return dyn_cast<ConstantUInt>(CI->getOperand(i));
Jim Laskey0bbdc552006-01-26 20:21:46 +0000175}
Jim Laskey85263232006-02-06 15:33:21 +0000176//===----------------------------------------------------------------------===//
177
Jim Laskeyb643ff52006-02-06 19:12:02 +0000178/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey85263232006-02-06 15:33:21 +0000179/// descriptor.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000180void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey85263232006-02-06 15:33:21 +0000181 DD->ApplyToFields(this);
182}
183
184//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000185/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
186/// the supplied DebugInfoDesc.
187class DICountVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000188private:
189 unsigned Count; // Running count of fields.
190
191public:
Jim Laskey5995d012006-02-11 01:01:30 +0000192 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey85263232006-02-06 15:33:21 +0000193
194 // Accessors.
195 unsigned getCount() const { return Count; }
196
197 /// Apply - Count each of the fields.
198 ///
199 virtual void Apply(int &Field) { ++Count; }
200 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000201 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskey69b9e262006-02-23 16:58:18 +0000202 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey85263232006-02-06 15:33:21 +0000203 virtual void Apply(bool &Field) { ++Count; }
204 virtual void Apply(std::string &Field) { ++Count; }
205 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
206 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey716edb92006-02-28 20:15:07 +0000207 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
208 ++Count;
209 }
Jim Laskey85263232006-02-06 15:33:21 +0000210};
211
212//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000213/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
214/// supplied DebugInfoDesc.
215class DIDeserializeVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000216private:
217 DIDeserializer &DR; // Active deserializer.
218 unsigned I; // Current operand index.
219 ConstantStruct *CI; // GlobalVariable constant initializer.
220
221public:
Jim Laskeyb643ff52006-02-06 19:12:02 +0000222 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
223 : DIVisitor()
Jim Laskey85263232006-02-06 15:33:21 +0000224 , DR(D)
Jim Laskey5995d012006-02-11 01:01:30 +0000225 , I(0)
Jim Laskey85263232006-02-06 15:33:21 +0000226 , CI(cast<ConstantStruct>(GV->getInitializer()))
227 {}
228
229 /// Apply - Set the value of each of the fields.
230 ///
231 virtual void Apply(int &Field) {
232 Constant *C = CI->getOperand(I++);
233 Field = cast<ConstantSInt>(C)->getValue();
234 }
235 virtual void Apply(unsigned &Field) {
236 Constant *C = CI->getOperand(I++);
237 Field = cast<ConstantUInt>(C)->getValue();
238 }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000239 virtual void Apply(int64_t &Field) {
240 Constant *C = CI->getOperand(I++);
241 Field = cast<ConstantSInt>(C)->getValue();
242 }
Jim Laskey69b9e262006-02-23 16:58:18 +0000243 virtual void Apply(uint64_t &Field) {
244 Constant *C = CI->getOperand(I++);
245 Field = cast<ConstantUInt>(C)->getValue();
246 }
Jim Laskey85263232006-02-06 15:33:21 +0000247 virtual void Apply(bool &Field) {
248 Constant *C = CI->getOperand(I++);
249 Field = cast<ConstantBool>(C)->getValue();
250 }
251 virtual void Apply(std::string &Field) {
252 Constant *C = CI->getOperand(I++);
253 Field = getStringValue(C);
254 }
255 virtual void Apply(DebugInfoDesc *&Field) {
256 Constant *C = CI->getOperand(I++);
257 Field = DR.Deserialize(C);
258 }
259 virtual void Apply(GlobalVariable *&Field) {
260 Constant *C = CI->getOperand(I++);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000261 Field = getGlobalVariable(C);
Jim Laskey85263232006-02-06 15:33:21 +0000262 }
Jim Laskey716edb92006-02-28 20:15:07 +0000263 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
264 Constant *C = CI->getOperand(I++);
265 GlobalVariable *GV = getGlobalVariable(C);
266 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
267 Field.resize(0);
268 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
269 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
270 DebugInfoDesc *DE = DR.Deserialize(GVE);
271 Field.push_back(DE);
272 }
273 }
Jim Laskey85263232006-02-06 15:33:21 +0000274};
275
276//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000277/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey85263232006-02-06 15:33:21 +0000278/// the supplied DebugInfoDesc.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000279class DISerializeVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000280private:
281 DISerializer &SR; // Active serializer.
282 std::vector<Constant*> &Elements; // Element accumulator.
283
284public:
Jim Laskeyb643ff52006-02-06 19:12:02 +0000285 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
286 : DIVisitor()
Jim Laskey85263232006-02-06 15:33:21 +0000287 , SR(S)
288 , Elements(E)
289 {}
290
291 /// Apply - Set the value of each of the fields.
292 ///
293 virtual void Apply(int &Field) {
Jim Laskey5995d012006-02-11 01:01:30 +0000294 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
Jim Laskey85263232006-02-06 15:33:21 +0000295 }
296 virtual void Apply(unsigned &Field) {
297 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
298 }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000299 virtual void Apply(int64_t &Field) {
300 Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
301 }
Jim Laskey69b9e262006-02-23 16:58:18 +0000302 virtual void Apply(uint64_t &Field) {
303 Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
304 }
Jim Laskey85263232006-02-06 15:33:21 +0000305 virtual void Apply(bool &Field) {
306 Elements.push_back(ConstantBool::get(Field));
307 }
308 virtual void Apply(std::string &Field) {
309 Elements.push_back(SR.getString(Field));
310 }
311 virtual void Apply(DebugInfoDesc *&Field) {
312 GlobalVariable *GV = NULL;
313
314 // If non-NULL the convert to global.
315 if (Field) GV = SR.Serialize(Field);
316
317 // FIXME - At some point should use specific type.
318 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
319
320 if (GV) {
321 // Set to pointer to global.
322 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
323 } else {
324 // Use NULL.
325 Elements.push_back(ConstantPointerNull::get(EmptyTy));
326 }
327 }
328 virtual void Apply(GlobalVariable *&Field) {
329 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskey5995d012006-02-11 01:01:30 +0000330 if (Field) {
331 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
332 } else {
333 Elements.push_back(ConstantPointerNull::get(EmptyTy));
334 }
Jim Laskey85263232006-02-06 15:33:21 +0000335 }
Jim Laskey716edb92006-02-28 20:15:07 +0000336 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
337 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
338 unsigned N = Field.size();
339 ArrayType *AT = ArrayType::get(EmptyTy, N);
340 std::vector<Constant *> ArrayElements;
341
342 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
343 GlobalVariable *GVE = SR.Serialize(Field[i]);
344 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
345 ArrayElements.push_back(cast<Constant>(CE));
346 }
347
348 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000349 GlobalVariable *CAGV = new GlobalVariable(AT, true,
350 GlobalValue::InternalLinkage,
351 CA, "llvm.dbg.array",
352 SR.getModule());
353 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey716edb92006-02-28 20:15:07 +0000354 Elements.push_back(CAE);
355 }
Jim Laskey85263232006-02-06 15:33:21 +0000356};
357
358//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000359/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey85263232006-02-06 15:33:21 +0000360/// the supplied DebugInfoDesc.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000361class DIGetTypesVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000362private:
363 DISerializer &SR; // Active serializer.
364 std::vector<const Type*> &Fields; // Type accumulator.
365
366public:
Jim Laskeyb643ff52006-02-06 19:12:02 +0000367 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
368 : DIVisitor()
Jim Laskey85263232006-02-06 15:33:21 +0000369 , SR(S)
370 , Fields(F)
371 {}
372
373 /// Apply - Set the value of each of the fields.
374 ///
375 virtual void Apply(int &Field) {
376 Fields.push_back(Type::IntTy);
377 }
378 virtual void Apply(unsigned &Field) {
379 Fields.push_back(Type::UIntTy);
380 }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000381 virtual void Apply(int64_t &Field) {
382 Fields.push_back(Type::IntTy);
383 }
Jim Laskey69b9e262006-02-23 16:58:18 +0000384 virtual void Apply(uint64_t &Field) {
385 Fields.push_back(Type::UIntTy);
386 }
Jim Laskey85263232006-02-06 15:33:21 +0000387 virtual void Apply(bool &Field) {
388 Fields.push_back(Type::BoolTy);
389 }
390 virtual void Apply(std::string &Field) {
391 Fields.push_back(SR.getStrPtrType());
392 }
393 virtual void Apply(DebugInfoDesc *&Field) {
394 // FIXME - At some point should use specific type.
395 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396 Fields.push_back(EmptyTy);
397 }
398 virtual void Apply(GlobalVariable *&Field) {
399 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
400 Fields.push_back(EmptyTy);
401 }
Jim Laskey716edb92006-02-28 20:15:07 +0000402 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
403 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
404 Fields.push_back(EmptyTy);
405 }
Jim Laskey85263232006-02-06 15:33:21 +0000406};
407
408//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000409/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey85263232006-02-06 15:33:21 +0000410/// a constant initializer.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000411class DIVerifyVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000412private:
413 DIVerifier &VR; // Active verifier.
414 bool IsValid; // Validity status.
415 unsigned I; // Current operand index.
416 ConstantStruct *CI; // GlobalVariable constant initializer.
417
418public:
Jim Laskeyb643ff52006-02-06 19:12:02 +0000419 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
420 : DIVisitor()
Jim Laskey85263232006-02-06 15:33:21 +0000421 , VR(V)
422 , IsValid(true)
Jim Laskey5995d012006-02-11 01:01:30 +0000423 , I(0)
Jim Laskey85263232006-02-06 15:33:21 +0000424 , CI(cast<ConstantStruct>(GV->getInitializer()))
425 {
426 }
427
428 // Accessors.
429 bool isValid() const { return IsValid; }
430
431 /// Apply - Set the value of each of the fields.
432 ///
433 virtual void Apply(int &Field) {
434 Constant *C = CI->getOperand(I++);
435 IsValid = IsValid && isa<ConstantInt>(C);
436 }
437 virtual void Apply(unsigned &Field) {
438 Constant *C = CI->getOperand(I++);
439 IsValid = IsValid && isa<ConstantInt>(C);
440 }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000441 virtual void Apply(int64_t &Field) {
442 Constant *C = CI->getOperand(I++);
443 IsValid = IsValid && isa<ConstantInt>(C);
444 }
Jim Laskey69b9e262006-02-23 16:58:18 +0000445 virtual void Apply(uint64_t &Field) {
446 Constant *C = CI->getOperand(I++);
447 IsValid = IsValid && isa<ConstantInt>(C);
448 }
Jim Laskey85263232006-02-06 15:33:21 +0000449 virtual void Apply(bool &Field) {
450 Constant *C = CI->getOperand(I++);
451 IsValid = IsValid && isa<ConstantBool>(C);
452 }
453 virtual void Apply(std::string &Field) {
454 Constant *C = CI->getOperand(I++);
455 IsValid = IsValid && isStringValue(C);
456 }
457 virtual void Apply(DebugInfoDesc *&Field) {
458 // FIXME - Prepare the correct descriptor.
459 Constant *C = CI->getOperand(I++);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000460 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey85263232006-02-06 15:33:21 +0000461 }
462 virtual void Apply(GlobalVariable *&Field) {
463 Constant *C = CI->getOperand(I++);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000464 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey85263232006-02-06 15:33:21 +0000465 }
Jim Laskey716edb92006-02-28 20:15:07 +0000466 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
467 Constant *C = CI->getOperand(I++);
468 IsValid = IsValid && isGlobalVariable(C);
469 if (!IsValid) return;
470
471 GlobalVariable *GV = getGlobalVariable(C);
472 IsValid = IsValid && GV && GV->hasInitializer();
473 if (!IsValid) return;
474
475 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
476 IsValid = IsValid && CA;
477 if (!IsValid) return;
478
479 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
480 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
481 if (!IsValid) return;
482
483 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
484 VR.Verify(GVE);
485 }
486 }
Jim Laskey85263232006-02-06 15:33:21 +0000487};
488
Jim Laskey5995d012006-02-11 01:01:30 +0000489
Jim Laskey85263232006-02-06 15:33:21 +0000490//===----------------------------------------------------------------------===//
491
Jim Laskey5995d012006-02-11 01:01:30 +0000492/// TagFromGlobal - Returns the Tag number from a debug info descriptor
493/// GlobalVariable.
494unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
495 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey4e71db12006-03-01 20:39:36 +0000496 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey5995d012006-02-11 01:01:30 +0000497}
498
499/// DescFactory - Create an instance of debug info descriptor based on Tag.
500/// Return NULL if not a recognized Tag.
501DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
502 switch (Tag) {
Jim Laskey4e71db12006-03-01 20:39:36 +0000503 case DW_TAG_anchor: return new AnchorDesc();
504 case DW_TAG_compile_unit: return new CompileUnitDesc();
505 case DW_TAG_variable: return new GlobalVariableDesc();
506 case DW_TAG_subprogram: return new SubprogramDesc();
507 case DW_TAG_base_type: return new BasicTypeDesc();
508 case DW_TAG_typedef:
509 case DW_TAG_pointer_type:
510 case DW_TAG_reference_type:
511 case DW_TAG_const_type:
512 case DW_TAG_volatile_type:
513 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
514 case DW_TAG_array_type:
515 case DW_TAG_structure_type:
516 case DW_TAG_union_type:
517 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
518 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey88f0fe12006-03-03 15:06:57 +0000519 case DW_TAG_member: return new DerivedTypeDesc(DW_TAG_member);
Jim Laskey862001a2006-03-01 23:52:37 +0000520 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskey5995d012006-02-11 01:01:30 +0000521 default: break;
522 }
523 return NULL;
524}
525
526/// getLinkage - get linkage appropriate for this type of descriptor.
527///
528GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
529 return GlobalValue::InternalLinkage;
530}
531
532/// ApplyToFields - Target the vistor to the fields of the descriptor.
533///
534void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
535 Visitor->Apply(Tag);
536}
537
538//===----------------------------------------------------------------------===//
539
Jim Laskey4e71db12006-03-01 20:39:36 +0000540AnchorDesc::AnchorDesc()
541: DebugInfoDesc(DW_TAG_anchor)
Jim Laskey69effa22006-03-07 20:53:47 +0000542, AnchorTag(0)
Jim Laskey4e71db12006-03-01 20:39:36 +0000543{}
Jim Laskey69effa22006-03-07 20:53:47 +0000544AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey4e71db12006-03-01 20:39:36 +0000545: DebugInfoDesc(DW_TAG_anchor)
Jim Laskey69effa22006-03-07 20:53:47 +0000546, AnchorTag(D->getTag())
Jim Laskey4e71db12006-03-01 20:39:36 +0000547{}
548
549// Implement isa/cast/dyncast.
550bool AnchorDesc::classof(const DebugInfoDesc *D) {
551 return D->getTag() == DW_TAG_anchor;
552}
553
Jim Laskey5995d012006-02-11 01:01:30 +0000554/// getLinkage - get linkage appropriate for this type of descriptor.
555///
556GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
557 return GlobalValue::LinkOnceLinkage;
558}
559
560/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
561///
562void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
563 DebugInfoDesc::ApplyToFields(Visitor);
564
Jim Laskey69effa22006-03-07 20:53:47 +0000565 Visitor->Apply(AnchorTag);
Jim Laskey5995d012006-02-11 01:01:30 +0000566}
567
Jim Laskey69effa22006-03-07 20:53:47 +0000568/// getDescString - Return a string used to compose global names and labels. A
569/// A global variable name needs to be defined for each debug descriptor that is
570/// anchored. NOTE: that each global variable name here also needs to be added
571/// to the list of names left external in the internalizer.
572/// ExternalNames.insert("llvm.dbg.compile_units");
573/// ExternalNames.insert("llvm.dbg.global_variables");
574/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskey5995d012006-02-11 01:01:30 +0000575const char *AnchorDesc::getDescString() const {
Jim Laskey69effa22006-03-07 20:53:47 +0000576 switch (AnchorTag) {
577 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
578 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
579 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
580 default: break;
581 }
582
583 assert(0 && "Tag does not have a case for anchor string");
584 return "";
Jim Laskey5995d012006-02-11 01:01:30 +0000585}
586
587/// getTypeString - Return a string used to label this descriptors type.
588///
589const char *AnchorDesc::getTypeString() const {
590 return "llvm.dbg.anchor.type";
591}
592
593#ifndef NDEBUG
594void AnchorDesc::dump() {
595 std::cerr << getDescString() << " "
596 << "Tag(" << getTag() << "), "
Jim Laskey69effa22006-03-07 20:53:47 +0000597 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskey5995d012006-02-11 01:01:30 +0000598}
599#endif
600
601//===----------------------------------------------------------------------===//
602
603AnchoredDesc::AnchoredDesc(unsigned T)
604: DebugInfoDesc(T)
605, Anchor(NULL)
606{}
607
608/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
609///
610void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
611 DebugInfoDesc::ApplyToFields(Visitor);
612
613 Visitor->Apply((DebugInfoDesc *&)Anchor);
614}
615
616//===----------------------------------------------------------------------===//
617
618CompileUnitDesc::CompileUnitDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000619: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskey5995d012006-02-11 01:01:30 +0000620, DebugVersion(LLVMDebugVersion)
621, Language(0)
622, FileName("")
623, Directory("")
624, Producer("")
625{}
626
Jim Laskey4e71db12006-03-01 20:39:36 +0000627// Implement isa/cast/dyncast.
628bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
629 return D->getTag() == DW_TAG_compile_unit;
630}
631
Jim Laskey85263232006-02-06 15:33:21 +0000632/// DebugVersionFromGlobal - Returns the version number from a compile unit
633/// GlobalVariable.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000634unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskey5995d012006-02-11 01:01:30 +0000635 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey4e71db12006-03-01 20:39:36 +0000636 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey85263232006-02-06 15:33:21 +0000637}
638
Jim Laskeyb643ff52006-02-06 19:12:02 +0000639/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
640///
641void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey5995d012006-02-11 01:01:30 +0000642 AnchoredDesc::ApplyToFields(Visitor);
643
Jim Laskeyb643ff52006-02-06 19:12:02 +0000644 Visitor->Apply(DebugVersion);
645 Visitor->Apply(Language);
646 Visitor->Apply(FileName);
647 Visitor->Apply(Directory);
648 Visitor->Apply(Producer);
Jim Laskey85263232006-02-06 15:33:21 +0000649}
650
Jim Laskey5995d012006-02-11 01:01:30 +0000651/// getDescString - Return a string used to compose global names and labels.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000652///
Jim Laskey5995d012006-02-11 01:01:30 +0000653const char *CompileUnitDesc::getDescString() const {
654 return "llvm.dbg.compile_unit";
655}
656
657/// getTypeString - Return a string used to label this descriptors type.
658///
659const char *CompileUnitDesc::getTypeString() const {
660 return "llvm.dbg.compile_unit.type";
661}
662
663/// getAnchorString - Return a string used to label this descriptor's anchor.
664///
Jim Laskey69effa22006-03-07 20:53:47 +0000665const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskey5995d012006-02-11 01:01:30 +0000666const char *CompileUnitDesc::getAnchorString() const {
Jim Laskey69effa22006-03-07 20:53:47 +0000667 return AnchorString;
Jim Laskey0bbdc552006-01-26 20:21:46 +0000668}
669
Jim Laskey85263232006-02-06 15:33:21 +0000670#ifndef NDEBUG
671void CompileUnitDesc::dump() {
Jim Laskey5995d012006-02-11 01:01:30 +0000672 std::cerr << getDescString() << " "
Jim Laskey85263232006-02-06 15:33:21 +0000673 << "Tag(" << getTag() << "), "
Jim Laskey5995d012006-02-11 01:01:30 +0000674 << "Anchor(" << getAnchor() << "), "
675 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey85263232006-02-06 15:33:21 +0000676 << "Language(" << Language << "), "
677 << "FileName(\"" << FileName << "\"), "
678 << "Directory(\"" << Directory << "\"), "
679 << "Producer(\"" << Producer << "\")\n";
680}
681#endif
682
683//===----------------------------------------------------------------------===//
684
Jim Laskey69b9e262006-02-23 16:58:18 +0000685TypeDesc::TypeDesc(unsigned T)
686: DebugInfoDesc(T)
687, Context(NULL)
688, Name("")
Jim Laskey723d3e02006-02-24 16:46:40 +0000689, File(NULL)
Jim Laskey69b9e262006-02-23 16:58:18 +0000690, Size(0)
Jim Laskey88f0fe12006-03-03 15:06:57 +0000691, Offset(0)
Jim Laskey69b9e262006-02-23 16:58:18 +0000692{}
693
Jim Laskey723d3e02006-02-24 16:46:40 +0000694/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskey69b9e262006-02-23 16:58:18 +0000695///
696void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
697 DebugInfoDesc::ApplyToFields(Visitor);
698
699 Visitor->Apply(Context);
700 Visitor->Apply(Name);
Jim Laskey723d3e02006-02-24 16:46:40 +0000701 Visitor->Apply((DebugInfoDesc *&)File);
702 Visitor->Apply(Line);
Jim Laskey69b9e262006-02-23 16:58:18 +0000703 Visitor->Apply(Size);
Jim Laskey88f0fe12006-03-03 15:06:57 +0000704 Visitor->Apply(Offset);
Jim Laskey69b9e262006-02-23 16:58:18 +0000705}
706
707/// getDescString - Return a string used to compose global names and labels.
708///
709const char *TypeDesc::getDescString() const {
710 return "llvm.dbg.type";
711}
712
713/// getTypeString - Return a string used to label this descriptor's type.
714///
715const char *TypeDesc::getTypeString() const {
716 return "llvm.dbg.type.type";
717}
718
719#ifndef NDEBUG
720void TypeDesc::dump() {
721 std::cerr << getDescString() << " "
722 << "Tag(" << getTag() << "), "
723 << "Context(" << Context << "), "
724 << "Name(\"" << Name << "\"), "
Jim Laskey723d3e02006-02-24 16:46:40 +0000725 << "File(" << File << "), "
726 << "Line(" << Line << "), "
Jim Laskey88f0fe12006-03-03 15:06:57 +0000727 << "Size(" << Size << "), "
728 << "Offset(" << Offset << ")\n";
Jim Laskey69b9e262006-02-23 16:58:18 +0000729}
730#endif
731
732//===----------------------------------------------------------------------===//
733
734BasicTypeDesc::BasicTypeDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000735: TypeDesc(DW_TAG_base_type)
Jim Laskey69b9e262006-02-23 16:58:18 +0000736, Encoding(0)
737{}
738
Jim Laskey4e71db12006-03-01 20:39:36 +0000739// Implement isa/cast/dyncast.
740bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
741 return D->getTag() == DW_TAG_base_type;
742}
743
Jim Laskey723d3e02006-02-24 16:46:40 +0000744/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskey69b9e262006-02-23 16:58:18 +0000745///
746void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
747 TypeDesc::ApplyToFields(Visitor);
748
749 Visitor->Apply(Encoding);
750}
751
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000752/// getDescString - Return a string used to compose global names and labels.
753///
754const char *BasicTypeDesc::getDescString() const {
755 return "llvm.dbg.basictype";
756}
757
758/// getTypeString - Return a string used to label this descriptor's type.
759///
760const char *BasicTypeDesc::getTypeString() const {
761 return "llvm.dbg.basictype.type";
762}
763
Jim Laskey69b9e262006-02-23 16:58:18 +0000764#ifndef NDEBUG
765void BasicTypeDesc::dump() {
766 std::cerr << getDescString() << " "
767 << "Tag(" << getTag() << "), "
768 << "Context(" << getContext() << "), "
769 << "Name(\"" << getName() << "\"), "
770 << "Size(" << getSize() << "), "
771 << "Encoding(" << Encoding << ")\n";
772}
773#endif
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000774
Jim Laskeye5386d42006-02-23 22:37:30 +0000775//===----------------------------------------------------------------------===//
776
Jim Laskey723d3e02006-02-24 16:46:40 +0000777DerivedTypeDesc::DerivedTypeDesc(unsigned T)
778: TypeDesc(T)
Jim Laskeye5386d42006-02-23 22:37:30 +0000779, FromType(NULL)
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000780{}
Jim Laskeye5386d42006-02-23 22:37:30 +0000781
Jim Laskey4e71db12006-03-01 20:39:36 +0000782// Implement isa/cast/dyncast.
783bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
784 unsigned T = D->getTag();
785 switch (T) {
786 case DW_TAG_typedef:
787 case DW_TAG_pointer_type:
788 case DW_TAG_reference_type:
789 case DW_TAG_const_type:
790 case DW_TAG_volatile_type:
791 case DW_TAG_restrict_type:
Jim Laskey88f0fe12006-03-03 15:06:57 +0000792 case DW_TAG_member:
Jim Laskey4e71db12006-03-01 20:39:36 +0000793 return true;
794 default: break;
795 }
796 return false;
797}
798
Jim Laskey723d3e02006-02-24 16:46:40 +0000799/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskeye5386d42006-02-23 22:37:30 +0000800///
Jim Laskey723d3e02006-02-24 16:46:40 +0000801void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeye5386d42006-02-23 22:37:30 +0000802 TypeDesc::ApplyToFields(Visitor);
803
804 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskeye5386d42006-02-23 22:37:30 +0000805}
806
Jim Laskeyb9ac4cb2006-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 Laskeye5386d42006-02-23 22:37:30 +0000819#ifndef NDEBUG
Jim Laskey723d3e02006-02-24 16:46:40 +0000820void DerivedTypeDesc::dump() {
Jim Laskeye5386d42006-02-23 22:37:30 +0000821 std::cerr << getDescString() << " "
822 << "Tag(" << getTag() << "), "
823 << "Context(" << getContext() << "), "
824 << "Name(\"" << getName() << "\"), "
825 << "Size(" << getSize() << "), "
Jim Laskey723d3e02006-02-24 16:46:40 +0000826 << "File(" << getFile() << "), "
827 << "Line(" << getLine() << "), "
828 << "FromType(" << FromType << ")\n";
Jim Laskeye5386d42006-02-23 22:37:30 +0000829}
830#endif
Jim Laskey69b9e262006-02-23 16:58:18 +0000831
832//===----------------------------------------------------------------------===//
833
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000834CompositeTypeDesc::CompositeTypeDesc(unsigned T)
835: DerivedTypeDesc(T)
836, Elements()
837{}
838
Jim Laskey4e71db12006-03-01 20:39:36 +0000839// Implement isa/cast/dyncast.
840bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
841 unsigned T = D->getTag();
842 switch (T) {
843 case DW_TAG_array_type:
844 case DW_TAG_structure_type:
845 case DW_TAG_union_type:
846 case DW_TAG_enumeration_type:
847 return true;
848 default: break;
849 }
850 return false;
851}
852
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000853/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
854///
855void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
856 DerivedTypeDesc::ApplyToFields(Visitor);
857
858 Visitor->Apply(Elements);
859}
860
861/// getDescString - Return a string used to compose global names and labels.
862///
863const char *CompositeTypeDesc::getDescString() const {
864 return "llvm.dbg.compositetype";
865}
866
867/// getTypeString - Return a string used to label this descriptor's type.
868///
869const char *CompositeTypeDesc::getTypeString() const {
870 return "llvm.dbg.compositetype.type";
871}
872
873#ifndef NDEBUG
874void CompositeTypeDesc::dump() {
875 std::cerr << getDescString() << " "
876 << "Tag(" << getTag() << "), "
877 << "Context(" << getContext() << "), "
878 << "Name(\"" << getName() << "\"), "
879 << "Size(" << getSize() << "), "
880 << "File(" << getFile() << "), "
881 << "Line(" << getLine() << "), "
882 << "FromType(" << getFromType() << "), "
883 << "Elements.size(" << Elements.size() << ")\n";
884}
885#endif
886
887//===----------------------------------------------------------------------===//
888
889SubrangeDesc::SubrangeDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000890: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000891, Lo(0)
892, Hi(0)
893{}
894
Jim Laskey4e71db12006-03-01 20:39:36 +0000895// Implement isa/cast/dyncast.
896bool SubrangeDesc::classof(const DebugInfoDesc *D) {
897 return D->getTag() == DW_TAG_subrange_type;
898}
899
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000900/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
901///
902void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
903 DebugInfoDesc::ApplyToFields(Visitor);
904
905 Visitor->Apply(Lo);
906 Visitor->Apply(Hi);
907}
908
909/// getDescString - Return a string used to compose global names and labels.
910///
911const char *SubrangeDesc::getDescString() const {
912 return "llvm.dbg.subrange";
913}
914
915/// getTypeString - Return a string used to label this descriptor's type.
916///
917const char *SubrangeDesc::getTypeString() const {
918 return "llvm.dbg.subrange.type";
919}
920
921#ifndef NDEBUG
922void SubrangeDesc::dump() {
923 std::cerr << getDescString() << " "
924 << "Tag(" << getTag() << "), "
925 << "Lo(" << Lo << "), "
926 << "Hi(" << Hi << ")\n";
927}
928#endif
929
930//===----------------------------------------------------------------------===//
931
Jim Laskey862001a2006-03-01 23:52:37 +0000932EnumeratorDesc::EnumeratorDesc()
933: DebugInfoDesc(DW_TAG_enumerator)
934, Name("")
935, Value(0)
936{}
937
938// Implement isa/cast/dyncast.
939bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
940 return D->getTag() == DW_TAG_enumerator;
941}
942
943/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
944///
945void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
946 DebugInfoDesc::ApplyToFields(Visitor);
947
948 Visitor->Apply(Name);
949 Visitor->Apply(Value);
950}
951
952/// getDescString - Return a string used to compose global names and labels.
953///
954const char *EnumeratorDesc::getDescString() const {
955 return "llvm.dbg.enumerator";
956}
957
958/// getTypeString - Return a string used to label this descriptor's type.
959///
960const char *EnumeratorDesc::getTypeString() const {
961 return "llvm.dbg.enumerator.type";
962}
963
964#ifndef NDEBUG
965void EnumeratorDesc::dump() {
966 std::cerr << getDescString() << " "
967 << "Tag(" << getTag() << "), "
968 << "Name(" << Name << "), "
969 << "Value(" << Value << ")\n";
970}
971#endif
972
973//===----------------------------------------------------------------------===//
974
Jim Laskey5995d012006-02-11 01:01:30 +0000975GlobalDesc::GlobalDesc(unsigned T)
976: AnchoredDesc(T)
977, Context(0)
978, Name("")
979, TyDesc(NULL)
980, IsStatic(false)
981, IsDefinition(false)
982{}
983
984/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000985///
Jim Laskey5995d012006-02-11 01:01:30 +0000986void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
987 AnchoredDesc::ApplyToFields(Visitor);
988
Jim Laskeyb643ff52006-02-06 19:12:02 +0000989 Visitor->Apply(Context);
990 Visitor->Apply(Name);
Jim Laskey69b9e262006-02-23 16:58:18 +0000991 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000992 Visitor->Apply(IsStatic);
993 Visitor->Apply(IsDefinition);
Jim Laskey5995d012006-02-11 01:01:30 +0000994}
995
996//===----------------------------------------------------------------------===//
997
998GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000999: GlobalDesc(DW_TAG_variable)
Jim Laskey5995d012006-02-11 01:01:30 +00001000, Global(NULL)
1001{}
1002
Jim Laskey4e71db12006-03-01 20:39:36 +00001003// Implement isa/cast/dyncast.
1004bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1005 return D->getTag() == DW_TAG_variable;
1006}
1007
Jim Laskey5995d012006-02-11 01:01:30 +00001008/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1009///
1010void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1011 GlobalDesc::ApplyToFields(Visitor);
1012
Jim Laskeyb643ff52006-02-06 19:12:02 +00001013 Visitor->Apply(Global);
Jim Laskey2fa33a92006-02-22 19:02:11 +00001014 Visitor->Apply(Line);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001015}
1016
Jim Laskey5995d012006-02-11 01:01:30 +00001017/// getDescString - Return a string used to compose global names and labels.
Jim Laskey0bbdc552006-01-26 20:21:46 +00001018///
Jim Laskey5995d012006-02-11 01:01:30 +00001019const char *GlobalVariableDesc::getDescString() const {
1020 return "llvm.dbg.global_variable";
1021}
1022
1023/// getTypeString - Return a string used to label this descriptors type.
1024///
1025const char *GlobalVariableDesc::getTypeString() const {
1026 return "llvm.dbg.global_variable.type";
1027}
1028
1029/// getAnchorString - Return a string used to label this descriptor's anchor.
1030///
Jim Laskey69effa22006-03-07 20:53:47 +00001031const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskey5995d012006-02-11 01:01:30 +00001032const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskey69effa22006-03-07 20:53:47 +00001033 return AnchorString;
Jim Laskey0bbdc552006-01-26 20:21:46 +00001034}
1035
Jim Laskey85263232006-02-06 15:33:21 +00001036#ifndef NDEBUG
1037void GlobalVariableDesc::dump() {
Jim Laskey5995d012006-02-11 01:01:30 +00001038 std::cerr << getDescString() << " "
Jim Laskey85263232006-02-06 15:33:21 +00001039 << "Tag(" << getTag() << "), "
Jim Laskey5995d012006-02-11 01:01:30 +00001040 << "Anchor(" << getAnchor() << "), "
1041 << "Name(\"" << getName() << "\"), "
Jim Laskey69b9e262006-02-23 16:58:18 +00001042 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskey5995d012006-02-11 01:01:30 +00001043 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1044 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey2fa33a92006-02-22 19:02:11 +00001045 << "Global(" << Global << "), "
1046 << "Line(" << Line << ")\n";
Jim Laskey85263232006-02-06 15:33:21 +00001047}
1048#endif
1049
1050//===----------------------------------------------------------------------===//
1051
Jim Laskey5995d012006-02-11 01:01:30 +00001052SubprogramDesc::SubprogramDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +00001053: GlobalDesc(DW_TAG_subprogram)
Jim Laskey5995d012006-02-11 01:01:30 +00001054{}
1055
Jim Laskey4e71db12006-03-01 20:39:36 +00001056// Implement isa/cast/dyncast.
1057bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1058 return D->getTag() == DW_TAG_subprogram;
1059}
1060
Jim Laskeyb643ff52006-02-06 19:12:02 +00001061/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey85263232006-02-06 15:33:21 +00001062/// SubprogramDesc.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001063void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey5995d012006-02-11 01:01:30 +00001064 GlobalDesc::ApplyToFields(Visitor);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001065}
1066
Jim Laskey5995d012006-02-11 01:01:30 +00001067/// getDescString - Return a string used to compose global names and labels.
Jim Laskey0bbdc552006-01-26 20:21:46 +00001068///
Jim Laskey5995d012006-02-11 01:01:30 +00001069const char *SubprogramDesc::getDescString() const {
1070 return "llvm.dbg.subprogram";
1071}
1072
1073/// getTypeString - Return a string used to label this descriptors type.
1074///
1075const char *SubprogramDesc::getTypeString() const {
1076 return "llvm.dbg.subprogram.type";
1077}
1078
1079/// getAnchorString - Return a string used to label this descriptor's anchor.
1080///
Jim Laskey69effa22006-03-07 20:53:47 +00001081const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskey5995d012006-02-11 01:01:30 +00001082const char *SubprogramDesc::getAnchorString() const {
Jim Laskey69effa22006-03-07 20:53:47 +00001083 return AnchorString;
Jim Laskey0bbdc552006-01-26 20:21:46 +00001084}
1085
Jim Laskey85263232006-02-06 15:33:21 +00001086#ifndef NDEBUG
1087void SubprogramDesc::dump() {
Jim Laskey5995d012006-02-11 01:01:30 +00001088 std::cerr << getDescString() << " "
Jim Laskey85263232006-02-06 15:33:21 +00001089 << "Tag(" << getTag() << "), "
Jim Laskey5995d012006-02-11 01:01:30 +00001090 << "Anchor(" << getAnchor() << "), "
1091 << "Name(\"" << getName() << "\"), "
Jim Laskey69b9e262006-02-23 16:58:18 +00001092 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskey5995d012006-02-11 01:01:30 +00001093 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1094 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey85263232006-02-06 15:33:21 +00001095}
1096#endif
1097
Jim Laskey716edb92006-02-28 20:15:07 +00001098//===----------------------------------------------------------------------===//
1099
Jim Laskey85263232006-02-06 15:33:21 +00001100DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskey5995d012006-02-11 01:01:30 +00001101 return Deserialize(getGlobalVariable(V));
Jim Laskey85263232006-02-06 15:33:21 +00001102}
1103DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskey5995d012006-02-11 01:01:30 +00001104 // Handle NULL.
1105 if (!GV) return NULL;
1106
Jim Laskey85263232006-02-06 15:33:21 +00001107 // Check to see if it has been already deserialized.
1108 DebugInfoDesc *&Slot = GlobalDescs[GV];
1109 if (Slot) return Slot;
1110
1111 // Get the Tag from the global.
1112 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1113
1114 // Get the debug version if a compile unit.
Jim Laskey4e71db12006-03-01 20:39:36 +00001115 if (Tag == DW_TAG_compile_unit) {
Jim Laskey85263232006-02-06 15:33:21 +00001116 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1117 }
1118
1119 // Create an empty instance of the correct sort.
1120 Slot = DebugInfoDesc::DescFactory(Tag);
1121 assert(Slot && "Unknown Tag");
1122
1123 // Deserialize the fields.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001124 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey85263232006-02-06 15:33:21 +00001125 DRAM.ApplyToFields(Slot);
1126
1127 return Slot;
1128}
1129
1130//===----------------------------------------------------------------------===//
1131
1132/// getStrPtrType - Return a "sbyte *" type.
Jim Laskey0bbdc552006-01-26 20:21:46 +00001133///
Jim Laskey85263232006-02-06 15:33:21 +00001134const PointerType *DISerializer::getStrPtrType() {
1135 // If not already defined.
1136 if (!StrPtrTy) {
1137 // Construct the pointer to signed bytes.
1138 StrPtrTy = PointerType::get(Type::SByteTy);
1139 }
1140
1141 return StrPtrTy;
1142}
1143
1144/// getEmptyStructPtrType - Return a "{ }*" type.
1145///
1146const PointerType *DISerializer::getEmptyStructPtrType() {
1147 // If not already defined.
1148 if (!EmptyStructPtrTy) {
1149 // Construct the empty structure type.
1150 const StructType *EmptyStructTy =
1151 StructType::get(std::vector<const Type*>());
1152 // Construct the pointer to empty structure type.
1153 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1154 }
1155
1156 return EmptyStructPtrTy;
1157}
1158
1159/// getTagType - Return the type describing the specified descriptor (via tag.)
1160///
1161const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1162 // Attempt to get the previously defined type.
1163 StructType *&Ty = TagTypes[DD->getTag()];
1164
1165 // If not already defined.
1166 if (!Ty) {
Jim Laskey85263232006-02-06 15:33:21 +00001167 // Set up fields vector.
1168 std::vector<const Type*> Fields;
Jim Laskey5995d012006-02-11 01:01:30 +00001169 // Get types of fields.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001170 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey85263232006-02-06 15:33:21 +00001171 GTAM.ApplyToFields(DD);
1172
1173 // Construct structured type.
1174 Ty = StructType::get(Fields);
1175
Jim Laskey85263232006-02-06 15:33:21 +00001176 // Register type name with module.
Jim Laskey5995d012006-02-11 01:01:30 +00001177 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey85263232006-02-06 15:33:21 +00001178 }
1179
1180 return Ty;
1181}
1182
1183/// getString - Construct the string as constant string global.
1184///
Jim Laskey5995d012006-02-11 01:01:30 +00001185Constant *DISerializer::getString(const std::string &String) {
Jim Laskey85263232006-02-06 15:33:21 +00001186 // Check string cache for previous edition.
Jim Laskey5995d012006-02-11 01:01:30 +00001187 Constant *&Slot = StringCache[String];
1188 // return Constant if previously defined.
Jim Laskey85263232006-02-06 15:33:21 +00001189 if (Slot) return Slot;
Jim Laskey5995d012006-02-11 01:01:30 +00001190 // Construct string as an llvm constant.
Jim Laskey85263232006-02-06 15:33:21 +00001191 Constant *ConstStr = ConstantArray::get(String);
1192 // Otherwise create and return a new string global.
Jim Laskey5995d012006-02-11 01:01:30 +00001193 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1194 GlobalVariable::InternalLinkage,
1195 ConstStr, "str", M);
1196 // Convert to generic string pointer.
1197 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1198 return Slot;
1199
Jim Laskey85263232006-02-06 15:33:21 +00001200}
1201
1202/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1203/// so that it can be serialized to a .bc or .ll file.
1204GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1205 // Check if the DebugInfoDesc is already in the map.
1206 GlobalVariable *&Slot = DescGlobals[DD];
1207
1208 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1209 if (Slot) return Slot;
1210
Jim Laskey85263232006-02-06 15:33:21 +00001211 // Get the type associated with the Tag.
1212 const StructType *Ty = getTagType(DD);
1213
1214 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskey5995d012006-02-11 01:01:30 +00001215 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1216 NULL, DD->getDescString(), M);
Jim Laskey85263232006-02-06 15:33:21 +00001217
1218 // Insert new GlobalVariable in DescGlobals map.
1219 Slot = GV;
1220
1221 // Set up elements vector
1222 std::vector<Constant*> Elements;
Jim Laskey5995d012006-02-11 01:01:30 +00001223 // Add fields.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001224 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey85263232006-02-06 15:33:21 +00001225 SRAM.ApplyToFields(DD);
1226
1227 // Set the globals initializer.
1228 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1229
1230 return GV;
1231}
1232
1233//===----------------------------------------------------------------------===//
1234
1235/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1236/// Mark visited otherwise.
1237bool DIVerifier::markVisited(GlobalVariable *GV) {
1238 // Check if the GlobalVariable is already in the Visited set.
1239 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1240
1241 // See if GlobalVariable exists.
1242 bool Exists = VI != Visited.end() && *VI == GV;
1243
1244 // Insert in set.
1245 if (!Exists) Visited.insert(VI, GV);
1246
1247 return Exists;
1248}
1249
1250/// Verify - Return true if the GlobalVariable appears to be a valid
1251/// serialization of a DebugInfoDesc.
Jim Laskey5995d012006-02-11 01:01:30 +00001252bool DIVerifier::Verify(Value *V) {
1253 return Verify(getGlobalVariable(V));
1254}
Jim Laskey85263232006-02-06 15:33:21 +00001255bool DIVerifier::Verify(GlobalVariable *GV) {
1256 // Check if seen before.
1257 if (markVisited(GV)) return true;
1258
1259 // Get the Tag
Jim Laskeyb643ff52006-02-06 19:12:02 +00001260 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskey4e71db12006-03-01 20:39:36 +00001261 if (Tag == DW_TAG_invalid) return false;
Jim Laskey85263232006-02-06 15:33:21 +00001262
1263 // If a compile unit we need the debug version.
Jim Laskey4e71db12006-03-01 20:39:36 +00001264 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyb643ff52006-02-06 19:12:02 +00001265 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskey4e71db12006-03-01 20:39:36 +00001266 if (DebugVersion == DW_TAG_invalid) return false;
Jim Laskey85263232006-02-06 15:33:21 +00001267 }
1268
1269 // Construct an empty DebugInfoDesc.
1270 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1271 if (!DD) return false;
1272
1273 // Get the initializer constant.
1274 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1275
1276 // Get the operand count.
1277 unsigned N = CI->getNumOperands();
1278
1279 // Get the field count.
1280 unsigned &Slot = Counts[Tag];
1281 if (!Slot) {
1282 // Check the operand count to the field count
Jim Laskeyb643ff52006-02-06 19:12:02 +00001283 DICountVisitor CTAM;
Jim Laskey85263232006-02-06 15:33:21 +00001284 CTAM.ApplyToFields(DD);
1285 Slot = CTAM.getCount();
1286 }
1287
1288 // Field count must equal operand count.
1289 if (Slot != N) {
1290 delete DD;
1291 return false;
1292 }
1293
1294 // Check each field for valid type.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001295 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey85263232006-02-06 15:33:21 +00001296 VRAM.ApplyToFields(DD);
1297
1298 // Release empty DebugInfoDesc.
1299 delete DD;
1300
1301 // Return result of field tests.
1302 return VRAM.isValid();
Jim Laskey0bbdc552006-01-26 20:21:46 +00001303}
1304
1305//===----------------------------------------------------------------------===//
1306
1307
1308MachineDebugInfo::MachineDebugInfo()
Jim Laskey5995d012006-02-11 01:01:30 +00001309: DR()
Jim Laskey85263232006-02-06 15:33:21 +00001310, CompileUnits()
Jim Laskey0bbdc552006-01-26 20:21:46 +00001311, Directories()
1312, SourceFiles()
1313, Lines()
1314{
1315
1316}
1317MachineDebugInfo::~MachineDebugInfo() {
1318
1319}
1320
Jim Laskey219d5592006-01-04 22:28:25 +00001321/// doInitialization - Initialize the debug state for a new module.
1322///
1323bool MachineDebugInfo::doInitialization() {
1324 return false;
Jim Laskey44317392006-01-04 13:36:38 +00001325}
1326
Jim Laskey219d5592006-01-04 22:28:25 +00001327/// doFinalization - Tear down the debug state after completion of a module.
1328///
1329bool MachineDebugInfo::doFinalization() {
1330 return false;
1331}
Jim Laskey0bbdc552006-01-26 20:21:46 +00001332
Jim Laskey390c63e2006-02-13 12:50:39 +00001333/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskey5995d012006-02-11 01:01:30 +00001334///
Jim Laskey390c63e2006-02-13 12:50:39 +00001335// FIXME - use new Value type when available.
1336DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskey5995d012006-02-11 01:01:30 +00001337 return DR.Deserialize(V);
1338}
1339
1340/// Verify - Verify that a Value is debug information descriptor.
1341///
1342bool MachineDebugInfo::Verify(Value *V) {
1343 DIVerifier VR;
1344 return VR.Verify(V);
1345}
1346
Jim Laskey0bbdc552006-01-26 20:21:46 +00001347/// AnalyzeModule - Scan the module for global debug information.
1348///
1349void MachineDebugInfo::AnalyzeModule(Module &M) {
1350 SetupCompileUnits(M);
1351}
1352
1353/// SetupCompileUnits - Set up the unique vector of compile units.
1354///
1355void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey2fa33a92006-02-22 19:02:11 +00001356 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001357
Jim Laskey2fa33a92006-02-22 19:02:11 +00001358 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1359 CompileUnits.insert(CU[i]);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001360 }
Jim Laskey0bbdc552006-01-26 20:21:46 +00001361}
1362
Jim Laskey0689dfa2006-01-26 21:22:49 +00001363/// getCompileUnits - Return a vector of debug compile units.
1364///
Jim Laskey85263232006-02-06 15:33:21 +00001365const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey0689dfa2006-01-26 21:22:49 +00001366 return CompileUnits;
1367}
1368
Jim Laskey2fa33a92006-02-22 19:02:11 +00001369/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1370/// named GlobalVariable.
1371std::vector<GlobalVariable*>
1372MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1373 const std::string &RootName) {
1374 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001375}