blob: 9e76daa4b78730dab09bb46f002e4bc7d2040d26 [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());
Jim Laskey313570f2006-03-07 22:00:35 +0000353 CAGV->setSection("llvm.metadata");
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000354 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey716edb92006-02-28 20:15:07 +0000355 Elements.push_back(CAE);
356 }
Jim Laskey85263232006-02-06 15:33:21 +0000357};
358
359//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000360/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey85263232006-02-06 15:33:21 +0000361/// the supplied DebugInfoDesc.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000362class DIGetTypesVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000363private:
364 DISerializer &SR; // Active serializer.
365 std::vector<const Type*> &Fields; // Type accumulator.
366
367public:
Jim Laskeyb643ff52006-02-06 19:12:02 +0000368 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
369 : DIVisitor()
Jim Laskey85263232006-02-06 15:33:21 +0000370 , SR(S)
371 , Fields(F)
372 {}
373
374 /// Apply - Set the value of each of the fields.
375 ///
376 virtual void Apply(int &Field) {
377 Fields.push_back(Type::IntTy);
378 }
379 virtual void Apply(unsigned &Field) {
380 Fields.push_back(Type::UIntTy);
381 }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000382 virtual void Apply(int64_t &Field) {
383 Fields.push_back(Type::IntTy);
384 }
Jim Laskey69b9e262006-02-23 16:58:18 +0000385 virtual void Apply(uint64_t &Field) {
386 Fields.push_back(Type::UIntTy);
387 }
Jim Laskey85263232006-02-06 15:33:21 +0000388 virtual void Apply(bool &Field) {
389 Fields.push_back(Type::BoolTy);
390 }
391 virtual void Apply(std::string &Field) {
392 Fields.push_back(SR.getStrPtrType());
393 }
394 virtual void Apply(DebugInfoDesc *&Field) {
395 // FIXME - At some point should use specific type.
396 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
397 Fields.push_back(EmptyTy);
398 }
399 virtual void Apply(GlobalVariable *&Field) {
400 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
401 Fields.push_back(EmptyTy);
402 }
Jim Laskey716edb92006-02-28 20:15:07 +0000403 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
404 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
405 Fields.push_back(EmptyTy);
406 }
Jim Laskey85263232006-02-06 15:33:21 +0000407};
408
409//===----------------------------------------------------------------------===//
Jim Laskeyb643ff52006-02-06 19:12:02 +0000410/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey85263232006-02-06 15:33:21 +0000411/// a constant initializer.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000412class DIVerifyVisitor : public DIVisitor {
Jim Laskey85263232006-02-06 15:33:21 +0000413private:
414 DIVerifier &VR; // Active verifier.
415 bool IsValid; // Validity status.
416 unsigned I; // Current operand index.
417 ConstantStruct *CI; // GlobalVariable constant initializer.
418
419public:
Jim Laskeyb643ff52006-02-06 19:12:02 +0000420 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
421 : DIVisitor()
Jim Laskey85263232006-02-06 15:33:21 +0000422 , VR(V)
423 , IsValid(true)
Jim Laskey5995d012006-02-11 01:01:30 +0000424 , I(0)
Jim Laskey85263232006-02-06 15:33:21 +0000425 , CI(cast<ConstantStruct>(GV->getInitializer()))
426 {
427 }
428
429 // Accessors.
430 bool isValid() const { return IsValid; }
431
432 /// Apply - Set the value of each of the fields.
433 ///
434 virtual void Apply(int &Field) {
435 Constant *C = CI->getOperand(I++);
436 IsValid = IsValid && isa<ConstantInt>(C);
437 }
438 virtual void Apply(unsigned &Field) {
439 Constant *C = CI->getOperand(I++);
440 IsValid = IsValid && isa<ConstantInt>(C);
441 }
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000442 virtual void Apply(int64_t &Field) {
443 Constant *C = CI->getOperand(I++);
444 IsValid = IsValid && isa<ConstantInt>(C);
445 }
Jim Laskey69b9e262006-02-23 16:58:18 +0000446 virtual void Apply(uint64_t &Field) {
447 Constant *C = CI->getOperand(I++);
448 IsValid = IsValid && isa<ConstantInt>(C);
449 }
Jim Laskey85263232006-02-06 15:33:21 +0000450 virtual void Apply(bool &Field) {
451 Constant *C = CI->getOperand(I++);
452 IsValid = IsValid && isa<ConstantBool>(C);
453 }
454 virtual void Apply(std::string &Field) {
455 Constant *C = CI->getOperand(I++);
456 IsValid = IsValid && isStringValue(C);
457 }
458 virtual void Apply(DebugInfoDesc *&Field) {
459 // FIXME - Prepare the correct descriptor.
460 Constant *C = CI->getOperand(I++);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000461 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey85263232006-02-06 15:33:21 +0000462 }
463 virtual void Apply(GlobalVariable *&Field) {
464 Constant *C = CI->getOperand(I++);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000465 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey85263232006-02-06 15:33:21 +0000466 }
Jim Laskey716edb92006-02-28 20:15:07 +0000467 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
468 Constant *C = CI->getOperand(I++);
469 IsValid = IsValid && isGlobalVariable(C);
470 if (!IsValid) return;
471
472 GlobalVariable *GV = getGlobalVariable(C);
473 IsValid = IsValid && GV && GV->hasInitializer();
474 if (!IsValid) return;
475
476 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
477 IsValid = IsValid && CA;
478 if (!IsValid) return;
479
480 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
481 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
482 if (!IsValid) return;
483
484 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
485 VR.Verify(GVE);
486 }
487 }
Jim Laskey85263232006-02-06 15:33:21 +0000488};
489
Jim Laskey5995d012006-02-11 01:01:30 +0000490
Jim Laskey85263232006-02-06 15:33:21 +0000491//===----------------------------------------------------------------------===//
492
Jim Laskey5995d012006-02-11 01:01:30 +0000493/// TagFromGlobal - Returns the Tag number from a debug info descriptor
494/// GlobalVariable.
495unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
496 ConstantUInt *C = getUIntOperand(GV, 0);
Jim Laskey4e71db12006-03-01 20:39:36 +0000497 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey5995d012006-02-11 01:01:30 +0000498}
499
500/// DescFactory - Create an instance of debug info descriptor based on Tag.
501/// Return NULL if not a recognized Tag.
502DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
503 switch (Tag) {
Jim Laskey4e71db12006-03-01 20:39:36 +0000504 case DW_TAG_anchor: return new AnchorDesc();
505 case DW_TAG_compile_unit: return new CompileUnitDesc();
506 case DW_TAG_variable: return new GlobalVariableDesc();
507 case DW_TAG_subprogram: return new SubprogramDesc();
508 case DW_TAG_base_type: return new BasicTypeDesc();
509 case DW_TAG_typedef:
510 case DW_TAG_pointer_type:
511 case DW_TAG_reference_type:
512 case DW_TAG_const_type:
513 case DW_TAG_volatile_type:
514 case DW_TAG_restrict_type: return new DerivedTypeDesc(Tag);
515 case DW_TAG_array_type:
516 case DW_TAG_structure_type:
517 case DW_TAG_union_type:
518 case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
519 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey88f0fe12006-03-03 15:06:57 +0000520 case DW_TAG_member: return new DerivedTypeDesc(DW_TAG_member);
Jim Laskey862001a2006-03-01 23:52:37 +0000521 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskey5995d012006-02-11 01:01:30 +0000522 default: break;
523 }
524 return NULL;
525}
526
527/// getLinkage - get linkage appropriate for this type of descriptor.
528///
529GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
530 return GlobalValue::InternalLinkage;
531}
532
533/// ApplyToFields - Target the vistor to the fields of the descriptor.
534///
535void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
536 Visitor->Apply(Tag);
537}
538
539//===----------------------------------------------------------------------===//
540
Jim Laskey4e71db12006-03-01 20:39:36 +0000541AnchorDesc::AnchorDesc()
542: DebugInfoDesc(DW_TAG_anchor)
Jim Laskey69effa22006-03-07 20:53:47 +0000543, AnchorTag(0)
Jim Laskey4e71db12006-03-01 20:39:36 +0000544{}
Jim Laskey69effa22006-03-07 20:53:47 +0000545AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey4e71db12006-03-01 20:39:36 +0000546: DebugInfoDesc(DW_TAG_anchor)
Jim Laskey69effa22006-03-07 20:53:47 +0000547, AnchorTag(D->getTag())
Jim Laskey4e71db12006-03-01 20:39:36 +0000548{}
549
550// Implement isa/cast/dyncast.
551bool AnchorDesc::classof(const DebugInfoDesc *D) {
552 return D->getTag() == DW_TAG_anchor;
553}
554
Jim Laskey5995d012006-02-11 01:01:30 +0000555/// getLinkage - get linkage appropriate for this type of descriptor.
556///
557GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
558 return GlobalValue::LinkOnceLinkage;
559}
560
561/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
562///
563void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
564 DebugInfoDesc::ApplyToFields(Visitor);
565
Jim Laskey69effa22006-03-07 20:53:47 +0000566 Visitor->Apply(AnchorTag);
Jim Laskey5995d012006-02-11 01:01:30 +0000567}
568
Jim Laskey69effa22006-03-07 20:53:47 +0000569/// getDescString - Return a string used to compose global names and labels. A
570/// A global variable name needs to be defined for each debug descriptor that is
571/// anchored. NOTE: that each global variable name here also needs to be added
572/// to the list of names left external in the internalizer.
573/// ExternalNames.insert("llvm.dbg.compile_units");
574/// ExternalNames.insert("llvm.dbg.global_variables");
575/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskey5995d012006-02-11 01:01:30 +0000576const char *AnchorDesc::getDescString() const {
Jim Laskey69effa22006-03-07 20:53:47 +0000577 switch (AnchorTag) {
578 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
579 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
580 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
581 default: break;
582 }
583
584 assert(0 && "Tag does not have a case for anchor string");
585 return "";
Jim Laskey5995d012006-02-11 01:01:30 +0000586}
587
588/// getTypeString - Return a string used to label this descriptors type.
589///
590const char *AnchorDesc::getTypeString() const {
591 return "llvm.dbg.anchor.type";
592}
593
594#ifndef NDEBUG
595void AnchorDesc::dump() {
596 std::cerr << getDescString() << " "
597 << "Tag(" << getTag() << "), "
Jim Laskey69effa22006-03-07 20:53:47 +0000598 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskey5995d012006-02-11 01:01:30 +0000599}
600#endif
601
602//===----------------------------------------------------------------------===//
603
604AnchoredDesc::AnchoredDesc(unsigned T)
605: DebugInfoDesc(T)
606, Anchor(NULL)
607{}
608
609/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
610///
611void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
612 DebugInfoDesc::ApplyToFields(Visitor);
613
614 Visitor->Apply((DebugInfoDesc *&)Anchor);
615}
616
617//===----------------------------------------------------------------------===//
618
619CompileUnitDesc::CompileUnitDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000620: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskey5995d012006-02-11 01:01:30 +0000621, DebugVersion(LLVMDebugVersion)
622, Language(0)
623, FileName("")
624, Directory("")
625, Producer("")
626{}
627
Jim Laskey4e71db12006-03-01 20:39:36 +0000628// Implement isa/cast/dyncast.
629bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
630 return D->getTag() == DW_TAG_compile_unit;
631}
632
Jim Laskey85263232006-02-06 15:33:21 +0000633/// DebugVersionFromGlobal - Returns the version number from a compile unit
634/// GlobalVariable.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000635unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
Jim Laskey5995d012006-02-11 01:01:30 +0000636 ConstantUInt *C = getUIntOperand(GV, 2);
Jim Laskey4e71db12006-03-01 20:39:36 +0000637 return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
Jim Laskey85263232006-02-06 15:33:21 +0000638}
639
Jim Laskeyb643ff52006-02-06 19:12:02 +0000640/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
641///
642void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey5995d012006-02-11 01:01:30 +0000643 AnchoredDesc::ApplyToFields(Visitor);
644
Jim Laskeyb643ff52006-02-06 19:12:02 +0000645 Visitor->Apply(DebugVersion);
646 Visitor->Apply(Language);
647 Visitor->Apply(FileName);
648 Visitor->Apply(Directory);
649 Visitor->Apply(Producer);
Jim Laskey85263232006-02-06 15:33:21 +0000650}
651
Jim Laskey5995d012006-02-11 01:01:30 +0000652/// getDescString - Return a string used to compose global names and labels.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000653///
Jim Laskey5995d012006-02-11 01:01:30 +0000654const char *CompileUnitDesc::getDescString() const {
655 return "llvm.dbg.compile_unit";
656}
657
658/// getTypeString - Return a string used to label this descriptors type.
659///
660const char *CompileUnitDesc::getTypeString() const {
661 return "llvm.dbg.compile_unit.type";
662}
663
664/// getAnchorString - Return a string used to label this descriptor's anchor.
665///
Jim Laskey69effa22006-03-07 20:53:47 +0000666const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskey5995d012006-02-11 01:01:30 +0000667const char *CompileUnitDesc::getAnchorString() const {
Jim Laskey69effa22006-03-07 20:53:47 +0000668 return AnchorString;
Jim Laskey0bbdc552006-01-26 20:21:46 +0000669}
670
Jim Laskey85263232006-02-06 15:33:21 +0000671#ifndef NDEBUG
672void CompileUnitDesc::dump() {
Jim Laskey5995d012006-02-11 01:01:30 +0000673 std::cerr << getDescString() << " "
Jim Laskey85263232006-02-06 15:33:21 +0000674 << "Tag(" << getTag() << "), "
Jim Laskey5995d012006-02-11 01:01:30 +0000675 << "Anchor(" << getAnchor() << "), "
676 << "DebugVersion(" << DebugVersion << "), "
Jim Laskey85263232006-02-06 15:33:21 +0000677 << "Language(" << Language << "), "
678 << "FileName(\"" << FileName << "\"), "
679 << "Directory(\"" << Directory << "\"), "
680 << "Producer(\"" << Producer << "\")\n";
681}
682#endif
683
684//===----------------------------------------------------------------------===//
685
Jim Laskey69b9e262006-02-23 16:58:18 +0000686TypeDesc::TypeDesc(unsigned T)
687: DebugInfoDesc(T)
688, Context(NULL)
689, Name("")
Jim Laskey723d3e02006-02-24 16:46:40 +0000690, File(NULL)
Jim Laskey69b9e262006-02-23 16:58:18 +0000691, Size(0)
Jim Laskey88f0fe12006-03-03 15:06:57 +0000692, Offset(0)
Jim Laskey69b9e262006-02-23 16:58:18 +0000693{}
694
Jim Laskey723d3e02006-02-24 16:46:40 +0000695/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskey69b9e262006-02-23 16:58:18 +0000696///
697void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
698 DebugInfoDesc::ApplyToFields(Visitor);
699
700 Visitor->Apply(Context);
701 Visitor->Apply(Name);
Jim Laskey723d3e02006-02-24 16:46:40 +0000702 Visitor->Apply((DebugInfoDesc *&)File);
703 Visitor->Apply(Line);
Jim Laskey69b9e262006-02-23 16:58:18 +0000704 Visitor->Apply(Size);
Jim Laskey88f0fe12006-03-03 15:06:57 +0000705 Visitor->Apply(Offset);
Jim Laskey69b9e262006-02-23 16:58:18 +0000706}
707
708/// getDescString - Return a string used to compose global names and labels.
709///
710const char *TypeDesc::getDescString() const {
711 return "llvm.dbg.type";
712}
713
714/// getTypeString - Return a string used to label this descriptor's type.
715///
716const char *TypeDesc::getTypeString() const {
717 return "llvm.dbg.type.type";
718}
719
720#ifndef NDEBUG
721void TypeDesc::dump() {
722 std::cerr << getDescString() << " "
723 << "Tag(" << getTag() << "), "
724 << "Context(" << Context << "), "
725 << "Name(\"" << Name << "\"), "
Jim Laskey723d3e02006-02-24 16:46:40 +0000726 << "File(" << File << "), "
727 << "Line(" << Line << "), "
Jim Laskey88f0fe12006-03-03 15:06:57 +0000728 << "Size(" << Size << "), "
729 << "Offset(" << Offset << ")\n";
Jim Laskey69b9e262006-02-23 16:58:18 +0000730}
731#endif
732
733//===----------------------------------------------------------------------===//
734
735BasicTypeDesc::BasicTypeDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000736: TypeDesc(DW_TAG_base_type)
Jim Laskey69b9e262006-02-23 16:58:18 +0000737, Encoding(0)
738{}
739
Jim Laskey4e71db12006-03-01 20:39:36 +0000740// Implement isa/cast/dyncast.
741bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
742 return D->getTag() == DW_TAG_base_type;
743}
744
Jim Laskey723d3e02006-02-24 16:46:40 +0000745/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskey69b9e262006-02-23 16:58:18 +0000746///
747void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
748 TypeDesc::ApplyToFields(Visitor);
749
750 Visitor->Apply(Encoding);
751}
752
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000753/// getDescString - Return a string used to compose global names and labels.
754///
755const char *BasicTypeDesc::getDescString() const {
756 return "llvm.dbg.basictype";
757}
758
759/// getTypeString - Return a string used to label this descriptor's type.
760///
761const char *BasicTypeDesc::getTypeString() const {
762 return "llvm.dbg.basictype.type";
763}
764
Jim Laskey69b9e262006-02-23 16:58:18 +0000765#ifndef NDEBUG
766void BasicTypeDesc::dump() {
767 std::cerr << getDescString() << " "
768 << "Tag(" << getTag() << "), "
769 << "Context(" << getContext() << "), "
770 << "Name(\"" << getName() << "\"), "
771 << "Size(" << getSize() << "), "
772 << "Encoding(" << Encoding << ")\n";
773}
774#endif
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000775
Jim Laskeye5386d42006-02-23 22:37:30 +0000776//===----------------------------------------------------------------------===//
777
Jim Laskey723d3e02006-02-24 16:46:40 +0000778DerivedTypeDesc::DerivedTypeDesc(unsigned T)
779: TypeDesc(T)
Jim Laskeye5386d42006-02-23 22:37:30 +0000780, FromType(NULL)
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000781{}
Jim Laskeye5386d42006-02-23 22:37:30 +0000782
Jim Laskey4e71db12006-03-01 20:39:36 +0000783// Implement isa/cast/dyncast.
784bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
785 unsigned T = D->getTag();
786 switch (T) {
787 case DW_TAG_typedef:
788 case DW_TAG_pointer_type:
789 case DW_TAG_reference_type:
790 case DW_TAG_const_type:
791 case DW_TAG_volatile_type:
792 case DW_TAG_restrict_type:
Jim Laskey88f0fe12006-03-03 15:06:57 +0000793 case DW_TAG_member:
Jim Laskey4e71db12006-03-01 20:39:36 +0000794 return true;
795 default: break;
796 }
797 return false;
798}
799
Jim Laskey723d3e02006-02-24 16:46:40 +0000800/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskeye5386d42006-02-23 22:37:30 +0000801///
Jim Laskey723d3e02006-02-24 16:46:40 +0000802void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeye5386d42006-02-23 22:37:30 +0000803 TypeDesc::ApplyToFields(Visitor);
804
805 Visitor->Apply((DebugInfoDesc *&)FromType);
Jim Laskeye5386d42006-02-23 22:37:30 +0000806}
807
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000808/// getDescString - Return a string used to compose global names and labels.
809///
810const char *DerivedTypeDesc::getDescString() const {
811 return "llvm.dbg.derivedtype";
812}
813
814/// getTypeString - Return a string used to label this descriptor's type.
815///
816const char *DerivedTypeDesc::getTypeString() const {
817 return "llvm.dbg.derivedtype.type";
818}
819
Jim Laskeye5386d42006-02-23 22:37:30 +0000820#ifndef NDEBUG
Jim Laskey723d3e02006-02-24 16:46:40 +0000821void DerivedTypeDesc::dump() {
Jim Laskeye5386d42006-02-23 22:37:30 +0000822 std::cerr << getDescString() << " "
823 << "Tag(" << getTag() << "), "
824 << "Context(" << getContext() << "), "
825 << "Name(\"" << getName() << "\"), "
826 << "Size(" << getSize() << "), "
Jim Laskey723d3e02006-02-24 16:46:40 +0000827 << "File(" << getFile() << "), "
828 << "Line(" << getLine() << "), "
829 << "FromType(" << FromType << ")\n";
Jim Laskeye5386d42006-02-23 22:37:30 +0000830}
831#endif
Jim Laskey69b9e262006-02-23 16:58:18 +0000832
833//===----------------------------------------------------------------------===//
834
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000835CompositeTypeDesc::CompositeTypeDesc(unsigned T)
836: DerivedTypeDesc(T)
837, Elements()
838{}
839
Jim Laskey4e71db12006-03-01 20:39:36 +0000840// Implement isa/cast/dyncast.
841bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
842 unsigned T = D->getTag();
843 switch (T) {
844 case DW_TAG_array_type:
845 case DW_TAG_structure_type:
846 case DW_TAG_union_type:
847 case DW_TAG_enumeration_type:
848 return true;
849 default: break;
850 }
851 return false;
852}
853
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000854/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
855///
856void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
857 DerivedTypeDesc::ApplyToFields(Visitor);
858
859 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() << " "
877 << "Tag(" << getTag() << "), "
878 << "Context(" << getContext() << "), "
879 << "Name(\"" << getName() << "\"), "
880 << "Size(" << getSize() << "), "
881 << "File(" << getFile() << "), "
882 << "Line(" << getLine() << "), "
883 << "FromType(" << getFromType() << "), "
884 << "Elements.size(" << Elements.size() << ")\n";
885}
886#endif
887
888//===----------------------------------------------------------------------===//
889
890SubrangeDesc::SubrangeDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +0000891: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000892, Lo(0)
893, Hi(0)
894{}
895
Jim Laskey4e71db12006-03-01 20:39:36 +0000896// Implement isa/cast/dyncast.
897bool SubrangeDesc::classof(const DebugInfoDesc *D) {
898 return D->getTag() == DW_TAG_subrange_type;
899}
900
Jim Laskeyb9ac4cb2006-03-01 17:53:02 +0000901/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
902///
903void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
904 DebugInfoDesc::ApplyToFields(Visitor);
905
906 Visitor->Apply(Lo);
907 Visitor->Apply(Hi);
908}
909
910/// getDescString - Return a string used to compose global names and labels.
911///
912const char *SubrangeDesc::getDescString() const {
913 return "llvm.dbg.subrange";
914}
915
916/// getTypeString - Return a string used to label this descriptor's type.
917///
918const char *SubrangeDesc::getTypeString() const {
919 return "llvm.dbg.subrange.type";
920}
921
922#ifndef NDEBUG
923void SubrangeDesc::dump() {
924 std::cerr << getDescString() << " "
925 << "Tag(" << getTag() << "), "
926 << "Lo(" << Lo << "), "
927 << "Hi(" << Hi << ")\n";
928}
929#endif
930
931//===----------------------------------------------------------------------===//
932
Jim Laskey862001a2006-03-01 23:52:37 +0000933EnumeratorDesc::EnumeratorDesc()
934: DebugInfoDesc(DW_TAG_enumerator)
935, Name("")
936, Value(0)
937{}
938
939// Implement isa/cast/dyncast.
940bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
941 return D->getTag() == DW_TAG_enumerator;
942}
943
944/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
945///
946void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
947 DebugInfoDesc::ApplyToFields(Visitor);
948
949 Visitor->Apply(Name);
950 Visitor->Apply(Value);
951}
952
953/// getDescString - Return a string used to compose global names and labels.
954///
955const char *EnumeratorDesc::getDescString() const {
956 return "llvm.dbg.enumerator";
957}
958
959/// getTypeString - Return a string used to label this descriptor's type.
960///
961const char *EnumeratorDesc::getTypeString() const {
962 return "llvm.dbg.enumerator.type";
963}
964
965#ifndef NDEBUG
966void EnumeratorDesc::dump() {
967 std::cerr << getDescString() << " "
968 << "Tag(" << getTag() << "), "
969 << "Name(" << Name << "), "
970 << "Value(" << Value << ")\n";
971}
972#endif
973
974//===----------------------------------------------------------------------===//
975
Jim Laskey5995d012006-02-11 01:01:30 +0000976GlobalDesc::GlobalDesc(unsigned T)
977: AnchoredDesc(T)
978, Context(0)
979, Name("")
980, TyDesc(NULL)
981, IsStatic(false)
982, IsDefinition(false)
983{}
984
985/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyb643ff52006-02-06 19:12:02 +0000986///
Jim Laskey5995d012006-02-11 01:01:30 +0000987void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
988 AnchoredDesc::ApplyToFields(Visitor);
989
Jim Laskeyb643ff52006-02-06 19:12:02 +0000990 Visitor->Apply(Context);
991 Visitor->Apply(Name);
Jim Laskey69b9e262006-02-23 16:58:18 +0000992 Visitor->Apply((DebugInfoDesc *&)TyDesc);
Jim Laskeyb643ff52006-02-06 19:12:02 +0000993 Visitor->Apply(IsStatic);
994 Visitor->Apply(IsDefinition);
Jim Laskey5995d012006-02-11 01:01:30 +0000995}
996
997//===----------------------------------------------------------------------===//
998
999GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +00001000: GlobalDesc(DW_TAG_variable)
Jim Laskey5995d012006-02-11 01:01:30 +00001001, Global(NULL)
1002{}
1003
Jim Laskey4e71db12006-03-01 20:39:36 +00001004// Implement isa/cast/dyncast.
1005bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1006 return D->getTag() == DW_TAG_variable;
1007}
1008
Jim Laskey5995d012006-02-11 01:01:30 +00001009/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1010///
1011void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1012 GlobalDesc::ApplyToFields(Visitor);
1013
Jim Laskeyb643ff52006-02-06 19:12:02 +00001014 Visitor->Apply(Global);
Jim Laskey2fa33a92006-02-22 19:02:11 +00001015 Visitor->Apply(Line);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001016}
1017
Jim Laskey5995d012006-02-11 01:01:30 +00001018/// getDescString - Return a string used to compose global names and labels.
Jim Laskey0bbdc552006-01-26 20:21:46 +00001019///
Jim Laskey5995d012006-02-11 01:01:30 +00001020const char *GlobalVariableDesc::getDescString() const {
1021 return "llvm.dbg.global_variable";
1022}
1023
1024/// getTypeString - Return a string used to label this descriptors type.
1025///
1026const char *GlobalVariableDesc::getTypeString() const {
1027 return "llvm.dbg.global_variable.type";
1028}
1029
1030/// getAnchorString - Return a string used to label this descriptor's anchor.
1031///
Jim Laskey69effa22006-03-07 20:53:47 +00001032const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskey5995d012006-02-11 01:01:30 +00001033const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskey69effa22006-03-07 20:53:47 +00001034 return AnchorString;
Jim Laskey0bbdc552006-01-26 20:21:46 +00001035}
1036
Jim Laskey85263232006-02-06 15:33:21 +00001037#ifndef NDEBUG
1038void GlobalVariableDesc::dump() {
Jim Laskey5995d012006-02-11 01:01:30 +00001039 std::cerr << getDescString() << " "
Jim Laskey85263232006-02-06 15:33:21 +00001040 << "Tag(" << getTag() << "), "
Jim Laskey5995d012006-02-11 01:01:30 +00001041 << "Anchor(" << getAnchor() << "), "
1042 << "Name(\"" << getName() << "\"), "
Jim Laskey69b9e262006-02-23 16:58:18 +00001043 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskey5995d012006-02-11 01:01:30 +00001044 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1045 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey2fa33a92006-02-22 19:02:11 +00001046 << "Global(" << Global << "), "
1047 << "Line(" << Line << ")\n";
Jim Laskey85263232006-02-06 15:33:21 +00001048}
1049#endif
1050
1051//===----------------------------------------------------------------------===//
1052
Jim Laskey5995d012006-02-11 01:01:30 +00001053SubprogramDesc::SubprogramDesc()
Jim Laskey4e71db12006-03-01 20:39:36 +00001054: GlobalDesc(DW_TAG_subprogram)
Jim Laskey5995d012006-02-11 01:01:30 +00001055{}
1056
Jim Laskey4e71db12006-03-01 20:39:36 +00001057// Implement isa/cast/dyncast.
1058bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1059 return D->getTag() == DW_TAG_subprogram;
1060}
1061
Jim Laskeyb643ff52006-02-06 19:12:02 +00001062/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey85263232006-02-06 15:33:21 +00001063/// SubprogramDesc.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001064void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey5995d012006-02-11 01:01:30 +00001065 GlobalDesc::ApplyToFields(Visitor);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001066}
1067
Jim Laskey5995d012006-02-11 01:01:30 +00001068/// getDescString - Return a string used to compose global names and labels.
Jim Laskey0bbdc552006-01-26 20:21:46 +00001069///
Jim Laskey5995d012006-02-11 01:01:30 +00001070const char *SubprogramDesc::getDescString() const {
1071 return "llvm.dbg.subprogram";
1072}
1073
1074/// getTypeString - Return a string used to label this descriptors type.
1075///
1076const char *SubprogramDesc::getTypeString() const {
1077 return "llvm.dbg.subprogram.type";
1078}
1079
1080/// getAnchorString - Return a string used to label this descriptor's anchor.
1081///
Jim Laskey69effa22006-03-07 20:53:47 +00001082const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskey5995d012006-02-11 01:01:30 +00001083const char *SubprogramDesc::getAnchorString() const {
Jim Laskey69effa22006-03-07 20:53:47 +00001084 return AnchorString;
Jim Laskey0bbdc552006-01-26 20:21:46 +00001085}
1086
Jim Laskey85263232006-02-06 15:33:21 +00001087#ifndef NDEBUG
1088void SubprogramDesc::dump() {
Jim Laskey5995d012006-02-11 01:01:30 +00001089 std::cerr << getDescString() << " "
Jim Laskey85263232006-02-06 15:33:21 +00001090 << "Tag(" << getTag() << "), "
Jim Laskey5995d012006-02-11 01:01:30 +00001091 << "Anchor(" << getAnchor() << "), "
1092 << "Name(\"" << getName() << "\"), "
Jim Laskey69b9e262006-02-23 16:58:18 +00001093 << "Type(\"" << getTypeDesc() << "\"), "
Jim Laskey5995d012006-02-11 01:01:30 +00001094 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1095 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey85263232006-02-06 15:33:21 +00001096}
1097#endif
1098
Jim Laskey716edb92006-02-28 20:15:07 +00001099//===----------------------------------------------------------------------===//
1100
Jim Laskey85263232006-02-06 15:33:21 +00001101DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskey5995d012006-02-11 01:01:30 +00001102 return Deserialize(getGlobalVariable(V));
Jim Laskey85263232006-02-06 15:33:21 +00001103}
1104DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskey5995d012006-02-11 01:01:30 +00001105 // Handle NULL.
1106 if (!GV) return NULL;
1107
Jim Laskey85263232006-02-06 15:33:21 +00001108 // Check to see if it has been already deserialized.
1109 DebugInfoDesc *&Slot = GlobalDescs[GV];
1110 if (Slot) return Slot;
1111
1112 // Get the Tag from the global.
1113 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1114
1115 // Get the debug version if a compile unit.
Jim Laskey4e71db12006-03-01 20:39:36 +00001116 if (Tag == DW_TAG_compile_unit) {
Jim Laskey85263232006-02-06 15:33:21 +00001117 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1118 }
1119
1120 // Create an empty instance of the correct sort.
1121 Slot = DebugInfoDesc::DescFactory(Tag);
1122 assert(Slot && "Unknown Tag");
1123
1124 // Deserialize the fields.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001125 DIDeserializeVisitor DRAM(*this, GV);
Jim Laskey85263232006-02-06 15:33:21 +00001126 DRAM.ApplyToFields(Slot);
1127
1128 return Slot;
1129}
1130
1131//===----------------------------------------------------------------------===//
1132
1133/// getStrPtrType - Return a "sbyte *" type.
Jim Laskey0bbdc552006-01-26 20:21:46 +00001134///
Jim Laskey85263232006-02-06 15:33:21 +00001135const PointerType *DISerializer::getStrPtrType() {
1136 // If not already defined.
1137 if (!StrPtrTy) {
1138 // Construct the pointer to signed bytes.
1139 StrPtrTy = PointerType::get(Type::SByteTy);
1140 }
1141
1142 return StrPtrTy;
1143}
1144
1145/// getEmptyStructPtrType - Return a "{ }*" type.
1146///
1147const PointerType *DISerializer::getEmptyStructPtrType() {
1148 // If not already defined.
1149 if (!EmptyStructPtrTy) {
1150 // Construct the empty structure type.
1151 const StructType *EmptyStructTy =
1152 StructType::get(std::vector<const Type*>());
1153 // Construct the pointer to empty structure type.
1154 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1155 }
1156
1157 return EmptyStructPtrTy;
1158}
1159
1160/// getTagType - Return the type describing the specified descriptor (via tag.)
1161///
1162const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1163 // Attempt to get the previously defined type.
1164 StructType *&Ty = TagTypes[DD->getTag()];
1165
1166 // If not already defined.
1167 if (!Ty) {
Jim Laskey85263232006-02-06 15:33:21 +00001168 // Set up fields vector.
1169 std::vector<const Type*> Fields;
Jim Laskey5995d012006-02-11 01:01:30 +00001170 // Get types of fields.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001171 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey85263232006-02-06 15:33:21 +00001172 GTAM.ApplyToFields(DD);
1173
1174 // Construct structured type.
1175 Ty = StructType::get(Fields);
1176
Jim Laskey85263232006-02-06 15:33:21 +00001177 // Register type name with module.
Jim Laskey5995d012006-02-11 01:01:30 +00001178 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey85263232006-02-06 15:33:21 +00001179 }
1180
1181 return Ty;
1182}
1183
1184/// getString - Construct the string as constant string global.
1185///
Jim Laskey5995d012006-02-11 01:01:30 +00001186Constant *DISerializer::getString(const std::string &String) {
Jim Laskey85263232006-02-06 15:33:21 +00001187 // Check string cache for previous edition.
Jim Laskey5995d012006-02-11 01:01:30 +00001188 Constant *&Slot = StringCache[String];
1189 // return Constant if previously defined.
Jim Laskey85263232006-02-06 15:33:21 +00001190 if (Slot) return Slot;
Jim Laskey5995d012006-02-11 01:01:30 +00001191 // Construct string as an llvm constant.
Jim Laskey85263232006-02-06 15:33:21 +00001192 Constant *ConstStr = ConstantArray::get(String);
1193 // Otherwise create and return a new string global.
Jim Laskey5995d012006-02-11 01:01:30 +00001194 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1195 GlobalVariable::InternalLinkage,
1196 ConstStr, "str", M);
Jim Laskey313570f2006-03-07 22:00:35 +00001197 StrGV->setSection("llvm.metadata");
Jim Laskey5995d012006-02-11 01:01:30 +00001198 // Convert to generic string pointer.
1199 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1200 return Slot;
1201
Jim Laskey85263232006-02-06 15:33:21 +00001202}
1203
1204/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1205/// so that it can be serialized to a .bc or .ll file.
1206GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1207 // Check if the DebugInfoDesc is already in the map.
1208 GlobalVariable *&Slot = DescGlobals[DD];
1209
1210 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1211 if (Slot) return Slot;
1212
Jim Laskey85263232006-02-06 15:33:21 +00001213 // Get the type associated with the Tag.
1214 const StructType *Ty = getTagType(DD);
1215
1216 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskey5995d012006-02-11 01:01:30 +00001217 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1218 NULL, DD->getDescString(), M);
Jim Laskey313570f2006-03-07 22:00:35 +00001219 GV->setSection("llvm.metadata");
Jim Laskey85263232006-02-06 15:33:21 +00001220
1221 // Insert new GlobalVariable in DescGlobals map.
1222 Slot = GV;
1223
1224 // Set up elements vector
1225 std::vector<Constant*> Elements;
Jim Laskey5995d012006-02-11 01:01:30 +00001226 // Add fields.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001227 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey85263232006-02-06 15:33:21 +00001228 SRAM.ApplyToFields(DD);
1229
1230 // Set the globals initializer.
1231 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1232
1233 return GV;
1234}
1235
1236//===----------------------------------------------------------------------===//
1237
1238/// markVisited - Return true if the GlobalVariable hase been "seen" before.
1239/// Mark visited otherwise.
1240bool DIVerifier::markVisited(GlobalVariable *GV) {
1241 // Check if the GlobalVariable is already in the Visited set.
1242 std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1243
1244 // See if GlobalVariable exists.
1245 bool Exists = VI != Visited.end() && *VI == GV;
1246
1247 // Insert in set.
1248 if (!Exists) Visited.insert(VI, GV);
1249
1250 return Exists;
1251}
1252
1253/// Verify - Return true if the GlobalVariable appears to be a valid
1254/// serialization of a DebugInfoDesc.
Jim Laskey5995d012006-02-11 01:01:30 +00001255bool DIVerifier::Verify(Value *V) {
1256 return Verify(getGlobalVariable(V));
1257}
Jim Laskey85263232006-02-06 15:33:21 +00001258bool DIVerifier::Verify(GlobalVariable *GV) {
1259 // Check if seen before.
1260 if (markVisited(GV)) return true;
1261
1262 // Get the Tag
Jim Laskeyb643ff52006-02-06 19:12:02 +00001263 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskey4e71db12006-03-01 20:39:36 +00001264 if (Tag == DW_TAG_invalid) return false;
Jim Laskey85263232006-02-06 15:33:21 +00001265
1266 // If a compile unit we need the debug version.
Jim Laskey4e71db12006-03-01 20:39:36 +00001267 if (Tag == DW_TAG_compile_unit) {
Jim Laskeyb643ff52006-02-06 19:12:02 +00001268 DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
Jim Laskey4e71db12006-03-01 20:39:36 +00001269 if (DebugVersion == DW_TAG_invalid) return false;
Jim Laskey85263232006-02-06 15:33:21 +00001270 }
1271
1272 // Construct an empty DebugInfoDesc.
1273 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1274 if (!DD) return false;
1275
1276 // Get the initializer constant.
1277 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1278
1279 // Get the operand count.
1280 unsigned N = CI->getNumOperands();
1281
1282 // Get the field count.
1283 unsigned &Slot = Counts[Tag];
1284 if (!Slot) {
1285 // Check the operand count to the field count
Jim Laskeyb643ff52006-02-06 19:12:02 +00001286 DICountVisitor CTAM;
Jim Laskey85263232006-02-06 15:33:21 +00001287 CTAM.ApplyToFields(DD);
1288 Slot = CTAM.getCount();
1289 }
1290
1291 // Field count must equal operand count.
1292 if (Slot != N) {
1293 delete DD;
1294 return false;
1295 }
1296
1297 // Check each field for valid type.
Jim Laskeyb643ff52006-02-06 19:12:02 +00001298 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey85263232006-02-06 15:33:21 +00001299 VRAM.ApplyToFields(DD);
1300
1301 // Release empty DebugInfoDesc.
1302 delete DD;
1303
1304 // Return result of field tests.
1305 return VRAM.isValid();
Jim Laskey0bbdc552006-01-26 20:21:46 +00001306}
1307
1308//===----------------------------------------------------------------------===//
1309
1310
1311MachineDebugInfo::MachineDebugInfo()
Jim Laskey5995d012006-02-11 01:01:30 +00001312: DR()
Jim Laskey85263232006-02-06 15:33:21 +00001313, CompileUnits()
Jim Laskey0bbdc552006-01-26 20:21:46 +00001314, Directories()
1315, SourceFiles()
1316, Lines()
1317{
1318
1319}
1320MachineDebugInfo::~MachineDebugInfo() {
1321
1322}
1323
Jim Laskey219d5592006-01-04 22:28:25 +00001324/// doInitialization - Initialize the debug state for a new module.
1325///
1326bool MachineDebugInfo::doInitialization() {
1327 return false;
Jim Laskey44317392006-01-04 13:36:38 +00001328}
1329
Jim Laskey219d5592006-01-04 22:28:25 +00001330/// doFinalization - Tear down the debug state after completion of a module.
1331///
1332bool MachineDebugInfo::doFinalization() {
1333 return false;
1334}
Jim Laskey0bbdc552006-01-26 20:21:46 +00001335
Jim Laskey390c63e2006-02-13 12:50:39 +00001336/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskey5995d012006-02-11 01:01:30 +00001337///
Jim Laskey390c63e2006-02-13 12:50:39 +00001338// FIXME - use new Value type when available.
1339DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskey5995d012006-02-11 01:01:30 +00001340 return DR.Deserialize(V);
1341}
1342
1343/// Verify - Verify that a Value is debug information descriptor.
1344///
1345bool MachineDebugInfo::Verify(Value *V) {
1346 DIVerifier VR;
1347 return VR.Verify(V);
1348}
1349
Jim Laskey0bbdc552006-01-26 20:21:46 +00001350/// AnalyzeModule - Scan the module for global debug information.
1351///
1352void MachineDebugInfo::AnalyzeModule(Module &M) {
1353 SetupCompileUnits(M);
1354}
1355
1356/// SetupCompileUnits - Set up the unique vector of compile units.
1357///
1358void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey2fa33a92006-02-22 19:02:11 +00001359 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001360
Jim Laskey2fa33a92006-02-22 19:02:11 +00001361 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1362 CompileUnits.insert(CU[i]);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001363 }
Jim Laskey0bbdc552006-01-26 20:21:46 +00001364}
1365
Jim Laskey0689dfa2006-01-26 21:22:49 +00001366/// getCompileUnits - Return a vector of debug compile units.
1367///
Jim Laskey85263232006-02-06 15:33:21 +00001368const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey0689dfa2006-01-26 21:22:49 +00001369 return CompileUnits;
1370}
1371
Jim Laskey2fa33a92006-02-22 19:02:11 +00001372/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1373/// named GlobalVariable.
1374std::vector<GlobalVariable*>
1375MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1376 const std::string &RootName) {
1377 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskey0bbdc552006-01-26 20:21:46 +00001378}