1 //===-- MSILWriter.h - TargetMachine for the MSIL ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MSILWriter that is used by the MSIL.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/CallingConv.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Pass.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Analysis/FindUsedTypes.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/GetElementPtrTypeIterator.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetMachine.h"
32 extern Target TheMSILTarget;
34 class MSILModule : public ModulePass {
36 const std::set<const Type *>*& UsedTypes;
37 const TargetData*& TD;
41 MSILModule(const std::set<const Type *>*& _UsedTypes,
42 const TargetData*& _TD)
43 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
45 void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<FindUsedTypes>();
47 AU.addRequired<TargetData>();
50 virtual const char *getPassName() const {
51 return "MSIL backend definitions";
54 virtual bool runOnModule(Module &M);
58 class MSILWriter : public FunctionPass {
59 struct StaticInitializer {
60 const Constant* constant;
64 : constant(0), offset(0) {}
66 StaticInitializer(const Constant* _constant, uint64_t _offset)
67 : constant(_constant), offset(_offset) {}
72 uint64_t getUniqID() {
77 formatted_raw_ostream &Out;
81 std::vector<StaticInitializer>* InitListPtr;
82 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
84 const std::set<const Type *>* UsedTypes;
86 DenseMap<const Value*, unsigned> AnonValueNumbers;
87 unsigned NextAnonValueNumber;
89 MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
90 NextAnonValueNumber(0) {
104 bool isVariable(ValueType V) {
105 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
108 bool isConstValue(ValueType V) {
109 return V==ConstVT || V==ConstExprVT;
112 virtual const char *getPassName() const { return "MSIL backend"; }
114 void getAnalysisUsage(AnalysisUsage &AU) const {
115 AU.addRequired<LoopInfo>();
116 AU.setPreservesAll();
119 bool runOnFunction(Function &F);
121 virtual bool doInitialization(Module &M);
123 virtual bool doFinalization(Module &M);
125 void printModuleStartup();
127 bool isZeroValue(const Value* V);
129 std::string getValueName(const Value* V);
131 std::string getLabelName(const Value* V);
133 std::string getLabelName(const std::string& Name);
135 std::string getConvModopt(CallingConv::ID CallingConvID);
137 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
139 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
141 std::string getFunctionTypeName(const Type* Ty);
143 std::string getPointerTypeName(const Type* Ty);
145 std::string getTypeName(const Type* Ty, bool isSigned = false,
146 bool isNested = false);
148 ValueType getValueLocation(const Value* V);
150 std::string getTypePostfix(const Type* Ty, bool Expand,
151 bool isSigned = false);
153 void printConvToPtr();
155 void printPtrLoad(uint64_t N);
157 void printValuePtrLoad(const Value* V);
159 void printConstLoad(const Constant* C);
161 void printValueLoad(const Value* V);
163 void printValueSave(const Value* V);
165 void printBinaryInstruction(const char* Name, const Value* Left,
168 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
170 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
172 void printBranchToBlock(const BasicBlock* CurrBB,
173 const BasicBlock* TrueBB,
174 const BasicBlock* FalseBB);
176 void printBranchInstruction(const BranchInst* Inst);
178 void printSelectInstruction(const Value* Cond, const Value* VTrue,
179 const Value* VFalse);
181 void printIndirectLoad(const Value* V);
183 void printIndirectSave(const Value* Ptr, const Value* Val);
185 void printIndirectSave(const Type* Ty);
187 void printCastInstruction(unsigned int Op, const Value* V,
188 const Type* Ty, const Type* SrcTy=0);
190 void printGepInstruction(const Value* V, gep_type_iterator I,
191 gep_type_iterator E);
193 std::string getCallSignature(const FunctionType* Ty,
194 const Instruction* Inst,
197 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
199 void printIntrinsicCall(const IntrinsicInst* Inst);
201 void printCallInstruction(const Instruction* Inst);
203 void printICmpInstruction(unsigned Predicate, const Value* Left,
206 void printFCmpInstruction(unsigned Predicate, const Value* Left,
209 void printInvokeInstruction(const InvokeInst* Inst);
211 void printSwitchInstruction(const SwitchInst* Inst);
213 void printVAArgInstruction(const VAArgInst* Inst);
215 void printAllocaInstruction(const AllocaInst* Inst);
217 void printInstruction(const Instruction* Inst);
219 void printLoop(const Loop* L);
221 void printBasicBlock(const BasicBlock* BB);
223 void printLocalVariables(const Function& F);
225 void printFunctionBody(const Function& F);
227 void printConstantExpr(const ConstantExpr* CE);
229 void printStaticInitializerList();
231 void printFunction(const Function& F);
233 void printDeclarations(const TypeSymbolTable& ST);
235 unsigned int getBitWidth(const Type* Ty);
237 void printStaticConstant(const Constant* C, uint64_t& Offset);
239 void printStaticInitializer(const Constant* C, const std::string& Name);
241 void printVariableDefinition(const GlobalVariable* G);
243 void printGlobalVariables();
245 const char* getLibraryName(const Function* F);
247 const char* getLibraryName(const GlobalVariable* GV);
249 const char* getLibraryForSymbol(const StringRef &Name, bool isFunction,
250 CallingConv::ID CallingConv);
252 void printExternals();