1 //===-- MSILWriter.h - TargetMachine for the MSIL ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Roman Samoilov and is distributed under
6 // the University of Illinois Open Source 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/Pass.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Analysis/FindUsedTypes.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetMachineRegistry.h"
27 #include "llvm/Support/Mangler.h"
34 class MSILModule : public ModulePass {
36 const std::set<const Type *>*& UsedTypes;
37 const TargetData*& TD;
41 MSILModule(const std::set<const Type *>*& _UsedTypes,
42 const TargetData*& _TD)
43 : ModulePass((intptr_t)&ID), UsedTypes(_UsedTypes), TD(_TD) {}
45 void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<FindUsedTypes>();
47 AU.addRequired<TargetData>();
50 virtual const char *getPassName() const {
51 return "MSIL backend definitions";
54 virtual bool runOnModule(Module &M);
58 class MSILWriter : public FunctionPass {
59 struct StaticInitializer {
60 const Constant* constant;
64 : constant(0), offset(0) {}
66 StaticInitializer(const Constant* _constant, uint64_t _offset)
67 : constant(_constant), offset(_offset) {}
72 uint64_t getUniqID() {
82 std::vector<StaticInitializer>* InitListPtr;
83 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
85 const std::set<const Type *>* UsedTypes;
87 MSILWriter(std::ostream &o) : FunctionPass((intptr_t)&ID), Out(o) {
101 bool isVariable(ValueType V) {
102 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
105 bool isConstValue(ValueType V) {
106 return V==ConstVT || V==ConstExprVT;
109 virtual const char *getPassName() const { return "MSIL backend"; }
111 void getAnalysisUsage(AnalysisUsage &AU) const {
112 AU.addRequired<LoopInfo>();
113 AU.setPreservesAll();
116 bool runOnFunction(Function &F);
118 virtual bool doInitialization(Module &M);
120 virtual bool doFinalization(Module &M);
122 bool isZeroValue(const Value* V);
124 std::string getValueName(const Value* V);
126 std::string getLabelName(const Value* V);
128 std::string getLabelName(const std::string& Name);
130 std::string getConvModopt(unsigned CallingConvID);
132 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
134 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
136 std::string getFunctionTypeName(const Type* Ty);
138 std::string getPointerTypeName(const Type* Ty);
140 std::string getTypeName(const Type* Ty, bool isSigned = false);
142 ValueType getValueLocation(const Value* V);
144 std::string getTypePostfix(const Type* Ty, bool Expand,
145 bool isSigned = false);
147 void printPtrLoad(uint64_t N);
149 void printConstLoad(const Constant* C);
151 void printValueLoad(const Value* V);
153 void printValueSave(const Value* V);
155 void printBinaryInstruction(const char* Name, const Value* Left,
158 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
160 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
162 void printBranchToBlock(const BasicBlock* CurrBB,
163 const BasicBlock* TrueBB,
164 const BasicBlock* FalseBB);
166 void printBranchInstruction(const BranchInst* Inst);
168 void printSelectInstruction(const Value* Cond, const Value* VTrue,
169 const Value* VFalse);
171 void printIndirectLoad(const Value* V);
173 void printStoreInstruction(const Instruction* Inst);
175 void printCastInstruction(unsigned int Op, const Value* V,
178 void printGepInstruction(const Value* V, gep_type_iterator I,
179 gep_type_iterator E);
181 std::string getCallSignature(const FunctionType* Ty,
182 const Instruction* Inst,
185 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
187 void printCallInstruction(const Instruction* Inst);
189 void printICmpInstruction(unsigned Predicate, const Value* Left,
192 void printFCmpInstruction(unsigned Predicate, const Value* Left,
195 void printInvokeInstruction(const InvokeInst* Inst);
197 void printSwitchInstruction(const SwitchInst* Inst);
199 void printInstruction(const Instruction* Inst);
201 void printLoop(const Loop* L);
203 void printBasicBlock(const BasicBlock* BB);
205 void printLocalVariables(const Function& F);
207 void printFunctionBody(const Function& F);
209 void printConstantExpr(const ConstantExpr* CE);
211 void printStaticInitializerList();
213 void printFunction(const Function& F);
215 void printDeclarations(const TypeSymbolTable& ST);
217 unsigned int getBitWidth(const Type* Ty);
219 void printStaticConstant(const Constant* C, uint64_t& Offset);
221 void printStaticInitializer(const Constant* C, const std::string& Name);
223 void printVariableDefinition(const GlobalVariable* G);
225 void printGlobalVariables();
227 void printExternals();