blob: 6c52a17be081b43082e5bdf13db48f31fa491a6d [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"
Daniel Dunbar92645ac2009-07-21 08:58:44 +000022#include "llvm/ADT/StringRef.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000023#include "llvm/Analysis/FindUsedTypes.h"
24#include "llvm/Analysis/LoopInfo.h"
David Greene71847812009-07-14 20:18:05 +000025#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000026#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"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000031#include <ios>
32using namespace llvm;
33
Daniel Dunbar4cb1e132009-07-18 23:03:22 +000034namespace llvm {
35 extern Target TheMSILTarget;
36}
37
Anton Korobeynikov099883f2007-03-21 21:38:25 +000038namespace {
39
40 class MSILModule : public ModulePass {
41 Module *ModulePtr;
42 const std::set<const Type *>*& UsedTypes;
43 const TargetData*& TD;
44
45 public:
Devang Patel19974732007-05-03 01:11:54 +000046 static char ID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000047 MSILModule(const std::set<const Type *>*& _UsedTypes,
48 const TargetData*& _TD)
Dan Gohmanae73dc12008-09-04 17:05:41 +000049 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
Anton Korobeynikov099883f2007-03-21 21:38:25 +000050
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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000076 uint64_t UniqID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000077
78 uint64_t getUniqID() {
79 return ++UniqID;
80 }
81
82 public:
David Greene71847812009-07-14 20:18:05 +000083 formatted_raw_ostream &Out;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000084 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;
Devang Patel19974732007-05-03 01:11:54 +000092 static char ID;
Chris Lattnerca1bafd2009-07-13 23:46:46 +000093 DenseMap<const Value*, unsigned> AnonValueNumbers;
94 unsigned NextAnonValueNumber;
95
Chris Lattner5473f072009-07-14 20:25:40 +000096 MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
97 NextAnonValueNumber(0) {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000098 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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000132 void printModuleStartup();
133
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000134 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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000152 std::string getTypeName(const Type* Ty, bool isSigned = false,
153 bool isNested = false);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000154
155 ValueType getValueLocation(const Value* V);
156
157 std::string getTypePostfix(const Type* Ty, bool Expand,
158 bool isSigned = false);
159
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000160 void printConvToPtr();
161
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000162 void printPtrLoad(uint64_t N);
163
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000164 void printValuePtrLoad(const Value* V);
165
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000166 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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000190 void printIndirectSave(const Value* Ptr, const Value* Val);
191
192 void printIndirectSave(const Type* Ty);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000193
194 void printCastInstruction(unsigned int Op, const Value* V,
Anton Korobeynikov94ac0342009-07-14 09:53:14 +0000195 const Type* Ty, const Type* SrcTy=0);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000196
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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000206 void printIntrinsicCall(const IntrinsicInst* Inst);
207
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000208 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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000220 void printVAArgInstruction(const VAArgInst* Inst);
221
222 void printAllocaInstruction(const AllocaInst* Inst);
223
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000224 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
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000252 const char* getLibraryName(const Function* F);
253
254 const char* getLibraryName(const GlobalVariable* GV);
255
Daniel Dunbarbda96532009-07-21 08:57:31 +0000256 const char* getLibraryForSymbol(const StringRef &Name, bool isFunction,
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000257 unsigned CallingConv);
258
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000259 void printExternals();
260 };
Daniel Dunbar4cb1e132009-07-18 23:03:22 +0000261
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000262}
263
264#endif
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000265