2003-10-13 11:32:08 +08:00
|
|
|
//===-- Function.cpp - Implement the Global object classes ----------------===//
|
2003-10-21 03:43:21 +08: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-07 04:29:01 +08:00
|
|
|
//
|
2002-03-27 02:01:55 +08:00
|
|
|
// This file implements the Function & GlobalVariable classes for the VMCore
|
2001-09-10 15:58:01 +08:00
|
|
|
// library.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-09-07 04:46:32 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/iOther.h"
|
2003-05-08 11:47:33 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2002-09-09 02:59:35 +08:00
|
|
|
#include "Support/LeakDetector.h"
|
2002-06-26 00:13:24 +08:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
2003-11-22 04:23:48 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2002-09-07 05:33:15 +08:00
|
|
|
BasicBlock *ilist_traits<BasicBlock>::createNode() {
|
2002-09-09 02:59:35 +08:00
|
|
|
BasicBlock *Ret = new BasicBlock();
|
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-09-07 05:33:15 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
|
|
|
|
return F->getBasicBlockList();
|
|
|
|
}
|
|
|
|
|
|
|
|
Argument *ilist_traits<Argument>::createNode() {
|
2002-09-09 02:59:35 +08:00
|
|
|
Argument *Ret = new Argument(Type::IntTy);
|
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-26 00:13:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
|
|
|
|
return F->getArgumentList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
|
|
|
// are not in the public header file...
|
2003-11-05 13:15:42 +08:00
|
|
|
template class SymbolTableListTraits<Argument, Function, Function>;
|
|
|
|
template class SymbolTableListTraits<BasicBlock, Function, Function>;
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2002-04-10 03:39:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Argument Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-09-17 09:17:57 +08:00
|
|
|
Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
|
2002-09-07 05:33:15 +08:00
|
|
|
: Value(Ty, Value::ArgumentVal, Name) {
|
|
|
|
Parent = 0;
|
2002-09-09 02:59:35 +08:00
|
|
|
|
|
|
|
// Make sure that we get added to a function
|
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-07 05:33:15 +08:00
|
|
|
if (Par)
|
|
|
|
Par->getArgumentList().push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-10 03:39:35 +08:00
|
|
|
// Specialize setName to take care of symbol table majik
|
|
|
|
void Argument::setName(const std::string &name, SymbolTable *ST) {
|
|
|
|
Function *P;
|
2002-11-21 02:36:02 +08:00
|
|
|
assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
|
2002-04-10 03:39:35 +08:00
|
|
|
"Invalid symtab argument!");
|
2002-11-21 02:36:02 +08:00
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
|
2002-04-10 03:39:35 +08:00
|
|
|
Value::setName(name);
|
2002-11-21 02:36:02 +08:00
|
|
|
if (P && hasName()) P->getSymbolTable().insert(this);
|
2002-04-10 03:39:35 +08:00
|
|
|
}
|
|
|
|
|
2002-09-07 05:33:15 +08:00
|
|
|
void Argument::setParent(Function *parent) {
|
2002-09-09 02:59:35 +08:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2002-09-07 05:33:15 +08:00
|
|
|
Parent = parent;
|
2002-09-09 02:59:35 +08:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2002-09-07 05:33:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-10 15:58:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-27 02:01:55 +08:00
|
|
|
// Function Implementation
|
2001-09-10 15:58:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-04-17 04:28:45 +08:00
|
|
|
Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
|
2002-09-07 04:46:32 +08:00
|
|
|
const std::string &name, Module *ParentModule)
|
2003-04-17 04:28:45 +08:00
|
|
|
: GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlocks.setItemParent(this);
|
|
|
|
BasicBlocks.setParent(this);
|
|
|
|
ArgumentList.setItemParent(this);
|
|
|
|
ArgumentList.setParent(this);
|
2002-11-21 02:07:48 +08:00
|
|
|
SymTab = new SymbolTable();
|
2002-09-07 04:46:32 +08:00
|
|
|
|
2003-11-22 06:32:23 +08:00
|
|
|
assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
|
|
|
|
&& "LLVM functions cannot return aggregate values!");
|
|
|
|
|
2002-10-14 04:57:00 +08:00
|
|
|
// Create the arguments vector, all arguments start out unnamed.
|
|
|
|
for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
|
|
|
|
assert(Ty->getParamType(i) != Type::VoidTy &&
|
|
|
|
"Cannot have void typed arguments!");
|
|
|
|
ArgumentList.push_back(new Argument(Ty->getParamType(i)));
|
|
|
|
}
|
|
|
|
|
2002-09-09 02:59:35 +08:00
|
|
|
// Make sure that we get added to a function
|
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-07 04:46:32 +08:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getFunctionList().push_back(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2002-03-24 06:51:58 +08:00
|
|
|
Function::~Function() {
|
2001-06-07 04:29:01 +08:00
|
|
|
dropAllReferences(); // After this it is safe to delete instructions.
|
|
|
|
|
|
|
|
// Delete all of the method arguments and unlink from symbol table...
|
2002-06-26 00:13:24 +08:00
|
|
|
ArgumentList.clear();
|
2001-06-07 04:29:01 +08:00
|
|
|
ArgumentList.setParent(0);
|
2002-04-28 12:51:51 +08:00
|
|
|
delete SymTab;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-03-24 06:51:58 +08:00
|
|
|
void Function::setName(const std::string &name, SymbolTable *ST) {
|
2001-06-07 04:29:01 +08:00
|
|
|
Module *P;
|
2002-11-21 02:36:02 +08:00
|
|
|
assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
|
2001-09-08 00:47:18 +08:00
|
|
|
"Invalid symtab argument!");
|
2002-11-21 02:36:02 +08:00
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
Value::setName(name);
|
2004-01-11 05:42:24 +08:00
|
|
|
if (P && hasName()) P->getSymbolTable().insert(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2002-03-24 06:51:58 +08:00
|
|
|
void Function::setParent(Module *parent) {
|
2002-09-09 02:59:35 +08:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
Parent = parent;
|
2002-09-09 02:59:35 +08:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2002-03-29 11:44:36 +08:00
|
|
|
const FunctionType *Function::getFunctionType() const {
|
|
|
|
return cast<FunctionType>(getType()->getElementType());
|
2001-10-03 22:53:21 +08:00
|
|
|
}
|
|
|
|
|
2002-03-24 06:51:58 +08:00
|
|
|
const Type *Function::getReturnType() const {
|
2002-03-29 11:44:36 +08:00
|
|
|
return getFunctionType()->getReturnType();
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// dropAllReferences() - This function causes all the subinstructions to "let
|
|
|
|
// go" of all references that they are maintaining. This allows one to
|
|
|
|
// 'delete' a whole class at a time, even though there may be circular
|
|
|
|
// references... first all references are dropped, and all use counts go to
|
2003-10-11 01:54:14 +08:00
|
|
|
// zero. Then everything is deleted for real. Note that no operations are
|
2001-06-07 04:29:01 +08:00
|
|
|
// valid on an object that has "dropped all references", except operator
|
|
|
|
// delete.
|
|
|
|
//
|
2002-03-24 06:51:58 +08:00
|
|
|
void Function::dropAllReferences() {
|
2002-06-26 00:13:24 +08:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2003-09-17 12:58:59 +08:00
|
|
|
BasicBlocks.clear(); // Delete all basic blocks...
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2001-09-10 15:58:01 +08:00
|
|
|
|
2003-05-08 11:47:33 +08:00
|
|
|
/// getIntrinsicID - This method returns the ID number of the specified
|
2003-11-12 06:41:34 +08:00
|
|
|
/// function, or Intrinsic::not_intrinsic if the function is not an
|
2003-10-11 01:54:14 +08:00
|
|
|
/// intrinsic, or if the pointer is null. This value is always defined to be
|
2003-05-08 11:47:33 +08:00
|
|
|
/// zero to allow easy checking for whether a function is intrinsic or not. The
|
|
|
|
/// particular intrinsic functions which correspond to this value are defined in
|
|
|
|
/// llvm/Intrinsics.h.
|
|
|
|
///
|
|
|
|
unsigned Function::getIntrinsicID() const {
|
2003-09-20 03:31:41 +08:00
|
|
|
if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
|
2003-05-08 11:47:33 +08:00
|
|
|
getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
|
|
|
|
return 0; // All intrinsics start with 'llvm.'
|
2003-09-20 03:31:41 +08:00
|
|
|
|
|
|
|
assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
|
2003-05-08 11:47:33 +08:00
|
|
|
|
2003-08-07 04:08:25 +08:00
|
|
|
// a table of all Alpha intrinsic functions
|
|
|
|
struct {
|
|
|
|
std::string name; // The name of the intrinsic
|
|
|
|
unsigned id; // Its ID number
|
|
|
|
} alpha_intrinsics[] = {
|
2003-11-12 06:41:34 +08:00
|
|
|
{ "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
|
|
|
|
{ "llvm.alpha.cttz", Intrinsic::alpha_cttz },
|
|
|
|
{ "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
|
|
|
|
{ "llvm.alpha.umulh", Intrinsic::alpha_umulh },
|
|
|
|
{ "llvm.alpha.vecop", Intrinsic::alpha_vecop },
|
|
|
|
{ "llvm.alpha.pup", Intrinsic::alpha_pup },
|
|
|
|
{ "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
|
|
|
|
{ "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
|
|
|
|
{ "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
|
|
|
|
{ "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
|
|
|
|
{ "llvm.alpha.unordered", Intrinsic::alpha_unordered },
|
|
|
|
{ "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
|
|
|
|
{ "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
|
|
|
|
{ "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
|
|
|
|
{ "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
|
2003-08-07 04:08:25 +08:00
|
|
|
};
|
|
|
|
const unsigned num_alpha_intrinsics =
|
|
|
|
sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
|
|
|
|
|
2003-05-08 11:47:33 +08:00
|
|
|
switch (getName()[5]) {
|
2003-07-29 05:20:57 +08:00
|
|
|
case 'a':
|
2003-08-24 13:30:29 +08:00
|
|
|
if (getName().size() > 11 &&
|
|
|
|
std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
|
|
|
|
for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
|
|
|
|
if (getName() == alpha_intrinsics[i].name)
|
|
|
|
return alpha_intrinsics[i].id;
|
|
|
|
break;
|
2004-01-05 13:36:30 +08:00
|
|
|
case 'd':
|
|
|
|
if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
|
|
|
|
if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
|
|
|
|
if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
|
|
|
|
if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
|
2004-01-06 13:33:02 +08:00
|
|
|
if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
|
2004-01-05 13:36:30 +08:00
|
|
|
break;
|
2004-02-14 10:47:17 +08:00
|
|
|
case 'f':
|
|
|
|
if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
|
|
|
|
break;
|
2004-05-24 05:16:51 +08:00
|
|
|
case 'g':
|
|
|
|
if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
|
|
|
|
if (getName() == "llvm.gcread") return Intrinsic::gcread;
|
|
|
|
if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
|
|
|
|
break;
|
2003-05-18 06:26:33 +08:00
|
|
|
case 'l':
|
2003-11-12 06:41:34 +08:00
|
|
|
if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
|
2003-05-18 06:26:33 +08:00
|
|
|
break;
|
2004-02-13 01:01:09 +08:00
|
|
|
case 'm':
|
|
|
|
if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
|
2004-02-13 02:11:20 +08:00
|
|
|
if (getName() == "llvm.memmove") return Intrinsic::memmove;
|
2004-02-14 10:47:17 +08:00
|
|
|
if (getName() == "llvm.memset") return Intrinsic::memset;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
|
2004-04-09 04:27:38 +08:00
|
|
|
if (getName() == "llvm.readport") return Intrinsic::readport;
|
2004-04-14 21:46:52 +08:00
|
|
|
if (getName() == "llvm.readio") return Intrinsic::readio;
|
2004-02-13 01:01:09 +08:00
|
|
|
break;
|
2003-05-18 06:26:33 +08:00
|
|
|
case 's':
|
2003-11-12 06:41:34 +08:00
|
|
|
if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
|
|
|
|
if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
|
|
|
|
if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
|
2003-05-18 06:26:33 +08:00
|
|
|
break;
|
2003-05-08 11:47:33 +08:00
|
|
|
case 'v':
|
2004-03-13 08:24:00 +08:00
|
|
|
if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
|
|
|
|
if (getName() == "llvm.va_end") return Intrinsic::vaend;
|
|
|
|
if (getName() == "llvm.va_start") return Intrinsic::vastart;
|
2004-04-09 04:27:38 +08:00
|
|
|
case 'w':
|
|
|
|
if (getName() == "llvm.writeport") return Intrinsic::writeport;
|
2004-04-14 21:46:52 +08:00
|
|
|
if (getName() == "llvm.writeio") return Intrinsic::writeio;
|
2003-05-08 11:47:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// The "llvm." namespace is reserved!
|
|
|
|
assert(0 && "Unknown LLVM intrinsic function!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-10 15:58:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GlobalVariable Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-04-17 04:28:45 +08:00
|
|
|
GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
|
2002-07-25 06:08:53 +08:00
|
|
|
Constant *Initializer,
|
2002-09-07 05:33:15 +08:00
|
|
|
const std::string &Name, Module *ParentModule)
|
2003-04-17 04:28:45 +08:00
|
|
|
: GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
|
2001-12-04 06:26:30 +08:00
|
|
|
isConstantGlobal(constant) {
|
2001-09-18 12:01:05 +08:00
|
|
|
if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
|
2002-09-07 05:33:15 +08:00
|
|
|
|
2002-09-09 02:59:35 +08:00
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-07 05:33:15 +08:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getGlobalList().push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalVariable::setParent(Module *parent) {
|
2002-09-09 02:59:35 +08:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2002-09-07 05:33:15 +08:00
|
|
|
Parent = parent;
|
2002-09-09 02:59:35 +08:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-09-10 15:58:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-01-21 06:54:45 +08:00
|
|
|
void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
|
2001-09-10 15:58:01 +08:00
|
|
|
Module *P;
|
2002-11-21 02:36:02 +08:00
|
|
|
assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
|
2001-09-10 15:58:01 +08:00
|
|
|
"Invalid symtab argument!");
|
2002-11-21 02:36:02 +08:00
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
|
2001-09-10 15:58:01 +08:00
|
|
|
Value::setName(name);
|
2004-01-11 05:40:29 +08:00
|
|
|
if (P && hasName()) P->getSymbolTable().insert(this);
|
2001-09-10 15:58:01 +08:00
|
|
|
}
|