-//===----------------------------------------------------------------------===//
-
-/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
-/// specified value in their initializer somewhere.
-static void
-getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
- // Scan though value users.
- for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
- // If the user is a GlobalVariable then add to result.
- Result.push_back(GV);
- } else if (Constant *C = dyn_cast<Constant>(*I)) {
- // If the user is a constant variable then scan its users
- getGlobalVariablesUsing(C, Result);
- }
- }
-}
-
-/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
-/// named GlobalVariable.
-static std::vector<GlobalVariable*>
-getGlobalVariablesUsing(Module &M, const std::string &RootName) {
- std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
-
- std::vector<const Type*> FieldTypes;
- FieldTypes.push_back(Type::Int32Ty);
- FieldTypes.push_back(Type::Int32Ty);
-
- // Get the GlobalVariable root.
- GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
- StructType::get(FieldTypes));
-
- // If present and linkonce then scan for users.
- if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
- getGlobalVariablesUsing(UseRoot, Result);
- }
-
- return Result;
-}
-
-/// isStringValue - Return true if the given value can be coerced to a string.
-///
-static bool isStringValue(Value *V) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
- if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
- ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
- return Init->isString();
- }
- } else if (Constant *C = dyn_cast<Constant>(V)) {
- if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
- return isStringValue(GV);
- else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
- if (CE->getOpcode() == Instruction::GetElementPtr) {
- if (CE->getNumOperands() == 3 &&
- cast<Constant>(CE->getOperand(1))->isNullValue() &&
- isa<ConstantInt>(CE->getOperand(2))) {
- return isStringValue(CE->getOperand(0));
- }
- }
- }
- }
- return false;
-}
-
-/// getGlobalVariable - Return either a direct or cast Global value.
-///
-static GlobalVariable *getGlobalVariable(Value *V) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
- return GV;
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() == Instruction::BitCast) {
- return dyn_cast<GlobalVariable>(CE->getOperand(0));
- }
- }
- return NULL;
-}
-
-/// isGlobalVariable - Return true if the given value can be coerced to a
-/// GlobalVariable.
-static bool isGlobalVariable(Value *V) {
- if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
- return true;
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() == Instruction::BitCast) {
- return isa<GlobalVariable>(CE->getOperand(0));
- }
- }
- return false;
-}
-
-/// getUIntOperand - Return ith operand if it is an unsigned integer.
-///
-static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
- // Make sure the GlobalVariable has an initializer.
- if (!GV->hasInitializer()) return NULL;
-
- // Get the initializer constant.
- ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
- if (!CI) return NULL;
-
- // Check if there is at least i + 1 operands.
- unsigned N = CI->getNumOperands();
- if (i >= N) return NULL;
-
- // Check constant.
- return dyn_cast<ConstantInt>(CI->getOperand(i));
-}
-
-//===----------------------------------------------------------------------===//
-
-/// ApplyToFields - Target the visitor to each field of the debug information
-/// descriptor.
-void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
- DD->ApplyToFields(this);
-}
-
-//===----------------------------------------------------------------------===//
-/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
-/// the supplied DebugInfoDesc.
-class DICountVisitor : public DIVisitor {
-private:
- unsigned Count; // Running count of fields.
-
-public:
- DICountVisitor() : DIVisitor(), Count(0) {}
-
- // Accessors.
- unsigned getCount() const { return Count; }
-
- /// Apply - Count each of the fields.
- ///
- virtual void Apply(int &Field) { ++Count; }
- virtual void Apply(unsigned &Field) { ++Count; }
- virtual void Apply(int64_t &Field) { ++Count; }
- virtual void Apply(uint64_t &Field) { ++Count; }
- virtual void Apply(bool &Field) { ++Count; }
- virtual void Apply(std::string &Field) { ++Count; }
- virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
- virtual void Apply(GlobalVariable *&Field) { ++Count; }
- virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
- ++Count;
- }
-};
-
-//===----------------------------------------------------------------------===//
-/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
-/// supplied DebugInfoDesc.
-class DIDeserializeVisitor : public DIVisitor {
-private:
- DIDeserializer &DR; // Active deserializer.
- unsigned I; // Current operand index.
- ConstantStruct *CI; // GlobalVariable constant initializer.
-
-public:
- DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
- : DIVisitor()
- , DR(D)
- , I(0)
- , CI(cast<ConstantStruct>(GV->getInitializer()))
- {}
-
- /// Apply - Set the value of each of the fields.
- ///
- virtual void Apply(int &Field) {
- Constant *C = CI->getOperand(I++);
- Field = cast<ConstantInt>(C)->getSExtValue();
- }
- virtual void Apply(unsigned &Field) {
- Constant *C = CI->getOperand(I++);
- Field = cast<ConstantInt>(C)->getZExtValue();
- }
- virtual void Apply(int64_t &Field) {
- Constant *C = CI->getOperand(I++);
- Field = cast<ConstantInt>(C)->getSExtValue();
- }
- virtual void Apply(uint64_t &Field) {
- Constant *C = CI->getOperand(I++);
- Field = cast<ConstantInt>(C)->getZExtValue();
- }
- virtual void Apply(bool &Field) {
- Constant *C = CI->getOperand(I++);
- Field = cast<ConstantInt>(C)->getZExtValue();
- }
- virtual void Apply(std::string &Field) {
- Constant *C = CI->getOperand(I++);
- Field = C->getStringValue();
- }
- virtual void Apply(DebugInfoDesc *&Field) {
- Constant *C = CI->getOperand(I++);
- Field = DR.Deserialize(C);
- }
- virtual void Apply(GlobalVariable *&Field) {
- Constant *C = CI->getOperand(I++);
- Field = getGlobalVariable(C);
- }
- virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
- Field.resize(0);
- Constant *C = CI->getOperand(I++);
- GlobalVariable *GV = getGlobalVariable(C);
- if (GV->hasInitializer()) {
- if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
- for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
- GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
- DebugInfoDesc *DE = DR.Deserialize(GVE);
- Field.push_back(DE);
- }
- } else if (GV->getInitializer()->isNullValue()) {
- if (const ArrayType *T =
- dyn_cast<ArrayType>(GV->getType()->getElementType())) {
- Field.resize(T->getNumElements());
- }
- }
- }
- }
-};