Add support for printing constpointerrefs more nicely
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
index 750169651287e92d337c20c2472f614979dfacee..4ba166214304ee65b1c6358ecb191cfd19bc0284 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/Function.h"
-#include "llvm/GlobalVariable.h"
-#include "llvm/BasicBlock.h"
 #include "llvm/Constants.h"
 #include "llvm/iMemory.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iPHINode.h"
 #include "llvm/iOther.h"
 #include "llvm/SymbolTable.h"
-#include "llvm/Argument.h"
 #include "Support/StringExtras.h"
 #include "Support/STLExtras.h"
 #include <algorithm>
@@ -31,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);
@@ -154,8 +157,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...
@@ -269,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 << '\\'
@@ -317,7 +319,7 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
   } else if (isa<ConstantPointerNull>(CV)) {
     Out << "null";
 
-  } else if (ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
+  } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
     const GlobalValue *V = PR->getValue();
     if (V->hasName()) {
       Out << "%" << V->getName();
@@ -330,8 +332,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>";
   }
 }
 
@@ -379,17 +401,17 @@ static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
 // whole instruction that generated it.
 //
 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, 
-                       bool PrintName, SlotCalculator *Table) {
+                       bool PrintName, const Module *Context) {
   map<const Type *, string> TypeNames;
-  const Module *M = getModuleFromVal(V);
+  if (Context == 0) Context = getModuleFromVal(V);
 
-  if (M && M->hasSymbolTable())
-    fillTypeNameTable(M, TypeNames);
+  if (Context && Context->hasSymbolTable())
+    fillTypeNameTable(Context, TypeNames);
 
   if (PrintType)
     printTypeInt(Out, V->getType(), TypeNames);
   
-  WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
+  WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
   return Out;
 }
 
@@ -414,7 +436,7 @@ public:
   inline void write(const GlobalVariable *G) { printGlobal(G);      }
   inline void write(const Function *F)       { printFunction(F);    }
   inline void write(const BasicBlock *BB)    { printBasicBlock(BB); }
-  inline void write(const Instruction *I)    { printInstruction(I); }
+  inline void write(const Instruction *I)    { printInstruction(*I); }
   inline void write(const Constant *CPV)     { printConstant(CPV);  }
   inline void write(const Type *Ty)          { printType(Ty);       }
 
@@ -428,7 +450,7 @@ private :
   void printFunction(const Function *F);
   void printArgument(const Argument *FA);
   void printBasicBlock(const BasicBlock *BB);
-  void printInstruction(const Instruction *I);
+  void printInstruction(const Instruction &I);
 
   // printType - Go to extreme measures to attempt to print out a short,
   // symbolic version of a type name.
@@ -444,7 +466,7 @@ private :
 
   // printInfoComment - Print a little comment after the instruction indicating
   // which slot it occupies.
-  void printInfoComment(const Value *V);
+  void printInfoComment(const Value &V);
 };
 
 
@@ -452,7 +474,7 @@ private :
 // without considering any symbolic types that we may have equal to it.
 //
 ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
-  if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
+  if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
     printType(FTy->getReturnType()) << " (";
     for (FunctionType::ParamTypes::const_iterator
            I = FTy->getParamTypes().begin(),
@@ -466,7 +488,7 @@ ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
       Out << "...";
     }
     Out << ")";
-  } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
+  } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
     Out << "{ ";
     for (StructType::ElementTypes::const_iterator
            I = STy->getElementTypes().begin(),
@@ -476,13 +498,16 @@ ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
       printType(*I);
     }
     Out << " }";
-  } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
+  } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
     printType(PTy->getElementType()) << "*";
-  } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
+  } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
     Out << "[" << ATy->getNumElements() << " x ";
     printType(ATy->getElementType()) << "]";
+  } 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;
@@ -501,13 +526,14 @@ void AssemblyWriter::printModule(const Module *M) {
   if (M->hasSymbolTable())
     printSymbolTable(*M->getSymbolTable());
   
-  for_each(M->gbegin(), M->gend(), 
-          bind_obj(this, &AssemblyWriter::printGlobal));
+  for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
+    printGlobal(I);
 
   Out << "\nimplementation   ; Functions:\n";
   
   // Output all of the functions...
-  for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
+  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
+    printFunction(I);
 }
 
 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
@@ -522,7 +548,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
   if (GV->hasInitializer())
     writeOperand(GV->getInitializer(), false, false);
 
