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 | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 200 | virtual void Apply(int64_t &Field) { ++Count; } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 201 | virtual void Apply(uint64_t &Field) { ++Count; } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 202 | virtual void Apply(bool &Field) { ++Count; } |
| 203 | virtual void Apply(std::string &Field) { ++Count; } |
| 204 | virtual void Apply(DebugInfoDesc *&Field) { ++Count; } |
| 205 | virtual void Apply(GlobalVariable *&Field) { ++Count; } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 206 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 207 | ++Count; |
| 208 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 212 | /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the |
| 213 | /// supplied DebugInfoDesc. |
| 214 | class DIDeserializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 215 | private: |
| 216 | DIDeserializer &DR; // Active deserializer. |
| 217 | unsigned I; // Current operand index. |
| 218 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 219 | |
| 220 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 221 | DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV) |
| 222 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 223 | , DR(D) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 224 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 225 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 226 | {} |
| 227 | |
| 228 | /// Apply - Set the value of each of the fields. |
| 229 | /// |
| 230 | virtual void Apply(int &Field) { |
| 231 | Constant *C = CI->getOperand(I++); |
| 232 | Field = cast<ConstantSInt>(C)->getValue(); |
| 233 | } |
| 234 | virtual void Apply(unsigned &Field) { |
| 235 | Constant *C = CI->getOperand(I++); |
| 236 | Field = cast<ConstantUInt>(C)->getValue(); |
| 237 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 238 | virtual void Apply(int64_t &Field) { |
| 239 | Constant *C = CI->getOperand(I++); |
| 240 | Field = cast<ConstantSInt>(C)->getValue(); |
| 241 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 242 | virtual void Apply(uint64_t &Field) { |
| 243 | Constant *C = CI->getOperand(I++); |
| 244 | Field = cast<ConstantUInt>(C)->getValue(); |
| 245 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 246 | virtual void Apply(bool &Field) { |
| 247 | Constant *C = CI->getOperand(I++); |
| 248 | Field = cast<ConstantBool>(C)->getValue(); |
| 249 | } |
| 250 | virtual void Apply(std::string &Field) { |
| 251 | Constant *C = CI->getOperand(I++); |
| 252 | Field = getStringValue(C); |
| 253 | } |
| 254 | virtual void Apply(DebugInfoDesc *&Field) { |
| 255 | Constant *C = CI->getOperand(I++); |
| 256 | Field = DR.Deserialize(C); |
| 257 | } |
| 258 | virtual void Apply(GlobalVariable *&Field) { |
| 259 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 260 | Field = getGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 261 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 262 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 263 | Constant *C = CI->getOperand(I++); |
| 264 | GlobalVariable *GV = getGlobalVariable(C); |
| 265 | ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); |
| 266 | Field.resize(0); |
| 267 | for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) { |
| 268 | GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); |
| 269 | DebugInfoDesc *DE = DR.Deserialize(GVE); |
| 270 | Field.push_back(DE); |
| 271 | } |
| 272 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 276 | /// DISerializeVisitor - This DIVisitor serializes all the fields in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 277 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 278 | class DISerializeVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 279 | private: |
| 280 | DISerializer &SR; // Active serializer. |
| 281 | std::vector<Constant*> &Elements; // Element accumulator. |
| 282 | |
| 283 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 284 | DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E) |
| 285 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 286 | , SR(S) |
| 287 | , Elements(E) |
| 288 | {} |
| 289 | |
| 290 | /// Apply - Set the value of each of the fields. |
| 291 | /// |
| 292 | virtual void Apply(int &Field) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 293 | Elements.push_back(ConstantSInt::get(Type::IntTy, Field)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 294 | } |
| 295 | virtual void Apply(unsigned &Field) { |
| 296 | Elements.push_back(ConstantUInt::get(Type::UIntTy, Field)); |
| 297 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 298 | virtual void Apply(int64_t &Field) { |
| 299 | Elements.push_back(ConstantSInt::get(Type::IntTy, Field)); |
| 300 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 301 | virtual void Apply(uint64_t &Field) { |
| 302 | Elements.push_back(ConstantUInt::get(Type::UIntTy, Field)); |
| 303 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 304 | virtual void Apply(bool &Field) { |
| 305 | Elements.push_back(ConstantBool::get(Field)); |
| 306 | } |
| 307 | virtual void Apply(std::string &Field) { |
| 308 | Elements.push_back(SR.getString(Field)); |
| 309 | } |
| 310 | virtual void Apply(DebugInfoDesc *&Field) { |
| 311 | GlobalVariable *GV = NULL; |
| 312 | |
| 313 | // If non-NULL the convert to global. |
| 314 | if (Field) GV = SR.Serialize(Field); |
| 315 | |
| 316 | // FIXME - At some point should use specific type. |
| 317 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 318 | |
| 319 | if (GV) { |
| 320 | // Set to pointer to global. |
| 321 | Elements.push_back(ConstantExpr::getCast(GV, EmptyTy)); |
| 322 | } else { |
| 323 | // Use NULL. |
| 324 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 325 | } |
| 326 | } |
| 327 | virtual void Apply(GlobalVariable *&Field) { |
| 328 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 329 | if (Field) { |
| 330 | Elements.push_back(ConstantExpr::getCast(Field, EmptyTy)); |
| 331 | } else { |
| 332 | Elements.push_back(ConstantPointerNull::get(EmptyTy)); |
| 333 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 334 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 335 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 336 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 337 | unsigned N = Field.size(); |
| 338 | ArrayType *AT = ArrayType::get(EmptyTy, N); |
| 339 | std::vector<Constant *> ArrayElements; |
| 340 | |
| 341 | for (unsigned i = 0, N = Field.size(); i < N; ++i) { |
| 342 | GlobalVariable *GVE = SR.Serialize(Field[i]); |
| 343 | Constant *CE = ConstantExpr::getCast(GVE, EmptyTy); |
| 344 | ArrayElements.push_back(cast<Constant>(CE)); |
| 345 | } |
| 346 | |
| 347 | Constant *CA = ConstantArray::get(AT, ArrayElements); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 348 | GlobalVariable *CAGV = new GlobalVariable(AT, true, |
| 349 | GlobalValue::InternalLinkage, |
| 350 | CA, "llvm.dbg.array", |
| 351 | SR.getModule()); |
| 352 | Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy); |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 353 | Elements.push_back(CAE); |
| 354 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 358 | /// DIGetTypesVisitor - This DIVisitor gathers all the field types in |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 359 | /// the supplied DebugInfoDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 360 | class DIGetTypesVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 361 | private: |
| 362 | DISerializer &SR; // Active serializer. |
| 363 | std::vector<const Type*> &Fields; // Type accumulator. |
| 364 | |
| 365 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 366 | DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F) |
| 367 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 368 | , SR(S) |
| 369 | , Fields(F) |
| 370 | {} |
| 371 | |
| 372 | /// Apply - Set the value of each of the fields. |
| 373 | /// |
| 374 | virtual void Apply(int &Field) { |
| 375 | Fields.push_back(Type::IntTy); |
| 376 | } |
| 377 | virtual void Apply(unsigned &Field) { |
| 378 | Fields.push_back(Type::UIntTy); |
| 379 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 380 | virtual void Apply(int64_t &Field) { |
| 381 | Fields.push_back(Type::IntTy); |
| 382 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 383 | virtual void Apply(uint64_t &Field) { |
| 384 | Fields.push_back(Type::UIntTy); |
| 385 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 386 | virtual void Apply(bool &Field) { |
| 387 | Fields.push_back(Type::BoolTy); |
| 388 | } |
| 389 | virtual void Apply(std::string &Field) { |
| 390 | Fields.push_back(SR.getStrPtrType()); |
| 391 | } |
| 392 | virtual void Apply(DebugInfoDesc *&Field) { |
| 393 | // FIXME - At some point should use specific type. |
| 394 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 395 | Fields.push_back(EmptyTy); |
| 396 | } |
| 397 | virtual void Apply(GlobalVariable *&Field) { |
| 398 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 399 | Fields.push_back(EmptyTy); |
| 400 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 401 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 402 | const PointerType *EmptyTy = SR.getEmptyStructPtrType(); |
| 403 | Fields.push_back(EmptyTy); |
| 404 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | //===----------------------------------------------------------------------===// |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 408 | /// DIVerifyVisitor - This DIVisitor verifies all the field types against |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 409 | /// a constant initializer. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 410 | class DIVerifyVisitor : public DIVisitor { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 411 | private: |
| 412 | DIVerifier &VR; // Active verifier. |
| 413 | bool IsValid; // Validity status. |
| 414 | unsigned I; // Current operand index. |
| 415 | ConstantStruct *CI; // GlobalVariable constant initializer. |
| 416 | |
| 417 | public: |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 418 | DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV) |
| 419 | : DIVisitor() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 420 | , VR(V) |
| 421 | , IsValid(true) |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 422 | , I(0) |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 423 | , CI(cast<ConstantStruct>(GV->getInitializer())) |
| 424 | { |
| 425 | } |
| 426 | |
| 427 | // Accessors. |
| 428 | bool isValid() const { return IsValid; } |
| 429 | |
| 430 | /// Apply - Set the value of each of the fields. |
| 431 | /// |
| 432 | virtual void Apply(int &Field) { |
| 433 | Constant *C = CI->getOperand(I++); |
| 434 | IsValid = IsValid && isa<ConstantInt>(C); |
| 435 | } |
| 436 | virtual void Apply(unsigned &Field) { |
| 437 | Constant *C = CI->getOperand(I++); |
| 438 | IsValid = IsValid && isa<ConstantInt>(C); |
| 439 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 440 | virtual void Apply(int64_t &Field) { |
| 441 | Constant *C = CI->getOperand(I++); |
| 442 | IsValid = IsValid && isa<ConstantInt>(C); |
| 443 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 444 | virtual void Apply(uint64_t &Field) { |
| 445 | Constant *C = CI->getOperand(I++); |
| 446 | IsValid = IsValid && isa<ConstantInt>(C); |
| 447 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 448 | virtual void Apply(bool &Field) { |
| 449 | Constant *C = CI->getOperand(I++); |
| 450 | IsValid = IsValid && isa<ConstantBool>(C); |
| 451 | } |
| 452 | virtual void Apply(std::string &Field) { |
| 453 | Constant *C = CI->getOperand(I++); |
| 454 | IsValid = IsValid && isStringValue(C); |
| 455 | } |
| 456 | virtual void Apply(DebugInfoDesc *&Field) { |
| 457 | // FIXME - Prepare the correct descriptor. |
| 458 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 459 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 460 | } |
| 461 | virtual void Apply(GlobalVariable *&Field) { |
| 462 | Constant *C = CI->getOperand(I++); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 463 | IsValid = IsValid && isGlobalVariable(C); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 464 | } |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 465 | virtual void Apply(std::vector<DebugInfoDesc *> &Field) { |
| 466 | Constant *C = CI->getOperand(I++); |
| 467 | IsValid = IsValid && isGlobalVariable(C); |
| 468 | if (!IsValid) return; |
| 469 | |
| 470 | GlobalVariable *GV = getGlobalVariable(C); |
| 471 | IsValid = IsValid && GV && GV->hasInitializer(); |
| 472 | if (!IsValid) return; |
| 473 | |
| 474 | ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 475 | IsValid = IsValid && CA; |
| 476 | if (!IsValid) return; |
| 477 | |
| 478 | for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) { |
| 479 | IsValid = IsValid && isGlobalVariable(CA->getOperand(i)); |
| 480 | if (!IsValid) return; |
| 481 | |
| 482 | GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); |
| 483 | VR.Verify(GVE); |
| 484 | } |
| 485 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 488 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 489 | //===----------------------------------------------------------------------===// |
| 490 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 491 | /// TagFromGlobal - Returns the Tag number from a debug info descriptor |
| 492 | /// GlobalVariable. |
| 493 | unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) { |
| 494 | ConstantUInt *C = getUIntOperand(GV, 0); |
| 495 | return C ? (unsigned)C->getValue() : (unsigned)DIInvalid; |
| 496 | } |
| 497 | |
| 498 | /// DescFactory - Create an instance of debug info descriptor based on Tag. |
| 499 | /// Return NULL if not a recognized Tag. |
| 500 | DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) { |
| 501 | switch (Tag) { |
| 502 | case DI_TAG_anchor: return new AnchorDesc(); |
| 503 | case DI_TAG_compile_unit: return new CompileUnitDesc(); |
| 504 | case DI_TAG_global_variable: return new GlobalVariableDesc(); |
| 505 | case DI_TAG_subprogram: return new SubprogramDesc(); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 506 | case DI_TAG_basictype: return new BasicTypeDesc(); |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 507 | case DI_TAG_typedef: |
| 508 | case DI_TAG_pointer: |
| 509 | case DI_TAG_reference: |
| 510 | case DI_TAG_const: |
| 511 | case DI_TAG_volatile: |
| 512 | case DI_TAG_restrict: return new DerivedTypeDesc(Tag); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 513 | case DI_TAG_array: |
| 514 | case DI_TAG_struct: |
| 515 | case DI_TAG_union: |
| 516 | case DI_TAG_enum: return new CompositeTypeDesc(Tag); |
| 517 | case DI_TAG_subrange: return new SubrangeDesc(); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 518 | default: break; |
| 519 | } |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | /// getLinkage - get linkage appropriate for this type of descriptor. |
| 524 | /// |
| 525 | GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const { |
| 526 | return GlobalValue::InternalLinkage; |
| 527 | } |
| 528 | |
| 529 | /// ApplyToFields - Target the vistor to the fields of the descriptor. |
| 530 | /// |
| 531 | void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) { |
| 532 | Visitor->Apply(Tag); |
| 533 | } |
| 534 | |
| 535 | //===----------------------------------------------------------------------===// |
| 536 | |
| 537 | /// getLinkage - get linkage appropriate for this type of descriptor. |
| 538 | /// |
| 539 | GlobalValue::LinkageTypes AnchorDesc::getLinkage() const { |
| 540 | return GlobalValue::LinkOnceLinkage; |
| 541 | } |
| 542 | |
| 543 | /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc. |
| 544 | /// |
| 545 | void AnchorDesc::ApplyToFields(DIVisitor *Visitor) { |
| 546 | DebugInfoDesc::ApplyToFields(Visitor); |
| 547 | |
| 548 | Visitor->Apply(Name); |
| 549 | } |
| 550 | |
| 551 | /// getDescString - Return a string used to compose global names and labels. |
| 552 | /// |
| 553 | const char *AnchorDesc::getDescString() const { |
| 554 | return Name.c_str(); |
| 555 | } |
| 556 | |
| 557 | /// getTypeString - Return a string used to label this descriptors type. |
| 558 | /// |
| 559 | const char *AnchorDesc::getTypeString() const { |
| 560 | return "llvm.dbg.anchor.type"; |
| 561 | } |
| 562 | |
| 563 | #ifndef NDEBUG |
| 564 | void AnchorDesc::dump() { |
| 565 | std::cerr << getDescString() << " " |
| 566 | << "Tag(" << getTag() << "), " |
| 567 | << "Name(" << Name << ")\n"; |
| 568 | } |
| 569 | #endif |
| 570 | |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
| 573 | AnchoredDesc::AnchoredDesc(unsigned T) |
| 574 | : DebugInfoDesc(T) |
| 575 | , Anchor(NULL) |
| 576 | {} |
| 577 | |
| 578 | /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc. |
| 579 | /// |
| 580 | void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) { |
| 581 | DebugInfoDesc::ApplyToFields(Visitor); |
| 582 | |
| 583 | Visitor->Apply((DebugInfoDesc *&)Anchor); |
| 584 | } |
| 585 | |
| 586 | //===----------------------------------------------------------------------===// |
| 587 | |
| 588 | CompileUnitDesc::CompileUnitDesc() |
| 589 | : AnchoredDesc(DI_TAG_compile_unit) |
| 590 | , DebugVersion(LLVMDebugVersion) |
| 591 | , Language(0) |
| 592 | , FileName("") |
| 593 | , Directory("") |
| 594 | , Producer("") |
| 595 | {} |
| 596 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 597 | /// DebugVersionFromGlobal - Returns the version number from a compile unit |
| 598 | /// GlobalVariable. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 599 | unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 600 | ConstantUInt *C = getUIntOperand(GV, 2); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 601 | return C ? (unsigned)C->getValue() : (unsigned)DIInvalid; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 604 | /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc. |
| 605 | /// |
| 606 | void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 607 | AnchoredDesc::ApplyToFields(Visitor); |
| 608 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 609 | Visitor->Apply(DebugVersion); |
| 610 | Visitor->Apply(Language); |
| 611 | Visitor->Apply(FileName); |
| 612 | Visitor->Apply(Directory); |
| 613 | Visitor->Apply(Producer); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 616 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 617 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 618 | const char *CompileUnitDesc::getDescString() const { |
| 619 | return "llvm.dbg.compile_unit"; |
| 620 | } |
| 621 | |
| 622 | /// getTypeString - Return a string used to label this descriptors type. |
| 623 | /// |
| 624 | const char *CompileUnitDesc::getTypeString() const { |
| 625 | return "llvm.dbg.compile_unit.type"; |
| 626 | } |
| 627 | |
| 628 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 629 | /// |
| 630 | const char *CompileUnitDesc::getAnchorString() const { |
| 631 | return "llvm.dbg.compile_units"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 634 | #ifndef NDEBUG |
| 635 | void CompileUnitDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 636 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 637 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 638 | << "Anchor(" << getAnchor() << "), " |
| 639 | << "DebugVersion(" << DebugVersion << "), " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 640 | << "Language(" << Language << "), " |
| 641 | << "FileName(\"" << FileName << "\"), " |
| 642 | << "Directory(\"" << Directory << "\"), " |
| 643 | << "Producer(\"" << Producer << "\")\n"; |
| 644 | } |
| 645 | #endif |
| 646 | |
| 647 | //===----------------------------------------------------------------------===// |
| 648 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 649 | TypeDesc::TypeDesc(unsigned T) |
| 650 | : DebugInfoDesc(T) |
| 651 | , Context(NULL) |
| 652 | , Name("") |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 653 | , File(NULL) |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 654 | , Size(0) |
| 655 | {} |
| 656 | |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 657 | /// ApplyToFields - Target the visitor to the fields of the TypeDesc. |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 658 | /// |
| 659 | void TypeDesc::ApplyToFields(DIVisitor *Visitor) { |
| 660 | DebugInfoDesc::ApplyToFields(Visitor); |
| 661 | |
| 662 | Visitor->Apply(Context); |
| 663 | Visitor->Apply(Name); |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 664 | Visitor->Apply((DebugInfoDesc *&)File); |
| 665 | Visitor->Apply(Line); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 666 | Visitor->Apply(Size); |
| 667 | } |
| 668 | |
| 669 | /// getDescString - Return a string used to compose global names and labels. |
| 670 | /// |
| 671 | const char *TypeDesc::getDescString() const { |
| 672 | return "llvm.dbg.type"; |
| 673 | } |
| 674 | |
| 675 | /// getTypeString - Return a string used to label this descriptor's type. |
| 676 | /// |
| 677 | const char *TypeDesc::getTypeString() const { |
| 678 | return "llvm.dbg.type.type"; |
| 679 | } |
| 680 | |
| 681 | #ifndef NDEBUG |
| 682 | void TypeDesc::dump() { |
| 683 | std::cerr << getDescString() << " " |
| 684 | << "Tag(" << getTag() << "), " |
| 685 | << "Context(" << Context << "), " |
| 686 | << "Name(\"" << Name << "\"), " |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 687 | << "File(" << File << "), " |
| 688 | << "Line(" << Line << "), " |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 689 | << "Size(" << Size << ")\n"; |
| 690 | } |
| 691 | #endif |
| 692 | |
| 693 | //===----------------------------------------------------------------------===// |
| 694 | |
| 695 | BasicTypeDesc::BasicTypeDesc() |
| 696 | : TypeDesc(DI_TAG_basictype) |
| 697 | , Encoding(0) |
| 698 | {} |
| 699 | |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 700 | /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc. |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 701 | /// |
| 702 | void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) { |
| 703 | TypeDesc::ApplyToFields(Visitor); |
| 704 | |
| 705 | Visitor->Apply(Encoding); |
| 706 | } |
| 707 | |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 708 | /// getDescString - Return a string used to compose global names and labels. |
| 709 | /// |
| 710 | const char *BasicTypeDesc::getDescString() const { |
| 711 | return "llvm.dbg.basictype"; |
| 712 | } |
| 713 | |
| 714 | /// getTypeString - Return a string used to label this descriptor's type. |
| 715 | /// |
| 716 | const char *BasicTypeDesc::getTypeString() const { |
| 717 | return "llvm.dbg.basictype.type"; |
| 718 | } |
| 719 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 720 | #ifndef NDEBUG |
| 721 | void BasicTypeDesc::dump() { |
| 722 | std::cerr << getDescString() << " " |
| 723 | << "Tag(" << getTag() << "), " |
| 724 | << "Context(" << getContext() << "), " |
| 725 | << "Name(\"" << getName() << "\"), " |
| 726 | << "Size(" << getSize() << "), " |
| 727 | << "Encoding(" << Encoding << ")\n"; |
| 728 | } |
| 729 | #endif |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 730 | |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 731 | //===----------------------------------------------------------------------===// |
| 732 | |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 733 | DerivedTypeDesc::DerivedTypeDesc(unsigned T) |
| 734 | : TypeDesc(T) |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 735 | , FromType(NULL) |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 736 | {} |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 737 | |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 738 | /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc. |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 739 | /// |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 740 | void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 741 | TypeDesc::ApplyToFields(Visitor); |
| 742 | |
| 743 | Visitor->Apply((DebugInfoDesc *&)FromType); |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 746 | /// getDescString - Return a string used to compose global names and labels. |
| 747 | /// |
| 748 | const char *DerivedTypeDesc::getDescString() const { |
| 749 | return "llvm.dbg.derivedtype"; |
| 750 | } |
| 751 | |
| 752 | /// getTypeString - Return a string used to label this descriptor's type. |
| 753 | /// |
| 754 | const char *DerivedTypeDesc::getTypeString() const { |
| 755 | return "llvm.dbg.derivedtype.type"; |
| 756 | } |
| 757 | |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 758 | #ifndef NDEBUG |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 759 | void DerivedTypeDesc::dump() { |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 760 | std::cerr << getDescString() << " " |
| 761 | << "Tag(" << getTag() << "), " |
| 762 | << "Context(" << getContext() << "), " |
| 763 | << "Name(\"" << getName() << "\"), " |
| 764 | << "Size(" << getSize() << "), " |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 765 | << "File(" << getFile() << "), " |
| 766 | << "Line(" << getLine() << "), " |
| 767 | << "FromType(" << FromType << ")\n"; |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 768 | } |
| 769 | #endif |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 770 | |
| 771 | //===----------------------------------------------------------------------===// |
| 772 | |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 773 | CompositeTypeDesc::CompositeTypeDesc(unsigned T) |
| 774 | : DerivedTypeDesc(T) |
| 775 | , Elements() |
| 776 | {} |
| 777 | |
| 778 | /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc. |
| 779 | /// |
| 780 | void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) { |
| 781 | DerivedTypeDesc::ApplyToFields(Visitor); |
| 782 | |
| 783 | Visitor->Apply(Elements); |
| 784 | } |
| 785 | |
| 786 | /// getDescString - Return a string used to compose global names and labels. |
| 787 | /// |
| 788 | const char *CompositeTypeDesc::getDescString() const { |
| 789 | return "llvm.dbg.compositetype"; |
| 790 | } |
| 791 | |
| 792 | /// getTypeString - Return a string used to label this descriptor's type. |
| 793 | /// |
| 794 | const char *CompositeTypeDesc::getTypeString() const { |
| 795 | return "llvm.dbg.compositetype.type"; |
| 796 | } |
| 797 | |
| 798 | #ifndef NDEBUG |
| 799 | void CompositeTypeDesc::dump() { |
| 800 | std::cerr << getDescString() << " " |
| 801 | << "Tag(" << getTag() << "), " |
| 802 | << "Context(" << getContext() << "), " |
| 803 | << "Name(\"" << getName() << "\"), " |
| 804 | << "Size(" << getSize() << "), " |
| 805 | << "File(" << getFile() << "), " |
| 806 | << "Line(" << getLine() << "), " |
| 807 | << "FromType(" << getFromType() << "), " |
| 808 | << "Elements.size(" << Elements.size() << ")\n"; |
| 809 | } |
| 810 | #endif |
| 811 | |
| 812 | //===----------------------------------------------------------------------===// |
| 813 | |
| 814 | SubrangeDesc::SubrangeDesc() |
| 815 | : DebugInfoDesc(DI_TAG_subrange) |
| 816 | , Lo(0) |
| 817 | , Hi(0) |
| 818 | {} |
| 819 | |
| 820 | /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc. |
| 821 | /// |
| 822 | void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) { |
| 823 | DebugInfoDesc::ApplyToFields(Visitor); |
| 824 | |
| 825 | Visitor->Apply(Lo); |
| 826 | Visitor->Apply(Hi); |
| 827 | } |
| 828 | |
| 829 | /// getDescString - Return a string used to compose global names and labels. |
| 830 | /// |
| 831 | const char *SubrangeDesc::getDescString() const { |
| 832 | return "llvm.dbg.subrange"; |
| 833 | } |
| 834 | |
| 835 | /// getTypeString - Return a string used to label this descriptor's type. |
| 836 | /// |
| 837 | const char *SubrangeDesc::getTypeString() const { |
| 838 | return "llvm.dbg.subrange.type"; |
| 839 | } |
| 840 | |
| 841 | #ifndef NDEBUG |
| 842 | void SubrangeDesc::dump() { |
| 843 | std::cerr << getDescString() << " " |
| 844 | << "Tag(" << getTag() << "), " |
| 845 | << "Lo(" << Lo << "), " |
| 846 | << "Hi(" << Hi << ")\n"; |
| 847 | } |
| 848 | #endif |
| 849 | |
| 850 | //===----------------------------------------------------------------------===// |
| 851 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 852 | GlobalDesc::GlobalDesc(unsigned T) |
| 853 | : AnchoredDesc(T) |
| 854 | , Context(0) |
| 855 | , Name("") |
| 856 | , TyDesc(NULL) |
| 857 | , IsStatic(false) |
| 858 | , IsDefinition(false) |
| 859 | {} |
| 860 | |
| 861 | /// ApplyToFields - Target the visitor to the fields of the global. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 862 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 863 | void GlobalDesc::ApplyToFields(DIVisitor *Visitor) { |
| 864 | AnchoredDesc::ApplyToFields(Visitor); |
| 865 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 866 | Visitor->Apply(Context); |
| 867 | Visitor->Apply(Name); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 868 | Visitor->Apply((DebugInfoDesc *&)TyDesc); |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 869 | Visitor->Apply(IsStatic); |
| 870 | Visitor->Apply(IsDefinition); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | //===----------------------------------------------------------------------===// |
| 874 | |
| 875 | GlobalVariableDesc::GlobalVariableDesc() |
| 876 | : GlobalDesc(DI_TAG_global_variable) |
| 877 | , Global(NULL) |
| 878 | {} |
| 879 | |
| 880 | /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc. |
| 881 | /// |
| 882 | void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) { |
| 883 | GlobalDesc::ApplyToFields(Visitor); |
| 884 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 885 | Visitor->Apply(Global); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 886 | Visitor->Apply(Line); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 889 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 890 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 891 | const char *GlobalVariableDesc::getDescString() const { |
| 892 | return "llvm.dbg.global_variable"; |
| 893 | } |
| 894 | |
| 895 | /// getTypeString - Return a string used to label this descriptors type. |
| 896 | /// |
| 897 | const char *GlobalVariableDesc::getTypeString() const { |
| 898 | return "llvm.dbg.global_variable.type"; |
| 899 | } |
| 900 | |
| 901 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 902 | /// |
| 903 | const char *GlobalVariableDesc::getAnchorString() const { |
| 904 | return "llvm.dbg.global_variables"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 907 | #ifndef NDEBUG |
| 908 | void GlobalVariableDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 909 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 910 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 911 | << "Anchor(" << getAnchor() << "), " |
| 912 | << "Name(\"" << getName() << "\"), " |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 913 | << "Type(\"" << getTypeDesc() << "\"), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 914 | << "IsStatic(" << (isStatic() ? "true" : "false") << "), " |
| 915 | << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), " |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 916 | << "Global(" << Global << "), " |
| 917 | << "Line(" << Line << ")\n"; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 918 | } |
| 919 | #endif |
| 920 | |
| 921 | //===----------------------------------------------------------------------===// |
| 922 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 923 | SubprogramDesc::SubprogramDesc() |
| 924 | : GlobalDesc(DI_TAG_subprogram) |
| 925 | {} |
| 926 | |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 927 | /// ApplyToFields - Target the visitor to the fields of the |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 928 | /// SubprogramDesc. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 929 | void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 930 | GlobalDesc::ApplyToFields(Visitor); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 933 | /// getDescString - Return a string used to compose global names and labels. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 934 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 935 | const char *SubprogramDesc::getDescString() const { |
| 936 | return "llvm.dbg.subprogram"; |
| 937 | } |
| 938 | |
| 939 | /// getTypeString - Return a string used to label this descriptors type. |
| 940 | /// |
| 941 | const char *SubprogramDesc::getTypeString() const { |
| 942 | return "llvm.dbg.subprogram.type"; |
| 943 | } |
| 944 | |
| 945 | /// getAnchorString - Return a string used to label this descriptor's anchor. |
| 946 | /// |
| 947 | const char *SubprogramDesc::getAnchorString() const { |
| 948 | return "llvm.dbg.subprograms"; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 951 | #ifndef NDEBUG |
| 952 | void SubprogramDesc::dump() { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 953 | std::cerr << getDescString() << " " |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 954 | << "Tag(" << getTag() << "), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 955 | << "Anchor(" << getAnchor() << "), " |
| 956 | << "Name(\"" << getName() << "\"), " |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 957 | << "Type(\"" << getTypeDesc() << "\"), " |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 958 | << "IsStatic(" << (isStatic() ? "true" : "false") << "), " |
| 959 | << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n"; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 960 | } |
| 961 | #endif |
| 962 | |
Jim Laskey | 45ccae5 | 2006-02-28 20:15:07 +0000 | [diff] [blame] | 963 | //===----------------------------------------------------------------------===// |
| 964 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 965 | DebugInfoDesc *DIDeserializer::Deserialize(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 966 | return Deserialize(getGlobalVariable(V)); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 967 | } |
| 968 | DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 969 | // Handle NULL. |
| 970 | if (!GV) return NULL; |
| 971 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 972 | // Check to see if it has been already deserialized. |
| 973 | DebugInfoDesc *&Slot = GlobalDescs[GV]; |
| 974 | if (Slot) return Slot; |
| 975 | |
| 976 | // Get the Tag from the global. |
| 977 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
| 978 | |
| 979 | // Get the debug version if a compile unit. |
| 980 | if (Tag == DI_TAG_compile_unit) { |
| 981 | DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV); |
| 982 | } |
| 983 | |
| 984 | // Create an empty instance of the correct sort. |
| 985 | Slot = DebugInfoDesc::DescFactory(Tag); |
| 986 | assert(Slot && "Unknown Tag"); |
| 987 | |
| 988 | // Deserialize the fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 989 | DIDeserializeVisitor DRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 990 | DRAM.ApplyToFields(Slot); |
| 991 | |
| 992 | return Slot; |
| 993 | } |
| 994 | |
| 995 | //===----------------------------------------------------------------------===// |
| 996 | |
| 997 | /// getStrPtrType - Return a "sbyte *" type. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 998 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 999 | const PointerType *DISerializer::getStrPtrType() { |
| 1000 | // If not already defined. |
| 1001 | if (!StrPtrTy) { |
| 1002 | // Construct the pointer to signed bytes. |
| 1003 | StrPtrTy = PointerType::get(Type::SByteTy); |
| 1004 | } |
| 1005 | |
| 1006 | return StrPtrTy; |
| 1007 | } |
| 1008 | |
| 1009 | /// getEmptyStructPtrType - Return a "{ }*" type. |
| 1010 | /// |
| 1011 | const PointerType *DISerializer::getEmptyStructPtrType() { |
| 1012 | // If not already defined. |
| 1013 | if (!EmptyStructPtrTy) { |
| 1014 | // Construct the empty structure type. |
| 1015 | const StructType *EmptyStructTy = |
| 1016 | StructType::get(std::vector<const Type*>()); |
| 1017 | // Construct the pointer to empty structure type. |
| 1018 | EmptyStructPtrTy = PointerType::get(EmptyStructTy); |
| 1019 | } |
| 1020 | |
| 1021 | return EmptyStructPtrTy; |
| 1022 | } |
| 1023 | |
| 1024 | /// getTagType - Return the type describing the specified descriptor (via tag.) |
| 1025 | /// |
| 1026 | const StructType *DISerializer::getTagType(DebugInfoDesc *DD) { |
| 1027 | // Attempt to get the previously defined type. |
| 1028 | StructType *&Ty = TagTypes[DD->getTag()]; |
| 1029 | |
| 1030 | // If not already defined. |
| 1031 | if (!Ty) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1032 | // Set up fields vector. |
| 1033 | std::vector<const Type*> Fields; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1034 | // Get types of fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 1035 | DIGetTypesVisitor GTAM(*this, Fields); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1036 | GTAM.ApplyToFields(DD); |
| 1037 | |
| 1038 | // Construct structured type. |
| 1039 | Ty = StructType::get(Fields); |
| 1040 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1041 | // Register type name with module. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1042 | M->addTypeName(DD->getTypeString(), Ty); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | return Ty; |
| 1046 | } |
| 1047 | |
| 1048 | /// getString - Construct the string as constant string global. |
| 1049 | /// |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1050 | Constant *DISerializer::getString(const std::string &String) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1051 | // Check string cache for previous edition. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1052 | Constant *&Slot = StringCache[String]; |
| 1053 | // return Constant if previously defined. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1054 | if (Slot) return Slot; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1055 | // Construct string as an llvm constant. |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1056 | Constant *ConstStr = ConstantArray::get(String); |
| 1057 | // Otherwise create and return a new string global. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1058 | GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, |
| 1059 | GlobalVariable::InternalLinkage, |
| 1060 | ConstStr, "str", M); |
| 1061 | // Convert to generic string pointer. |
| 1062 | Slot = ConstantExpr::getCast(StrGV, getStrPtrType()); |
| 1063 | return Slot; |
| 1064 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /// Serialize - Recursively cast the specified descriptor into a GlobalVariable |
| 1068 | /// so that it can be serialized to a .bc or .ll file. |
| 1069 | GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) { |
| 1070 | // Check if the DebugInfoDesc is already in the map. |
| 1071 | GlobalVariable *&Slot = DescGlobals[DD]; |
| 1072 | |
| 1073 | // See if DebugInfoDesc exists, if so return prior GlobalVariable. |
| 1074 | if (Slot) return Slot; |
| 1075 | |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1076 | // Get the type associated with the Tag. |
| 1077 | const StructType *Ty = getTagType(DD); |
| 1078 | |
| 1079 | // Create the GlobalVariable early to prevent infinite recursion. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1080 | GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(), |
| 1081 | NULL, DD->getDescString(), M); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1082 | |
| 1083 | // Insert new GlobalVariable in DescGlobals map. |
| 1084 | Slot = GV; |
| 1085 | |
| 1086 | // Set up elements vector |
| 1087 | std::vector<Constant*> Elements; |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1088 | // Add fields. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 1089 | DISerializeVisitor SRAM(*this, Elements); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1090 | SRAM.ApplyToFields(DD); |
| 1091 | |
| 1092 | // Set the globals initializer. |
| 1093 | GV->setInitializer(ConstantStruct::get(Ty, Elements)); |
| 1094 | |
| 1095 | return GV; |
| 1096 | } |
| 1097 | |
| 1098 | //===----------------------------------------------------------------------===// |
| 1099 | |
| 1100 | /// markVisited - Return true if the GlobalVariable hase been "seen" before. |
| 1101 | /// Mark visited otherwise. |
| 1102 | bool DIVerifier::markVisited(GlobalVariable *GV) { |
| 1103 | // Check if the GlobalVariable is already in the Visited set. |
| 1104 | std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV); |
| 1105 | |
| 1106 | // See if GlobalVariable exists. |
| 1107 | bool Exists = VI != Visited.end() && *VI == GV; |
| 1108 | |
| 1109 | // Insert in set. |
| 1110 | if (!Exists) Visited.insert(VI, GV); |
| 1111 | |
| 1112 | return Exists; |
| 1113 | } |
| 1114 | |
| 1115 | /// Verify - Return true if the GlobalVariable appears to be a valid |
| 1116 | /// serialization of a DebugInfoDesc. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1117 | bool DIVerifier::Verify(Value *V) { |
| 1118 | return Verify(getGlobalVariable(V)); |
| 1119 | } |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1120 | bool DIVerifier::Verify(GlobalVariable *GV) { |
| 1121 | // Check if seen before. |
| 1122 | if (markVisited(GV)) return true; |
| 1123 | |
| 1124 | // Get the Tag |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 1125 | unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 1126 | if (Tag == DIInvalid) return false; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1127 | |
| 1128 | // If a compile unit we need the debug version. |
| 1129 | if (Tag == DI_TAG_compile_unit) { |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 1130 | DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV); |
Jim Laskey | f60c241 | 2006-02-06 21:54:05 +0000 | [diff] [blame] | 1131 | if (DebugVersion == DIInvalid) return false; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | // Construct an empty DebugInfoDesc. |
| 1135 | DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag); |
| 1136 | if (!DD) return false; |
| 1137 | |
| 1138 | // Get the initializer constant. |
| 1139 | ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer()); |
| 1140 | |
| 1141 | // Get the operand count. |
| 1142 | unsigned N = CI->getNumOperands(); |
| 1143 | |
| 1144 | // Get the field count. |
| 1145 | unsigned &Slot = Counts[Tag]; |
| 1146 | if (!Slot) { |
| 1147 | // Check the operand count to the field count |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 1148 | DICountVisitor CTAM; |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1149 | CTAM.ApplyToFields(DD); |
| 1150 | Slot = CTAM.getCount(); |
| 1151 | } |
| 1152 | |
| 1153 | // Field count must equal operand count. |
| 1154 | if (Slot != N) { |
| 1155 | delete DD; |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
| 1159 | // Check each field for valid type. |
Jim Laskey | c2f0c8d | 2006-02-06 19:12:02 +0000 | [diff] [blame] | 1160 | DIVerifyVisitor VRAM(*this, GV); |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1161 | VRAM.ApplyToFields(DD); |
| 1162 | |
| 1163 | // Release empty DebugInfoDesc. |
| 1164 | delete DD; |
| 1165 | |
| 1166 | // Return result of field tests. |
| 1167 | return VRAM.isValid(); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | //===----------------------------------------------------------------------===// |
| 1171 | |
| 1172 | |
| 1173 | MachineDebugInfo::MachineDebugInfo() |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1174 | : DR() |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1175 | , CompileUnits() |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1176 | , Directories() |
| 1177 | , SourceFiles() |
| 1178 | , Lines() |
| 1179 | { |
| 1180 | |
| 1181 | } |
| 1182 | MachineDebugInfo::~MachineDebugInfo() { |
| 1183 | |
| 1184 | } |
| 1185 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1186 | /// doInitialization - Initialize the debug state for a new module. |
| 1187 | /// |
| 1188 | bool MachineDebugInfo::doInitialization() { |
| 1189 | return false; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1192 | /// doFinalization - Tear down the debug state after completion of a module. |
| 1193 | /// |
| 1194 | bool MachineDebugInfo::doFinalization() { |
| 1195 | return false; |
| 1196 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1197 | |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 1198 | /// getDescFor - Convert a Value to a debug information descriptor. |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1199 | /// |
Jim Laskey | d96185a | 2006-02-13 12:50:39 +0000 | [diff] [blame] | 1200 | // FIXME - use new Value type when available. |
| 1201 | DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1202 | return DR.Deserialize(V); |
| 1203 | } |
| 1204 | |
| 1205 | /// Verify - Verify that a Value is debug information descriptor. |
| 1206 | /// |
| 1207 | bool MachineDebugInfo::Verify(Value *V) { |
| 1208 | DIVerifier VR; |
| 1209 | return VR.Verify(V); |
| 1210 | } |
| 1211 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1212 | /// AnalyzeModule - Scan the module for global debug information. |
| 1213 | /// |
| 1214 | void MachineDebugInfo::AnalyzeModule(Module &M) { |
| 1215 | SetupCompileUnits(M); |
| 1216 | } |
| 1217 | |
| 1218 | /// SetupCompileUnits - Set up the unique vector of compile units. |
| 1219 | /// |
| 1220 | void MachineDebugInfo::SetupCompileUnits(Module &M) { |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1221 | std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1222 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1223 | for (unsigned i = 0, N = CU.size(); i < N; i++) { |
| 1224 | CompileUnits.insert(CU[i]); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1225 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 1228 | /// getCompileUnits - Return a vector of debug compile units. |
| 1229 | /// |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 1230 | const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{ |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 1231 | return CompileUnits; |
| 1232 | } |
| 1233 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1234 | /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the |
| 1235 | /// named GlobalVariable. |
| 1236 | std::vector<GlobalVariable*> |
| 1237 | MachineDebugInfo::getGlobalVariablesUsing(Module &M, |
| 1238 | const std::string &RootName) { |
| 1239 | return ::getGlobalVariablesUsing(M, RootName); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 1240 | } |