Refactor common initialization code in private init() functions.
[oota-llvm.git] / lib / VMCore / iCall.cpp
1 //===-- iCall.cpp - Implement the call & invoke instructions --------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the call and invoke instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/iOther.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Support/CallSite.h"
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //                        CallInst Implementation
24 //===----------------------------------------------------------------------===//
25
26 CallInst::CallInst(Value *Func, const std::vector<Value*> &params, 
27                    const std::string &Name, Instruction *InsertBefore) 
28   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
29                                  ->getElementType())->getReturnType(),
30                 Instruction::Call, Name, InsertBefore) {
31   Operands.reserve(1+params.size());
32   Operands.push_back(Use(Func, this));
33
34   const FunctionType *FTy = 
35     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
36
37   assert((params.size() == FTy->getNumParams() || 
38           (FTy->isVarArg() && params.size() > FTy->getNumParams())) &&
39          "Calling a function with bad signature");
40   for (unsigned i = 0; i != params.size(); i++)
41     Operands.push_back(Use(params[i], this));
42 }
43
44 CallInst::CallInst(Value *Func, const std::string &Name,
45                    Instruction *InsertBefore)
46   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
47                                    ->getElementType())->getReturnType(),
48                 Instruction::Call, Name, InsertBefore) {
49   Operands.reserve(1);
50   Operands.push_back(Use(Func, this));
51   
52   const FunctionType *MTy = 
53     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
54
55   assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
56 }
57
58 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
59                    Instruction  *InsertBefore)
60   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
61                                    ->getElementType())->getReturnType(),
62                 Instruction::Call, Name, InsertBefore) {
63   Operands.reserve(2);
64   Operands.push_back(Use(Func, this));
65   
66   const FunctionType *MTy = 
67     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
68
69   assert((MTy->getNumParams() == 1 ||
70           (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
71          "Calling a function with bad signature");
72   Operands.push_back(Use(A, this));
73 }
74
75 CallInst::CallInst(const CallInst &CI) 
76   : Instruction(CI.getType(), Instruction::Call) {
77   Operands.reserve(CI.Operands.size());
78   for (unsigned i = 0; i < CI.Operands.size(); ++i)
79     Operands.push_back(Use(CI.Operands[i], this));
80 }
81
82 const Function *CallInst::getCalledFunction() const {
83   if (const Function *F = dyn_cast<Function>(Operands[0]))
84     return F;
85   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
86     return cast<Function>(CPR->getValue());
87   return 0;
88 }
89 Function *CallInst::getCalledFunction() {
90   if (Function *F = dyn_cast<Function>(Operands[0]))
91     return F;
92   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
93     return cast<Function>(CPR->getValue());
94   return 0;
95 }
96
97
98 //===----------------------------------------------------------------------===//
99 //                        InvokeInst Implementation
100 //===----------------------------------------------------------------------===//
101
102 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
103                       const std::vector<Value*> &Params)
104 {
105   Operands.reserve(3+Params.size());
106   Operands.push_back(Use(Fn, this));
107   Operands.push_back(Use((Value*)IfNormal, this));
108   Operands.push_back(Use((Value*)IfException, this));
109   const FunctionType *MTy = 
110     cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
111   
112   assert((Params.size() == MTy->getNumParams()) || 
113          (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
114          "Calling a function with bad signature");
115   
116   for (unsigned i = 0; i < Params.size(); i++)
117     Operands.push_back(Use(Params[i], this));
118 }
119
120 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
121                        BasicBlock *IfException,
122                        const std::vector<Value*> &Params,
123                        const std::string &Name, Instruction *InsertBefore)
124   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
125                                     ->getElementType())->getReturnType(),
126                    Instruction::Invoke, Name, InsertBefore) {
127   init(Fn, IfNormal, IfException, Params);
128 }
129
130 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
131                        BasicBlock *IfException,
132                        const std::vector<Value*> &Params,
133                        const std::string &Name, BasicBlock *InsertAtEnd)
134   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
135                                     ->getElementType())->getReturnType(),
136                    Instruction::Invoke, Name, InsertAtEnd) {
137   init(Fn, IfNormal, IfException, Params);
138 }
139
140 InvokeInst::InvokeInst(const InvokeInst &CI) 
141   : TerminatorInst(CI.getType(), Instruction::Invoke) {
142   Operands.reserve(CI.Operands.size());
143   for (unsigned i = 0; i < CI.Operands.size(); ++i)
144     Operands.push_back(Use(CI.Operands[i], this));
145 }
146
147 const Function *InvokeInst::getCalledFunction() const {
148   if (const Function *F = dyn_cast<Function>(Operands[0]))
149     return F;
150   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
151     return cast<Function>(CPR->getValue());
152   return 0;
153 }
154 Function *InvokeInst::getCalledFunction() {
155   if (Function *F = dyn_cast<Function>(Operands[0]))
156     return F;
157   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
158     return cast<Function>(CPR->getValue());
159   return 0;
160 }
161
162 Function *CallSite::getCalledFunction() const {
163   Value *Callee = getCalledValue();
164   if (Function *F = dyn_cast<Function>(Callee))
165     return F;
166   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
167     return cast<Function>(CPR->getValue());
168   return 0;
169 }