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/Analysis/FindUsedTypes.h"
23 #include "llvm/Analysis/LoopInfo.h"
24 #include "llvm/Support/GetElementPtrTypeIterator.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetMachineRegistry.h"
29 #include "llvm/Support/Mangler.h"
36 class MSILModule : public ModulePass {
38 const std::set<const Type *>*& UsedTypes;
39 const TargetData*& TD;
43 MSILModule(const std::set<const Type *>*& _UsedTypes,
44 const TargetData*& _TD)
45 : ModulePass((intptr_t)&ID), UsedTypes(_UsedTypes), TD(_TD) {}
47 void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<FindUsedTypes>();
49 AU.addRequired<TargetData>();
52 virtual const char *getPassName() const {
53 return "MSIL backend definitions";
56 virtual bool runOnModule(Module &M);
60 class MSILWriter : public FunctionPass {
61 struct StaticInitializer {
62 const Constant* constant;
66 : constant(0), offset(0) {}
68 StaticInitializer(const Constant* _constant, uint64_t _offset)
69 : constant(_constant), offset(_offset) {}
74 uint64_t getUniqID() {
84 std::vector<StaticInitializer>* InitListPtr;
85 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
87 const std::set<const Type *>* UsedTypes;
89 MSILWriter(raw_ostream &o) : FunctionPass((intptr_t)&ID), Out(o) {
103 bool isVariable(ValueType V) {
104 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
107 bool isConstValue(ValueType V) {
108 return V==ConstVT || V==ConstExprVT;
111 virtual const char *getPassName() const { return "MSIL backend"; }
113 void getAnalysisUsage(AnalysisUsage &AU) const {
114 AU.addRequired<LoopInfo>();
115 AU.setPreservesAll();
118 bool runOnFunction(Function &F);
120 virtual bool doInitialization(Module &M);
122 virtual bool doFinalization(Module &M);
124 void printModuleStartup();
126 bool isZeroValue(const Value* V);
128 std::string getValueName(const Value* V);
130 std::string getLabelName(const Value* V);
132 std::string getLabelName(const std::string& Name);
134 std::string getConvModopt(unsigned CallingConvID);
136 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
138 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
140 std::string getFunctionTypeName(const Type* Ty);
142 std::string getPointerTypeName(const Type* Ty);
144 std::string getTypeName(const Type* Ty, bool isSigned = false,
145 bool isNested = false);
147 ValueType getValueLocation(const Value* V);
149 std::string getTypePostfix(const Type* Ty, bool Expand,
150 bool isSigned = false);
152 void printConvToPtr();
154 void printPtrLoad(uint64_t N);
156 void printValuePtrLoad(const Value* V);
158 void printConstLoad(const Constant* C);
160 void printValueLoad(const Value* V);
162 void printValueSave(const Value* V);
164 void printBinaryInstruction(const char* Name, const Value* Left,
167 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
169 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
171 void printBranchToBlock(const BasicBlock* CurrBB,
172 const BasicBlock* TrueBB,
173 const BasicBlock* FalseBB);
175 void printBranchInstruction(const BranchInst* Inst);
177 void printSelectInstruction(const Value* Cond, const Value* VTrue,
178 const Value* VFalse);
180 void printIndirectLoad(const Value* V);
182 void printIndirectSave(const Value* Ptr, const Value* Val);
184 void printIndirectSave(const Type* Ty);
186 void printCastInstruction(unsigned int Op, const Value* V,
189 void printGepInstruction(const Value* V, gep_type_iterator I,
190 gep_type_iterator E);
192 std::string getCallSignature(const FunctionType* Ty,
193 const Instruction* Inst,
196 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
198 void printIntrinsicCall(const IntrinsicInst* Inst);
200 void printCallInstruction(const Instruction* Inst);
202 void printICmpInstruction(unsigned Predicate, const Value* Left,
205 void printFCmpInstruction(unsigned Predicate, const Value* Left,
208 void printInvokeInstruction(const InvokeInst* Inst);
210 void printSwitchInstruction(const SwitchInst* Inst);
212 void printVAArgInstruction(const VAArgInst* Inst);
214 void printAllocaInstruction(const AllocaInst* Inst);
216 void printInstruction(const Instruction* Inst);
218 void printLoop(const Loop* L);
220 void printBasicBlock(const BasicBlock* BB);
222 void printLocalVariables(const Function& F);
224 void printFunctionBody(const Function& F);
226 void printConstantExpr(const ConstantExpr* CE);
228 void printStaticInitializerList();
230 void printFunction(const Function& F);
232 void printDeclarations(const TypeSymbolTable& ST);
234 unsigned int getBitWidth(const Type* Ty);
236 void printStaticConstant(const Constant* C, uint64_t& Offset);
238 void printStaticInitializer(const Constant* C, const std::string& Name);
240 void printVariableDefinition(const GlobalVariable* G);
242 void printGlobalVariables();
244 const char* getLibraryName(const Function* F);
246 const char* getLibraryName(const GlobalVariable* GV);
248 const char* getLibraryForSymbol(const char* Name, bool isFunction,
249 unsigned CallingConv);
251 void printExternals();