blob: a95ae2327c2c5bd903ef9241651d0bd35d740005 [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
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000016#include "llvm/CallingConv.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000017#include "llvm/Constants.h"
18#include "llvm/Module.h"
19#include "llvm/Instructions.h"
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000020#include "llvm/IntrinsicInst.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000021#include "llvm/Pass.h"
22#include "llvm/PassManager.h"
Daniel Dunbar92645ac2009-07-21 08:58:44 +000023#include "llvm/ADT/StringRef.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000024#include "llvm/Analysis/FindUsedTypes.h"
25#include "llvm/Analysis/LoopInfo.h"
David Greene71847812009-07-14 20:18:05 +000026#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000027#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov099883f2007-03-21 21:38:25 +000030
Daniel Dunbar4cb1e132009-07-18 23:03:22 +000031namespace llvm {
32 extern Target TheMSILTarget;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000033
34 class MSILModule : public ModulePass {
35 Module *ModulePtr;
36 const std::set<const Type *>*& UsedTypes;
37 const TargetData*& TD;
38
39 public:
Devang Patel19974732007-05-03 01:11:54 +000040 static char ID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000041 MSILModule(const std::set<const Type *>*& _UsedTypes,
42 const TargetData*& _TD)
Dan Gohmanae73dc12008-09-04 17:05:41 +000043 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
Anton Korobeynikov099883f2007-03-21 21:38:25 +000044
45 void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<FindUsedTypes>();
47 AU.addRequired<TargetData>();
48 }
49
50 virtual const char *getPassName() const {
51 return "MSIL backend definitions";
52 }
53
54 virtual bool runOnModule(Module &M);
55
56 };
57
Nick Lewycky92fbbc72009-07-26 08:16:51 +000058 class MSILWriter : public FunctionPass {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000059 struct StaticInitializer {
60 const Constant* constant;
61 uint64_t offset;
62
63 StaticInitializer()
64 : constant(0), offset(0) {}
65
66 StaticInitializer(const Constant* _constant, uint64_t _offset)
67 : constant(_constant), offset(_offset) {}
68 };
69
Anton Korobeynikovf13090c2007-05-06 20:13:33 +000070 uint64_t UniqID;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000071
72 uint64_t getUniqID() {
73 return ++UniqID;
74 }
75
76 public:
David Greene71847812009-07-14 20:18:05 +000077 formatted_raw_ostream &Out;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000078 Module* ModulePtr;
79 const TargetData* TD;
Anton Korobeynikov099883f2007-03-21 21:38:25 +000080 LoopInfo *LInfo;
81 std::vector<StaticInitializer>* InitListPtr;
82 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
83 StaticInitList;
84 const std::set<const Type *>* UsedTypes;
Devang Patel19974732007-05-03 01:11:54 +000085 static char ID;
Chris Lattnerca1bafd2009-07-13 23:46:46 +000086 DenseMap<const Value*, unsigned> AnonValueNumbers;
87 unsigned NextAnonValueNumber;
88
Chris Lattner5473f072009-07-14 20:25:40 +000089 MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
90 NextAnonValueNumber(0) {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000091 UniqID = 0;
92 }
93
94 enum ValueType {
95 UndefVT,
96 GlobalVT,
97 InternalVT,
98 ArgumentVT,
99 LocalVT,
100 ConstVT,
101 ConstExprVT
102 };
103
104 bool isVariable(ValueType V) {
105 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
106 }
107
108 bool isConstValue(ValueType V) {
109 return V==ConstVT || V==ConstExprVT;
110 }
111
112 virtual const char *getPassName() const { return "MSIL backend"; }
113
114 void getAnalysisUsage(AnalysisUsage &AU) const {
115 AU.addRequired<LoopInfo>();
116 AU.setPreservesAll();
117 }
118
119 bool runOnFunction(Function &F);
120
121 virtual bool doInitialization(Module &M);
122
123 virtual bool doFinalization(Module &M);
124
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000125 void printModuleStartup();
126
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000127 bool isZeroValue(const Value* V);
128
129 std::string getValueName(const Value* V);
130
131 std::string getLabelName(const Value* V);
132
133 std::string getLabelName(const std::string& Name);
134
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000135 std::string getConvModopt(CallingConv::ID CallingConvID);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000136
137 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
138
139 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
140
141 std::string getFunctionTypeName(const Type* Ty);
142
143 std::string getPointerTypeName(const Type* Ty);
144
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000145 std::string getTypeName(const Type* Ty, bool isSigned = false,
146 bool isNested = false);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000147
148 ValueType getValueLocation(const Value* V);
149
150 std::string getTypePostfix(const Type* Ty, bool Expand,
151 bool isSigned = false);
152
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000153 void printConvToPtr();
154
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000155 void printPtrLoad(uint64_t N);
156
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000157 void printValuePtrLoad(const Value* V);
158
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000159 void printConstLoad(const Constant* C);
160
161 void printValueLoad(const Value* V);
162
163 void printValueSave(const Value* V);
164
165 void printBinaryInstruction(const char* Name, const Value* Left,
166 const Value* Right);
167
168 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
169
170 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
171
172 void printBranchToBlock(const BasicBlock* CurrBB,
173 const BasicBlock* TrueBB,
174 const BasicBlock* FalseBB);
175
176 void printBranchInstruction(const BranchInst* Inst);
177
178 void printSelectInstruction(const Value* Cond, const Value* VTrue,
179 const Value* VFalse);
180
181 void printIndirectLoad(const Value* V);
182
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000183 void printIndirectSave(const Value* Ptr, const Value* Val);
184
185 void printIndirectSave(const Type* Ty);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000186
187 void printCastInstruction(unsigned int Op, const Value* V,
Anton Korobeynikov94ac0342009-07-14 09:53:14 +0000188 const Type* Ty, const Type* SrcTy=0);
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000189
190 void printGepInstruction(const Value* V, gep_type_iterator I,
191 gep_type_iterator E);
192
193 std::string getCallSignature(const FunctionType* Ty,
194 const Instruction* Inst,
195 std::string Name);
196
197 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
198
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000199 void printIntrinsicCall(const IntrinsicInst* Inst);
200
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000201 void printCallInstruction(const Instruction* Inst);
202
203 void printICmpInstruction(unsigned Predicate, const Value* Left,
204 const Value* Right);
205
206 void printFCmpInstruction(unsigned Predicate, const Value* Left,
207 const Value* Right);
208
209 void printInvokeInstruction(const InvokeInst* Inst);
210
211 void printSwitchInstruction(const SwitchInst* Inst);
212
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000213 void printVAArgInstruction(const VAArgInst* Inst);
214
215 void printAllocaInstruction(const AllocaInst* Inst);
216
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000217 void printInstruction(const Instruction* Inst);
218
219 void printLoop(const Loop* L);
220
221 void printBasicBlock(const BasicBlock* BB);
222
223 void printLocalVariables(const Function& F);
224
225 void printFunctionBody(const Function& F);
226
227 void printConstantExpr(const ConstantExpr* CE);
228
229 void printStaticInitializerList();
230
231 void printFunction(const Function& F);
232
233 void printDeclarations(const TypeSymbolTable& ST);
234
235 unsigned int getBitWidth(const Type* Ty);
236
237 void printStaticConstant(const Constant* C, uint64_t& Offset);
238
239 void printStaticInitializer(const Constant* C, const std::string& Name);
240
241 void printVariableDefinition(const GlobalVariable* G);
242
243 void printGlobalVariables();
244
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000245 const char* getLibraryName(const Function* F);
246
247 const char* getLibraryName(const GlobalVariable* GV);
248
Daniel Dunbarbda96532009-07-21 08:57:31 +0000249 const char* getLibraryForSymbol(const StringRef &Name, bool isFunction,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000250 CallingConv::ID CallingConv);
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000251
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000252 void printExternals();
253 };
Daniel Dunbar4cb1e132009-07-18 23:03:22 +0000254
Anton Korobeynikov099883f2007-03-21 21:38:25 +0000255}
256
257#endif
Anton Korobeynikovf13090c2007-05-06 20:13:33 +0000258