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