Added MachineCodeForMethod object in class Method.
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=//
2 //
3 // This file implements the Method & GlobalVariable classes for the VMCore
4 // library.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/ValueHolderImpl.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/Module.h"
12 #include "llvm/Method.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/iOther.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17
18 //===----------------------------------------------------------------------===//
19 // Method Implementation
20 //===----------------------------------------------------------------------===//
21
22
23 // Instantiate Templates - This ugliness is the price we have to pay
24 // for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
25 //
26 template class ValueHolder<MethodArgument, Method, Method>;
27 template class ValueHolder<BasicBlock    , Method, Method>;
28
29 Method::Method(const MethodType *Ty, const string &name) 
30   : GlobalValue(PointerType::get(Ty), Value::MethodVal, name),
31     SymTabValue(this), BasicBlocks(this), ArgumentList(this, this),
32     machineCode(new MachineCodeForMethod(this)) {
33   assert(::isa<MethodType>(Ty) && "Method signature must be of method type!");
34 }
35
36 Method::~Method() {
37   dropAllReferences();    // After this it is safe to delete instructions.
38
39   // TODO: Should remove from the end, not the beginning of vector!
40   iterator BI = begin();
41   while ((BI = begin()) != end())
42     delete BasicBlocks.remove(BI);
43
44   // Delete all of the method arguments and unlink from symbol table...
45   ArgumentList.delete_all();
46   ArgumentList.setParent(0);
47 }
48
49 // Specialize setName to take care of symbol table majik
50 void Method::setName(const string &name, SymbolTable *ST) {
51   Module *P;
52   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
53          "Invalid symtab argument!");
54   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
55   Value::setName(name);
56   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
57 }
58
59 void Method::setParent(Module *parent) {
60   Parent = parent;
61
62   // Relink symbol tables together...
63   setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
64 }
65
66 const MethodType *Method::getMethodType() const {
67   return cast<MethodType>(cast<PointerType>(getType())->getValueType());
68 }
69
70 const Type *Method::getReturnType() const { 
71   return getMethodType()->getReturnType();
72 }
73
74 // dropAllReferences() - This function causes all the subinstructions to "let
75 // go" of all references that they are maintaining.  This allows one to
76 // 'delete' a whole class at a time, even though there may be circular
77 // references... first all references are dropped, and all use counts go to
78 // zero.  Then everything is delete'd for real.  Note that no operations are
79 // valid on an object that has "dropped all references", except operator 
80 // delete.
81 //
82 void Method::dropAllReferences() {
83   for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
84 }
85
86 //===----------------------------------------------------------------------===//
87 // GlobalVariable Implementation
88 //===----------------------------------------------------------------------===//
89
90 GlobalVariable::GlobalVariable(const Type *Ty, bool isConstant,
91                                ConstPoolVal *Initializer = 0, 
92                                const string &Name = "")
93   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Name),
94     Constant(isConstant) {
95   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
96 }
97
98 // Specialize setName to take care of symbol table majik
99 void GlobalVariable::setName(const string &name, SymbolTable *ST) {
100   Module *P;
101   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
102          "Invalid symtab argument!");
103   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
104   Value::setName(name);
105   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
106 }