Move MSILModule and MSILWriter into the 'llvm' namespace, instead of the 'MSIL'
[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 is distributed under the University of Illinois Open Source
6 // 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/IntrinsicInst.h"
20 #include "llvm/Pass.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Analysis/FindUsedTypes.h"
24 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Support/FormattedStream.h"
26 #include "llvm/Support/GetElementPtrTypeIterator.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Support/Mangler.h"
30
31 namespace llvm {
32   extern Target TheMSILTarget;
33
34   class MSILModule : public ModulePass {
35     Module *ModulePtr;
36     const std::set<const Type *>*& UsedTypes;
37     const TargetData*& TD;
38
39   public:
40     static char ID;
41     MSILModule(const std::set<const Type *>*& _UsedTypes,
42                const TargetData*& _TD)
43       : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
44
45     void getAnalysisUsage(AnalysisUsage &AU) const {
46       AU.addRequired<FindUsedTypes>();
47       AU.addRequired<TargetData>();
48     }
49
50     virtual const char *getPassName() const {
51       return "MSIL backend definitions";
52     }
53
54     virtual bool runOnModule(Module &M);
55
56   };
57
58   class MSILWriter : public FunctionPass {
59     struct StaticInitializer {
60       const Constant* constant;
61       uint64_t offset;
62       
63       StaticInitializer()
64         : constant(0), offset(0) {}
65
66       StaticInitializer(const Constant* _constant, uint64_t _offset)
67         : constant(_constant), offset(_offset) {} 
68     };
69
70     uint64_t UniqID;
71
72     uint64_t getUniqID() {
73       return ++UniqID;
74     }
75
76   public:
77     formatted_raw_ostream &Out;
78     Module* ModulePtr;
79     const TargetData* TD;
80     Mangler* Mang;
81     LoopInfo *LInfo;
82     std::vector<StaticInitializer>* InitListPtr;
83     std::map<const GlobalVariable*,std::vector<StaticInitializer> >
84       StaticInitList;
85     const std::set<const Type *>* UsedTypes;
86     static char ID;
87     DenseMap<const Value*, unsigned> AnonValueNumbers;
88     unsigned NextAnonValueNumber;
89
90     MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
91          NextAnonValueNumber(0) {
92       UniqID = 0;
93     }
94
95     enum ValueType {
96       UndefVT,
97       GlobalVT,
98       InternalVT,
99       ArgumentVT,
100       LocalVT,
101       ConstVT,
102       ConstExprVT
103     };
104
105     bool isVariable(ValueType V) {
106       return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
107     }
108
109     bool isConstValue(ValueType V) {
110       return V==ConstVT || V==ConstExprVT;
111     }
112
113     virtual const char *getPassName() const { return "MSIL backend"; }
114
115     void getAnalysisUsage(AnalysisUsage &AU) const {
116       AU.addRequired<LoopInfo>();
117       AU.setPreservesAll();
118     }
119
120     bool runOnFunction(Function &F);
121
122     virtual bool doInitialization(Module &M);
123
124     virtual bool doFinalization(Module &M);
125
126     void printModuleStartup();
127
128     bool isZeroValue(const Value* V);
129
130     std::string getValueName(const Value* V);
131
132     std::string getLabelName(const Value* V);
133
134     std::string getLabelName(const std::string& Name);
135
136     std::string getConvModopt(unsigned CallingConvID);
137
138     std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
139
140     std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
141
142     std::string getFunctionTypeName(const Type* Ty);
143
144     std::string getPointerTypeName(const Type* Ty);
145
146     std::string getTypeName(const Type* Ty, bool isSigned = false,
147                             bool isNested = false);
148
149     ValueType getValueLocation(const Value* V);
150
151     std::string getTypePostfix(const Type* Ty, bool Expand,
152                                bool isSigned = false);
153
154     void printConvToPtr();
155
156     void printPtrLoad(uint64_t N);
157
158     void printValuePtrLoad(const Value* V);
159
160     void printConstLoad(const Constant* C);
161
162     void printValueLoad(const Value* V);
163
164     void printValueSave(const Value* V);
165
166     void printBinaryInstruction(const char* Name, const Value* Left,
167                                 const Value* Right);
168
169     void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
170
171     void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
172
173     void printBranchToBlock(const BasicBlock* CurrBB,
174                             const BasicBlock* TrueBB,
175                             const BasicBlock* FalseBB);
176
177     void printBranchInstruction(const BranchInst* Inst);
178
179     void printSelectInstruction(const Value* Cond, const Value* VTrue,
180                                 const Value* VFalse);
181
182     void printIndirectLoad(const Value* V);
183
184     void printIndirectSave(const Value* Ptr, const Value* Val);
185
186     void printIndirectSave(const Type* Ty);
187
188     void printCastInstruction(unsigned int Op, const Value* V,
189                               const Type* Ty, const Type* SrcTy=0);
190
191     void printGepInstruction(const Value* V, gep_type_iterator I,
192                              gep_type_iterator E);
193
194     std::string getCallSignature(const FunctionType* Ty,
195                                  const Instruction* Inst,
196                                  std::string Name);
197
198     void printFunctionCall(const Value* FnVal, const Instruction* Inst);
199
200     void printIntrinsicCall(const IntrinsicInst* Inst);
201
202     void printCallInstruction(const Instruction* Inst);
203
204     void printICmpInstruction(unsigned Predicate, const Value* Left,
205                               const Value* Right);
206
207     void printFCmpInstruction(unsigned Predicate, const Value* Left,
208                               const Value* Right);
209
210     void printInvokeInstruction(const InvokeInst* Inst);
211
212     void printSwitchInstruction(const SwitchInst* Inst);
213
214     void printVAArgInstruction(const VAArgInst* Inst);
215
216     void printAllocaInstruction(const AllocaInst* Inst);
217
218     void printInstruction(const Instruction* Inst);
219
220     void printLoop(const Loop* L);
221
222     void printBasicBlock(const BasicBlock* BB);
223     
224     void printLocalVariables(const Function& F);
225
226     void printFunctionBody(const Function& F);
227
228     void printConstantExpr(const ConstantExpr* CE);
229
230     void printStaticInitializerList();
231
232     void printFunction(const Function& F);
233
234     void printDeclarations(const TypeSymbolTable& ST);
235
236     unsigned int getBitWidth(const Type* Ty);
237
238     void printStaticConstant(const Constant* C, uint64_t& Offset);
239
240     void printStaticInitializer(const Constant* C, const std::string& Name);
241
242     void printVariableDefinition(const GlobalVariable* G);
243
244     void printGlobalVariables();
245
246     const char* getLibraryName(const Function* F);
247
248     const char* getLibraryName(const GlobalVariable* GV); 
249     
250     const char* getLibraryForSymbol(const StringRef &Name, bool isFunction,
251                                     unsigned CallingConv);
252
253     void printExternals();
254   };
255
256 }
257
258 #endif
259