Don't pass back a reference to a temporary.
[oota-llvm.git] / lib / CodeGen / AsmPrinter.cpp
index 2d7dec40f751b29bcea64643db52bfb118c89072..6622bcdee669e8ef605777b770713ce7b32e6633 100644 (file)
@@ -32,9 +32,10 @@ using namespace llvm;
 static cl::opt<bool>
 AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
 
+char AsmPrinter::ID = 0;
 AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
                        const TargetAsmInfo *T)
-: FunctionNumber(0), O(o), TM(tm), TAI(T)
+  : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
 {}
 
 std::string AsmPrinter::getSectionForFunction(const Function &F) const {
@@ -111,7 +112,7 @@ bool AsmPrinter::doInitialization(Module &M) {
 
 bool AsmPrinter::doFinalization(Module &M) {
   if (TAI->getWeakRefDirective()) {
-    if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
+    if (!ExtWeakSymbols.empty())
       SwitchToDataSection("");
 
     for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
@@ -122,10 +123,49 @@ bool AsmPrinter::doFinalization(Module &M) {
     }
   }
 
+  if (TAI->getSetDirective()) {
+    if (!M.alias_empty())
+      SwitchToTextSection(TAI->getTextSection());
+
+    O << "\n";
+    for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
+         I!=E; ++I) {
+      std::string Name = Mang->getValueName(I);
+      std::string Target;
+
+      const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
+      Target = Mang->getValueName(GV);
+      
+      if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
+        O << "\t.globl\t" << Name << "\n";
+      else if (I->hasWeakLinkage())
+        O << TAI->getWeakRefDirective() << Name << "\n";
+      else if (!I->hasInternalLinkage())
+        assert(0 && "Invalid alias linkage");
+      
+      O << TAI->getSetDirective() << Name << ", " << Target << "\n";
+
+      // If the aliasee has external weak linkage it can be referenced only by
+      // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
+      // weak reference in such case.
+      if (GV->hasExternalWeakLinkage())
+        if (TAI->getWeakRefDirective())
+          O << TAI->getWeakRefDirective() << Target << "\n";
+        else
+          O << "\t.globl\t" << Target << "\n";
+    }
+  }
+
   delete Mang; Mang = 0;
   return false;
 }
 
+std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
+  assert(MF && "No machine function?");
+  return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
+                              TAI->getGlobalPrefix());
+}
+
 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
   // What's my mangled name?
   CurrentFnName = Mang->getValueName(MF.getFunction());
@@ -287,6 +327,12 @@ void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
 /// special global used by LLVM.  If so, emit it and return true, otherwise
 /// do nothing and return false.
 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
+  if (GV->getName() == "llvm.used") {
+    if (TAI->getUsedDirective() != 0)    // No need to emit this at all.
+      EmitLLVMUsedList(GV->getInitializer());
+    return true;
+  }
+
   // Ignore debug and non-emitted data.
   if (GV->getSection() == "llvm.metadata") return true;
   
@@ -294,22 +340,18 @@ bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
 
   assert(GV->hasInitializer() && "Not a special LLVM global!");
   
-  if (GV->getName() == "llvm.used") {
-    if (TAI->getUsedDirective() != 0)    // No need to emit this at all.
-      EmitLLVMUsedList(GV->getInitializer());
-    return true;
-  }
-
+  const TargetData *TD = TM.getTargetData();
+  unsigned Align = Log2_32(TD->getPointerPrefAlignment());
   if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
     SwitchToDataSection(TAI->getStaticCtorsSection());
-    EmitAlignment(2, 0);
+    EmitAlignment(Align, 0);
     EmitXXStructorList(GV->getInitializer());
     return true;
   } 
   
   if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
     SwitchToDataSection(TAI->getStaticDtorsSection());
-    EmitAlignment(2, 0);
+    EmitAlignment(Align, 0);
     EmitXXStructorList(GV->getInitializer());
     return true;
   }
@@ -370,6 +412,14 @@ const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
   return LinkName;
 }
 
+/// EmitExternalGlobal - Emit the external reference to a global variable.
+/// Should be overridden if an indirect reference should be used.
+void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
+  O << getGlobalLinkName(GV);
+}
+
+
+
 //===----------------------------------------------------------------------===//
 /// LEB 128 number encoding.
 
