Start using the new function cloning header
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
index 6806575b197733998bec628e1531392f132685cf..e27bd26ba27e7e621547245dffbf27ee7edb1071 100644 (file)
 
 #include "llvm/Assembly/CachedWriter.h"
 #include "llvm/Assembly/Writer.h"
+#include "llvm/Assembly/PrintModulePass.h"
 #include "llvm/SlotCalculator.h"
 #include "llvm/DerivedTypes.h"
+#include "llvm/Instruction.h"
 #include "llvm/Module.h"
 #include "llvm/Constants.h"
 #include "llvm/iMemory.h"
@@ -19,6 +21,7 @@
 #include "llvm/iPHINode.h"
 #include "llvm/iOther.h"
 #include "llvm/SymbolTable.h"
+#include "llvm/Support/CFG.h"
 #include "Support/StringExtras.h"
 #include "Support/STLExtras.h"
 #include <algorithm>
@@ -27,6 +30,11 @@ using std::map;
 using std::vector;
 using std::ostream;
 
+static RegisterPass<PrintModulePass>
+X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
+static RegisterPass<PrintFunctionPass>
+Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
+
 static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
                                    map<const Type *, string> &TypeTable,
                                    SlotCalculator *Table);
@@ -150,8 +158,7 @@ static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
     break;
   }
   default:
-    assert(0 && "Unhandled case in getTypeProps!");
-    Result = "<error>";
+    Result = "<unrecognized-type>";
   }
 
   TypeStack.pop_back();       // Remove self from stack...
@@ -265,7 +272,7 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
           (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
           (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
         
-        if (isprint(C)) {
+        if (isprint(C) && C != '"' && C != '\\') {
           Out << C;
         } else {
           Out << '\\'
@@ -326,8 +333,28 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
     } else {
       Out << "<pointer reference without context info>";
     }
+
+  } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
+    Out << CE->getOpcodeName();
+
+    bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
+    Out << " (";
+    
+    for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
+      printTypeInt(Out, (*OI)->getType(), TypeTable);
+      WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
+      if (OI+1 != CE->op_end())
+        Out << ", ";
+    }
+    
+    if (CE->getOpcode() == Instruction::Cast) {
+      Out << " to ";
+      printTypeInt(Out, CE->getType(), TypeTable);
+    }
+    Out << ")";
+
   } else {
-    assert(0 && "Unrecognized constant value!!!");
+    Out << "<placeholder or erroneous Constant>";
   }
 }
 
@@ -480,7 +507,8 @@ ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
   } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
     Out << OTy->getDescription();
   } else {
-    assert(Ty->isPrimitiveType() && "Unknown derived type!");
+    if (!Ty->isPrimitiveType())
+      Out << "<unknown derived type>";
     printType(Ty);
   }
   return Out;
@@ -513,7 +541,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
   if (GV->hasName()) Out << "%" << GV->getName() << " = ";
 
   if (GV->hasInternalLinkage()) Out << "internal ";
-  if (!GV->hasInitializer()) Out << "uninitialized ";
+  if (!GV->hasInitializer()) Out << "external ";
 
   Out << (GV->isConstant() ? "constant " : "global ");
   printType(GV->getType()->getElementType());
@@ -579,17 +607,8 @@ void AssemblyWriter::printFunction(const Function *F) {
   // Loop over the arguments, printing them...
   const FunctionType *FT = F->getFunctionType();
 
-  if (!F->isExternal()) {
-    for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
-      printArgument(I);
-  } else {
-    // Loop over the arguments, printing them...
-    for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
-          E = FT->getParamTypes().end(); I != E; ++I) {
-      if (I != FT->getParamTypes().begin()) Out << ", ";
-      printType(*I);
-    }
-  }
+  for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+    printArgument(I);
 
   // Finish printing arguments...
   if (FT->isVarArg()) {
@@ -634,8 +653,7 @@ void AssemblyWriter::printArgument(const Argument *Arg) {
 //
 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
   if (BB->hasName()) {              // Print out the label if it exists...
-    Out << "\n" << BB->getName() << ":\t\t\t\t\t;[#uses="
-        << BB->use_size() << "]";  // Output # uses
+    Out << "\n" << BB->getName() << ":";
   } else if (!BB->use_empty()) {      // Don't print block # of no uses...
     int Slot = Table.getValSlot(BB);
     Out << "\n; <label>:";
@@ -643,7 +661,21 @@ void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
       Out << Slot;         // Extra newline seperates out label's
     else 
       Out << "<badref>"; 
-    Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]";  // Output # uses
+  }
+  
+  // Output predecessors for the block...
+  Out << "\t\t;";
+  pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
+
+  if (PI == PE) {
+    Out << " No predecessors!";
+  } else {
+    Out << " preds =";
+    writeOperand(*PI, false, true);
+    for (++PI; PI != PE; ++PI) {
+      Out << ",";
+      writeOperand(*PI, false, true);
+    }
   }
   
   Out << "\n";
@@ -729,7 +761,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     //
     if (RetTy && MTy && !MTy->isVarArg() &&
         (!isa<PointerType>(RetTy) || 
-         !isa<FunctionType>(cast<PointerType>(RetTy)))) {
+         !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
       Out << " "; printType(RetTy);
       writeOperand(Operand, false);
     } else {
@@ -845,6 +877,13 @@ void Instruction::print(std::ostream &o) const {
 
 void Constant::print(std::ostream &o) const {
   if (this == 0) { o << "<null> constant value\n"; return; }
+
+  // Handle CPR's special, because they have context information...
+  if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
+    CPR->getValue()->print(o);  // Print as a global value, with context info.
+    return;
+  }
+
   o << " " << getType()->getDescription() << " ";
 
   map<const Type *, string> TypeTable;