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;
40 MSILModule(const std::set<const Type *>*& _UsedTypes,
41 const TargetData*& _TD)
42 : UsedTypes(_UsedTypes), TD(_TD) {}
44 void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addRequired<FindUsedTypes>();
46 AU.addRequired<TargetData>();
49 virtual const char *getPassName() const {
50 return "MSIL backend definitions";
53 virtual bool runOnModule(Module &M);
57 class MSILWriter : public FunctionPass {
58 struct StaticInitializer {
59 const Constant* constant;
63 : constant(0), offset(0) {}
65 StaticInitializer(const Constant* _constant, uint64_t _offset)
66 : constant(_constant), offset(_offset) {}
71 uint64_t getUniqID() {
81 std::vector<StaticInitializer>* InitListPtr;
82 std::map<const GlobalVariable*,std::vector<StaticInitializer> >
84 const std::set<const Type *>* UsedTypes;
86 MSILWriter(std::ostream &o) : Out(o) {
100 bool isVariable(ValueType V) {
101 return V==GlobalVT || V==InternalVT || V==ArgumentVT || V==LocalVT;
104 bool isConstValue(ValueType V) {
105 return V==ConstVT || V==ConstExprVT;
108 virtual const char *getPassName() const { return "MSIL backend"; }
110 void getAnalysisUsage(AnalysisUsage &AU) const {
111 AU.addRequired<LoopInfo>();
112 AU.setPreservesAll();
115 bool runOnFunction(Function &F);
117 virtual bool doInitialization(Module &M);
119 virtual bool doFinalization(Module &M);
121 bool isZeroValue(const Value* V);
123 std::string getValueName(const Value* V);
125 std::string getLabelName(const Value* V);
127 std::string getLabelName(const std::string& Name);
129 std::string getConvModopt(unsigned CallingConvID);
131 std::string getArrayTypeName(Type::TypeID TyID, const Type* Ty);
133 std::string getPrimitiveTypeName(const Type* Ty, bool isSigned);
135 std::string getFunctionTypeName(const Type* Ty);
137 std::string getPointerTypeName(const Type* Ty);
139 std::string getTypeName(const Type* Ty, bool isSigned = false);
141 ValueType getValueLocation(const Value* V);
143 std::string getTypePostfix(const Type* Ty, bool Expand,
144 bool isSigned = false);
146 void printPtrLoad(uint64_t N);
148 void printConstLoad(const Constant* C);
150 void printValueLoad(const Value* V);
152 void printValueSave(const Value* V);
154 void printBinaryInstruction(const char* Name, const Value* Left,
157 void printSimpleInstruction(const char* Inst, const char* Operand = NULL);
159 void printPHICopy(const BasicBlock* Src, const BasicBlock* Dst);
161 void printBranchToBlock(const BasicBlock* CurrBB,
162 const BasicBlock* TrueBB,
163 const BasicBlock* FalseBB);
165 void printBranchInstruction(const BranchInst* Inst);
167 void printSelectInstruction(const Value* Cond, const Value* VTrue,
168 const Value* VFalse);
170 void printIndirectLoad(const Value* V);
172 void printStoreInstruction(const Instruction* Inst);
174 void printCastInstruction(unsigned int Op, const Value* V,
177 void printGepInstruction(const Value* V, gep_type_iterator I,
178 gep_type_iterator E);
180 std::string getCallSignature(const FunctionType* Ty,
181 const Instruction* Inst,
184 void printFunctionCall(const Value* FnVal, const Instruction* Inst);
186 void printCallInstruction(const Instruction* Inst);
188 void printICmpInstruction(unsigned Predicate, const Value* Left,
191 void printFCmpInstruction(unsigned Predicate, const Value* Left,
194 void printInvokeInstruction(const InvokeInst* Inst);
196 void printSwitchInstruction(const SwitchInst* Inst);
198 void printInstruction(const Instruction* Inst);
200 void printLoop(const Loop* L);
202 void printBasicBlock(const BasicBlock* BB);
204 void printLocalVariables(const Function& F);
206 void printFunctionBody(const Function& F);
208 void printConstantExpr(const ConstantExpr* CE);
210 void printStaticInitializerList();
212 void printFunction(const Function& F);
214 void printDeclarations(const TypeSymbolTable& ST);
216 unsigned int getBitWidth(const Type* Ty);
218 void printStaticConstant(const Constant* C, uint64_t& Offset);
220 void printStaticInitializer(const Constant* C, const std::string& Name);
222 void printVariableDefinition(const GlobalVariable* G);
224 void printGlobalVariables();
226 void printExternals();