Initial revision
[oota-llvm.git] / include / llvm / iMemory.h
1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
2 //
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_IMEMORY_H
9 #define LLVM_IMEMORY_H
10
11 #include "llvm/Instruction.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/ConstPoolVals.h"
14
15 class ConstPoolType;
16
17 class AllocationInst : public Instruction {
18 protected:
19   UseTy<ConstPoolType> TyVal;
20   Use ArraySize;
21 public:
22   AllocationInst(ConstPoolType *tyVal, Value *arrSize, unsigned iTy, 
23                  const string &Name = "") 
24     : Instruction(tyVal->getValue(), iTy, Name),
25       TyVal(tyVal, this), ArraySize(arrSize, this) {
26
27     // Make sure they didn't try to specify a size for an invalid type...
28     assert(arrSize == 0 || 
29            (getType()->getValueType()->isArrayType() && 
30             ((const ArrayType*)getType()->getValueType())->isUnsized()) && 
31            "Trying to allocate something other than unsized array, with size!");
32
33     // Make sure that if a size is specified, that it is a uint!
34     assert(arrSize == 0 || arrSize->getType() == Type::UIntTy &&
35            "Malloc SIZE is not a 'uint'!");
36   }
37   inline ~AllocationInst() {}
38
39   // getType - Overload to return most specific pointer type...
40   inline const PointerType *getType() const {
41     return (const PointerType*)Instruction::getType(); 
42   }
43
44   virtual Instruction *clone() const = 0;
45
46   inline virtual void dropAllReferences() { TyVal = 0; ArraySize = 0; }
47   virtual bool setOperand(unsigned i, Value *Val) { 
48     if (i == 0) {
49       assert(!Val || Val->getValueType() == Value::ConstantVal);
50       TyVal = (ConstPoolType*)Val;
51       return true;
52     } else if (i == 1) {
53       // Make sure they didn't try to specify a size for an invalid type...
54       assert(Val == 0 || 
55              (getType()->getValueType()->isArrayType() && 
56               ((const ArrayType*)getType()->getValueType())->isUnsized()) && 
57            "Trying to allocate something other than unsized array, with size!");
58       
59       // Make sure that if a size is specified, that it is a uint!
60       assert(Val == 0 || Val->getType() == Type::UIntTy &&
61              "Malloc SIZE is not a 'uint'!");
62       
63       ArraySize = Val;
64       return true;
65     }
66     return false; 
67   }
68
69   virtual unsigned getNumOperands() const { return 2; }
70
71   virtual const Value *getOperand(unsigned i) const { 
72     return i == 0 ? TyVal : (i == 1 ? ArraySize : 0); 
73   }
74 };
75
76 class MallocInst : public AllocationInst {
77 public:
78   MallocInst(ConstPoolType *tyVal, Value *ArraySize = 0, 
79              const string &Name = "") 
80     : AllocationInst(tyVal, ArraySize, Instruction::Malloc, Name) {}
81   inline ~MallocInst() {}
82
83   virtual Instruction *clone() const { 
84     return new MallocInst(TyVal, ArraySize);
85   }
86
87   virtual string getOpcode() const { return "malloc"; }
88 };
89
90 class AllocaInst : public AllocationInst {
91 public:
92   AllocaInst(ConstPoolType *tyVal, Value *ArraySize = 0, 
93              const string &Name = "") 
94     : AllocationInst(tyVal, ArraySize, Instruction::Alloca, Name) {}
95   inline ~AllocaInst() {}
96
97   virtual Instruction *clone() const { 
98     return new AllocaInst(TyVal, ArraySize);
99   }
100
101   virtual string getOpcode() const { return "alloca"; }
102 };
103
104
105
106 class FreeInst : public Instruction {
107 protected:
108   Use Pointer;
109 public:
110   FreeInst(Value *Ptr, const string &Name = "") 
111     : Instruction(Type::VoidTy, Instruction::Free, Name),
112       Pointer(Ptr, this) {
113
114     assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
115   }
116   inline ~FreeInst() {}
117
118   virtual Instruction *clone() const { return new FreeInst(Pointer); }
119
120   inline virtual void dropAllReferences() { Pointer = 0;  }
121
122   virtual bool setOperand(unsigned i, Value *Val) { 
123     if (i == 0) {
124       assert(!Val || Val->getType()->isPointerType() &&
125              "Can't free nonpointer!");
126       Pointer = Val;
127       return true;
128     }
129     return false; 
130   }
131
132   virtual unsigned getNumOperands() const { return 1; }
133   virtual const Value *getOperand(unsigned i) const { 
134     return i == 0 ? Pointer : 0;
135   }
136
137   virtual string getOpcode() const { return "free"; }
138 };
139
140 #endif // LLVM_IMEMORY_H