2003-10-13 05:32:08 +02:00
|
|
|
//===-- iCall.cpp - Implement the call & invoke instructions --------------===//
|
2003-10-20 21:43:21 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2001-10-13 09:01:45 +02:00
|
|
|
// This file implements the call and invoke instructions.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/iOther.h"
|
2001-10-13 09:01:45 +02:00
|
|
|
#include "llvm/iTerminators.h"
|
2003-10-31 18:51:16 +01:00
|
|
|
#include "llvm/Constants.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-04-07 22:49:59 +02:00
|
|
|
#include "llvm/Function.h"
|
2003-11-20 18:45:12 +01:00
|
|
|
#include "llvm/Support/CallSite.h"
|
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2001-10-13 09:01:45 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-07 10:36:50 +02:00
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
CallInst::CallInst(Value *Func, const std::vector<Value*> ¶ms,
|
2002-09-10 17:45:53 +02:00
|
|
|
const std::string &Name, Instruction *InsertBefore)
|
2002-04-07 22:49:59 +02:00
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
2001-12-04 01:03:30 +01:00
|
|
|
->getElementType())->getReturnType(),
|
2002-09-10 17:45:53 +02:00
|
|
|
Instruction::Call, Name, InsertBefore) {
|
2001-07-07 10:36:50 +02:00
|
|
|
Operands.reserve(1+params.size());
|
2002-04-07 22:49:59 +02:00
|
|
|
Operands.push_back(Use(Func, this));
|
2001-10-13 09:01:45 +02:00
|
|
|
|
2004-02-09 05:14:01 +01:00
|
|
|
const FunctionType *FTy =
|
2002-04-07 22:49:59 +02:00
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2004-02-09 05:14:01 +01:00
|
|
|
assert((params.size() == FTy->getNumParams() ||
|
|
|
|
(FTy->isVarArg() && params.size() > FTy->getNumParams())) &&
|
2001-07-26 00:47:55 +02:00
|
|
|
"Calling a function with bad signature");
|
2004-02-09 05:14:01 +01:00
|
|
|
for (unsigned i = 0; i != params.size(); i++)
|
2001-07-07 10:36:50 +02:00
|
|
|
Operands.push_back(Use(params[i], this));
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-02-01 01:39:58 +01:00
|
|
|
CallInst::CallInst(Value *Func, const std::string &Name,
|
2003-02-01 04:33:22 +01:00
|
|
|
Instruction *InsertBefore)
|
2003-02-01 01:39:58 +01:00
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
|
|
|
Instruction::Call, Name, InsertBefore) {
|
|
|
|
Operands.reserve(1);
|
|
|
|
Operands.push_back(Use(Func, this));
|
|
|
|
|
|
|
|
const FunctionType *MTy =
|
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
|
|
|
|
2004-02-09 05:14:01 +01:00
|
|
|
assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
|
2003-02-01 01:39:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
|
|
|
|
Instruction *InsertBefore)
|
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
|
|
|
Instruction::Call, Name, InsertBefore) {
|
|
|
|
Operands.reserve(2);
|
|
|
|
Operands.push_back(Use(Func, this));
|
|
|
|
|
|
|
|
const FunctionType *MTy =
|
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
|
|
|
|
2004-02-09 05:14:01 +01:00
|
|
|
assert((MTy->getNumParams() == 1 ||
|
|
|
|
(MTy->isVarArg() && MTy->getNumParams() == 0)) &&
|
2003-02-01 01:39:58 +01:00
|
|
|
"Calling a function with bad signature");
|
|
|
|
Operands.push_back(Use(A, this));
|
|
|
|
}
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
CallInst::CallInst(const CallInst &CI)
|
2001-07-07 10:36:50 +02:00
|
|
|
: Instruction(CI.getType(), Instruction::Call) {
|
|
|
|
Operands.reserve(CI.Operands.size());
|
|
|
|
for (unsigned i = 0; i < CI.Operands.size(); ++i)
|
|
|
|
Operands.push_back(Use(CI.Operands[i], this));
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-10-31 18:51:16 +01:00
|
|
|
const Function *CallInst::getCalledFunction() const {
|
|
|
|
if (const Function *F = dyn_cast<Function>(Operands[0]))
|
|
|
|
return F;
|
|
|
|
if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
|
|
|
|
return cast<Function>(CPR->getValue());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Function *CallInst::getCalledFunction() {
|
|
|
|
if (Function *F = dyn_cast<Function>(Operands[0]))
|
|
|
|
return F;
|
|
|
|
if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
|
|
|
|
return cast<Function>(CPR->getValue());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-13 09:01:45 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// InvokeInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-04-09 20:36:52 +02:00
|
|
|
InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
|
2002-01-20 23:54:45 +01:00
|
|
|
BasicBlock *IfException,
|
|
|
|
const std::vector<Value*> ¶ms,
|
2002-09-10 17:45:53 +02:00
|
|
|
const std::string &Name, Instruction *InsertBefore)
|
2002-04-07 22:49:59 +02:00
|
|
|
: TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
|
2001-12-04 01:03:30 +01:00
|
|
|
->getElementType())->getReturnType(),
|
2002-09-10 17:45:53 +02:00
|
|
|
Instruction::Invoke, Name, InsertBefore) {
|
2001-10-13 09:01:45 +02:00
|
|
|
Operands.reserve(3+params.size());
|
2002-04-07 22:49:59 +02:00
|
|
|
Operands.push_back(Use(Func, this));
|
2002-04-09 20:36:52 +02:00
|
|
|
Operands.push_back(Use((Value*)IfNormal, this));
|
|
|
|
Operands.push_back(Use((Value*)IfException, this));
|
2002-04-05 00:19:18 +02:00
|
|
|
const FunctionType *MTy =
|
2002-04-07 22:49:59 +02:00
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
2001-10-13 09:01:45 +02:00
|
|
|
|
2004-02-09 05:14:01 +01:00
|
|
|
assert((params.size() == MTy->getNumParams()) ||
|
|
|
|
(MTy->isVarArg() && params.size() > MTy->getNumParams()) &&
|
2001-10-13 09:01:45 +02:00
|
|
|
"Calling a function with bad signature");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < params.size(); i++)
|
|
|
|
Operands.push_back(Use(params[i], this));
|
|
|
|
}
|
|
|
|
|
2003-11-20 18:45:12 +01:00
|
|
|
InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
|
|
|
|
BasicBlock *IfException,
|
|
|
|
const std::vector<Value*> ¶ms,
|
|
|
|
const std::string &Name, BasicBlock *InsertAtEnd)
|
|
|
|
: TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
|
|
|
Instruction::Invoke, Name) {
|
|
|
|
Operands.reserve(3+params.size());
|
|
|
|
Operands.push_back(Use(Func, this));
|
|
|
|
Operands.push_back(Use((Value*)IfNormal, this));
|
|
|
|
Operands.push_back(Use((Value*)IfException, this));
|
|
|
|
const FunctionType *MTy =
|
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
|
|
|
|
2004-02-09 05:14:01 +01:00
|
|
|
assert((params.size() == MTy->getNumParams()) ||
|
|
|
|
(MTy->isVarArg() && params.size() > MTy->getNumParams()) &&
|
2003-11-20 18:45:12 +01:00
|
|
|
"Calling a function with bad signature");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < params.size(); i++)
|
|
|
|
Operands.push_back(Use(params[i], this));
|
|
|
|
|
|
|
|
if (InsertAtEnd)
|
|
|
|
InsertAtEnd->getInstList().push_back(this);
|
|
|
|
}
|
|
|
|
|
2001-10-13 09:01:45 +02:00
|
|
|
InvokeInst::InvokeInst(const InvokeInst &CI)
|
|
|
|
: TerminatorInst(CI.getType(), Instruction::Invoke) {
|
|
|
|
Operands.reserve(CI.Operands.size());
|
|
|
|
for (unsigned i = 0; i < CI.Operands.size(); ++i)
|
|
|
|
Operands.push_back(Use(CI.Operands[i], this));
|
|
|
|
}
|
|
|
|
|
2003-10-31 18:51:16 +01:00
|
|
|
const Function *InvokeInst::getCalledFunction() const {
|
|
|
|
if (const Function *F = dyn_cast<Function>(Operands[0]))
|
|
|
|
return F;
|
|
|
|
if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
|
|
|
|
return cast<Function>(CPR->getValue());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Function *InvokeInst::getCalledFunction() {
|
|
|
|
if (Function *F = dyn_cast<Function>(Operands[0]))
|
|
|
|
return F;
|
|
|
|
if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
|
|
|
|
return cast<Function>(CPR->getValue());
|
|
|
|
return 0;
|
|
|
|
}
|
2003-10-31 19:38:06 +01:00
|
|
|
|
|
|
|
Function *CallSite::getCalledFunction() const {
|
|
|
|
Value *Callee = getCalledValue();
|
|
|
|
if (Function *F = dyn_cast<Function>(Callee))
|
|
|
|
return F;
|
|
|
|
if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
|
|
|
|
return cast<Function>(CPR->getValue());
|
|
|
|
return 0;
|
|
|
|
}
|