-  printInfoComment(GV);
+  printInfoComment(*GV);
   Out << "\n";
 }
 
@@ -564,50 +590,49 @@ void AssemblyWriter::printConstant(const Constant *CPV) {
   // Write the value out now...
   writeOperand(CPV, true, false);
 
-  printInfoComment(CPV);
+  printInfoComment(*CPV);
   Out << "\n";
 }
 
 // printFunction - Print all aspects of a function.
 //
-void AssemblyWriter::printFunction(const Function *M) {
+void AssemblyWriter::printFunction(const Function *F) {
   // Print out the return type and name...
-  Out << "\n" << (M->isExternal() ? "declare " : "")
-      << (M->hasInternalLinkage() ? "internal " : "");
-  printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
-  Table.incorporateFunction(M);
+  Out << "\n" << (F->isExternal() ? "declare " : "")
+      << (F->hasInternalLinkage() ? "internal " : "");
+  printType(F->getReturnType()) << " %" << F->getName() << "(";
+  Table.incorporateFunction(F);
 
   // Loop over the arguments, printing them...
-  const FunctionType *MT = M->getFunctionType();
+  const FunctionType *FT = F->getFunctionType();
 
-  if (!M->isExternal()) {
-    for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
-            bind_obj(this, &AssemblyWriter::printArgument));
+  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...
-    const FunctionType *MT = M->getFunctionType();
-    for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
-          E = MT->getParamTypes().end(); I != E; ++I) {
-      if (I != MT->getParamTypes().begin()) Out << ", ";
+    for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
+          E = FT->getParamTypes().end(); I != E; ++I) {
+      if (I != FT->getParamTypes().begin()) Out << ", ";
       printType(*I);
     }
   }
 
   // Finish printing arguments...
