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; } |
| 200 | virtual void Apply(bool &Field) { ++Count; } |
| 201 | virtual void Apply(std::string &Field) { ++Count; } |
| 202 | virtual void Apply(DebugInfoDesc *&Field) { ++Count; } |
| 203 | virtual void Apply(GlobalVariable *&Field) { ++Count; } |
| 204 | }; |
| 205 | |
| 206 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 207 | /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the |
| 208 | /// supplied DebugInfoDesc. |
| 209 | class DIDeserializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 210 | private: |
| 211 | DIDeserializer &DR; // Active deserializer. |
| 212 | unsigned I; // Current operand index. |
| 213 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 214 | |
| 215 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 216 | DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV) |
| 217 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 218 | , DR(D) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 219 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 220 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 221 | {} |
| 222 | |
| 223 | /// Apply - Set the value of each of the fields. |
| 224 | /// |
| 225 | virtual void Apply(int &Field) { |
| 226 | Constant *C = CI->getOperand(I++); |
| 227 | Field = cast<ConstantSInt>(C)->getValue(); |
| 228 | } |
| 229 | virtual void Apply(unsigned &Field) { |
| 230 | Constant *C = CI->getOperand(I++); |
| 231 | Field = cast<ConstantUInt>(C)->getValue(); |
| 232 | } |
| 233 | virtual void Apply(bool &Field) { |
| 234 | Constant *C = CI->getOperand(I++); |
| 235 | Field = cast<ConstantBool>(C)->getValue(); |
| 236 | } |
| 237 | virtual void Apply(std::string &Field) { |
| 238 | Constant *C = CI->getOperand(I++); |
| 239 | Field = getStringValue(C); |
| 240 | } |
| 241 | virtual void Apply(DebugInfoDesc *&Field) { |
| 242 | Constant *C = CI->getOperand(I++); |
| 243 | Field = DR.Deserialize(C); |
| 244 | } |
| 245 | virtual void Apply(GlobalVariable *&Field) { |
| 246 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 247 | Field = getGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 248 | } |
| 249 | }; |
| 250 | |
| 251 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 252 | /// DISerializeVisitor - This DIVisitor serializes all the fields in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 253 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 254 | class DISerializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 255 | private: |
| 256 | DISerializer &SR; // Active serializer. |
| 257 | std::vector<Constant*> &Elements; // Element accumulator. |
| 258 | |
| 259 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 260 | DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E) |
| 261 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 262 | , SR(S) |
| 263 | , Elements(E) |
| 264 | {} |
| 265 | |
| 266 | /// Apply - Set the value of each of the fields. |
| 267 | /// |
| 268 | virtual void Apply(int &Field) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 269 | Elements.push_back(ConstantSInt::get(Type::IntTy, Field)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 270 | } |
| 271 | virtual void Apply(unsigned &Field) { |
| 272 | Elements.push_back(ConstantUInt::get(Type::UIntTy, Field)); |
| 273 | } |
| 274 | virtual void Apply(bool &Field) { |
| 275 | Elements.push_back(ConstantBool::get(Field)); |
| 276 | } |
| 277 | virtual void Apply(std::string &Field) { |
| 278 | Elements.push_back(SR.getString(Field)); |
| 279 | } |
| 280 | virtual void Apply(DebugInfoDesc *&Field) { |
| 281 | GlobalVariable *GV = NULL; |
| 282 | |
| 283 | // If non-NULL the convert to global. |
| 284 | if (Field) GV = SR.Serialize(Field); |
| 285 | |
| 286 | // FIXME - At some point should use specific type. |
| 287 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 288 | |
| 289 | if (GV) { |
| 290 | // Set to pointer to global. |
| 291 | Elements.push_back(ConstantExpr::getCast(GV, EmptyTy)); |
| 292 | } else { |
| 293 | // Use NULL. |
| 294 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 295 | } |
| 296 | } |
| 297 | virtual void Apply(GlobalVariable *&Field) { |
| 298 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 299 | if (Field) { |
| 300 | Elements.push_back(ConstantExpr::getCast(Field, EmptyTy)); |
| 301 | } else { |
| 302 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 303 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 304 | } |
| 305 | }; |
| 306 | |
| 307 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 308 | /// DIGetTypesVisitor - This DIVisitor gathers all the field types in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 309 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 310 | class DIGetTypesVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 311 | private: |
| 312 | DISerializer &SR; // Active serializer. |
| 313 | std::vector<const Type*> &Fields; // Type accumulator. |
| 314 | |
| 315 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 316 | DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F) |
| 317 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 318 | , SR(S) |
| 319 | , Fields(F) |
| 320 | {} |
| 321 | |
| 322 | /// Apply - Set the value of each of the fields. |
| 323 | /// |
| 324 | virtual void Apply(int &Field) { |
| 325 | Fields.push_back(Type::IntTy); |
| 326 | } |
| 327 | virtual void Apply(unsigned &Field) { |
| 328 | Fields.push_back(Type::UIntTy); |
| 329 | } |
| 330 | virtual void Apply(bool &Field) { |
| 331 | Fields.push_back(Type::BoolTy); |
| 332 | } |
| 333 | virtual void Apply(std::string &Field) { |
| 334 | Fields.push_back(SR.getStrPtrType()); |
| 335 | } |
| 336 | virtual void Apply(DebugInfoDesc *&Field) { |
| 337 | // FIXME - At some point should use specific type. |
| 338 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 339 | Fields.push_back(EmptyTy); |
| 340 | } |
| 341 | virtual void Apply(GlobalVariable *&Field) { |
| 342 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 343 | Fields.push_back(EmptyTy); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 348 | /// DIVerifyVisitor - This DIVisitor verifies all the field types against |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 349 | /// a constant initializer. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 350 | class DIVerifyVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 351 | private: |
| 352 | DIVerifier &VR; // Active verifier. |
| 353 | bool IsValid; // Validity status. |
| 354 | unsigned I; // Current operand index. |
| 355 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 356 | |
| 357 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 358 | DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV) |
| 359 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 360 | , VR(V) |
| 361 | , IsValid(true) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 362 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 363 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | // Accessors. |
| 368 | bool isValid() const { return IsValid; } |
| 369 | |
| 370 | /// Apply - Set the value of each of the fields. |
| 371 | /// |
| 372 | virtual void Apply(int &Field) { |
| 373 | Constant *C = CI->getOperand(I++); |
| 374 | IsValid = IsValid && isa<ConstantInt>(C); |
| 375 | } |
| 376 | virtual void Apply(unsigned &Field) { |
| 377 | Constant *C = CI->getOperand(I++); |
| 378 | IsValid = IsValid && isa<ConstantInt>(C); |
| 379 | } |
| 380 | virtual void Apply(bool &Field) { |
| 381 | Constant *C = CI->getOperand(I++); |
| 382 | IsValid = IsValid && isa<ConstantBool>(C); |
| 383 | } |
| 384 | virtual void Apply(std::string &Field) { |
| 385 | Constant *C = CI->getOperand(I++); |
| 386 | IsValid = IsValid && isStringValue(C); |
| 387 | } |
| 388 | virtual void Apply(DebugInfoDesc *&Field) { |
| 389 | // FIXME - Prepare the correct descriptor. |
| 390 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 391 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 392 | } |
| 393 | virtual void Apply(GlobalVariable *&Field) { |
| 394 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 395 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 396 | } |
| 397 | }; |
| 398 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 399 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 400 | //===----------------------------------------------------------------------===// |
| 401 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 402 | /// TagFromGlobal - Returns the Tag number from a debug info descriptor |
| 403 | /// GlobalVariable. |
| 404 | unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) { |
| 405 | ConstantUInt *C = getUIntOperand(GV, 0); |
| 406 | return C ? (unsigned)C->getValue() : (unsigned)DIInvalid; |
| 407 | } |
| 408 | |
| 409 | /// DescFactory - Create an instance of debug info descriptor based on Tag. |
| 410 | /// Return NULL if not a recognized Tag. |
| 411 | DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) { |
| 412 | switch (Tag) { |
| 413 | case DI_TAG_anchor: return new AnchorDesc(); |
| 414 | case DI_TAG_compile_unit: return new CompileUnitDesc(); |
| 415 | case DI_TAG_global_variable: return new GlobalVariableDesc(); |
| 416 | case DI_TAG_subprogram: return new SubprogramDesc(); |
| 417 | default: break; |
| 418 | } |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | /// getLinkage - get linkage appropriate for this type of descriptor. |
| 423 | /// |
| 424 | GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const { |
| 425 | return GlobalValue::InternalLinkage; |
| 426 | } |
| 427 | |
| 428 | /// ApplyToFields - Target the vistor to the fields of the descriptor. |
| 429 | /// |
| 430 | void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) { |
| 431 | Visitor->Apply(Tag); |
| 432 | } |
| 433 | |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | |
| 436 | /// getLinkage - get linkage appropriate for this type of descriptor. |
| 437 | /// |
| 438 | GlobalValue::LinkageTypes AnchorDesc::getLinkage() const { |
| 439 | return GlobalValue::LinkOnceLinkage; |
| 440 | } |
| 441 | |
| 442 | /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc. |
| 443 | /// |
| 444 | void AnchorDesc::ApplyToFields(DIVisitor *Visitor) { |
| 445 | DebugInfoDesc::ApplyToFields(Visitor); |
| 446 | |
| 447 | Visitor->Apply(Name); |
| 448 | } |
| 449 | |
| 450 | /// getDescString - Return a string used to compose global names and labels. |
| 451 | /// |
| 452 | const char *AnchorDesc::getDescString() const { |
| 453 | return Name.c_str(); |
| 454 | } |
| 455 | |
| 456 | /// getTypeString - Return a string used to label this descriptors type. |
| 457 | /// |
| 458 | const char *AnchorDesc::getTypeString() const { |
| 459 | return "llvm.dbg.anchor.type"; |
| 460 | } |
| 461 | |
| 462 | #ifndef NDEBUG |
| 463 | void AnchorDesc::dump() { |
| 464 | std::cerr << getDescString() << " " |
| 465 | << "Tag(" << getTag() << "), " |
| 466 | << "Name(" << Name << ")\n"; |
| 467 | } |
| 468 | #endif |
| 469 | |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | |
| 472 | AnchoredDesc::AnchoredDesc(unsigned T) |
| 473 | : DebugInfoDesc(T) |
| 474 | , Anchor(NULL) |
| 475 | {} |
| 476 | |
| 477 | /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc. |
| 478 | /// |
| 479 | void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) { |
| 480 | DebugInfoDesc::ApplyToFields(Visitor); |
| 481 | |
| 482 | Visitor->Apply((DebugInfoDesc *&)Anchor); |
| 483 | } |
| 484 | |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | |
| 487 | CompileUnitDesc::CompileUnitDesc() |
| 488 | : AnchoredDesc(DI_TAG_compile_unit) |
| 489 | , DebugVersion(LLVMDebugVersion) |
| 490 | , Language(0) |
| 491 | , FileName("") |
| 492 | , Directory("") |
| 493 | , Producer("") |
| 494 | {} |
| 495 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 496 | /// DebugVersionFromGlobal - Returns the version number from a compile unit |
| 497 | /// GlobalVariable. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 498 | unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 499 | ConstantUInt *C = getUIntOperand(GV, 2); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 500 | return C ? (unsigned)C->getValue() : (unsigned)DIInvalid; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 503 | /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc. |
| 504 | /// |
| 505 | void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 506 | AnchoredDesc::ApplyToFields(Visitor); |
| 507 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 508 | Visitor->Apply(DebugVersion); |
| 509 | Visitor->Apply(Language); |
| 510 | Visitor->Apply(FileName); |
| 511 | Visitor->Apply(Directory); |
| 512 | Visitor->Apply(Producer); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 515 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 516 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 517 | const char *CompileUnitDesc::getDescString() const { |
| 518 | return "llvm.dbg.compile_unit"; |
| 519 | } |
| 520 | |
| 521 | /// getTypeString - Return a string used to label this descriptors type. |
| 522 | /// |
| 523 | const char *CompileUnitDesc::getTypeString() const { |
| 524 | return "llvm.dbg.compile_unit.type"; |
| 525 | } |
| 526 | |
| 527 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 528 | /// |
| 529 | const char *CompileUnitDesc::getAnchorString() const { |
| 530 | return "llvm.dbg.compile_units"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 533 | #ifndef NDEBUG |
| 534 | void CompileUnitDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 535 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 536 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 537 | << "Anchor(" << getAnchor() << "), " |
| 538 | << "DebugVersion(" << DebugVersion << "), " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 539 | << "Language(" << Language << "), " |
| 540 | << "FileName(\"" << FileName << "\"), " |
| 541 | << "Directory(\"" << Directory << "\"), " |
| 542 | << "Producer(\"" << Producer << "\")\n"; |
| 543 | } |
| 544 | #endif |
| 545 | |
| 546 | //===----------------------------------------------------------------------===// |
| 547 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 548 | GlobalDesc::GlobalDesc(unsigned T) |
| 549 | : AnchoredDesc(T) |
| 550 | , Context(0) |
| 551 | , Name("") |
| 552 | , TyDesc(NULL) |
| 553 | , IsStatic(false) |
| 554 | , IsDefinition(false) |
| 555 | {} |
| 556 | |
| 557 | /// ApplyToFields - Target the visitor to the fields of the global. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 558 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 559 | void GlobalDesc::ApplyToFields(DIVisitor *Visitor) { |
| 560 | AnchoredDesc::ApplyToFields(Visitor); |
| 561 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 562 | Visitor->Apply(Context); |
| 563 | Visitor->Apply(Name); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 564 | Visitor->Apply(TyDesc); |
| 565 | Visitor->Apply(IsStatic); |
| 566 | Visitor->Apply(IsDefinition); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | //===----------------------------------------------------------------------===// |
| 570 | |
| 571 | GlobalVariableDesc::GlobalVariableDesc() |
| 572 | : GlobalDesc(DI_TAG_global_variable) |
| 573 | , Global(NULL) |
| 574 | {} |
| 575 | |
| 576 | /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc. |
| 577 | /// |
| 578 | void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) { |
| 579 | GlobalDesc::ApplyToFields(Visitor); |
| 580 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 581 | Visitor->Apply(Global); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 584 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 585 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 586 | const char *GlobalVariableDesc::getDescString() const { |
| 587 | return "llvm.dbg.global_variable"; |
| 588 | } |
| 589 | |
| 590 | /// getTypeString - Return a string used to label this descriptors type. |
| 591 | /// |
| 592 | const char *GlobalVariableDesc::getTypeString() const { |
| 593 | return "llvm.dbg.global_variable.type"; |
| 594 | } |
| 595 | |
| 596 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 597 | /// |
| 598 | const char *GlobalVariableDesc::getAnchorString() const { |
| 599 | return "llvm.dbg.global_variables"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 602 | #ifndef NDEBUG |
| 603 | void GlobalVariableDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 604 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 605 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 606 | << "Anchor(" << getAnchor() << "), " |
| 607 | << "Name(\"" << getName() << "\"), " |
| 608 | << "IsStatic(" << (isStatic() ? "true" : "false") << "), " |
| 609 | << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 610 | << "Global(" << Global << ")\n"; |
| 611 | } |
| 612 | #endif |
| 613 | |
| 614 | //===----------------------------------------------------------------------===// |
| 615 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 616 | SubprogramDesc::SubprogramDesc() |
| 617 | : GlobalDesc(DI_TAG_subprogram) |
| 618 | {} |
| 619 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 620 | /// ApplyToFields - Target the visitor to the fields of the |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 621 | /// SubprogramDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 622 | void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 623 | GlobalDesc::ApplyToFields(Visitor); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 626 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 627 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 628 | const char *SubprogramDesc::getDescString() const { |
| 629 | return "llvm.dbg.subprogram"; |
| 630 | } |
| 631 | |
| 632 | /// getTypeString - Return a string used to label this descriptors type. |
| 633 | /// |
| 634 | const char *SubprogramDesc::getTypeString() const { |
| 635 | return "llvm.dbg.subprogram.type"; |
| 636 | } |
| 637 | |
| 638 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 639 | /// |
| 640 | const char *SubprogramDesc::getAnchorString() const { |
| 641 | return "llvm.dbg.subprograms"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 644 | #ifndef NDEBUG |
| 645 | void SubprogramDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 646 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 647 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 648 | << "Anchor(" << getAnchor() << "), " |
| 649 | << "Name(\"" << getName() << "\"), " |
| 650 | << "IsStatic(" << (isStatic() ? "true" : "false") << "), " |
| 651 | << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n"; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 652 | } |
| 653 | #endif |
| 654 | |
| 655 | //===----------------------------------------------------------------------===// |
| 656 | |
| 657 | DebugInfoDesc *DIDeserializer::Deserialize(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 658 | return Deserialize(getGlobalVariable(V)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 659 | } |
| 660 | DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 661 | // Handle NULL. |
| 662 | if (!GV) return NULL; |
| 663 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 664 | // Check to see if it has been already deserialized. |
| 665 | DebugInfoDesc *&Slot = GlobalDescs[GV]; |
| 666 | if (Slot) return Slot; |
| 667 | |
| 668 | // Get the Tag from the global. |
| 669 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
| 670 | |
| 671 | // Get the debug version if a compile unit. |
| 672 | if (Tag == DI_TAG_compile_unit) { |
| 673 | DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV); |
| 674 | } |
| 675 | |
| 676 | // Create an empty instance of the correct sort. |
| 677 | Slot = DebugInfoDesc::DescFactory(Tag); |
| 678 | assert(Slot && "Unknown Tag"); |
| 679 | |
| 680 | // Deserialize the fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 681 | DIDeserializeVisitor DRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 682 | DRAM.ApplyToFields(Slot); |
| 683 | |
| 684 | return Slot; |
| 685 | } |
| 686 | |
| 687 | //===----------------------------------------------------------------------===// |
| 688 | |
| 689 | /// getStrPtrType - Return a "sbyte *" type. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 690 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 691 | const PointerType *DISerializer::getStrPtrType() { |
| 692 | // If not already defined. |
| 693 | if (!StrPtrTy) { |
| 694 | // Construct the pointer to signed bytes. |
| 695 | StrPtrTy = PointerType::get(Type::SByteTy); |
| 696 | } |
| 697 | |
| 698 | return StrPtrTy; |
| 699 | } |
| 700 | |
| 701 | /// getEmptyStructPtrType - Return a "{ }*" type. |
| 702 | /// |
| 703 | const PointerType *DISerializer::getEmptyStructPtrType() { |
| 704 | // If not already defined. |
| 705 | if (!EmptyStructPtrTy) { |
| 706 | // Construct the empty structure type. |
| 707 | const StructType *EmptyStructTy = |
| 708 | StructType::get(std::vector<const Type*>()); |
| 709 | // Construct the pointer to empty structure type. |
| 710 | EmptyStructPtrTy = PointerType::get(EmptyStructTy); |
| 711 | } |
| 712 | |
| 713 | return EmptyStructPtrTy; |
| 714 | } |
| 715 | |
| 716 | /// getTagType - Return the type describing the specified descriptor (via tag.) |
| 717 | /// |
| 718 | const StructType *DISerializer::getTagType(DebugInfoDesc *DD) { |
| 719 | // Attempt to get the previously defined type. |
| 720 | StructType *&Ty = TagTypes[DD->getTag()]; |
| 721 | |
| 722 | // If not already defined. |
| 723 | if (!Ty) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 724 | // Set up fields vector. |
| 725 | std::vector<const Type*> Fields; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 726 | // Get types of fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 727 | DIGetTypesVisitor GTAM(*this, Fields); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 728 | GTAM.ApplyToFields(DD); |
| 729 | |
| 730 | // Construct structured type. |
| 731 | Ty = StructType::get(Fields); |
| 732 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 733 | // Register type name with module. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 734 | M->addTypeName(DD->getTypeString(), Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | return Ty; |
| 738 | } |
| 739 | |
| 740 | /// getString - Construct the string as constant string global. |
| 741 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 742 | Constant *DISerializer::getString(const std::string &String) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 743 | // Check string cache for previous edition. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 744 | Constant *&Slot = StringCache[String]; |
| 745 | // return Constant if previously defined. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 746 | if (Slot) return Slot; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 747 | // Construct string as an llvm constant. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 748 | Constant *ConstStr = ConstantArray::get(String); |
| 749 | // Otherwise create and return a new string global. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 750 | GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, |
| 751 | GlobalVariable::InternalLinkage, |
| 752 | ConstStr, "str", M); |
| 753 | // Convert to generic string pointer. |
| 754 | Slot = ConstantExpr::getCast(StrGV, getStrPtrType()); |
| 755 | return Slot; |
| 756 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /// Serialize - Recursively cast the specified descriptor into a GlobalVariable |
| 760 | /// so that it can be serialized to a .bc or .ll file. |
| 761 | GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) { |
| 762 | // Check if the DebugInfoDesc is already in the map. |
| 763 | GlobalVariable *&Slot = DescGlobals[DD]; |
| 764 | |
| 765 | // See if DebugInfoDesc exists, if so return prior GlobalVariable. |
| 766 | if (Slot) return Slot; |
| 767 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 768 | // Get the type associated with the Tag. |
| 769 | const StructType *Ty = getTagType(DD); |
| 770 | |
| 771 | // Create the GlobalVariable early to prevent infinite recursion. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 772 | GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(), |
| 773 | NULL, DD->getDescString(), M); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 774 | |
| 775 | // Insert new GlobalVariable in DescGlobals map. |
| 776 | Slot = GV; |
| 777 | |
| 778 | // Set up elements vector |
| 779 | std::vector<Constant*> Elements; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 780 | // Add fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 781 | DISerializeVisitor SRAM(*this, Elements); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 782 | SRAM.ApplyToFields(DD); |
| 783 | |
| 784 | // Set the globals initializer. |
| 785 | GV->setInitializer(ConstantStruct::get(Ty, Elements)); |
| 786 | |
| 787 | return GV; |
| 788 | } |
| 789 | |
| 790 | //===----------------------------------------------------------------------===// |
| 791 | |
| 792 | /// markVisited - Return true if the GlobalVariable hase been "seen" before. |
| 793 | /// Mark visited otherwise. |
| 794 | bool DIVerifier::markVisited(GlobalVariable *GV) { |
| 795 | // Check if the GlobalVariable is already in the Visited set. |
| 796 | std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV); |
| 797 | |
| 798 | // See if GlobalVariable exists. |
| 799 | bool Exists = VI != Visited.end() && *VI == GV; |
| 800 | |
| 801 | // Insert in set. |
| 802 | if (!Exists) Visited.insert(VI, GV); |
| 803 | |
| 804 | return Exists; |
| 805 | } |
| 806 | |
| 807 | /// Verify - Return true if the GlobalVariable appears to be a valid |
| 808 | /// serialization of a DebugInfoDesc. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 809 | bool DIVerifier::Verify(Value *V) { |
| 810 | return Verify(getGlobalVariable(V)); |
| 811 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 812 | bool DIVerifier::Verify(GlobalVariable *GV) { |
| 813 | // Check if seen before. |
| 814 | if (markVisited(GV)) return true; |
| 815 | |
| 816 | // Get the Tag |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 817 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 818 | if (Tag == DIInvalid) return false; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 819 | |
| 820 | // If a compile unit we need the debug version. |
| 821 | if (Tag == DI_TAG_compile_unit) { |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 822 | DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 823 | if (DebugVersion == DIInvalid) return false; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // Construct an empty DebugInfoDesc. |
| 827 | DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag); |
| 828 | if (!DD) return false; |
| 829 | |
| 830 | // Get the initializer constant. |
| 831 | ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer()); |
| 832 | |
| 833 | // Get the operand count. |
| 834 | unsigned N = CI->getNumOperands(); |
| 835 | |
| 836 | // Get the field count. |
| 837 | unsigned &Slot = Counts[Tag]; |
| 838 | if (!Slot) { |
| 839 | // Check the operand count to the field count |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 840 | DICountVisitor CTAM; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 841 | CTAM.ApplyToFields(DD); |
| 842 | Slot = CTAM.getCount(); |
| 843 | } |
| 844 | |
| 845 | // Field count must equal operand count. |
| 846 | if (Slot != N) { |
| 847 | delete DD; |
| 848 | return false; |
| 849 | } |
| 850 | |
| 851 | // Check each field for valid type. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 852 | DIVerifyVisitor VRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 853 | VRAM.ApplyToFields(DD); |
| 854 | |
| 855 | // Release empty DebugInfoDesc. |
| 856 | delete DD; |
| 857 | |
| 858 | // Return result of field tests. |
| 859 | return VRAM.isValid(); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | //===----------------------------------------------------------------------===// |
| 863 | |
| 864 | |
| 865 | MachineDebugInfo::MachineDebugInfo() |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 866 | : DR() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 867 | , CompileUnits() |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 868 | , Directories() |
| 869 | , SourceFiles() |
| 870 | , Lines() |
| 871 | { |
| 872 | |
| 873 | } |
| 874 | MachineDebugInfo::~MachineDebugInfo() { |
| 875 | |
| 876 | } |
| 877 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 878 | /// doInitialization - Initialize the debug state for a new module. |
| 879 | /// |
| 880 | bool MachineDebugInfo::doInitialization() { |
| 881 | return false; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 884 | /// doFinalization - Tear down the debug state after completion of a module. |
| 885 | /// |
| 886 | bool MachineDebugInfo::doFinalization() { |
| 887 | return false; |
| 888 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 889 | |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 890 | /// getDescFor - Convert a Value to a debug information descriptor. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 891 | /// |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 892 | // FIXME - use new Value type when available. |
| 893 | DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 894 | return DR.Deserialize(V); |
| 895 | } |
| 896 | |
| 897 | /// Verify - Verify that a Value is debug information descriptor. |
| 898 | /// |
| 899 | bool MachineDebugInfo::Verify(Value *V) { |
| 900 | DIVerifier VR; |
| 901 | return VR.Verify(V); |
| 902 | } |
| 903 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 904 | /// AnalyzeModule - Scan the module for global debug information. |
| 905 | /// |
| 906 | void MachineDebugInfo::AnalyzeModule(Module &M) { |
| 907 | SetupCompileUnits(M); |
| 908 | } |
| 909 | |
| 910 | /// SetupCompileUnits - Set up the unique vector of compile units. |
| 911 | /// |
| 912 | void MachineDebugInfo::SetupCompileUnits(Module &M) { |
| 913 | // Get vector of all debug compile units. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 914 | CompileUnitDesc CompileUnit; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 915 | std::vector<GlobalVariable*> Globals = |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 916 | getGlobalVariablesUsing(M, CompileUnit.getAnchorString()); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 917 | |
| 918 | // Scan all compile unit globals. |
| 919 | for (unsigned i = 0, N = Globals.size(); i < N; ++i) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 920 | // Add compile unit to result. |
| 921 | CompileUnits.insert( |
| 922 | static_cast<CompileUnitDesc *>(DR.Deserialize(Globals[i]))); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 923 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 926 | /// getCompileUnits - Return a vector of debug compile units. |
| 927 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 928 | const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{ |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 929 | return CompileUnits; |
| 930 | } |
| 931 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 932 | /// getGlobalVariables - Return a vector of debug GlobalVariables. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 933 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 934 | std::vector<GlobalVariableDesc *> |
| 935 | MachineDebugInfo::getGlobalVariables(Module &M) { |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 936 | // Get vector of all debug global objects. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 937 | GlobalVariableDesc Global; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 938 | std::vector<GlobalVariable*> Globals = |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 939 | getGlobalVariablesUsing(M, Global.getAnchorString()); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 940 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 941 | // Accumulation of GlobalVariables. |
| 942 | std::vector<GlobalVariableDesc *> GlobalVariables; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 943 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 944 | // Scan all globals. |
| 945 | for (unsigned i = 0, N = Globals.size(); i < N; ++i) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 946 | GlobalVariable *GV = Globals[i]; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 947 | GlobalVariableDesc *GVD = |
| 948 | static_cast<GlobalVariableDesc *>(DR.Deserialize(GV)); |
| 949 | GlobalVariables.push_back(GVD); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 950 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 951 | |
| 952 | return GlobalVariables; |
| 953 | } |
| 954 | |