blob: 3db825fedb76a77f1f0cb1ee86ed4fb3aabbbf25 [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//
5// This file was developed by Roman Samoilov and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
19#include "llvm/Pass.h"
20#include "llvm/PassManager.h"
21#include "llvm/Analysis/FindUsedTypes.h"
22#include "llvm/Analysis/LoopInfo.h"
23#include "llvm/Support/GetElementPtrTypeIterator.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetMachineRegistry.h"
27#include "llvm/Support/Mangler.h"
28#include <algorithm>
29#include <ios>
30using namespace llvm;
31
32namespace {
33
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)
Devang Patel794fd752007-05-01 21:15:47 +000043 : ModulePass((intptr_t)&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
58 class MSILWriter : public FunctionPass {
59 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
70 uint64_t UniqID;
71
72 uint64_t getUniqID() {
73 return ++UniqID;
74 }
75
76 public:
77 std::ostream &Out;
78 Module* ModulePtr;
79 const TargetData* TD;
80 Mangler* Mang;
81 LoopInfo *LInfo;
82 std::vector<StaticInitializer>* InitListPtr;
83 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
84 StaticInitList;
85 const std::set<const Type *>* UsedTypes;
Devang Patel19974732007-05-03 01:11:54 +000086 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000087 MSILWriter(std::ostream &o) : FunctionPass((intptr_t)&ID), Out(o) {
Anton Korobeynikov099883f2007-03-21 21:38:25 +000088 UniqID = 0;
89 }
90
91 enum ValueType {
92 UndefVT,
93 GlobalVT,
94 InternalVT,
95 ArgumentVT,
96 LocalVT,
97 ConstVT,
98 ConstExprVT
99 };
100
101 bool isVariable(ValueType V) {
102 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
103 }
104
105 bool isConstValue(ValueType V) {
106 return V==ConstVT || V==ConstExprVT;
107 }
108
109 virtual const char *getPassName() const { return "MSIL backend"; }
110
111 void getAnalysisUsage(AnalysisUsage &AU) const {
112 AU.addRequired<LoopInfo>();
113 AU.setPreservesAll();
114 }
115
116 bool runOnFunction(Function &F);
117
118 virtual bool doInitialization(Module &M);
119
120 virtual bool doFinalization(Module &M);
121
122 bool isZeroValue(const Value* V);
123
124 std::string getValueName(const Value* V);
125
126 std::string getLabelName(const Value* V);
127
128 std::string getLabelName(const std::string& Name);
129
130 std::string getConvModopt(unsigned CallingConvID);
131
132 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
133
134 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
135
136 std::string getFunctionTypeName(const Type* Ty);
137
138 std::string getPointerTypeName(const Type* Ty);
139
140 std::string getTypeName(const Type* Ty, bool isSigned = false);
141
142 ValueType getValueLocation(const Value* V);
143
144 std::string getTypePostfix(const Type* Ty, bool Expand,
145 bool isSigned = false);
146
147 void printPtrLoad(uint64_t N);
148
149 void printConstLoad(const Constant* C);
150
151 void printValueLoad(const Value* V);
152
153 void printValueSave(const Value* V);
154
155 void printBinaryInstruction(const char* Name, const Value* Left,
156 const Value* Right);
157
158 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
159
160 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
161
162 void printBranchToBlock(const BasicBlock* CurrBB,
163 const BasicBlock* TrueBB,
164 const BasicBlock* FalseBB);
165
166 void printBranchInstruction(const BranchInst* Inst);
167
168 void printSelectInstruction(const Value* Cond, const Value* VTrue,
169 const Value* VFalse);
170
171 void printIndirectLoad(const Value* V);
172
173 void printStoreInstruction(const Instruction* Inst);
174
175 void printCastInstruction(unsigned int Op, const Value* V,
176 const Type* Ty);
177
178 void printGepInstruction(const Value* V, gep_type_iterator I,
179 gep_type_iterator E);
180
181 std::string getCallSignature(const FunctionType* Ty,
182 const Instruction* Inst,
183 std::string Name);
184
185 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
186
187 void printCallInstruction(const Instruction* Inst);
188
189 void printICmpInstruction(unsigned Predicate, const Value* Left,
190 const Value* Right);
191
192 void printFCmpInstruction(unsigned Predicate, const Value* Left,
193 const Value* Right);
194
195 void printInvokeInstruction(const InvokeInst* Inst);
196
197 void printSwitchInstruction(const SwitchInst* Inst);
198
199 void printInstruction(const Instruction* Inst);
200
201 void printLoop(const Loop* L);
202
203 void printBasicBlock(const BasicBlock* BB);
204
205 void printLocalVariables(const Function& F);
206
207 void printFunctionBody(const Function& F);
208
209 void printConstantExpr(const ConstantExpr* CE);
210
211 void printStaticInitializerList();
212
213 void printFunction(const Function& F);
214
215 void printDeclarations(const TypeSymbolTable& ST);
216
217 unsigned int getBitWidth(const Type* Ty);
218
219 void printStaticConstant(const Constant* C, uint64_t& Offset);
220
221 void printStaticInitializer(const Constant* C, const std::string& Name);
222
223 void printVariableDefinition(const GlobalVariable* G);
224
225 void printGlobalVariables();
226
227 void printExternals();
228 };
229}
230
231#endif