Printing support for more stuff
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9AsmPrinter.cpp
index 34d80b1e8656b359d4d397e24f6f6d4ccfc7436b..38518eef41c2c9ebd23df1ffa645047112387351 100644 (file)
 
 #include "SparcInternals.h"
 #include "llvm/CodeGen/MachineInstr.h"
-#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
-#include "llvm/CodeGen/MachineCodeForMethod.h"
-#include "llvm/GlobalVariable.h"
+#include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Function.h"
 #include "llvm/Module.h"
 #include "llvm/SlotCalculator.h"
 #include "llvm/Pass.h"
 #include "llvm/Assembly/Writer.h"
 #include "Support/StringExtras.h"
-#include <iostream>
 using std::string;
 
 namespace {
@@ -34,7 +29,7 @@ class GlobalIdTable: public Annotation {
   static AnnotationID AnnotId;
   friend class AsmPrinter;              // give access to AnnotId
   
-  typedef std::hash_map<const Value*, int> ValIdMap;
+  typedef hash_map<const Value*, int> ValIdMap;
   typedef ValIdMap::const_iterator ValIdMapConstIterator;
   typedef ValIdMap::      iterator ValIdMapIterator;
 public:
@@ -62,7 +57,7 @@ public:
     Text,
     ReadOnlyData,
     InitRWData,
-    UninitRWData,
+    ZeroInitRWData,
   } CurSection;
 
   AsmPrinter(std::ostream &os, const TargetMachine &T)
@@ -107,21 +102,20 @@ public:
       case Text:         toAsm << "\".text\""; break;
       case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
       case InitRWData:   toAsm << "\".data\",#alloc,#write"; break;
-      case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
+      case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
       }
     toAsm << "\n";
   }
 
-  static std::string getValidSymbolName(const string &S) {
+  static string getValidSymbolName(const string &S) {
     string Result;
     
     // Symbol names in Sparc assembly language have these rules:
     // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
     // (b) A name beginning in "." is treated as a local name.
-    // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
     // 
-    if (S[0] == '_' || isdigit(S[0]))
-      Result += "ll";
+    if (isdigit(S[0]))
+      Result = "ll";
     
     for (unsigned i = 0; i < S.size(); ++i)
       {
@@ -145,9 +139,9 @@ public:
   //
   string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
     string Result = FPrefix ? FPrefix : "";  // "Forced prefix"
-    
+
     Result +=  V->hasName() ? V->getName() : string(Prefix);
-    
+
     // Qualify all internal names with a unique id.
     if (!isExternal(V)) {
       int valId = idTable->Table.getValSlot(V);
@@ -159,9 +153,12 @@ public:
           valId = I->second;
       }
       Result = Result + "_" + itostr(valId);
+
+      // Replace or prefix problem characters in the name
+      Result = getValidSymbolName(Result);
     }
-    
-    return getValidSymbolName(Result);
+
+    return Result;
   }
   
   // getID Wrappers - Ensure consistent usage...
@@ -185,6 +182,82 @@ public:
     assert(0 && "Unexpected type of GlobalValue!");
     return "";
   }
