Rename some GC classes so that their roll will hopefully be clearer.
[oota-llvm.git] / lib / Target / CppBackend / CPPBackend.cpp
index fda5d00e6ae66e9b0c799b38d00779558afb6ffa..b359659bcdf714092d263fe0942a1a37a30a2b73 100644 (file)
 #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;
@@ -73,10 +70,10 @@ static cl::opt<std::string> NameToGenerate("cppfor", cl::Optional,
   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;
@@ -104,28 +101,13 @@ namespace {
 
   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 );
@@ -227,7 +209,7 @@ namespace {
   }
 
   void CppWriter::error(const std::string& msg) {
-    std::cerr << progname << ": " << msg << "\n";
+    cerr << progname << ": " << msg << "\n";
     exit(2);
   }
 
@@ -239,10 +221,6 @@ namespace {
     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];
@@ -319,6 +297,8 @@ namespace {
       Out << "GlobalValue::ExternalWeakLinkage"; break;
     case GlobalValue::GhostLinkage:
       Out << "GlobalValue::GhostLinkage"; break;
+    case GlobalValue::CommonLinkage:
+      Out << "GlobalValue::CommonLinkage"; break;
     }
   }
 
@@ -449,7 +429,7 @@ namespace {
 
   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);
@@ -458,7 +438,7 @@ namespace {
       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)
@@ -753,8 +733,8 @@ namespace {
     }
     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 << ");";
@@ -1085,6 +1065,10 @@ namespace {
     }
 
     switch (I->getOpcode()) {
+    default:
+      error("Invalid instruction");
+      break;
+
     case Instruction::Ret: {
       const ReturnInst* ret =  cast<ReturnInst>(I);
       Out << "ReturnInst::Create("
@@ -1171,7 +1155,7 @@ namespace {
     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;
@@ -1458,6 +1442,40 @@ namespace {
       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);
@@ -1585,9 +1603,9 @@ namespace {
       Out << ");";
       nl(Out);
     }
-    if (F->hasCollector()) {
+    if (F->hasGC()) {
       printCppName(F);
-      Out << "->setCollector(\"" << F->getCollector() << "\");";
+      Out << "->setGC(\"" << F->getGC() << "\");";
       nl(Out);
     }
     if (is_inline) {
@@ -1763,6 +1781,7 @@ namespace {
     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";