LLVM API Documentation

MSILWriter.h

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