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