Eliminate MachineFunction& argument from eliminateFrameIndex in x86 Target. Get...
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9AsmPrinter.cpp
index e87f6a2215a8c6c637fb01b9e2571d73323156f7..0be404d65393876a37bea30b14567e8aab7e882f 100644 (file)
@@ -1,4 +1,4 @@
-//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
+//===-- EmitAssembly.cpp - Emit SparcV9 Specific .s File -------------------==//
 // 
 //                     The LLVM Compiler Infrastructure
 //
@@ -30,7 +30,7 @@
 #include "llvm/Support/Mangler.h"
 #include "Support/StringExtras.h"
 #include "Support/Statistic.h"
-#include "SparcInternals.h"
+#include "SparcV9Internals.h"
 #include <string>
 using namespace llvm;
 
@@ -74,10 +74,18 @@ namespace {
             arrayType->getElementType() == Type::SByteTy);
   }
 
+  unsigned findOptimalStorageSize(const TargetMachine &TM, const Type *Ty) {
+    // All integer types smaller than ints promote to 4 byte integers.
+    if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4)
+      return 4;
+
+    return TM.getTargetData().getTypeSize(Ty);
+  }
+
+
   inline const std::string
   TypeToDataDirective(const Type* type) {
-    switch(type->getPrimitiveID())
-    {
+    switch(type->getTypeID()) {
     case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
       return ".byte";
     case Type::UShortTyID: case Type::ShortTyID:
@@ -111,7 +119,7 @@ namespace {
         return 1 + CVA->getNumOperands();
     }
   
-    return target.findOptimalStorageSize(CV->getType());
+    return findOptimalStorageSize(target, CV->getType());
   }
 
   /// Align data larger than one L1 cache line on L1 cache line boundaries.
@@ -119,7 +127,7 @@ namespace {
   /// 
   inline unsigned int
   SizeToAlignment(unsigned int size, const TargetMachine& target) {
-    unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); 
+    const unsigned short cacheLineSize = 16;
     if (size > (unsigned) cacheLineSize / 2)
       return cacheLineSize;
     else
@@ -132,7 +140,7 @@ namespace {
   /// 
   inline unsigned int
   TypeToAlignment(const Type* type, const TargetMachine& target) {
-    return SizeToAlignment(target.findOptimalStorageSize(type), target);
+    return SizeToAlignment(findOptimalStorageSize(target, type), target);
   }
 
   /// Get the size of the constant and then use SizeToAlignment.
@@ -251,7 +259,7 @@ namespace {
     }
 
     // getID Wrappers - Ensure consistent usage
-    // Symbol names in Sparc assembly language have these rules:
+    // Symbol names in SparcV9 assembly language have these rules:
     // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
     // (b) A name beginning in "." is treated as a local name.
     std::string getID(const Function *F) {
@@ -301,7 +309,6 @@ namespace {
 ///
 void AsmPrinter::printSingleConstantValue(const Constant* CV) {
   assert(CV->getType() != Type::VoidTy &&
-         CV->getType() != Type::TypeTy &&
          CV->getType() != Type::LabelTy &&
          "Unexpected type for Constant");
   
@@ -310,11 +317,8 @@ void AsmPrinter::printSingleConstantValue(const Constant* CV) {
   
   toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
   
-  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";
+  if (const GlobalValue* GV = dyn_cast<GlobalValue>(CV)) {
+    toAsm << getID(GV) << "\n";
   } else if (isa<ConstantPointerNull>(CV)) {
     // Null pointer value
     toAsm << "0\n";
@@ -361,23 +365,21 @@ void AsmPrinter::printConstantValueOnly(const Constant* CV,
       toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
     } else {
       // Not a string.  Print the values in successive locations
-      const std::vector<Use> &constValues = CVA->getValues();
-      for (unsigned i=0; i < constValues.size(); i++)
-        printConstantValueOnly(cast<Constant>(constValues[i].get()));
+      for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
+        printConstantValueOnly(CVA->getOperand(i));
     }
   } 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());
+    for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
+      const Constant* field = CVS->getOperand(i);
 
       // 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
+      int padSize = ((i == e-1? cvsLayout->StructSize
                       : cvsLayout->MemberOffsets[i+1])
                      - cvsLayout->MemberOffsets[i]) - fieldSize;
       sizeSoFar += (fieldSize + padSize);
@@ -387,8 +389,9 @@ void AsmPrinter::printConstantValueOnly(const Constant* CV,
     }
     assert(sizeSoFar == cvsLayout->StructSize &&
            "Layout of constant struct may be incorrect!");
-  }
-  else
+  } else if (isa<ConstantAggregateZero>(CV)) {
+    PrintZeroBytesToPad(Target.getTargetData().getTypeSize(CV->getType()));
+  } else
     printSingleConstantValue(CV);
 
   if (numPadBytesAfter)
@@ -472,7 +475,9 @@ std::string AsmPrinter::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 GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
+    S += getID(GV);
+  } else 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))
@@ -483,14 +488,10 @@ std::string AsmPrinter::valToExprString(const Value* V,
       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;
 
@@ -503,19 +504,19 @@ std::string AsmPrinter::valToExprString(const Value* V,
 
 
 //===----------------------------------------------------------------------===//
-//   SparcAsmPrinter Code
+//   SparcV9AsmPrinter Code
 //===----------------------------------------------------------------------===//
 
 namespace {
 
-  struct SparcAsmPrinter : public FunctionPass, public AsmPrinter {
-    inline SparcAsmPrinter(std::ostream &os, const TargetMachine &t)
+  struct SparcV9AsmPrinter : public FunctionPass, public AsmPrinter {
+    inline SparcV9AsmPrinter(std::ostream &os, const TargetMachine &t)
       : AsmPrinter(os, t) {}
 
     const Function *currFunction;
 
     const char *getPassName() const {
-      return "Output Sparc Assembly for Functions";
+      return "Output SparcV9 Assembly for Functions";
     }
 
     virtual bool doInitialization(Module &M) {
@@ -564,7 +565,7 @@ namespace {
 } // End anonymous namespace
 
 inline bool
-SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
+SparcV9AsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
                                        unsigned int opNum) {
   switch (MI->getOpcode()) {
   case V9::JMPLCALLr:
@@ -578,11 +579,11 @@ SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
 }
 
 inline bool
-SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
+SparcV9AsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
                                        unsigned int opNum) {
-  if (Target.getInstrInfo().isLoad(MI->getOpcode()))
+  if (Target.getInstrInfo()->isLoad(MI->getOpcode()))
     return (opNum == 0);
-  else if (Target.getInstrInfo().isStore(MI->getOpcode()))
+  else if (Target.getInstrInfo()->isStore(MI->getOpcode()))
     return (opNum == 1);
   else
     return false;
@@ -595,7 +596,7 @@ SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
   printOneOperand(mop2, opCode);
 
 unsigned int
-SparcAsmPrinter::printOperands(const MachineInstr *MI,
+SparcV9AsmPrinter::printOperands(const MachineInstr *MI,
                                unsigned int opNum)
 {
   const MachineOperand& mop = MI->getOperand(opNum);
@@ -615,7 +616,7 @@ SparcAsmPrinter::printOperands(const MachineInstr *MI,
 }
 
 void
-SparcAsmPrinter::printOneOperand(const MachineOperand &mop,
+SparcV9AsmPrinter::printOneOperand(const MachineOperand &mop,
                                  MachineOpCode opCode)
 {
   bool needBitsFlag = true;
@@ -639,18 +640,18 @@ SparcAsmPrinter::printOneOperand(const MachineOperand &mop,
       {
         int regNum = (int)mop.getReg();
         
-        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_ConstantPoolIndex:
       {
-        toAsm << ".CPI_" << currFunction->getName() 
+        toAsm << ".CPI_" << getID(currFunction)
               << "_" << mop.getConstantPoolIndex();
         break;
       }
@@ -658,18 +659,18 @@ SparcAsmPrinter::printOneOperand(const MachineOperand &mop,
     case MachineOperand::MO_PCRelativeDisp:
       {
         const Value *Val = mop.getVRegValue();
-        assert(Val && "\tNULL Value in SparcAsmPrinter");
+        assert(Val && "\tNULL Value in SparcV9AsmPrinter");
         
         if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
           toAsm << getID(BB);
-        else if (const Function *M = dyn_cast<Function>(Val))
-          toAsm << getID(M);
+        else if (const Function *F = dyn_cast<Function>(Val))
+          toAsm << getID(F);
         else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
           toAsm << getID(GV);
         else if (const Constant *CV = dyn_cast<Constant>(Val))
           toAsm << getID(CV);
         else
-          assert(0 && "Unrecognized value in SparcAsmPrinter");
+          assert(0 && "Unrecognized value in SparcV9AsmPrinter");
         break;
       }
     
@@ -690,13 +691,13 @@ SparcAsmPrinter::printOneOperand(const MachineOperand &mop,
     toAsm << ")";
 }
 
-void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
+void SparcV9AsmPrinter::emitMachineInst(const MachineInstr *MI) {
   unsigned Opcode = MI->getOpcode();
 
-  if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
+  if (Target.getInstrInfo()->isDummyPhiInstr(Opcode))
     return;  // IGNORE PHI NODES
 
-  toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
+  toAsm << "\t" << Target.getInstrInfo()->getName(Opcode) << "\t";
 
   unsigned Mask = getOperandMask(Opcode);
   
@@ -714,7 +715,7 @@ void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
   ++EmittedInsts;
 }
 
-void SparcAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
+void SparcV9AsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
   // Emit a label for the basic block
   toAsm << getID(MBB.getBasicBlock()) << ":\n";
 
@@ -725,7 +726,7 @@ void SparcAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
   toAsm << "\n";  // Separate BB's with newlines
 }
 
-void SparcAsmPrinter::emitFunction(const Function &F) {
+void SparcV9AsmPrinter::emitFunction(const Function &F) {
   std::string methName = getID(&F);
   toAsm << "!****** Outputing Function: " << methName << " ******\n";
 
@@ -735,7 +736,7 @@ void SparcAsmPrinter::emitFunction(const Function &F) {
 
   enterSection(AsmPrinter::ReadOnlyData);
   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
-    std::string cpiName = ".CPI_" + F.getName() + "_" + utostr(i);
+    std::string cpiName = ".CPI_" + methName + "_" + utostr(i);
     printConstant(CP[i], cpiName);
   }
 
@@ -759,7 +760,7 @@ void SparcAsmPrinter::emitFunction(const Function &F) {
   toAsm << "\n\n";
 }
 
-void SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
+void SparcV9AsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
   if (GV->hasExternalLinkage())
     toAsm << "\t.global\t" << getID(GV) << "\n";
   
@@ -770,12 +771,12 @@ void SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
                                                 Target) << "\n";
     toAsm << "\t.type\t" << getID(GV) << ",#object\n";
     toAsm << "\t.reserve\t" << getID(GV) << ","
-          << Target.findOptimalStorageSize(GV->getType()->getElementType())
+          << findOptimalStorageSize(Target, GV->getType()->getElementType())
           << "\n";
   }
 }
 
-void SparcAsmPrinter::emitGlobals(const Module &M) {
+void SparcV9AsmPrinter::emitGlobals(const Module &M) {
   // Output global variables...
   for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
     if (! GI->isExternal()) {
@@ -795,5 +796,5 @@ void SparcAsmPrinter::emitGlobals(const Module &M) {
 
 FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out,
                                          const TargetMachine &TM) {
-  return new SparcAsmPrinter(Out, TM);
+  return new SparcV9AsmPrinter(Out, TM);
 }