Let the new backend begin!
[oota-llvm.git] / lib / Target / MSIL / MSILWriter.h
1 //===-- MSILWriter.h - TargetMachine for the MSIL ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Roman Samoilov and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the MSILWriter that is used by the MSIL.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef MSILWRITER_H
14 #define MSILWRITER_H
15
16 #include "llvm/Constants.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Pass.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Analysis/FindUsedTypes.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetMachineRegistry.h"
27 #include "llvm/Support/Mangler.h"
28 #include <algorithm>
29 #include <ios>
30 using namespace llvm;
31
32 namespace {
33
34   class MSILModule : public ModulePass {
35     Module *ModulePtr;
36     const std::set<const Type *>*& UsedTypes;
37     const TargetData*& TD;
38
39   public:
40     MSILModule(const std::set<const Type *>*& _UsedTypes,
41                const TargetData*& _TD)
42       : UsedTypes(_UsedTypes), TD(_TD) {}
43
44     void getAnalysisUsage(AnalysisUsage &AU) const {
45       AU.addRequired<FindUsedTypes>();
46       AU.addRequired<TargetData>();
47     }
48
49     virtual const char *getPassName() const {
50       return "MSIL backend definitions";
51     }
52
53     virtual bool runOnModule(Module &M);
54
55   };
56
57   class MSILWriter  : public FunctionPass {
58     struct StaticInitializer {
59       const Constant* constant;
60       uint64_t offset;
61       
62       StaticInitializer()
63         : constant(0), offset(0) {}
64
65       StaticInitializer(const Constant* _constant, uint64_t _offset)
66         : constant(_constant), offset(_offset) {} 
67     };
68
69     uint64_t UniqID;    
70
71     uint64_t getUniqID() {
72       return ++UniqID;
73     }
74
75   public:
76     std::ostream &Out;
77     Module* ModulePtr;
78     const TargetData* TD;
79     Mangler* Mang;
80     LoopInfo *LInfo;
81     std::vector<StaticInitializer>* InitListPtr;
82     std::map<const GlobalVariable*,std::vector<StaticInitializer> >
83       StaticInitList;
84     const std::set<const Type *>* UsedTypes;
85
86     MSILWriter(std::ostream &o) : Out(o) {
87       UniqID = 0;
88     }
89
90     enum ValueType {
91       UndefVT,
92       GlobalVT,
93       InternalVT,
94       ArgumentVT,
95       LocalVT,
96       ConstVT,
97       ConstExprVT
98     };
99
100     bool isVariable(ValueType V) {
101       return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
102     }
103
104     bool isConstValue(ValueType V) {
105       return V==ConstVT || V==ConstExprVT;
106     }
107
108     virtual const char *getPassName() const { return "MSIL backend"; }
109
110     void getAnalysisUsage(AnalysisUsage &AU) const {
111       AU.addRequired<LoopInfo>();
112       AU.setPreservesAll();
113     }
114
115     bool runOnFunction(Function &F);
116
117     virtual bool doInitialization(Module &M);
118
119     virtual bool doFinalization(Module &M);
120
121     bool isZeroValue(const Value* V);
122
123     std::string getValueName(const Value* V);
124
125     std::string getLabelName(const Value* V);
126
127     std::string getLabelName(const std::string& Name);
128
129     std::string getConvModopt(unsigned CallingConvID);
130
131     std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
132
133     std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
134
135     std::string getFunctionTypeName(const Type* Ty);
136
137     std::string getPointerTypeName(const Type* Ty);
138
139     std::string getTypeName(const Type* Ty, bool isSigned = false);
140
141     ValueType getValueLocation(const Value* V);
142
143     std::string getTypePostfix(const Type* Ty, bool Expand,
144                                bool isSigned = false);
145
146     void printPtrLoad(uint64_t N);
147
148     void printConstLoad(const Constant* C);
149
150     void printValueLoad(const Value* V);
151
152     void printValueSave(const Value* V);
153
154     void printBinaryInstruction(const char* Name, const Value* Left,
155                                 const Value* Right);
156
157     void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
158
159     void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
160
161     void printBranchToBlock(const BasicBlock* CurrBB,
162                             const BasicBlock* TrueBB,
163                             const BasicBlock* FalseBB);
164
165     void printBranchInstruction(const BranchInst* Inst);
166
167     void printSelectInstruction(const Value* Cond, const Value* VTrue,
168                                 const Value* VFalse);
169
170     void printIndirectLoad(const Value* V);
171
172     void printStoreInstruction(const Instruction* Inst);
173
174     void printCastInstruction(unsigned int Op, const Value* V,
175                               const Type* Ty);
176
177     void printGepInstruction(const Value* V, gep_type_iterator I,
178                              gep_type_iterator E);
179
180     std::string getCallSignature(const FunctionType* Ty,
181                                  const Instruction* Inst,
182                                  std::string Name);
183
184     void printFunctionCall(const Value* FnVal, const Instruction* Inst);
185
186     void printCallInstruction(const Instruction* Inst);
187
188     void printICmpInstruction(unsigned Predicate, const Value* Left,
189                               const Value* Right);
190
191     void printFCmpInstruction(unsigned Predicate, const Value* Left,
192                               const Value* Right);
193
194     void printInvokeInstruction(const InvokeInst* Inst);
195
196     void printSwitchInstruction(const SwitchInst* Inst);
197
198     void printInstruction(const Instruction* Inst);
199
200     void printLoop(const Loop* L);
201
202     void printBasicBlock(const BasicBlock* BB);
203     
204     void printLocalVariables(const Function& F);
205
206     void printFunctionBody(const Function& F);
207
208     void printConstantExpr(const ConstantExpr* CE);
209
210     void printStaticInitializerList();
211
212     void printFunction(const Function& F);
213
214     void printDeclarations(const TypeSymbolTable& ST);
215
216     unsigned int getBitWidth(const Type* Ty);
217
218     void printStaticConstant(const Constant* C, uint64_t& Offset);
219
220     void printStaticInitializer(const Constant* C, const std::string& Name);
221
222     void printVariableDefinition(const GlobalVariable* G);
223
224     void printGlobalVariables();
225
226     void printExternals();
227   };
228 }
229
230 #endif