* Removed `using' declaration, now use full namespace qualifier std::string
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9AsmPrinter.cpp
index 618fb6dc30f6411b3b3d08ab2f9558444f997d1a..13b3c38c97b7cbb3bd54d6533f4a6d48e94eaf6e 100644 (file)
@@ -4,70 +4,90 @@
 // LLVM.  The code in this file assumes that the specified module has already
 // been compiled into the internal data structures of the Module.
 //
-// The entry point of this file is the UltraSparc::emitAssembly method.
+// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
+// The FunctionPass is pipelined together with all of the rest of the code
+// generation stages, and the Pass runs at the end to emit code for global
+// variables and such.
 //
 //===----------------------------------------------------------------------===//
 
-#include "SparcInternals.h"
-#include "llvm/Analysis/SlotCalculator.h"
-#include "llvm/Transforms/Linker.h"
 #include "llvm/CodeGen/MachineInstr.h"
-#include "llvm/GlobalVariable.h"
-#include "llvm/GlobalValue.h"
-#include "llvm/ConstPoolVals.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionInfo.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Method.h"
 #include "llvm/Module.h"
-#include "llvm/Support/HashExtras.h"
-#include "llvm/Support/StringExtras.h"
-#include <locale.h>
+#include "llvm/SlotCalculator.h"
+#include "llvm/Pass.h"
+#include "llvm/Assembly/Writer.h"
+#include "Support/StringExtras.h"
+#include "SparcInternals.h"
+#include <string>
 
 namespace {
 
-
-class SparcAsmPrinter {
+class GlobalIdTable: public Annotation {
+  static AnnotationID AnnotId;
+  friend class AsmPrinter;              // give access to AnnotId
+  
   typedef hash_map<const Value*, int> ValIdMap;
-  typedef ValIdMap::      iterator ValIdMapIterator;
   typedef ValIdMap::const_iterator ValIdMapConstIterator;
+  typedef ValIdMap::      iterator ValIdMapIterator;
+public:
+  SlotCalculator Table;    // map anonymous values to unique integer IDs
+  ValIdMap valToIdMap;     // used for values not handled by SlotCalculator 
   
-  ostream &toAsm;
-  SlotCalculator Table;   // map anonymous values to unique integer IDs
-  ValIdMap valToIdMap;    // used for values not handled by SlotCalculator 
-  const UltraSparc &Target;
+  GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
+};
+
+AnnotationID GlobalIdTable::AnnotId =
+  AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
+  
+//===---------------------------------------------------------------------===//
+//   Code Shared By the two printer passes, as a mixin
+//===---------------------------------------------------------------------===//
+
+class AsmPrinter {
+  GlobalIdTable* idTable;
+public:
+  std::ostream &toAsm;
+  const TargetMachine &Target;
   
   enum Sections {
     Unknown,
     Text,
     ReadOnlyData,
     InitRWData,
-    UninitRWData,
+    ZeroInitRWData,
   } CurSection;
-  
-public:
-  inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
-    : toAsm(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
-    emitModule(M);
-  }
 
-private :
-  void emitModule(const Module *M);
-  void emitMethod(const Method *M);
-  void emitGlobalsAndConstants(const Module* module);
-  //void processMethodArgument(const MethodArgument *MA);
-  void emitBasicBlock(const BasicBlock *BB);
-  void emitMachineInst(const MachineInstr *MI);
+  AsmPrinter(std::ostream &os, const TargetMachine &T)
+    : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
   
-  void printGlobalVariable(   const GlobalVariable* GV);
-  void printSingleConstant(   const ConstPoolVal* CV);
-  void printConstantValueOnly(const ConstPoolVal* CV);
-  void printConstant(         const ConstPoolVal* CV, string valID=string(""));
-  
-  unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
-  void printOneOperand(const MachineOperand &Op);
+  // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
+  void startModule(Module &M) {
+    // Create the global id table if it does not already exist
+    idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
+    if (idTable == NULL) {
+      idTable = new GlobalIdTable(&M);
+      M.addAnnotation(idTable);
+    }
+  }
+  void startFunction(Function &F) {
+    // Make sure the slot table has information about this function...
+    idTable->Table.incorporateFunction(&F);
+  }
+  void endFunction(Function &) {
+    idTable->Table.purgeFunction();  // Forget all about F
+  }
+  void endModule() {
+  }
 
-  bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
-  bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
+  // Check if a value is external or accessible from external code.
+  bool isExternal(const Value* V) {
+    const GlobalValue *GV = dyn_cast<GlobalValue>(V);
+    return GV && GV->hasExternalLinkage();
+  }
   
   // enterSection - Use this method to enter a different section of the output
   // executable.  This is used to only output neccesary section transitions.
@@ -83,21 +103,20 @@ private :
       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";
   }
 
-  string getValidSymbolName(const string &S) {
-    string Result;
+  static std::string getValidSymbolName(const std::string &S) {
+    std::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)
       {
@@ -115,66 +134,247 @@ private :
   }
 
   // getID - Return a valid identifier for the specified value.  Base it on
-  // the name of the identifier if possible, use a numbered value based on
-  // prefix otherwise.  FPrefix is always prepended to the output identifier.
+  // the name of the identifier if possible (qualified by the type), and
+  // use a numbered value based on prefix otherwise.
+  // FPrefix is always prepended to the output identifier.
   //
-  string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
-    string Result;
-    string FP(FPrefix ? FPrefix : "");  // "Forced prefix"
-    if (V->hasName()) {
-      Result = FP + V->getName();
-    } else {
-      int valId = Table.getValSlot(V);
+  std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
+    std::string Result = FPrefix ? FPrefix : "";  // "Forced prefix"
+
+    Result += V->hasName() ? V->getName() : std::string(Prefix);
+
+    // Qualify all internal names with a unique id.
+    if (!isExternal(V)) {
+      int valId = idTable->Table.getValSlot(V);
       if (valId == -1) {
-        ValIdMapConstIterator I = valToIdMap.find(V);
-        valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
-                                       : (*I).second;
+        GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
+        if (I == idTable->valToIdMap.end())
+          valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
+        else
+          valId = I->second;
       }
-      Result = FP + string(Prefix) + itostr(valId);
+      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...
-  string getID(const Module *M) {
-    return getID(M, "LLVMModule_");
+  std::string getID(const Function *F) {
+    return getID(F, "LLVMFunction_");
   }
-  string getID(const Method *M) {
-    return getID(M, "LLVMMethod_");
-  }
-  string getID(const BasicBlock *BB) {
+  std::string getID(const BasicBlock *BB) {
     return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
   }
-  string getID(const GlobalVariable *GV) {
-    return getID(GV, "LLVMGlobal_", ".G_");
+  std::string getID(const GlobalVariable *GV) {
+    return getID(GV, "LLVMGlobal_");
   }
-  string getID(const ConstPoolVal *CV) {
+  std::string getID(const Constant *CV) {
     return getID(CV, "LLVMConst_", ".C_");
   }
+  std::string getID(const GlobalValue *GV) {
+    if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
+      return getID(V);
+    else if (const Function *F = dyn_cast<Function>(GV))
+      return getID(F);
+    assert(0 && "Unexpected type of GlobalValue!");
+    return "";
+  }
+
+  // Combines expressions 
+  inline std::string ConstantArithExprToString(const ConstantExpr* CE,
+                                               const TargetMachine &TM,
+                                               const std::string &op) {
+    return "(" + valToExprString(CE->getOperand(0), TM) + op
+               + valToExprString(CE->getOperand(1), TM) + ")";
+  }
+
+  // ConstantExprToString() - Convert a ConstantExpr to an asm expression
+  // and return this as a string.
+  std::string ConstantExprToString(const ConstantExpr* CE,
+                                   const TargetMachine& target) {
+    std::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());
+        const TargetData &TD = target.getTargetData();
+        S += "(" + valToExprString(ptrVal, target) + ") + ("
+          + utostr(TD.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.getTargetData().getTypeSize(CE->getType()) ==
+             target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
+      S += "(" + valToExprString(CE->getOperand(0), target) + ")";
+      break;
+
+    case Instruction::Add:
+      S += ConstantArithExprToString(CE, target, ") + (");
+      break;
+
+    case Instruction::Sub:
+      S += ConstantArithExprToString(CE, target, ") - (");
+      break;
+
+    case Instruction::Mul:
+      S += ConstantArithExprToString(CE, target, ") * (");
+      break;
+
+    case Instruction::Div:
+      S += ConstantArithExprToString(CE, target, ") / (");
+      break;
+
+    case Instruction::Rem:
+      S += ConstantArithExprToString(CE, target, ") % (");
+      break;
+
+    case Instruction::And:
+      // Logical && for booleans; bitwise & otherwise
+      S += ConstantArithExprToString(CE, target,
+               ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
+      break;
+
+    case Instruction::Or:
+      // Logical || for booleans; bitwise | otherwise
+      S += ConstantArithExprToString(CE, target,
+               ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
+      break;
+
+    case Instruction::Xor:
+      // Bitwise ^ for all types
+      S += ConstantArithExprToString(CE, target, ") ^ (");
+      break;
+
+    default:
+      assert(0 && "Unsupported operator in ConstantExprToString()");
+      break;
+    }
+
+    return S;
+  }
+
+  // valToExprString - Helper function for ConstantExprToString().
+  // Appends result to argument string S.
+  // 
+  std::string valToExprString(const Value* V, const TargetMachine& target) {
+    std::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 += std::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;
+  }
+
+};
+
+
+
+//===----------------------------------------------------------------------===//
+//   SparcFunctionAsmPrinter Code
+//===----------------------------------------------------------------------===//
+
+struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
+  inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
+    : AsmPrinter(os, t) {}
+
+  const char *getPassName() const {
+    return "Output Sparc Assembly for Functions";
+  }
+
+  virtual bool doInitialization(Module &M) {
+    startModule(M);
+    return false;
+  }
+
+  virtual bool runOnFunction(Function &F) {
+    startFunction(F);
+    emitFunction(F);
+    endFunction(F);
+    return false;
+  }
+
+  virtual bool doFinalization(Module &M) {
+    endModule();
+    return false;
+  }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+  }
+
+  void emitFunction(const Function &F);
+private :
+  void emitBasicBlock(const MachineBasicBlock &MBB);
+  void emitMachineInst(const MachineInstr *MI);
+  
+  unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
+  void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
+
+  bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
+  bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
   
   unsigned getOperandMask(unsigned Opcode) {
     switch (Opcode) {
-    case SUBcc:   return 1 << 3;  // Remove CC argument
-    case BA:      return 1 << 0;  // Remove Arg #0, which is always null or xcc
+    case V9::SUBccr:
+    case V9::SUBcci:   return 1 << 3;  // Remove CC argument
+  //case BA:      return 1 << 0;  // Remove Arg #0, which is always null or xcc
     default:      return 0;       // By default, don't hack operands...
     }
   }
 };
 
 inline bool
-SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
-                                       unsigned int opNum) {
+SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
+                                               unsigned int opNum) {
   switch (MI->getOpCode()) {
-  case JMPLCALL:
-  case JMPLRET: return (opNum == 0);
-  default:      return false;
+  case V9::JMPLCALLr:
+  case V9::JMPLCALLi:
+  case V9::JMPLRETr:
+  case V9::JMPLRETi:
+    return (opNum == 0);
+  default:
+    return false;
   }
 }
 
 
 inline bool
-SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
-                                       unsigned int opNum) {
+SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
+                                               unsigned int opNum) {
   if (Target.getInstrInfo().isLoad(MI->getOpCode()))
     return (opNum == 0);
   else if (Target.getInstrInfo().isStore(MI->getOpCode()))
@@ -184,96 +384,115 @@ SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
 }
 
 
-#define PrintOp1PlusOp2(Op1, Op2) \
-  printOneOperand(Op1); \
+#define PrintOp1PlusOp2(mop1, mop2, opCode) \
+  printOneOperand(mop1, opCode); \
   toAsm << "+"; \
-  printOneOperand(Op2);
+  printOneOperand(mop2, opCode);
 
 unsigned int
-SparcAsmPrinter::printOperands(const MachineInstr *MI,
+SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
                                unsigned int opNum)
 {
-  const MachineOperand& Op = MI->getOperand(opNum);
+  const MachineOperand& mop = MI->getOperand(opNum);
   
   if (OpIsBranchTargetLabel(MI, opNum))
     {
-      PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
+      PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
       return 2;
     }
   else if (OpIsMemoryAddressBase(MI, opNum))
     {
       toAsm << "[";
-      PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
+      PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
       toAsm << "]";
       return 2;
     }
   else
     {
-      printOneOperand(Op);
+      printOneOperand(mop, MI->getOpCode());
       return 1;
     }
 }
 
-
 void
-SparcAsmPrinter::printOneOperand(const MachineOperand &op)
+SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
+                                         MachineOpCode opCode)
 {
-  switch (op.getOperandType())
+  bool needBitsFlag = true;
+  
+  if (mop.opHiBits32())
+    toAsm << "%lm(";
+  else if (mop.opLoBits32())
+    toAsm << "%lo(";
+  else if (mop.opHiBits64())
+    toAsm << "%hh(";
+  else if (mop.opLoBits64())
+    toAsm << "%hm(";
+  else
+    needBitsFlag = false;
+  
+  switch (mop.getType())
     {
     case MachineOperand::MO_VirtualRegister:
     case MachineOperand::MO_CCRegister:
     case MachineOperand::MO_MachineRegister:
       {
-        int RegNum = (int)op.getAllocatedRegNum();
+        int regNum = (int)mop.getAllocatedRegNum();
         
-        // ****this code is temporary till NULL Values are fixed
-        if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
+        if (regNum == Target.getRegInfo().getInvalidRegNum()) {
+          // better to print code with NULL registers than to die
           toAsm << "<NULL VALUE>";
         } else {
-          toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
+          toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
         }
         break;
       }
     
     case MachineOperand::MO_PCRelativeDisp:
       {
-        const Value *Val = op.getVRegValue();
-        if (!Val)
-          toAsm << "\t<*NULL Value*>";
-        else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
+        const Value *Val = mop.getVRegValue();
+        assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
+        
+        if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
           toAsm << getID(BB);
-        else if (const Method *M = dyn_cast<const Method>(Val))
+        else if (const Function *M = dyn_cast<Function>(Val))
           toAsm << getID(M);
-        else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
+        else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
           toAsm << getID(GV);
-        else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
+        else if (const Constant *CV = dyn_cast<Constant>(Val))
           toAsm << getID(CV);
         else
-          toAsm << "<unknown value=" << Val << ">";
+          assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
         break;
       }
     
     case MachineOperand::MO_SignExtendedImmed:
+      toAsm << mop.getImmedValue();
+      break;
+
     case MachineOperand::MO_UnextendedImmed:
-      toAsm << op.getImmedValue();
+      toAsm << (uint64_t) mop.getImmedValue();
       break;
     
     default:
-      toAsm << op;      // use dump field
+      toAsm << mop;      // use dump field
       break;
     }
+  
+  if (needBitsFlag)
+    toAsm << ")";
 }
 
 
 void
-SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
+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);
   
@@ -284,69 +503,157 @@ SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
       if (NeedComma) toAsm << ", ";         // Handle comma outputing
       NeedComma = true;
       N = printOperands(MI, OpNum);
-    }
-  else
-    N = 1;
+    } else
+      N = 1;
   
-  toAsm << endl;
+  toAsm << "\n";
 }
 
 void
-SparcAsmPrinter::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 = BB->getMachineInstrVec();
-  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
+  toAsm << "\n";  // Separate BB's with newlines
 }
 
 void
-SparcAsmPrinter::emitMethod(const Method *M)
+SparcFunctionAsmPrinter::emitFunction(const Function &F)
 {
-  if (M->isExternal()) return;
-
-  // Make sure the slot table has information about this method...
-  Table.incorporateMethod(M);
-
-  string methName = getID(M);
-  toAsm << "!****** Outputing Method: " << methName << " ******\n";
-  enterSection(Text);
+  std::string methName = getID(&F);
+  toAsm << "!****** Outputing Function: " << methName << " ******\n";
+  enterSection(AsmPrinter::Text);
   toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
   //toAsm << "\t.type\t" << methName << ",#function\n";
   toAsm << "\t.type\t" << methName << ", 2\n";
   toAsm << methName << ":\n";
 
-  // Output code for all of the basic blocks in the method...
-  for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
+  // Output code for all of the basic blocks in the function...
+  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 "
-        << methName << ", .EndOf_"
-        << methName << "-" << methName << endl;
+           << methName << ", .EndOf_"
+           << methName << "-" << methName << "\n";
 
-  // Put some spaces between the methods
+  // Put some spaces between the functions
   toAsm << "\n\n";
+}
+
+}  // End anonymous namespace
+
+Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
+  return new SparcFunctionAsmPrinter(Out, *this);
+}
+
+
+
+
+
+//===----------------------------------------------------------------------===//
+//   SparcFunctionAsmPrinter Code
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
+public:
+  SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
+    : AsmPrinter(os, t) {}
+
+  const char *getPassName() const { return "Output Sparc Assembly for Module"; }
+
+  virtual bool run(Module &M) {
+    startModule(M);
+    emitGlobalsAndConstants(M);
+    endModule();
+    return false;
+  }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+  }
+
+private:
+  void emitGlobalsAndConstants  (const Module &M);
+
+  void printGlobalVariable      (const GlobalVariable *GV);
+  void PrintZeroBytesToPad      (int numBytes);
+  void printSingleConstantValue (const Constant* CV);
+  void printConstantValueOnly   (const Constant* CV, int numPadBytesAfter = 0);
+  void printConstant            (const Constant* CV, std::string valID = "");
+
+  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 *CVA) {
+  const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
+  if (ETy == Type::UByteTy) return true;
+  if (ETy != Type::SByteTy) return false;
 
-  // Forget all about M.
-  Table.purgeMethod();
+  for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
+    if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
+      return false;
+
+  return true;
+}
+
+// toOctal - Convert the low order bits of X into an octal letter
+static inline char toOctal(int X) {
+  return (X&7)+'0';
+}
+
+// getAsCString - Return the specified array as a C compatible string, only if
+// the predicate isStringCompatible is true.
+//
+static std::string getAsCString(const ConstantArray *CVA) {
+  assert(isStringCompatible(CVA) && "Array is not string compatible!");
+
+  std::string Result;
+  const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
+  Result = "\"";
+  for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
+    unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
+
+    if (C == '"') {
+      Result += "\\\"";
+    } else if (C == '\\') {
+      Result += "\\\\";
+    } else if (isprint(C)) {
+      Result += C;
+    } else {
+      Result += '\\';                   // print all other chars as octal value
+      Result += toOctal(C >> 6);
+      Result += toOctal(C >> 3);
+      Result += toOctal(C >> 0);
+    }
+  }
+  Result += "\"";
+
+  return Result;
 }
 
 inline bool
-ArrayTypeIsString(ArrayType* arrayType)
+ArrayTypeIsString(const ArrayType* arrayType)
 {
   return (arrayType->getElementType() == Type::UByteTy ||
           arrayType->getElementType() == Type::SByteTy);
 }
 
-inline const string
+
+inline const std::string
 TypeToDataDirective(const Type* type)
 {
   switch(type->getPrimitiveID())
@@ -360,9 +667,9 @@ TypeToDataDirective(const Type* type)
     case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
       return ".xword";
     case Type::FloatTyID:
-      return ".single";
+      return ".word";
     case Type::DoubleTyID:
-      return ".double";
+      return ".xword";
     case Type::ArrayTyID:
       if (ArrayTypeIsString((ArrayType*) type))
         return ".ascii";
@@ -373,31 +680,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 ConstPoolVal* CV, const TargetMachine& target)
+ConstantToSize(const Constant* CV, const TargetMachine& target)
 {
-  if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV))
+  if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
     {
-      ArrayType *aty = cast<ArrayType>(CPA->getType());
+      const ArrayType *aty = cast<ArrayType>(CVA->getType());
       if (ArrayTypeIsString(aty))
-        return 1 + CPA->getNumOperands();
-      else if (! aty->isSized())
-        return 0;
+        return 1 + CVA->getNumOperands();
     }
   
-  return target.findOptimalStorageSize(CV->getType());
-}
-
-inline
-unsigned int TypeToSize(const Type* type, const TargetMachine& target)
-{
-  return target.findOptimalStorageSize(type);
+  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, ...).
 // 
@@ -414,28 +720,21 @@ SizeToAlignment(unsigned int size, const TargetMachine& target)
 }
 
 // Get the size of the type and then use SizeToAlignment.
-// If this is an unsized array, just return the L1 cache line size
-// (viz., the default behavior for large global objects).
 // 
 inline unsigned int
 TypeToAlignment(const Type* type, const TargetMachine& target)
 {
-  if (ArrayType* aty = dyn_cast<ArrayType>(type))
-    if (! aty->isSized())
-      return target.getCacheInfo().getCacheLineSize(1);
-  
-  return SizeToAlignment(target.findOptimalStorageSize(type), target);
+  return SizeToAlignment(TypeToSize(type, target), target);
 }
 
 // Get the size of the constant and then use SizeToAlignment.
 // Handles strings as a special case;
 inline unsigned int
-ConstantToAlignment(const ConstPoolVal* CV, const TargetMachine& target)
+ConstantToAlignment(const Constant* CV, const TargetMachine& target)
 {
-  unsigned int constantSize;
-  if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(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);
 }
@@ -443,35 +742,54 @@ ConstantToAlignment(const ConstPoolVal* CV, const TargetMachine& target)
 
 // Print a single constant value.
 void
-SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV)
+SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
 {
   assert(CV->getType() != Type::VoidTy &&
          CV->getType() != Type::TypeTy &&
          CV->getType() != Type::LabelTy &&
-         "Unexpected type for ConstPoolVal");
+         "Unexpected type for Constant");
   
-  assert((! isa<ConstPoolArray>( CV) && ! isa<ConstPoolStruct>(CV))
-         && "Collective types should be handled outside this function");
+  assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
+         && "Aggregate types should be handled outside this function");
   
-  toAsm << "\t"
-        << TypeToDataDirective(CV->getType()) << "\t";
+  toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
   
-  if (CV->getType()->isPrimitiveType())
-    {
-      if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
-        toAsm << "0r";                  // FP constants must have this prefix
-      toAsm << CV->getStrValue() << endl;
+  if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
+    { // This is a constant address for a global variable or method.
+      // Use the name of the variable or method as the address value.
+      assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
+      toAsm << getID(CPR->getValue()) << "\n";
     }
-  else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV))
-    {
-      if (! CPP->isNullValue())
-        assert(0 && "Cannot yet print non-null pointer constants to assembly");
-      else
-        toAsm << (void*) NULL << endl;
+  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 if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV))
+  else if (CV->getType()->isPrimitiveType())     // Check primitive types last
     {
-      assert(0 && "Cannot yet initialize pointer refs in assembly");
+      if (CV->getType()->isFloatingPoint()) {
+        // FP Constants are printed as integer constants to avoid losing
+        // precision...
+        double Val = cast<ConstantFP>(CV)->getValue();
+        if (CV->getType() == Type::FloatTy) {
+          float FVal = (float)Val;
+          char *ProxyPtr = (char*)&FVal;        // Abide by C TBAA rules
+          toAsm << *(unsigned int*)ProxyPtr;            
+        } else if (CV->getType() == Type::DoubleTy) {
+          char *ProxyPtr = (char*)&Val;         // Abide by C TBAA rules
+          toAsm << *(uint64_t*)ProxyPtr;            
+        } else {
+          assert(0 && "Unknown floating point type!");
+        }
+        
+        toAsm << "\t! " << CV->getType()->getDescription()
+              << " value: " << Val << "\n";
+      } else {
+        WriteAsOperand(toAsm, CV, false, false) << "\n";
+      }
     }
   else
     {
@@ -479,178 +797,166 @@ SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV)
     }
 }
 
+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
-SparcAsmPrinter::printConstantValueOnly(const ConstPoolVal* CV)
+SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
+                                              int numPadBytesAfter /* = 0*/)
 {
-  ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV);
-  
-  if (CPA && isStringCompatible(CPA))
+  const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
+
+  if (CVA && isStringCompatible(CVA))
     { // print the string alone and return
-      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
+      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
     }
-  else if (CPA)
+  else if (CVA)
     { // Not a string.  Print the values in successive locations
-      const vector<Use>& constValues = CPA->getValues();
-      for (unsigned i=1; i < constValues.size(); i++)
-        this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get()));
+      const std::vector<Use> &constValues = CVA->getValues();
+      for (unsigned i=0; i < constValues.size(); i++)
+        printConstantValueOnly(cast<Constant>(constValues[i].get()));
     }
-  else if (ConstPoolStruct *CPS = dyn_cast<ConstPoolStruct>(CV))
-    { // Print the fields in successive locations
-      const vector<Use>& constValues = CPS->getValues();
-      for (unsigned i=1; i < constValues.size(); i++)
-        this->printConstantValueOnly(cast<ConstPoolVal>(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.getTargetData().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.getTargetData().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
-    this->printSingleConstant(CV);
+    printSingleConstantValue(CV);
+
+  if (numPadBytesAfter)
+    PrintZeroBytesToPad(numPadBytesAfter);
 }
 
 // Print a constant (which may be an aggregate) prefixed by all the
 // appropriate directives.  Uses printConstantValueOnly() to print the
 // value or values.
 void
-SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
+SparcModuleAsmPrinter::printConstant(const Constant* CV, std::string valID)
 {
   if (valID.length() == 0)
     valID = getID(CV);
   
-  toAsm << "\t.align\t" << ConstantToAlignment(CV, Target)
-        << endl;
+  toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
   
   // Print .size and .type only if it is not a string.
-  ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(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 << ":" << endl;
-      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
+      toAsm << valID << ":\n";
+      toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
       return;
     }
   
-  toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
+  toAsm << "\t.type" << "\t" << valID << ",#object\n";
 
   unsigned int constSize = ConstantToSize(CV, Target);
   if (constSize)
-    toAsm << "\t.size" << "\t" << valID << ","
-          << constSize << endl;
+    toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
   
-  toAsm << valID << ":" << endl;
+  toAsm << valID << ":\n";
   
-  this->printConstantValueOnly(CV);
+  printConstantValueOnly(CV);
 }
 
 
-void
-SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
+void SparcModuleAsmPrinter::FoldConstants(const Module &M,
+                                          hash_set<const Constant*> &MC) {
+  for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
+    if (!I->isExternal()) {
+      const hash_set<const Constant*> &pool =
+        MachineFunction::get(I).getInfo()->getConstantPoolValues();
+      MC.insert(pool.begin(), pool.end());
+    }
+}
+
+void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
 {
-  toAsm << "\t.global\t" << getID(GV) << endl;
+  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()->getValueType(), Target) << endl;
-    toAsm << "\t.type\t" << getID(GV) << ",#object" << endl;
+    toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
+                                                Target) << "\n";
+    toAsm << "\t.type\t" << getID(GV) << ",#object\n";
     toAsm << "\t.reserve\t" << getID(GV) << ","
-          << TypeToSize(GV->getType()->getValueType(), Target)
-          << endl;
+          << TypeToSize(GV->getType()->getElementType(), Target)
+          << "\n";
   }
 }
 
 
-static void
-FoldConstPools(const Module *M,
-               hash_set<const ConstPoolVal*>& moduleConstPool)
-{
-  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
-    if (! (*I)->isExternal())
-      {
-        const hash_set<const ConstPoolVal*>& pool =
-          MachineCodeForMethod::get(*I).getConstantPoolValues();
-        moduleConstPool.insert(pool.begin(), pool.end());
-      }
-}
-
-
-void
-SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
-{
+void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
   // First, get the constants there were marked by the code generator for
   // inclusion in the assembly code data area and fold them all into a
   // single constant pool since there may be lots of duplicates.  Also,
   // lets force these constants into the slot table so that we can get
   // unique names for unnamed constants also.
   // 
-  hash_set<const ConstPoolVal*> moduleConstPool;
-  FoldConstPools(M, moduleConstPool);
-  
-  // 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!
-  // 
-  // Read-only data section (implies initialized)
-  for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
-    {
-      const GlobalVariable* GV = *GI;
-      if (GV->hasInitializer() && GV->isConstant())
-        {
-          if (GI == M->gbegin())
-            enterSection(ReadOnlyData);
-          printGlobalVariable(GV);
-        }
-  }
-  
-  for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
-         E = moduleConstPool.end();  I != E; ++I)
+  hash_set<const Constant*> moduleConstants;
+  FoldConstants(M, moduleConstants);
+    
+  // Output constants spilled to memory
+  enterSection(AsmPrinter::ReadOnlyData);
+  for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
+         E = moduleConstants.end();  I != E; ++I)
     printConstant(*I);
-  
-  // Initialized read-write data section
-  for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
-    {
-      const GlobalVariable* GV = *GI;
-      if (GV->hasInitializer() && ! GV->isConstant())
-        {
-          if (GI == M->gbegin())
-            enterSection(InitRWData);
-          printGlobalVariable(GV);
-        }
-  }
 
-  // Uninitialized read-write data section
-  for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
-    {
-      const GlobalVariable* GV = *GI;
-      if (! GV->hasInitializer())
-        {
-          if (GI == M->gbegin())
-            enterSection(UninitRWData);
-          printGlobalVariable(GV);
-        }
-  }
-
-  toAsm << endl;
-}
+  // Output global variables...
+  for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
+    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);
+    }
 
-void
-SparcAsmPrinter::emitModule(const Module *M)
-{
-  // TODO: Look for a filename annotation on M to emit a .file directive
-  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
-    emitMethod(*I);
-  
-  emitGlobalsAndConstants(M);
+  toAsm << "\n";
 }
 
 }  // End anonymous namespace
 
-
-//
-// emitAssembly - Output assembly language code (a .s file) for the specified
-// method. The specified method must have been compiled before this may be
-// used.
-//
-void
-UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
-{
-  SparcAsmPrinter Print(toAsm, M, *this);
+Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
+  return new SparcModuleAsmPrinter(Out, *this);
 }