+
+  // ConstantExprToString() - Convert a ConstantExpr to an asm expression
+  // and return this as a string.
+  string ConstantExprToString(const ConstantExpr* CE,
+                              const TargetMachine& target) {
+    string S;
+    switch(CE->getOpcode()) {
+    case Instruction::GetElementPtr:
+      { // generate a symbolic expression for the byte address
+        const Value* ptrVal = CE->getOperand(0);
+        std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
+        S += "(" + valToExprString(ptrVal, target) + ") + ("
+          + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
+          + ")";
+        break;
+      }
+
+    case Instruction::Cast:
+      // Support only non-converting casts for now, i.e., a no-op.
+      // This assertion is not a complete check.
+      assert(target.DataLayout.getTypeSize(CE->getType()) ==
+             target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
+      S += "(" + valToExprString(CE->getOperand(0), target) + ")";
+      break;
+
+    case Instruction::Add:
+      S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
+               + valToExprString(CE->getOperand(1), target) + ")";
+      break;
+
+    default:
+      assert(0 && "Unsupported operator in ConstantExprToString()");
+      break;
+    }
+
+    return S;
+  }
+
+  // valToExprString - Helper function for ConstantExprToString().
+  // Appends result to argument string S.
+  // 
+  string valToExprString(const Value* V, const TargetMachine& target) {
+    string S;
+    bool failed = false;
+    if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
+
+      if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
+        S += string(CB == ConstantBool::True ? "1" : "0");
+      else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
+        S += itostr(CI->getValue());
+      else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
+        S += utostr(CI->getValue());
+      else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
+        S += ftostr(CFP->getValue());
+      else if (isa<ConstantPointerNull>(CV))
+        S += "0";
+      else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
+        S += valToExprString(CPR->getValue(), target);
+      else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
+        S += ConstantExprToString(CE, target);
+      else
+        failed = true;
+
+    } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
+      S += getID(GV);
+    }
+    else
+      failed = true;
+
+    if (failed) {
+      assert(0 && "Cannot convert value to string");
+      S += "<illegal-value>";
+    }
+    return S;
+  }
+
 };
 
 
@@ -224,7 +297,7 @@ struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
 
   void emitFunction(const Function &F);
 private :
-  void emitBasicBlock(const BasicBlock *BB);
+  void emitBasicBlock(const MachineBasicBlock &MBB);
   void emitMachineInst(const MachineInstr *MI);
   
   unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
@@ -312,7 +385,7 @@ SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
   else
     needBitsFlag = false;
   
-  switch (mop.getOperandType())
+  switch (mop.getType())
     {
     case MachineOperand::MO_VirtualRegister:
     case MachineOperand::MO_CCRegister:
@@ -370,10 +443,10 @@ SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
 {
   unsigned Opcode = MI->getOpCode();
 
-  if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
+  if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
     return;  // IGNORE PHI NODES
 
-  toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
+  toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
 
   unsigned Mask = getOperandMask(Opcode);
   
@@ -384,25 +457,21 @@ SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
       if (NeedComma) toAsm << ", ";         // Handle comma outputing
       NeedComma = true;
       N = printOperands(MI, OpNum);
-    }
-  else
-    N = 1;
+    } else
+      N = 1;
   
   toAsm << "\n";
 }
 
 void
-SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
+SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
 {
   // Emit a label for the basic block
-  toAsm << getID(BB) << ":\n";
-
-  // Get the vector of machine instructions corresponding to this bb.
-  const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
-  MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
+  toAsm << getID(MBB.getBasicBlock()) << ":\n";
 
   // Loop over all of the instructions in the basic block...
-  for (; MII != MIE; ++MII)
+  for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
+       MII != MIE; ++MII)
     emitMachineInst(*MII);
   toAsm << "\n";  // Seperate BB's with newlines
 }
@@ -419,8 +488,9 @@ SparcFunctionAsmPrinter::emitFunction(const Function &F)
   toAsm << methName << ":\n";
 
   // Output code for all of the basic blocks in the function...
-  for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
-    emitBasicBlock(I);
+  MachineFunction &MF = MachineFunction::get(&F);
+  for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E; ++I)
+    emitBasicBlock(*I);
 
   // Output a .size directive so the debugger knows the extents of the function
   toAsm << ".EndOf_" << methName << ":\n\t.size "
@@ -433,7 +503,7 @@ SparcFunctionAsmPrinter::emitFunction(const Function &F)
 
 }  // End anonymous namespace
 
-Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
+Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
   return new SparcFunctionAsmPrinter(Out, *this);
 }
 
@@ -466,28 +536,29 @@ public:
   }
 
 private:
