Add support for printing constpointerrefs more nicely
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
index 842bc0912aa0ce0410bbf0af31d2d4460a91e63d..4ba166214304ee65b1c6358ecb191cfd19bc0284 100644 (file)
@@ -10,6 +10,7 @@
 
 #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"
@@ -28,6 +29,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);
@@ -265,7 +271,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 << '\\'
@@ -327,21 +333,25 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
       Out << "<pointer reference without context info>";
     }
 
-  } else if (const ConstantExpr *CE=dyn_cast<ConstantExpr>(CV)) {
+  } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
     Out << CE->getOpcodeName();
 
     bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
-    Out << (isGEP? " (" : " ");
+    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 << ", ";    // ((isGEP && OI == CE->op_begin())? " " : ", ");
+        Out << ", ";
     }
     
-    if (isGEP)
-      Out << ")";
+    if (CE->getOpcode() == Instruction::Cast) {
+      Out << " to ";
+      printTypeInt(Out, CE->getType(), TypeTable);
+    }
+    Out << ")";
+
   } else {
     Out << "<placeholder or erroneous Constant>";
   }
@@ -746,7 +756,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 {
@@ -862,6 +872,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;