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