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/Target/TargetMachineRegistry.h"
30 #include "llvm/Support/Mangler.h"
35 extern Target TheMSILTarget;
40 class MSILModule : public ModulePass {
42 const std::set<const Type *>*& UsedTypes;
43 const TargetData*& TD;
47 MSILModule(const std::set<const Type *>*& _UsedTypes,
48 const TargetData*& _TD)
49 : ModulePass(&ID), UsedTypes(_UsedTypes), TD(_TD) {}
51 void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequired<FindUsedTypes>();
53 AU.addRequired<TargetData>();
56 virtual const char *getPassName() const {
57 return "MSIL backend definitions";
60 virtual bool runOnModule(Module &M);
64 class MSILWriter : public FunctionPass {
65 struct StaticInitializer {
66 const Constant* constant;
70 : constant(0), offset(0) {}
72 StaticInitializer(const Constant* _constant, uint64_t _offset)
73 : constant(_constant), offset(_offset) {}
78 uint64_t getUniqID() {
83 formatted_raw_ostream &Out;
88 std::vector<StaticInitializer>* InitListPtr;
89 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
91 const std::set<const Type *>* UsedTypes;
93 DenseMap<const Value*, unsigned> AnonValueNumbers;
94 unsigned NextAnonValueNumber;
96 MSILWriter(formatted_raw_ostream &o) : FunctionPass(&ID), Out(o),
97 NextAnonValueNumber(0) {
111 bool isVariable(ValueType V) {
112 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
115 bool isConstValue(ValueType V) {
116 return V==ConstVT || V==ConstExprVT;
119 virtual const char *getPassName() const { return "MSIL backend"; }
121 void getAnalysisUsage(AnalysisUsage &AU) const {
122 AU.addRequired<LoopInfo>();
123 AU.setPreservesAll();
126 bool runOnFunction(Function &F);
128 virtual bool doInitialization(Module &M);
130 virtual bool doFinalization(Module &M);
132 void printModuleStartup();
134 bool isZeroValue(const Value* V);
136 std::string getValueName(const Value* V);
138 std::string getLabelName(const Value* V);
140 std::string getLabelName(const std::string& Name);
142 std::string getConvModopt(unsigned CallingConvID);
144 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
146 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
148 std::string getFunctionTypeName(const Type* Ty);
150 std::string getPointerTypeName(const Type* Ty);
152 std::string getTypeName(const Type* Ty, bool isSigned = false,
153 bool isNested = false);
155 ValueType getValueLocation(const Value* V);
157 std::string getTypePostfix(const Type* Ty, bool Expand,
158 bool isSigned = false);
160 void printConvToPtr();
162 void printPtrLoad(uint64_t N);
164 void printValuePtrLoad(const Value* V);
166 void printConstLoad(const Constant* C);
168 void printValueLoad(const Value* V);
170 void printValueSave(const Value* V);
172 void printBinaryInstruction(const char* Name, const Value* Left,
175 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
177 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
179 void printBranchToBlock(const BasicBlock* CurrBB,
180 const BasicBlock* TrueBB,
181 const BasicBlock* FalseBB);
183 void printBranchInstruction(const BranchInst* Inst);
185 void printSelectInstruction(const Value* Cond, const Value* VTrue,
186 const Value* VFalse);
188 void printIndirectLoad(const Value* V);
190 void printIndirectSave(const Value* Ptr, const Value* Val);
192 void printIndirectSave(const Type* Ty);
194 void printCastInstruction(unsigned int Op, const Value* V,
195 const Type* Ty, const Type* SrcTy=0);
197 void printGepInstruction(const Value* V, gep_type_iterator I,
198 gep_type_iterator E);
200 std::string getCallSignature(const FunctionType* Ty,
201 const Instruction* Inst,
204 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
206 void printIntrinsicCall(const IntrinsicInst* Inst);
208 void printCallInstruction(const Instruction* Inst);
210 void printICmpInstruction(unsigned Predicate, const Value* Left,
213 void printFCmpInstruction(unsigned Predicate, const Value* Left,
216 void printInvokeInstruction(const InvokeInst* Inst);
218 void printSwitchInstruction(const SwitchInst* Inst);
220 void printVAArgInstruction(const VAArgInst* Inst);
222 void printAllocaInstruction(const AllocaInst* Inst);
224 void printInstruction(const Instruction* Inst);
226 void printLoop(const Loop* L);
228 void printBasicBlock(const BasicBlock* BB);
230 void printLocalVariables(const Function& F);
232 void printFunctionBody(const Function& F);
234 void printConstantExpr(const ConstantExpr* CE);
236 void printStaticInitializerList();
238 void printFunction(const Function& F);
240 void printDeclarations(const TypeSymbolTable& ST);
242 unsigned int getBitWidth(const Type* Ty);
244 void printStaticConstant(const Constant* C, uint64_t& Offset);
246 void printStaticInitializer(const Constant* C, const std::string& Name);
248 void printVariableDefinition(const GlobalVariable* G);
250 void printGlobalVariables();
252 const char* getLibraryName(const Function* F);
254 const char* getLibraryName(const GlobalVariable* GV);
256 const char* getLibraryForSymbol(const StringRef &Name, bool isFunction,
257 unsigned CallingConv);
259 void printExternals();