Adjust to changes in the APInt interface.
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
index 3b0f0d6d9d83aca3c185da07052c9fe288746e7b..7c851f78fa5a4a456e85db5d03702e427848934a 100644 (file)
@@ -24,7 +24,7 @@
 #include "llvm/Instruction.h"
 #include "llvm/Instructions.h"
 #include "llvm/Module.h"
-#include "llvm/SymbolTable.h"
+#include "llvm/ValueSymbolTable.h"
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/STLExtras.h"
@@ -179,31 +179,47 @@ static SlotMachine *createSlotMachine(const Value *V) {
   return 0;
 }
 
-// getLLVMName - Turn the specified string into an 'LLVM name', which is either
-// prefixed with % (if the string only contains simple characters) or is
-// surrounded with ""'s (if it has special chars in it).
-static std::string getLLVMName(const std::string &Name,
-                               bool prefixName = true) {
-  assert(!Name.empty() && "Cannot get empty name!");
-
-  // First character cannot start with a number...
-  if (Name[0] >= '0' && Name[0] <= '9')
-    return "\"" + Name + "\"";
-
+/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
+/// with ""'s.
+static bool NameNeedsQuotes(const std::string &Name) {
+  if (Name[0] >= '0' && Name[0] <= '9') return true;
   // Scan to see if we have any characters that are not on the "white list"
   for (unsigned i = 0, e = Name.size(); i != e; ++i) {
     char C = Name[i];
     assert(C != '"' && "Illegal character in LLVM value name!");
     if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
         C != '-' && C != '.' && C != '_')
-      return "\"" + Name + "\"";
+      return true;
+  }
+  return false;
+}
+
+enum PrefixType {
+  GlobalPrefix,
+  LabelPrefix,
+  LocalPrefix
+};
+
+/// getLLVMName - Turn the specified string into an 'LLVM name', which is either
+/// prefixed with % (if the string only contains simple characters) or is
+/// surrounded with ""'s (if it has special chars in it).
+static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
+  assert(!Name.empty() && "Cannot get empty name!");
+
+  // First character cannot start with a number...
+  if (NameNeedsQuotes(Name)) {
+    if (Prefix == GlobalPrefix)
+      return "@\"" + Name + "\"";
+    return "\"" + Name + "\"";
   }
 
   // If we get here, then the identifier is legal to use as a "VarID".
-  if (prefixName)
-    return "%"+Name;
-  else
-    return Name;
+  switch (Prefix) {
+  default: assert(0 && "Bad prefix!");
+  case GlobalPrefix: return '@' + Name;
+  case LabelPrefix:  return Name;
+  case LocalPrefix:  return '%' + Name;
+  }      
 }
 
 
@@ -222,9 +238,9 @@ static void fillTypeNameTable(const Module *M,
     const Type *Ty = cast<Type>(TI->second);
     if (!isa<PointerType>(Ty) ||
         !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
-        !cast<PointerType>(Ty)->getElementType()->isIntegral() ||
+        !cast<PointerType>(Ty)->getElementType()->isInteger() ||
         isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
-      TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
+      TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
   }
 }
 
