blob: 0d0d0ff271f32338fba159f645c9048151aae61f [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"
David Greene71847812009-07-14 20:18:05 +000024#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000025#include "llvm/Support/GetElementPtrTypeIterator.h"
26#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
Daniel Dunbar4cb1e132009-07-18 23:03:22 +000033namespace llvm {
34 extern Target TheMSILTarget;
35}
36
Anton Korobeynikov099883f2007-03-21 21:38:25 +000037namespace {
38
39 class MSILModule : public ModulePass {
40 Module *ModulePtr;
41 const std::set<const Type *>*& UsedTypes;
42 const TargetData*& TD;
43
44 public:
Devang Patel19974732007-05-03 01:11:54 +000045 static char ID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000046 MSILModule(const std::set<const Type *>*& _UsedTypes,
47 const TargetData*& _TD)
Dan Gohmanae73dc12008-09-04 17:05:41 +000048 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
Anton Korobeynikov099883f2007-03-21 21:38:25 +000049
50 void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.addRequired<FindUsedTypes>();
52 AU.addRequired<TargetData>();
53 }
54
55 virtual const char *getPassName() const {
56 return "MSIL backend definitions";
57 }
58
59 virtual bool runOnModule(Module &M);
60
61 };
62
63 class MSILWriter : public FunctionPass {
64 struct StaticInitializer {
65 const Constant* constant;
66 uint64_t offset;
67
68 StaticInitializer()
69 : constant(0), offset(0) {}
70
71 StaticInitializer(const Constant* _constant, uint64_t _offset)
72 : constant(_constant), offset(_offset) {}
73 };
74
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000075 uint64_t UniqID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000076
77 uint64_t getUniqID() {
78 return ++UniqID;
79 }
80
81 public:
David Greene71847812009-07-14 20:18:05 +000082 formatted_raw_ostream &Out;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000083 Module* ModulePtr;
84 const TargetData* TD;
85 Mangler* Mang;
86 LoopInfo *LInfo;
87 std::vector<StaticInitializer>* InitListPtr;
88 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
89 StaticInitList;
90 const std::set<const Type *>* UsedTypes;
Devang Patel19974732007-05-03 01:11:54 +000091 static char ID;
Chris Lattnerca1bafd2009-07-13 23:46:46 +000092 DenseMap<const Value*, unsigned> AnonValueNumbers;
93 unsigned NextAnonValueNumber;
94
Chris Lattner5473f072009-07-14 20:25:40 +000095 MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
96 NextAnonValueNumber(0) {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000097 UniqID = 0;
98 }
99
100 enum ValueType {
101 UndefVT,
102 GlobalVT,
103 InternalVT,
104 ArgumentVT,
105 LocalVT,
106 ConstVT,
107 ConstExprVT
108 };
109
110 bool isVariable(ValueType V) {
111 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
112 }
113
114 bool isConstValue(ValueType V) {
115 return V==ConstVT || V==ConstExprVT;
116 }
117
118 virtual const char *getPassName() const { return "MSIL backend"; }
119
120 void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.addRequired<LoopInfo>();
122 AU.setPreservesAll();
123 }
124
125 bool runOnFunction(Function &F);
126
127 virtual bool doInitialization(Module &M);
128
129 virtual bool doFinalization(Module &M);
130
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000131 void printModuleStartup();
132
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000133 bool isZeroValue(const Value* V);
134
135 std::string getValueName(const Value* V);
136
137 std::string getLabelName(const Value* V);
138
139 std::string getLabelName(const std::string& Name);
140
141 std::string getConvModopt(unsigned CallingConvID);
142
143 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
144
145 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
146
147 std::string getFunctionTypeName(const Type* Ty);
148
149 std::string getPointerTypeName(const Type* Ty);
150
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000151 std::string getTypeName(const Type* Ty, bool isSigned = false,
152 bool isNested = false);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000153
154 ValueType getValueLocation(const Value* V);
155
156 std::string getTypePostfix(const Type* Ty, bool Expand,
157 bool isSigned = false);
158
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000159 void printConvToPtr();
160
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000161 void printPtrLoad(uint64_t N);
162
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000163 void printValuePtrLoad(const Value* V);
164
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000165 void printConstLoad(const Constant* C);
166
167 void printValueLoad(const Value* V);
168
169 void printValueSave(const Value* V);
170
171 void printBinaryInstruction(const char* Name, const Value* Left,
172 const Value* Right);
173
174 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
175
176 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
177
178 void printBranchToBlock(const BasicBlock* CurrBB,
179 const BasicBlock* TrueBB,
180 const BasicBlock* FalseBB);
181
182 void printBranchInstruction(const BranchInst* Inst);
183
184 void printSelectInstruction(const Value* Cond, const Value* VTrue,
185 const Value* VFalse);
186
187 void printIndirectLoad(const Value* V);
188
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000189 void printIndirectSave(const Value* Ptr, const Value* Val);
190
191 void printIndirectSave(const Type* Ty);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000192
193 void printCastInstruction(unsigned int Op, const Value* V,
Anton Korobeynikov94ac0342009-07-14 09:53:14 +0000194 const Type* Ty, const Type* SrcTy=0);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000195
196 void printGepInstruction(const Value* V, gep_type_iterator I,
197 gep_type_iterator E);
198
199 std::string getCallSignature(const FunctionType* Ty,
200 const Instruction* Inst,
201 std::string Name);
202
203 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
204
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000205 void printIntrinsicCall(const IntrinsicInst* Inst);
206
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000207 void printCallInstruction(const Instruction* Inst);
208
209 void printICmpInstruction(unsigned Predicate, const Value* Left,
210 const Value* Right);
211
212 void printFCmpInstruction(unsigned Predicate, const Value* Left,
213 const Value* Right);
214
215 void printInvokeInstruction(const InvokeInst* Inst);
216
217 void printSwitchInstruction(const SwitchInst* Inst);
218
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000219 void printVAArgInstruction(const VAArgInst* Inst);
220
221 void printAllocaInstruction(const AllocaInst* Inst);
222
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000223 void printInstruction(const Instruction* Inst);
224
225 void printLoop(const Loop* L);
226
227 void printBasicBlock(const BasicBlock* BB);
228
229 void printLocalVariables(const Function& F);
230
231 void printFunctionBody(const Function& F);
232
233 void printConstantExpr(const ConstantExpr* CE);
234
235 void printStaticInitializerList();
236
237 void printFunction(const Function& F);
238
239 void printDeclarations(const TypeSymbolTable& ST);
240
241 unsigned int getBitWidth(const Type* Ty);
242
243 void printStaticConstant(const Constant* C, uint64_t& Offset);
244
245 void printStaticInitializer(const Constant* C, const std::string& Name);
246
247 void printVariableDefinition(const GlobalVariable* G);
248
249 void printGlobalVariables();
250
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000251 const char* getLibraryName(const Function* F);
252
253 const char* getLibraryName(const GlobalVariable* GV);
254
255 const char* getLibraryForSymbol(const char* Name, bool isFunction,
256 unsigned CallingConv);
257
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000258 void printExternals();
259 };
Daniel Dunbar4cb1e132009-07-18 23:03:22 +0000260
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000261}
262
263#endif
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000264