blob: ea0dfad0e469165dc618f8f975d33a084d149eb3 [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"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000030#include <ios>
31using namespace llvm;
32
33namespace {
34
35 class MSILModule : public ModulePass {
36 Module *ModulePtr;
37 const std::set<const Type *>*& UsedTypes;
38 const TargetData*& TD;
39
40 public:
Devang Patel19974732007-05-03 01:11:54 +000041 static char ID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000042 MSILModule(const std::set<const Type *>*& _UsedTypes,
43 const TargetData*& _TD)
Dan Gohmanae73dc12008-09-04 17:05:41 +000044 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
Anton Korobeynikov099883f2007-03-21 21:38:25 +000045
46 void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.addRequired<FindUsedTypes>();
48 AU.addRequired<TargetData>();
49 }
50
51 virtual const char *getPassName() const {
52 return "MSIL backend definitions";
53 }
54
55 virtual bool runOnModule(Module &M);
56
57 };
58
59 class MSILWriter : public FunctionPass {
60 struct StaticInitializer {
61 const Constant* constant;
62 uint64_t offset;
63
64 StaticInitializer()
65 : constant(0), offset(0) {}
66
67 StaticInitializer(const Constant* _constant, uint64_t _offset)
68 : constant(_constant), offset(_offset) {}
69 };
70
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000071 uint64_t UniqID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000072
73 uint64_t getUniqID() {
74 return ++UniqID;
75 }
76
77 public:
Owen Andersoncb371882008-08-21 00:14:44 +000078 raw_ostream &Out;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000079 Module* ModulePtr;
80 const TargetData* TD;
81 Mangler* Mang;
82 LoopInfo *LInfo;
83 std::vector<StaticInitializer>* InitListPtr;
84 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
85 StaticInitList;
86 const std::set<const Type *>* UsedTypes;
Devang Patel19974732007-05-03 01:11:54 +000087 static char ID;
Chris Lattnerca1bafd2009-07-13 23:46:46 +000088 DenseMap<const Value*, unsigned> AnonValueNumbers;
89 unsigned NextAnonValueNumber;
90
91 MSILWriter(raw_ostream &o)
92 : FunctionPass(&ID), Out(o), NextAnonValueNumber(0) {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000093 UniqID = 0;
94 }
95
96 enum ValueType {
97 UndefVT,
98 GlobalVT,
99 InternalVT,
100 ArgumentVT,
101 LocalVT,
102 ConstVT,
103 ConstExprVT
104 };
105
106 bool isVariable(ValueType V) {
107 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
108 }
109
110 bool isConstValue(ValueType V) {
111 return V==ConstVT || V==ConstExprVT;
112 }
113
114 virtual const char *getPassName() const { return "MSIL backend"; }
115
116 void getAnalysisUsage(AnalysisUsage &AU) const {
117 AU.addRequired<LoopInfo>();
118 AU.setPreservesAll();
119 }
120
121 bool runOnFunction(Function &F);
122
123 virtual bool doInitialization(Module &M);
124
125 virtual bool doFinalization(Module &M);
126
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000127 void printModuleStartup();
128
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000129 bool isZeroValue(const Value* V);
130
131 std::string getValueName(const Value* V);
132
133 std::string getLabelName(const Value* V);
134
135 std::string getLabelName(const std::string& Name);
136
137 std::string getConvModopt(unsigned CallingConvID);
138
139 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
140
141 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
142
143 std::string getFunctionTypeName(const Type* Ty);
144
145 std::string getPointerTypeName(const Type* Ty);
146
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000147 std::string getTypeName(const Type* Ty, bool isSigned = false,
148 bool isNested = false);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000149
150 ValueType getValueLocation(const Value* V);
151
152 std::string getTypePostfix(const Type* Ty, bool Expand,
153 bool isSigned = false);
154
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000155 void printConvToPtr();
156
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000157 void printPtrLoad(uint64_t N);
158
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000159 void printValuePtrLoad(const Value* V);
160
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000161 void printConstLoad(const Constant* C);
162
163 void printValueLoad(const Value* V);
164
165 void printValueSave(const Value* V);
166
167 void printBinaryInstruction(const char* Name, const Value* Left,
168 const Value* Right);
169
170 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
171
172 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
173
174 void printBranchToBlock(const BasicBlock* CurrBB,
175 const BasicBlock* TrueBB,
176 const BasicBlock* FalseBB);
177
178 void printBranchInstruction(const BranchInst* Inst);
179
180 void printSelectInstruction(const Value* Cond, const Value* VTrue,
181 const Value* VFalse);
182
183 void printIndirectLoad(const Value* V);
184
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000185 void printIndirectSave(const Value* Ptr, const Value* Val);
186
187 void printIndirectSave(const Type* Ty);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000188
189 void printCastInstruction(unsigned int Op, const Value* V,
Anton Korobeynikov94ac0342009-07-14 09:53:14 +0000190 const Type* Ty, const Type* SrcTy=0);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000191
192 void printGepInstruction(const Value* V, gep_type_iterator I,
193 gep_type_iterator E);
194
195 std::string getCallSignature(const FunctionType* Ty,
196 const Instruction* Inst,
197 std::string Name);
198
199 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
200
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000201 void printIntrinsicCall(const IntrinsicInst* Inst);
202
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000203 void printCallInstruction(const Instruction* Inst);
204
205 void printICmpInstruction(unsigned Predicate, const Value* Left,
206 const Value* Right);
207
208 void printFCmpInstruction(unsigned Predicate, const Value* Left,
209 const Value* Right);
210
211 void printInvokeInstruction(const InvokeInst* Inst);
212
213 void printSwitchInstruction(const SwitchInst* Inst);
214
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000215 void printVAArgInstruction(const VAArgInst* Inst);
216
217 void printAllocaInstruction(const AllocaInst* Inst);
218
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000219 void printInstruction(const Instruction* Inst);
220
221 void printLoop(const Loop* L);
222
223 void printBasicBlock(const BasicBlock* BB);
224
225 void printLocalVariables(const Function& F);
226
227 void printFunctionBody(const Function& F);
228
229 void printConstantExpr(const ConstantExpr* CE);
230
231 void printStaticInitializerList();
232
233 void printFunction(const Function& F);
234
235 void printDeclarations(const TypeSymbolTable& ST);
236
237 unsigned int getBitWidth(const Type* Ty);
238
239 void printStaticConstant(const Constant* C, uint64_t& Offset);
240
241 void printStaticInitializer(const Constant* C, const std::string& Name);
242
243 void printVariableDefinition(const GlobalVariable* G);
244
245 void printGlobalVariables();
246
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000247 const char* getLibraryName(const Function* F);
248
249 const char* getLibraryName(const GlobalVariable* GV);
250
251 const char* getLibraryForSymbol(const char* Name, bool isFunction,
252 unsigned CallingConv);
253
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000254 void printExternals();
255 };
256}
257
258#endif
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000259