Added implementation of alternate CallInst constructors (one ctor is
[oota-llvm.git] / lib / VMCore / iCall.cpp
1 //===-- iCall.cpp - Implement the call & invoke instructions -----*- C++ -*--=//
2 //
3 // This file implements the call and invoke instructions.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iOther.h"
8 #include "llvm/iTerminators.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Function.h"
11
12 //===----------------------------------------------------------------------===//
13 //                        CallInst Implementation
14 //===----------------------------------------------------------------------===//
15
16 CallInst::CallInst(Value *Func, const std::vector<Value*> &params, 
17                    const std::string &Name, Instruction *InsertBefore) 
18   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
19                                  ->getElementType())->getReturnType(),
20                 Instruction::Call, Name, InsertBefore) {
21   Operands.reserve(1+params.size());
22   Operands.push_back(Use(Func, this));
23
24   const FunctionType *MTy = 
25     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
26
27   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
28   assert((params.size() == PL.size()) || 
29          (MTy->isVarArg() && params.size() >= PL.size()) &&
30          "Calling a function with bad signature");
31   for (unsigned i = 0; i < params.size(); i++)
32     Operands.push_back(Use(params[i], this));
33 }
34
35 CallInst::CallInst(Value *Func, const std::string &Name,
36                    Instruction  *InsertBefore)
37   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
38                                    ->getElementType())->getReturnType(),
39                 Instruction::Call, Name, InsertBefore) {
40   Operands.reserve(1);
41   Operands.push_back(Use(Func, this));
42   
43   const FunctionType *MTy = 
44     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
45
46   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
47   assert((0 == PL.size()) ||
48          (MTy->isVarArg() && 0 >= PL.size()) &&
49          "Calling a function with bad signature");
50 }
51
52 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
53                    Instruction  *InsertBefore)
54   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
55                                    ->getElementType())->getReturnType(),
56                 Instruction::Call, Name, InsertBefore) {
57   Operands.reserve(2);
58   Operands.push_back(Use(Func, this));
59   
60   const FunctionType *MTy = 
61     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
62
63   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
64   assert((1 == PL.size()) || 
65          (MTy->isVarArg() && 1 >= PL.size()) &&
66          "Calling a function with bad signature");
67   Operands.push_back(Use(A, this));
68 }
69
70 CallInst::CallInst(const CallInst &CI) 
71   : Instruction(CI.getType(), Instruction::Call) {
72   Operands.reserve(CI.Operands.size());
73   for (unsigned i = 0; i < CI.Operands.size(); ++i)
74     Operands.push_back(Use(CI.Operands[i], this));
75 }
76
77 //===----------------------------------------------------------------------===//
78 //                        InvokeInst Implementation
79 //===----------------------------------------------------------------------===//
80
81 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
82                        BasicBlock *IfException,
83                        const std::vector<Value*> &params,
84                        const std::string &Name, Instruction *InsertBefore)
85   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
86                                     ->getElementType())->getReturnType(),
87                    Instruction::Invoke, Name, InsertBefore) {
88   Operands.reserve(3+params.size());
89   Operands.push_back(Use(Func, this));
90   Operands.push_back(Use((Value*)IfNormal, this));
91   Operands.push_back(Use((Value*)IfException, this));
92   const FunctionType *MTy = 
93     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
94   
95   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
96   assert((params.size() == PL.size()) || 
97          (MTy->isVarArg() && params.size() > PL.size()) &&
98          "Calling a function with bad signature");
99   
100   for (unsigned i = 0; i < params.size(); i++)
101     Operands.push_back(Use(params[i], this));
102 }
103
104 InvokeInst::InvokeInst(const InvokeInst &CI) 
105   : TerminatorInst(CI.getType(), Instruction::Invoke) {
106   Operands.reserve(CI.Operands.size());
107   for (unsigned i = 0; i < CI.Operands.size(); ++i)
108     Operands.push_back(Use(CI.Operands[i], this));
109 }
110