1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
6 //===----------------------------------------------------------------------===//
11 #include "llvm/Instruction.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/ConstPoolVals.h"
17 class AllocationInst : public Instruction {
19 UseTy<ConstPoolType> TyVal;
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) {
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!");
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'!");
37 inline ~AllocationInst() {}
39 // getType - Overload to return most specific pointer type...
40 inline const PointerType *getType() const {
41 return (const PointerType*)Instruction::getType();
44 virtual Instruction *clone() const = 0;
46 inline virtual void dropAllReferences() { TyVal = 0; ArraySize = 0; }
47 virtual bool setOperand(unsigned i, Value *Val) {
49 assert(!Val || Val->getValueType() == Value::ConstantVal);
50 TyVal = (ConstPoolType*)Val;
53 // Make sure they didn't try to specify a size for an invalid type...
55 (getType()->getValueType()->isArrayType() &&
56 ((const ArrayType*)getType()->getValueType())->isUnsized()) &&
57 "Trying to allocate something other than unsized array, with size!");
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'!");
69 virtual unsigned getNumOperands() const { return 2; }
71 virtual const Value *getOperand(unsigned i) const {
72 return i == 0 ? TyVal : (i == 1 ? ArraySize : 0);
76 class MallocInst : public AllocationInst {
78 MallocInst(ConstPoolType *tyVal, Value *ArraySize = 0,
79 const string &Name = "")
80 : AllocationInst(tyVal, ArraySize, Instruction::Malloc, Name) {}
81 inline ~MallocInst() {}
83 virtual Instruction *clone() const {
84 return new MallocInst(TyVal, ArraySize);
87 virtual string getOpcode() const { return "malloc"; }
90 class AllocaInst : public AllocationInst {
92 AllocaInst(ConstPoolType *tyVal, Value *ArraySize = 0,
93 const string &Name = "")
94 : AllocationInst(tyVal, ArraySize, Instruction::Alloca, Name) {}
95 inline ~AllocaInst() {}
97 virtual Instruction *clone() const {
98 return new AllocaInst(TyVal, ArraySize);
101 virtual string getOpcode() const { return "alloca"; }
106 class FreeInst : public Instruction {
110 FreeInst(Value *Ptr, const string &Name = "")
111 : Instruction(Type::VoidTy, Instruction::Free, Name),
114 assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
116 inline ~FreeInst() {}
118 virtual Instruction *clone() const { return new FreeInst(Pointer); }
120 inline virtual void dropAllReferences() { Pointer = 0; }
122 virtual bool setOperand(unsigned i, Value *Val) {
124 assert(!Val || Val->getType()->isPointerType() &&
125 "Can't free nonpointer!");
132 virtual unsigned getNumOperands() const { return 1; }
133 virtual const Value *getOperand(unsigned i) const {
134 return i == 0 ? Pointer : 0;
137 virtual string getOpcode() const { return "free"; }
140 #endif // LLVM_IMEMORY_H