blob: bab2904bd18d9ff8818da1903eddf2e212bd303c [file] [log] [blame]
Anton Korobeynikov099883f2007-03-21 21:38:25 +00001//===-- MSILWriter.cpp - Library for converting LLVM code to MSIL ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This library converts LLVM code to MSIL code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSILWriter.h"
15#include "llvm/CallingConv.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Intrinsics.h"
18#include "llvm/IntrinsicInst.h"
19#include "llvm/TypeSymbolTable.h"
20#include "llvm/Analysis/ConstantsScanner.h"
21#include "llvm/Support/CallSite.h"
22#include "llvm/Support/InstVisitor.h"
23#include "llvm/Transforms/Scalar.h"
24#include "llvm/ADT/StringExtras.h"
25
26namespace {
27 // TargetMachine for the MSIL
28 struct VISIBILITY_HIDDEN MSILTarget : public TargetMachine {
29 const TargetData DataLayout; // Calculates type size & alignment
30
31 MSILTarget(const Module &M, const std::string &FS)
32 : DataLayout(&M) {}
33
34 virtual bool WantsWholeFile() const { return true; }
35 virtual bool addPassesToEmitWholeFile(PassManager &PM, std::ostream &Out,
36 CodeGenFileType FileType, bool Fast);
37
38 // This class always works, but shouldn't be the default in most cases.
39 static unsigned getModuleMatchQuality(const Module &M) { return 1; }
40
41 virtual const TargetData *getTargetData() const { return &DataLayout; }
42 };
43}
44
45
46RegisterTarget<MSILTarget> X("msil", " MSIL backend");
47
48bool MSILModule::runOnModule(Module &M) {
49 ModulePtr = &M;
50 TD = &getAnalysis<TargetData>();
51 bool Changed = false;
52 // Find named types.
53 TypeSymbolTable& Table = M.getTypeSymbolTable();
54 std::set<const Type *> Types = getAnalysis<FindUsedTypes>().getTypes();
55 for (TypeSymbolTable::iterator I = Table.begin(), E = Table.end(); I!=E; ) {
56 if (!isa<StructType>(I->second) && !isa<OpaqueType>(I->second))
57 Table.remove(I++);
58 else {
59 std::set<const Type *>::iterator T = Types.find(I->second);
60 if (T==Types.end())
61 Table.remove(I++);
62 else {
63 Types.erase(T);
64 ++I;
65 }
66 }
67 }
68 // Find unnamed types.
69 unsigned RenameCounter = 0;
70 for (std::set<const Type *>::const_iterator I = Types.begin(),
71 E = Types.end(); I!=E; ++I)
72 if (const StructType *STy = dyn_cast<StructType>(*I)) {
73 while (ModulePtr->addTypeName("unnamed$"+utostr(RenameCounter), STy))
74 ++RenameCounter;
75 Changed = true;
76 }
77 // Pointer for FunctionPass.
78 UsedTypes = &getAnalysis<FindUsedTypes>().getTypes();
79 return Changed;
80}
81
82
83bool MSILWriter::runOnFunction(Function &F) {
84 if (F.isDeclaration()) return false;
85 LInfo = &getAnalysis<LoopInfo>();
86 printFunction(F);
87 return false;
88}
89
90
91bool MSILWriter::doInitialization(Module &M) {
92 ModulePtr = &M;
93 Mang = new Mangler(M);
94 Out << ".assembly extern mscorlib {}\n";
95 Out << ".assembly MSIL {}\n\n";
96 Out << "// External\n";
97 printExternals();
98 Out << "// Declarations\n";
99 printDeclarations(M.getTypeSymbolTable());
100 Out << "// Definitions\n";
101 printGlobalVariables();
102 return false;
103}
104
105
106bool MSILWriter::doFinalization(Module &M) {
107 delete Mang;
108 return false;
109}
110
111
112bool MSILWriter::isZeroValue(const Value* V) {
113 if (const Constant *C = dyn_cast<Constant>(V))
114 return C->isNullValue();
115 return false;
116}
117
118
119std::string MSILWriter::getValueName(const Value* V) {
120 // Name into the quotes allow control and space characters.
121 return "'"+Mang->getValueName(V)+"'";
122}
123
124
125std::string MSILWriter::getLabelName(const std::string& Name) {
126 if (Name.find('.')!=std::string::npos) {
127 std::string Tmp(Name);
128 // Replace unaccepable characters in the label name.
129 for (std::string::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I)
130 if (*I=='.') *I = '@';
131 return Tmp;
132 }
133 return Name;
134}
135
136
137std::string MSILWriter::getLabelName(const Value* V) {
138 return getLabelName(Mang->getValueName(V));
139}
140
141
142std::string MSILWriter::getConvModopt(unsigned CallingConvID) {
143 switch (CallingConvID) {
144 case CallingConv::C:
145 case CallingConv::Cold:
146 case CallingConv::Fast:
147 return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) ";
148 case CallingConv::X86_FastCall:
149 return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvFastcall) ";
150 case CallingConv::X86_StdCall:
151 return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvStdcall) ";
152 default:
153 cerr << "CallingConvID = " << CallingConvID << '\n';
154 assert(0 && "Unsupported calling convention");
155 }
156}
157
158
159std::string MSILWriter::getArrayTypeName(Type::TypeID TyID, const Type* Ty) {
160 std::string Tmp = "";
161 const Type* ElemTy = Ty;
162 assert(Ty->getTypeID()==TyID && "Invalid type passed");
163 // Walk trought array element types.
164 for (;;) {
165 // Multidimensional array.
166 if (ElemTy->getTypeID()==TyID) {
167 if (const ArrayType* ATy = dyn_cast<ArrayType>(ElemTy))
168 Tmp += utostr(ATy->getNumElements());
169 else if (const VectorType* VTy = dyn_cast<VectorType>(ElemTy))
170 Tmp += utostr(VTy->getNumElements());
171 ElemTy = cast<SequentialType>(ElemTy)->getElementType();
172 }
173 // Base element type found.
174 if (ElemTy->getTypeID()!=TyID) break;
175 Tmp += ",";
176 }
177 return getTypeName(ElemTy)+"["+Tmp+"]";
178}
179
180
181std::string MSILWriter::getPrimitiveTypeName(const Type* Ty, bool isSigned) {
182 unsigned NumBits = 0;
183 switch (Ty->getTypeID()) {
184 case Type::VoidTyID:
185 return "void ";
186 case Type::IntegerTyID:
187 NumBits = getBitWidth(Ty);
188 if(NumBits==1)
189 return "bool ";
190 if (!isSigned)
191 return "unsigned int"+utostr(NumBits)+" ";
192 return "int"+utostr(NumBits)+" ";
193 case Type::FloatTyID:
194 return "float32 ";
195 case Type::DoubleTyID:
196 return "float64 ";
197 default:
198 cerr << "Type = " << *Ty << '\n';
199 assert(0 && "Invalid primitive type");
200 }
201}
202
203
204std::string MSILWriter::getTypeName(const Type* Ty, bool isSigned) {
205 if (Ty->isPrimitiveType() || Ty->isInteger())
206 return getPrimitiveTypeName(Ty,isSigned);
207 // FIXME: "OpaqueType" support
208 switch (Ty->getTypeID()) {
209 case Type::PointerTyID:
210 return "void* ";
211 case Type::StructTyID:
212 return "valuetype '"+ModulePtr->getTypeName(Ty)+"' ";
213 case Type::ArrayTyID:
214 return "valuetype '"+getArrayTypeName(Ty->getTypeID(),Ty)+"' ";
215 case Type::VectorTyID:
216 return "valuetype '"+getArrayTypeName(Ty->getTypeID(),Ty)+"' ";
217 default:
218 cerr << "Type = " << *Ty << '\n';
219 assert(0 && "Invalid type in getTypeName()");
220 }
221}
222
223
224MSILWriter::ValueType MSILWriter::getValueLocation(const Value* V) {
225 // Function argument
226 if (isa<Argument>(V))
227 return ArgumentVT;
228 // Function
229 else if (const Function* F = dyn_cast<Function>(V))
230 return F->hasInternalLinkage() ? InternalVT : GlobalVT;
231 // Variable
232 else if (const GlobalVariable* G = dyn_cast<GlobalVariable>(V))
233 return G->hasInternalLinkage() ? InternalVT : GlobalVT;
234 // Constant
235 else if (isa<Constant>(V))
236 return isa<ConstantExpr>(V) ? ConstExprVT : ConstVT;
237 // Local variable
238 return LocalVT;
239}
240
241
242std::string MSILWriter::getTypePostfix(const Type* Ty, bool Expand,
243 bool isSigned) {
244 unsigned NumBits = 0;
245 switch (Ty->getTypeID()) {
246 // Integer constant, expanding for stack operations.
247 case Type::IntegerTyID:
248 NumBits = getBitWidth(Ty);
249 // Expand integer value to "int32" or "int64".
250 if (Expand) return (NumBits<=32 ? "i4" : "i8");
251 if (NumBits==1) return "i1";
252 return (isSigned ? "i" : "u")+utostr(NumBits/8);
253 // Float constant.
254 case Type::FloatTyID:
255 return "r4";
256 case Type::DoubleTyID:
257 return "r8";
258 case Type::PointerTyID:
259 return "i"+utostr(TD->getTypeSize(Ty));
260 default:
261 cerr << "TypeID = " << Ty->getTypeID() << '\n';
262 assert(0 && "Invalid type in TypeToPostfix()");
263 }
264}
265
266
267void MSILWriter::printPtrLoad(uint64_t N) {
268 switch (ModulePtr->getPointerSize()) {
269 case Module::Pointer32:
270 printSimpleInstruction("ldc.i4",utostr(N).c_str());
271 // FIXME: Need overflow test?
272 assert(N<0xFFFFFFFF && "32-bit pointer overflowed");
273 break;
274 case Module::Pointer64:
275 printSimpleInstruction("ldc.i8",utostr(N).c_str());
276 break;
277 default:
278 assert(0 && "Module use not supporting pointer size");
279 }
280}
281
282
283void MSILWriter::printConstLoad(const Constant* C) {
284 if (const ConstantInt* CInt = dyn_cast<ConstantInt>(C)) {
285 // Integer constant
286 Out << "\tldc." << getTypePostfix(C->getType(),true) << '\t';
287 if (CInt->isMinValue(true))
288 Out << CInt->getSExtValue();
289 else
290 Out << CInt->getZExtValue();
291 } else if (const ConstantFP* CFp = dyn_cast<ConstantFP>(C)) {
292 // Float constant
293 Out << "\tldc." << getTypePostfix(C->getType(),true) << '\t' <<
294 CFp->getValue();
295 } else {
296 cerr << "Constant = " << *C << '\n';
297 assert(0 && "Invalid constant value");
298 }
299 Out << '\n';
300}
301
302
303void MSILWriter::printValueLoad(const Value* V) {
304 switch (getValueLocation(V)) {
305 // Global variable or function address.
306 case GlobalVT:
307 case InternalVT:
308 if (const Function* F = dyn_cast<Function>(V)) {
309 std::string Name = getConvModopt(F->getCallingConv())+getValueName(F);
310 printSimpleInstruction("ldftn",
311 getCallSignature(F->getFunctionType(),NULL,Name).c_str());
312 } else {
313 const Type* ElemTy = cast<PointerType>(V->getType())->getElementType();
314 std::string Tmp = getTypeName(ElemTy)+getValueName(V);
315 printSimpleInstruction("ldsflda",Tmp.c_str());
316 }
317 break;
318 // Function argument.
319 case ArgumentVT:
320 printSimpleInstruction("ldarg",getValueName(V).c_str());
321 break;
322 // Local function variable.
323 case LocalVT:
324 printSimpleInstruction("ldloc",getValueName(V).c_str());
325 break;
326 // Constant value.
327 case ConstVT:
328 if (isa<ConstantPointerNull>(V))
329 printPtrLoad(0);
330 else
331 printConstLoad(cast<Constant>(V));
332 break;
333 // Constant expression.
334 case ConstExprVT:
335 printConstantExpr(cast<ConstantExpr>(V));
336 break;
337 default:
338 cerr << "Value = " << *V << '\n';
339 assert(0 && "Invalid value location");
340 }
341}
342
343
344void MSILWriter::printValueSave(const Value* V) {
345 switch (getValueLocation(V)) {
346 case ArgumentVT:
347 printSimpleInstruction("starg",getValueName(V).c_str());
348 break;
349 case LocalVT:
350 printSimpleInstruction("stloc",getValueName(V).c_str());
351 break;
352 default:
353 cerr << "Value = " << *V << '\n';
354 assert(0 && "Invalid value location");
355 }
356}
357
358
359void MSILWriter::printBinaryInstruction(const char* Name, const Value* Left,
360 const Value* Right) {
361 printValueLoad(Left);
362 printValueLoad(Right);
363 Out << '\t' << Name << '\n';
364}
365
366
367void MSILWriter::printSimpleInstruction(const char* Inst, const char* Operand) {
368 if(Operand)
369 Out << '\t' << Inst << '\t' << Operand << '\n';
370 else
371 Out << '\t' << Inst << '\n';
372}
373
374
375void MSILWriter::printPHICopy(const BasicBlock* Src, const BasicBlock* Dst) {
376 for (BasicBlock::const_iterator I = Dst->begin(), E = Dst->end();
377 isa<PHINode>(I); ++I) {
378 const PHINode* Phi = cast<PHINode>(I);
379 const Value* Val = Phi->getIncomingValueForBlock(Src);
380 if (isa<UndefValue>(Val)) continue;
381 printValueLoad(Val);
382 printValueSave(Phi);
383 }
384}
385
386
387void MSILWriter::printBranchToBlock(const BasicBlock* CurrBB,
388 const BasicBlock* TrueBB,
389 const BasicBlock* FalseBB) {
390 if (TrueBB==FalseBB) {
391 // "TrueBB" and "FalseBB" destination equals
392 printPHICopy(CurrBB,TrueBB);
393 printSimpleInstruction("pop");
394 printSimpleInstruction("br",getLabelName(TrueBB).c_str());
395 } else if (FalseBB==NULL) {
396 // If "FalseBB" not used the jump have condition
397 printPHICopy(CurrBB,TrueBB);
398 printSimpleInstruction("brtrue",getLabelName(TrueBB).c_str());
399 } else if (TrueBB==NULL) {
400 // If "TrueBB" not used the jump is unconditional
401 printPHICopy(CurrBB,FalseBB);
402 printSimpleInstruction("br",getLabelName(FalseBB).c_str());
403 } else {
404 // Copy PHI instructions for each block
405 std::string TmpLabel;
406 // Print PHI instructions for "TrueBB"
407 if (isa<PHINode>(TrueBB->begin())) {
408 TmpLabel = getLabelName(TrueBB)+"$phi_"+utostr(getUniqID());
409 printSimpleInstruction("brtrue",TmpLabel.c_str());
410 } else {
411 printSimpleInstruction("brtrue",getLabelName(TrueBB).c_str());
412 }
413 // Print PHI instructions for "FalseBB"
414 if (isa<PHINode>(FalseBB->begin())) {
415 printPHICopy(CurrBB,FalseBB);
416 printSimpleInstruction("br",getLabelName(FalseBB).c_str());
417 } else {
418 printSimpleInstruction("br",getLabelName(FalseBB).c_str());
419 }
420 if (isa<PHINode>(TrueBB->begin())) {
421 // Handle "TrueBB" PHI Copy
422 Out << TmpLabel << ":\n";
423 printPHICopy(CurrBB,TrueBB);
424 printSimpleInstruction("br",getLabelName(TrueBB).c_str());
425 }
426 }
427}
428
429
430void MSILWriter::printBranchInstruction(const BranchInst* Inst) {
431 if (Inst->isUnconditional()) {
432 printBranchToBlock(Inst->getParent(),NULL,Inst->getSuccessor(0));
433 } else {
434 printValueLoad(Inst->getCondition());
435 printBranchToBlock(Inst->getParent(),Inst->getSuccessor(0),
436 Inst->getSuccessor(1));
437 }
438}
439
440
441void MSILWriter::printSelectInstruction(const Value* Cond, const Value* VTrue,
442 const Value* VFalse) {
443 std::string TmpLabel = std::string("select$true_")+utostr(getUniqID());
444 printValueLoad(VTrue);
445 printValueLoad(Cond);
446 printSimpleInstruction("brtrue",TmpLabel.c_str());
447 printSimpleInstruction("pop");
448 printValueLoad(VFalse);
449 Out << TmpLabel << ":\n";
450}
451
452
453void MSILWriter::printIndirectLoad(const Value* V) {
454 printValueLoad(V);
455 std::string Tmp = "ldind."+getTypePostfix(V->getType(),false);
456 printSimpleInstruction(Tmp.c_str());
457}
458
459
460void MSILWriter::printStoreInstruction(const Instruction* Inst) {
461 const Value* Val = Inst->getOperand(0);
462 const Value* Ptr = Inst->getOperand(1);
463 // Load destination address.
464 printValueLoad(Ptr);
465 // Load value.
466 printValueLoad(Val);
467 // Instruction need signed postfix for any type.
468 std::string postfix = getTypePostfix(Val->getType(),false);
469 if (*postfix.begin()=='u') *postfix.begin() = 'i';
470 postfix = "stind."+postfix;
471 printSimpleInstruction(postfix.c_str());
472}
473
474
475void MSILWriter::printCastInstruction(unsigned int Op, const Value* V,
476 const Type* Ty) {
477 std::string Tmp("");
478 printValueLoad(V);
479 switch (Op) {
480 // Signed
481 case Instruction::SExt:
482 case Instruction::SIToFP:
483 case Instruction::FPToSI:
484 Tmp = "conv."+getTypePostfix(Ty,false,true);
485 printSimpleInstruction(Tmp.c_str());
486 break;
487 // Unsigned
488 case Instruction::FPTrunc:
489 case Instruction::FPExt:
490 case Instruction::UIToFP:
491 case Instruction::Trunc:
492 case Instruction::ZExt:
493 case Instruction::FPToUI:
494 case Instruction::PtrToInt:
495 case Instruction::IntToPtr:
496 Tmp = "conv."+getTypePostfix(Ty,false);
497 printSimpleInstruction(Tmp.c_str());
498 break;
499 // Do nothing
500 case Instruction::BitCast:
501 // FIXME: meaning that ld*/st* instruction do not change data format.
502 break;
503 default:
504 cerr << "Opcode = " << Op << '\n';
505 assert(0 && "Invalid conversion instruction");
506 }
507}
508
509
510void MSILWriter::printGepInstruction(const Value* V, gep_type_iterator I,
511 gep_type_iterator E) {
512 // Load address
513 printValueLoad(V);
514 // Calculate element offset.
515 unsigned TySize;
516 for (++I; I!=E; ++I){
517 const Type* Ty = I.getIndexedType();
518 const Value* Idx = I.getOperand();
519 // Get size of type.
520 switch (Ty->getTypeID()) {
521 case Type::IntegerTyID:
522 case Type::FloatTyID:
523 case Type::DoubleTyID:
524 case Type::PointerTyID:
525 TySize = TD->getTypeSize(Ty);
526 break;
527 case Type::StructTyID:
528 TySize = 0;
529 break;
530 case Type::ArrayTyID:
531 TySize = TD->getTypeSize(cast<ArrayType>(Ty)->getElementType());
532 break;
533 case Type::VectorTyID:
534 TySize = TD->getTypeSize(cast<VectorType>(Ty)->getElementType());
535 break;
536 default:
537 cerr << "Type = " << *Ty << '\n';
538 assert(0 && "Invalid index type in printGepInstruction()");
539 }
540 // Calculate offset to structure field.
541 if (const StructType* STy = dyn_cast<StructType>(Ty)) {
542 TySize = 0;
543 uint64_t FieldIdx = cast<ConstantInt>(Idx)->getZExtValue();
544 // Offset is the summ of all previous structure fields.
545 for (uint64_t F = 0; F<FieldIdx; ++F)
546 TySize += TD->getTypeSize(STy->getContainedType(unsigned(F)));
547 // Add field offset to stack top.
548 printPtrLoad(TySize);
549 printSimpleInstruction("add");
550 continue;
551 }
552 // Add offset of current element to stack top.
553 if (!isZeroValue(Idx)) {
554 uint64_t TySize = TD->getTypeSize(I.getIndexedType());
555 // Constant optimization
556 if (const ConstantInt* CInt = dyn_cast<ConstantInt>(Idx)) {
557 printPtrLoad(CInt->getZExtValue()*TySize);
558 } else {
559 printPtrLoad(TySize);
560 printValueLoad(Idx);
561 printSimpleInstruction("mul");
562 }
563 printSimpleInstruction("add");
564 }
565 }
566}
567
568
569std::string MSILWriter::getCallSignature(const FunctionType* Ty,
570 const Instruction* Inst,
571 std::string Name) {
572 std::string Tmp = "";
573 if (Ty->isVarArg()) Tmp += "vararg ";
574 // Name and return type.
575 Tmp += getTypeName(Ty->getReturnType())+Name+"(";
576 // Function argument type list.
577 unsigned NumParams = Ty->getNumParams();
578 for (unsigned I = 0; I!=NumParams; ++I) {
579 if (I!=0) Tmp += ",";
580 Tmp += getTypeName(Ty->getParamType(I));
581 }
582 // CLR needs to know the exact amount of parameters received by vararg
583 // function, because caller cleans the stack.
584 if (Ty->isVarArg() && Inst) {
585 // Origin to function arguments in "CallInst" or "InvokeInst"
586 unsigned Org = isa<InvokeInst>(Inst) ? 3 : 1;
587 // Print variable argument types.
588 unsigned NumOperands = Inst->getNumOperands()-Org;
589 if (NumParams<NumOperands) {
590 if (NumParams!=0) Tmp += ", ";
591 Tmp += "... , ";
592 for (unsigned J = NumParams; J!=NumOperands; ++J) {
593 if (J!=NumParams) Tmp += ", ";
594 Tmp += getTypeName(Inst->getOperand(J+Org)->getType());
595 }
596 }
597 }
598 return Tmp+")";
599}
600
601
602void MSILWriter::printFunctionCall(const Value* FnVal,
603 const Instruction* Inst) {
604 // Get function calling convention
605 std::string Name = "";
606 if (const CallInst* Call = dyn_cast<CallInst>(Inst))
607 Name = getConvModopt(Call->getCallingConv());
608 else if (const InvokeInst* Invoke = dyn_cast<InvokeInst>(Inst))
609 Name = getConvModopt(Invoke->getCallingConv());
610 else {
611 cerr << "Instruction = " << Inst->getName() << '\n';
612 assert(0 && "Need \"Invoke\" or \"Call\" instruction only");
613 }
614
615 if (const Function* F = dyn_cast<Function>(FnVal)) {
616 // Direct call
617 Name += getValueName(F);
618 printSimpleInstruction("call",
619 getCallSignature(F->getFunctionType(),Inst,Name).c_str());
620 } else {
621 // Indirect function call
622 const PointerType* PTy = cast<PointerType>(FnVal->getType());
623 const FunctionType* FTy = cast<FunctionType>(PTy->getElementType());
624 // Load function address
625 printValueLoad(FnVal);
626 printSimpleInstruction("calli",getCallSignature(FTy,Inst,Name).c_str());
627 }
628}
629
630
631void MSILWriter::printCallInstruction(const Instruction* Inst) {
632 // Load arguments to stack
633 for (int I = 1, E = Inst->getNumOperands(); I!=E; ++I)
634 printValueLoad(Inst->getOperand(I));
635 printFunctionCall(Inst->getOperand(0),Inst);
636}
637
638
639void MSILWriter::printICmpInstruction(unsigned Predicate, const Value* Left,
640 const Value* Right) {
641 switch (Predicate) {
642 case ICmpInst::ICMP_EQ:
643 printBinaryInstruction("ceq",Left,Right);
644 break;
645 case ICmpInst::ICMP_NE:
646 // Emulate = not (Op1 eq Op2)
647 printBinaryInstruction("ceq",Left,Right);
648 printSimpleInstruction("not");
649 break;
650 case ICmpInst::ICMP_ULE:
651 case ICmpInst::ICMP_SLE:
652 // Emulate = (Op1 eq Op2) or (Op1 lt Op2)
653 printBinaryInstruction("ceq",Left,Right);
654 if (Predicate==ICmpInst::ICMP_ULE)
655 printBinaryInstruction("clt.un",Left,Right);
656 else
657 printBinaryInstruction("clt",Left,Right);
658 printSimpleInstruction("or");
659 break;
660 case ICmpInst::ICMP_UGE:
661 case ICmpInst::ICMP_SGE:
662 // Emulate = (Op1 eq Op2) or (Op1 gt Op2)
663 printBinaryInstruction("ceq",Left,Right);
664 if (Predicate==ICmpInst::ICMP_UGE)
665 printBinaryInstruction("cgt.un",Left,Right);
666 else
667 printBinaryInstruction("cgt",Left,Right);
668 printSimpleInstruction("or");
669 break;
670 case ICmpInst::ICMP_ULT:
671 printBinaryInstruction("clt.un",Left,Right);
672 break;
673 case ICmpInst::ICMP_SLT:
674 printBinaryInstruction("clt",Left,Right);
675 break;
676 case ICmpInst::ICMP_UGT:
677 printBinaryInstruction("cgt.un",Left,Right);
678 case ICmpInst::ICMP_SGT:
679 printBinaryInstruction("cgt",Left,Right);
680 break;
681 default:
682 cerr << "Predicate = " << Predicate << '\n';
683 assert(0 && "Invalid icmp predicate");
684 }
685}
686
687
688void MSILWriter::printFCmpInstruction(unsigned Predicate, const Value* Left,
689 const Value* Right) {
690 // FIXME: Correct comparison
691 std::string NanFunc = "bool [mscorlib]System.Double::IsNaN(float64)";
692 switch (Predicate) {
693 case FCmpInst::FCMP_UGT:
694 // X > Y || llvm_fcmp_uno(X, Y)
695 printBinaryInstruction("cgt",Left,Right);
696 printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right);
697 printSimpleInstruction("or");
698 break;
699 case FCmpInst::FCMP_OGT:
700 // X > Y
701 printBinaryInstruction("cgt",Left,Right);
702 break;
703 case FCmpInst::FCMP_UGE:
704 // X >= Y || llvm_fcmp_uno(X, Y)
705 printBinaryInstruction("ceq",Left,Right);
706 printBinaryInstruction("cgt",Left,Right);
707 printSimpleInstruction("or");
708 printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right);
709 printSimpleInstruction("or");
710 break;
711 case FCmpInst::FCMP_OGE:
712 // X >= Y
713 printBinaryInstruction("ceq",Left,Right);
714 printBinaryInstruction("cgt",Left,Right);
715 printSimpleInstruction("or");
716 break;
717 case FCmpInst::FCMP_ULT:
718 // X < Y || llvm_fcmp_uno(X, Y)
719 printBinaryInstruction("clt",Left,Right);
720 printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right);
721 printSimpleInstruction("or");
722 break;
723 case FCmpInst::FCMP_OLT:
724 // X < Y
725 printBinaryInstruction("clt",Left,Right);
726 break;
727 case FCmpInst::FCMP_ULE:
728 // X <= Y || llvm_fcmp_uno(X, Y)
729 printBinaryInstruction("ceq",Left,Right);
730 printBinaryInstruction("clt",Left,Right);
731 printSimpleInstruction("or");
732 printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right);
733 printSimpleInstruction("or");
734 break;
735 case FCmpInst::FCMP_OLE:
736 // X <= Y
737 printBinaryInstruction("ceq",Left,Right);
738 printBinaryInstruction("clt",Left,Right);
739 printSimpleInstruction("or");
740 break;
741 case FCmpInst::FCMP_UEQ:
742 // X == Y || llvm_fcmp_uno(X, Y)
743 printBinaryInstruction("ceq",Left,Right);
744 printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right);
745 printSimpleInstruction("or");
746 break;
747 case FCmpInst::FCMP_OEQ:
748 // X == Y
749 printBinaryInstruction("ceq",Left,Right);
750 break;
751 case FCmpInst::FCMP_UNE:
752 // X != Y
753 printBinaryInstruction("ceq",Left,Right);
754 printSimpleInstruction("not");
755 break;
756 case FCmpInst::FCMP_ONE:
757 // X != Y && llvm_fcmp_ord(X, Y)
758 printBinaryInstruction("ceq",Left,Right);
759 printSimpleInstruction("not");
760 break;
761 case FCmpInst::FCMP_ORD:
762 // return X == X && Y == Y
763 printBinaryInstruction("ceq",Left,Left);
764 printBinaryInstruction("ceq",Right,Right);
765 printSimpleInstruction("or");
766 break;
767 case FCmpInst::FCMP_UNO:
768 // X != X || Y != Y
769 printBinaryInstruction("ceq",Left,Left);
770 printSimpleInstruction("not");
771 printBinaryInstruction("ceq",Right,Right);
772 printSimpleInstruction("not");
773 printSimpleInstruction("or");
774 break;
775 default:
776 assert(0 && "Illegal FCmp predicate");
777 }
778}
779
780
781void MSILWriter::printInvokeInstruction(const InvokeInst* Inst) {
782 std::string Label = "leave$normal_"+utostr(getUniqID());
783 Out << ".try {\n";
784 // Load arguments
785 for (int I = 3, E = Inst->getNumOperands(); I!=E; ++I)
786 printValueLoad(Inst->getOperand(I));
787 // Print call instruction
788 printFunctionCall(Inst->getOperand(0),Inst);
789 // Save function result and leave "try" block
790 printValueSave(Inst);
791 printSimpleInstruction("leave",Label.c_str());
792 Out << "}\n";
793 Out << "catch [mscorlib]System.Exception {\n";
794 // Redirect to unwind block
795 printSimpleInstruction("pop");
796 printBranchToBlock(Inst->getParent(),NULL,Inst->getUnwindDest());
797 Out << "}\n" << Label << ":\n";
798 // Redirect to continue block
799 printBranchToBlock(Inst->getParent(),NULL,Inst->getNormalDest());
800}
801
802
803void MSILWriter::printSwitchInstruction(const SwitchInst* Inst) {
804 // FIXME: Emulate with IL "switch" instruction
805 // Emulate = if () else if () else if () else ...
806 for (unsigned int I = 1, E = Inst->getNumCases(); I!=E; ++I) {
807 printValueLoad(Inst->getCondition());
808 printValueLoad(Inst->getCaseValue(I));
809 printSimpleInstruction("ceq");
810 // Condition jump to successor block
811 printBranchToBlock(Inst->getParent(),Inst->getSuccessor(I),NULL);
812 }
813 // Jump to default block
814 printBranchToBlock(Inst->getParent(),NULL,Inst->getDefaultDest());
815}
816
817
818void MSILWriter::printInstruction(const Instruction* Inst) {
819 const Value *Left = 0, *Right = 0;
820 if (Inst->getNumOperands()>=1) Left = Inst->getOperand(0);
821 if (Inst->getNumOperands()>=2) Right = Inst->getOperand(1);
822 // Print instruction
823 // FIXME: "ShuffleVector","ExtractElement","InsertElement","VAArg" support.
824 switch (Inst->getOpcode()) {
825 // Terminator
826 case Instruction::Ret:
827 if (Inst->getNumOperands()) {
828 printValueLoad(Left);
829 printSimpleInstruction("ret");
830 } else
831 printSimpleInstruction("ret");
832 break;
833 case Instruction::Br:
834 printBranchInstruction(cast<BranchInst>(Inst));
835 break;
836 // Binary
837 case Instruction::Add:
838 printBinaryInstruction("add",Left,Right);
839 break;
840 case Instruction::Sub:
841 printBinaryInstruction("sub",Left,Right);
842 break;
843 case Instruction::Mul:
844 printBinaryInstruction("mul",Left,Right);
845 break;
846 case Instruction::UDiv:
847 printBinaryInstruction("div.un",Left,Right);
848 break;
849 case Instruction::SDiv:
850 case Instruction::FDiv:
851 printBinaryInstruction("div",Left,Right);
852 break;
853 case Instruction::URem:
854 printBinaryInstruction("rem.un",Left,Right);
855 break;
856 case Instruction::SRem:
857 case Instruction::FRem:
858 printBinaryInstruction("rem",Left,Right);
859 break;
860 // Binary Condition
861 case Instruction::ICmp:
862 printICmpInstruction(cast<ICmpInst>(Inst)->getPredicate(),Left,Right);
863 break;
864 case Instruction::FCmp:
865 printFCmpInstruction(cast<FCmpInst>(Inst)->getPredicate(),Left,Right);
866 break;
867 // Bitwise Binary
868 case Instruction::And:
869 printBinaryInstruction("and",Left,Right);
870 break;
871 case Instruction::Or:
872 printBinaryInstruction("or",Left,Right);
873 break;
874 case Instruction::Xor:
875 printBinaryInstruction("xor",Left,Right);
876 break;
877 case Instruction::Shl:
878 printBinaryInstruction("shl",Left,Right);
879 break;
880 case Instruction::LShr:
881 printBinaryInstruction("shr.un",Left,Right);
882 break;
883 case Instruction::AShr:
884 printBinaryInstruction("shr",Left,Right);
885 break;
886 case Instruction::Select:
887 printSelectInstruction(Inst->getOperand(0),Inst->getOperand(1),Inst->getOperand(2));
888 break;
889 case Instruction::Load:
890 printIndirectLoad(Inst->getOperand(0));
891 break;
892 case Instruction::Store:
893 printStoreInstruction(Inst);
894 break;
895 case Instruction::Trunc:
896 case Instruction::ZExt:
897 case Instruction::SExt:
898 case Instruction::FPTrunc:
899 case Instruction::FPExt:
900 case Instruction::UIToFP:
901 case Instruction::SIToFP:
902 case Instruction::FPToUI:
903 case Instruction::FPToSI:
904 case Instruction::PtrToInt:
905 case Instruction::IntToPtr:
906 case Instruction::BitCast:
907 printCastInstruction(Inst->getOpcode(),Left,
908 cast<CastInst>(Inst)->getDestTy());
909 break;
910 case Instruction::GetElementPtr:
911 printGepInstruction(Inst->getOperand(0),gep_type_begin(Inst),
912 gep_type_end(Inst));
913 break;
914 case Instruction::Call:
915 printCallInstruction(cast<CallInst>(Inst));
916 break;
917 case Instruction::Invoke:
918 printInvokeInstruction(cast<InvokeInst>(Inst));
919 break;
920 case Instruction::Unwind: {
921 std::string Class = "instance void [mscorlib]System.Exception::.ctor()";
922 printSimpleInstruction("newobj",Class.c_str());
923 printSimpleInstruction("throw");
924 break;
925 }
926 case Instruction::Switch:
927 printSwitchInstruction(cast<SwitchInst>(Inst));
928 break;
929 case Instruction::Alloca:
930 printValueLoad(Inst->getOperand(0));
931 printSimpleInstruction("localloc");
932 break;
933 case Instruction::Malloc:
934 assert(0 && "LowerAllocationsPass used");
935 break;
936 case Instruction::Free:
937 assert(0 && "LowerAllocationsPass used");
938 break;
939 case Instruction::Unreachable:
940 printSimpleInstruction("ldnull");
941 printSimpleInstruction("throw");
942 break;
943 default:
944 cerr << "Instruction = " << Inst->getName() << '\n';
945 assert(0 && "Unsupported instruction");
946 }
947}
948
949
950void MSILWriter::printLoop(const Loop* L) {
951 Out << getLabelName(L->getHeader()->getName()) << ":\n";
952 const std::vector<BasicBlock*>& blocks = L->getBlocks();
953 for (unsigned I = 0, E = blocks.size(); I!=E; I++) {
954 BasicBlock* BB = blocks[I];
955 Loop* BBLoop = LInfo->getLoopFor(BB);
956 if (BBLoop == L)
957 printBasicBlock(BB);
958 else if (BB==BBLoop->getHeader() && BBLoop->getParentLoop()==L)
959 printLoop(BBLoop);
960 }
961 printSimpleInstruction("br",getLabelName(L->getHeader()->getName()).c_str());
962}
963
964
965void MSILWriter::printBasicBlock(const BasicBlock* BB) {
966 Out << getLabelName(BB) << ":\n";
967 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
968 const Instruction* Inst = I;
969 // Comment llvm original instruction
970 Out << "\n//" << *Inst << "\n";
971 // Do not handle PHI instruction in current block
972 if (Inst->getOpcode()==Instruction::PHI) continue;
973 // Print instruction
974 printInstruction(Inst);
975 // Save result
976 if (Inst->getType()!=Type::VoidTy) {
977 // Do not save value after invoke, it done in "try" block
978 if (Inst->getOpcode()==Instruction::Invoke) continue;
979 printValueSave(Inst);
980 }
981 }
982}
983
984
985void MSILWriter::printLocalVariables(const Function& F) {
986 std::string Name;
987 const Type* Ty = NULL;
988 // Find variables
989 for (const_inst_iterator I = inst_begin(&F), E = inst_end(&F); I!=E; ++I) {
990 const AllocaInst* AI = dyn_cast<AllocaInst>(&*I);
991 if (AI && !isa<GlobalVariable>(AI)) {
992 Ty = PointerType::get(AI->getAllocatedType());
993 Name = getValueName(AI);
994 } else if (I->getType()!=Type::VoidTy) {
995 Ty = I->getType();
996 Name = getValueName(&*I);
997 } else continue;
998 Out << "\t.locals (" << getTypeName(Ty) << Name << ")\n";
999 }
1000}
1001
1002
1003void MSILWriter::printFunctionBody(const Function& F) {
1004 // Print body
1005 for (Function::const_iterator I = F.begin(), E = F.end(); I!=E; ++I) {
1006 if (Loop *L = LInfo->getLoopFor(I)) {
1007 if (L->getHeader()==I && L->getParentLoop()==0)
1008 printLoop(L);
1009 } else {
1010 printBasicBlock(I);
1011 }
1012 }
1013}
1014
1015
1016void MSILWriter::printConstantExpr(const ConstantExpr* CE) {
1017 const Value *left = 0, *right = 0;
1018 if (CE->getNumOperands()>=1) left = CE->getOperand(0);
1019 if (CE->getNumOperands()>=2) right = CE->getOperand(1);
1020 // Print instruction
1021 switch (CE->getOpcode()) {
1022 case Instruction::Trunc:
1023 case Instruction::ZExt:
1024 case Instruction::SExt:
1025 case Instruction::FPTrunc:
1026 case Instruction::FPExt:
1027 case Instruction::UIToFP:
1028 case Instruction::SIToFP:
1029 case Instruction::FPToUI:
1030 case Instruction::FPToSI:
1031 case Instruction::PtrToInt:
1032 case Instruction::IntToPtr:
1033 case Instruction::BitCast:
1034 printCastInstruction(CE->getOpcode(),left,CE->getType());
1035 break;
1036 case Instruction::GetElementPtr:
1037 printGepInstruction(CE->getOperand(0),gep_type_begin(CE),gep_type_end(CE));
1038 break;
1039 case Instruction::ICmp:
1040 printICmpInstruction(CE->getPredicate(),left,right);
1041 break;
1042 case Instruction::FCmp:
1043 printFCmpInstruction(CE->getPredicate(),left,right);
1044 break;
1045 case Instruction::Select:
1046 printSelectInstruction(CE->getOperand(0),CE->getOperand(1),CE->getOperand(2));
1047 break;
1048 case Instruction::Add:
1049 printBinaryInstruction("add",left,right);
1050 break;
1051 case Instruction::Sub:
1052 printBinaryInstruction("sub",left,right);
1053 break;
1054 case Instruction::Mul:
1055 printBinaryInstruction("mul",left,right);
1056 break;
1057 case Instruction::UDiv:
1058 printBinaryInstruction("div.un",left,right);
1059 break;
1060 case Instruction::SDiv:
1061 case Instruction::FDiv:
1062 printBinaryInstruction("div",left,right);
1063 break;
1064 case Instruction::URem:
1065 printBinaryInstruction("rem.un",left,right);
1066 break;
1067 case Instruction::SRem:
1068 case Instruction::FRem:
1069 printBinaryInstruction("rem",left,right);
1070 break;
1071 case Instruction::And:
1072 printBinaryInstruction("and",left,right);
1073 break;
1074 case Instruction::Or:
1075 printBinaryInstruction("or",left,right);
1076 break;
1077 case Instruction::Xor:
1078 printBinaryInstruction("xor",left,right);
1079 break;
1080 case Instruction::Shl:
1081 printBinaryInstruction("shl",left,right);
1082 break;
1083 case Instruction::LShr:
1084 printBinaryInstruction("shr.un",left,right);
1085 break;
1086 case Instruction::AShr:
1087 printBinaryInstruction("shr",left,right);
1088 break;
1089 default:
1090 cerr << "Expression = " << *CE << "\n";
1091 assert(0 && "Invalid constant expression");
1092 }
1093}
1094
1095
1096void MSILWriter::printStaticInitializerList() {
1097 // List of global variables with uninitialized fields.
1098 for (std::map<const GlobalVariable*,std::vector<StaticInitializer> >::iterator
1099 VarI = StaticInitList.begin(), VarE = StaticInitList.end(); VarI!=VarE;
1100 ++VarI) {
1101 const std::vector<StaticInitializer>& InitList = VarI->second;
1102 if (InitList.empty()) continue;
1103 // For each uninitialized field.
1104 for (std::vector<StaticInitializer>::const_iterator I = InitList.begin(),
1105 E = InitList.end(); I!=E; ++I) {
1106 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(I->constant)) {
1107 Out << "\n// Init " << getValueName(VarI->first) << ", offset " <<
1108 utostr(I->offset) << ", type "<< *I->constant->getType() << "\n\n";
1109 // Load variable address
1110 printValueLoad(VarI->first);
1111 // Add offset
1112 if (I->offset!=0) {
1113 printPtrLoad(I->offset);
1114 printSimpleInstruction("add");
1115 }
1116 // Load value
1117 printConstantExpr(CE);
1118 // Save result at offset
1119 std::string postfix = getTypePostfix(CE->getType(),true);
1120 if (*postfix.begin()=='u') *postfix.begin() = 'i';
1121 postfix = "stind."+postfix;
1122 printSimpleInstruction(postfix.c_str());
1123 } else {
1124 cerr << "Constant = " << *I->constant << '\n';
1125 assert(0 && "Invalid static initializer");
1126 }
1127 }
1128 }
1129}
1130
1131
1132void MSILWriter::printFunction(const Function& F) {
1133 const FunctionType* FTy = F.getFunctionType();
1134 bool isSigned = FTy->paramHasAttr(0,FunctionType::SExtAttribute);
1135 Out << "\n.method static ";
1136 Out << (F.hasInternalLinkage() ? "private " : "public ");
1137 if (F.isVarArg()) Out << "vararg ";
1138 Out << getTypeName(F.getReturnType(),isSigned) <<
1139 getConvModopt(F.getCallingConv()) << getValueName(&F) << '\n';
1140 // Arguments
1141 Out << "\t(";
1142 unsigned ArgIdx = 1;
1143 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I!=E;
1144 ++I, ++ArgIdx) {
1145 isSigned = FTy->paramHasAttr(ArgIdx,FunctionType::SExtAttribute);
1146 if (I!=F.arg_begin()) Out << ", ";
1147 Out << getTypeName(I->getType(),isSigned) << getValueName(I);
1148 }
1149 Out << ") cil managed\n";
1150 // Body
1151 Out << "{\n";
1152 // FIXME: Convert "string[]" to "argc,argv"
1153 if (F.getName()=="main") {
1154 printSimpleInstruction(".entrypoint");
1155 printLocalVariables(F);
1156 printStaticInitializerList();
1157 } else {
1158 printLocalVariables(F);
1159 }
1160 printFunctionBody(F);
1161 Out << "}\n";
1162}
1163
1164
1165void MSILWriter::printDeclarations(const TypeSymbolTable& ST) {
1166 std::string Name;
1167 std::set<const Type*> Printed;
1168 //cerr << "UsedTypes = " << UsedTypes << '\n';
1169 for (std::set<const Type*>::const_iterator
1170 UI = UsedTypes->begin(), UE = UsedTypes->end(); UI!=UE; ++UI) {
1171 const Type* Ty = *UI;
1172 if (isa<ArrayType>(Ty))
1173 Name = getArrayTypeName(Ty->getTypeID(),Ty);
1174 else if (isa<VectorType>(Ty))
1175 Name = getArrayTypeName(Ty->getTypeID(),Ty);
1176 else if (isa<StructType>(Ty))
1177 Name = ModulePtr->getTypeName(Ty);
1178 // Type with no need to declare.
1179 else continue;
1180 // Print not duplicated type
1181 if (Printed.insert(Ty).second) {
1182 Out << ".class value explicit ansi sealed '" << Name << "'";
1183 Out << " { .pack " << 1 << " .size " << TD->getTypeSize(Ty) << " }\n\n";
1184 }
1185 }
1186}
1187
1188
1189unsigned int MSILWriter::getBitWidth(const Type* Ty) {
1190 unsigned int N = Ty->getPrimitiveSizeInBits();
1191 assert(N!=0 && "Invalid type in getBitWidth()");
1192 switch (N) {
1193 case 1:
1194 case 8:
1195 case 16:
1196 case 32:
1197 case 64:
1198 return N;
1199 default:
1200 cerr << "Bits = " << N << '\n';
1201 assert(0 && "Unsupported integer width");
1202 }
1203}
1204
1205
1206void MSILWriter::printStaticConstant(const Constant* C, uint64_t& Offset) {
1207 uint64_t TySize = 0;
1208 const Type* Ty = C->getType();
1209 // Print zero initialized constant.
1210 if (isa<ConstantAggregateZero>(C) || C->isNullValue()) {
1211 TySize = TD->getTypeSize(C->getType());
1212 Offset += TySize;
1213 Out << "int8 (0) [" << TySize << "]";
1214 return;
1215 }
1216 // Print constant initializer
1217 switch (Ty->getTypeID()) {
1218 case Type::IntegerTyID: {
1219 TySize = TD->getTypeSize(Ty);
1220 const ConstantInt* Int = cast<ConstantInt>(C);
1221 Out << getPrimitiveTypeName(Ty,true) << "(" << Int->getSExtValue() << ")";
1222 break;
1223 }
1224 case Type::FloatTyID:
1225 case Type::DoubleTyID: {
1226 TySize = TD->getTypeSize(Ty);
1227 const ConstantFP* CFp = cast<ConstantFP>(C);
1228 Out << getPrimitiveTypeName(Ty,true) << "(" << CFp->getValue() << ")";
1229 break;
1230 }
1231 case Type::ArrayTyID:
1232 case Type::VectorTyID:
1233 case Type::StructTyID:
1234 for (unsigned I = 0, E = C->getNumOperands(); I<E; I++) {
1235 if (I!=0) Out << ",\n";
1236 printStaticConstant(C->getOperand(I),Offset);
1237 }
1238 break;
1239 case Type::PointerTyID:
1240 TySize = TD->getTypeSize(C->getType());
1241 // Initialize with global variable address
1242 if (const GlobalVariable *G = dyn_cast<GlobalVariable>(C)) {
1243 std::string name = getValueName(G);
1244 Out << "&(" << name.insert(name.length()-1,"$data") << ")";
1245 } else {
1246 // Dynamic initialization
1247 if (!isa<ConstantPointerNull>(C) && !C->isNullValue())
1248 InitListPtr->push_back(StaticInitializer(C,Offset));
1249 // Null pointer initialization
1250 if (TySize==4) Out << "int32 (0)";
1251 else if (TySize==8) Out << "int64 (0)";
1252 else assert(0 && "Invalid pointer size");
1253 }
1254 break;
1255 default:
1256 cerr << "TypeID = " << Ty->getTypeID() << '\n';
1257 assert(0 && "Invalid type in printStaticConstant()");
1258 }
1259 // Increase offset.
1260 Offset += TySize;
1261}
1262
1263
1264void MSILWriter::printStaticInitializer(const Constant* C,
1265 const std::string& Name) {
1266 switch (C->getType()->getTypeID()) {
1267 case Type::IntegerTyID:
1268 case Type::FloatTyID:
1269 case Type::DoubleTyID:
1270 Out << getPrimitiveTypeName(C->getType(),true);
1271 break;
1272 case Type::ArrayTyID:
1273 case Type::VectorTyID:
1274 case Type::StructTyID:
1275 case Type::PointerTyID:
1276 Out << getTypeName(C->getType());
1277 break;
1278 default:
1279 cerr << "Type = " << *C << "\n";
1280 assert(0 && "Invalid constant type");
1281 }
1282 // Print initializer
1283 std::string label = Name;
1284 label.insert(label.length()-1,"$data");
1285 Out << Name << " at " << label << '\n';
1286 Out << ".data " << label << " = {\n";
1287 uint64_t offset = 0;
1288 printStaticConstant(C,offset);
1289 Out << "\n}\n\n";
1290}
1291
1292
1293void MSILWriter::printVariableDefinition(const GlobalVariable* G) {
1294 const Constant* C = G->getInitializer();
1295 if (C->isNullValue() || isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
1296 InitListPtr = 0;
1297 else
1298 InitListPtr = &StaticInitList[G];
1299 printStaticInitializer(C,getValueName(G));
1300}
1301
1302
1303void MSILWriter::printGlobalVariables() {
1304 if (ModulePtr->global_empty()) return;
1305 Module::global_iterator I,E;
1306 for (I = ModulePtr->global_begin(), E = ModulePtr->global_end(); I!=E; ++I) {
1307 // Variable definition
1308 if (I->isDeclaration()) continue;
1309 Out << ".field static " << (I->hasExternalLinkage() ? "public " :
1310 "private ");
1311 printVariableDefinition(&*I);
1312 }
1313}
1314
1315
1316void MSILWriter::printExternals() {
1317 Module::const_iterator I,E;
1318 for (I=ModulePtr->begin(),E=ModulePtr->end(); I!=E; ++I) {
1319 // Skip intrisics
1320 if (I->getIntrinsicID()) continue;
1321 // FIXME: Treat as standard library function
1322 if (I->isDeclaration()) {
1323 const Function* F = &*I;
1324 const FunctionType* FTy = F->getFunctionType();
1325 std::string Name = getConvModopt(F->getCallingConv())+getValueName(F);
1326 std::string Sig = getCallSignature(FTy,NULL,Name);
1327 Out << ".method static hidebysig pinvokeimpl(\"msvcrt.dll\" cdecl)\n\t"
1328 << Sig << " preservesig {}\n\n";
1329 }
1330 }
1331}
1332
1333//===----------------------------------------------------------------------===//
1334// External Interface declaration
1335//===----------------------------------------------------------------------===//
1336
1337bool MSILTarget::addPassesToEmitWholeFile(PassManager &PM, std::ostream &o,
1338 CodeGenFileType FileType, bool Fast)
1339{
1340 if (FileType != TargetMachine::AssemblyFile) return true;
1341 MSILWriter* Writer = new MSILWriter(o);
1342 PM.add(createLowerGCPass());
1343 PM.add(createLowerAllocationsPass(true));
1344 // FIXME: Handle switch trougth native IL instruction "switch"
1345 PM.add(createLowerSwitchPass());
1346 PM.add(createCFGSimplificationPass());
1347 PM.add(new MSILModule(Writer->UsedTypes,Writer->TD));
1348 PM.add(Writer);
1349 return false;
1350}