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/CallingConv.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Pass.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Analysis/FindUsedTypes.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/GetElementPtrTypeIterator.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Support/Mangler.h"
33 extern Target TheMSILTarget
;
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() {
78 formatted_raw_ostream
&Out
;
83 std::vector
<StaticInitializer
>* InitListPtr
;
84 std::map
<const GlobalVariable
*,std::vector
<StaticInitializer
> >
86 const std::set
<const Type
*>* UsedTypes
;
88 DenseMap
<const Value
*, unsigned> AnonValueNumbers
;
89 unsigned NextAnonValueNumber
;
91 MSILWriter(formatted_raw_ostream
&o
) : FunctionPass(&ID
), Out(o
),
92 NextAnonValueNumber(0) {
106 bool isVariable(ValueType V
) {
107 return V
==GlobalVT
|| V
==InternalVT
|| V
==ArgumentVT
|| V
==LocalVT
;
110 bool isConstValue(ValueType V
) {
111 return V
==ConstVT
|| V
==ConstExprVT
;
114 virtual const char *getPassName() const { return "MSIL backend"; }
116 void getAnalysisUsage(AnalysisUsage
&AU
) const {
117 AU
.addRequired
<LoopInfo
>();
118 AU
.setPreservesAll();
121 bool runOnFunction(Function
&F
);
123 virtual bool doInitialization(Module
&M
);
125 virtual bool doFinalization(Module
&M
);
127 void printModuleStartup();
129 bool isZeroValue(const Value
* V
);
131 std::string
getValueName(const Value
* V
);
133 std::string
getLabelName(const Value
* V
);
135 std::string
getLabelName(const std::string
& Name
);
137 std::string
getConvModopt(CallingConv::ID CallingConvID
);
139 std::string
getArrayTypeName(Type::TypeID TyID
, const Type
* Ty
);
141 std::string
getPrimitiveTypeName(const Type
* Ty
, bool isSigned
);
143 std::string
getFunctionTypeName(const Type
* Ty
);
145 std::string
getPointerTypeName(const Type
* Ty
);
147 std::string
getTypeName(const Type
* Ty
, bool isSigned
= false,
148 bool isNested
= false);
150 ValueType
getValueLocation(const Value
* V
);
152 std::string
getTypePostfix(const Type
* Ty
, bool Expand
,
153 bool isSigned
= false);
155 void printConvToPtr();
157 void printPtrLoad(uint64_t N
);
159 void printValuePtrLoad(const Value
* V
);
161 void printConstLoad(const Constant
* C
);
163 void printValueLoad(const Value
* V
);
165 void printValueSave(const Value
* V
);
167 void printBinaryInstruction(const char* Name
, const Value
* Left
,
170 void printSimpleInstruction(const char* Inst
, const char* Operand
= NULL
);
172 void printPHICopy(const BasicBlock
* Src
, const BasicBlock
* Dst
);
174 void printBranchToBlock(const BasicBlock
* CurrBB
,
175 const BasicBlock
* TrueBB
,
176 const BasicBlock
* FalseBB
);
178 void printBranchInstruction(const BranchInst
* Inst
);
180 void printSelectInstruction(const Value
* Cond
, const Value
* VTrue
,
181 const Value
* VFalse
);
183 void printIndirectLoad(const Value
* V
);
185 void printIndirectSave(const Value
* Ptr
, const Value
* Val
);
187 void printIndirectSave(const Type
* Ty
);
189 void printCastInstruction(unsigned int Op
, const Value
* V
,
190 const Type
* Ty
, const Type
* SrcTy
=0);
192 void printGepInstruction(const Value
* V
, gep_type_iterator I
,
193 gep_type_iterator E
);
195 std::string
getCallSignature(const FunctionType
* Ty
,
196 const Instruction
* Inst
,
199 void printFunctionCall(const Value
* FnVal
, const Instruction
* Inst
);
201 void printIntrinsicCall(const IntrinsicInst
* Inst
);
203 void printCallInstruction(const Instruction
* Inst
);
205 void printICmpInstruction(unsigned Predicate
, const Value
* Left
,
208 void printFCmpInstruction(unsigned Predicate
, const Value
* Left
,
211 void printInvokeInstruction(const InvokeInst
* Inst
);
213 void printSwitchInstruction(const SwitchInst
* Inst
);
215 void printVAArgInstruction(const VAArgInst
* Inst
);
217 void printAllocaInstruction(const AllocaInst
* Inst
);
219 void printInstruction(const Instruction
* Inst
);
221 void printLoop(const Loop
* L
);
223 void printBasicBlock(const BasicBlock
* BB
);
225 void printLocalVariables(const Function
& F
);
227 void printFunctionBody(const Function
& F
);
229 void printConstantExpr(const ConstantExpr
* CE
);
231 void printStaticInitializerList();
233 void printFunction(const Function
& F
);
235 void printDeclarations(const TypeSymbolTable
& ST
);
237 unsigned int getBitWidth(const Type
* Ty
);
239 void printStaticConstant(const Constant
* C
, uint64_t& Offset
);
241 void printStaticInitializer(const Constant
* C
, const std::string
& Name
);
243 void printVariableDefinition(const GlobalVariable
* G
);
245 void printGlobalVariables();
247 const char* getLibraryName(const Function
* F
);
249 const char* getLibraryName(const GlobalVariable
* GV
);
251 const char* getLibraryForSymbol(const StringRef
&Name
, bool isFunction
,
252 CallingConv::ID CallingConv
);
254 void printExternals();