summaryrefslogtreecommitdiff
path: root/include/llvm/iMemory.h
blob: 077266de94cb58d7a87ed995b00935389b51884d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
//
// This file contains the declarations of all of the memory related operators.
// This includes: malloc, free, alloca, load, store, getfield, putfield
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_IMEMORY_H
#define LLVM_IMEMORY_H

#include "llvm/Instruction.h"
#include "llvm/DerivedTypes.h"
#include "llvm/ConstPoolVals.h"

class ConstPoolType;

class AllocationInst : public Instruction {
protected:
  UseTy<ConstPoolType> TyVal;
  Use ArraySize;
public:
  AllocationInst(ConstPoolType *tyVal, Value *arrSize, unsigned iTy, 
		 const string &Name = "") 
    : Instruction(tyVal->getValue(), iTy, Name),
      TyVal(tyVal, this), ArraySize(arrSize, this) {

    // Make sure they didn't try to specify a size for an invalid type...
    assert(arrSize == 0 || 
	   (getType()->getValueType()->isArrayType() && 
	    ((const ArrayType*)getType()->getValueType())->isUnsized()) && 
	   "Trying to allocate something other than unsized array, with size!");

    // Make sure that if a size is specified, that it is a uint!
    assert(arrSize == 0 || arrSize->getType() == Type::UIntTy &&
	   "Malloc SIZE is not a 'uint'!");
  }
  inline ~AllocationInst() {}

  // getType - Overload to return most specific pointer type...
  inline const PointerType *getType() const {
    return (const PointerType*)Instruction::getType(); 
  }

  virtual Instruction *clone() const = 0;

  inline virtual void dropAllReferences() { TyVal = 0; ArraySize = 0; }
  virtual bool setOperand(unsigned i, Value *Val) { 
    if (i == 0) {
      assert(!Val || Val->getValueType() == Value::ConstantVal);
      TyVal = (ConstPoolType*)Val;
      return true;
    } else if (i == 1) {
      // Make sure they didn't try to specify a size for an invalid type...
      assert(Val == 0 || 
	     (getType()->getValueType()->isArrayType() && 
	      ((const ArrayType*)getType()->getValueType())->isUnsized()) && 
           "Trying to allocate something other than unsized array, with size!");
      
      // Make sure that if a size is specified, that it is a uint!
      assert(Val == 0 || Val->getType() == Type::UIntTy &&
	     "Malloc SIZE is not a 'uint'!");
      
      ArraySize = Val;
      return true;
    }
    return false; 
  }

  virtual unsigned getNumOperands() const { return 2; }

  virtual const Value *getOperand(unsigned i) const { 
    return i == 0 ? TyVal : (i == 1 ? ArraySize : 0); 
  }
};

class MallocInst : public AllocationInst {
public:
  MallocInst(ConstPoolType *tyVal, Value *ArraySize = 0, 
	     const string &Name = "") 
    : AllocationInst(tyVal, ArraySize, Instruction::Malloc, Name) {}
  inline ~MallocInst() {}

  virtual Instruction *clone() const { 
    return new MallocInst(TyVal, ArraySize);
  }

  virtual string getOpcode() const { return "malloc"; }
};

class AllocaInst : public AllocationInst {
public:
  AllocaInst(ConstPoolType *tyVal, Value *ArraySize = 0, 
	     const string &Name = "") 
    : AllocationInst(tyVal, ArraySize, Instruction::Alloca, Name) {}
  inline ~AllocaInst() {}

  virtual Instruction *clone() const { 
    return new AllocaInst(TyVal, ArraySize);
  }

  virtual string getOpcode() const { return "alloca"; }
};



class FreeInst : public Instruction {
protected:
  Use Pointer;
public:
  FreeInst(Value *Ptr, const string &Name = "") 
    : Instruction(Type::VoidTy, Instruction::Free, Name),
      Pointer(Ptr, this) {

    assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
  }
  inline ~FreeInst() {}

  virtual Instruction *clone() const { return new FreeInst(Pointer); }

  inline virtual void dropAllReferences() { Pointer = 0;  }

  virtual bool setOperand(unsigned i, Value *Val) { 
    if (i == 0) {
      assert(!Val || Val->getType()->isPointerType() &&
	     "Can't free nonpointer!");
      Pointer = Val;
      return true;
    }
    return false; 
  }

  virtual unsigned getNumOperands() const { return 1; }
  virtual const Value *getOperand(unsigned i) const { 
    return i == 0 ? Pointer : 0;
  }

  virtual string getOpcode() const { return "free"; }
};

#endif // LLVM_IMEMORY_H