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"
35 class MSILModule
: public ModulePass
{
37 const std::set
<const Type
*>*& UsedTypes
;
38 const TargetData
*& TD
;
42 MSILModule(const std::set
<const Type
*>*& _UsedTypes
,
43 const TargetData
*& _TD
)
44 : ModulePass(&ID
), UsedTypes(_UsedTypes
), TD(_TD
) {}
46 void getAnalysisUsage(AnalysisUsage
&AU
) const {
47 AU
.addRequired
<FindUsedTypes
>();
48 AU
.addRequired
<TargetData
>();
51 virtual const char *getPassName() const {
52 return "MSIL backend definitions";
55 virtual bool runOnModule(Module
&M
);
59 class MSILWriter
: public FunctionPass
{
60 struct StaticInitializer
{
61 const Constant
* constant
;
65 : constant(0), offset(0) {}
67 StaticInitializer(const Constant
* _constant
, uint64_t _offset
)
68 : constant(_constant
), offset(_offset
) {}
73 uint64_t getUniqID() {
83 std::vector
<StaticInitializer
>* InitListPtr
;
84 std::map
<const GlobalVariable
*,std::vector
<StaticInitializer
> >
86 const std::set
<const Type
*>* UsedTypes
;
88 MSILWriter(raw_ostream
&o
) : FunctionPass(&ID
), Out(o
) {
102 bool isVariable(ValueType V
) {
103 return V
==GlobalVT
|| V
==InternalVT
|| V
==ArgumentVT
|| V
==LocalVT
;
106 bool isConstValue(ValueType V
) {
107 return V
==ConstVT
|| V
==ConstExprVT
;
110 virtual const char *getPassName() const { return "MSIL backend"; }
112 void getAnalysisUsage(AnalysisUsage
&AU
) const {
113 AU
.addRequired
<LoopInfo
>();
114 AU
.setPreservesAll();
117 bool runOnFunction(Function
&F
);
119 virtual bool doInitialization(Module
&M
);
121 virtual bool doFinalization(Module
&M
);
123 void printModuleStartup();
125 bool isZeroValue(const Value
* V
);
127 std::string
getValueName(const Value
* V
);
129 std::string
getLabelName(const Value
* V
);
131 std::string
getLabelName(const std::string
& Name
);
133 std::string
getConvModopt(unsigned CallingConvID
);
135 std::string
getArrayTypeName(Type::TypeID TyID
, const Type
* Ty
);
137 std::string
getPrimitiveTypeName(const Type
* Ty
, bool isSigned
);
139 std::string
getFunctionTypeName(const Type
* Ty
);
141 std::string
getPointerTypeName(const Type
* Ty
);
143 std::string
getTypeName(const Type
* Ty
, bool isSigned
= false,
144 bool isNested
= false);
146 ValueType
getValueLocation(const Value
* V
);
148 std::string
getTypePostfix(const Type
* Ty
, bool Expand
,
149 bool isSigned
= false);
151 void printConvToPtr();
153 void printPtrLoad(uint64_t N
);
155 void printValuePtrLoad(const Value
* V
);
157 void printConstLoad(const Constant
* C
);
159 void printValueLoad(const Value
* V
);
161 void printValueSave(const Value
* V
);
163 void printBinaryInstruction(const char* Name
, const Value
* Left
,
166 void printSimpleInstruction(const char* Inst
, const char* Operand
= NULL
);
168 void printPHICopy(const BasicBlock
* Src
, const BasicBlock
* Dst
);
170 void printBranchToBlock(const BasicBlock
* CurrBB
,
171 const BasicBlock
* TrueBB
,
172 const BasicBlock
* FalseBB
);
174 void printBranchInstruction(const BranchInst
* Inst
);
176 void printSelectInstruction(const Value
* Cond
, const Value
* VTrue
,
177 const Value
* VFalse
);
179 void printIndirectLoad(const Value
* V
);
181 void printIndirectSave(const Value
* Ptr
, const Value
* Val
);
183 void printIndirectSave(const Type
* Ty
);
185 void printCastInstruction(unsigned int Op
, const Value
* V
,
188 void printGepInstruction(const Value
* V
, gep_type_iterator I
,
189 gep_type_iterator E
);
191 std::string
getCallSignature(const FunctionType
* Ty
,
192 const Instruction
* Inst
,
195 void printFunctionCall(const Value
* FnVal
, const Instruction
* Inst
);
197 void printIntrinsicCall(const IntrinsicInst
* Inst
);
199 void printCallInstruction(const Instruction
* Inst
);
201 void printICmpInstruction(unsigned Predicate
, const Value
* Left
,
204 void printFCmpInstruction(unsigned Predicate
, const Value
* Left
,
207 void printInvokeInstruction(const InvokeInst
* Inst
);
209 void printSwitchInstruction(const SwitchInst
* Inst
);
211 void printVAArgInstruction(const VAArgInst
* Inst
);
213 void printAllocaInstruction(const AllocaInst
* Inst
);
215 void printInstruction(const Instruction
* Inst
);
217 void printLoop(const Loop
* L
);
219 void printBasicBlock(const BasicBlock
* BB
);
221 void printLocalVariables(const Function
& F
);
223 void printFunctionBody(const Function
& F
);
225 void printConstantExpr(const ConstantExpr
* CE
);
227 void printStaticInitializerList();
229 void printFunction(const Function
& F
);
231 void printDeclarations(const TypeSymbolTable
& ST
);
233 unsigned int getBitWidth(const Type
* Ty
);
235 void printStaticConstant(const Constant
* C
, uint64_t& Offset
);
237 void printStaticInitializer(const Constant
* C
, const std::string
& Name
);
239 void printVariableDefinition(const GlobalVariable
* G
);
241 void printGlobalVariables();
243 const char* getLibraryName(const Function
* F
);
245 const char* getLibraryName(const GlobalVariable
* GV
);
247 const char* getLibraryForSymbol(const char* Name
, bool isFunction
,
248 unsigned CallingConv
);
250 void printExternals();