Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 1 | //===-- 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 Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 9 | |
| 10 | #include "llvm/CodeGen/MachineDebugInfo.h" |
| 11 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 12 | #include "llvm/Constants.h" |
| 13 | #include "llvm/DerivedTypes.h" |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 14 | #include "llvm/GlobalVariable.h" |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 15 | #include "llvm/Intrinsics.h" |
| 16 | #include "llvm/Instructions.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Support/Dwarf.h" |
| 19 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 20 | #include <iostream> |
| 21 | |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | // Handle the Pass registration stuff necessary to use TargetData's. |
| 25 | namespace { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 26 | RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information"); |
| 27 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 28 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 31 | /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 32 | /// specified value in their initializer somewhere. |
| 33 | static void |
| 34 | getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) { |
| 35 | // Scan though value users. |
| 36 | for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { |
| 37 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 38 | // If the user is a GlobalVariable then add to result. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 39 | Result.push_back(GV); |
| 40 | } else if (Constant *C = dyn_cast<Constant>(*I)) { |
| 41 | // If the user is a constant variable then scan its users |
| 42 | getGlobalVariablesUsing(C, Result); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 47 | /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the |
| 48 | /// named GlobalVariable. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 49 | static std::vector<GlobalVariable*> |
| 50 | getGlobalVariablesUsing(Module &M, const std::string &RootName) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 51 | std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 52 | |
| 53 | std::vector<const Type*> FieldTypes; |
| 54 | FieldTypes.push_back(Type::UIntTy); |
| 55 | FieldTypes.push_back(PointerType::get(Type::SByteTy)); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 56 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 57 | // Get the GlobalVariable root. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 58 | GlobalVariable *UseRoot = M.getGlobalVariable(RootName, |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 59 | StructType::get(FieldTypes)); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 60 | |
| 61 | // If present and linkonce then scan for users. |
| 62 | if (UseRoot && UseRoot->hasLinkOnceLinkage()) { |
| 63 | getGlobalVariablesUsing(UseRoot, Result); |
| 64 | } |
| 65 | |
| 66 | return Result; |
| 67 | } |
| 68 | |
| 69 | /// getStringValue - Turn an LLVM constant pointer that eventually points to a |
| 70 | /// global into a string value. Return an empty string if we can't do it. |
| 71 | /// |
Chris Lattner | 22760af | 2006-01-27 17:31:30 +0000 | [diff] [blame] | 72 | static const std::string getStringValue(Value *V, unsigned Offset = 0) { |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 73 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 74 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 75 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 76 | if (Init->isString()) { |
| 77 | std::string Result = Init->getAsString(); |
| 78 | if (Offset < Result.size()) { |
| 79 | // If we are pointing INTO The string, erase the beginning... |
| 80 | Result.erase(Result.begin(), Result.begin()+Offset); |
| 81 | |
| 82 | // Take off the null terminator, and any string fragments after it. |
| 83 | std::string::size_type NullPos = Result.find_first_of((char)0); |
| 84 | if (NullPos != std::string::npos) |
| 85 | Result.erase(Result.begin()+NullPos, Result.end()); |
| 86 | return Result; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } else if (Constant *C = dyn_cast<Constant>(V)) { |
| 91 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 92 | return getStringValue(GV, Offset); |
| 93 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 94 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 95 | // Turn a gep into the specified offset. |
| 96 | if (CE->getNumOperands() == 3 && |
| 97 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 98 | isa<ConstantInt>(CE->getOperand(2))) { |
| 99 | return getStringValue(CE->getOperand(0), |
| 100 | Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue()); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return ""; |
| 106 | } |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 107 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 108 | /// isStringValue - Return true if the given value can be coerced to a string. |
| 109 | /// |
| 110 | static bool isStringValue(Value *V) { |
| 111 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 112 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 113 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 114 | return Init->isString(); |
| 115 | } |
| 116 | } else if (Constant *C = dyn_cast<Constant>(V)) { |
| 117 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 118 | return isStringValue(GV); |
| 119 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 120 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 121 | if (CE->getNumOperands() == 3 && |
| 122 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 123 | isa<ConstantInt>(CE->getOperand(2))) { |
| 124 | return isStringValue(CE->getOperand(0)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 132 | /// getGlobalVariable - Return either a direct or cast Global value. |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 133 | /// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 134 | static GlobalVariable *getGlobalVariable(Value *V) { |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 135 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 136 | return GV; |
| 137 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 138 | if (CE->getOpcode() == Instruction::Cast) { |
| 139 | return dyn_cast<GlobalVariable>(CE->getOperand(0)); |
| 140 | } |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 145 | /// isGlobalVariable - Return true if the given value can be coerced to a |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 146 | /// GlobalVariable. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 147 | static bool isGlobalVariable(Value *V) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 148 | if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) { |
| 149 | return true; |
| 150 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 151 | if (CE->getOpcode() == Instruction::Cast) { |
| 152 | return isa<GlobalVariable>(CE->getOperand(0)); |
| 153 | } |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 158 | /// getUIntOperand - Return ith operand if it is an unsigned integer. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 159 | /// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 160 | static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 161 | // Make sure the GlobalVariable has an initializer. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 162 | if (!GV->hasInitializer()) return NULL; |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 163 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 164 | // Get the initializer constant. |
| 165 | ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer()); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 166 | if (!CI) return NULL; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 167 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 168 | // Check if there is at least i + 1 operands. |
| 169 | unsigned N = CI->getNumOperands(); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 170 | if (i >= N) return NULL; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 171 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 172 | // Check constant. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 173 | return dyn_cast<ConstantUInt>(CI->getOperand(i)); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 174 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 175 | //===----------------------------------------------------------------------===// |
| 176 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 177 | /// ApplyToFields - Target the visitor to each field of the debug information |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 178 | /// descriptor. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 179 | void DIVisitor::ApplyToFields(DebugInfoDesc *DD) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 180 | DD->ApplyToFields(this); |
| 181 | } |
| 182 | |
| 183 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 184 | /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug |
| 185 | /// the supplied DebugInfoDesc. |
| 186 | class DICountVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 187 | private: |
| 188 | unsigned Count; // Running count of fields. |
| 189 | |
| 190 | public: |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 191 | DICountVisitor() : DIVisitor(), Count(0) {} |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 192 | |
| 193 | // Accessors. |
| 194 | unsigned getCount() const { return Count; } |
| 195 | |
| 196 | /// Apply - Count each of the fields. |
| 197 | /// |
| 198 | virtual void Apply(int &Field) { ++Count; } |
| 199 | virtual void Apply(unsigned &Field) { ++Count; } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 200 | virtual void Apply(uint64_t &Field) { ++Count; } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 201 | virtual void Apply(bool &Field) { ++Count; } |
| 202 | virtual void Apply(std::string &Field) { ++Count; } |
| 203 | virtual void Apply(DebugInfoDesc *&Field) { ++Count; } |
| 204 | virtual void Apply(GlobalVariable *&Field) { ++Count; } |
| 205 | }; |
| 206 | |
| 207 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 208 | /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the |
| 209 | /// supplied DebugInfoDesc. |
| 210 | class DIDeserializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 211 | private: |
| 212 | DIDeserializer &DR; // Active deserializer. |
| 213 | unsigned I; // Current operand index. |
| 214 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 215 | |
| 216 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 217 | DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV) |
| 218 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 219 | , DR(D) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 220 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 221 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 222 | {} |
| 223 | |
| 224 | /// Apply - Set the value of each of the fields. |
| 225 | /// |
| 226 | virtual void Apply(int &Field) { |
| 227 | Constant *C = CI->getOperand(I++); |
| 228 | Field = cast<ConstantSInt>(C)->getValue(); |
| 229 | } |
| 230 | virtual void Apply(unsigned &Field) { |
| 231 | Constant *C = CI->getOperand(I++); |
| 232 | Field = cast<ConstantUInt>(C)->getValue(); |
| 233 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 234 | virtual void Apply(uint64_t &Field) { |
| 235 | Constant *C = CI->getOperand(I++); |
| 236 | Field = cast<ConstantUInt>(C)->getValue(); |
| 237 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 238 | virtual void Apply(bool &Field) { |
| 239 | Constant *C = CI->getOperand(I++); |
| 240 | Field = cast<ConstantBool>(C)->getValue(); |
| 241 | } |
| 242 | virtual void Apply(std::string &Field) { |
| 243 | Constant *C = CI->getOperand(I++); |
| 244 | Field = getStringValue(C); |
| 245 | } |
| 246 | virtual void Apply(DebugInfoDesc *&Field) { |
| 247 | Constant *C = CI->getOperand(I++); |
| 248 | Field = DR.Deserialize(C); |
| 249 | } |
| 250 | virtual void Apply(GlobalVariable *&Field) { |
| 251 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 252 | Field = getGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 253 | } |
| 254 | }; |
| 255 | |
| 256 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 257 | /// DISerializeVisitor - This DIVisitor serializes all the fields in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 258 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 259 | class DISerializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 260 | private: |
| 261 | DISerializer &SR; // Active serializer. |
| 262 | std::vector<Constant*> &Elements; // Element accumulator. |
| 263 | |
| 264 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 265 | DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E) |
| 266 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 267 | , SR(S) |
| 268 | , Elements(E) |
| 269 | {} |
| 270 | |
| 271 | /// Apply - Set the value of each of the fields. |
| 272 | /// |
| 273 | virtual void Apply(int &Field) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 274 | Elements.push_back(ConstantSInt::get(Type::IntTy, Field)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 275 | } |
| 276 | virtual void Apply(unsigned &Field) { |
| 277 | Elements.push_back(ConstantUInt::get(Type::UIntTy, Field)); |
| 278 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 279 | virtual void Apply(uint64_t &Field) { |
| 280 | Elements.push_back(ConstantUInt::get(Type::UIntTy, Field)); |
| 281 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 282 | virtual void Apply(bool &Field) { |
| 283 | Elements.push_back(ConstantBool::get(Field)); |
| 284 | } |
| 285 | virtual void Apply(std::string &Field) { |
| 286 | Elements.push_back(SR.getString(Field)); |
| 287 | } |
| 288 | virtual void Apply(DebugInfoDesc *&Field) { |
| 289 | GlobalVariable *GV = NULL; |
| 290 | |
| 291 | // If non-NULL the convert to global. |
| 292 | if (Field) GV = SR.Serialize(Field); |
| 293 | |
| 294 | // FIXME - At some point should use specific type. |
| 295 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 296 | |
| 297 | if (GV) { |
| 298 | // Set to pointer to global. |
| 299 | Elements.push_back(ConstantExpr::getCast(GV, EmptyTy)); |
| 300 | } else { |
| 301 | // Use NULL. |
| 302 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 303 | } |
| 304 | } |
| 305 | virtual void Apply(GlobalVariable *&Field) { |
| 306 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 307 | if (Field) { |
| 308 | Elements.push_back(ConstantExpr::getCast(Field, EmptyTy)); |
| 309 | } else { |
| 310 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 311 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 312 | } |
| 313 | }; |
| 314 | |
| 315 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 316 | /// DIGetTypesVisitor - This DIVisitor gathers all the field types in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 317 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 318 | class DIGetTypesVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 319 | private: |
| 320 | DISerializer &SR; // Active serializer. |
| 321 | std::vector<const Type*> &Fields; // Type accumulator. |
| 322 | |
| 323 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 324 | DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F) |
| 325 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 326 | , SR(S) |
| 327 | , Fields(F) |
| 328 | {} |
| 329 | |
| 330 | /// Apply - Set the value of each of the fields. |
| 331 | /// |
| 332 | virtual void Apply(int &Field) { |
| 333 | Fields.push_back(Type::IntTy); |
| 334 | } |
| 335 | virtual void Apply(unsigned &Field) { |
| 336 | Fields.push_back(Type::UIntTy); |
| 337 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 338 | virtual void Apply(uint64_t &Field) { |
| 339 | Fields.push_back(Type::UIntTy); |
| 340 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 341 | virtual void Apply(bool &Field) { |
| 342 | Fields.push_back(Type::BoolTy); |
| 343 | } |
| 344 | virtual void Apply(std::string &Field) { |
| 345 | Fields.push_back(SR.getStrPtrType()); |
| 346 | } |
| 347 | virtual void Apply(DebugInfoDesc *&Field) { |
| 348 | // FIXME - At some point should use specific type. |
| 349 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 350 | Fields.push_back(EmptyTy); |
| 351 | } |
| 352 | virtual void Apply(GlobalVariable *&Field) { |
| 353 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 354 | Fields.push_back(EmptyTy); |
| 355 | } |
| 356 | }; |
| 357 | |
| 358 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 359 | /// DIVerifyVisitor - This DIVisitor verifies all the field types against |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 360 | /// a constant initializer. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 361 | class DIVerifyVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 362 | private: |
| 363 | DIVerifier &VR; // Active verifier. |
| 364 | bool IsValid; // Validity status. |
| 365 | unsigned I; // Current operand index. |
| 366 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 367 | |
| 368 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 369 | DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV) |
| 370 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 371 | , VR(V) |
| 372 | , IsValid(true) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 373 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 374 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 375 | { |
| 376 | } |
| 377 | |
| 378 | // Accessors. |
| 379 | bool isValid() const { return IsValid; } |
| 380 | |
| 381 | /// Apply - Set the value of each of the fields. |
| 382 | /// |
| 383 | virtual void Apply(int &Field) { |
| 384 | Constant *C = CI->getOperand(I++); |
| 385 | IsValid = IsValid && isa<ConstantInt>(C); |
| 386 | } |
| 387 | virtual void Apply(unsigned &Field) { |
| 388 | Constant *C = CI->getOperand(I++); |
| 389 | IsValid = IsValid && isa<ConstantInt>(C); |
| 390 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 391 | virtual void Apply(uint64_t &Field) { |
| 392 | Constant *C = CI->getOperand(I++); |
| 393 | IsValid = IsValid && isa<ConstantInt>(C); |
| 394 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 395 | virtual void Apply(bool &Field) { |
| 396 | Constant *C = CI->getOperand(I++); |
| 397 | IsValid = IsValid && isa<ConstantBool>(C); |
| 398 | } |
| 399 | virtual void Apply(std::string &Field) { |
| 400 | Constant *C = CI->getOperand(I++); |
| 401 | IsValid = IsValid && isStringValue(C); |
| 402 | } |
| 403 | virtual void Apply(DebugInfoDesc *&Field) { |
| 404 | // FIXME - Prepare the correct descriptor. |
| 405 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 406 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 407 | } |
| 408 | virtual void Apply(GlobalVariable *&Field) { |
| 409 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 410 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 411 | } |
| 412 | }; |
| 413 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 414 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
| 416 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 417 | /// TagFromGlobal - Returns the Tag number from a debug info descriptor |
| 418 | /// GlobalVariable. |
| 419 | unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) { |
| 420 | ConstantUInt *C = getUIntOperand(GV, 0); |
| 421 | return C ? (unsigned)C->getValue() : (unsigned)DIInvalid; |
| 422 | } |
| 423 | |
| 424 | /// DescFactory - Create an instance of debug info descriptor based on Tag. |
| 425 | /// Return NULL if not a recognized Tag. |
| 426 | DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) { |
| 427 | switch (Tag) { |
| 428 | case DI_TAG_anchor: return new AnchorDesc(); |
| 429 | case DI_TAG_compile_unit: return new CompileUnitDesc(); |
| 430 | case DI_TAG_global_variable: return new GlobalVariableDesc(); |
| 431 | case DI_TAG_subprogram: return new SubprogramDesc(); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 432 | case DI_TAG_basictype: return new BasicTypeDesc(); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 433 | default: break; |
| 434 | } |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | /// getLinkage - get linkage appropriate for this type of descriptor. |
| 439 | /// |
| 440 | GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const { |
| 441 | return GlobalValue::InternalLinkage; |
| 442 | } |
| 443 | |
| 444 | /// ApplyToFields - Target the vistor to the fields of the descriptor. |
| 445 | /// |
| 446 | void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) { |
| 447 | Visitor->Apply(Tag); |
| 448 | } |
| 449 | |
| 450 | //===----------------------------------------------------------------------===// |
| 451 | |
| 452 | /// getLinkage - get linkage appropriate for this type of descriptor. |
| 453 | /// |
| 454 | GlobalValue::LinkageTypes AnchorDesc::getLinkage() const { |
| 455 | return GlobalValue::LinkOnceLinkage; |
| 456 | } |
| 457 | |
| 458 | /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc. |
| 459 | /// |
| 460 | void AnchorDesc::ApplyToFields(DIVisitor *Visitor) { |
| 461 | DebugInfoDesc::ApplyToFields(Visitor); |
| 462 | |
| 463 | Visitor->Apply(Name); |
| 464 | } |
| 465 | |
| 466 | /// getDescString - Return a string used to compose global names and labels. |
| 467 | /// |
| 468 | const char *AnchorDesc::getDescString() const { |
| 469 | return Name.c_str(); |
| 470 | } |
| 471 | |
| 472 | /// getTypeString - Return a string used to label this descriptors type. |
| 473 | /// |
| 474 | const char *AnchorDesc::getTypeString() const { |
| 475 | return "llvm.dbg.anchor.type"; |
| 476 | } |
| 477 | |
| 478 | #ifndef NDEBUG |
| 479 | void AnchorDesc::dump() { |
| 480 | std::cerr << getDescString() << " " |
| 481 | << "Tag(" << getTag() << "), " |
| 482 | << "Name(" << Name << ")\n"; |
| 483 | } |
| 484 | #endif |
| 485 | |
| 486 | //===----------------------------------------------------------------------===// |
| 487 | |
| 488 | AnchoredDesc::AnchoredDesc(unsigned T) |
| 489 | : DebugInfoDesc(T) |
| 490 | , Anchor(NULL) |
| 491 | {} |
| 492 | |
| 493 | /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc. |
| 494 | /// |
| 495 | void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) { |
| 496 | DebugInfoDesc::ApplyToFields(Visitor); |
| 497 | |
| 498 | Visitor->Apply((DebugInfoDesc *&)Anchor); |
| 499 | } |
| 500 | |
| 501 | //===----------------------------------------------------------------------===// |
| 502 | |
| 503 | CompileUnitDesc::CompileUnitDesc() |
| 504 | : AnchoredDesc(DI_TAG_compile_unit) |
| 505 | , DebugVersion(LLVMDebugVersion) |
| 506 | , Language(0) |
| 507 | , FileName("") |
| 508 | , Directory("") |
| 509 | , Producer("") |
| 510 | {} |
| 511 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 512 | /// DebugVersionFromGlobal - Returns the version number from a compile unit |
| 513 | /// GlobalVariable. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 514 | unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 515 | ConstantUInt *C = getUIntOperand(GV, 2); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 516 | return C ? (unsigned)C->getValue() : (unsigned)DIInvalid; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 519 | /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc. |
| 520 | /// |
| 521 | void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 522 | AnchoredDesc::ApplyToFields(Visitor); |
| 523 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 524 | Visitor->Apply(DebugVersion); |
| 525 | Visitor->Apply(Language); |
| 526 | Visitor->Apply(FileName); |
| 527 | Visitor->Apply(Directory); |
| 528 | Visitor->Apply(Producer); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 531 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 532 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 533 | const char *CompileUnitDesc::getDescString() const { |
| 534 | return "llvm.dbg.compile_unit"; |
| 535 | } |
| 536 | |
| 537 | /// getTypeString - Return a string used to label this descriptors type. |
| 538 | /// |
| 539 | const char *CompileUnitDesc::getTypeString() const { |
| 540 | return "llvm.dbg.compile_unit.type"; |
| 541 | } |
| 542 | |
| 543 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 544 | /// |
| 545 | const char *CompileUnitDesc::getAnchorString() const { |
| 546 | return "llvm.dbg.compile_units"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 549 | #ifndef NDEBUG |
| 550 | void CompileUnitDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 551 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 552 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 553 | << "Anchor(" << getAnchor() << "), " |
| 554 | << "DebugVersion(" << DebugVersion << "), " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 555 | << "Language(" << Language << "), " |
| 556 | << "FileName(\"" << FileName << "\"), " |
| 557 | << "Directory(\"" << Directory << "\"), " |
| 558 | << "Producer(\"" << Producer << "\")\n"; |
| 559 | } |
| 560 | #endif |
| 561 | |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 564 | //===----------------------------------------------------------------------===// |
| 565 | |
| 566 | TypeDesc::TypeDesc(unsigned T) |
| 567 | : DebugInfoDesc(T) |
| 568 | , Context(NULL) |
| 569 | , Name("") |
| 570 | , Size(0) |
| 571 | {} |
| 572 | |
| 573 | /// ApplyToFields - Target the visitor to the fields of the TypeDesc. |
| 574 | /// |
| 575 | void TypeDesc::ApplyToFields(DIVisitor *Visitor) { |
| 576 | DebugInfoDesc::ApplyToFields(Visitor); |
| 577 | |
| 578 | Visitor->Apply(Context); |
| 579 | Visitor->Apply(Name); |
| 580 | Visitor->Apply(Size); |
| 581 | } |
| 582 | |
| 583 | /// getDescString - Return a string used to compose global names and labels. |
| 584 | /// |
| 585 | const char *TypeDesc::getDescString() const { |
| 586 | return "llvm.dbg.type"; |
| 587 | } |
| 588 | |
| 589 | /// getTypeString - Return a string used to label this descriptor's type. |
| 590 | /// |
| 591 | const char *TypeDesc::getTypeString() const { |
| 592 | return "llvm.dbg.type.type"; |
| 593 | } |
| 594 | |
| 595 | #ifndef NDEBUG |
| 596 | void TypeDesc::dump() { |
| 597 | std::cerr << getDescString() << " " |
| 598 | << "Tag(" << getTag() << "), " |
| 599 | << "Context(" << Context << "), " |
| 600 | << "Name(\"" << Name << "\"), " |
| 601 | << "Size(" << Size << ")\n"; |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | |
| 607 | BasicTypeDesc::BasicTypeDesc() |
| 608 | : TypeDesc(DI_TAG_basictype) |
| 609 | , Encoding(0) |
| 610 | {} |
| 611 | |
| 612 | /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc. |
| 613 | /// |
| 614 | void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) { |
| 615 | TypeDesc::ApplyToFields(Visitor); |
| 616 | |
| 617 | Visitor->Apply(Encoding); |
| 618 | } |
| 619 | |
| 620 | #ifndef NDEBUG |
| 621 | void BasicTypeDesc::dump() { |
| 622 | std::cerr << getDescString() << " " |
| 623 | << "Tag(" << getTag() << "), " |
| 624 | << "Context(" << getContext() << "), " |
| 625 | << "Name(\"" << getName() << "\"), " |
| 626 | << "Size(" << getSize() << "), " |
| 627 | << "Encoding(" << Encoding << ")\n"; |
| 628 | } |
| 629 | #endif |
| 630 | |
| 631 | //===----------------------------------------------------------------------===// |
| 632 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 633 | GlobalDesc::GlobalDesc(unsigned T) |
| 634 | : AnchoredDesc(T) |
| 635 | , Context(0) |
| 636 | , Name("") |
| 637 | , TyDesc(NULL) |
| 638 | , IsStatic(false) |
| 639 | , IsDefinition(false) |
| 640 | {} |
| 641 | |
| 642 | /// ApplyToFields - Target the visitor to the fields of the global. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 643 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 644 | void GlobalDesc::ApplyToFields(DIVisitor *Visitor) { |
| 645 | AnchoredDesc::ApplyToFields(Visitor); |
| 646 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 647 | Visitor->Apply(Context); |
| 648 | Visitor->Apply(Name); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 649 | Visitor->Apply((DebugInfoDesc *&)TyDesc); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 650 | Visitor->Apply(IsStatic); |
| 651 | Visitor->Apply(IsDefinition); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | //===----------------------------------------------------------------------===// |
| 655 | |
| 656 | GlobalVariableDesc::GlobalVariableDesc() |
| 657 | : GlobalDesc(DI_TAG_global_variable) |
| 658 | , Global(NULL) |
| 659 | {} |
| 660 | |
| 661 | /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc. |
| 662 | /// |
| 663 | void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) { |
| 664 | GlobalDesc::ApplyToFields(Visitor); |
| 665 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 666 | Visitor->Apply(Global); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 667 | Visitor->Apply(Line); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 670 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 671 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 672 | const char *GlobalVariableDesc::getDescString() const { |
| 673 | return "llvm.dbg.global_variable"; |
| 674 | } |
| 675 | |
| 676 | /// getTypeString - Return a string used to label this descriptors type. |
| 677 | /// |
| 678 | const char *GlobalVariableDesc::getTypeString() const { |
| 679 | return "llvm.dbg.global_variable.type"; |
| 680 | } |
| 681 | |
| 682 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 683 | /// |
| 684 | const char *GlobalVariableDesc::getAnchorString() const { |
| 685 | return "llvm.dbg.global_variables"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 688 | #ifndef NDEBUG |
| 689 | void GlobalVariableDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 690 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 691 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 692 | << "Anchor(" << getAnchor() << "), " |
| 693 | << "Name(\"" << getName() << "\"), " |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 694 | << "Type(\"" << getTypeDesc() << "\"), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 695 | << "IsStatic(" << (isStatic() ? "true" : "false") << "), " |
| 696 | << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), " |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 697 | << "Global(" << Global << "), " |
| 698 | << "Line(" << Line << ")\n"; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 699 | } |
| 700 | #endif |
| 701 | |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 704 | SubprogramDesc::SubprogramDesc() |
| 705 | : GlobalDesc(DI_TAG_subprogram) |
| 706 | {} |
| 707 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 708 | /// ApplyToFields - Target the visitor to the fields of the |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 709 | /// SubprogramDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 710 | void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 711 | GlobalDesc::ApplyToFields(Visitor); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 714 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 715 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 716 | const char *SubprogramDesc::getDescString() const { |
| 717 | return "llvm.dbg.subprogram"; |
| 718 | } |
| 719 | |
| 720 | /// getTypeString - Return a string used to label this descriptors type. |
| 721 | /// |
| 722 | const char *SubprogramDesc::getTypeString() const { |
| 723 | return "llvm.dbg.subprogram.type"; |
| 724 | } |
| 725 | |
| 726 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 727 | /// |
| 728 | const char *SubprogramDesc::getAnchorString() const { |
| 729 | return "llvm.dbg.subprograms"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 732 | #ifndef NDEBUG |
| 733 | void SubprogramDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 734 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 735 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 736 | << "Anchor(" << getAnchor() << "), " |
| 737 | << "Name(\"" << getName() << "\"), " |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 738 | << "Type(\"" << getTypeDesc() << "\"), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 739 | << "IsStatic(" << (isStatic() ? "true" : "false") << "), " |
| 740 | << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n"; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 741 | } |
| 742 | #endif |
| 743 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 744 | DebugInfoDesc *DIDeserializer::Deserialize(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 745 | return Deserialize(getGlobalVariable(V)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 746 | } |
| 747 | DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 748 | // Handle NULL. |
| 749 | if (!GV) return NULL; |
| 750 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 751 | // Check to see if it has been already deserialized. |
| 752 | DebugInfoDesc *&Slot = GlobalDescs[GV]; |
| 753 | if (Slot) return Slot; |
| 754 | |
| 755 | // Get the Tag from the global. |
| 756 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
| 757 | |
| 758 | // Get the debug version if a compile unit. |
| 759 | if (Tag == DI_TAG_compile_unit) { |
| 760 | DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV); |
| 761 | } |
| 762 | |
| 763 | // Create an empty instance of the correct sort. |
| 764 | Slot = DebugInfoDesc::DescFactory(Tag); |
| 765 | assert(Slot && "Unknown Tag"); |
| 766 | |
| 767 | // Deserialize the fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 768 | DIDeserializeVisitor DRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 769 | DRAM.ApplyToFields(Slot); |
| 770 | |
| 771 | return Slot; |
| 772 | } |
| 773 | |
| 774 | //===----------------------------------------------------------------------===// |
| 775 | |
| 776 | /// getStrPtrType - Return a "sbyte *" type. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 777 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 778 | const PointerType *DISerializer::getStrPtrType() { |
| 779 | // If not already defined. |
| 780 | if (!StrPtrTy) { |
| 781 | // Construct the pointer to signed bytes. |
| 782 | StrPtrTy = PointerType::get(Type::SByteTy); |
| 783 | } |
| 784 | |
| 785 | return StrPtrTy; |
| 786 | } |
| 787 | |
| 788 | /// getEmptyStructPtrType - Return a "{ }*" type. |
| 789 | /// |
| 790 | const PointerType *DISerializer::getEmptyStructPtrType() { |
| 791 | // If not already defined. |
| 792 | if (!EmptyStructPtrTy) { |
| 793 | // Construct the empty structure type. |
| 794 | const StructType *EmptyStructTy = |
| 795 | StructType::get(std::vector<const Type*>()); |
| 796 | // Construct the pointer to empty structure type. |
| 797 | EmptyStructPtrTy = PointerType::get(EmptyStructTy); |
| 798 | } |
| 799 | |
| 800 | return EmptyStructPtrTy; |
| 801 | } |
| 802 | |
| 803 | /// getTagType - Return the type describing the specified descriptor (via tag.) |
| 804 | /// |
| 805 | const StructType *DISerializer::getTagType(DebugInfoDesc *DD) { |
| 806 | // Attempt to get the previously defined type. |
| 807 | StructType *&Ty = TagTypes[DD->getTag()]; |
| 808 | |
| 809 | // If not already defined. |
| 810 | if (!Ty) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 811 | // Set up fields vector. |
| 812 | std::vector<const Type*> Fields; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 813 | // Get types of fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 814 | DIGetTypesVisitor GTAM(*this, Fields); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 815 | GTAM.ApplyToFields(DD); |
| 816 | |
| 817 | // Construct structured type. |
| 818 | Ty = StructType::get(Fields); |
| 819 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 820 | // Register type name with module. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 821 | M->addTypeName(DD->getTypeString(), Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | return Ty; |
| 825 | } |
| 826 | |
| 827 | /// getString - Construct the string as constant string global. |
| 828 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 829 | Constant *DISerializer::getString(const std::string &String) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 830 | // Check string cache for previous edition. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 831 | Constant *&Slot = StringCache[String]; |
| 832 | // return Constant if previously defined. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 833 | if (Slot) return Slot; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 834 | // Construct string as an llvm constant. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 835 | Constant *ConstStr = ConstantArray::get(String); |
| 836 | // Otherwise create and return a new string global. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 837 | GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, |
| 838 | GlobalVariable::InternalLinkage, |
| 839 | ConstStr, "str", M); |
| 840 | // Convert to generic string pointer. |
| 841 | Slot = ConstantExpr::getCast(StrGV, getStrPtrType()); |
| 842 | return Slot; |
| 843 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /// Serialize - Recursively cast the specified descriptor into a GlobalVariable |
| 847 | /// so that it can be serialized to a .bc or .ll file. |
| 848 | GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) { |
| 849 | // Check if the DebugInfoDesc is already in the map. |
| 850 | GlobalVariable *&Slot = DescGlobals[DD]; |
| 851 | |
| 852 | // See if DebugInfoDesc exists, if so return prior GlobalVariable. |
| 853 | if (Slot) return Slot; |
| 854 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 855 | // Get the type associated with the Tag. |
| 856 | const StructType *Ty = getTagType(DD); |
| 857 | |
| 858 | // Create the GlobalVariable early to prevent infinite recursion. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 859 | GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(), |
| 860 | NULL, DD->getDescString(), M); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 861 | |
| 862 | // Insert new GlobalVariable in DescGlobals map. |
| 863 | Slot = GV; |
| 864 | |
| 865 | // Set up elements vector |
| 866 | std::vector<Constant*> Elements; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 867 | // Add fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 868 | DISerializeVisitor SRAM(*this, Elements); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 869 | SRAM.ApplyToFields(DD); |
| 870 | |
| 871 | // Set the globals initializer. |
| 872 | GV->setInitializer(ConstantStruct::get(Ty, Elements)); |
| 873 | |
| 874 | return GV; |
| 875 | } |
| 876 | |
| 877 | //===----------------------------------------------------------------------===// |
| 878 | |
| 879 | /// markVisited - Return true if the GlobalVariable hase been "seen" before. |
| 880 | /// Mark visited otherwise. |
| 881 | bool DIVerifier::markVisited(GlobalVariable *GV) { |
| 882 | // Check if the GlobalVariable is already in the Visited set. |
| 883 | std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV); |
| 884 | |
| 885 | // See if GlobalVariable exists. |
| 886 | bool Exists = VI != Visited.end() && *VI == GV; |
| 887 | |
| 888 | // Insert in set. |
| 889 | if (!Exists) Visited.insert(VI, GV); |
| 890 | |
| 891 | return Exists; |
| 892 | } |
| 893 | |
| 894 | /// Verify - Return true if the GlobalVariable appears to be a valid |
| 895 | /// serialization of a DebugInfoDesc. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 896 | bool DIVerifier::Verify(Value *V) { |
| 897 | return Verify(getGlobalVariable(V)); |
| 898 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 899 | bool DIVerifier::Verify(GlobalVariable *GV) { |
| 900 | // Check if seen before. |
| 901 | if (markVisited(GV)) return true; |
| 902 | |
| 903 | // Get the Tag |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 904 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 905 | if (Tag == DIInvalid) return false; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 906 | |
| 907 | // If a compile unit we need the debug version. |
| 908 | if (Tag == DI_TAG_compile_unit) { |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 909 | DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 910 | if (DebugVersion == DIInvalid) return false; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | // Construct an empty DebugInfoDesc. |
| 914 | DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag); |
| 915 | if (!DD) return false; |
| 916 | |
| 917 | // Get the initializer constant. |
| 918 | ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer()); |
| 919 | |
| 920 | // Get the operand count. |
| 921 | unsigned N = CI->getNumOperands(); |
| 922 | |
| 923 | // Get the field count. |
| 924 | unsigned &Slot = Counts[Tag]; |
| 925 | if (!Slot) { |
| 926 | // Check the operand count to the field count |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 927 | DICountVisitor CTAM; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 928 | CTAM.ApplyToFields(DD); |
| 929 | Slot = CTAM.getCount(); |
| 930 | } |
| 931 | |
| 932 | // Field count must equal operand count. |
| 933 | if (Slot != N) { |
| 934 | delete DD; |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | // Check each field for valid type. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 939 | DIVerifyVisitor VRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 940 | VRAM.ApplyToFields(DD); |
| 941 | |
| 942 | // Release empty DebugInfoDesc. |
| 943 | delete DD; |
| 944 | |
| 945 | // Return result of field tests. |
| 946 | return VRAM.isValid(); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | //===----------------------------------------------------------------------===// |
| 950 | |
| 951 | |
| 952 | MachineDebugInfo::MachineDebugInfo() |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 953 | : DR() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 954 | , CompileUnits() |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 955 | , Directories() |
| 956 | , SourceFiles() |
| 957 | , Lines() |
| 958 | { |
| 959 | |
| 960 | } |
| 961 | MachineDebugInfo::~MachineDebugInfo() { |
| 962 | |
| 963 | } |
| 964 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 965 | /// doInitialization - Initialize the debug state for a new module. |
| 966 | /// |
| 967 | bool MachineDebugInfo::doInitialization() { |
| 968 | return false; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 971 | /// doFinalization - Tear down the debug state after completion of a module. |
| 972 | /// |
| 973 | bool MachineDebugInfo::doFinalization() { |
| 974 | return false; |
| 975 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 976 | |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 977 | /// getDescFor - Convert a Value to a debug information descriptor. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 978 | /// |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 979 | // FIXME - use new Value type when available. |
| 980 | DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 981 | return DR.Deserialize(V); |
| 982 | } |
| 983 | |
| 984 | /// Verify - Verify that a Value is debug information descriptor. |
| 985 | /// |
| 986 | bool MachineDebugInfo::Verify(Value *V) { |
| 987 | DIVerifier VR; |
| 988 | return VR.Verify(V); |
| 989 | } |
| 990 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 991 | /// AnalyzeModule - Scan the module for global debug information. |
| 992 | /// |
| 993 | void MachineDebugInfo::AnalyzeModule(Module &M) { |
| 994 | SetupCompileUnits(M); |
| 995 | } |
| 996 | |
| 997 | /// SetupCompileUnits - Set up the unique vector of compile units. |
| 998 | /// |
| 999 | void MachineDebugInfo::SetupCompileUnits(Module &M) { |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1000 | std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1001 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1002 | for (unsigned i = 0, N = CU.size(); i < N; i++) { |
| 1003 | CompileUnits.insert(CU[i]); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1004 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 1007 | /// getCompileUnits - Return a vector of debug compile units. |
| 1008 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1009 | const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{ |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 1010 | return CompileUnits; |
| 1011 | } |
| 1012 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1013 | /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the |
| 1014 | /// named GlobalVariable. |
| 1015 | std::vector<GlobalVariable*> |
| 1016 | MachineDebugInfo::getGlobalVariablesUsing(Module &M, |
| 1017 | const std::string &RootName) { |
| 1018 | return ::getGlobalVariablesUsing(M, RootName); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1019 | } |