@@ -234,7 +250,7 @@ static void calcTypeName(const Type *Ty,
                          std::vector<const Type *> &TypeStack,
                          std::map<const Type *, std::string> &TypeNames,
                          std::string & Result){
-  if (Ty->isIntegral() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
+  if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
     Result += Ty->getDescription();  // Base case
     return;
   }
@@ -326,8 +342,8 @@ static void calcTypeName(const Type *Ty,
     Result += "]";
     break;
   }
-  case Type::PackedTyID: {
-    const PackedType *PTy = cast<PackedType>(Ty);
+  case Type::VectorTyID: {
+    const VectorType *PTy = cast<VectorType>(Ty);
     Result += "<" + utostr(PTy->getNumElements()) + " x ";
     calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
     Result += ">";
@@ -353,7 +369,7 @@ static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
   // Primitive types always print out their description, regardless of whether
   // they have been named or not.
   //
-  if (Ty->isIntegral() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
+  if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
     return Out << Ty->getDescription();
 
   // Check to see if the type is named.
@@ -448,7 +464,7 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
     if (CI->getType() == Type::Int1Ty) 
       Out << (CI->getZExtValue() ? "true" : "false");
     else 
-      Out << CI->getSExtValue();
+      Out << CI->getValue().toStringSigned(10);
   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
     // We would like to output the FP constant value in exponential notation,
     // but we cannot do this if doing so will lose precision.  Check here to
@@ -532,7 +548,7 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
     Out << " }";
     if (CS->getType()->isPacked())
       Out << '>';
-  } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
+  } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
       const Type *ETy = CP->getType()->getElementType();
       assert(CP->getNumOperands() > 0 &&
              "Number of operands for a PackedConst must be > 0");
@@ -587,7 +603,8 @@ static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
                                    SlotMachine *Machine) {
   Out << ' ';
   if (V->hasName())
-    Out << getLLVMName(V->getName());
+    Out << getLLVMName(V->getName(),
+                       isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
   else {
     const Constant *CV = dyn_cast<Constant>(V);
     if (CV && !isa<GlobalValue>(CV)) {
@@ -602,26 +619,31 @@ static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
       PrintEscapedString(IA->getConstraintString(), Out);
       Out << '"';
     } else {
+      char Prefix = '%';
       int Slot;
       if (Machine) {
-        if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+        if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
           Slot = Machine->getGlobalSlot(GV);
-        else
+          Prefix = '@';
+        } else {
           Slot = Machine->getLocalSlot(V);
+        }
       } else {
         Machine = createSlotMachine(V);
         if (Machine) {
-          if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+          if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
             Slot = Machine->getGlobalSlot(GV);
-          else
+            Prefix = '@';
+          } else {
             Slot = Machine->getLocalSlot(V);
+          }
         } else {
           Slot = -1;
         }
         delete Machine;
       }
       if (Slot != -1)
-        Out << '%' << Slot;
+        Out << Prefix << Slot;
       else
         Out << "<badref>";
     }
@@ -672,7 +694,6 @@ public:
   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 Constant *CPV)     { printConstant(CPV);  }
   inline void write(const Type *Ty)          { printType(Ty);       }
 
   void writeOperand(const Value *Op, bool PrintType);
@@ -682,8 +703,6 @@ public:
 private:
   void printModule(const Module *M);
   void printTypeSymbolTable(const TypeSymbolTable &ST);
-  void printValueSymbolTable(const SymbolTable &ST);
-  void printConstant(const Constant *CPV);
   void printGlobal(const GlobalVariable *GV);
   void printFunction(const Function *F);
   void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
@@ -753,7 +772,7 @@ std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
   } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
     Out << '[' << ATy->getNumElements() << " x ";
     printType(ATy->getElementType()) << ']';
-  } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
+  } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
     Out << '<' << PTy->getNumElements() << " x ";
     printType(PTy->getElementType()) << '>';
   }
@@ -787,17 +806,6 @@ void AssemblyWriter::printModule(const Module *M) {
 
   if (!M->getDataLayout().empty())
     Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
-
-  switch (M->getEndianness()) {
-  case Module::LittleEndian: Out << "target endian = little\n"; break;
-  case Module::BigEndian:    Out << "target endian = big\n";    break;
-  case Module::AnyEndianness: break;
-  }
-  switch (M->getPointerSize()) {
-  case Module::Pointer32:    Out << "target pointersize = 32\n"; break;
-  case Module::Pointer64:    Out << "target pointersize = 64\n"; break;
-  case Module::AnyPointerSize: break;
-  }
   if (!M->getTargetTriple().empty())
     Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
 
@@ -837,7 +845,6 @@ void AssemblyWriter::printModule(const Module *M) {
 
   // Loop over the symbol table, emitting all named constants.
   printTypeSymbolTable(M->getTypeSymbolTable());
-  printValueSymbolTable(M->getValueSymbolTable());
 
   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
        I != E; ++I)
@@ -851,7 +858,7 @@ void AssemblyWriter::printModule(const Module *M) {
 }
 
 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
-  if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
+  if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
 
   if (!GV->hasInitializer())
     switch (GV->getLinkage()) {
@@ -873,11 +880,9 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
       abort();
     }
     switch (GV->getVisibility()) {
+    default: assert(0 && "Invalid visibility style!");
     case GlobalValue::DefaultVisibility: break;
     case GlobalValue::HiddenVisibility: Out << "hidden "; break;
-    default:
-     cerr << "Invalid visibility style!\n";
-     abort();
     }
   }
   
@@ -903,7 +908,7 @@ void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
   // Print the types.
   for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
        TI != TE; ++TI) {
-    Out << "\t" << getLLVMName(TI->first) << " = type ";
+    Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
 
     // Make sure we print out at least one level of the type structure, so
     // that we do not get %FILE = type %FILE
@@ -912,55 +917,15 @@ void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
   }
 }
 
