diff options
Diffstat (limited to 'lib/VMCore/iMemory.cpp')
-rw-r--r-- | lib/VMCore/iMemory.cpp | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/lib/VMCore/iMemory.cpp b/lib/VMCore/iMemory.cpp index 41c14b04d5..4403224b88 100644 --- a/lib/VMCore/iMemory.cpp +++ b/lib/VMCore/iMemory.cpp @@ -7,8 +7,19 @@ #include "llvm/iMemory.h" #include "llvm/ConstPoolVals.h" -const Type *LoadInst::getIndexedType(const Type *Ptr, - const vector<ConstPoolVal*> &Idx) { +//===----------------------------------------------------------------------===// +// MemAccessInst Implementation +//===----------------------------------------------------------------------===// + +// getIndexedType - Returns the type of the element that would be loaded with +// a load instruction with the specified parameters. +// +// A null type is returned if the indices are invalid for the specified +// pointer type. +// +const Type *MemAccessInst::getIndexedType(const Type *Ptr, + const vector<ConstPoolVal*> &Idx, + bool AllowStructLeaf = false) { if (!Ptr->isPointerType()) return 0; // Type isn't a pointer type! // Get the type pointed to... @@ -17,7 +28,8 @@ const Type *LoadInst::getIndexedType(const Type *Ptr, if (Ptr->isStructType()) { unsigned CurIDX = 0; while (Ptr->isStructType()) { - if (Idx.size() == CurIDX) return 0; // Can't load a whole structure! + if (Idx.size() == CurIDX) + return AllowStructLeaf ? Ptr : 0; // Can't load a whole structure!?!? if (Idx[CurIDX]->getType() != Type::UByteTy) return 0; // Illegal idx unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue(); @@ -33,11 +45,49 @@ const Type *LoadInst::getIndexedType(const Type *Ptr, } +//===----------------------------------------------------------------------===// +// LoadInst Implementation +//===----------------------------------------------------------------------===// + LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx, const string &Name = "") - : Instruction(getIndexedType(Ptr->getType(), Idx), Load, Name) { + : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Name) { assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!"); - assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!"); + Operands.reserve(1+Idx.size()); + Operands.push_back(Use(Ptr, this)); + + for (unsigned i = 0, E = Idx.size(); i != E; ++i) + Operands.push_back(Use(Idx[i], this)); +} + + +//===----------------------------------------------------------------------===// +// StoreInst Implementation +//===----------------------------------------------------------------------===// + +StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx, + const string &Name = "") + : MemAccessInst(Type::VoidTy, Store, Name) { + assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!"); + + Operands.reserve(2+Idx.size()); + Operands.push_back(Use(Val, this)); + Operands.push_back(Use(Ptr, this)); + + for (unsigned i = 0, E = Idx.size(); i != E; ++i) + Operands.push_back(Use(Idx[i], this)); +} + + +//===----------------------------------------------------------------------===// +// GetElementPtrInst Implementation +//===----------------------------------------------------------------------===// + +GetElementPtrInst::GetElementPtrInst(Value *Ptr, + const vector<ConstPoolVal*> &Idx, + const string &Name = "") + : MemAccessInst(PointerType::getPointerType(getIndexedType(Ptr->getType(), Idx, true)), GetElementPtr, Name) { + assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!"); Operands.reserve(1+Idx.size()); Operands.push_back(Use(Ptr, this)); |