-  if (MT->isVarArg()) {
-    if (MT->getParamTypes().size()) Out << ", ";
+  if (FT->isVarArg()) {
+    if (FT->getParamTypes().size()) Out << ", ";
     Out << "...";  // Output varargs portion of signature!
   }
   Out << ")";
 
-  if (M->isExternal()) {
+  if (F->isExternal()) {
     Out << "\n";
   } else {
     Out << " {";
   
     // Output all of its basic blocks... for the function
-    for_each(M->begin(), M->end(),
-            bind_obj(this, &AssemblyWriter::printBasicBlock));
+    for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
+      printBasicBlock(I);
 
     Out << "}\n";
   }
@@ -620,7 +645,7 @@ void AssemblyWriter::printFunction(const Function *M) {
 //
 void AssemblyWriter::printArgument(const Argument *Arg) {
   // Insert commas as we go... the first arg doesn't get a comma
-  if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
+  if (Arg != &Arg->getParent()->afront()) Out << ", ";
 
   // Output type...
   printType(Arg->getType());
@@ -651,72 +676,72 @@ void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
   Out << "\n";
 
   // Output all of the instructions in the basic block...
-  for_each(BB->begin(), BB->end(),
-          bind_obj(this, &AssemblyWriter::printInstruction));
+  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+    printInstruction(*I);
 }
 
 
 // printInfoComment - Print a little comment after the instruction indicating
 // which slot it occupies.
 //
-void AssemblyWriter::printInfoComment(const Value *V) {
-  if (V->getType() != Type::VoidTy) {
+void AssemblyWriter::printInfoComment(const Value &V) {
+  if (V.getType() != Type::VoidTy) {
     Out << "\t\t; <";
-    printType(V->getType()) << ">";
+    printType(V.getType()) << ">";
 
-    if (!V->hasName()) {
-      int Slot = Table.getValSlot(V); // Print out the def slot taken...
+    if (!V.hasName()) {
+      int Slot = Table.getValSlot(&V); // Print out the def slot taken...
       if (Slot >= 0) Out << ":" << Slot;
       else Out << ":<badref>";
     }
-    Out << " [#uses=" << V->use_size() << "]";  // Output # uses
+    Out << " [#uses=" << V.use_size() << "]";  // Output # uses
   }
 }
 
 // printInstruction - This member is called for each Instruction in a methd.
 //
-void AssemblyWriter::printInstruction(const Instruction *I) {
+void AssemblyWriter::printInstruction(const Instruction &I) {
   Out << "\t";
 
   // Print out name if it exists...
-  if (I && I->hasName())
-    Out << "%" << I->getName() << " = ";
+  if (I.hasName())
+    Out << "%" << I.getName() << " = ";
 
   // Print out the opcode...
-  Out << I->getOpcodeName();
+  Out << I.getOpcodeName();
 
   // Print out the type of the operands...
-  const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
+  const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
 
   // Special case conditional branches to swizzle the condition out to the front
-  if (isa<BranchInst>(I) && I->getNumOperands() > 1) {
-    writeOperand(I->getOperand(2), true);
+  if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
+    writeOperand(I.getOperand(2), true);
     Out << ",";
     writeOperand(Operand, true);
     Out << ",";
-    writeOperand(I->getOperand(1), true);
+    writeOperand(I.getOperand(1), true);
 
   } else if (isa<SwitchInst>(I)) {
     // Special case switch statement to get formatting nice and correct...
-    writeOperand(Operand         , true); Out << ",";
-    writeOperand(I->getOperand(1), true); Out << " [";
+    writeOperand(Operand        , true); Out << ",";
+    writeOperand(I.getOperand(1), true); Out << " [";
 
-    for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
+    for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
       Out << "\n\t\t";
-      writeOperand(I->getOperand(op  ), true); Out << ",";
-      writeOperand(I->getOperand(op+1), true);
+      writeOperand(I.getOperand(op  ), true); Out << ",";
+      writeOperand(I.getOperand(op+1), true);
     }
     Out << "\n\t]";
   } else if (isa<PHINode>(I)) {
     Out << " ";
-    printType(I->getType());
+    printType(I.getType());
     Out << " ";
 
-    for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
+    for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
       if (op) Out << ", ";
       Out << "[";  
-      writeOperand(I->getOperand(op  ), false); Out << ",";
-      writeOperand(I->getOperand(op+1), false); Out << " ]";
+      writeOperand(I.getOperand(op  ), false); Out << ",";
+      writeOperand(I.getOperand(op+1), false); Out << " ]";
     }
   } else if (isa<ReturnInst>(I) && !Operand) {
     Out << " void";
@@ -731,28 +756,28 @@ 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 {
       writeOperand(Operand, true);
     }
     Out << "(";
-    if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
-    for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
+    if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
+    for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
       Out << ",";
-      writeOperand(I->getOperand(op), true);
+      writeOperand(I.getOperand(op), true);
     }
 
     Out << " )";
-  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
+  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
     // TODO: Should try to print out short form of the Invoke instruction
     writeOperand(Operand, true);
     Out << "(";
-    if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
-    for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
+    if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
+    for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
       Out << ",";
-      writeOperand(I->getOperand(op), true);
+      writeOperand(I.getOperand(op), true);
     }
 
     Out << " )\n\t\t\tto";
@@ -760,7 +785,7 @@ void AssemblyWriter::printInstruction(const Instruction *I) {
     Out << " except";
     writeOperand(II->getExceptionalDest(), true);
 
-  } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
+  } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
     Out << " ";
     printType(AI->getType()->getElementType());
     if (AI->isArrayAllocation()) {
@@ -768,9 +793,9 @@ void AssemblyWriter::printInstruction(const Instruction *I) {
       writeOperand(AI->getArraySize(), true);
     }
   } else if (isa<CastInst>(I)) {
-    writeOperand(Operand, true);
+    if (Operand) writeOperand(Operand, true);
     Out << " to ";
-    printType(I->getType());
+    printType(I.getType());
   } else if (Operand) {   // Print the normal way...
 
     // PrintAllTypes - Instructions who have operands of all the same type 
@@ -779,8 +804,8 @@ void AssemblyWriter::printInstruction(const Instruction *I) {
     bool PrintAllTypes = false;
     const Type *TheType = Operand->getType();
 
-    for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
-      Operand = I->getOperand(i);
+    for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
+      Operand = I.getOperand(i);
       if (Operand->getType() != TheType) {
        PrintAllTypes = true;       // We have differing types!  Print them all!
        break;
@@ -792,12 +817,12 @@ void AssemblyWriter::printInstruction(const Instruction *I) {
 
     if (!PrintAllTypes) {
       Out << " ";
-      printType(I->getOperand(0)->getType());
+      printType(I.getOperand(0)->getType());
     }
 
-    for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
+    for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
       if (i) Out << ",";
-      writeOperand(I->getOperand(i), PrintAllTypes);
+      writeOperand(I.getOperand(i), PrintAllTypes);
     }
   }
 
@@ -847,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;