1 //===-- iCall.cpp - Implement the call & invoke instructions --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the call and invoke instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/iOther.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
19 //===----------------------------------------------------------------------===//
20 // CallInst Implementation
21 //===----------------------------------------------------------------------===//
23 CallInst::CallInst(Value *Func, const std::vector<Value*> ¶ms,
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));
31 const FunctionType *MTy =
32 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
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));
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) {
48 Operands.push_back(Use(Func, this));
50 const FunctionType *MTy =
51 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
53 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
54 assert(PL.empty() && "Calling a function with bad signature");
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) {
63 Operands.push_back(Use(Func, this));
65 const FunctionType *MTy =
66 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
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));
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));
81 //===----------------------------------------------------------------------===//
82 // InvokeInst Implementation
83 //===----------------------------------------------------------------------===//
85 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
86 BasicBlock *IfException,
87 const std::vector<Value*> ¶ms,
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());
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");
104 for (unsigned i = 0; i < params.size(); i++)
105 Operands.push_back(Use(params[i], this));
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));