Fix bug: Assembler/2002-12-15-GlobalResolve.ll
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
index e33d9f400ab4b584595b3f1dae09563afeab68ab..2c9a77c834fb909e9fe668868fbaa40444ea3b64 100644 (file)
@@ -21,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>
@@ -29,8 +30,10 @@ using std::map;
 using std::vector;
 using std::ostream;
 
-static RegisterPass<PrintModulePass>   X("printm", "Print module to stderr");
-static RegisterPass<PrintFunctionPass> Y("print", "Print function to stderr");
+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,
@@ -71,20 +74,19 @@ static SlotCalculator *createSlotCalculator(const Value *V) {
 //
 static void fillTypeNameTable(const Module *M,
                               map<const Type *, string> &TypeNames) {
-  if (M && M->hasSymbolTable()) {
-    const SymbolTable *ST = M->getSymbolTable();
-    SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
-    if (PI != ST->end()) {
-      SymbolTable::type_const_iterator I = PI->second.begin();
-      for (; I != PI->second.end(); ++I) {
-        // As a heuristic, don't insert pointer to primitive types, because
-        // they are used too often to have a single useful name.
-        //
-        const Type *Ty = cast<const Type>(I->second);
-        if (!isa<PointerType>(Ty) ||
-            !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
-          TypeNames.insert(std::make_pair(Ty, "%"+I->first));
-      }
+  if (!M) return;
+  const SymbolTable &ST = M->getSymbolTable();
+  SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
+  if (PI != ST.end()) {
+    SymbolTable::type_const_iterator I = PI->second.begin();
+    for (; I != PI->second.end(); ++I) {
+      // As a heuristic, don't insert pointer to primitive types, because
+      // they are used too often to have a single useful name.
+      //
+      const Type *Ty = cast<const Type>(I->second);
+      if (!isa<PointerType>(Ty) ||
+          !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
+        TypeNames.insert(std::make_pair(Ty, "%"+I->first));
     }
   }
 }
@@ -197,7 +199,7 @@ ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
 
   // If they want us to print out a type, attempt to make it symbolic if there
   // is a symbol table in the module...
-  if (M && M->hasSymbolTable()) {
+  if (M) {
     map<const Type *, string> TypeNames;
     fillTypeNameTable(M, TypeNames);
     
@@ -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) && C != '"') {
+        if (isprint(C) && C != '"' && C != '\\') {
           Out << C;
         } else {
           Out << '\\'
@@ -331,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>";
   }
@@ -399,7 +405,7 @@ ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
   map<const Type *, string> TypeNames;
   if (Context == 0) Context = getModuleFromVal(V);
 
-  if (Context && Context->hasSymbolTable())
+  if (Context)
     fillTypeNameTable(Context, TypeNames);
 
   if (PrintType)
@@ -517,8 +523,7 @@ void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
 
 void AssemblyWriter::printModule(const Module *M) {
   // Loop over the symbol table, emitting all named constants...
-  if (M->hasSymbolTable())
-    printSymbolTable(*M->getSymbolTable());
+  printSymbolTable(M->getSymbolTable());
   
   for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
     printGlobal(I);
@@ -534,7 +539,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());
@@ -600,17 +605,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()) {
@@ -655,8 +651,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>:";
@@ -664,7 +659,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";
@@ -866,6 +875,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;