blob: c64ceeeff5945648c794ac42b09ebe19247f5a1a [file] [log] [blame]
Anton Korobeynikov099883f2007-03-21 21:38:25 +00001//===-- MSILWriter.h - TargetMachine for the MSIL ---------------*- C++ -*-===//
2//
Anton Korobeynikovbed29462007-04-16 18:10:23 +00003// The LLVM Compiler Infrastructure
Anton Korobeynikov099883f2007-03-21 21:38:25 +00004//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anton Korobeynikov099883f2007-03-21 21:38:25 +00007//
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"
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000019#include "llvm/IntrinsicInst.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000020#include "llvm/Pass.h"
21#include "llvm/PassManager.h"
22#include "llvm/Analysis/FindUsedTypes.h"
23#include "llvm/Analysis/LoopInfo.h"
24#include "llvm/Support/GetElementPtrTypeIterator.h"
Owen Andersoncb371882008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetMachineRegistry.h"
29#include "llvm/Support/Mangler.h"
30#include <algorithm>
31#include <ios>
32using namespace llvm;
33
34namespace {
35
36 class MSILModule : public ModulePass {
37 Module *ModulePtr;
38 const std::set<const Type *>*& UsedTypes;
39 const TargetData*& TD;
40
41 public:
Devang Patel19974732007-05-03 01:11:54 +000042 static char ID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000043 MSILModule(const std::set<const Type *>*& _UsedTypes,
44 const TargetData*& _TD)
Dan Gohmanae73dc12008-09-04 17:05:41 +000045 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
Anton Korobeynikov099883f2007-03-21 21:38:25 +000046
47 void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<FindUsedTypes>();
49 AU.addRequired<TargetData>();
50 }
51
52 virtual const char *getPassName() const {
53 return "MSIL backend definitions";
54 }
55
56 virtual bool runOnModule(Module &M);
57
58 };
59
60 class MSILWriter : public FunctionPass {
61 struct StaticInitializer {
62 const Constant* constant;
63 uint64_t offset;
64
65 StaticInitializer()
66 : constant(0), offset(0) {}
67
68 StaticInitializer(const Constant* _constant, uint64_t _offset)
69 : constant(_constant), offset(_offset) {}
70 };
71
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000072 uint64_t UniqID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000073
74 uint64_t getUniqID() {
75 return ++UniqID;
76 }
77
78 public:
Owen Andersoncb371882008-08-21 00:14:44 +000079 raw_ostream &Out;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000080 Module* ModulePtr;
81 const TargetData* TD;
82 Mangler* Mang;
83 LoopInfo *LInfo;
84 std::vector<StaticInitializer>* InitListPtr;
85 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
86 StaticInitList;
87 const std::set<const Type *>* UsedTypes;
Devang Patel19974732007-05-03 01:11:54 +000088 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000089 MSILWriter(raw_ostream &o) : FunctionPass(&ID), Out(o) {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000090 UniqID = 0;
91 }
92
93 enum ValueType {
94 UndefVT,
95 GlobalVT,
96 InternalVT,
97 ArgumentVT,
98 LocalVT,
99 ConstVT,
100 ConstExprVT
101 };
102
103 bool isVariable(ValueType V) {
104 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
105 }
106
107 bool isConstValue(ValueType V) {
108 return V==ConstVT || V==ConstExprVT;
109 }
110
111 virtual const char *getPassName() const { return "MSIL backend"; }
112
113 void getAnalysisUsage(AnalysisUsage &AU) const {
114 AU.addRequired<LoopInfo>();
115 AU.setPreservesAll();
116 }
117
118 bool runOnFunction(Function &F);
119
120 virtual bool doInitialization(Module &M);
121
122 virtual bool doFinalization(Module &M);
123
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000124 void printModuleStartup();
125
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000126 bool isZeroValue(const Value* V);
127
128 std::string getValueName(const Value* V);
129
130 std::string getLabelName(const Value* V);
131
132 std::string getLabelName(const std::string& Name);
133
134 std::string getConvModopt(unsigned CallingConvID);
135
136 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
137
138 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
139
140 std::string getFunctionTypeName(const Type* Ty);
141
142 std::string getPointerTypeName(const Type* Ty);
143
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000144 std::string getTypeName(const Type* Ty, bool isSigned = false,
145 bool isNested = false);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000146
147 ValueType getValueLocation(const Value* V);
148
149 std::string getTypePostfix(const Type* Ty, bool Expand,
150 bool isSigned = false);
151
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000152 void printConvToPtr();
153
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000154 void printPtrLoad(uint64_t N);
155
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000156 void printValuePtrLoad(const Value* V);
157
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000158 void printConstLoad(const Constant* C);
159
160 void printValueLoad(const Value* V);
161
162 void printValueSave(const Value* V);
163
164 void printBinaryInstruction(const char* Name, const Value* Left,
165 const Value* Right);
166
167 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
168
169 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
170
171 void printBranchToBlock(const BasicBlock* CurrBB,
172 const BasicBlock* TrueBB,
173 const BasicBlock* FalseBB);
174
175 void printBranchInstruction(const BranchInst* Inst);
176
177 void printSelectInstruction(const Value* Cond, const Value* VTrue,
178 const Value* VFalse);
179
180 void printIndirectLoad(const Value* V);
181
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000182 void printIndirectSave(const Value* Ptr, const Value* Val);
183
184 void printIndirectSave(const Type* Ty);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000185
186 void printCastInstruction(unsigned int Op, const Value* V,
187 const Type* Ty);
188
189 void printGepInstruction(const Value* V, gep_type_iterator I,
190 gep_type_iterator E);
191
192 std::string getCallSignature(const FunctionType* Ty,
193 const Instruction* Inst,
194 std::string Name);
195
196 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
197
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000198 void printIntrinsicCall(const IntrinsicInst* Inst);
199
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000200 void printCallInstruction(const Instruction* Inst);
201
202 void printICmpInstruction(unsigned Predicate, const Value* Left,
203 const Value* Right);
204
205 void printFCmpInstruction(unsigned Predicate, const Value* Left,
206 const Value* Right);
207
208 void printInvokeInstruction(const InvokeInst* Inst);
209
210 void printSwitchInstruction(const SwitchInst* Inst);
211
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000212 void printVAArgInstruction(const VAArgInst* Inst);
213
214 void printAllocaInstruction(const AllocaInst* Inst);
215
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000216 void printInstruction(const Instruction* Inst);
217
218 void printLoop(const Loop* L);
219
220 void printBasicBlock(const BasicBlock* BB);
221
222 void printLocalVariables(const Function& F);
223
224 void printFunctionBody(const Function& F);
225
226 void printConstantExpr(const ConstantExpr* CE);
227
228 void printStaticInitializerList();
229
230 void printFunction(const Function& F);
231
232 void printDeclarations(const TypeSymbolTable& ST);
233
234 unsigned int getBitWidth(const Type* Ty);
235
236 void printStaticConstant(const Constant* C, uint64_t& Offset);
237
238 void printStaticInitializer(const Constant* C, const std::string& Name);
239
240 void printVariableDefinition(const GlobalVariable* G);
241
242 void printGlobalVariables();
243
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000244 const char* getLibraryName(const Function* F);
245
246 const char* getLibraryName(const GlobalVariable* GV);
247
248 const char* getLibraryForSymbol(const char* Name, bool isFunction,
249 unsigned CallingConv);
250
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000251 void printExternals();
252 };
253}
254
255#endif
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000256