-// printSymbolTable - Run through symbol table looking for constants
-// and types. Emit their declarations.
-void AssemblyWriter::printValueSymbolTable(const SymbolTable &ST) {
-
-  // Print the constants, in type plane order.
-  for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
-       PI != ST.plane_end(); ++PI) {
-    SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
-    SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
-
-    for (; VI != VE; ++VI) {
-      const Value* V = VI->second;
-      const Constant *CPV = dyn_cast<Constant>(V) ;
-      if (CPV && !isa<GlobalValue>(V)) {
-        printConstant(CPV);
-      }
-    }
-  }
-}
-
-
-/// printConstant - Print out a constant pool entry...
-///
-void AssemblyWriter::printConstant(const Constant *CPV) {
-  // Don't print out unnamed constants, they will be inlined
-  if (!CPV->hasName()) return;
-
-  // Print out name...
-  Out << "\t" << getLLVMName(CPV->getName()) << " =";
-
-  // Write the value out now.
-  writeOperand(CPV, true);
-
-  printInfoComment(*CPV);
-  Out << "\n";
-}
-
 /// printFunction - Print all aspects of a function.
 ///
 void AssemblyWriter::printFunction(const Function *F) {
   // Print out the return type and name...
   Out << "\n";
 
-  // Ensure that no local symbols conflict with global symbols.
-  const_cast<Function*>(F)->renameLocalSymbols();
-
   if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
 
-  if (F->isExternal())
+  if (F->isDeclaration())
     switch (F->getLinkage()) {
     case GlobalValue::DLLImportLinkage:    Out << "declare dllimport "; break;
     case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
@@ -982,18 +947,15 @@ void AssemblyWriter::printFunction(const Function *F) {
       abort();
     }
     switch (F->getVisibility()) {
+    default: assert(0 && "Invalid visibility style!");
     case GlobalValue::DefaultVisibility: break;
     case GlobalValue::HiddenVisibility: Out << "hidden "; break;
-    default:
-     cerr << "Invalid visibility style!\n";
-     abort();
     }
   }
 
   // Print the calling convention.
   switch (F->getCallingConv()) {
   case CallingConv::C: break;   // default
-  case CallingConv::CSRet:        Out << "csretcc "; break;
   case CallingConv::Fast:         Out << "fastcc "; break;
   case CallingConv::Cold:         Out << "coldcc "; break;
   case CallingConv::X86_StdCall:  Out << "x86_stdcallcc "; break;
@@ -1004,9 +966,9 @@ void AssemblyWriter::printFunction(const Function *F) {
   const FunctionType *FT = F->getFunctionType();
   printType(F->getReturnType()) << ' ';
   if (!F->getName().empty())
-    Out << getLLVMName(F->getName());
+    Out << getLLVMName(F->getName(), GlobalPrefix);
   else
-    Out << "\"\"";
+    Out << "@\"\"";
   Out << '(';
   Machine.incorporateFunction(F);
 
@@ -1034,7 +996,7 @@ void AssemblyWriter::printFunction(const Function *F) {
   if (F->getAlignment())
     Out << " align " << F->getAlignment();
 
-  if (F->isExternal()) {
+  if (F->isDeclaration()) {
     Out << "\n";
   } else {
     Out << " {";
@@ -1062,14 +1024,14 @@ void AssemblyWriter::printArgument(const Argument *Arg,
 
   // Output name, if available...
   if (Arg->hasName())
-    Out << ' ' << getLLVMName(Arg->getName());
+    Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
 }
 
 /// 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 << "\n" << getLLVMName(BB->getName(), false) << ':';
+    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);
@@ -1143,7 +1105,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
 
   // Print out name if it exists...
   if (I.hasName())
-    Out << getLLVMName(I.getName()) << " = ";
+    Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
 
   // If this is a volatile load or store, print out the volatile marker.
   if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isVolatile()) ||
@@ -1203,7 +1165,6 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     // Print the calling convention being used.
     switch (CI->getCallingConv()) {
     case CallingConv::C: break;   // default
-    case CallingConv::CSRet: Out << " csretcc"; break;
     case CallingConv::Fast:  Out << " fastcc"; break;
     case CallingConv::Cold:  Out << " coldcc"; break;
     case CallingConv::X86_StdCall:  Out << "x86_stdcallcc "; break;
@@ -1246,7 +1207,6 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     // Print the calling convention being used.
     switch (II->getCallingConv()) {
     case CallingConv::C: break;   // default
-    case CallingConv::CSRet: Out << " csretcc"; break;
     case CallingConv::Fast:  Out << " fastcc"; break;
     case CallingConv::Cold:  Out << " coldcc"; break;
     case CallingConv::X86_StdCall:  Out << "x86_stdcallcc "; break;
@@ -1310,10 +1270,8 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     bool PrintAllTypes = false;
     const Type *TheType = Operand->getType();
 
-    // Shift Left & Right print both types even for Ubyte LHS, and select prints
-    // types even if all operands are bools.
-    if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
-        isa<ShuffleVectorInst>(I)) {
+    // Select, Store and ShuffleVector always print all types.
+    if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
       PrintAllTypes = true;
     } else {
       for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
@@ -1537,18 +1495,11 @@ int SlotMachine::getLocalSlot(const Value *V) {
   
   // Lookup the Value in the function and module maps.
   ValueMap::const_iterator FVI = FI->second.map.find(V);
-  TypedPlanes::const_iterator MI = mMap.find(VTy);
   
   // If the value doesn't exist in the function map, it is a <badref>
   if (FVI == FI->second.map.end()) return -1;
   
-  // Return the slot number as the module's contribution to
-  // the type plane plus the index in the function's contribution
-  // to the type plane.
-  if (MI != mMap.end())
-    return MI->second.next_slot + FVI->second;
-  else
-    return FVI->second;
+  return FVI->second;
 }