Chris Lattner | 31c2ec3 | 2007-05-06 20:31:17 +0000 | [diff] [blame] | 1 | //===-- MSILWriter.cpp - Library for converting LLVM code to MSIL ---------===// |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 2 | // |
Bill Wendling | 85db3a9 | 2008-02-26 10:57:23 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 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" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 23 | #include "llvm/Support/InstVisitor.h" |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/Passes.h" |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | // TargetMachine for the MSIL |
| 31 | struct VISIBILITY_HIDDEN MSILTarget : public TargetMachine { |
| 32 | const TargetData DataLayout; // Calculates type size & alignment |
| 33 | |
| 34 | MSILTarget(const Module &M, const std::string &FS) |
| 35 | : DataLayout(&M) {} |
| 36 | |
| 37 | virtual bool WantsWholeFile() const { return true; } |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 38 | virtual bool addPassesToEmitWholeFile(PassManager &PM, raw_ostream &Out, |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 39 | CodeGenFileType FileType, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 40 | CodeGenOpt::Level OptLevel); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 41 | |
| 42 | // This class always works, but shouldn't be the default in most cases. |
| 43 | static unsigned getModuleMatchQuality(const Module &M) { return 1; } |
| 44 | |
| 45 | virtual const TargetData *getTargetData() const { return &DataLayout; } |
| 46 | }; |
| 47 | } |
| 48 | |
Oscar Fuentes | 92adc19 | 2008-11-15 21:36:30 +0000 | [diff] [blame] | 49 | /// MSILTargetMachineModule - Note that this is used on hosts that |
| 50 | /// cannot link in a library unless there are references into the |
| 51 | /// library. In particular, it seems that it is not possible to get |
| 52 | /// things to work on Win32 without this. Though it is unused, do not |
| 53 | /// remove it. |
| 54 | extern "C" int MSILTargetMachineModule; |
| 55 | int MSILTargetMachineModule = 0; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 56 | |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 57 | static RegisterTarget<MSILTarget> X("msil", "MSIL backend"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 58 | |
Bob Wilson | a96751f | 2009-06-23 23:59:40 +0000 | [diff] [blame] | 59 | // Force static initialization. |
| 60 | extern "C" void LLVMInitializeMSILTarget() { } |
Douglas Gregor | 1555a23 | 2009-06-16 20:12:29 +0000 | [diff] [blame] | 61 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 62 | bool MSILModule::runOnModule(Module &M) { |
| 63 | ModulePtr = &M; |
| 64 | TD = &getAnalysis<TargetData>(); |
| 65 | bool Changed = false; |
| 66 | // Find named types. |
| 67 | TypeSymbolTable& Table = M.getTypeSymbolTable(); |
| 68 | std::set<const Type *> Types = getAnalysis<FindUsedTypes>().getTypes(); |
| 69 | for (TypeSymbolTable::iterator I = Table.begin(), E = Table.end(); I!=E; ) { |
| 70 | if (!isa<StructType>(I->second) && !isa<OpaqueType>(I->second)) |
| 71 | Table.remove(I++); |
| 72 | else { |
| 73 | std::set<const Type *>::iterator T = Types.find(I->second); |
| 74 | if (T==Types.end()) |
| 75 | Table.remove(I++); |
| 76 | else { |
| 77 | Types.erase(T); |
| 78 | ++I; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | // Find unnamed types. |
| 83 | unsigned RenameCounter = 0; |
| 84 | for (std::set<const Type *>::const_iterator I = Types.begin(), |
| 85 | E = Types.end(); I!=E; ++I) |
| 86 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 87 | while (ModulePtr->addTypeName("unnamed$"+utostr(RenameCounter), STy)) |
| 88 | ++RenameCounter; |
| 89 | Changed = true; |
| 90 | } |
| 91 | // Pointer for FunctionPass. |
| 92 | UsedTypes = &getAnalysis<FindUsedTypes>().getTypes(); |
| 93 | return Changed; |
| 94 | } |
| 95 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 96 | char MSILModule::ID = 0; |
| 97 | char MSILWriter::ID = 0; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 98 | |
| 99 | bool MSILWriter::runOnFunction(Function &F) { |
| 100 | if (F.isDeclaration()) return false; |
Chris Lattner | 9062d9a | 2009-04-17 00:26:12 +0000 | [diff] [blame] | 101 | |
| 102 | // Do not codegen any 'available_externally' functions at all, they have |
| 103 | // definitions outside the translation unit. |
| 104 | if (F.hasAvailableExternallyLinkage()) |
| 105 | return false; |
| 106 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 107 | LInfo = &getAnalysis<LoopInfo>(); |
| 108 | printFunction(F); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | bool MSILWriter::doInitialization(Module &M) { |
| 114 | ModulePtr = &M; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 115 | Mang = new Mangler(M); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 116 | Out << ".assembly extern mscorlib {}\n"; |
| 117 | Out << ".assembly MSIL {}\n\n"; |
| 118 | Out << "// External\n"; |
| 119 | printExternals(); |
| 120 | Out << "// Declarations\n"; |
| 121 | printDeclarations(M.getTypeSymbolTable()); |
| 122 | Out << "// Definitions\n"; |
| 123 | printGlobalVariables(); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 124 | Out << "// Startup code\n"; |
| 125 | printModuleStartup(); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | bool MSILWriter::doFinalization(Module &M) { |
| 131 | delete Mang; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 136 | void MSILWriter::printModuleStartup() { |
| 137 | Out << |
| 138 | ".method static public int32 $MSIL_Startup() {\n" |
| 139 | "\t.entrypoint\n" |
| 140 | "\t.locals (native int i)\n" |
| 141 | "\t.locals (native int argc)\n" |
| 142 | "\t.locals (native int ptr)\n" |
| 143 | "\t.locals (void* argv)\n" |
| 144 | "\t.locals (string[] args)\n" |
| 145 | "\tcall\tstring[] [mscorlib]System.Environment::GetCommandLineArgs()\n" |
| 146 | "\tdup\n" |
| 147 | "\tstloc\targs\n" |
| 148 | "\tldlen\n" |
| 149 | "\tconv.i4\n" |
| 150 | "\tdup\n" |
| 151 | "\tstloc\targc\n"; |
| 152 | printPtrLoad(TD->getPointerSize()); |
| 153 | Out << |
| 154 | "\tmul\n" |
| 155 | "\tlocalloc\n" |
| 156 | "\tstloc\targv\n" |
| 157 | "\tldc.i4.0\n" |
| 158 | "\tstloc\ti\n" |
| 159 | "L_01:\n" |
| 160 | "\tldloc\ti\n" |
| 161 | "\tldloc\targc\n" |
| 162 | "\tceq\n" |
| 163 | "\tbrtrue\tL_02\n" |
| 164 | "\tldloc\targs\n" |
| 165 | "\tldloc\ti\n" |
| 166 | "\tldelem.ref\n" |
| 167 | "\tcall\tnative int [mscorlib]System.Runtime.InteropServices.Marshal::" |
| 168 | "StringToHGlobalAnsi(string)\n" |
| 169 | "\tstloc\tptr\n" |
| 170 | "\tldloc\targv\n" |
| 171 | "\tldloc\ti\n"; |
| 172 | printPtrLoad(TD->getPointerSize()); |
| 173 | Out << |
| 174 | "\tmul\n" |
| 175 | "\tadd\n" |
| 176 | "\tldloc\tptr\n" |
| 177 | "\tstind.i\n" |
| 178 | "\tldloc\ti\n" |
| 179 | "\tldc.i4.1\n" |
| 180 | "\tadd\n" |
| 181 | "\tstloc\ti\n" |
| 182 | "\tbr\tL_01\n" |
| 183 | "L_02:\n" |
| 184 | "\tcall void $MSIL_Init()\n"; |
| 185 | |
| 186 | // Call user 'main' function. |
| 187 | const Function* F = ModulePtr->getFunction("main"); |
| 188 | if (!F || F->isDeclaration()) { |
| 189 | Out << "\tldc.i4.0\n\tret\n}\n"; |
| 190 | return; |
| 191 | } |
Nick Lewycky | 9c0f146 | 2009-03-19 05:51:39 +0000 | [diff] [blame] | 192 | bool BadSig = true; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 193 | std::string Args(""); |
| 194 | Function::const_arg_iterator Arg1,Arg2; |
| 195 | |
| 196 | switch (F->arg_size()) { |
| 197 | case 0: |
| 198 | BadSig = false; |
| 199 | break; |
| 200 | case 1: |
| 201 | Arg1 = F->arg_begin(); |
| 202 | if (Arg1->getType()->isInteger()) { |
| 203 | Out << "\tldloc\targc\n"; |
| 204 | Args = getTypeName(Arg1->getType()); |
| 205 | BadSig = false; |
| 206 | } |
| 207 | break; |
| 208 | case 2: |
| 209 | Arg1 = Arg2 = F->arg_begin(); ++Arg2; |
| 210 | if (Arg1->getType()->isInteger() && |
| 211 | Arg2->getType()->getTypeID() == Type::PointerTyID) { |
| 212 | Out << "\tldloc\targc\n\tldloc\targv\n"; |
| 213 | Args = getTypeName(Arg1->getType())+","+getTypeName(Arg2->getType()); |
| 214 | BadSig = false; |
| 215 | } |
| 216 | break; |
| 217 | default: |
| 218 | BadSig = true; |
| 219 | } |
| 220 | |
| 221 | bool RetVoid = (F->getReturnType()->getTypeID() == Type::VoidTyID); |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 222 | if (BadSig || (!F->getReturnType()->isInteger() && !RetVoid)) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 223 | Out << "\tldc.i4.0\n"; |
| 224 | } else { |
| 225 | Out << "\tcall\t" << getTypeName(F->getReturnType()) << |
| 226 | getConvModopt(F->getCallingConv()) << "main(" << Args << ")\n"; |
| 227 | if (RetVoid) |
| 228 | Out << "\tldc.i4.0\n"; |
| 229 | else |
| 230 | Out << "\tconv.i4\n"; |
| 231 | } |
| 232 | Out << "\tret\n}\n"; |
| 233 | } |
| 234 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 235 | bool MSILWriter::isZeroValue(const Value* V) { |
| 236 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 237 | return C->isNullValue(); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | std::string MSILWriter::getValueName(const Value* V) { |
Chris Lattner | ca1bafd | 2009-07-13 23:46:46 +0000 | [diff] [blame] | 243 | std::string Name; |
| 244 | if (const GlobalValue *GV = cast<GlobalValue>(V)) |
Chris Lattner | b09d2cc | 2009-07-14 06:18:50 +0000 | [diff] [blame] | 245 | Name = Mang->getMangledName(GV); |
Chris Lattner | ca1bafd | 2009-07-13 23:46:46 +0000 | [diff] [blame] | 246 | else { |
| 247 | unsigned &No = AnonValueNumbers[V]; |
| 248 | if (No == 0) No = ++NextAnonValueNumber; |
| 249 | Name = "tmp" + utostr(No); |
| 250 | } |
| 251 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 252 | // Name into the quotes allow control and space characters. |
Chris Lattner | ca1bafd | 2009-07-13 23:46:46 +0000 | [diff] [blame] | 253 | return "'"+Name+"'"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | std::string MSILWriter::getLabelName(const std::string& Name) { |
| 258 | if (Name.find('.')!=std::string::npos) { |
| 259 | std::string Tmp(Name); |
| 260 | // Replace unaccepable characters in the label name. |
| 261 | for (std::string::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) |
| 262 | if (*I=='.') *I = '@'; |
| 263 | return Tmp; |
| 264 | } |
| 265 | return Name; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | std::string MSILWriter::getLabelName(const Value* V) { |
Chris Lattner | ca1bafd | 2009-07-13 23:46:46 +0000 | [diff] [blame] | 270 | std::string Name; |
| 271 | if (const GlobalValue *GV = cast<GlobalValue>(V)) |
Chris Lattner | b09d2cc | 2009-07-14 06:18:50 +0000 | [diff] [blame] | 272 | Name = Mang->getMangledName(GV); |
Chris Lattner | ca1bafd | 2009-07-13 23:46:46 +0000 | [diff] [blame] | 273 | else { |
| 274 | unsigned &No = AnonValueNumbers[V]; |
| 275 | if (No == 0) No = ++NextAnonValueNumber; |
| 276 | Name = "tmp" + utostr(No); |
| 277 | } |
| 278 | |
| 279 | return getLabelName(Name); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | std::string MSILWriter::getConvModopt(unsigned CallingConvID) { |
| 284 | switch (CallingConvID) { |
| 285 | case CallingConv::C: |
| 286 | case CallingConv::Cold: |
| 287 | case CallingConv::Fast: |
| 288 | return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) "; |
| 289 | case CallingConv::X86_FastCall: |
| 290 | return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvFastcall) "; |
| 291 | case CallingConv::X86_StdCall: |
| 292 | return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvStdcall) "; |
| 293 | default: |
| 294 | cerr << "CallingConvID = " << CallingConvID << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 295 | LLVM_UNREACHABLE("Unsupported calling convention"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 296 | } |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 297 | return ""; // Not reached |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
| 301 | std::string MSILWriter::getArrayTypeName(Type::TypeID TyID, const Type* Ty) { |
| 302 | std::string Tmp = ""; |
| 303 | const Type* ElemTy = Ty; |
| 304 | assert(Ty->getTypeID()==TyID && "Invalid type passed"); |
| 305 | // Walk trought array element types. |
| 306 | for (;;) { |
| 307 | // Multidimensional array. |
| 308 | if (ElemTy->getTypeID()==TyID) { |
| 309 | if (const ArrayType* ATy = dyn_cast<ArrayType>(ElemTy)) |
| 310 | Tmp += utostr(ATy->getNumElements()); |
| 311 | else if (const VectorType* VTy = dyn_cast<VectorType>(ElemTy)) |
| 312 | Tmp += utostr(VTy->getNumElements()); |
| 313 | ElemTy = cast<SequentialType>(ElemTy)->getElementType(); |
| 314 | } |
| 315 | // Base element type found. |
| 316 | if (ElemTy->getTypeID()!=TyID) break; |
| 317 | Tmp += ","; |
| 318 | } |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 319 | return getTypeName(ElemTy, false, true)+"["+Tmp+"]"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | std::string MSILWriter::getPrimitiveTypeName(const Type* Ty, bool isSigned) { |
| 324 | unsigned NumBits = 0; |
| 325 | switch (Ty->getTypeID()) { |
| 326 | case Type::VoidTyID: |
| 327 | return "void "; |
| 328 | case Type::IntegerTyID: |
| 329 | NumBits = getBitWidth(Ty); |
| 330 | if(NumBits==1) |
| 331 | return "bool "; |
| 332 | if (!isSigned) |
| 333 | return "unsigned int"+utostr(NumBits)+" "; |
| 334 | return "int"+utostr(NumBits)+" "; |
| 335 | case Type::FloatTyID: |
| 336 | return "float32 "; |
| 337 | case Type::DoubleTyID: |
| 338 | return "float64 "; |
| 339 | default: |
| 340 | cerr << "Type = " << *Ty << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 341 | LLVM_UNREACHABLE("Invalid primitive type"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 342 | } |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 343 | return ""; // Not reached |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 347 | std::string MSILWriter::getTypeName(const Type* Ty, bool isSigned, |
| 348 | bool isNested) { |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 349 | if (Ty->isPrimitiveType() || Ty->isInteger()) |
| 350 | return getPrimitiveTypeName(Ty,isSigned); |
| 351 | // FIXME: "OpaqueType" support |
| 352 | switch (Ty->getTypeID()) { |
| 353 | case Type::PointerTyID: |
| 354 | return "void* "; |
| 355 | case Type::StructTyID: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 356 | if (isNested) |
| 357 | return ModulePtr->getTypeName(Ty); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 358 | return "valuetype '"+ModulePtr->getTypeName(Ty)+"' "; |
| 359 | case Type::ArrayTyID: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 360 | if (isNested) |
| 361 | return getArrayTypeName(Ty->getTypeID(),Ty); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 362 | return "valuetype '"+getArrayTypeName(Ty->getTypeID(),Ty)+"' "; |
| 363 | case Type::VectorTyID: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 364 | if (isNested) |
| 365 | return getArrayTypeName(Ty->getTypeID(),Ty); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 366 | return "valuetype '"+getArrayTypeName(Ty->getTypeID(),Ty)+"' "; |
| 367 | default: |
| 368 | cerr << "Type = " << *Ty << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 369 | LLVM_UNREACHABLE("Invalid type in getTypeName()"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 371 | return ""; // Not reached |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
| 375 | MSILWriter::ValueType MSILWriter::getValueLocation(const Value* V) { |
| 376 | // Function argument |
| 377 | if (isa<Argument>(V)) |
| 378 | return ArgumentVT; |
| 379 | // Function |
| 380 | else if (const Function* F = dyn_cast<Function>(V)) |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 381 | return F->hasLocalLinkage() ? InternalVT : GlobalVT; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 382 | // Variable |
| 383 | else if (const GlobalVariable* G = dyn_cast<GlobalVariable>(V)) |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 384 | return G->hasLocalLinkage() ? InternalVT : GlobalVT; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 385 | // Constant |
| 386 | else if (isa<Constant>(V)) |
| 387 | return isa<ConstantExpr>(V) ? ConstExprVT : ConstVT; |
| 388 | // Local variable |
| 389 | return LocalVT; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | std::string MSILWriter::getTypePostfix(const Type* Ty, bool Expand, |
| 394 | bool isSigned) { |
| 395 | unsigned NumBits = 0; |
| 396 | switch (Ty->getTypeID()) { |
| 397 | // Integer constant, expanding for stack operations. |
| 398 | case Type::IntegerTyID: |
| 399 | NumBits = getBitWidth(Ty); |
| 400 | // Expand integer value to "int32" or "int64". |
| 401 | if (Expand) return (NumBits<=32 ? "i4" : "i8"); |
| 402 | if (NumBits==1) return "i1"; |
| 403 | return (isSigned ? "i" : "u")+utostr(NumBits/8); |
| 404 | // Float constant. |
| 405 | case Type::FloatTyID: |
| 406 | return "r4"; |
| 407 | case Type::DoubleTyID: |
| 408 | return "r8"; |
| 409 | case Type::PointerTyID: |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 410 | return "i"+utostr(TD->getTypeAllocSize(Ty)); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 411 | default: |
| 412 | cerr << "TypeID = " << Ty->getTypeID() << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 413 | LLVM_UNREACHABLE("Invalid type in TypeToPostfix()"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 414 | } |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 415 | return ""; // Not reached |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 419 | void MSILWriter::printConvToPtr() { |
| 420 | switch (ModulePtr->getPointerSize()) { |
| 421 | case Module::Pointer32: |
| 422 | printSimpleInstruction("conv.u4"); |
| 423 | break; |
| 424 | case Module::Pointer64: |
| 425 | printSimpleInstruction("conv.u8"); |
| 426 | break; |
| 427 | default: |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 428 | LLVM_UNREACHABLE("Module use not supporting pointer size"); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 433 | void MSILWriter::printPtrLoad(uint64_t N) { |
| 434 | switch (ModulePtr->getPointerSize()) { |
| 435 | case Module::Pointer32: |
| 436 | printSimpleInstruction("ldc.i4",utostr(N).c_str()); |
| 437 | // FIXME: Need overflow test? |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 438 | if (!isUInt32(N)) { |
| 439 | cerr << "Value = " << utostr(N) << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 440 | LLVM_UNREACHABLE("32-bit pointer overflowed"); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 441 | } |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 442 | break; |
| 443 | case Module::Pointer64: |
| 444 | printSimpleInstruction("ldc.i8",utostr(N).c_str()); |
| 445 | break; |
| 446 | default: |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 447 | LLVM_UNREACHABLE("Module use not supporting pointer size"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 452 | void MSILWriter::printValuePtrLoad(const Value* V) { |
| 453 | printValueLoad(V); |
| 454 | printConvToPtr(); |
| 455 | } |
| 456 | |
| 457 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 458 | void MSILWriter::printConstLoad(const Constant* C) { |
| 459 | if (const ConstantInt* CInt = dyn_cast<ConstantInt>(C)) { |
| 460 | // Integer constant |
| 461 | Out << "\tldc." << getTypePostfix(C->getType(),true) << '\t'; |
| 462 | if (CInt->isMinValue(true)) |
| 463 | Out << CInt->getSExtValue(); |
| 464 | else |
| 465 | Out << CInt->getZExtValue(); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 466 | } else if (const ConstantFP* FP = dyn_cast<ConstantFP>(C)) { |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 467 | // Float constant |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 468 | uint64_t X; |
| 469 | unsigned Size; |
| 470 | if (FP->getType()->getTypeID()==Type::FloatTyID) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 471 | X = (uint32_t)FP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 472 | Size = 4; |
| 473 | } else { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 474 | X = FP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 475 | Size = 8; |
| 476 | } |
| 477 | Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')'; |
| 478 | } else if (isa<UndefValue>(C)) { |
| 479 | // Undefined constant value = NULL. |
| 480 | printPtrLoad(0); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 481 | } else { |
| 482 | cerr << "Constant = " << *C << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 483 | LLVM_UNREACHABLE("Invalid constant value"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 484 | } |
| 485 | Out << '\n'; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | void MSILWriter::printValueLoad(const Value* V) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 490 | MSILWriter::ValueType Location = getValueLocation(V); |
| 491 | switch (Location) { |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 492 | // Global variable or function address. |
| 493 | case GlobalVT: |
| 494 | case InternalVT: |
| 495 | if (const Function* F = dyn_cast<Function>(V)) { |
| 496 | std::string Name = getConvModopt(F->getCallingConv())+getValueName(F); |
| 497 | printSimpleInstruction("ldftn", |
| 498 | getCallSignature(F->getFunctionType(),NULL,Name).c_str()); |
| 499 | } else { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 500 | std::string Tmp; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 501 | const Type* ElemTy = cast<PointerType>(V->getType())->getElementType(); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 502 | if (Location==GlobalVT && cast<GlobalVariable>(V)->hasDLLImportLinkage()) { |
| 503 | Tmp = "void* "+getValueName(V); |
| 504 | printSimpleInstruction("ldsfld",Tmp.c_str()); |
| 505 | } else { |
| 506 | Tmp = getTypeName(ElemTy)+getValueName(V); |
| 507 | printSimpleInstruction("ldsflda",Tmp.c_str()); |
| 508 | } |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 509 | } |
| 510 | break; |
| 511 | // Function argument. |
| 512 | case ArgumentVT: |
| 513 | printSimpleInstruction("ldarg",getValueName(V).c_str()); |
| 514 | break; |
| 515 | // Local function variable. |
| 516 | case LocalVT: |
| 517 | printSimpleInstruction("ldloc",getValueName(V).c_str()); |
| 518 | break; |
| 519 | // Constant value. |
| 520 | case ConstVT: |
| 521 | if (isa<ConstantPointerNull>(V)) |
| 522 | printPtrLoad(0); |
| 523 | else |
| 524 | printConstLoad(cast<Constant>(V)); |
| 525 | break; |
| 526 | // Constant expression. |
| 527 | case ConstExprVT: |
| 528 | printConstantExpr(cast<ConstantExpr>(V)); |
| 529 | break; |
| 530 | default: |
| 531 | cerr << "Value = " << *V << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 532 | LLVM_UNREACHABLE("Invalid value location"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | |
| 537 | void MSILWriter::printValueSave(const Value* V) { |
| 538 | switch (getValueLocation(V)) { |
| 539 | case ArgumentVT: |
| 540 | printSimpleInstruction("starg",getValueName(V).c_str()); |
| 541 | break; |
| 542 | case LocalVT: |
| 543 | printSimpleInstruction("stloc",getValueName(V).c_str()); |
| 544 | break; |
| 545 | default: |
| 546 | cerr << "Value = " << *V << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 547 | LLVM_UNREACHABLE("Invalid value location"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | |
| 552 | void MSILWriter::printBinaryInstruction(const char* Name, const Value* Left, |
| 553 | const Value* Right) { |
| 554 | printValueLoad(Left); |
| 555 | printValueLoad(Right); |
| 556 | Out << '\t' << Name << '\n'; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | void MSILWriter::printSimpleInstruction(const char* Inst, const char* Operand) { |
| 561 | if(Operand) |
| 562 | Out << '\t' << Inst << '\t' << Operand << '\n'; |
| 563 | else |
| 564 | Out << '\t' << Inst << '\n'; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | void MSILWriter::printPHICopy(const BasicBlock* Src, const BasicBlock* Dst) { |
| 569 | for (BasicBlock::const_iterator I = Dst->begin(), E = Dst->end(); |
| 570 | isa<PHINode>(I); ++I) { |
| 571 | const PHINode* Phi = cast<PHINode>(I); |
| 572 | const Value* Val = Phi->getIncomingValueForBlock(Src); |
| 573 | if (isa<UndefValue>(Val)) continue; |
| 574 | printValueLoad(Val); |
| 575 | printValueSave(Phi); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | void MSILWriter::printBranchToBlock(const BasicBlock* CurrBB, |
| 581 | const BasicBlock* TrueBB, |
| 582 | const BasicBlock* FalseBB) { |
| 583 | if (TrueBB==FalseBB) { |
| 584 | // "TrueBB" and "FalseBB" destination equals |
| 585 | printPHICopy(CurrBB,TrueBB); |
| 586 | printSimpleInstruction("pop"); |
| 587 | printSimpleInstruction("br",getLabelName(TrueBB).c_str()); |
| 588 | } else if (FalseBB==NULL) { |
| 589 | // If "FalseBB" not used the jump have condition |
| 590 | printPHICopy(CurrBB,TrueBB); |
| 591 | printSimpleInstruction("brtrue",getLabelName(TrueBB).c_str()); |
| 592 | } else if (TrueBB==NULL) { |
| 593 | // If "TrueBB" not used the jump is unconditional |
| 594 | printPHICopy(CurrBB,FalseBB); |
| 595 | printSimpleInstruction("br",getLabelName(FalseBB).c_str()); |
| 596 | } else { |
| 597 | // Copy PHI instructions for each block |
| 598 | std::string TmpLabel; |
| 599 | // Print PHI instructions for "TrueBB" |
| 600 | if (isa<PHINode>(TrueBB->begin())) { |
| 601 | TmpLabel = getLabelName(TrueBB)+"$phi_"+utostr(getUniqID()); |
| 602 | printSimpleInstruction("brtrue",TmpLabel.c_str()); |
| 603 | } else { |
| 604 | printSimpleInstruction("brtrue",getLabelName(TrueBB).c_str()); |
| 605 | } |
| 606 | // Print PHI instructions for "FalseBB" |
| 607 | if (isa<PHINode>(FalseBB->begin())) { |
| 608 | printPHICopy(CurrBB,FalseBB); |
| 609 | printSimpleInstruction("br",getLabelName(FalseBB).c_str()); |
| 610 | } else { |
| 611 | printSimpleInstruction("br",getLabelName(FalseBB).c_str()); |
| 612 | } |
| 613 | if (isa<PHINode>(TrueBB->begin())) { |
| 614 | // Handle "TrueBB" PHI Copy |
| 615 | Out << TmpLabel << ":\n"; |
| 616 | printPHICopy(CurrBB,TrueBB); |
| 617 | printSimpleInstruction("br",getLabelName(TrueBB).c_str()); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | |
| 623 | void MSILWriter::printBranchInstruction(const BranchInst* Inst) { |
| 624 | if (Inst->isUnconditional()) { |
| 625 | printBranchToBlock(Inst->getParent(),NULL,Inst->getSuccessor(0)); |
| 626 | } else { |
| 627 | printValueLoad(Inst->getCondition()); |
| 628 | printBranchToBlock(Inst->getParent(),Inst->getSuccessor(0), |
| 629 | Inst->getSuccessor(1)); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | |
| 634 | void MSILWriter::printSelectInstruction(const Value* Cond, const Value* VTrue, |
| 635 | const Value* VFalse) { |
| 636 | std::string TmpLabel = std::string("select$true_")+utostr(getUniqID()); |
| 637 | printValueLoad(VTrue); |
| 638 | printValueLoad(Cond); |
| 639 | printSimpleInstruction("brtrue",TmpLabel.c_str()); |
| 640 | printSimpleInstruction("pop"); |
| 641 | printValueLoad(VFalse); |
| 642 | Out << TmpLabel << ":\n"; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void MSILWriter::printIndirectLoad(const Value* V) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 647 | const Type* Ty = V->getType(); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 648 | printValueLoad(V); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 649 | if (const PointerType* P = dyn_cast<PointerType>(Ty)) |
| 650 | Ty = P->getElementType(); |
| 651 | std::string Tmp = "ldind."+getTypePostfix(Ty, false); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 652 | printSimpleInstruction(Tmp.c_str()); |
| 653 | } |
| 654 | |
| 655 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 656 | void MSILWriter::printIndirectSave(const Value* Ptr, const Value* Val) { |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 657 | printValueLoad(Ptr); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 658 | printValueLoad(Val); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 659 | printIndirectSave(Val->getType()); |
| 660 | } |
| 661 | |
| 662 | |
| 663 | void MSILWriter::printIndirectSave(const Type* Ty) { |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 664 | // Instruction need signed postfix for any type. |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 665 | std::string postfix = getTypePostfix(Ty, false); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 666 | if (*postfix.begin()=='u') *postfix.begin() = 'i'; |
| 667 | postfix = "stind."+postfix; |
| 668 | printSimpleInstruction(postfix.c_str()); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | void MSILWriter::printCastInstruction(unsigned int Op, const Value* V, |
| 673 | const Type* Ty) { |
| 674 | std::string Tmp(""); |
| 675 | printValueLoad(V); |
| 676 | switch (Op) { |
| 677 | // Signed |
| 678 | case Instruction::SExt: |
| 679 | case Instruction::SIToFP: |
| 680 | case Instruction::FPToSI: |
| 681 | Tmp = "conv."+getTypePostfix(Ty,false,true); |
| 682 | printSimpleInstruction(Tmp.c_str()); |
| 683 | break; |
| 684 | // Unsigned |
| 685 | case Instruction::FPTrunc: |
| 686 | case Instruction::FPExt: |
| 687 | case Instruction::UIToFP: |
| 688 | case Instruction::Trunc: |
| 689 | case Instruction::ZExt: |
| 690 | case Instruction::FPToUI: |
| 691 | case Instruction::PtrToInt: |
| 692 | case Instruction::IntToPtr: |
| 693 | Tmp = "conv."+getTypePostfix(Ty,false); |
| 694 | printSimpleInstruction(Tmp.c_str()); |
| 695 | break; |
| 696 | // Do nothing |
| 697 | case Instruction::BitCast: |
| 698 | // FIXME: meaning that ld*/st* instruction do not change data format. |
| 699 | break; |
| 700 | default: |
| 701 | cerr << "Opcode = " << Op << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 702 | LLVM_UNREACHABLE("Invalid conversion instruction"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | |
| 707 | void MSILWriter::printGepInstruction(const Value* V, gep_type_iterator I, |
| 708 | gep_type_iterator E) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 709 | unsigned Size; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 710 | // Load address |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 711 | printValuePtrLoad(V); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 712 | // Calculate element offset. |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 713 | for (; I!=E; ++I){ |
| 714 | Size = 0; |
| 715 | const Value* IndexValue = I.getOperand(); |
| 716 | if (const StructType* StrucTy = dyn_cast<StructType>(*I)) { |
| 717 | uint64_t FieldIndex = cast<ConstantInt>(IndexValue)->getZExtValue(); |
| 718 | // Offset is the sum of all previous structure fields. |
| 719 | for (uint64_t F = 0; F<FieldIndex; ++F) |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 720 | Size += TD->getTypeAllocSize(StrucTy->getContainedType((unsigned)F)); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 721 | printPtrLoad(Size); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 722 | printSimpleInstruction("add"); |
| 723 | continue; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 724 | } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(*I)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 725 | Size = TD->getTypeAllocSize(SeqTy->getElementType()); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 726 | } else { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 727 | Size = TD->getTypeAllocSize(*I); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 728 | } |
| 729 | // Add offset of current element to stack top. |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 730 | if (!isZeroValue(IndexValue)) { |
| 731 | // Constant optimization. |
| 732 | if (const ConstantInt* C = dyn_cast<ConstantInt>(IndexValue)) { |
| 733 | if (C->getValue().isNegative()) { |
| 734 | printPtrLoad(C->getValue().abs().getZExtValue()*Size); |
| 735 | printSimpleInstruction("sub"); |
| 736 | continue; |
| 737 | } else |
| 738 | printPtrLoad(C->getZExtValue()*Size); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 739 | } else { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 740 | printPtrLoad(Size); |
| 741 | printValuePtrLoad(IndexValue); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 742 | printSimpleInstruction("mul"); |
| 743 | } |
| 744 | printSimpleInstruction("add"); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | |
| 750 | std::string MSILWriter::getCallSignature(const FunctionType* Ty, |
| 751 | const Instruction* Inst, |
| 752 | std::string Name) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 753 | std::string Tmp(""); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 754 | if (Ty->isVarArg()) Tmp += "vararg "; |
| 755 | // Name and return type. |
| 756 | Tmp += getTypeName(Ty->getReturnType())+Name+"("; |
| 757 | // Function argument type list. |
| 758 | unsigned NumParams = Ty->getNumParams(); |
| 759 | for (unsigned I = 0; I!=NumParams; ++I) { |
| 760 | if (I!=0) Tmp += ","; |
| 761 | Tmp += getTypeName(Ty->getParamType(I)); |
| 762 | } |
| 763 | // CLR needs to know the exact amount of parameters received by vararg |
| 764 | // function, because caller cleans the stack. |
| 765 | if (Ty->isVarArg() && Inst) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 766 | // Origin to function arguments in "CallInst" or "InvokeInst". |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 767 | unsigned Org = isa<InvokeInst>(Inst) ? 3 : 1; |
| 768 | // Print variable argument types. |
| 769 | unsigned NumOperands = Inst->getNumOperands()-Org; |
| 770 | if (NumParams<NumOperands) { |
| 771 | if (NumParams!=0) Tmp += ", "; |
| 772 | Tmp += "... , "; |
| 773 | for (unsigned J = NumParams; J!=NumOperands; ++J) { |
| 774 | if (J!=NumParams) Tmp += ", "; |
| 775 | Tmp += getTypeName(Inst->getOperand(J+Org)->getType()); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | return Tmp+")"; |
| 780 | } |
| 781 | |
| 782 | |
| 783 | void MSILWriter::printFunctionCall(const Value* FnVal, |
| 784 | const Instruction* Inst) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 785 | // Get function calling convention. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 786 | std::string Name = ""; |
| 787 | if (const CallInst* Call = dyn_cast<CallInst>(Inst)) |
| 788 | Name = getConvModopt(Call->getCallingConv()); |
| 789 | else if (const InvokeInst* Invoke = dyn_cast<InvokeInst>(Inst)) |
| 790 | Name = getConvModopt(Invoke->getCallingConv()); |
| 791 | else { |
| 792 | cerr << "Instruction = " << Inst->getName() << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 793 | LLVM_UNREACHABLE("Need \"Invoke\" or \"Call\" instruction only"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 794 | } |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 795 | if (const Function* F = dyn_cast<Function>(FnVal)) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 796 | // Direct call. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 797 | Name += getValueName(F); |
| 798 | printSimpleInstruction("call", |
| 799 | getCallSignature(F->getFunctionType(),Inst,Name).c_str()); |
| 800 | } else { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 801 | // Indirect function call. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 802 | const PointerType* PTy = cast<PointerType>(FnVal->getType()); |
| 803 | const FunctionType* FTy = cast<FunctionType>(PTy->getElementType()); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 804 | // Load function address. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 805 | printValueLoad(FnVal); |
| 806 | printSimpleInstruction("calli",getCallSignature(FTy,Inst,Name).c_str()); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 811 | void MSILWriter::printIntrinsicCall(const IntrinsicInst* Inst) { |
| 812 | std::string Name; |
| 813 | switch (Inst->getIntrinsicID()) { |
| 814 | case Intrinsic::vastart: |
| 815 | Name = getValueName(Inst->getOperand(1)); |
| 816 | Name.insert(Name.length()-1,"$valist"); |
| 817 | // Obtain the argument handle. |
| 818 | printSimpleInstruction("ldloca",Name.c_str()); |
| 819 | printSimpleInstruction("arglist"); |
| 820 | printSimpleInstruction("call", |
| 821 | "instance void [mscorlib]System.ArgIterator::.ctor" |
| 822 | "(valuetype [mscorlib]System.RuntimeArgumentHandle)"); |
| 823 | // Save as pointer type "void*" |
| 824 | printValueLoad(Inst->getOperand(1)); |
| 825 | printSimpleInstruction("ldloca",Name.c_str()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 826 | printIndirectSave(PointerType::getUnqual(IntegerType::get(8))); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 827 | break; |
| 828 | case Intrinsic::vaend: |
| 829 | // Close argument list handle. |
| 830 | printIndirectLoad(Inst->getOperand(1)); |
| 831 | printSimpleInstruction("call","instance void [mscorlib]System.ArgIterator::End()"); |
| 832 | break; |
| 833 | case Intrinsic::vacopy: |
| 834 | // Copy "ArgIterator" valuetype. |
| 835 | printIndirectLoad(Inst->getOperand(1)); |
| 836 | printIndirectLoad(Inst->getOperand(2)); |
| 837 | printSimpleInstruction("cpobj","[mscorlib]System.ArgIterator"); |
| 838 | break; |
| 839 | default: |
| 840 | cerr << "Intrinsic ID = " << Inst->getIntrinsicID() << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 841 | LLVM_UNREACHABLE("Invalid intrinsic function"); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 846 | void MSILWriter::printCallInstruction(const Instruction* Inst) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 847 | if (isa<IntrinsicInst>(Inst)) { |
| 848 | // Handle intrinsic function. |
| 849 | printIntrinsicCall(cast<IntrinsicInst>(Inst)); |
| 850 | } else { |
| 851 | // Load arguments to stack and call function. |
| 852 | for (int I = 1, E = Inst->getNumOperands(); I!=E; ++I) |
| 853 | printValueLoad(Inst->getOperand(I)); |
| 854 | printFunctionCall(Inst->getOperand(0),Inst); |
| 855 | } |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | |
| 859 | void MSILWriter::printICmpInstruction(unsigned Predicate, const Value* Left, |
| 860 | const Value* Right) { |
| 861 | switch (Predicate) { |
| 862 | case ICmpInst::ICMP_EQ: |
| 863 | printBinaryInstruction("ceq",Left,Right); |
| 864 | break; |
| 865 | case ICmpInst::ICMP_NE: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 866 | // Emulate = not neg (Op1 eq Op2) |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 867 | printBinaryInstruction("ceq",Left,Right); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 868 | printSimpleInstruction("neg"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 869 | printSimpleInstruction("not"); |
| 870 | break; |
| 871 | case ICmpInst::ICMP_ULE: |
| 872 | case ICmpInst::ICMP_SLE: |
| 873 | // Emulate = (Op1 eq Op2) or (Op1 lt Op2) |
| 874 | printBinaryInstruction("ceq",Left,Right); |
| 875 | if (Predicate==ICmpInst::ICMP_ULE) |
| 876 | printBinaryInstruction("clt.un",Left,Right); |
| 877 | else |
| 878 | printBinaryInstruction("clt",Left,Right); |
| 879 | printSimpleInstruction("or"); |
| 880 | break; |
| 881 | case ICmpInst::ICMP_UGE: |
| 882 | case ICmpInst::ICMP_SGE: |
| 883 | // Emulate = (Op1 eq Op2) or (Op1 gt Op2) |
| 884 | printBinaryInstruction("ceq",Left,Right); |
| 885 | if (Predicate==ICmpInst::ICMP_UGE) |
| 886 | printBinaryInstruction("cgt.un",Left,Right); |
| 887 | else |
| 888 | printBinaryInstruction("cgt",Left,Right); |
| 889 | printSimpleInstruction("or"); |
| 890 | break; |
| 891 | case ICmpInst::ICMP_ULT: |
| 892 | printBinaryInstruction("clt.un",Left,Right); |
| 893 | break; |
| 894 | case ICmpInst::ICMP_SLT: |
| 895 | printBinaryInstruction("clt",Left,Right); |
| 896 | break; |
| 897 | case ICmpInst::ICMP_UGT: |
| 898 | printBinaryInstruction("cgt.un",Left,Right); |
Anton Korobeynikov | e9fd67e | 2009-07-14 09:52:47 +0000 | [diff] [blame^] | 899 | break; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 900 | case ICmpInst::ICMP_SGT: |
| 901 | printBinaryInstruction("cgt",Left,Right); |
| 902 | break; |
| 903 | default: |
| 904 | cerr << "Predicate = " << Predicate << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 905 | LLVM_UNREACHABLE("Invalid icmp predicate"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | void MSILWriter::printFCmpInstruction(unsigned Predicate, const Value* Left, |
| 911 | const Value* Right) { |
| 912 | // FIXME: Correct comparison |
| 913 | std::string NanFunc = "bool [mscorlib]System.Double::IsNaN(float64)"; |
| 914 | switch (Predicate) { |
| 915 | case FCmpInst::FCMP_UGT: |
| 916 | // X > Y || llvm_fcmp_uno(X, Y) |
| 917 | printBinaryInstruction("cgt",Left,Right); |
| 918 | printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right); |
| 919 | printSimpleInstruction("or"); |
| 920 | break; |
| 921 | case FCmpInst::FCMP_OGT: |
| 922 | // X > Y |
| 923 | printBinaryInstruction("cgt",Left,Right); |
| 924 | break; |
| 925 | case FCmpInst::FCMP_UGE: |
| 926 | // X >= Y || llvm_fcmp_uno(X, Y) |
| 927 | printBinaryInstruction("ceq",Left,Right); |
| 928 | printBinaryInstruction("cgt",Left,Right); |
| 929 | printSimpleInstruction("or"); |
| 930 | printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right); |
| 931 | printSimpleInstruction("or"); |
| 932 | break; |
| 933 | case FCmpInst::FCMP_OGE: |
| 934 | // X >= Y |
| 935 | printBinaryInstruction("ceq",Left,Right); |
| 936 | printBinaryInstruction("cgt",Left,Right); |
| 937 | printSimpleInstruction("or"); |
| 938 | break; |
| 939 | case FCmpInst::FCMP_ULT: |
| 940 | // X < Y || llvm_fcmp_uno(X, Y) |
| 941 | printBinaryInstruction("clt",Left,Right); |
| 942 | printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right); |
| 943 | printSimpleInstruction("or"); |
| 944 | break; |
| 945 | case FCmpInst::FCMP_OLT: |
| 946 | // X < Y |
| 947 | printBinaryInstruction("clt",Left,Right); |
| 948 | break; |
| 949 | case FCmpInst::FCMP_ULE: |
| 950 | // X <= Y || llvm_fcmp_uno(X, Y) |
| 951 | printBinaryInstruction("ceq",Left,Right); |
| 952 | printBinaryInstruction("clt",Left,Right); |
| 953 | printSimpleInstruction("or"); |
| 954 | printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right); |
| 955 | printSimpleInstruction("or"); |
| 956 | break; |
| 957 | case FCmpInst::FCMP_OLE: |
| 958 | // X <= Y |
| 959 | printBinaryInstruction("ceq",Left,Right); |
| 960 | printBinaryInstruction("clt",Left,Right); |
| 961 | printSimpleInstruction("or"); |
| 962 | break; |
| 963 | case FCmpInst::FCMP_UEQ: |
| 964 | // X == Y || llvm_fcmp_uno(X, Y) |
| 965 | printBinaryInstruction("ceq",Left,Right); |
| 966 | printFCmpInstruction(FCmpInst::FCMP_UNO,Left,Right); |
| 967 | printSimpleInstruction("or"); |
| 968 | break; |
| 969 | case FCmpInst::FCMP_OEQ: |
| 970 | // X == Y |
| 971 | printBinaryInstruction("ceq",Left,Right); |
| 972 | break; |
| 973 | case FCmpInst::FCMP_UNE: |
| 974 | // X != Y |
| 975 | printBinaryInstruction("ceq",Left,Right); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 976 | printSimpleInstruction("neg"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 977 | printSimpleInstruction("not"); |
| 978 | break; |
| 979 | case FCmpInst::FCMP_ONE: |
| 980 | // X != Y && llvm_fcmp_ord(X, Y) |
| 981 | printBinaryInstruction("ceq",Left,Right); |
| 982 | printSimpleInstruction("not"); |
| 983 | break; |
| 984 | case FCmpInst::FCMP_ORD: |
| 985 | // return X == X && Y == Y |
| 986 | printBinaryInstruction("ceq",Left,Left); |
| 987 | printBinaryInstruction("ceq",Right,Right); |
| 988 | printSimpleInstruction("or"); |
| 989 | break; |
| 990 | case FCmpInst::FCMP_UNO: |
| 991 | // X != X || Y != Y |
| 992 | printBinaryInstruction("ceq",Left,Left); |
| 993 | printSimpleInstruction("not"); |
| 994 | printBinaryInstruction("ceq",Right,Right); |
| 995 | printSimpleInstruction("not"); |
| 996 | printSimpleInstruction("or"); |
| 997 | break; |
| 998 | default: |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 999 | LLVM_UNREACHABLE("Illegal FCmp predicate"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | void MSILWriter::printInvokeInstruction(const InvokeInst* Inst) { |
| 1005 | std::string Label = "leave$normal_"+utostr(getUniqID()); |
| 1006 | Out << ".try {\n"; |
| 1007 | // Load arguments |
| 1008 | for (int I = 3, E = Inst->getNumOperands(); I!=E; ++I) |
| 1009 | printValueLoad(Inst->getOperand(I)); |
| 1010 | // Print call instruction |
| 1011 | printFunctionCall(Inst->getOperand(0),Inst); |
| 1012 | // Save function result and leave "try" block |
| 1013 | printValueSave(Inst); |
| 1014 | printSimpleInstruction("leave",Label.c_str()); |
| 1015 | Out << "}\n"; |
| 1016 | Out << "catch [mscorlib]System.Exception {\n"; |
| 1017 | // Redirect to unwind block |
| 1018 | printSimpleInstruction("pop"); |
| 1019 | printBranchToBlock(Inst->getParent(),NULL,Inst->getUnwindDest()); |
| 1020 | Out << "}\n" << Label << ":\n"; |
| 1021 | // Redirect to continue block |
| 1022 | printBranchToBlock(Inst->getParent(),NULL,Inst->getNormalDest()); |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | void MSILWriter::printSwitchInstruction(const SwitchInst* Inst) { |
| 1027 | // FIXME: Emulate with IL "switch" instruction |
| 1028 | // Emulate = if () else if () else if () else ... |
| 1029 | for (unsigned int I = 1, E = Inst->getNumCases(); I!=E; ++I) { |
| 1030 | printValueLoad(Inst->getCondition()); |
| 1031 | printValueLoad(Inst->getCaseValue(I)); |
| 1032 | printSimpleInstruction("ceq"); |
| 1033 | // Condition jump to successor block |
| 1034 | printBranchToBlock(Inst->getParent(),Inst->getSuccessor(I),NULL); |
| 1035 | } |
| 1036 | // Jump to default block |
| 1037 | printBranchToBlock(Inst->getParent(),NULL,Inst->getDefaultDest()); |
| 1038 | } |
| 1039 | |
| 1040 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1041 | void MSILWriter::printVAArgInstruction(const VAArgInst* Inst) { |
| 1042 | printIndirectLoad(Inst->getOperand(0)); |
| 1043 | printSimpleInstruction("call", |
| 1044 | "instance typedref [mscorlib]System.ArgIterator::GetNextArg()"); |
| 1045 | printSimpleInstruction("refanyval","void*"); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1046 | std::string Name = |
| 1047 | "ldind."+getTypePostfix(PointerType::getUnqual(IntegerType::get(8)),false); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1048 | printSimpleInstruction(Name.c_str()); |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | void MSILWriter::printAllocaInstruction(const AllocaInst* Inst) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1053 | uint64_t Size = TD->getTypeAllocSize(Inst->getAllocatedType()); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1054 | // Constant optimization. |
| 1055 | if (const ConstantInt* CInt = dyn_cast<ConstantInt>(Inst->getOperand(0))) { |
| 1056 | printPtrLoad(CInt->getZExtValue()*Size); |
| 1057 | } else { |
| 1058 | printPtrLoad(Size); |
| 1059 | printValueLoad(Inst->getOperand(0)); |
| 1060 | printSimpleInstruction("mul"); |
| 1061 | } |
| 1062 | printSimpleInstruction("localloc"); |
| 1063 | } |
| 1064 | |
| 1065 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1066 | void MSILWriter::printInstruction(const Instruction* Inst) { |
| 1067 | const Value *Left = 0, *Right = 0; |
| 1068 | if (Inst->getNumOperands()>=1) Left = Inst->getOperand(0); |
| 1069 | if (Inst->getNumOperands()>=2) Right = Inst->getOperand(1); |
| 1070 | // Print instruction |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1071 | // FIXME: "ShuffleVector","ExtractElement","InsertElement" support. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1072 | switch (Inst->getOpcode()) { |
| 1073 | // Terminator |
| 1074 | case Instruction::Ret: |
| 1075 | if (Inst->getNumOperands()) { |
| 1076 | printValueLoad(Left); |
| 1077 | printSimpleInstruction("ret"); |
| 1078 | } else |
| 1079 | printSimpleInstruction("ret"); |
| 1080 | break; |
| 1081 | case Instruction::Br: |
| 1082 | printBranchInstruction(cast<BranchInst>(Inst)); |
| 1083 | break; |
| 1084 | // Binary |
| 1085 | case Instruction::Add: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1086 | case Instruction::FAdd: |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1087 | printBinaryInstruction("add",Left,Right); |
| 1088 | break; |
| 1089 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1090 | case Instruction::FSub: |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1091 | printBinaryInstruction("sub",Left,Right); |
| 1092 | break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1093 | case Instruction::Mul: |
| 1094 | case Instruction::FMul: |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1095 | printBinaryInstruction("mul",Left,Right); |
| 1096 | break; |
| 1097 | case Instruction::UDiv: |
| 1098 | printBinaryInstruction("div.un",Left,Right); |
| 1099 | break; |
| 1100 | case Instruction::SDiv: |
| 1101 | case Instruction::FDiv: |
| 1102 | printBinaryInstruction("div",Left,Right); |
| 1103 | break; |
| 1104 | case Instruction::URem: |
| 1105 | printBinaryInstruction("rem.un",Left,Right); |
| 1106 | break; |
| 1107 | case Instruction::SRem: |
| 1108 | case Instruction::FRem: |
| 1109 | printBinaryInstruction("rem",Left,Right); |
| 1110 | break; |
| 1111 | // Binary Condition |
| 1112 | case Instruction::ICmp: |
| 1113 | printICmpInstruction(cast<ICmpInst>(Inst)->getPredicate(),Left,Right); |
| 1114 | break; |
| 1115 | case Instruction::FCmp: |
| 1116 | printFCmpInstruction(cast<FCmpInst>(Inst)->getPredicate(),Left,Right); |
| 1117 | break; |
| 1118 | // Bitwise Binary |
| 1119 | case Instruction::And: |
| 1120 | printBinaryInstruction("and",Left,Right); |
| 1121 | break; |
| 1122 | case Instruction::Or: |
| 1123 | printBinaryInstruction("or",Left,Right); |
| 1124 | break; |
| 1125 | case Instruction::Xor: |
| 1126 | printBinaryInstruction("xor",Left,Right); |
| 1127 | break; |
| 1128 | case Instruction::Shl: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1129 | printValueLoad(Left); |
| 1130 | printValueLoad(Right); |
| 1131 | printSimpleInstruction("conv.i4"); |
| 1132 | printSimpleInstruction("shl"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | case Instruction::LShr: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1135 | printValueLoad(Left); |
| 1136 | printValueLoad(Right); |
| 1137 | printSimpleInstruction("conv.i4"); |
| 1138 | printSimpleInstruction("shr.un"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1139 | break; |
| 1140 | case Instruction::AShr: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1141 | printValueLoad(Left); |
| 1142 | printValueLoad(Right); |
| 1143 | printSimpleInstruction("conv.i4"); |
| 1144 | printSimpleInstruction("shr"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | case Instruction::Select: |
| 1147 | printSelectInstruction(Inst->getOperand(0),Inst->getOperand(1),Inst->getOperand(2)); |
| 1148 | break; |
| 1149 | case Instruction::Load: |
| 1150 | printIndirectLoad(Inst->getOperand(0)); |
| 1151 | break; |
| 1152 | case Instruction::Store: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1153 | printIndirectSave(Inst->getOperand(1), Inst->getOperand(0)); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1154 | break; |
| 1155 | case Instruction::Trunc: |
| 1156 | case Instruction::ZExt: |
| 1157 | case Instruction::SExt: |
| 1158 | case Instruction::FPTrunc: |
| 1159 | case Instruction::FPExt: |
| 1160 | case Instruction::UIToFP: |
| 1161 | case Instruction::SIToFP: |
| 1162 | case Instruction::FPToUI: |
| 1163 | case Instruction::FPToSI: |
| 1164 | case Instruction::PtrToInt: |
| 1165 | case Instruction::IntToPtr: |
| 1166 | case Instruction::BitCast: |
| 1167 | printCastInstruction(Inst->getOpcode(),Left, |
| 1168 | cast<CastInst>(Inst)->getDestTy()); |
| 1169 | break; |
| 1170 | case Instruction::GetElementPtr: |
| 1171 | printGepInstruction(Inst->getOperand(0),gep_type_begin(Inst), |
| 1172 | gep_type_end(Inst)); |
| 1173 | break; |
| 1174 | case Instruction::Call: |
| 1175 | printCallInstruction(cast<CallInst>(Inst)); |
| 1176 | break; |
| 1177 | case Instruction::Invoke: |
| 1178 | printInvokeInstruction(cast<InvokeInst>(Inst)); |
| 1179 | break; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1180 | case Instruction::Unwind: |
| 1181 | printSimpleInstruction("newobj", |
| 1182 | "instance void [mscorlib]System.Exception::.ctor()"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1183 | printSimpleInstruction("throw"); |
| 1184 | break; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1185 | case Instruction::Switch: |
| 1186 | printSwitchInstruction(cast<SwitchInst>(Inst)); |
| 1187 | break; |
| 1188 | case Instruction::Alloca: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1189 | printAllocaInstruction(cast<AllocaInst>(Inst)); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1190 | break; |
| 1191 | case Instruction::Malloc: |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1192 | LLVM_UNREACHABLE("LowerAllocationsPass used"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1193 | break; |
| 1194 | case Instruction::Free: |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1195 | LLVM_UNREACHABLE("LowerAllocationsPass used"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1196 | break; |
| 1197 | case Instruction::Unreachable: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1198 | printSimpleInstruction("ldstr", "\"Unreachable instruction\""); |
| 1199 | printSimpleInstruction("newobj", |
| 1200 | "instance void [mscorlib]System.Exception::.ctor(string)"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1201 | printSimpleInstruction("throw"); |
| 1202 | break; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1203 | case Instruction::VAArg: |
| 1204 | printVAArgInstruction(cast<VAArgInst>(Inst)); |
| 1205 | break; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1206 | default: |
| 1207 | cerr << "Instruction = " << Inst->getName() << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1208 | LLVM_UNREACHABLE("Unsupported instruction"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | void MSILWriter::printLoop(const Loop* L) { |
| 1214 | Out << getLabelName(L->getHeader()->getName()) << ":\n"; |
| 1215 | const std::vector<BasicBlock*>& blocks = L->getBlocks(); |
| 1216 | for (unsigned I = 0, E = blocks.size(); I!=E; I++) { |
| 1217 | BasicBlock* BB = blocks[I]; |
| 1218 | Loop* BBLoop = LInfo->getLoopFor(BB); |
| 1219 | if (BBLoop == L) |
| 1220 | printBasicBlock(BB); |
| 1221 | else if (BB==BBLoop->getHeader() && BBLoop->getParentLoop()==L) |
| 1222 | printLoop(BBLoop); |
| 1223 | } |
| 1224 | printSimpleInstruction("br",getLabelName(L->getHeader()->getName()).c_str()); |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | void MSILWriter::printBasicBlock(const BasicBlock* BB) { |
| 1229 | Out << getLabelName(BB) << ":\n"; |
| 1230 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { |
| 1231 | const Instruction* Inst = I; |
| 1232 | // Comment llvm original instruction |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 1233 | // Out << "\n//" << *Inst << "\n"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1234 | // Do not handle PHI instruction in current block |
| 1235 | if (Inst->getOpcode()==Instruction::PHI) continue; |
| 1236 | // Print instruction |
| 1237 | printInstruction(Inst); |
| 1238 | // Save result |
| 1239 | if (Inst->getType()!=Type::VoidTy) { |
| 1240 | // Do not save value after invoke, it done in "try" block |
| 1241 | if (Inst->getOpcode()==Instruction::Invoke) continue; |
| 1242 | printValueSave(Inst); |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | void MSILWriter::printLocalVariables(const Function& F) { |
| 1249 | std::string Name; |
| 1250 | const Type* Ty = NULL; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1251 | std::set<const Value*> Printed; |
| 1252 | const Value* VaList = NULL; |
| 1253 | unsigned StackDepth = 8; |
| 1254 | // Find local variables |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1255 | for (const_inst_iterator I = inst_begin(&F), E = inst_end(&F); I!=E; ++I) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1256 | if (I->getOpcode()==Instruction::Call || |
| 1257 | I->getOpcode()==Instruction::Invoke) { |
| 1258 | // Test stack depth. |
| 1259 | if (StackDepth<I->getNumOperands()) |
| 1260 | StackDepth = I->getNumOperands(); |
| 1261 | } |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1262 | const AllocaInst* AI = dyn_cast<AllocaInst>(&*I); |
| 1263 | if (AI && !isa<GlobalVariable>(AI)) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1264 | // Local variable allocation. |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1265 | Ty = PointerType::getUnqual(AI->getAllocatedType()); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1266 | Name = getValueName(AI); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1267 | Out << "\t.locals (" << getTypeName(Ty) << Name << ")\n"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1268 | } else if (I->getType()!=Type::VoidTy) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1269 | // Operation result. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1270 | Ty = I->getType(); |
| 1271 | Name = getValueName(&*I); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1272 | Out << "\t.locals (" << getTypeName(Ty) << Name << ")\n"; |
| 1273 | } |
| 1274 | // Test on 'va_list' variable |
| 1275 | bool isVaList = false; |
| 1276 | if (const VAArgInst* VaInst = dyn_cast<VAArgInst>(&*I)) { |
| 1277 | // "va_list" as "va_arg" instruction operand. |
| 1278 | isVaList = true; |
| 1279 | VaList = VaInst->getOperand(0); |
| 1280 | } else if (const IntrinsicInst* Inst = dyn_cast<IntrinsicInst>(&*I)) { |
| 1281 | // "va_list" as intrinsic function operand. |
| 1282 | switch (Inst->getIntrinsicID()) { |
| 1283 | case Intrinsic::vastart: |
| 1284 | case Intrinsic::vaend: |
| 1285 | case Intrinsic::vacopy: |
| 1286 | isVaList = true; |
| 1287 | VaList = Inst->getOperand(1); |
| 1288 | break; |
| 1289 | default: |
| 1290 | isVaList = false; |
| 1291 | } |
| 1292 | } |
| 1293 | // Print "va_list" variable. |
| 1294 | if (isVaList && Printed.insert(VaList).second) { |
| 1295 | Name = getValueName(VaList); |
| 1296 | Name.insert(Name.length()-1,"$valist"); |
| 1297 | Out << "\t.locals (valuetype [mscorlib]System.ArgIterator " |
| 1298 | << Name << ")\n"; |
| 1299 | } |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1300 | } |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1301 | printSimpleInstruction(".maxstack",utostr(StackDepth*2).c_str()); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | void MSILWriter::printFunctionBody(const Function& F) { |
| 1306 | // Print body |
| 1307 | for (Function::const_iterator I = F.begin(), E = F.end(); I!=E; ++I) { |
| 1308 | if (Loop *L = LInfo->getLoopFor(I)) { |
| 1309 | if (L->getHeader()==I && L->getParentLoop()==0) |
| 1310 | printLoop(L); |
| 1311 | } else { |
| 1312 | printBasicBlock(I); |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | void MSILWriter::printConstantExpr(const ConstantExpr* CE) { |
| 1319 | const Value *left = 0, *right = 0; |
| 1320 | if (CE->getNumOperands()>=1) left = CE->getOperand(0); |
| 1321 | if (CE->getNumOperands()>=2) right = CE->getOperand(1); |
| 1322 | // Print instruction |
| 1323 | switch (CE->getOpcode()) { |
| 1324 | case Instruction::Trunc: |
| 1325 | case Instruction::ZExt: |
| 1326 | case Instruction::SExt: |
| 1327 | case Instruction::FPTrunc: |
| 1328 | case Instruction::FPExt: |
| 1329 | case Instruction::UIToFP: |
| 1330 | case Instruction::SIToFP: |
| 1331 | case Instruction::FPToUI: |
| 1332 | case Instruction::FPToSI: |
| 1333 | case Instruction::PtrToInt: |
| 1334 | case Instruction::IntToPtr: |
| 1335 | case Instruction::BitCast: |
| 1336 | printCastInstruction(CE->getOpcode(),left,CE->getType()); |
| 1337 | break; |
| 1338 | case Instruction::GetElementPtr: |
| 1339 | printGepInstruction(CE->getOperand(0),gep_type_begin(CE),gep_type_end(CE)); |
| 1340 | break; |
| 1341 | case Instruction::ICmp: |
| 1342 | printICmpInstruction(CE->getPredicate(),left,right); |
| 1343 | break; |
| 1344 | case Instruction::FCmp: |
| 1345 | printFCmpInstruction(CE->getPredicate(),left,right); |
| 1346 | break; |
| 1347 | case Instruction::Select: |
| 1348 | printSelectInstruction(CE->getOperand(0),CE->getOperand(1),CE->getOperand(2)); |
| 1349 | break; |
| 1350 | case Instruction::Add: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1351 | case Instruction::FAdd: |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1352 | printBinaryInstruction("add",left,right); |
| 1353 | break; |
| 1354 | case Instruction::Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1355 | case Instruction::FSub: |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1356 | printBinaryInstruction("sub",left,right); |
| 1357 | break; |
| 1358 | case Instruction::Mul: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1359 | case Instruction::FMul: |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1360 | printBinaryInstruction("mul",left,right); |
| 1361 | break; |
| 1362 | case Instruction::UDiv: |
| 1363 | printBinaryInstruction("div.un",left,right); |
| 1364 | break; |
| 1365 | case Instruction::SDiv: |
| 1366 | case Instruction::FDiv: |
| 1367 | printBinaryInstruction("div",left,right); |
| 1368 | break; |
| 1369 | case Instruction::URem: |
| 1370 | printBinaryInstruction("rem.un",left,right); |
| 1371 | break; |
| 1372 | case Instruction::SRem: |
| 1373 | case Instruction::FRem: |
| 1374 | printBinaryInstruction("rem",left,right); |
| 1375 | break; |
| 1376 | case Instruction::And: |
| 1377 | printBinaryInstruction("and",left,right); |
| 1378 | break; |
| 1379 | case Instruction::Or: |
| 1380 | printBinaryInstruction("or",left,right); |
| 1381 | break; |
| 1382 | case Instruction::Xor: |
| 1383 | printBinaryInstruction("xor",left,right); |
| 1384 | break; |
| 1385 | case Instruction::Shl: |
| 1386 | printBinaryInstruction("shl",left,right); |
| 1387 | break; |
| 1388 | case Instruction::LShr: |
| 1389 | printBinaryInstruction("shr.un",left,right); |
| 1390 | break; |
| 1391 | case Instruction::AShr: |
| 1392 | printBinaryInstruction("shr",left,right); |
| 1393 | break; |
| 1394 | default: |
| 1395 | cerr << "Expression = " << *CE << "\n"; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1396 | LLVM_UNREACHABLE("Invalid constant expression"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | void MSILWriter::printStaticInitializerList() { |
| 1402 | // List of global variables with uninitialized fields. |
| 1403 | for (std::map<const GlobalVariable*,std::vector<StaticInitializer> >::iterator |
| 1404 | VarI = StaticInitList.begin(), VarE = StaticInitList.end(); VarI!=VarE; |
| 1405 | ++VarI) { |
| 1406 | const std::vector<StaticInitializer>& InitList = VarI->second; |
| 1407 | if (InitList.empty()) continue; |
| 1408 | // For each uninitialized field. |
| 1409 | for (std::vector<StaticInitializer>::const_iterator I = InitList.begin(), |
| 1410 | E = InitList.end(); I!=E; ++I) { |
| 1411 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(I->constant)) { |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 1412 | // Out << "\n// Init " << getValueName(VarI->first) << ", offset " << |
| 1413 | // utostr(I->offset) << ", type "<< *I->constant->getType() << "\n\n"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1414 | // Load variable address |
| 1415 | printValueLoad(VarI->first); |
| 1416 | // Add offset |
| 1417 | if (I->offset!=0) { |
| 1418 | printPtrLoad(I->offset); |
| 1419 | printSimpleInstruction("add"); |
| 1420 | } |
| 1421 | // Load value |
| 1422 | printConstantExpr(CE); |
| 1423 | // Save result at offset |
| 1424 | std::string postfix = getTypePostfix(CE->getType(),true); |
| 1425 | if (*postfix.begin()=='u') *postfix.begin() = 'i'; |
| 1426 | postfix = "stind."+postfix; |
| 1427 | printSimpleInstruction(postfix.c_str()); |
| 1428 | } else { |
| 1429 | cerr << "Constant = " << *I->constant << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1430 | LLVM_UNREACHABLE("Invalid static initializer"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | void MSILWriter::printFunction(const Function& F) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1438 | bool isSigned = F.paramHasAttr(0, Attribute::SExt); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1439 | Out << "\n.method static "; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 1440 | Out << (F.hasLocalLinkage() ? "private " : "public "); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1441 | if (F.isVarArg()) Out << "vararg "; |
| 1442 | Out << getTypeName(F.getReturnType(),isSigned) << |
| 1443 | getConvModopt(F.getCallingConv()) << getValueName(&F) << '\n'; |
| 1444 | // Arguments |
| 1445 | Out << "\t("; |
| 1446 | unsigned ArgIdx = 1; |
| 1447 | for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I!=E; |
| 1448 | ++I, ++ArgIdx) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1449 | isSigned = F.paramHasAttr(ArgIdx, Attribute::SExt); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1450 | if (I!=F.arg_begin()) Out << ", "; |
| 1451 | Out << getTypeName(I->getType(),isSigned) << getValueName(I); |
| 1452 | } |
| 1453 | Out << ") cil managed\n"; |
| 1454 | // Body |
| 1455 | Out << "{\n"; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1456 | printLocalVariables(F); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1457 | printFunctionBody(F); |
| 1458 | Out << "}\n"; |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | void MSILWriter::printDeclarations(const TypeSymbolTable& ST) { |
| 1463 | std::string Name; |
| 1464 | std::set<const Type*> Printed; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1465 | for (std::set<const Type*>::const_iterator |
| 1466 | UI = UsedTypes->begin(), UE = UsedTypes->end(); UI!=UE; ++UI) { |
| 1467 | const Type* Ty = *UI; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1468 | if (isa<ArrayType>(Ty) || isa<VectorType>(Ty) || isa<StructType>(Ty)) |
| 1469 | Name = getTypeName(Ty, false, true); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1470 | // Type with no need to declare. |
| 1471 | else continue; |
| 1472 | // Print not duplicated type |
| 1473 | if (Printed.insert(Ty).second) { |
| 1474 | Out << ".class value explicit ansi sealed '" << Name << "'"; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1475 | Out << " { .pack " << 1 << " .size " << TD->getTypeAllocSize(Ty); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 1476 | Out << " }\n\n"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | unsigned int MSILWriter::getBitWidth(const Type* Ty) { |
| 1483 | unsigned int N = Ty->getPrimitiveSizeInBits(); |
| 1484 | assert(N!=0 && "Invalid type in getBitWidth()"); |
| 1485 | switch (N) { |
| 1486 | case 1: |
| 1487 | case 8: |
| 1488 | case 16: |
| 1489 | case 32: |
| 1490 | case 64: |
| 1491 | return N; |
| 1492 | default: |
| 1493 | cerr << "Bits = " << N << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1494 | LLVM_UNREACHABLE("Unsupported integer width"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1495 | } |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 1496 | return 0; // Not reached |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | void MSILWriter::printStaticConstant(const Constant* C, uint64_t& Offset) { |
| 1501 | uint64_t TySize = 0; |
| 1502 | const Type* Ty = C->getType(); |
| 1503 | // Print zero initialized constant. |
| 1504 | if (isa<ConstantAggregateZero>(C) || C->isNullValue()) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1505 | TySize = TD->getTypeAllocSize(C->getType()); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1506 | Offset += TySize; |
| 1507 | Out << "int8 (0) [" << TySize << "]"; |
| 1508 | return; |
| 1509 | } |
| 1510 | // Print constant initializer |
| 1511 | switch (Ty->getTypeID()) { |
| 1512 | case Type::IntegerTyID: { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1513 | TySize = TD->getTypeAllocSize(Ty); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1514 | const ConstantInt* Int = cast<ConstantInt>(C); |
| 1515 | Out << getPrimitiveTypeName(Ty,true) << "(" << Int->getSExtValue() << ")"; |
| 1516 | break; |
| 1517 | } |
| 1518 | case Type::FloatTyID: |
| 1519 | case Type::DoubleTyID: { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1520 | TySize = TD->getTypeAllocSize(Ty); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1521 | const ConstantFP* FP = cast<ConstantFP>(C); |
| 1522 | if (Ty->getTypeID() == Type::FloatTyID) |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1523 | Out << "int32 (" << |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 1524 | (uint32_t)FP->getValueAPF().bitcastToAPInt().getZExtValue() << ')'; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1525 | else |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1526 | Out << "int64 (" << |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 1527 | FP->getValueAPF().bitcastToAPInt().getZExtValue() << ')'; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | case Type::ArrayTyID: |
| 1531 | case Type::VectorTyID: |
| 1532 | case Type::StructTyID: |
| 1533 | for (unsigned I = 0, E = C->getNumOperands(); I<E; I++) { |
| 1534 | if (I!=0) Out << ",\n"; |
| 1535 | printStaticConstant(C->getOperand(I),Offset); |
| 1536 | } |
| 1537 | break; |
| 1538 | case Type::PointerTyID: |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1539 | TySize = TD->getTypeAllocSize(C->getType()); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1540 | // Initialize with global variable address |
| 1541 | if (const GlobalVariable *G = dyn_cast<GlobalVariable>(C)) { |
| 1542 | std::string name = getValueName(G); |
| 1543 | Out << "&(" << name.insert(name.length()-1,"$data") << ")"; |
| 1544 | } else { |
| 1545 | // Dynamic initialization |
| 1546 | if (!isa<ConstantPointerNull>(C) && !C->isNullValue()) |
| 1547 | InitListPtr->push_back(StaticInitializer(C,Offset)); |
| 1548 | // Null pointer initialization |
| 1549 | if (TySize==4) Out << "int32 (0)"; |
| 1550 | else if (TySize==8) Out << "int64 (0)"; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1551 | else LLVM_UNREACHABLE("Invalid pointer size"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1552 | } |
| 1553 | break; |
| 1554 | default: |
| 1555 | cerr << "TypeID = " << Ty->getTypeID() << '\n'; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1556 | LLVM_UNREACHABLE("Invalid type in printStaticConstant()"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1557 | } |
| 1558 | // Increase offset. |
| 1559 | Offset += TySize; |
| 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | void MSILWriter::printStaticInitializer(const Constant* C, |
| 1564 | const std::string& Name) { |
| 1565 | switch (C->getType()->getTypeID()) { |
| 1566 | case Type::IntegerTyID: |
| 1567 | case Type::FloatTyID: |
| 1568 | case Type::DoubleTyID: |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1569 | Out << getPrimitiveTypeName(C->getType(), false); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1570 | break; |
| 1571 | case Type::ArrayTyID: |
| 1572 | case Type::VectorTyID: |
| 1573 | case Type::StructTyID: |
| 1574 | case Type::PointerTyID: |
| 1575 | Out << getTypeName(C->getType()); |
| 1576 | break; |
| 1577 | default: |
| 1578 | cerr << "Type = " << *C << "\n"; |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 1579 | LLVM_UNREACHABLE("Invalid constant type"); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1580 | } |
| 1581 | // Print initializer |
| 1582 | std::string label = Name; |
| 1583 | label.insert(label.length()-1,"$data"); |
| 1584 | Out << Name << " at " << label << '\n'; |
| 1585 | Out << ".data " << label << " = {\n"; |
| 1586 | uint64_t offset = 0; |
| 1587 | printStaticConstant(C,offset); |
| 1588 | Out << "\n}\n\n"; |
| 1589 | } |
| 1590 | |
| 1591 | |
| 1592 | void MSILWriter::printVariableDefinition(const GlobalVariable* G) { |
| 1593 | const Constant* C = G->getInitializer(); |
| 1594 | if (C->isNullValue() || isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) |
| 1595 | InitListPtr = 0; |
| 1596 | else |
| 1597 | InitListPtr = &StaticInitList[G]; |
| 1598 | printStaticInitializer(C,getValueName(G)); |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | void MSILWriter::printGlobalVariables() { |
| 1603 | if (ModulePtr->global_empty()) return; |
| 1604 | Module::global_iterator I,E; |
| 1605 | for (I = ModulePtr->global_begin(), E = ModulePtr->global_end(); I!=E; ++I) { |
| 1606 | // Variable definition |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1607 | Out << ".field static " << (I->isDeclaration() ? "public " : |
| 1608 | "private "); |
| 1609 | if (I->isDeclaration()) { |
| 1610 | Out << getTypeName(I->getType()) << getValueName(&*I) << "\n\n"; |
| 1611 | } else |
| 1612 | printVariableDefinition(&*I); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1617 | const char* MSILWriter::getLibraryName(const Function* F) { |
| 1618 | return getLibraryForSymbol(F->getName().c_str(), true, F->getCallingConv()); |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | const char* MSILWriter::getLibraryName(const GlobalVariable* GV) { |
Chris Lattner | b09d2cc | 2009-07-14 06:18:50 +0000 | [diff] [blame] | 1623 | return getLibraryForSymbol(Mang->getMangledName(GV).c_str(), false, 0); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | const char* MSILWriter::getLibraryForSymbol(const char* Name, bool isFunction, |
| 1628 | unsigned CallingConv) { |
| 1629 | // TODO: Read *.def file with function and libraries definitions. |
| 1630 | return "MSVCRT.DLL"; |
| 1631 | } |
| 1632 | |
| 1633 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1634 | void MSILWriter::printExternals() { |
| 1635 | Module::const_iterator I,E; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1636 | // Functions. |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1637 | for (I=ModulePtr->begin(),E=ModulePtr->end(); I!=E; ++I) { |
| 1638 | // Skip intrisics |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 1639 | if (I->isIntrinsic()) continue; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1640 | if (I->isDeclaration()) { |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1641 | const Function* F = I; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1642 | std::string Name = getConvModopt(F->getCallingConv())+getValueName(F); |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1643 | std::string Sig = |
| 1644 | getCallSignature(cast<FunctionType>(F->getFunctionType()), NULL, Name); |
| 1645 | Out << ".method static hidebysig pinvokeimpl(\"" |
| 1646 | << getLibraryName(F) << "\")\n\t" << Sig << " preservesig {}\n\n"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1647 | } |
| 1648 | } |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1649 | // External variables and static initialization. |
| 1650 | Out << |
| 1651 | ".method public hidebysig static pinvokeimpl(\"KERNEL32.DLL\" ansi winapi)" |
| 1652 | " native int LoadLibrary(string) preservesig {}\n" |
| 1653 | ".method public hidebysig static pinvokeimpl(\"KERNEL32.DLL\" ansi winapi)" |
| 1654 | " native int GetProcAddress(native int, string) preservesig {}\n"; |
| 1655 | Out << |
| 1656 | ".method private static void* $MSIL_Import(string lib,string sym)\n" |
| 1657 | " managed cil\n{\n" |
| 1658 | "\tldarg\tlib\n" |
| 1659 | "\tcall\tnative int LoadLibrary(string)\n" |
| 1660 | "\tldarg\tsym\n" |
| 1661 | "\tcall\tnative int GetProcAddress(native int,string)\n" |
| 1662 | "\tdup\n" |
| 1663 | "\tbrtrue\tL_01\n" |
| 1664 | "\tldstr\t\"Can no import variable\"\n" |
| 1665 | "\tnewobj\tinstance void [mscorlib]System.Exception::.ctor(string)\n" |
| 1666 | "\tthrow\n" |
| 1667 | "L_01:\n" |
| 1668 | "\tret\n" |
| 1669 | "}\n\n" |
| 1670 | ".method static private void $MSIL_Init() managed cil\n{\n"; |
| 1671 | printStaticInitializerList(); |
| 1672 | // Foreach global variable. |
| 1673 | for (Module::global_iterator I = ModulePtr->global_begin(), |
| 1674 | E = ModulePtr->global_end(); I!=E; ++I) { |
| 1675 | if (!I->isDeclaration() || !I->hasDLLImportLinkage()) continue; |
| 1676 | // Use "LoadLibrary"/"GetProcAddress" to recive variable address. |
| 1677 | std::string Label = "not_null$_"+utostr(getUniqID()); |
| 1678 | std::string Tmp = getTypeName(I->getType())+getValueName(&*I); |
| 1679 | printSimpleInstruction("ldsflda",Tmp.c_str()); |
| 1680 | Out << "\tldstr\t\"" << getLibraryName(&*I) << "\"\n"; |
Chris Lattner | b09d2cc | 2009-07-14 06:18:50 +0000 | [diff] [blame] | 1681 | Out << "\tldstr\t\"" << Mang->getMangledName(&*I) << "\"\n"; |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1682 | printSimpleInstruction("call","void* $MSIL_Import(string,string)"); |
| 1683 | printIndirectSave(I->getType()); |
| 1684 | } |
| 1685 | printSimpleInstruction("ret"); |
| 1686 | Out << "}\n\n"; |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Anton Korobeynikov | f13090c | 2007-05-06 20:13:33 +0000 | [diff] [blame] | 1689 | |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1690 | //===----------------------------------------------------------------------===// |
Bill Wendling | 85db3a9 | 2008-02-26 10:57:23 +0000 | [diff] [blame] | 1691 | // External Interface declaration |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1692 | //===----------------------------------------------------------------------===// |
| 1693 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 1694 | bool MSILTarget::addPassesToEmitWholeFile(PassManager &PM, raw_ostream &o, |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 1695 | CodeGenFileType FileType, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 1696 | CodeGenOpt::Level OptLevel) |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1697 | { |
| 1698 | if (FileType != TargetMachine::AssemblyFile) return true; |
| 1699 | MSILWriter* Writer = new MSILWriter(o); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 1700 | PM.add(createGCLoweringPass()); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1701 | PM.add(createLowerAllocationsPass(true)); |
| 1702 | // FIXME: Handle switch trougth native IL instruction "switch" |
| 1703 | PM.add(createLowerSwitchPass()); |
| 1704 | PM.add(createCFGSimplificationPass()); |
| 1705 | PM.add(new MSILModule(Writer->UsedTypes,Writer->TD)); |
| 1706 | PM.add(Writer); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1707 | PM.add(createGCInfoDeleter()); |
Anton Korobeynikov | 099883f | 2007-03-21 21:38:25 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | } |