Prevent a crash in debug dumps.
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
index 64119eddc82eadc02927f2a802f49cb8fa68b443..9e010051a51102c08c06818e5e4140d11ffbcfd8 100644 (file)
@@ -20,7 +20,6 @@
 #include "llvm/CallingConv.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/ParamAttrsList.h"
 #include "llvm/InlineAsm.h"
 #include "llvm/Instruction.h"
 #include "llvm/Instructions.h"
@@ -528,7 +527,7 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
           Out << (unsigned char)(nibble + '0');
         else
           Out << (unsigned char)(nibble - 10 + 'A');
-        if (shiftcount == 0) {
+        if (shiftcount == 0 && j+4 < width) {
           word = *(++p);
           shiftcount = 64;
           if (width-j-4 < 64)
@@ -540,7 +539,7 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
     Out << "zeroinitializer";
   } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
     // As a special case, print the array as a string if it is an array of
-    // ubytes or an array of sbytes with positive values.
+    // i8 with ConstantInt values.
     //
     const Type *ETy = CA->getType()->getElementType();
     if (CA->isString()) {
@@ -625,6 +624,12 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
         Out << ", ";
     }
 
+    if (CE->hasIndices()) {
+      const SmallVector<unsigned, 4> &Indices = CE->getIndices();
+      for (unsigned i = 0, e = Indices.size(); i != e; ++i)
+        Out << ", " << Indices[i];
+    }
+
     if (CE->isCast()) {
       Out << " to ";
       printTypeInt(Out, CE->getType(), TypeTable);
@@ -849,7 +854,7 @@ void AssemblyWriter::writeParamOperand(const Value *Operand,
     printType(Operand->getType());
     // Print parameter attributes list
     if (Attrs != ParamAttr::None)
-      Out << ' ' << ParamAttrsList::getParamAttrsText(Attrs);
+      Out << ' ' << ParamAttr::getAsString(Attrs);
     // Print the operand
     WriteAsOperandInternal(Out, Operand, TypeNames, &Machine);
   }
@@ -931,6 +936,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
   } else {
     switch (GV->getLinkage()) {
     case GlobalValue::InternalLinkage:     Out << "internal "; break;
+    case GlobalValue::CommonLinkage:       Out << "common "; break;
     case GlobalValue::LinkOnceLinkage:     Out << "linkonce "; break;
     case GlobalValue::WeakLinkage:         Out << "weak "; break;
     case GlobalValue::AppendingLinkage:    Out << "appending "; break;
@@ -973,7 +979,11 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
 }
 
 void AssemblyWriter::printAlias(const GlobalAlias *GA) {
-  Out << getLLVMName(GA->getName(), GlobalPrefix) << " = ";
+  // Don't crash when dumping partially built GA
+  if (!GA->hasName())
+    Out << "<<nameless>> = ";
+  else
+    Out << getLLVMName(GA->getName(), GlobalPrefix) << " = ";
   switch (GA->getVisibility()) {
   default: assert(0 && "Invalid visibility style!");
   case GlobalValue::DefaultVisibility: break;
@@ -1004,6 +1014,9 @@ void AssemblyWriter::printAlias(const GlobalAlias *GA) {
       Out << getLLVMName(F->getName(), GlobalPrefix);
     else
       Out << "@\"\"";
+  } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
+    printType(GA->getType());
+    Out << " " << getLLVMName(GA->getName(), GlobalPrefix);
   } else {
     const ConstantExpr *CE = 0;
     if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
@@ -1047,6 +1060,7 @@ void AssemblyWriter::printFunction(const Function *F) {
   case GlobalValue::InternalLinkage:     Out << "internal "; break;
   case GlobalValue::LinkOnceLinkage:     Out << "linkonce "; break;
   case GlobalValue::WeakLinkage:         Out << "weak "; break;
+  case GlobalValue::CommonLinkage:       Out << "common "; break;
   case GlobalValue::AppendingLinkage:    Out << "appending "; break;
   case GlobalValue::DLLImportLinkage:    Out << "dllimport "; break;
   case GlobalValue::DLLExportLinkage:    Out << "dllexport "; break;
@@ -1074,7 +1088,7 @@ void AssemblyWriter::printFunction(const Function *F) {
   }
 
   const FunctionType *FT = F->getFunctionType();
-  const ParamAttrsList *Attrs = F->getParamAttrs();
+  const PAListPtr &Attrs = F->getParamAttrs();
   printType(F->getReturnType()) << ' ';
   if (!F->getName().empty())
     Out << getLLVMName(F->getName(), GlobalPrefix);
@@ -1092,8 +1106,7 @@ void AssemblyWriter::printFunction(const Function *F) {
          I != E; ++I) {
       // Insert commas as we go... the first arg doesn't get a comma
       if (I != F->arg_begin()) Out << ", ";
-      printArgument(I, (Attrs ? Attrs->getParamAttrs(Idx)
-                              : ParamAttr::None));
+      printArgument(I, Attrs.getParamAttrs(Idx));
       Idx++;
     }
   } else {
@@ -1105,10 +1118,9 @@ void AssemblyWriter::printFunction(const Function *F) {
       // Output type...
       printType(FT->getParamType(i));
       
-      ParameterAttributes ArgAttrs = ParamAttr::None;
-      if (Attrs) ArgAttrs = Attrs->getParamAttrs(i+1);
+      ParameterAttributes ArgAttrs = Attrs.getParamAttrs(i+1);
       if (ArgAttrs != ParamAttr::None)
-        Out << ' ' << ParamAttrsList::getParamAttrsText(ArgAttrs);
+        Out << ' ' << ParamAttr::getAsString(ArgAttrs);
     }
   }
 
@@ -1118,8 +1130,9 @@ void AssemblyWriter::printFunction(const Function *F) {
     Out << "...";  // Output varargs portion of signature!
   }
   Out << ')';
-  if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None)
-    Out << ' ' << Attrs->getParamAttrsTextByIndex(0);
+  ParameterAttributes RetAttrs = Attrs.getParamAttrs(0);
+  if (RetAttrs != ParamAttr::None)
+    Out << ' ' << ParamAttr::getAsString(Attrs.getParamAttrs(0));
   if (F->hasSection())
     Out << " section \"" << F->getSection() << '"';
   if (F->getAlignment())
@@ -1130,7 +1143,7 @@ void AssemblyWriter::printFunction(const Function *F) {
   if (F->isDeclaration()) {
     Out << "\n";
   } else {
-    Out << " {\n";
+    Out << " {";
 
     // Output all of its basic blocks... for the function
     for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
@@ -1152,7 +1165,7 @@ void AssemblyWriter::printArgument(const Argument *Arg,
 
   // Output parameter attributes list
   if (Attrs != ParamAttr::None)
-    Out << ' ' << ParamAttrsList::getParamAttrsText(Attrs);
+    Out << ' ' << ParamAttr::getAsString(Attrs);
 
   // Output name, if available...
   if (Arg->hasName())
@@ -1162,19 +1175,10 @@ void AssemblyWriter::printArgument(const Argument *Arg,
 /// printBasicBlock - This member is called for each basic block in a method.
 ///
 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
-  if (BB->hasName())              // Print out the label if it exists...
-    Out << getLLVMName(BB->getName(), LabelPrefix) << ':';
-
-  if (const BasicBlock* unwindDest = BB->getUnwindDest()) {
-    if (BB->hasName())
-      Out << ' ';
-
-    Out << "unwind_to";
-    writeOperand(unwindDest, false);
-  }
-
-  if (!BB->hasName() && !BB->use_empty()) { // Don't print block # of no uses...
-    Out << "; <label>:";
+  if (BB->hasName()) {              // Print out the label if it exists...
+    Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
+  } else if (!BB->use_empty()) {      // Don't print block # of no uses...
+    Out << "\n; <label>:";
     int Slot = Machine.getLocalSlot(BB);
     if (Slot != -1)
       Out << Slot;
@@ -1184,28 +1188,24 @@ void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
 
   if (BB->getParent() == 0)
     Out << "\t\t; Error: Block without parent!";
-  else {
-    if (BB != &BB->getParent()->getEntryBlock()) {  // Not the entry block?
-      // 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 =";
+  else if (BB != &BB->getParent()->getEntryBlock()) {  // Not the entry block?
+    // 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);
+      for (++PI; PI != PE; ++PI) {
+        Out << ',';
         writeOperand(*PI, false);
-        for (++PI; PI != PE; ++PI) {
-          Out << ',';
-          writeOperand(*PI, false);
-        }
       }
     }
   }
 
-  if (BB->hasName() || !BB->use_empty() || BB->getUnwindDest() ||
-      BB != &BB->getParent()->getEntryBlock())
-    Out << "\n";
+  Out << "\n";
 
   if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
 
@@ -1263,11 +1263,8 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
   Out << I.getOpcodeName();
 
   // Print out the compare instruction predicates
-  if (const FCmpInst *FCI = dyn_cast<FCmpInst>(&I)) {
-    Out << " " << getPredicateText(FCI->getPredicate());
-  } else if (const ICmpInst *ICI = dyn_cast<ICmpInst>(&I)) {
-    Out << " " << getPredicateText(ICI->getPredicate());
-  }
+  if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
+    Out << " " << getPredicateText(CI->getPredicate());
 
   // Print out the type of the operands...
   const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
@@ -1305,6 +1302,15 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
   } else if (const GetResultInst *GRI = dyn_cast<GetResultInst>(&I)) {
     writeOperand(I.getOperand(0), true);
     Out << ", " << GRI->getIndex();
+  } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
+    writeOperand(I.getOperand(0), true);
+    for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
+      Out << ", " << *i;
+  } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
+    writeOperand(I.getOperand(0), true); Out << ',';
+    writeOperand(I.getOperand(1), true);
+    for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
+      Out << ", " << *i;
   } else if (isa<ReturnInst>(I) && !Operand) {
     Out << " void";
   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
@@ -1321,7 +1327,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     const PointerType    *PTy = cast<PointerType>(Operand->getType());
     const FunctionType   *FTy = cast<FunctionType>(PTy->getElementType());
     const Type         *RetTy = FTy->getReturnType();
-    const ParamAttrsList *PAL = CI->getParamAttrs();
+    const PAListPtr &PAL = CI->getParamAttrs();
 
     // If possible, print out the short form of the call instruction.  We can
     // only do this if the first argument is a pointer to a nonvararg function,
@@ -1339,17 +1345,16 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
       if (op > 1)
         Out << ',';
-      writeParamOperand(I.getOperand(op), PAL ? PAL->getParamAttrs(op) : 
-                                          ParamAttr::None);
+      writeParamOperand(I.getOperand(op), PAL.getParamAttrs(op));
     }
     Out << " )";
-    if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
-      Out << ' ' << PAL->getParamAttrsTextByIndex(0);
+    if (PAL.getParamAttrs(0) != ParamAttr::None)
+      Out << ' ' << ParamAttr::getAsString(PAL.getParamAttrs(0));
   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
     const PointerType    *PTy = cast<PointerType>(Operand->getType());
     const FunctionType   *FTy = cast<FunctionType>(PTy->getElementType());
     const Type         *RetTy = FTy->getReturnType();
-    const ParamAttrsList *PAL = II->getParamAttrs();
+    const PAListPtr &PAL = II->getParamAttrs();
 
     // Print the calling convention being used.
     switch (II->getCallingConv()) {
@@ -1378,13 +1383,12 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
       if (op > 3)
         Out << ',';
-      writeParamOperand(I.getOperand(op), PAL ? PAL->getParamAttrs(op-2) : 
-                                          ParamAttr::None);
+      writeParamOperand(I.getOperand(op), PAL.getParamAttrs(op-2));
     }
 
     Out << " )";
-    if (PAL && PAL->getParamAttrs(0) != ParamAttr::None)
-      Out << " " << PAL->getParamAttrsTextByIndex(0);
+    if (PAL.getParamAttrs(0) != ParamAttr::None)
+      Out << " " << ParamAttr::getAsString(PAL.getParamAttrs(0));
     Out << "\n\t\t\tto";
     writeOperand(II->getNormalDest(), true);
     Out << " unwind";
@@ -1529,18 +1533,6 @@ void Value::dump() const { print(*cerr.stream()); cerr << '\n'; }
 // Located here because so much of the needed functionality is here.
 void Type::dump() const { print(*cerr.stream()); cerr << '\n'; }
 
-void
-ParamAttrsList::dump() const {
-  cerr << "PAL[ ";
-  for (unsigned i = 0; i < attrs.size(); ++i) {
-    uint16_t index = getParamIndex(i);
-    ParameterAttributes attrs = getParamAttrs(index);
-    cerr << "{" << index << "," << attrs << "} ";
-  }
-
-  cerr << "]\n";
-}
-
 //===----------------------------------------------------------------------===//
 //                         SlotMachine Implementation
 //===----------------------------------------------------------------------===//