Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1 | //===-- MSILWriter.h - TargetMachine for the MSIL ---------------*- C++ -*-===// |
| 2 | // |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 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" |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/IntrinsicInst.h" |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
| 21 | #include "llvm/PassManager.h" |
| 22 | #include "llvm/Analysis/FindUsedTypes.h" |
| 23 | #include "llvm/Analysis/LoopInfo.h" |
| 24 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Target/TargetMachineRegistry.h" |
| 29 | #include "llvm/Support/Mangler.h" |
| 30 | #include <algorithm> |
| 31 | #include <ios> |
| 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | class MSILModule : public ModulePass { |
| 37 | Module *ModulePtr; |
| 38 | const std::set<const Type *>*& UsedTypes; |
| 39 | const TargetData*& TD; |
| 40 | |
| 41 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 42 | static char ID; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 43 | MSILModule(const std::set<const Type *>*& _UsedTypes, |
| 44 | const TargetData*& _TD) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 45 | : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {} |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 46 | |
| 47 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 48 | AU.addRequired<FindUsedTypes>(); |
| 49 | AU.addRequired<TargetData>(); |
| 50 | } |
| 51 | |
| 52 | virtual const char *getPassName() const { |
| 53 | return "MSIL backend definitions"; |
| 54 | } |
| 55 | |
| 56 | virtual bool runOnModule(Module &M); |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | class MSILWriter : public FunctionPass { |
| 61 | struct StaticInitializer { |
| 62 | const Constant* constant; |
| 63 | uint64_t offset; |
| 64 | |
| 65 | StaticInitializer() |
| 66 | : constant(0), offset(0) {} |
| 67 | |
| 68 | StaticInitializer(const Constant* _constant, uint64_t _offset) |
| 69 | : constant(_constant), offset(_offset) {} |
| 70 | }; |
| 71 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 72 | uint64_t UniqID; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 73 | |
| 74 | uint64_t getUniqID() { |
| 75 | return ++UniqID; |
| 76 | } |
| 77 | |
| 78 | public: |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 79 | raw_ostream &Out; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 80 | Module* ModulePtr; |
| 81 | const TargetData* TD; |
| 82 | Mangler* Mang; |
| 83 | LoopInfo *LInfo; |
| 84 | std::vector<StaticInitializer>* InitListPtr; |
| 85 | std::map<const GlobalVariable*,std::vector<StaticInitializer> > |
| 86 | StaticInitList; |
| 87 | const std::set<const Type *>* UsedTypes; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 88 | static char ID; |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 89 | MSILWriter(raw_ostream &o) : FunctionPass(&ID), Out(o) { |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 90 | UniqID = 0; |
| 91 | } |
| 92 | |
| 93 | enum ValueType { |
| 94 | UndefVT, |
| 95 | GlobalVT, |
| 96 | InternalVT, |
| 97 | ArgumentVT, |
| 98 | LocalVT, |
| 99 | ConstVT, |
| 100 | ConstExprVT |
| 101 | }; |
| 102 | |
| 103 | bool isVariable(ValueType V) { |
| 104 | return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT; |
| 105 | } |
| 106 | |
| 107 | bool isConstValue(ValueType V) { |
| 108 | return V==ConstVT || V==ConstExprVT; |
| 109 | } |
| 110 | |
| 111 | virtual const char *getPassName() const { return "MSIL backend"; } |
| 112 | |
| 113 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 114 | AU.addRequired<LoopInfo>(); |
| 115 | AU.setPreservesAll(); |
| 116 | } |
| 117 | |
| 118 | bool runOnFunction(Function &F); |
| 119 | |
| 120 | virtual bool doInitialization(Module &M); |
| 121 | |
| 122 | virtual bool doFinalization(Module &M); |
| 123 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 124 | void printModuleStartup(); |
| 125 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 126 | bool isZeroValue(const Value* V); |
| 127 | |
| 128 | std::string getValueName(const Value* V); |
| 129 | |
| 130 | std::string getLabelName(const Value* V); |
| 131 | |
| 132 | std::string getLabelName(const std::string& Name); |
| 133 | |
| 134 | std::string getConvModopt(unsigned CallingConvID); |
| 135 | |
| 136 | std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty); |
| 137 | |
| 138 | std::string getPrimitiveTypeName(const Type* Ty, bool isSigned); |
| 139 | |
| 140 | std::string getFunctionTypeName(const Type* Ty); |
| 141 | |
| 142 | std::string getPointerTypeName(const Type* Ty); |
| 143 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 144 | std::string getTypeName(const Type* Ty, bool isSigned = false, |
| 145 | bool isNested = false); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 146 | |
| 147 | ValueType getValueLocation(const Value* V); |
| 148 | |
| 149 | std::string getTypePostfix(const Type* Ty, bool Expand, |
| 150 | bool isSigned = false); |
| 151 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 152 | void printConvToPtr(); |
| 153 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 154 | void printPtrLoad(uint64_t N); |
| 155 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 156 | void printValuePtrLoad(const Value* V); |
| 157 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 158 | void printConstLoad(const Constant* C); |
| 159 | |
| 160 | void printValueLoad(const Value* V); |
| 161 | |
| 162 | void printValueSave(const Value* V); |
| 163 | |
| 164 | void printBinaryInstruction(const char* Name, const Value* Left, |
| 165 | const Value* Right); |
| 166 | |
| 167 | void printSimpleInstruction(const char* Inst, const char* Operand = NULL); |
| 168 | |
| 169 | void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst); |
| 170 | |
| 171 | void printBranchToBlock(const BasicBlock* CurrBB, |
| 172 | const BasicBlock* TrueBB, |
| 173 | const BasicBlock* FalseBB); |
| 174 | |
| 175 | void printBranchInstruction(const BranchInst* Inst); |
| 176 | |
| 177 | void printSelectInstruction(const Value* Cond, const Value* VTrue, |
| 178 | const Value* VFalse); |
| 179 | |
| 180 | void printIndirectLoad(const Value* V); |
| 181 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 182 | void printIndirectSave(const Value* Ptr, const Value* Val); |
| 183 | |
| 184 | void printIndirectSave(const Type* Ty); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 185 | |
| 186 | void printCastInstruction(unsigned int Op, const Value* V, |
| 187 | const Type* Ty); |
| 188 | |
| 189 | void printGepInstruction(const Value* V, gep_type_iterator I, |
| 190 | gep_type_iterator E); |
| 191 | |
| 192 | std::string getCallSignature(const FunctionType* Ty, |
| 193 | const Instruction* Inst, |
| 194 | std::string Name); |
| 195 | |
| 196 | void printFunctionCall(const Value* FnVal, const Instruction* Inst); |
| 197 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 198 | void printIntrinsicCall(const IntrinsicInst* Inst); |
| 199 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 200 | void printCallInstruction(const Instruction* Inst); |
| 201 | |
| 202 | void printICmpInstruction(unsigned Predicate, const Value* Left, |
| 203 | const Value* Right); |
| 204 | |
| 205 | void printFCmpInstruction(unsigned Predicate, const Value* Left, |
| 206 | const Value* Right); |
| 207 | |
| 208 | void printInvokeInstruction(const InvokeInst* Inst); |
| 209 | |
| 210 | void printSwitchInstruction(const SwitchInst* Inst); |
| 211 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 212 | void printVAArgInstruction(const VAArgInst* Inst); |
| 213 | |
| 214 | void printAllocaInstruction(const AllocaInst* Inst); |
| 215 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 216 | void printInstruction(const Instruction* Inst); |
| 217 | |
| 218 | void printLoop(const Loop* L); |
| 219 | |
| 220 | void printBasicBlock(const BasicBlock* BB); |
| 221 | |
| 222 | void printLocalVariables(const Function& F); |
| 223 | |
| 224 | void printFunctionBody(const Function& F); |
| 225 | |
| 226 | void printConstantExpr(const ConstantExpr* CE); |
| 227 | |
| 228 | void printStaticInitializerList(); |
| 229 | |
| 230 | void printFunction(const Function& F); |
| 231 | |
| 232 | void printDeclarations(const TypeSymbolTable& ST); |
| 233 | |
| 234 | unsigned int getBitWidth(const Type* Ty); |
| 235 | |
| 236 | void printStaticConstant(const Constant* C, uint64_t& Offset); |
| 237 | |
| 238 | void printStaticInitializer(const Constant* C, const std::string& Name); |
| 239 | |
| 240 | void printVariableDefinition(const GlobalVariable* G); |
| 241 | |
| 242 | void printGlobalVariables(); |
| 243 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 244 | const char* getLibraryName(const Function* F); |
| 245 | |
| 246 | const char* getLibraryName(const GlobalVariable* GV); |
| 247 | |
| 248 | const char* getLibraryForSymbol(const char* Name, bool isFunction, |
| 249 | unsigned CallingConv); |
| 250 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 251 | void printExternals(); |
| 252 | }; |
| 253 | } |
| 254 | |
| 255 | #endif |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 256 | |