Finegrainify namespacification
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes ----------------===//
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 Function & GlobalVariable classes for the VMCore
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Module.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/iOther.h"
18 #include "llvm/Intrinsics.h"
19 #include "Support/LeakDetector.h"
20 #include "SymbolTableListTraitsImpl.h"
21 using namespace llvm;
22
23 BasicBlock *ilist_traits<BasicBlock>::createNode() {
24   BasicBlock *Ret = new BasicBlock();
25   // This should not be garbage monitored.
26   LeakDetector::removeGarbageObject(Ret);
27   return Ret;
28 }
29
30 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
31   return F->getBasicBlockList();
32 }
33
34 Argument *ilist_traits<Argument>::createNode() {
35   Argument *Ret = new Argument(Type::IntTy);
36   // This should not be garbage monitored.
37   LeakDetector::removeGarbageObject(Ret);
38   return Ret;
39 }
40
41 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
42   return F->getArgumentList();
43 }
44
45 // Explicit instantiations of SymbolTableListTraits since some of the methods
46 // are not in the public header file...
47 template class SymbolTableListTraits<Argument, Function, Function>;
48 template class SymbolTableListTraits<BasicBlock, Function, Function>;
49
50 //===----------------------------------------------------------------------===//
51 // Argument Implementation
52 //===----------------------------------------------------------------------===//
53
54 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) 
55   : Value(Ty, Value::ArgumentVal, Name) {
56   Parent = 0;
57
58   // Make sure that we get added to a function
59   LeakDetector::addGarbageObject(this);
60
61   if (Par)
62     Par->getArgumentList().push_back(this);
63 }
64
65
66 // Specialize setName to take care of symbol table majik
67 void Argument::setName(const std::string &name, SymbolTable *ST) {
68   Function *P;
69   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
70          "Invalid symtab argument!");
71   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
72   Value::setName(name);
73   if (P && hasName()) P->getSymbolTable().insert(this);
74 }
75
76 void Argument::setParent(Function *parent) {
77   if (getParent())
78     LeakDetector::addGarbageObject(this);
79   Parent = parent;
80   if (getParent())
81     LeakDetector::removeGarbageObject(this);
82 }
83
84
85 //===----------------------------------------------------------------------===//
86 // Function Implementation
87 //===----------------------------------------------------------------------===//
88
89 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
90                    const std::string &name, Module *ParentModule)
91   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
92   BasicBlocks.setItemParent(this);
93   BasicBlocks.setParent(this);
94   ArgumentList.setItemParent(this);
95   ArgumentList.setParent(this);
96   SymTab = new SymbolTable();
97
98   // Create the arguments vector, all arguments start out unnamed.
99   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
100     assert(Ty->getParamType(i) != Type::VoidTy &&
101            "Cannot have void typed arguments!");
102     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
103   }
104
105   // Make sure that we get added to a function
106   LeakDetector::addGarbageObject(this);
107
108   if (ParentModule)
109     ParentModule->getFunctionList().push_back(this);
110 }
111
112 Function::~Function() {
113   dropAllReferences();    // After this it is safe to delete instructions.
114
115   // Delete all of the method arguments and unlink from symbol table...
116   ArgumentList.clear();
117   ArgumentList.setParent(0);
118   delete SymTab;
119 }
120
121 // Specialize setName to take care of symbol table majik
122 void Function::setName(const std::string &name, SymbolTable *ST) {
123   Module *P;
124   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
125          "Invalid symtab argument!");
126   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
127   Value::setName(name);
128   if (P && getName() != "") P->getSymbolTable().insert(this);
129 }
130
131 void Function::setParent(Module *parent) {
132   if (getParent())
133     LeakDetector::addGarbageObject(this);
134   Parent = parent;
135   if (getParent())
136     LeakDetector::removeGarbageObject(this);
137 }
138
139 const FunctionType *Function::getFunctionType() const {
140   return cast<FunctionType>(getType()->getElementType());
141 }
142
143 const Type *Function::getReturnType() const { 
144   return getFunctionType()->getReturnType();
145 }
146
147 // dropAllReferences() - This function causes all the subinstructions to "let
148 // go" of all references that they are maintaining.  This allows one to
149 // 'delete' a whole class at a time, even though there may be circular
150 // references... first all references are dropped, and all use counts go to
151 // zero.  Then everything is deleted for real.  Note that no operations are
152 // valid on an object that has "dropped all references", except operator 
153 // delete.
154 //
155 void Function::dropAllReferences() {
156   for (iterator I = begin(), E = end(); I != E; ++I)
157     I->dropAllReferences();
158   BasicBlocks.clear();    // Delete all basic blocks...
159 }
160
161 /// getIntrinsicID - This method returns the ID number of the specified
162 /// function, or Intrinsic::not_intrinsic if the function is not an
163 /// intrinsic, or if the pointer is null.  This value is always defined to be
164 /// zero to allow easy checking for whether a function is intrinsic or not.  The
165 /// particular intrinsic functions which correspond to this value are defined in
166 /// llvm/Intrinsics.h.
167 ///
168 unsigned Function::getIntrinsicID() const {
169   if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
170       getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
171     return 0;  // All intrinsics start with 'llvm.'
172
173   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
174   
175   // a table of all Alpha intrinsic functions
176   struct {
177    std::string name;  // The name of the intrinsic 
178    unsigned id;       // Its ID number
179   } alpha_intrinsics[] = {
180      { "llvm.alpha.ctlz",      Intrinsic::alpha_ctlz },
181      { "llvm.alpha.cttz",      Intrinsic::alpha_cttz },
182      { "llvm.alpha.ctpop",     Intrinsic::alpha_ctpop },
183      { "llvm.alpha.umulh",     Intrinsic::alpha_umulh },
184      { "llvm.alpha.vecop",     Intrinsic::alpha_vecop },
185      { "llvm.alpha.pup",       Intrinsic::alpha_pup },
186      { "llvm.alpha.bytezap",   Intrinsic::alpha_bytezap },
187      { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
188      { "llvm.alpha.dfp_bop",   Intrinsic::alpha_dfpbop }, 
189      { "llvm.alpha.dfp_uop",   Intrinsic::alpha_dfpuop },
190      { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
191      { "llvm.alpha.uqtodfp",   Intrinsic::alpha_uqtodfp },
192      { "llvm.alpha.uqtosfp",   Intrinsic::alpha_uqtosfp },
193      { "llvm.alpha.dfptosq",   Intrinsic::alpha_dfptosq },
194      { "llvm.alpha.sfptosq",   Intrinsic::alpha_sfptosq },
195   };
196   const unsigned num_alpha_intrinsics = 
197                  sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
198
199   switch (getName()[5]) {
200   case 'a':
201     if (getName().size() > 11 &&
202         std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
203       for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
204         if (getName() == alpha_intrinsics[i].name)
205           return alpha_intrinsics[i].id;
206     break;
207   case 'l':
208     if (getName() == "llvm.longjmp")  return Intrinsic::longjmp;
209     break;
210   case 's':
211     if (getName() == "llvm.setjmp")     return Intrinsic::setjmp;
212     if (getName() == "llvm.sigsetjmp")  return Intrinsic::sigsetjmp;
213     if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
214     break;
215   case 'v':
216     if (getName() == "llvm.va_copy")  return Intrinsic::va_copy;
217     if (getName() == "llvm.va_end")   return Intrinsic::va_end;
218     if (getName() == "llvm.va_start") return Intrinsic::va_start;
219     break;
220   }
221   // The "llvm." namespace is reserved!
222   assert(0 && "Unknown LLVM intrinsic function!");
223   return 0;
224 }
225
226
227 //===----------------------------------------------------------------------===//
228 // GlobalVariable Implementation
229 //===----------------------------------------------------------------------===//
230
231 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
232                                Constant *Initializer,
233                                const std::string &Name, Module *ParentModule)
234   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
235     isConstantGlobal(constant) {
236   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
237
238   LeakDetector::addGarbageObject(this);
239
240   if (ParentModule)
241     ParentModule->getGlobalList().push_back(this);
242 }
243
244 void GlobalVariable::setParent(Module *parent) {
245   if (getParent())
246     LeakDetector::addGarbageObject(this);
247   Parent = parent;
248   if (getParent())
249     LeakDetector::removeGarbageObject(this);
250 }
251
252 // Specialize setName to take care of symbol table majik
253 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
254   Module *P;
255   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
256          "Invalid symtab argument!");
257   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
258   Value::setName(name);
259   if (P && getName() != "") P->getSymbolTable().insert(this);
260 }