summaryrefslogtreecommitdiff
path: root/include/llvm/InstrTypes.h
blob: be6ea26974033bb757c54c3a17c0d564b53badc1 (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
//===-- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*--=//
//
// This file defines various meta classes of instructions that exist in the VM
// representation.  Specific concrete subclasses of these may be found in the 
// i*.h files...
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_INSTRUCTION_TYPES_H
#define LLVM_INSTRUCTION_TYPES_H

#include "llvm/Instruction.h"
#include <list>
#include <vector>

class Method;
class SymTabValue;

//===----------------------------------------------------------------------===//
//                            TerminatorInst Class
//===----------------------------------------------------------------------===//

// TerminatorInst - Subclasses of this class are all able to terminate a basic 
// block.  Thus, these are all the flow control type of operations.
//
class TerminatorInst : public Instruction {
public:
  TerminatorInst(unsigned iType);
  inline ~TerminatorInst() {}

  // Terminators must implement the methods required by Instruction...
  virtual Instruction *clone() const = 0;
  virtual void dropAllReferences() = 0;
  virtual string getOpcode() const = 0;

  virtual bool setOperand(unsigned i, Value *Val) = 0;
  virtual const Value *getOperand(unsigned i) const = 0;

  // Additionally, they must provide a method to get at the successors of this
  // terminator instruction.  If 'idx' is out of range, a null pointer shall be
  // returned.
  //
  virtual const BasicBlock *getSuccessor(unsigned idx) const = 0;
  virtual unsigned getNumSuccessors() const = 0;

  inline BasicBlock *getSuccessor(unsigned idx) {
    return (BasicBlock*)((const TerminatorInst *)this)->getSuccessor(idx);
  }
};


//===----------------------------------------------------------------------===//
//                            UnaryOperator Class
//===----------------------------------------------------------------------===//

class UnaryOperator : public Instruction {
  Use Source;
public:
  UnaryOperator(Value *S, unsigned iType, const string &Name = "")
      : Instruction(S->getType(), iType, Name), Source(S, this) {
  }
  inline ~UnaryOperator() { dropAllReferences(); }

  virtual Instruction *clone() const { 
    return Instruction::getUnaryOperator(getInstType(), Source);
  }

  virtual void dropAllReferences() {
    Source = 0;
  }

  virtual string getOpcode() const = 0;

  virtual unsigned getNumOperands() const { return 1; }
  virtual const Value *getOperand(unsigned i) const {
    return (i == 0) ? Source : 0;
  }
  virtual bool setOperand(unsigned i, Value *Val) {
    // assert(Val && "operand must not be null!");
    if (i) return false;
    Source = Val;
    return true;
  }
};



//===----------------------------------------------------------------------===//
//                           BinaryOperator Class
//===----------------------------------------------------------------------===//

class BinaryOperator : public Instruction {
  Use Source1, Source2;
public:
  BinaryOperator(unsigned iType, Value *S1, Value *S2, 
                 const string &Name = "") 
    : Instruction(S1->getType(), iType, Name), Source1(S1, this), 
      Source2(S2, this){
    assert(S1 && S2 && S1->getType() == S2->getType());
  }
  inline ~BinaryOperator() { dropAllReferences(); }

  virtual Instruction *clone() const { 
    return Instruction::getBinaryOperator(getInstType(), Source1, Source2);
  }

  virtual void dropAllReferences() {
    Source1 = Source2 = 0;
  }

  virtual string getOpcode() const = 0;

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

  virtual bool setOperand(unsigned i, Value *Val) {
    // assert(Val && "operand must not be null!");
    if (i == 0) {
      Source1 = Val; //assert(Val->getType() == Source2->getType());
    } else if (i == 1) {
      Source2 = Val; //assert(Val->getType() == Source1->getType());
    } else {
      return false;
    }
    return true;
  }
};

#endif