LLVM API Documentation
00001 //===-- MSILWriter.h - TargetMachine for the MSIL ---------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the MSILWriter that is used by the MSIL. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 #ifndef MSILWRITER_H 00014 #define MSILWRITER_H 00015 00016 #include "llvm/Constants.h" 00017 #include "llvm/Module.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/IntrinsicInst.h" 00020 #include "llvm/Pass.h" 00021 #include "llvm/PassManager.h" 00022 #include "llvm/Analysis/FindUsedTypes.h" 00023 #include "llvm/Analysis/LoopInfo.h" 00024 #include "llvm/Support/GetElementPtrTypeIterator.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 #include "llvm/Target/TargetData.h" 00027 #include "llvm/Target/TargetMachine.h" 00028 #include "llvm/Target/TargetMachineRegistry.h" 00029 #include "llvm/Support/Mangler.h" 00030 #include <ios> 00031 using namespace llvm; 00032 00033 namespace { 00034 00035 class MSILModule : public ModulePass { 00036 Module *ModulePtr; 00037 const std::set<const Type *>*& UsedTypes; 00038 const TargetData*& TD; 00039 00040 public: 00041 static char ID; 00042 MSILModule(const std::set<const Type *>*& _UsedTypes, 00043 const TargetData*& _TD) 00044 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {} 00045 00046 void getAnalysisUsage(AnalysisUsage &AU) const { 00047 AU.addRequired<FindUsedTypes>(); 00048 AU.addRequired<TargetData>(); 00049 } 00050 00051 virtual const char *getPassName() const { 00052 return "MSIL backend definitions"; 00053 } 00054 00055 virtual bool runOnModule(Module &M); 00056 00057 }; 00058 00059 class MSILWriter : public FunctionPass { 00060 struct StaticInitializer { 00061 const Constant* constant; 00062 uint64_t offset; 00063 00064 StaticInitializer() 00065 : constant(0), offset(0) {} 00066 00067 StaticInitializer(const Constant* _constant, uint64_t _offset) 00068 : constant(_constant), offset(_offset) {} 00069 }; 00070 00071 uint64_t UniqID; 00072 00073 uint64_t getUniqID() { 00074 return ++UniqID; 00075 } 00076 00077 public: 00078 raw_ostream &Out; 00079 Module* ModulePtr; 00080 const TargetData* TD; 00081 Mangler* Mang; 00082 LoopInfo *LInfo; 00083 std::vector<StaticInitializer>* InitListPtr; 00084 std::map<const GlobalVariable*,std::vector<StaticInitializer> > 00085 StaticInitList; 00086 const std::set<const Type *>* UsedTypes; 00087 static char ID; 00088 MSILWriter(raw_ostream &o) : FunctionPass(&ID), Out(o) { 00089 UniqID = 0; 00090 } 00091 00092 enum ValueType { 00093 UndefVT, 00094 GlobalVT, 00095 InternalVT, 00096 ArgumentVT, 00097 LocalVT, 00098 ConstVT, 00099 ConstExprVT 00100 }; 00101 00102 bool isVariable(ValueType V) { 00103 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT; 00104 } 00105 00106 bool isConstValue(ValueType V) { 00107 return V==ConstVT || V==ConstExprVT; 00108 } 00109 00110 virtual const char *getPassName() const { return "MSIL backend"; } 00111 00112 void getAnalysisUsage(AnalysisUsage &AU) const { 00113 AU.addRequired<LoopInfo>(); 00114 AU.setPreservesAll(); 00115 } 00116 00117 bool runOnFunction(Function &F); 00118 00119 virtual bool doInitialization(Module &M); 00120 00121 virtual bool doFinalization(Module &M); 00122 00123 void printModuleStartup(); 00124 00125 bool isZeroValue(const Value* V); 00126 00127 std::string getValueName(const Value* V); 00128 00129 std::string getLabelName(const Value* V); 00130 00131 std::string getLabelName(const std::string& Name); 00132 00133 std::string getConvModopt(unsigned CallingConvID); 00134 00135 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty); 00136 00137 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned); 00138 00139 std::string getFunctionTypeName(const Type* Ty); 00140 00141 std::string getPointerTypeName(const Type* Ty); 00142 00143 std::string getTypeName(const Type* Ty, bool isSigned = false, 00144 bool isNested = false); 00145 00146 ValueType getValueLocation(const Value* V); 00147 00148 std::string getTypePostfix(const Type* Ty, bool Expand, 00149 bool isSigned = false); 00150 00151 void printConvToPtr(); 00152 00153 void printPtrLoad(uint64_t N); 00154 00155 void printValuePtrLoad(const Value* V); 00156 00157 void printConstLoad(const Constant* C); 00158 00159 void printValueLoad(const Value* V); 00160 00161 void printValueSave(const Value* V); 00162 00163 void printBinaryInstruction(const char* Name, const Value* Left, 00164 const Value* Right); 00165 00166 void printSimpleInstruction(const char* Inst, const char* Operand = NULL); 00167 00168 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst); 00169 00170 void printBranchToBlock(const BasicBlock* CurrBB, 00171 const BasicBlock* TrueBB, 00172 const BasicBlock* FalseBB); 00173 00174 void printBranchInstruction(const BranchInst* Inst); 00175 00176 void printSelectInstruction(const Value* Cond, const Value* VTrue, 00177 const Value* VFalse); 00178 00179 void printIndirectLoad(const Value* V); 00180 00181 void printIndirectSave(const Value* Ptr, const Value* Val); 00182 00183 void printIndirectSave(const Type* Ty); 00184 00185 void printCastInstruction(unsigned int Op, const Value* V, 00186 const Type* Ty); 00187 00188 void printGepInstruction(const Value* V, gep_type_iterator I, 00189 gep_type_iterator E); 00190 00191 std::string getCallSignature(const FunctionType* Ty, 00192 const Instruction* Inst, 00193 std::string Name); 00194 00195 void printFunctionCall(const Value* FnVal, const Instruction* Inst); 00196 00197 void printIntrinsicCall(const IntrinsicInst* Inst); 00198 00199 void printCallInstruction(const Instruction* Inst); 00200 00201 void printICmpInstruction(unsigned Predicate, const Value* Left, 00202 const Value* Right); 00203 00204 void printFCmpInstruction(unsigned Predicate, const Value* Left, 00205 const Value* Right); 00206 00207 void printInvokeInstruction(const InvokeInst* Inst); 00208 00209 void printSwitchInstruction(const SwitchInst* Inst); 00210 00211 void printVAArgInstruction(const VAArgInst* Inst); 00212 00213 void printAllocaInstruction(const AllocaInst* Inst); 00214 00215 void printInstruction(const Instruction* Inst); 00216 00217 void printLoop(const Loop* L); 00218 00219 void printBasicBlock(const BasicBlock* BB); 00220 00221 void printLocalVariables(const Function& F); 00222 00223 void printFunctionBody(const Function& F); 00224 00225 void printConstantExpr(const ConstantExpr* CE); 00226 00227 void printStaticInitializerList(); 00228 00229 void printFunction(const Function& F); 00230 00231 void printDeclarations(const TypeSymbolTable& ST); 00232 00233 unsigned int getBitWidth(const Type* Ty); 00234 00235 void printStaticConstant(const Constant* C, uint64_t& Offset); 00236 00237 void printStaticInitializer(const Constant* C, const std::string& Name); 00238 00239 void printVariableDefinition(const GlobalVariable* G); 00240 00241 void printGlobalVariables(); 00242 00243 const char* getLibraryName(const Function* F); 00244 00245 const char* getLibraryName(const GlobalVariable* GV); 00246 00247 const char* getLibraryForSymbol(const char* Name, bool isFunction, 00248 unsigned CallingConv); 00249 00250 void printExternals(); 00251 }; 00252 } 00253 00254 #endif 00255