@@ -440,6 +490,9 @@ void AsmPrinter::PrintHex(int Value) const {
 
 /// EOL - Print a newline character to asm stream.  If a comment is present
 /// then it will be printed first.  Comments should not contain '\n'.
+void AsmPrinter::EOL() const {
+  O << "\n";
+}
 void AsmPrinter::EOL(const std::string &Comment) const {
   if (AsmVerbose && !Comment.empty()) {
     O << "\t"
@@ -548,27 +601,53 @@ static void printStringChar(std::ostream &O, unsigned char C) {
 /// Special characters are emitted properly.
 /// \literal (Eg. '\t') \endliteral
 void AsmPrinter::EmitString(const std::string &String) const {
-  O << TAI->getAsciiDirective()
-    << "\"";
+  const char* AscizDirective = TAI->getAscizDirective();
+  if (AscizDirective)
+    O << AscizDirective;
+  else
+    O << TAI->getAsciiDirective();
+  O << "\"";
   for (unsigned i = 0, N = String.size(); i < N; ++i) {
     unsigned char C = String[i];
     printStringChar(O, C);
   }
-  O << "\\0\"";
+  if (AscizDirective)
+    O << "\"";
+  else
+    O << "\\0\"";
 }
 
 
 //===----------------------------------------------------------------------===//
 
-// EmitAlignment - Emit an alignment directive to the specified power of two.
-void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
+// EmitAlignment - Emit an alignment directive to the specified power of
+// two boundary.  For example, if you pass in 3 here, you will get an 8
+// byte alignment.  If a global value is specified, and if that global has
+// an explicit alignment requested, it will unconditionally override the
+// alignment request.  However, if ForcedAlignBits is specified, this value
+// has final say: the ultimate alignment will be the max of ForcedAlignBits
+// and the alignment computed with NumBits and the global.
+//
+// The algorithm is:
+//     Align = NumBits;
+//     if (GV && GV->hasalignment) Align = GV->getalignment();
+//     Align = std::max(Align, ForcedAlignBits);
+//
+void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
+                               unsigned ForcedAlignBits, bool UseFillExpr,
+                               unsigned FillValue) const {
   if (GV && GV->getAlignment())
     NumBits = Log2_32(GV->getAlignment());
+  NumBits = std::max(NumBits, ForcedAlignBits);
+  
   if (NumBits == 0) return;   // No need to emit alignment.
   if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
-  O << TAI->getAlignDirective() << NumBits << "\n";
+  O << TAI->getAlignDirective() << NumBits;
+  if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
+  O << "\n";
 }
 
+    
 /// EmitZeros - Emit a block of zeros.
 ///
 void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
@@ -613,8 +692,9 @@ void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
     case Instruction::GetElementPtr: {
       // generate a symbolic expression for the byte address
       const Constant *ptrVal = CE->getOperand(0);
-      std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
-      if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
+      SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
+      if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
+                                                idxVec.size())) {
         if (Offset)
           O << "(";
         EmitConstantValueOnly(ptrVal);
@@ -755,32 +835,72 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
     // FP Constants are printed as integer constants to avoid losing
     // precision...
-    double Val = CFP->getValue();
     if (CFP->getType() == Type::DoubleTy) {
+      double Val = CFP->getValueAPF().convertToDouble();  // for comment only
+      uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
       if (TAI->getData64bitsDirective())
-        O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
+        O << TAI->getData64bitsDirective() << i << "\t"
           << TAI->getCommentString() << " double value: " << Val << "\n";
       else if (TD->isBigEndian()) {
-        O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
+        O << TAI->getData32bitsDirective() << unsigned(i >> 32)
           << "\t" << TAI->getCommentString()
           << " double most significant word " << Val << "\n";
-        O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
+        O << TAI->getData32bitsDirective() << unsigned(i)
           << "\t" << TAI->getCommentString()
           << " double least significant word " << Val << "\n";
       } else {
-        O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
+        O << TAI->getData32bitsDirective() << unsigned(i)
           << "\t" << TAI->getCommentString()
           << " double least significant word " << Val << "\n";
-        O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
+        O << TAI->getData32bitsDirective() << unsigned(i >> 32)
           << "\t" << TAI->getCommentString()
           << " double most significant word " << Val << "\n";
       }
       return;
-    } else {
-      O << TAI->getData32bitsDirective() << FloatToBits(Val)
+    } else if (CFP->getType() == Type::FloatTy) {
+      float Val = CFP->getValueAPF().convertToFloat();  // for comment only
+      O << TAI->getData32bitsDirective()
+        << CFP->getValueAPF().convertToAPInt().getZExtValue()
         << "\t" << TAI->getCommentString() << " float " << Val << "\n";
       return;
-    }
+    } else if (CFP->getType() == Type::X86_FP80Ty) {
+      // all long double variants are printed as hex
+      const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
+      if (TD->isBigEndian()) {
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
+          << "\t" << TAI->getCommentString()
+          << " long double most significant halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0])
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[1])
+          << "\t" << TAI->getCommentString()
+          << " long double least significant halfword\n";
+       } else {
+        O << TAI->getData16bitsDirective() << uint16_t(p[1])
+          << "\t" << TAI->getCommentString()
+          << " long double least significant halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0])
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
+          << "\t" << TAI->getCommentString()
+          << " long double most significant halfword\n";
+      }
+      return;
+    } else assert(0 && "Floating point constant type not handled");
   } else if (CV->getType() == Type::Int64Ty) {
     if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
       uint64_t Val = CI->getZExtValue();
@@ -804,8 +924,8 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
       }
       return;
     }
-  } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
-    const PackedType *PTy = CP->getType();
+  } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
+    const VectorType *PTy = CP->getType();
     
     for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
       EmitGlobalConstant(CP->getOperand(I));
@@ -868,7 +988,7 @@ void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
   
   // Count the number of register definitions.
   unsigned NumDefs = 0;
-  for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
+  for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
        ++NumDefs)
     assert(NumDefs != NumOperands-1 && "No asm string?");
   
@@ -906,7 +1026,7 @@ void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
     }
     case '\n':
       ++LastEmitted;   // Consume newline character.
-      O << "\n\t";     // Indent code with newline.
+      O << "\n";       // Indent code with newline.
       break;
     case '$': {
       ++LastEmitted;   // Consume '$' character.
@@ -1046,7 +1166,7 @@ void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
 void AsmPrinter::printLabel(const MachineInstr *MI) const {
   O << "\n"
     << TAI->getPrivateGlobalPrefix()
-    << "label_"
+    << "label"
     << MI->getOperand(0).getImmedValue()
     << ":\n";
 }
@@ -1077,7 +1197,8 @@ void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
   if (printColon)
     O << ':';
   if (printComment && MBB->getBasicBlock())
-    O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
+    O << '\t' << TAI->getCommentString() << ' '
+      << MBB->getBasicBlock()->getName();
 }
 
 /// printSetLabel - This method prints a set label for the specified
@@ -1143,3 +1264,4 @@ void AsmPrinter::printDataDirective(const Type *type) {
     break;
   }
 }
+