-  void emitGlobalsAndConstants(const Module &M);
+  void emitGlobalsAndConstants  (const Module &M);
 
-  void printGlobalVariable(const GlobalVariable *GV);
-  void printSingleConstant(   const Constant* CV);
-  void printConstantValueOnly(const Constant* CV);
-  void printConstant(         const Constant* CV, std::string valID = "");
+  void printGlobalVariable      (const GlobalVariable *GV);
+  void PrintZeroBytesToPad      (int numBytes);
+  void printSingleConstantValue (const Constant* CV);
+  void printConstantValueOnly   (const Constant* CV, int numPadBytes = 0);
+  void printConstant            (const Constant* CV, string valID = "");
 
-  static void FoldConstants(const Module &M,
-                            std::hash_set<const Constant*> &moduleConstants);
+  static void FoldConstants     (const Module &M,
+                                 hash_set<const Constant*> &moduleConstants);
 };
 
 
 // Can we treat the specified array as a string?  Only if it is an array of
 // ubytes or non-negative sbytes.
 //
-static bool isStringCompatible(const ConstantArray *CPA) {
-  const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
+static bool isStringCompatible(const ConstantArray *CVA) {
+  const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
   if (ETy == Type::UByteTy) return true;
   if (ETy != Type::SByteTy) return false;
 
-  for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
-    if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
+  for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
+    if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
       return false;
 
   return true;
@@ -501,19 +572,21 @@ static inline char toOctal(int X) {
 // getAsCString - Return the specified array as a C compatible string, only if
 // the predicate isStringCompatible is true.
 //
-static string getAsCString(const ConstantArray *CPA) {
-  assert(isStringCompatible(CPA) && "Array is not string compatible!");
+static string getAsCString(const ConstantArray *CVA) {
+  assert(isStringCompatible(CVA) && "Array is not string compatible!");
 
   string Result;
-  const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
+  const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
   Result = "\"";
-  for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
+  for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
     unsigned char C = (ETy == Type::SByteTy) ?
-      (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
-      (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
+      (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
+      (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
 
     if (C == '"') {
       Result += "\\\"";
+    } else if (C == '\\') {
+      Result += "\\\\";
     } else if (isprint(C)) {
       Result += C;
     } else {
@@ -546,6 +619,7 @@ ArrayTypeIsString(const ArrayType* arrayType)
           arrayType->getElementType() == Type::SByteTy);
 }
 
+
 inline const string
 TypeToDataDirective(const Type* type)
 {
@@ -573,24 +647,30 @@ TypeToDataDirective(const Type* type)
     }
 }
 
+// Get the size of the type
+// 
+inline unsigned int
+TypeToSize(const Type* type, const TargetMachine& target)
+{
+  return target.findOptimalStorageSize(type);
+}
+
 // Get the size of the constant for the given target.
 // If this is an unsized array, return 0.
 // 
 inline unsigned int
 ConstantToSize(const Constant* CV, const TargetMachine& target)
 {
-  if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
+  if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
     {
-      const ArrayType *aty = cast<ArrayType>(CPA->getType());
+      const ArrayType *aty = cast<ArrayType>(CVA->getType());
       if (ArrayTypeIsString(aty))
-        return 1 + CPA->getNumOperands();
+        return 1 + CVA->getNumOperands();
     }
   
-  return target.findOptimalStorageSize(CV->getType());
+  return TypeToSize(CV->getType(), target);
 }
 
-
-
 // Align data larger than one L1 cache line on L1 cache line boundaries.
 // Align all smaller data on the next higher 2^x boundary (4, 8, ...).
 // 
@@ -611,7 +691,7 @@ SizeToAlignment(unsigned int size, const TargetMachine& target)
 inline unsigned int
 TypeToAlignment(const Type* type, const TargetMachine& target)
 {
-  return SizeToAlignment(target.findOptimalStorageSize(type), target);
+  return SizeToAlignment(TypeToSize(type, target), target);
 }
 
 // Get the size of the constant and then use SizeToAlignment.
@@ -619,9 +699,9 @@ TypeToAlignment(const Type* type, const TargetMachine& target)
 inline unsigned int
 ConstantToAlignment(const Constant* CV, const TargetMachine& target)
 {
-  if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
-    if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
-      return SizeToAlignment(1 + CPA->getNumOperands(), target);
+  if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
+    if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
+      return SizeToAlignment(1 + CVA->getNumOperands(), target);
   
   return TypeToAlignment(CV->getType(), target);
 }
@@ -629,7 +709,7 @@ ConstantToAlignment(const Constant* CV, const TargetMachine& target)
 
 // Print a single constant value.
 void
-SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
+SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
 {
   assert(CV->getType() != Type::VoidTy &&
          CV->getType() != Type::TypeTy &&
@@ -669,43 +749,82 @@ SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
       // Use the name of the variable or method as the address value.
       toAsm << getID(CPR->getValue()) << "\n";
     }
-  else if (const ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
-    {
-      assert(CPP->isNullValue() &&
-             "Cannot yet print non-null pointer constants to assembly");
+  else if (isa<ConstantPointerNull>(CV))
+    { // Null pointer value
       toAsm << "0\n";
     }
+  else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
+    { // Constant expression built from operators, constants, and symbolic addrs
+      toAsm << ConstantExprToString(CE, Target) << "\n";
+    }
   else
     {
       assert(0 && "Unknown elementary type for constant");
     }
 }
 
+void
+SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
+{
+  for ( ; numBytes >= 8; numBytes -= 8)
+    printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
+
+  if (numBytes >= 4)
+    {
+      printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
+      numBytes -= 4;
+    }
+
+  while (numBytes--)
+    printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
+}
+
 // Print a constant value or values (it may be an aggregate).
-// Uses printSingleConstant() to print each individual value.
+// Uses printSingleConstantValue() to print each individual value.
 void
-SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
+SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
+                                              int numPadBytes /* = 0*/)
 {
-  const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
-  
-  if (CPA && isStringCompatible(CPA))
+  const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
+
+  if (numPadBytes)
+    PrintZeroBytesToPad(numPadBytes);
+
+  if (CVA && isStringCompatible(CVA))
     { // print the string alone and return
-      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
+      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
     }
-  else if (CPA)
+  else if (CVA)
     { // Not a string.  Print the values in successive locations
-      const std::vector<Use> &constValues = CPA->getValues();
+      const std::vector<Use> &constValues = CVA->getValues();
       for (unsigned i=0; i < constValues.size(); i++)
         printConstantValueOnly(cast<Constant>(constValues[i].get()));
     }
-  else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
-    { // Print the fields in successive locations
-      const std::vector<Use>& constValues = CPS->getValues();
-      for (unsigned i=0; i < constValues.size(); i++)
-        printConstantValueOnly(cast<Constant>(constValues[i].get()));
+  else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
+    { // Print the fields in successive locations. Pad to align if needed!
+      const StructLayout *cvsLayout =
+        Target.DataLayout.getStructLayout(CVS->getType());
+      const std::vector<Use>& constValues = CVS->getValues();
+      unsigned sizeSoFar = 0;
+      for (unsigned i=0, N = constValues.size(); i < N; i++)
+        {
+          const Constant* field = cast<Constant>(constValues[i].get());
+
+          // Check if padding is needed and insert one or more 0s.
+          unsigned fieldSize = Target.DataLayout.getTypeSize(field->getType());
+          int padSize = ((i == N-1? cvsLayout->StructSize
+                                  : cvsLayout->MemberOffsets[i+1])
+                         - cvsLayout->MemberOffsets[i]) - fieldSize;
+          sizeSoFar += (fieldSize + padSize);
+
+          // Now print the actual field value
+          printConstantValueOnly(field, padSize);
+        }
+      assert(sizeSoFar == cvsLayout->StructSize &&
+             "Layout of constant struct may be incorrect!");
     }
   else
-    printSingleConstant(CV);
+    printSingleConstantValue(CV);
 }
 
 // Print a constant (which may be an aggregate) prefixed by all the
@@ -720,11 +839,11 @@ SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
   toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
   
   // Print .size and .type only if it is not a string.
-  const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
-  if (CPA && isStringCompatible(CPA))
+  const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
+  if (CVA && isStringCompatible(CVA))
     { // print it as a string and return
       toAsm << valID << ":\n";
-      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
+      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
       return;
     }
   
@@ -741,27 +860,28 @@ SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
 
 
 void SparcModuleAsmPrinter::FoldConstants(const Module &M,
-                                          std::hash_set<const Constant*> &MC) {
+                                          hash_set<const Constant*> &MC) {
   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
     if (!I->isExternal()) {
-      const std::hash_set<const Constant*> &pool =
-        MachineCodeForMethod::get(I).getConstantPoolValues();
+      const hash_set<const Constant*> &pool =
+        MachineFunction::get(I).getConstantPoolValues();
       MC.insert(pool.begin(), pool.end());
     }
 }
 
 void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
 {
-  toAsm << "\t.global\t" << getID(GV) << "\n";
+  if (GV->hasExternalLinkage())
+    toAsm << "\t.global\t" << getID(GV) << "\n";
   
-  if (GV->hasInitializer())
+  if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
     printConstant(GV->getInitializer(), getID(GV));
   else {
     toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
                                                 Target) << "\n";
     toAsm << "\t.type\t" << getID(GV) << ",#object\n";
     toAsm << "\t.reserve\t" << getID(GV) << ","
-          << Target.findOptimalStorageSize(GV->getType()->getElementType())
+          << TypeToSize(GV->getType()->getElementType(), Target)
           << "\n";
   }
 }
@@ -774,40 +894,34 @@ void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
   // lets force these constants into the slot table so that we can get
   // unique names for unnamed constants also.
   // 
-  std::hash_set<const Constant*> moduleConstants;
+  hash_set<const Constant*> moduleConstants;
   FoldConstants(M, moduleConstants);
     
-  // Now, emit the three data sections separately; the cost of I/O should
-  // make up for the cost of extra passes over the globals list!
-  
-  // Section 1 : Read-only data section (implies initialized)
+  // Output constants spilled to memory
   enterSection(AsmPrinter::ReadOnlyData);
-  for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
-    if (GI->hasInitializer() && GI->isConstant())
-      printGlobalVariable(GI);
-  
-  for (std::hash_set<const Constant*>::const_iterator
-         I = moduleConstants.begin(),
+  for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
          E = moduleConstants.end();  I != E; ++I)
     printConstant(*I);
-  
-  // Section 2 : Initialized read-write data section
-  enterSection(AsmPrinter::InitRWData);
-  for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
-    if (GI->hasInitializer() && !GI->isConstant())
-      printGlobalVariable(GI);
-  
-  // Section 3 : Uninitialized read-write data section
-  enterSection(AsmPrinter::UninitRWData);
+
+  // Output global variables...
   for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
-    if (!GI->hasInitializer())
+    if (! GI->isExternal()) {
+      assert(GI->hasInitializer());
+      if (GI->isConstant())
+        enterSection(AsmPrinter::ReadOnlyData);   // read-only, initialized data
+      else if (GI->getInitializer()->isNullValue())
+        enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
+      else
+        enterSection(AsmPrinter::InitRWData);     // read-write non-zero data
+
       printGlobalVariable(GI);
-  
+    }
+
   toAsm << "\n";
 }
 
 }  // End anonymous namespace
 
-Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
+Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
   return new SparcModuleAsmPrinter(Out, *this);
 }