#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/CFG.h"
-#include "llvm/Support/ManagedStatic.h"
-#include "llvm/Support/MathExtras.h"
+#include "llvm/Support/Streams.h"
#include "llvm/Config/config.h"
#include <algorithm>
-#include <iostream>
#include <set>
using namespace llvm;
cl::desc("Specify the name of the thing to generate"),
cl::init("!bad!"));
-namespace {
- // Register the target.
- RegisterTarget<CPPTargetMachine> X("cpp", " C++ backend");
+// Register the target.
+static RegisterTarget<CPPTargetMachine> X("cpp", " C++ backend");
+namespace {
typedef std::vector<const Type*> TypeList;
typedef std::map<const Type*,std::string> TypeMap;
typedef std::map<const Value*,std::string> ValueMap;
public:
static char ID;
- explicit CppWriter(std::ostream &o) : ModulePass((intptr_t)&ID), Out(o) {}
+ explicit CppWriter(std::ostream &o) :
+ ModulePass((intptr_t)&ID), Out(o), uniqueNum(0), is_inline(false) {}
virtual const char *getPassName() const { return "C++ backend"; }
bool runOnModule(Module &M);
- bool doInitialization(Module &M) {
- uniqueNum = 0;
- is_inline = false;
-
- TypeNames.clear();
- ValueNames.clear();
- UnresolvedTypes.clear();
- TypeStack.clear();
- UsedNames.clear();
- DefinedTypes.clear();
- DefinedValues.clear();
- ForwardRefs.clear();
-
- return false;
- }
-
void printProgram(const std::string& fname, const std::string& modName );
void printModule(const std::string& fname, const std::string& modName );
void printContents(const std::string& fname, const std::string& modName );
}
void CppWriter::error(const std::string& msg) {
- std::cerr << progname << ": " << msg << "\n";
+ cerr << progname << ": " << msg << "\n";
exit(2);
}
if (CFP->getType() == Type::FloatTy)
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Out << "ConstantFP::get(";
- if (CFP->getType() == Type::DoubleTy)
- Out << "Type::DoubleTy, ";
- else
- Out << "Type::FloatTy, ";
Out << "APFloat(";
#if HAVE_PRINTF_A
char Buffer[100];
Out << "GlobalValue::ExternalWeakLinkage"; break;
case GlobalValue::GhostLinkage:
Out << "GlobalValue::GhostLinkage"; break;
+ case GlobalValue::CommonLinkage:
+ Out << "GlobalValue::CommonLinkage"; break;
}
}
void CppWriter::printParamAttrs(const PAListPtr &PAL,
const std::string &name) {
- Out << "PAListPtr " << name << "_PAL = 0;";
+ Out << "PAListPtr " << name << "_PAL;";
nl(Out);
if (!PAL.isEmpty()) {
Out << '{'; in(); nl(Out);
for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
uint16_t index = PAL.getSlot(i).Index;
ParameterAttributes attrs = PAL.getSlot(i).Attrs;
- Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
+ Out << "PAWI.Index = " << index << "; PAWI.Attrs = 0 ";
if (attrs & ParamAttr::SExt)
Out << " | ParamAttr::SExt";
if (attrs & ParamAttr::ZExt)
}
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
- << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
- << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
+ << cast<IntegerType>(CI->getType())->getBitWidth() << ", \""
+ << CI->getValue() << "\", 10));";
} else if (isa<ConstantAggregateZero>(CV)) {
Out << "ConstantAggregateZero* " << constName
<< " = ConstantAggregateZero::get(" << typeName << ");";
}
switch (I->getOpcode()) {
+ default:
+ error("Invalid instruction");
+ break;
+
case Instruction::Ret: {
const ReturnInst* ret = cast<ReturnInst>(I);
Out << "ReturnInst::Create("
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:{
- Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
+ Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
switch (I->getOpcode()) {
case Instruction::Add: Out << "Instruction::Add"; break;
case Instruction::Sub: Out << "Instruction::Sub"; break;
Out << "\", " << bbname << ");";
break;
}
+ case Instruction::ExtractValue: {
+ const ExtractValueInst *evi = cast<ExtractValueInst>(I);
+ Out << "std::vector<unsigned> " << iName << "_indices;";
+ nl(Out);
+ for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
+ Out << iName << "_indices.push_back("
+ << evi->idx_begin()[i] << ");";
+ nl(Out);
+ }
+ Out << "ExtractValueInst* " << getCppName(evi)
+ << " = ExtractValueInst::Create(" << opNames[0]
+ << ", "
+ << iName << "_indices.begin(), " << iName << "_indices.end(), \"";
+ printEscapedString(evi->getName());
+ Out << "\", " << bbname << ");";
+ break;
+ }
+ case Instruction::InsertValue: {
+ const InsertValueInst *ivi = cast<InsertValueInst>(I);
+ Out << "std::vector<unsigned> " << iName << "_indices;";
+ nl(Out);
+ for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
+ Out << iName << "_indices.push_back("
+ << ivi->idx_begin()[i] << ");";
+ nl(Out);
+ }
+ Out << "InsertValueInst* " << getCppName(ivi)
+ << " = InsertValueInst::Create(" << opNames[0]
+ << ", " << opNames[1] << ", "
+ << iName << "_indices.begin(), " << iName << "_indices.end(), \"";
+ printEscapedString(ivi->getName());
+ Out << "\", " << bbname << ");";
+ break;
+ }
}
DefinedValues.insert(I);
nl(Out);
Out << ");";
nl(Out);
}
- if (F->hasCollector()) {
+ if (F->hasGC()) {
printCppName(F);
- Out << "->setCollector(\"" << F->getCollector() << "\");";
+ Out << "->setGC(\"" << F->getGC() << "\");";
nl(Out);
}
if (is_inline) {
Out << "#include <llvm/Support/MathExtras.h>\n";
Out << "#include <llvm/Pass.h>\n";
Out << "#include <llvm/PassManager.h>\n";
+ Out << "#include <llvm/ADT/SmallVector.h>\n";
Out << "#include <llvm/Analysis/Verifier.h>\n";
Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
Out << "#include <algorithm>\n";