#include "llvm/TypeSymbolTable.h"
#include "llvm/Analysis/ConstantsScanner.h"
#include "llvm/Support/CallSite.h"
+#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/Target/TargetRegistry.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/Passes.h"
+using namespace llvm;
-namespace {
+namespace llvm {
// TargetMachine for the MSIL
- struct VISIBILITY_HIDDEN MSILTarget : public TargetMachine {
- const TargetData DataLayout; // Calculates type size & alignment
-
- MSILTarget(const Module &M, const std::string &FS)
- : DataLayout(&M) {}
+ struct MSILTarget : public TargetMachine {
+ MSILTarget(const Target &T, const std::string &TT, const std::string &FS)
+ : TargetMachine(T) {}
virtual bool WantsWholeFile() const { return true; }
- virtual bool addPassesToEmitWholeFile(PassManager &PM, raw_ostream &Out,
- CodeGenFileType FileType, bool Fast);
-
- // This class always works, but shouldn't be the default in most cases.
- static unsigned getModuleMatchQuality(const Module &M) { return 1; }
+ virtual bool addPassesToEmitWholeFile(PassManager &PM,
+ formatted_raw_ostream &Out,
+ CodeGenFileType FileType,
+ CodeGenOpt::Level OptLevel);
- virtual const TargetData *getTargetData() const { return &DataLayout; }
+ virtual const TargetData *getTargetData() const { return 0; }
};
}
-
-static RegisterTarget<MSILTarget> X("msil", " MSIL backend");
+extern "C" void LLVMInitializeMSILTarget() {
+ // Register the target.
+ RegisterTargetMachine<MSILTarget> X(TheMSILTarget);
+}
bool MSILModule::runOnModule(Module &M) {
ModulePtr = &M;
bool MSILWriter::runOnFunction(Function &F) {
if (F.isDeclaration()) return false;
+
+ // Do not codegen any 'available_externally' functions at all, they have
+ // definitions outside the translation unit.
+ if (F.hasAvailableExternallyLinkage())
+ return false;
+
LInfo = &getAnalysis<LoopInfo>();
printFunction(F);
return false;
Out << "\tldc.i4.0\n\tret\n}\n";
return;
}
- bool BadSig = true;;
+ bool BadSig = true;
std::string Args("");
Function::const_arg_iterator Arg1,Arg2;
std::string MSILWriter::getValueName(const Value* V) {
+ std::string Name;
+ if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+ Name = Mang->getMangledName(GV);
+ else {
+ unsigned &No = AnonValueNumbers[V];
+ if (No == 0) No = ++NextAnonValueNumber;
+ Name = "tmp" + utostr(No);
+ }
+
// Name into the quotes allow control and space characters.
- return "'"+Mang->getValueName(V)+"'";
+ return "'"+Name+"'";
}
std::string MSILWriter::getLabelName(const Value* V) {
- return getLabelName(Mang->getValueName(V));
+ std::string Name;
+ if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+ Name = Mang->getMangledName(GV);
+ else {
+ unsigned &No = AnonValueNumbers[V];
+ if (No == 0) No = ++NextAnonValueNumber;
+ Name = "tmp" + utostr(No);
+ }
+
+ return getLabelName(Name);
}
-std::string MSILWriter::getConvModopt(unsigned CallingConvID) {
+std::string MSILWriter::getConvModopt(CallingConv::ID CallingConvID) {
switch (CallingConvID) {
case CallingConv::C:
case CallingConv::Cold:
case CallingConv::X86_StdCall:
return "modopt([mscorlib]System.Runtime.CompilerServices.CallConvStdcall) ";
default:
- cerr << "CallingConvID = " << CallingConvID << '\n';
- assert(0 && "Unsupported calling convention");
+ errs() << "CallingConvID = " << CallingConvID << '\n';
+ llvm_unreachable("Unsupported calling convention");
}
return ""; // Not reached
}
case Type::DoubleTyID:
return "float64 ";
default:
- cerr << "Type = " << *Ty << '\n';
- assert(0 && "Invalid primitive type");
+ errs() << "Type = " << *Ty << '\n';
+ llvm_unreachable("Invalid primitive type");
}
return ""; // Not reached
}
return getArrayTypeName(Ty->getTypeID(),Ty);
return "valuetype '"+getArrayTypeName(Ty->getTypeID(),Ty)+"' ";
default:
- cerr << "Type = " << *Ty << '\n';
- assert(0 && "Invalid type in getTypeName()");
+ errs() << "Type = " << *Ty << '\n';
+ llvm_unreachable("Invalid type in getTypeName()");
}
return ""; // Not reached
}
return ArgumentVT;
// Function
else if (const Function* F = dyn_cast<Function>(V))
- return F->hasInternalLinkage() ? InternalVT : GlobalVT;
+ return F->hasLocalLinkage() ? InternalVT : GlobalVT;
// Variable
else if (const GlobalVariable* G = dyn_cast<GlobalVariable>(V))
- return G->hasInternalLinkage() ? InternalVT : GlobalVT;
+ return G->hasLocalLinkage() ? InternalVT : GlobalVT;
// Constant
else if (isa<Constant>(V))
return isa<ConstantExpr>(V) ? ConstExprVT : ConstVT;
case Type::DoubleTyID:
return "r8";
case Type::PointerTyID:
- return "i"+utostr(TD->getABITypeSize(Ty));
+ return "i"+utostr(TD->getTypeAllocSize(Ty));
default:
- cerr << "TypeID = " << Ty->getTypeID() << '\n';
- assert(0 && "Invalid type in TypeToPostfix()");
+ errs() << "TypeID = " << Ty->getTypeID() << '\n';
+ llvm_unreachable("Invalid type in TypeToPostfix()");
}
return ""; // Not reached
}
printSimpleInstruction("conv.u8");
break;
default:
- assert(0 && "Module use not supporting pointer size");
+ llvm_unreachable("Module use not supporting pointer size");
}
}
printSimpleInstruction("ldc.i4",utostr(N).c_str());
// FIXME: Need overflow test?
if (!isUInt32(N)) {
- cerr << "Value = " << utostr(N) << '\n';
- assert(0 && "32-bit pointer overflowed");
+ errs() << "Value = " << utostr(N) << '\n';
+ llvm_unreachable("32-bit pointer overflowed");
}
break;
case Module::Pointer64:
printSimpleInstruction("ldc.i8",utostr(N).c_str());
break;
default:
- assert(0 && "Module use not supporting pointer size");
+ llvm_unreachable("Module use not supporting pointer size");
}
}
uint64_t X;
unsigned Size;
if (FP->getType()->getTypeID()==Type::FloatTyID) {
- X = (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue();
+ X = (uint32_t)FP->getValueAPF().bitcastToAPInt().getZExtValue();
Size = 4;
} else {
- X = FP->getValueAPF().convertToAPInt().getZExtValue();
+ X = FP->getValueAPF().bitcastToAPInt().getZExtValue();
Size = 8;
}
Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')';
// Undefined constant value = NULL.
printPtrLoad(0);
} else {
- cerr << "Constant = " << *C << '\n';
- assert(0 && "Invalid constant value");
+ errs() << "Constant = " << *C << '\n';
+ llvm_unreachable("Invalid constant value");
}
Out << '\n';
}
printConstantExpr(cast<ConstantExpr>(V));
break;
default:
- cerr << "Value = " << *V << '\n';
- assert(0 && "Invalid value location");
+ errs() << "Value = " << *V << '\n';
+ llvm_unreachable("Invalid value location");
}
}
printSimpleInstruction("stloc",getValueName(V).c_str());
break;
default:
- cerr << "Value = " << *V << '\n';
- assert(0 && "Invalid value location");
+ errs() << "Value = " << *V << '\n';
+ llvm_unreachable("Invalid value location");
}
}
void MSILWriter::printCastInstruction(unsigned int Op, const Value* V,
- const Type* Ty) {
+ const Type* Ty, const Type* SrcTy) {
std::string Tmp("");
printValueLoad(V);
switch (Op) {
// Signed
case Instruction::SExt:
+ // If sign extending int, convert first from unsigned to signed
+ // with the same bit size - because otherwise we will loose the sign.
+ if (SrcTy) {
+ Tmp = "conv."+getTypePostfix(SrcTy,false,true);
+ printSimpleInstruction(Tmp.c_str());
+ }
+ // FALLTHROUGH
case Instruction::SIToFP:
case Instruction::FPToSI:
Tmp = "conv."+getTypePostfix(Ty,false,true);
// FIXME: meaning that ld*/st* instruction do not change data format.
break;
default:
- cerr << "Opcode = " << Op << '\n';
- assert(0 && "Invalid conversion instruction");
+ errs() << "Opcode = " << Op << '\n';
+ llvm_unreachable("Invalid conversion instruction");
}
}
uint64_t FieldIndex = cast<ConstantInt>(IndexValue)->getZExtValue();
// Offset is the sum of all previous structure fields.
for (uint64_t F = 0; F<FieldIndex; ++F)
- Size += TD->getABITypeSize(StrucTy->getContainedType((unsigned)F));
+ Size += TD->getTypeAllocSize(StrucTy->getContainedType((unsigned)F));
printPtrLoad(Size);
printSimpleInstruction("add");
continue;
} else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(*I)) {
- Size = TD->getABITypeSize(SeqTy->getElementType());
+ Size = TD->getTypeAllocSize(SeqTy->getElementType());
} else {
- Size = TD->getABITypeSize(*I);
+ Size = TD->getTypeAllocSize(*I);
}
// Add offset of current element to stack top.
if (!isZeroValue(IndexValue)) {
else if (const InvokeInst* Invoke = dyn_cast<InvokeInst>(Inst))
Name = getConvModopt(Invoke->getCallingConv());
else {
- cerr << "Instruction = " << Inst->getName() << '\n';
- assert(0 && "Need \"Invoke\" or \"Call\" instruction only");
+ errs() << "Instruction = " << Inst->getName() << '\n';
+ llvm_unreachable("Need \"Invoke\" or \"Call\" instruction only");
}
if (const Function* F = dyn_cast<Function>(FnVal)) {
// Direct call.
// Save as pointer type "void*"
printValueLoad(Inst->getOperand(1));
printSimpleInstruction("ldloca",Name.c_str());
- printIndirectSave(PointerType::getUnqual(IntegerType::get(8)));
+ printIndirectSave(PointerType::getUnqual(
+ IntegerType::get(Inst->getContext(), 8)));
break;
case Intrinsic::vaend:
// Close argument list handle.
printSimpleInstruction("cpobj","[mscorlib]System.ArgIterator");
break;
default:
- cerr << "Intrinsic ID = " << Inst->getIntrinsicID() << '\n';
- assert(0 && "Invalid intrinsic function");
+ errs() << "Intrinsic ID = " << Inst->getIntrinsicID() << '\n';
+ llvm_unreachable("Invalid intrinsic function");
}
}
break;
case ICmpInst::ICMP_UGT:
printBinaryInstruction("cgt.un",Left,Right);
+ break;
case ICmpInst::ICMP_SGT:
printBinaryInstruction("cgt",Left,Right);
break;
default:
- cerr << "Predicate = " << Predicate << '\n';
- assert(0 && "Invalid icmp predicate");
+ errs() << "Predicate = " << Predicate << '\n';
+ llvm_unreachable("Invalid icmp predicate");
}
}
printSimpleInstruction("or");
break;
default:
- assert(0 && "Illegal FCmp predicate");
+ llvm_unreachable("Illegal FCmp predicate");
}
}
"instance typedref [mscorlib]System.ArgIterator::GetNextArg()");
printSimpleInstruction("refanyval","void*");
std::string Name =
- "ldind."+getTypePostfix(PointerType::getUnqual(IntegerType::get(8)),false);
+ "ldind."+getTypePostfix(PointerType::getUnqual(
+ IntegerType::get(Inst->getContext(), 8)),false);
printSimpleInstruction(Name.c_str());
}
void MSILWriter::printAllocaInstruction(const AllocaInst* Inst) {
- uint64_t Size = TD->getABITypeSize(Inst->getAllocatedType());
+ uint64_t Size = TD->getTypeAllocSize(Inst->getAllocatedType());
// Constant optimization.
if (const ConstantInt* CInt = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
printPtrLoad(CInt->getZExtValue()*Size);
break;
// Binary
case Instruction::Add:
+ case Instruction::FAdd:
printBinaryInstruction("add",Left,Right);
break;
case Instruction::Sub:
+ case Instruction::FSub:
printBinaryInstruction("sub",Left,Right);
break;
- case Instruction::Mul:
+ case Instruction::Mul:
+ case Instruction::FMul:
printBinaryInstruction("mul",Left,Right);
break;
case Instruction::UDiv:
case Instruction::Store:
printIndirectSave(Inst->getOperand(1), Inst->getOperand(0));
break;
+ case Instruction::SExt:
+ printCastInstruction(Inst->getOpcode(),Left,
+ cast<CastInst>(Inst)->getDestTy(),
+ cast<CastInst>(Inst)->getSrcTy());
+ break;
case Instruction::Trunc:
case Instruction::ZExt:
- case Instruction::SExt:
case Instruction::FPTrunc:
case Instruction::FPExt:
case Instruction::UIToFP:
case Instruction::Alloca:
printAllocaInstruction(cast<AllocaInst>(Inst));
break;
- case Instruction::Malloc:
- assert(0 && "LowerAllocationsPass used");
- break;
- case Instruction::Free:
- assert(0 && "LowerAllocationsPass used");
- break;
case Instruction::Unreachable:
printSimpleInstruction("ldstr", "\"Unreachable instruction\"");
printSimpleInstruction("newobj",
printVAArgInstruction(cast<VAArgInst>(Inst));
break;
default:
- cerr << "Instruction = " << Inst->getName() << '\n';
- assert(0 && "Unsupported instruction");
+ errs() << "Instruction = " << Inst->getName() << '\n';
+ llvm_unreachable("Unsupported instruction");
}
}
// Print instruction
printInstruction(Inst);
// Save result
- if (Inst->getType()!=Type::VoidTy) {
+ if (Inst->getType()!=Type::getVoidTy(BB->getContext())) {
// Do not save value after invoke, it done in "try" block
if (Inst->getOpcode()==Instruction::Invoke) continue;
printValueSave(Inst);
Ty = PointerType::getUnqual(AI->getAllocatedType());
Name = getValueName(AI);
Out << "\t.locals (" << getTypeName(Ty) << Name << ")\n";
- } else if (I->getType()!=Type::VoidTy) {
+ } else if (I->getType()!=Type::getVoidTy(F.getContext())) {
// Operation result.
Ty = I->getType();
Name = getValueName(&*I);
printSelectInstruction(CE->getOperand(0),CE->getOperand(1),CE->getOperand(2));
break;
case Instruction::Add:
+ case Instruction::FAdd:
printBinaryInstruction("add",left,right);
break;
case Instruction::Sub:
+ case Instruction::FSub:
printBinaryInstruction("sub",left,right);
break;
case Instruction::Mul:
+ case Instruction::FMul:
printBinaryInstruction("mul",left,right);
break;
case Instruction::UDiv:
printBinaryInstruction("shr",left,right);
break;
default:
- cerr << "Expression = " << *CE << "\n";
- assert(0 && "Invalid constant expression");
+ errs() << "Expression = " << *CE << "\n";
+ llvm_unreachable("Invalid constant expression");
}
}
postfix = "stind."+postfix;
printSimpleInstruction(postfix.c_str());
} else {
- cerr << "Constant = " << *I->constant << '\n';
- assert(0 && "Invalid static initializer");
+ errs() << "Constant = " << *I->constant << '\n';
+ llvm_unreachable("Invalid static initializer");
}
}
}
void MSILWriter::printFunction(const Function& F) {
- bool isSigned = F.paramHasAttr(0, ParamAttr::SExt);
+ bool isSigned = F.paramHasAttr(0, Attribute::SExt);
Out << "\n.method static ";
- Out << (F.hasInternalLinkage() ? "private " : "public ");
+ Out << (F.hasLocalLinkage() ? "private " : "public ");
if (F.isVarArg()) Out << "vararg ";
Out << getTypeName(F.getReturnType(),isSigned) <<
getConvModopt(F.getCallingConv()) << getValueName(&F) << '\n';
unsigned ArgIdx = 1;
for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I!=E;
++I, ++ArgIdx) {
- isSigned = F.paramHasAttr(ArgIdx, ParamAttr::SExt);
+ isSigned = F.paramHasAttr(ArgIdx, Attribute::SExt);
if (I!=F.arg_begin()) Out << ", ";
Out << getTypeName(I->getType(),isSigned) << getValueName(I);
}
// Print not duplicated type
if (Printed.insert(Ty).second) {
Out << ".class value explicit ansi sealed '" << Name << "'";
- Out << " { .pack " << 1 << " .size " << TD->getABITypeSize(Ty)<< " }\n\n";
+ Out << " { .pack " << 1 << " .size " << TD->getTypeAllocSize(Ty);
+ Out << " }\n\n";
}
}
}
case 64:
return N;
default:
- cerr << "Bits = " << N << '\n';
- assert(0 && "Unsupported integer width");
+ errs() << "Bits = " << N << '\n';
+ llvm_unreachable("Unsupported integer width");
}
return 0; // Not reached
}
const Type* Ty = C->getType();
// Print zero initialized constant.
if (isa<ConstantAggregateZero>(C) || C->isNullValue()) {
- TySize = TD->getABITypeSize(C->getType());
+ TySize = TD->getTypeAllocSize(C->getType());
Offset += TySize;
Out << "int8 (0) [" << TySize << "]";
return;
// Print constant initializer
switch (Ty->getTypeID()) {
case Type::IntegerTyID: {
- TySize = TD->getABITypeSize(Ty);
+ TySize = TD->getTypeAllocSize(Ty);
const ConstantInt* Int = cast<ConstantInt>(C);
Out << getPrimitiveTypeName(Ty,true) << "(" << Int->getSExtValue() << ")";
break;
}
case Type::FloatTyID:
case Type::DoubleTyID: {
- TySize = TD->getABITypeSize(Ty);
+ TySize = TD->getTypeAllocSize(Ty);
const ConstantFP* FP = cast<ConstantFP>(C);
if (Ty->getTypeID() == Type::FloatTyID)
Out << "int32 (" <<
- (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue() << ')';
+ (uint32_t)FP->getValueAPF().bitcastToAPInt().getZExtValue() << ')';
else
Out << "int64 (" <<
- FP->getValueAPF().convertToAPInt().getZExtValue() << ')';
+ FP->getValueAPF().bitcastToAPInt().getZExtValue() << ')';
break;
}
case Type::ArrayTyID:
}
break;
case Type::PointerTyID:
- TySize = TD->getABITypeSize(C->getType());
+ TySize = TD->getTypeAllocSize(C->getType());
// Initialize with global variable address
if (const GlobalVariable *G = dyn_cast<GlobalVariable>(C)) {
std::string name = getValueName(G);
// Null pointer initialization
if (TySize==4) Out << "int32 (0)";
else if (TySize==8) Out << "int64 (0)";
- else assert(0 && "Invalid pointer size");
+ else llvm_unreachable("Invalid pointer size");
}
break;
default:
- cerr << "TypeID = " << Ty->getTypeID() << '\n';
- assert(0 && "Invalid type in printStaticConstant()");
+ errs() << "TypeID = " << Ty->getTypeID() << '\n';
+ llvm_unreachable("Invalid type in printStaticConstant()");
}
// Increase offset.
Offset += TySize;
Out << getTypeName(C->getType());
break;
default:
- cerr << "Type = " << *C << "\n";
- assert(0 && "Invalid constant type");
+ errs() << "Type = " << *C << "\n";
+ llvm_unreachable("Invalid constant type");
}
// Print initializer
std::string label = Name;
const char* MSILWriter::getLibraryName(const Function* F) {
- return getLibraryForSymbol(F->getName().c_str(), true, F->getCallingConv());
+ return getLibraryForSymbol(F->getName(), true, F->getCallingConv());
}
const char* MSILWriter::getLibraryName(const GlobalVariable* GV) {
- return getLibraryForSymbol(Mang->getValueName(GV).c_str(), false, 0);
+ return getLibraryForSymbol(Mang->getMangledName(GV), false, CallingConv::C);
}
-const char* MSILWriter::getLibraryForSymbol(const char* Name, bool isFunction,
- unsigned CallingConv) {
+const char* MSILWriter::getLibraryForSymbol(const StringRef &Name,
+ bool isFunction,
+ CallingConv::ID CallingConv) {
// TODO: Read *.def file with function and libraries definitions.
return "MSVCRT.DLL";
}
E = ModulePtr->global_end(); I!=E; ++I) {
if (!I->isDeclaration() || !I->hasDLLImportLinkage()) continue;
// Use "LoadLibrary"/"GetProcAddress" to recive variable address.
- std::string Label = "not_null$_"+utostr(getUniqID());
std::string Tmp = getTypeName(I->getType())+getValueName(&*I);
printSimpleInstruction("ldsflda",Tmp.c_str());
Out << "\tldstr\t\"" << getLibraryName(&*I) << "\"\n";
- Out << "\tldstr\t\"" << Mang->getValueName(&*I) << "\"\n";
+ Out << "\tldstr\t\"" << Mang->getMangledName(&*I) << "\"\n";
printSimpleInstruction("call","void* $MSIL_Import(string,string)");
printIndirectSave(I->getType());
}
// External Interface declaration
//===----------------------------------------------------------------------===//
-bool MSILTarget::addPassesToEmitWholeFile(PassManager &PM, raw_ostream &o,
- CodeGenFileType FileType, bool Fast)
+bool MSILTarget::addPassesToEmitWholeFile(PassManager &PM,
+ formatted_raw_ostream &o,
+ CodeGenFileType FileType,
+ CodeGenOpt::Level OptLevel)
{
if (FileType != TargetMachine::AssemblyFile) return true;
MSILWriter* Writer = new MSILWriter(o);
PM.add(createGCLoweringPass());
- PM.add(createLowerAllocationsPass(true));
// FIXME: Handle switch trougth native IL instruction "switch"
PM.add(createLowerSwitchPass());
PM.add(createCFGSimplificationPass());