Implemented shl, shl, & load instructions
[oota-llvm.git] / lib / VMCore / iMemory.cpp
1 //===-- iMemory.cpp - Implement Memory instructions --------------*- C++ -*--=//
2 //
3 // This file implements the various memory related classes defined in iMemory.h
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iMemory.h"
8 #include "llvm/ConstPoolVals.h"
9
10 const Type *LoadInst::getIndexedType(const Type *Ptr, 
11                                      const vector<ConstPoolVal*> &Idx) {
12   if (!Ptr->isPointerType()) return 0;   // Type isn't a pointer type!
13  
14   // Get the type pointed to...
15   Ptr = ((const PointerType*)Ptr)->getValueType();
16
17   if (Ptr->isStructType()) {
18     unsigned CurIDX = 0;
19     while (Ptr->isStructType()) {
20       if (Idx.size() == CurIDX) return 0;       // Can't load a whole structure!
21       if (Idx[CurIDX]->getType() != Type::UByteTy) return 0; // Illegal idx
22       unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
23       
24       const StructType *ST = (const StructType *)Ptr;
25       Ptr = ST->getElementTypes()[NextIdx];
26     }
27     return Ptr;
28   } else if (Ptr->isArrayType()) {
29     assert(0 && "Loading from arrays not implemented yet!");
30   } else {
31     return (Idx.size() == 0) ? Ptr : 0;  // Load directly through ptr
32   }
33 }
34
35
36 LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
37                    const string &Name = "")
38   : Instruction(getIndexedType(Ptr->getType(), Idx), Load, Name) {
39   assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
40   assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
41   Operands.reserve(1+Idx.size());
42   Operands.push_back(Use(Ptr, this));
43
44   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
45     Operands.push_back(Use(Idx[i], this));
46 }
47