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/Constants.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Pass.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Analysis/FindUsedTypes.h"
24 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Support/FormattedStream.h"
26 #include "llvm/Support/GetElementPtrTypeIterator.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Support/Mangler.h"
34 extern Target TheMSILTarget;
39 class MSILModule : public ModulePass {
41 const std::set<const Type *>*& UsedTypes;
42 const TargetData*& TD;
46 MSILModule(const std::set<const Type *>*& _UsedTypes,
47 const TargetData*& _TD)
48 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
50 void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.addRequired<FindUsedTypes>();
52 AU.addRequired<TargetData>();
55 virtual const char *getPassName() const {
56 return "MSIL backend definitions";
59 virtual bool runOnModule(Module &M);
63 class MSILWriter : public FunctionPass {
64 struct StaticInitializer {
65 const Constant* constant;
69 : constant(0), offset(0) {}
71 StaticInitializer(const Constant* _constant, uint64_t _offset)
72 : constant(_constant), offset(_offset) {}
77 uint64_t getUniqID() {
82 formatted_raw_ostream &Out;
87 std::vector<StaticInitializer>* InitListPtr;
88 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
90 const std::set<const Type *>* UsedTypes;
92 DenseMap<const Value*, unsigned> AnonValueNumbers;
93 unsigned NextAnonValueNumber;
95 MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
96 NextAnonValueNumber(0) {
110 bool isVariable(ValueType V) {
111 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
114 bool isConstValue(ValueType V) {
115 return V==ConstVT || V==ConstExprVT;
118 virtual const char *getPassName() const { return "MSIL backend"; }
120 void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.addRequired<LoopInfo>();
122 AU.setPreservesAll();
125 bool runOnFunction(Function &F);
127 virtual bool doInitialization(Module &M);
129 virtual bool doFinalization(Module &M);
131 void printModuleStartup();
133 bool isZeroValue(const Value* V);
135 std::string getValueName(const Value* V);
137 std::string getLabelName(const Value* V);
139 std::string getLabelName(const std::string& Name);
141 std::string getConvModopt(unsigned CallingConvID);
143 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
145 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
147 std::string getFunctionTypeName(const Type* Ty);
149 std::string getPointerTypeName(const Type* Ty);
151 std::string getTypeName(const Type* Ty, bool isSigned = false,
152 bool isNested = false);
154 ValueType getValueLocation(const Value* V);
156 std::string getTypePostfix(const Type* Ty, bool Expand,
157 bool isSigned = false);
159 void printConvToPtr();
161 void printPtrLoad(uint64_t N);
163 void printValuePtrLoad(const Value* V);
165 void printConstLoad(const Constant* C);
167 void printValueLoad(const Value* V);
169 void printValueSave(const Value* V);
171 void printBinaryInstruction(const char* Name, const Value* Left,
174 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
176 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
178 void printBranchToBlock(const BasicBlock* CurrBB,
179 const BasicBlock* TrueBB,
180 const BasicBlock* FalseBB);
182 void printBranchInstruction(const BranchInst* Inst);
184 void printSelectInstruction(const Value* Cond, const Value* VTrue,
185 const Value* VFalse);
187 void printIndirectLoad(const Value* V);
189 void printIndirectSave(const Value* Ptr, const Value* Val);
191 void printIndirectSave(const Type* Ty);
193 void printCastInstruction(unsigned int Op, const Value* V,
194 const Type* Ty, const Type* SrcTy=0);
196 void printGepInstruction(const Value* V, gep_type_iterator I,
197 gep_type_iterator E);
199 std::string getCallSignature(const FunctionType* Ty,
200 const Instruction* Inst,
203 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
205 void printIntrinsicCall(const IntrinsicInst* Inst);
207 void printCallInstruction(const Instruction* Inst);
209 void printICmpInstruction(unsigned Predicate, const Value* Left,
212 void printFCmpInstruction(unsigned Predicate, const Value* Left,
215 void printInvokeInstruction(const InvokeInst* Inst);
217 void printSwitchInstruction(const SwitchInst* Inst);
219 void printVAArgInstruction(const VAArgInst* Inst);
221 void printAllocaInstruction(const AllocaInst* Inst);
223 void printInstruction(const Instruction* Inst);
225 void printLoop(const Loop* L);
227 void printBasicBlock(const BasicBlock* BB);
229 void printLocalVariables(const Function& F);
231 void printFunctionBody(const Function& F);
233 void printConstantExpr(const ConstantExpr* CE);
235 void printStaticInitializerList();
237 void printFunction(const Function& F);
239 void printDeclarations(const TypeSymbolTable& ST);
241 unsigned int getBitWidth(const Type* Ty);
243 void printStaticConstant(const Constant* C, uint64_t& Offset);
245 void printStaticInitializer(const Constant* C, const std::string& Name);
247 void printVariableDefinition(const GlobalVariable* G);
249 void printGlobalVariables();
251 const char* getLibraryName(const Function* F);
253 const char* getLibraryName(const GlobalVariable* GV);
255 const char* getLibraryForSymbol(const StringRef &Name, bool isFunction,
256 unsigned CallingConv);
258 void printExternals();