#include "llvm/Pass.h"
#include "llvm/PassManager.h"
#include "llvm/TypeSymbolTable.h"
-#include "llvm/Target/TargetMachineRegistry.h"
-#include "llvm/ADT/StringExtras.h"
-#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormattedStream.h"
-#include "llvm/Support/Streams.h"
+#include "llvm/Target/TargetRegistry.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/Config/config.h"
#include <algorithm>
#include <set>
cl::desc("Specify the name of the thing to generate"),
cl::init("!bad!"));
-// Register the target.
-static RegisterTarget<CPPTargetMachine> X(TheCppBackendTarget, "cpp", "C++ backend");
-
-// Force static initialization.
-extern "C" void LLVMInitializeCppBackendTarget() { }
+extern "C" void LLVMInitializeCppBackendTarget() {
+ // Register the target.
+ RegisterTargetMachine<CPPTargetMachine> X(TheCppBackendTarget);
+}
namespace {
typedef std::vector<const Type*> TypeList;
private:
void printLinkageType(GlobalValue::LinkageTypes LT);
void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
- void printCallingConv(unsigned cc);
+ void printCallingConv(CallingConv::ID cc);
void printEscapedString(const std::string& str);
void printCFP(const ConstantFP* CFP);
void CppWriter::printCFP(const ConstantFP *CFP) {
bool ignored;
APFloat APF = APFloat(CFP->getValueAPF()); // copy
- if (CFP->getType() == Type::FloatTy)
+ if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
- Out << "ConstantFP::get(";
+ Out << "ConstantFP::get(getGlobalContext(), ";
Out << "APFloat(";
#if HAVE_PRINTF_A
char Buffer[100];
!strncmp(Buffer, "-0x", 3) ||
!strncmp(Buffer, "+0x", 3)) &&
APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
- if (CFP->getType() == Type::DoubleTy)
+ if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
Out << "BitsToDouble(" << Buffer << ")";
else
Out << "BitsToFloat((float)" << Buffer << ")";
((StrVal[0] == '-' || StrVal[0] == '+') &&
(StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
(CFP->isExactlyValue(atof(StrVal.c_str())))) {
- if (CFP->getType() == Type::DoubleTy)
+ if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
Out << StrVal;
else
Out << StrVal << "f";
- } else if (CFP->getType() == Type::DoubleTy)
+ } else if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
Out << "BitsToDouble(0x"
<< utohexstr(CFP->getValueAPF().bitcastToAPInt().getZExtValue())
<< "ULL) /* " << StrVal << " */";
Out << ")";
}
- void CppWriter::printCallingConv(unsigned cc){
+ void CppWriter::printCallingConv(CallingConv::ID cc){
// Print the calling convention.
switch (cc) {
case CallingConv::C: Out << "CallingConv::C"; break;
Out << "GlobalValue::InternalLinkage"; break;
case GlobalValue::PrivateLinkage:
Out << "GlobalValue::PrivateLinkage"; break;
+ case GlobalValue::LinkerPrivateLinkage:
+ Out << "GlobalValue::LinkerPrivateLinkage"; break;
case GlobalValue::AvailableExternallyLinkage:
Out << "GlobalValue::AvailableExternallyLinkage "; break;
case GlobalValue::LinkOnceAnyLinkage:
Out << "GlobalValue::DLLExportLinkage"; break;
case GlobalValue::ExternalWeakLinkage:
Out << "GlobalValue::ExternalWeakLinkage"; break;
- case GlobalValue::GhostLinkage:
- Out << "GlobalValue::GhostLinkage"; break;
case GlobalValue::CommonLinkage:
Out << "GlobalValue::CommonLinkage"; break;
}
// First, handle the primitive types .. easy
if (Ty->isPrimitiveType() || Ty->isInteger()) {
switch (Ty->getTypeID()) {
- case Type::VoidTyID: return "Type::VoidTy";
+ case Type::VoidTyID: return "Type::getVoidTy(getGlobalContext())";
case Type::IntegerTyID: {
unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
- return "IntegerType::get(" + utostr(BitWidth) + ")";
+ return "IntegerType::get(getGlobalContext(), " + utostr(BitWidth) + ")";
}
- case Type::X86_FP80TyID: return "Type::X86_FP80Ty";
- case Type::FloatTyID: return "Type::FloatTy";
- case Type::DoubleTyID: return "Type::DoubleTy";
- case Type::LabelTyID: return "Type::LabelTy";
+ case Type::X86_FP80TyID: return "Type::getX86_FP80Ty(getGlobalContext())";
+ case Type::FloatTyID: return "Type::getFloatTy(getGlobalContext())";
+ case Type::DoubleTyID: return "Type::getDoubleTy(getGlobalContext())";
+ case Type::LabelTyID: return "Type::getLabelTy(getGlobalContext())";
default:
error("Invalid primitive type");
break;
}
- return "Type::VoidTy"; // shouldn't be returned, but make it sensible
+ // shouldn't be returned, but make it sensible
+ return "Type::getVoidTy(getGlobalContext())";
}
// Now, see if we've seen the type before and return that
} else {
name = getTypePrefix(val->getType());
}
- name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
+ if (val->hasName())
+ name += val->getName();
+ else
+ name += utostr(uniqueNum++);
sanitize(name);
NameSet::iterator NI = UsedNames.find(name);
if (NI != UsedNames.end())
if (TI != TypeStack.end()) {
TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
if (I == UnresolvedTypes.end()) {
- Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
+ Out << "PATypeHolder " << typeName;
+ Out << "_fwd = OpaqueType::get(getGlobalContext());";
nl(Out);
UnresolvedTypes[Ty] = typeName;
}
nl(Out);
}
Out << "StructType* " << typeName << " = StructType::get("
+ << "mod->getContext(), "
<< typeName << "_fields, /*isPacked=*/"
<< (ST->isPacked() ? "true" : "false") << ");";
nl(Out);
break;
}
case Type::OpaqueTyID: {
- Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
+ Out << "OpaqueType* " << typeName;
+ Out << " = OpaqueType::get(getGlobalContext());";
nl(Out);
break;
}
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
std::string constValue = CI->getValue().toString(10, true);
- Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
- << cast<IntegerType>(CI->getType())->getBitWidth() << ", \""
- << constValue << "\", " << constValue.length() << ", 10));";
+ Out << "ConstantInt* " << constName
+ << " = ConstantInt::get(getGlobalContext(), APInt("
+ << cast<IntegerType>(CI->getType())->getBitWidth()
+ << ", StringRef(\"" << constValue << "\"), 10));";
} else if (isa<ConstantAggregateZero>(CV)) {
Out << "ConstantAggregateZero* " << constName
<< " = ConstantAggregateZero::get(" << typeName << ");";
printCFP(CFP);
Out << ";";
} else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
- if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
- Out << "Constant* " << constName << " = ConstantArray::get(\"";
+ if (CA->isString() &&
+ CA->getType()->getElementType() ==
+ Type::getInt8Ty(CA->getContext())) {
+ Out << "Constant* " << constName <<
+ " = ConstantArray::get(getGlobalContext(), \"";
std::string tmp = CA->getAsString();
bool nullTerminate = false;
if (tmp[tmp.length()-1] == 0) {
<< getCppName(CE->getOperand(0)) << ", "
<< "&" << constName << "_indices[0], "
<< constName << "_indices.size()"
- << " );";
+ << ");";
} else if (CE->isCast()) {
printConstant(CE->getOperand(0));
Out << "Constant* " << constName << " = ConstantExpr::getCast(";
nl(Out);
printType(GV->getType());
if (GV->hasInitializer()) {
- Constant* Init = GV->getInitializer();
+ Constant *Init = GV->getInitializer();
printType(Init->getType());
- if (Function* F = dyn_cast<Function>(Init)) {
+ if (Function *F = dyn_cast<Function>(Init)) {
nl(Out)<< "/ Function Declarations"; nl(Out);
printFunctionHead(F);
} else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
nl(Out) << "// Global Variable Declarations"; nl(Out);
printVariableHead(gv);
- } else {
- nl(Out) << "// Constant Definitions"; nl(Out);
- printConstant(gv);
- }
- if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
+
nl(Out) << "// Global Variable Definitions"; nl(Out);
printVariableBody(gv);
+ } else {
+ nl(Out) << "// Constant Definitions"; nl(Out);
+ printConstant(Init);
}
}
}
void CppWriter::printVariableHead(const GlobalVariable *GV) {
nl(Out) << "GlobalVariable* " << getCppName(GV);
if (is_inline) {
- Out << " = mod->getGlobalVariable(";
+ Out << " = mod->getGlobalVariable(getGlobalContext(), ";
printEscapedString(GV->getName());
Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
nl(Out) << "if (!" << getCppName(GV) << ") {";
in(); nl(Out) << getCppName(GV);
}
- Out << " = new GlobalVariable(/*Module=*/*mod";
+ Out << " = new GlobalVariable(/*Module=*/*mod, ";
nl(Out) << "/*Type=*/";
printCppName(GV->getType()->getElementType());
Out << ",";
case Instruction::Ret: {
const ReturnInst* ret = cast<ReturnInst>(I);
- Out << "ReturnInst::Create("
+ Out << "ReturnInst::Create(getGlobalContext(), "
<< (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
break;
}
break;
}
case Instruction::Switch: {
- const SwitchInst* sw = cast<SwitchInst>(I);
+ const SwitchInst *SI = cast<SwitchInst>(I);
Out << "SwitchInst* " << iName << " = SwitchInst::Create("
<< opNames[0] << ", "
<< opNames[1] << ", "
- << sw->getNumCases() << ", " << bbname << ");";
+ << SI->getNumCases() << ", " << bbname << ");";
nl(Out);
- for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
+ for (unsigned i = 2; i != SI->getNumOperands(); i += 2) {
Out << iName << "->addCase("
<< opNames[i] << ", "
<< opNames[i+1] << ");";
}
break;
}
+ case Instruction::IndirectBr: {
+ const IndirectBrInst *IBI = cast<IndirectBrInst>(I);
+ Out << "IndirectBrInst *" << iName << " = IndirectBrInst::Create("
+ << opNames[0] << ", " << IBI->getNumDestinations() << ");";
+ nl(Out);
+ for (unsigned i = 1; i != IBI->getNumOperands(); ++i) {
+ Out << iName << "->addDestination(" << opNames[i] << ");";
+ nl(Out);
+ }
+ break;
+ }
case Instruction::Invoke: {
const InvokeInst* inv = cast<InvokeInst>(I);
Out << "std::vector<Value*> " << iName << "_params;";
<< bbname << ");";
break;
}
- case Instruction::Unreachable:{
+ case Instruction::Unreachable: {
Out << "new UnreachableInst("
+ << "getGlobalContext(), "
<< bbname << ");";
break;
}
break;
}
case Instruction::FCmp: {
- Out << "FCmpInst* " << iName << " = new FCmpInst(";
+ Out << "FCmpInst* " << iName << " = new FCmpInst(*" << bbname << ", ";
switch (cast<FCmpInst>(I)->getPredicate()) {
case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
}
Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
printEscapedString(I->getName());
- Out << "\", " << bbname << ");";
+ Out << "\");";
break;
}
case Instruction::ICmp: {
- Out << "ICmpInst* " << iName << " = new ICmpInst(";
+ Out << "ICmpInst* " << iName << " = new ICmpInst(*" << bbname << ", ";
switch (cast<ICmpInst>(I)->getPredicate()) {
case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
}
Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
printEscapedString(I->getName());
- Out << "\", " << bbname << ");";
- break;
- }
- case Instruction::Malloc: {
- const MallocInst* mallocI = cast<MallocInst>(I);
- Out << "MallocInst* " << iName << " = new MallocInst("
- << getCppName(mallocI->getAllocatedType()) << ", ";
- if (mallocI->isArrayAllocation())
- Out << opNames[0] << ", " ;
- Out << "\"";
- printEscapedString(mallocI->getName());
- Out << "\", " << bbname << ");";
- if (mallocI->getAlignment())
- nl(Out) << iName << "->setAlignment("
- << mallocI->getAlignment() << ");";
- break;
- }
- case Instruction::Free: {
- Out << "FreeInst* " << iName << " = new FreeInst("
- << getCppName(I->getOperand(0)) << ", " << bbname << ");";
+ Out << "\");";
break;
}
case Instruction::Alloca: {
for (Function::const_iterator BI = F->begin(), BE = F->end();
BI != BE; ++BI) {
std::string bbname(getCppName(BI));
- Out << "BasicBlock* " << bbname << " = BasicBlock::Create(\"";
+ Out << "BasicBlock* " << bbname <<
+ " = BasicBlock::Create(getGlobalContext(), \"";
if (BI->hasName())
printEscapedString(BI->getName());
Out << "\"," << getCppName(BI->getParent()) << ",0);";
void CppWriter::printProgram(const std::string& fname,
const std::string& mName) {
+ Out << "#include <llvm/LLVMContext.h>\n";
Out << "#include <llvm/Module.h>\n";
Out << "#include <llvm/DerivedTypes.h>\n";
Out << "#include <llvm/Constants.h>\n";
Out << "int main(int argc, char**argv) {\n";
Out << " Module* Mod = " << fname << "();\n";
Out << " verifyModule(*Mod, PrintMessageAction);\n";
- Out << " outs().flush();\n";
Out << " PassManager PM;\n";
Out << " PM.add(createPrintModulePass(&outs()));\n";
Out << " PM.run(*Mod);\n";
nl(Out,1) << "// Module Construction";
nl(Out) << "Module* mod = new Module(\"";
printEscapedString(mName);
- Out << "\");";
+ Out << "\", getGlobalContext());";
if (!TheModule->getTargetTriple().empty()) {
nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
}