Added LLVM project notice to the top of every C++ source file.
[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/DerivedTypes.h"
17 #include "llvm/Function.h"
18
19 //===----------------------------------------------------------------------===//
20 //                        CallInst Implementation
21 //===----------------------------------------------------------------------===//
22
23 CallInst::CallInst(Value *Func, const std::vector<Value*> &params, 
24                    const std::string &Name, Instruction *InsertBefore) 
25   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
26                                  ->getElementType())->getReturnType(),
27                 Instruction::Call, Name, InsertBefore) {
28   Operands.reserve(1+params.size());
29   Operands.push_back(Use(Func, this));
30
31   const FunctionType *MTy = 
32     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
33
34   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
35   assert(params.size() == PL.size() || 
36          (MTy->isVarArg() && params.size() > PL.size()) &&
37          "Calling a function with bad signature");
38   for (unsigned i = 0; i < params.size(); i++)
39     Operands.push_back(Use(params[i], this));
40 }
41
42 CallInst::CallInst(Value *Func, const std::string &Name,
43                    Instruction *InsertBefore)
44   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
45                                    ->getElementType())->getReturnType(),
46                 Instruction::Call, Name, InsertBefore) {
47   Operands.reserve(1);
48   Operands.push_back(Use(Func, this));
49   
50   const FunctionType *MTy = 
51     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
52
53   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
54   assert(PL.empty() && "Calling a function with bad signature");
55 }
56
57 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
58                    Instruction  *InsertBefore)
59   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
60                                    ->getElementType())->getReturnType(),
61                 Instruction::Call, Name, InsertBefore) {
62   Operands.reserve(2);
63   Operands.push_back(Use(Func, this));
64   
65   const FunctionType *MTy = 
66     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
67
68   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
69   assert(PL.size() == 1 || (MTy->isVarArg() && PL.empty()) &&
70          "Calling a function with bad signature");
71   Operands.push_back(Use(A, this));
72 }
73
74 CallInst::CallInst(const CallInst &CI) 
75   : Instruction(CI.getType(), Instruction::Call) {
76   Operands.reserve(CI.Operands.size());
77   for (unsigned i = 0; i < CI.Operands.size(); ++i)
78     Operands.push_back(Use(CI.Operands[i], this));
79 }
80
81 //===----------------------------------------------------------------------===//
82 //                        InvokeInst Implementation
83 //===----------------------------------------------------------------------===//
84
85 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
86                        BasicBlock *IfException,
87                        const std::vector<Value*> &params,
88                        const std::string &Name, Instruction *InsertBefore)
89   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
90                                     ->getElementType())->getReturnType(),
91                    Instruction::Invoke, Name, InsertBefore) {
92   Operands.reserve(3+params.size());
93   Operands.push_back(Use(Func, this));
94   Operands.push_back(Use((Value*)IfNormal, this));
95   Operands.push_back(Use((Value*)IfException, this));
96   const FunctionType *MTy = 
97     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
98   
99   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
100   assert((params.size() == PL.size()) || 
101          (MTy->isVarArg() && params.size() > PL.size()) &&
102          "Calling a function with bad signature");
103   
104   for (unsigned i = 0; i < params.size(); i++)
105     Operands.push_back(Use(params[i], this));
106 }
107
108 InvokeInst::InvokeInst(const InvokeInst &CI) 
109   : TerminatorInst(CI.getType(), Instruction::Invoke) {
110   Operands.reserve(CI.Operands.size());
111   for (unsigned i = 0; i < CI.Operands.size(); ++i)
112     Operands.push_back(Use(CI.Operands[i], this));
113 }
114