LLVM API Documentation

ARMAsmPrinter.cpp

Go to the documentation of this file.
00001 //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a printer that converts from our internal representation
00011 // of machine-dependent LLVM code to GAS-format ARM assembly language.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "asm-printer"
00016 #include "ARM.h"
00017 #include "ARMBuildAttrs.h"
00018 #include "ARMTargetMachine.h"
00019 #include "ARMAddressingModes.h"
00020 #include "ARMConstantPoolValue.h"
00021 #include "ARMMachineFunctionInfo.h"
00022 #include "llvm/Constants.h"
00023 #include "llvm/Module.h"
00024 #include "llvm/Assembly/Writer.h"
00025 #include "llvm/CodeGen/AsmPrinter.h"
00026 #include "llvm/CodeGen/DwarfWriter.h"
00027 #include "llvm/CodeGen/MachineModuleInfo.h"
00028 #include "llvm/CodeGen/MachineFunctionPass.h"
00029 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00030 #include "llvm/MC/MCSectionMachO.h"
00031 #include "llvm/MC/MCStreamer.h"
00032 #include "llvm/Target/TargetAsmInfo.h"
00033 #include "llvm/Target/TargetData.h"
00034 #include "llvm/Target/TargetLoweringObjectFile.h"
00035 #include "llvm/Target/TargetMachine.h"
00036 #include "llvm/Target/TargetOptions.h"
00037 #include "llvm/Target/TargetRegistry.h"
00038 #include "llvm/ADT/SmallPtrSet.h"
00039 #include "llvm/ADT/Statistic.h"
00040 #include "llvm/ADT/StringSet.h"
00041 #include "llvm/Support/Compiler.h"
00042 #include "llvm/Support/ErrorHandling.h"
00043 #include "llvm/Support/Mangler.h"
00044 #include "llvm/Support/MathExtras.h"
00045 #include "llvm/Support/FormattedStream.h"
00046 #include <cctype>
00047 using namespace llvm;
00048 
00049 STATISTIC(EmittedInsts, "Number of machine instrs printed");
00050 
00051 namespace {
00052   class VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
00053     DwarfWriter *DW;
00054 
00055     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
00056     /// make the right decision when printing asm code for different targets.
00057     const ARMSubtarget *Subtarget;
00058 
00059     /// AFI - Keep a pointer to ARMFunctionInfo for the current
00060     /// MachineFunction.
00061     ARMFunctionInfo *AFI;
00062 
00063     /// MCP - Keep a pointer to constantpool entries of the current
00064     /// MachineFunction.
00065     const MachineConstantPool *MCP;
00066 
00067     /// We name each basic block in a Function with a unique number, so
00068     /// that we can consistently refer to them later. This is cleared
00069     /// at the beginning of each call to runOnMachineFunction().
00070     ///
00071     typedef std::map<const Value *, unsigned> ValueMapTy;
00072     ValueMapTy NumberForBB;
00073 
00074     /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
00075     /// non-lazy-pointers for indirect access.
00076     StringMap<std::string> GVNonLazyPtrs;
00077 
00078     /// HiddenGVNonLazyPtrs - Keeps the set of GlobalValues with hidden
00079     /// visibility that require non-lazy-pointers for indirect access.
00080     StringMap<std::string> HiddenGVNonLazyPtrs;
00081 
00082     /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
00083     bool InCPMode;
00084   public:
00085     explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
00086                            const TargetAsmInfo *T, bool V)
00087       : AsmPrinter(O, TM, T, V), DW(0), AFI(NULL), MCP(NULL),
00088         InCPMode(false) {
00089       Subtarget = &TM.getSubtarget<ARMSubtarget>();
00090     }
00091 
00092     virtual const char *getPassName() const {
00093       return "ARM Assembly Printer";
00094     }
00095 
00096     void printOperand(const MachineInstr *MI, int OpNum,
00097                       const char *Modifier = 0);
00098     void printSOImmOperand(const MachineInstr *MI, int OpNum);
00099     void printSOImm2PartOperand(const MachineInstr *MI, int OpNum);
00100     void printSORegOperand(const MachineInstr *MI, int OpNum);
00101     void printAddrMode2Operand(const MachineInstr *MI, int OpNum);
00102     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum);
00103     void printAddrMode3Operand(const MachineInstr *MI, int OpNum);
00104     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum);
00105     void printAddrMode4Operand(const MachineInstr *MI, int OpNum,
00106                                const char *Modifier = 0);
00107     void printAddrMode5Operand(const MachineInstr *MI, int OpNum,
00108                                const char *Modifier = 0);
00109     void printAddrMode6Operand(const MachineInstr *MI, int OpNum);
00110     void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
00111                                 const char *Modifier = 0);
00112     void printBitfieldInvMaskImmOperand (const MachineInstr *MI, int OpNum);
00113 
00114     void printThumbITMask(const MachineInstr *MI, int OpNum);
00115     void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum);
00116     void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
00117                                       unsigned Scale);
00118     void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum);
00119     void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum);
00120     void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum);
00121     void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum);
00122 
00123     void printT2SOOperand(const MachineInstr *MI, int OpNum);
00124     void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum);
00125     void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum);
00126     void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum);
00127     void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum);
00128     void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum);
00129 
00130     void printPredicateOperand(const MachineInstr *MI, int OpNum);
00131     void printSBitModifierOperand(const MachineInstr *MI, int OpNum);
00132     void printPCLabel(const MachineInstr *MI, int OpNum);
00133     void printRegisterList(const MachineInstr *MI, int OpNum);
00134     void printCPInstOperand(const MachineInstr *MI, int OpNum,
00135                             const char *Modifier);
00136     void printJTBlockOperand(const MachineInstr *MI, int OpNum);
00137     void printJT2BlockOperand(const MachineInstr *MI, int OpNum);
00138     void printTBAddrMode(const MachineInstr *MI, int OpNum);
00139     void printNoHashImmediate(const MachineInstr *MI, int OpNum);
00140 
00141     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
00142                                  unsigned AsmVariant, const char *ExtraCode);
00143     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
00144                                        unsigned AsmVariant,
00145                                        const char *ExtraCode);
00146 
00147     void PrintGlobalVariable(const GlobalVariable* GVar);
00148     void printInstruction(const MachineInstr *MI);  // autogenerated.
00149     void printMachineInstruction(const MachineInstr *MI);
00150     bool runOnMachineFunction(MachineFunction &F);
00151     bool doInitialization(Module &M);
00152     bool doFinalization(Module &M);
00153 
00154     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
00155     /// the .s file.
00156     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
00157       printDataDirective(MCPV->getType());
00158 
00159       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
00160       GlobalValue *GV = ACPV->getGV();
00161       std::string Name;
00162       
00163       if (GV) {
00164         bool isIndirect = Subtarget->isTargetDarwin() &&
00165           Subtarget->GVIsIndirectSymbol(GV,
00166                                         TM.getRelocationModel() == Reloc::Static);
00167         if (!isIndirect)
00168           Name = Mang->getMangledName(GV);
00169         else {
00170           // FIXME: Remove this when Darwin transition to @GOT like syntax.
00171           std::string SymName = Mang->getMangledName(GV);
00172           Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
00173           if (GV->hasHiddenVisibility())
00174             HiddenGVNonLazyPtrs[SymName] = Name;
00175           else
00176             GVNonLazyPtrs[SymName] = Name;
00177         }
00178       } else if (!strncmp(ACPV->getSymbol(), "L_lsda_", 7))
00179         Name = ACPV->getSymbol();
00180       else
00181         Name = Mang->makeNameProper(ACPV->getSymbol());
00182       O << Name;      
00183       
00184       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
00185       if (ACPV->getPCAdjustment() != 0) {
00186         O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
00187           << ACPV->getLabelId()
00188           << "+" << (unsigned)ACPV->getPCAdjustment();
00189          if (ACPV->mustAddCurrentAddress())
00190            O << "-.";
00191          O << ")";
00192       }
00193       O << "\n";
00194     }
00195     
00196     void getAnalysisUsage(AnalysisUsage &AU) const {
00197       AsmPrinter::getAnalysisUsage(AU);
00198       AU.setPreservesAll();
00199       AU.addRequired<MachineModuleInfo>();
00200       AU.addRequired<DwarfWriter>();
00201     }
00202   };
00203 } // end of anonymous namespace
00204 
00205 #include "ARMGenAsmWriter.inc"
00206 
00207 /// runOnMachineFunction - This uses the printInstruction()
00208 /// method to print assembly for each instruction.
00209 ///
00210 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
00211   this->MF = &MF;
00212 
00213   AFI = MF.getInfo<ARMFunctionInfo>();
00214   MCP = MF.getConstantPool();
00215 
00216   SetupMachineFunction(MF);
00217   O << "\n";
00218 
00219   // NOTE: we don't print out constant pools here, they are handled as
00220   // instructions.
00221 
00222   O << '\n';
00223   
00224   // Print out labels for the function.
00225   const Function *F = MF.getFunction();
00226   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
00227 
00228   switch (F->getLinkage()) {
00229   default: llvm_unreachable("Unknown linkage type!");
00230   case Function::PrivateLinkage:
00231   case Function::LinkerPrivateLinkage:
00232   case Function::InternalLinkage:
00233     break;
00234   case Function::ExternalLinkage:
00235     O << "\t.globl\t" << CurrentFnName << "\n";
00236     break;
00237   case Function::WeakAnyLinkage:
00238   case Function::WeakODRLinkage:
00239   case Function::LinkOnceAnyLinkage:
00240   case Function::LinkOnceODRLinkage:
00241     if (Subtarget->isTargetDarwin()) {
00242       O << "\t.globl\t" << CurrentFnName << "\n";
00243       O << "\t.weak_definition\t" << CurrentFnName << "\n";
00244     } else {
00245       O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
00246     }
00247     break;
00248   }
00249 
00250   printVisibility(CurrentFnName, F->getVisibility());
00251 
00252   if (AFI->isThumbFunction()) {
00253     EmitAlignment(MF.getAlignment(), F, AFI->getAlign());
00254     O << "\t.code\t16\n";
00255     O << "\t.thumb_func";
00256     if (Subtarget->isTargetDarwin())
00257       O << "\t" << CurrentFnName;
00258     O << "\n";
00259     InCPMode = false;
00260   } else {
00261     EmitAlignment(MF.getAlignment(), F);
00262   }
00263 
00264   O << CurrentFnName << ":\n";
00265   // Emit pre-function debug information.
00266   DW->BeginFunction(&MF);
00267 
00268   if (Subtarget->isTargetDarwin()) {
00269     // If the function is empty, then we need to emit *something*. Otherwise,
00270     // the function's label might be associated with something that it wasn't
00271     // meant to be associated with. We emit a noop in this situation.
00272     MachineFunction::iterator I = MF.begin();
00273 
00274     if (++I == MF.end() && MF.front().empty())
00275       O << "\tnop\n";
00276   }
00277 
00278   // Print out code for the function.
00279   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
00280        I != E; ++I) {
00281     // Print a label for the basic block.
00282     if (I != MF.begin()) {
00283       printBasicBlockLabel(I, true, true, VerboseAsm);
00284       O << '\n';
00285     }
00286     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
00287          II != E; ++II) {
00288       // Print the assembly for the instruction.
00289       printMachineInstruction(II);
00290     }
00291   }
00292 
00293   if (TAI->hasDotTypeDotSizeDirective())
00294     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
00295 
00296   // Emit post-function debug information.
00297   DW->EndFunction(&MF);
00298 
00299   return false;
00300 }
00301 
00302 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
00303                                  const char *Modifier) {
00304   const MachineOperand &MO = MI->getOperand(OpNum);
00305   switch (MO.getType()) {
00306   case MachineOperand::MO_Register: {
00307     unsigned Reg = MO.getReg();
00308     if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
00309       if (Modifier && strcmp(Modifier, "dregpair") == 0) {
00310         unsigned DRegLo = TRI->getSubReg(Reg, 5); // arm_dsubreg_0
00311         unsigned DRegHi = TRI->getSubReg(Reg, 6); // arm_dsubreg_1
00312         O << '{'
00313           << TRI->getAsmName(DRegLo) << ',' << TRI->getAsmName(DRegHi)
00314           << '}';
00315       } else if (Modifier && strcmp(Modifier, "lane") == 0) {
00316         unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
00317         unsigned DReg = TRI->getMatchingSuperReg(Reg, RegNum & 1 ? 0 : 1,
00318                                                  &ARM::DPRRegClass);
00319         O << TRI->getAsmName(DReg) << '[' << (RegNum & 1) << ']';
00320       } else {
00321         O << TRI->getAsmName(Reg);
00322       }
00323     } else
00324       llvm_unreachable("not implemented");
00325     break;
00326   }
00327   case MachineOperand::MO_Immediate: {
00328     O << '#' << MO.getImm();
00329     break;
00330   }
00331   case MachineOperand::MO_MachineBasicBlock:
00332     printBasicBlockLabel(MO.getMBB());
00333     return;
00334   case MachineOperand::MO_GlobalAddress: {
00335     bool isCallOp = Modifier && !strcmp(Modifier, "call");
00336     GlobalValue *GV = MO.getGlobal();
00337     O << Mang->getMangledName(GV);
00338 
00339     printOffset(MO.getOffset());
00340 
00341     if (isCallOp && Subtarget->isTargetELF() &&
00342         TM.getRelocationModel() == Reloc::PIC_)
00343       O << "(PLT)";
00344     break;
00345   }
00346   case MachineOperand::MO_ExternalSymbol: {
00347     bool isCallOp = Modifier && !strcmp(Modifier, "call");
00348     std::string Name = Mang->makeNameProper(MO.getSymbolName());
00349     
00350     O << Name;
00351     if (isCallOp && Subtarget->isTargetELF() &&
00352         TM.getRelocationModel() == Reloc::PIC_)
00353       O << "(PLT)";
00354     break;
00355   }
00356   case MachineOperand::MO_ConstantPoolIndex:
00357     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
00358       << '_' << MO.getIndex();
00359     break;
00360   case MachineOperand::MO_JumpTableIndex:
00361     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
00362       << '_' << MO.getIndex();
00363     break;
00364   default:
00365     O << "<unknown operand type>"; abort (); break;
00366   }
00367 }
00368 
00369 static void printSOImm(formatted_raw_ostream &O, int64_t V, bool VerboseAsm,
00370                        const TargetAsmInfo *TAI) {
00371   // Break it up into two parts that make up a shifter immediate.
00372   V = ARM_AM::getSOImmVal(V);
00373   assert(V != -1 && "Not a valid so_imm value!");
00374 
00375   unsigned Imm = ARM_AM::getSOImmValImm(V);
00376   unsigned Rot = ARM_AM::getSOImmValRot(V);
00377 
00378   // Print low-level immediate formation info, per
00379   // A5.1.3: "Data-processing operands - Immediate".
00380   if (Rot) {
00381     O << "#" << Imm << ", " << Rot;
00382     // Pretty printed version.
00383     if (VerboseAsm)
00384       O << ' ' << TAI->getCommentString()
00385         << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
00386   } else {
00387     O << "#" << Imm;
00388   }
00389 }
00390 
00391 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
00392 /// immediate in bits 0-7.
00393 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
00394   const MachineOperand &MO = MI->getOperand(OpNum);
00395   assert(MO.isImm() && "Not a valid so_imm value!");
00396   printSOImm(O, MO.getImm(), VerboseAsm, TAI);
00397 }
00398 
00399 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
00400 /// followed by an 'orr' to materialize.
00401 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
00402   const MachineOperand &MO = MI->getOperand(OpNum);
00403   assert(MO.isImm() && "Not a valid so_imm value!");
00404   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
00405   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
00406   printSOImm(O, V1, VerboseAsm, TAI);
00407   O << "\n\torr";
00408   printPredicateOperand(MI, 2);
00409   O << " ";
00410   printOperand(MI, 0); 
00411   O << ", ";
00412   printOperand(MI, 0); 
00413   O << ", ";
00414   printSOImm(O, V2, VerboseAsm, TAI);
00415 }
00416 
00417 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
00418 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
00419 //    REG 0   0           - e.g. R5
00420 //    REG REG 0,SH_OPC    - e.g. R5, ROR R3
00421 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
00422 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
00423   const MachineOperand &MO1 = MI->getOperand(Op);
00424   const MachineOperand &MO2 = MI->getOperand(Op+1);
00425   const MachineOperand &MO3 = MI->getOperand(Op+2);
00426 
00427   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
00428   O << TRI->getAsmName(MO1.getReg());
00429 
00430   // Print the shift opc.
00431   O << ", "
00432     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
00433     << " ";
00434 
00435   if (MO2.getReg()) {
00436     assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
00437     O << TRI->getAsmName(MO2.getReg());
00438     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
00439   } else {
00440     O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
00441   }
00442 }
00443 
00444 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
00445   const MachineOperand &MO1 = MI->getOperand(Op);
00446   const MachineOperand &MO2 = MI->getOperand(Op+1);
00447   const MachineOperand &MO3 = MI->getOperand(Op+2);
00448 
00449   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
00450     printOperand(MI, Op);
00451     return;
00452   }
00453 
00454   O << "[" << TRI->getAsmName(MO1.getReg());
00455 
00456   if (!MO2.getReg()) {
00457     if (ARM_AM::getAM2Offset(MO3.getImm()))  // Don't print +0.
00458       O << ", #"
00459         << (char)ARM_AM::getAM2Op(MO3.getImm())
00460         << ARM_AM::getAM2Offset(MO3.getImm());
00461     O << "]";
00462     return;
00463   }
00464 
00465   O << ", "
00466     << (char)ARM_AM::getAM2Op(MO3.getImm())
00467     << TRI->getAsmName(MO2.getReg());
00468   
00469   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
00470     O << ", "
00471       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
00472       << " #" << ShImm;
00473   O << "]";
00474 }
00475 
00476 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
00477   const MachineOperand &MO1 = MI->getOperand(Op);
00478   const MachineOperand &MO2 = MI->getOperand(Op+1);
00479 
00480   if (!MO1.getReg()) {
00481     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
00482     assert(ImmOffs && "Malformed indexed load / store!");
00483     O << "#"
00484       << (char)ARM_AM::getAM2Op(MO2.getImm())
00485       << ImmOffs;
00486     return;
00487   }
00488 
00489   O << (char)ARM_AM::getAM2Op(MO2.getImm())
00490     << TRI->getAsmName(MO1.getReg());
00491   
00492   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
00493     O << ", "
00494       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
00495       << " #" << ShImm;
00496 }
00497 
00498 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
00499   const MachineOperand &MO1 = MI->getOperand(Op);
00500   const MachineOperand &MO2 = MI->getOperand(Op+1);
00501   const MachineOperand &MO3 = MI->getOperand(Op+2);
00502   
00503   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
00504   O << "[" << TRI->getAsmName(MO1.getReg());
00505 
00506   if (MO2.getReg()) {
00507     O << ", "
00508       << (char)ARM_AM::getAM3Op(MO3.getImm())
00509       << TRI->getAsmName(MO2.getReg())
00510       << "]";
00511     return;
00512   }
00513   
00514   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
00515     O << ", #"
00516       << (char)ARM_AM::getAM3Op(MO3.getImm())
00517       << ImmOffs;
00518   O << "]";
00519 }
00520 
00521 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
00522   const MachineOperand &MO1 = MI->getOperand(Op);
00523   const MachineOperand &MO2 = MI->getOperand(Op+1);
00524 
00525   if (MO1.getReg()) {
00526     O << (char)ARM_AM::getAM3Op(MO2.getImm())
00527       << TRI->getAsmName(MO1.getReg());
00528     return;
00529   }
00530 
00531   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
00532   assert(ImmOffs && "Malformed indexed load / store!");
00533   O << "#"
00534     << (char)ARM_AM::getAM3Op(MO2.getImm())
00535     << ImmOffs;
00536 }
00537   
00538 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
00539                                           const char *Modifier) {
00540   const MachineOperand &MO1 = MI->getOperand(Op);
00541   const MachineOperand &MO2 = MI->getOperand(Op+1);
00542   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
00543   if (Modifier && strcmp(Modifier, "submode") == 0) {
00544     if (MO1.getReg() == ARM::SP) {
00545       // FIXME
00546       bool isLDM = (MI->getOpcode() == ARM::LDM ||
00547                     MI->getOpcode() == ARM::LDM_RET ||
00548                     MI->getOpcode() == ARM::t2LDM ||
00549                     MI->getOpcode() == ARM::t2LDM_RET);
00550       O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
00551     } else
00552       O << ARM_AM::getAMSubModeStr(Mode);
00553   } else if (Modifier && strcmp(Modifier, "wide") == 0) {
00554     ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
00555     if (Mode == ARM_AM::ia)
00556       O << ".w";
00557   } else {
00558     printOperand(MI, Op);
00559     if (ARM_AM::getAM4WBFlag(MO2.getImm()))
00560       O << "!";
00561   }
00562 }
00563 
00564 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
00565                                           const char *Modifier) {
00566   const MachineOperand &MO1 = MI->getOperand(Op);
00567   const MachineOperand &MO2 = MI->getOperand(Op+1);
00568 
00569   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
00570     printOperand(MI, Op);
00571     return;
00572   }
00573   
00574   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
00575 
00576   if (Modifier && strcmp(Modifier, "submode") == 0) {
00577     ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
00578     if (MO1.getReg() == ARM::SP) {
00579       bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
00580                      MI->getOpcode() == ARM::FLDMS);
00581       O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
00582     } else
00583       O << ARM_AM::getAMSubModeStr(Mode);
00584     return;
00585   } else if (Modifier && strcmp(Modifier, "base") == 0) {
00586     // Used for FSTM{D|S} and LSTM{D|S} operations.
00587     O << TRI->getAsmName(MO1.getReg());
00588     if (ARM_AM::getAM5WBFlag(MO2.getImm()))
00589       O << "!";
00590     return;
00591   }
00592   
00593   O << "[" << TRI->getAsmName(MO1.getReg());
00594   
00595   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
00596     O << ", #"
00597       << (char)ARM_AM::getAM5Op(MO2.getImm())
00598       << ImmOffs*4;
00599   }
00600   O << "]";
00601 }
00602 
00603 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op) {
00604   const MachineOperand &MO1 = MI->getOperand(Op);
00605   const MachineOperand &MO2 = MI->getOperand(Op+1);
00606   const MachineOperand &MO3 = MI->getOperand(Op+2);
00607 
00608   // FIXME: No support yet for specifying alignment.
00609   O << "[" << TRI->getAsmName(MO1.getReg()) << "]";
00610 
00611   if (ARM_AM::getAM6WBFlag(MO3.getImm())) {
00612     if (MO2.getReg() == 0)
00613       O << "!";
00614     else
00615       O << ", " << TRI->getAsmName(MO2.getReg());
00616   }
00617 }
00618 
00619 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
00620                                            const char *Modifier) {
00621   if (Modifier && strcmp(Modifier, "label") == 0) {
00622     printPCLabel(MI, Op+1);
00623     return;
00624   }
00625 
00626   const MachineOperand &MO1 = MI->getOperand(Op);
00627   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
00628   O << "[pc, +" << TRI->getAsmName(MO1.getReg()) << "]";
00629 }
00630 
00631 void
00632 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
00633   const MachineOperand &MO = MI->getOperand(Op);
00634   uint32_t v = ~MO.getImm();
00635   int32_t lsb = CountTrailingZeros_32(v);
00636   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
00637   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
00638   O << "#" << lsb << ", #" << width;
00639 }
00640 
00641 //===--------------------------------------------------------------------===//
00642 
00643 void
00644 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
00645   // (3 - the number of trailing zeros) is the number of then / else.
00646   unsigned Mask = MI->getOperand(Op).getImm();
00647   unsigned NumTZ = CountTrailingZeros_32(Mask);
00648   assert(NumTZ <= 3 && "Invalid IT mask!");
00649   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
00650     bool T = (Mask & (1 << Pos)) == 0;
00651     if (T)
00652       O << 't';
00653     else
00654       O << 'e';
00655   }
00656 }
00657 
00658 void
00659 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
00660   const MachineOperand &MO1 = MI->getOperand(Op);
00661   const MachineOperand &MO2 = MI->getOperand(Op+1);
00662   O << "[" << TRI->getAsmName(MO1.getReg());
00663   O << ", " << TRI->getAsmName(MO2.getReg()) << "]";
00664 }
00665 
00666 void
00667 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
00668                                             unsigned Scale) {
00669   const MachineOperand &MO1 = MI->getOperand(Op);
00670   const MachineOperand &MO2 = MI->getOperand(Op+1);
00671   const MachineOperand &MO3 = MI->getOperand(Op+2);
00672 
00673   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
00674     printOperand(MI, Op);
00675     return;
00676   }
00677 
00678   O << "[" << TRI->getAsmName(MO1.getReg());
00679   if (MO3.getReg())
00680     O << ", " << TRI->getAsmName(MO3.getReg());
00681   else if (unsigned ImmOffs = MO2.getImm()) {
00682     O << ", #" << ImmOffs;
00683     if (Scale > 1)
00684       O << " * " << Scale;
00685   }
00686   O << "]";
00687 }
00688 
00689 void
00690 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
00691   printThumbAddrModeRI5Operand(MI, Op, 1);
00692 }
00693 void
00694 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
00695   printThumbAddrModeRI5Operand(MI, Op, 2);
00696 }
00697 void
00698 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
00699   printThumbAddrModeRI5Operand(MI, Op, 4);
00700 }
00701 
00702 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
00703   const MachineOperand &MO1 = MI->getOperand(Op);
00704   const MachineOperand &MO2 = MI->getOperand(Op+1);
00705   O << "[" << TRI->getAsmName(MO1.getReg());
00706   if (unsigned ImmOffs = MO2.getImm())
00707     O << ", #" << ImmOffs << " * 4";
00708   O << "]";
00709 }
00710 
00711 //===--------------------------------------------------------------------===//
00712 
00713 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
00714 // register with shift forms.
00715 // REG 0   0           - e.g. R5
00716 // REG IMM, SH_OPC     - e.g. R5, LSL #3
00717 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
00718   const MachineOperand &MO1 = MI->getOperand(OpNum);
00719   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
00720 
00721   unsigned Reg = MO1.getReg();
00722   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
00723   O << TRI->getAsmName(Reg);
00724 
00725   // Print the shift opc.
00726   O << ", "
00727     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
00728     << " ";
00729 
00730   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
00731   O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
00732 }
00733 
00734 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
00735                                                 int OpNum) {
00736   const MachineOperand &MO1 = MI->getOperand(OpNum);
00737   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
00738 
00739   O << "[" << TRI->getAsmName(MO1.getReg());
00740 
00741   unsigned OffImm = MO2.getImm();
00742   if (OffImm)  // Don't print +0.
00743     O << ", #+" << OffImm;
00744   O << "]";
00745 }
00746 
00747 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
00748                                                int OpNum) {
00749   const MachineOperand &MO1 = MI->getOperand(OpNum);
00750   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
00751 
00752   O << "[" << TRI->getAsmName(MO1.getReg());
00753 
00754   int32_t OffImm = (int32_t)MO2.getImm();
00755   // Don't print +0.
00756   if (OffImm < 0)
00757     O << ", #-" << -OffImm;
00758   else if (OffImm > 0)
00759     O << ", #+" << OffImm;
00760   O << "]";
00761 }
00762 
00763 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
00764                                                  int OpNum) {
00765   const MachineOperand &MO1 = MI->getOperand(OpNum);
00766   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
00767 
00768   O << "[" << TRI->getAsmName(MO1.getReg());
00769 
00770   int32_t OffImm = (int32_t)MO2.getImm() / 4;
00771   // Don't print +0.
00772   if (OffImm < 0)
00773     O << ", #-" << -OffImm << " * 4";
00774   else if (OffImm > 0)
00775     O << ", #+" << OffImm << " * 4";
00776   O << "]";
00777 }
00778 
00779 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
00780                                                      int OpNum) {
00781   const MachineOperand &MO1 = MI->getOperand(OpNum);
00782   int32_t OffImm = (int32_t)MO1.getImm();
00783   // Don't print +0.
00784   if (OffImm < 0)
00785     O << "#-" << -OffImm;
00786   else if (OffImm > 0)
00787     O << "#+" << OffImm;
00788 }
00789 
00790 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
00791                                                 int OpNum) {
00792   const MachineOperand &MO1 = MI->getOperand(OpNum);
00793   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
00794   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
00795 
00796   O << "[" << TRI->getAsmName(MO1.getReg());
00797 
00798   assert(MO2.getReg() && "Invalid so_reg load / store address!");
00799   O << ", " << TRI->getAsmName(MO2.getReg());
00800 
00801   unsigned ShAmt = MO3.getImm();
00802   if (ShAmt) {
00803     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
00804     O << ", lsl #" << ShAmt;
00805   }
00806   O << "]";
00807 }
00808 
00809 
00810 //===--------------------------------------------------------------------===//
00811 
00812 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
00813   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
00814   if (CC != ARMCC::AL)
00815     O << ARMCondCodeToString(CC);
00816 }
00817 
00818 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
00819   unsigned Reg = MI->getOperand(OpNum).getReg();
00820   if (Reg) {
00821     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
00822     O << 's';
00823   }
00824 }
00825 
00826 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
00827   int Id = (int)MI->getOperand(OpNum).getImm();
00828   O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
00829 }
00830 
00831 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
00832   O << "{";
00833   for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
00834     if (MI->getOperand(i).isImplicit())
00835       continue;
00836     if ((int)i != OpNum) O << ", ";
00837     printOperand(MI, i);
00838   }
00839   O << "}";
00840 }
00841 
00842 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
00843                                        const char *Modifier) {
00844   assert(Modifier && "This operand only works with a modifier!");
00845   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
00846   // data itself.
00847   if (!strcmp(Modifier, "label")) {
00848     unsigned ID = MI->getOperand(OpNum).getImm();
00849     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
00850       << '_' << ID << ":\n";
00851   } else {
00852     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
00853     unsigned CPI = MI->getOperand(OpNum).getIndex();
00854 
00855     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
00856     
00857     if (MCPE.isMachineConstantPoolEntry()) {
00858       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
00859     } else {
00860       EmitGlobalConstant(MCPE.Val.ConstVal);
00861     }
00862   }
00863 }
00864 
00865 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
00866   assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
00867 
00868   const MachineOperand &MO1 = MI->getOperand(OpNum);
00869   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
00870   unsigned JTI = MO1.getIndex();
00871   O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
00872     << '_' << JTI << '_' << MO2.getImm() << ":\n";
00873 
00874   const char *JTEntryDirective = TAI->getData32bitsDirective();
00875 
00876   const MachineFunction *MF = MI->getParent()->getParent();
00877   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
00878   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
00879   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
00880   bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
00881   SmallPtrSet<MachineBasicBlock*, 8> JTSets;
00882   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
00883     MachineBasicBlock *MBB = JTBBs[i];
00884     bool isNew = JTSets.insert(MBB);
00885 
00886     if (UseSet && isNew)
00887       printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
00888 
00889     O << JTEntryDirective << ' ';
00890     if (UseSet)
00891       O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
00892         << '_' << JTI << '_' << MO2.getImm()
00893         << "_set_" << MBB->getNumber();
00894     else if (TM.getRelocationModel() == Reloc::PIC_) {
00895       printBasicBlockLabel(MBB, false, false, false);
00896       O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
00897         << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
00898     } else {
00899       printBasicBlockLabel(MBB, false, false, false);
00900     }
00901     if (i != e-1)
00902       O << '\n';
00903   }
00904 }
00905 
00906 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum) {
00907   const MachineOperand &MO1 = MI->getOperand(OpNum);
00908   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
00909   unsigned JTI = MO1.getIndex();
00910   O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
00911     << '_' << JTI << '_' << MO2.getImm() << ":\n";
00912 
00913   const MachineFunction *MF = MI->getParent()->getParent();
00914   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
00915   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
00916   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
00917   bool ByteOffset = false, HalfWordOffset = false;
00918   if (MI->getOpcode() == ARM::t2TBB)
00919     ByteOffset = true;
00920   else if (MI->getOpcode() == ARM::t2TBH)
00921     HalfWordOffset = true;
00922 
00923   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
00924     MachineBasicBlock *MBB = JTBBs[i];
00925     if (ByteOffset)
00926       O << TAI->getData8bitsDirective();
00927     else if (HalfWordOffset)
00928       O << TAI->getData16bitsDirective();
00929     if (ByteOffset || HalfWordOffset) {
00930       O << '(';
00931       printBasicBlockLabel(MBB, false, false, false);
00932       O << "-" << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
00933         << '_' << JTI << '_' << MO2.getImm() << ")/2";
00934     } else {
00935       O << "\tb.w ";
00936       printBasicBlockLabel(MBB, false, false, false);
00937     }
00938     if (i != e-1)
00939       O << '\n';
00940   }
00941 
00942   // Make sure the instruction that follows TBB is 2-byte aligned.
00943   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
00944   if (ByteOffset && (JTBBs.size() & 1)) {
00945     O << '\n';
00946     EmitAlignment(1);
00947   }
00948 }
00949 
00950 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum) {
00951   O << "[pc, " << TRI->getAsmName(MI->getOperand(OpNum).getReg());
00952   if (MI->getOpcode() == ARM::t2TBH)
00953     O << ", lsl #1";
00954   O << ']';
00955 }
00956 
00957 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum) {
00958   O << MI->getOperand(OpNum).getImm();
00959 }
00960 
00961 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
00962                                     unsigned AsmVariant, const char *ExtraCode){
00963   // Does this asm operand have a single letter operand modifier?
00964   if (ExtraCode && ExtraCode[0]) {
00965     if (ExtraCode[1] != 0) return true; // Unknown modifier.
00966 
00967     switch (ExtraCode[0]) {
00968     default: return true;  // Unknown modifier.
00969     case 'a': // Print as a memory address.
00970       if (MI->getOperand(OpNum).isReg()) {
00971         O << "[" << TRI->getAsmName(MI->getOperand(OpNum).getReg()) << "]";
00972         return false;
00973       }
00974       // Fallthrough
00975     case 'c': // Don't print "#" before an immediate operand.
00976       if (!MI->getOperand(OpNum).isImm())
00977         return true;
00978       printNoHashImmediate(MI, OpNum);
00979       return false;
00980     case 'P': // Print a VFP double precision register.
00981       printOperand(MI, OpNum);
00982       return false;
00983     case 'Q':
00984       if (TM.getTargetData()->isLittleEndian())
00985         break;
00986       // Fallthrough
00987     case 'R':
00988       if (TM.getTargetData()->isBigEndian())
00989         break;
00990       // Fallthrough
00991     case 'H': // Write second word of DI / DF reference.  
00992       // Verify that this operand has two consecutive registers.
00993       if (!MI->getOperand(OpNum).isReg() ||
00994           OpNum+1 == MI->getNumOperands() ||
00995           !MI->getOperand(OpNum+1).isReg())
00996         return true;
00997       ++OpNum;   // Return the high-part.
00998     }
00999   }
01000   
01001   printOperand(MI, OpNum);
01002   return false;
01003 }
01004 
01005 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
01006                                           unsigned OpNum, unsigned AsmVariant,
01007                                           const char *ExtraCode) {
01008   if (ExtraCode && ExtraCode[0])
01009     return true; // Unknown modifier.
01010   printAddrMode2Operand(MI, OpNum);
01011   return false;
01012 }
01013 
01014 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
01015   ++EmittedInsts;
01016 
01017   int Opc = MI->getOpcode();
01018   switch (Opc) {
01019   case ARM::CONSTPOOL_ENTRY:
01020     if (!InCPMode && AFI->isThumbFunction()) {
01021       EmitAlignment(2);
01022       InCPMode = true;
01023     }
01024     break;
01025   default: {
01026     if (InCPMode && AFI->isThumbFunction())
01027       InCPMode = false;
01028   }}
01029 
01030   // Call the autogenerated instruction printer routines.
01031   printInstruction(MI);
01032 }
01033 
01034 bool ARMAsmPrinter::doInitialization(Module &M) {
01035 
01036   bool Result = AsmPrinter::doInitialization(M);
01037   DW = getAnalysisIfAvailable<DwarfWriter>();
01038 
01039   // Use unified assembler syntax mode for Thumb.
01040   if (Subtarget->isThumb())
01041     O << "\t.syntax unified\n";
01042 
01043   // Emit ARM Build Attributes
01044   if (Subtarget->isTargetELF()) {
01045     // CPU Type
01046     std::string CPUString = Subtarget->getCPUString();
01047     if (CPUString != "generic")
01048       O << "\t.cpu " << CPUString << '\n';
01049 
01050     // FIXME: Emit FPU type
01051     if (Subtarget->hasVFP2())
01052       O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
01053 
01054     // Signal various FP modes.
01055     if (!UnsafeFPMath)
01056       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
01057         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
01058 
01059     if (FiniteOnlyFPMath())
01060       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
01061     else
01062       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
01063 
01064     // 8-bytes alignment stuff.
01065     O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
01066       << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
01067 
01068     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
01069     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard)
01070       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_HardFP_use << ", 3\n"
01071         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_VFP_args << ", 1\n";
01072 
01073     // FIXME: Should we signal R9 usage?
01074   }
01075 
01076   return Result;
01077 }
01078 
01079 void ARMAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
01080   const TargetData *TD = TM.getTargetData();
01081 
01082   if (!GVar->hasInitializer())   // External global require no code
01083     return;
01084 
01085   // Check to see if this is a special global used by LLVM, if so, emit it.
01086 
01087   if (EmitSpecialLLVMGlobal(GVar)) {
01088     if (Subtarget->isTargetDarwin() &&
01089         TM.getRelocationModel() == Reloc::Static) {
01090       if (GVar->getName() == "llvm.global_ctors")
01091         O << ".reference .constructors_used\n";
01092       else if (GVar->getName() == "llvm.global_dtors")
01093         O << ".reference .destructors_used\n";
01094     }
01095     return;
01096   }
01097 
01098   std::string name = Mang->getMangledName(GVar);
01099   Constant *C = GVar->getInitializer();
01100   const Type *Type = C->getType();
01101   unsigned Size = TD->getTypeAllocSize(Type);
01102   unsigned Align = TD->getPreferredAlignmentLog(GVar);
01103   bool isDarwin = Subtarget->isTargetDarwin();
01104 
01105   printVisibility(name, GVar->getVisibility());
01106 
01107   if (Subtarget->isTargetELF())
01108     O << "\t.type " << name << ",%object\n";
01109   
01110   const MCSection *TheSection =
01111     getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
01112   OutStreamer.SwitchSection(TheSection);
01113 
01114   // FIXME: get this stuff from section kind flags.
01115   if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal() &&
01116       // Don't put things that should go in the cstring section into "comm".
01117       !TheSection->getKind().isMergeableCString()) {
01118     if (GVar->hasExternalLinkage()) {
01119       if (const char *Directive = TAI->getZeroFillDirective()) {
01120         O << "\t.globl\t" << name << "\n";
01121         O << Directive << "__DATA, __common, " << name << ", "
01122           << Size << ", " << Align << "\n";
01123         return;
01124       }
01125     }
01126 
01127     if (GVar->hasLocalLinkage() || GVar->isWeakForLinker()) {
01128       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
01129 
01130       if (isDarwin) {
01131         if (GVar->hasLocalLinkage()) {
01132           O << TAI->getLCOMMDirective()  << name << "," << Size
01133             << ',' << Align;
01134         } else if (GVar->hasCommonLinkage()) {
01135           O << TAI->getCOMMDirective()  << name << "," << Size
01136             << ',' << Align;
01137         } else {
01138           OutStreamer.SwitchSection(TheSection);
01139           O << "\t.globl " << name << '\n'
01140             << TAI->getWeakDefDirective() << name << '\n';
01141           EmitAlignment(Align, GVar);
01142           O << name << ":";
01143           if (VerboseAsm) {
01144             O << "\t\t\t\t" << TAI->getCommentString() << ' ';
01145             WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
01146           }
01147           O << '\n';
01148           EmitGlobalConstant(C);
01149           return;
01150         }
01151       } else if (TAI->getLCOMMDirective() != NULL) {
01152         if (GVar->hasLocalLinkage()) {
01153           O << TAI->getLCOMMDirective() << name << "," << Size;
01154         } else {
01155           O << TAI->getCOMMDirective()  << name << "," << Size;
01156           if (TAI->getCOMMDirectiveTakesAlignment())
01157             O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
01158         }
01159       } else {
01160         if (GVar->hasLocalLinkage())
01161           O << "\t.local\t" << name << "\n";
01162         O << TAI->getCOMMDirective()  << name << "," << Size;
01163         if (TAI->getCOMMDirectiveTakesAlignment())
01164           O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
01165       }
01166       if (VerboseAsm) {
01167         O << "\t\t" << TAI->getCommentString() << " ";
01168         WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
01169       }
01170       O << "\n";
01171       return;
01172     }
01173   }
01174   
01175   switch (GVar->getLinkage()) {
01176   case GlobalValue::CommonLinkage:
01177   case GlobalValue::LinkOnceAnyLinkage:
01178   case GlobalValue::LinkOnceODRLinkage:
01179   case GlobalValue::WeakAnyLinkage:
01180   case GlobalValue::WeakODRLinkage:
01181     if (isDarwin) {
01182       O << "\t.globl " << name << "\n"
01183         << "\t.weak_definition " << name << "\n";
01184     } else {
01185       O << "\t.weak " << name << "\n";
01186     }
01187     break;
01188   case GlobalValue::AppendingLinkage:
01189   // FIXME: appending linkage variables should go into a section of
01190   // their name or something.  For now, just emit them as external.
01191   case GlobalValue::ExternalLinkage:
01192     O << "\t.globl " << name << "\n";
01193     break;
01194   case GlobalValue::PrivateLinkage:
01195   case GlobalValue::LinkerPrivateLinkage:
01196   case GlobalValue::InternalLinkage:
01197     break;
01198   default:
01199     llvm_unreachable("Unknown linkage type!");
01200   }
01201 
01202   EmitAlignment(Align, GVar);
01203   O << name << ":";
01204   if (VerboseAsm) {
01205     O << "\t\t\t\t" << TAI->getCommentString() << " ";
01206     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
01207   }
01208   O << "\n";
01209   if (TAI->hasDotTypeDotSizeDirective())
01210     O << "\t.size " << name << ", " << Size << "\n";
01211 
01212   EmitGlobalConstant(C);
01213   O << '\n';
01214 }
01215 
01216 
01217 bool ARMAsmPrinter::doFinalization(Module &M) {
01218   if (Subtarget->isTargetDarwin()) {
01219     // All darwin targets use mach-o.
01220     TargetLoweringObjectFileMachO &TLOFMacho = 
01221       static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
01222     
01223     O << '\n';
01224 
01225     // Output non-lazy-pointers for external and common global variables.
01226     if (!GVNonLazyPtrs.empty()) {
01227       // Switch with ".non_lazy_symbol_pointer" directive.
01228       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
01229       EmitAlignment(2);
01230       for (StringMap<std::string>::iterator I = GVNonLazyPtrs.begin(),
01231            E = GVNonLazyPtrs.end(); I != E; ++I) {
01232         O << I->second << ":\n";
01233         O << "\t.indirect_symbol " << I->getKeyData() << "\n";
01234         O << "\t.long\t0\n";
01235       }
01236     }
01237 
01238     if (!HiddenGVNonLazyPtrs.empty()) {
01239       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
01240       EmitAlignment(2);
01241       for (StringMap<std::string>::iterator I = HiddenGVNonLazyPtrs.begin(),
01242              E = HiddenGVNonLazyPtrs.end(); I != E; ++I) {
01243         O << I->second << ":\n";
01244         O << "\t.long " << I->getKeyData() << "\n";
01245       }
01246     }
01247 
01248     // Funny Darwin hack: This flag tells the linker that no global symbols
01249     // contain code that falls through to other global symbols (e.g. the obvious
01250     // implementation of multiple entry points).  If this doesn't occur, the
01251     // linker can safely perform dead code stripping.  Since LLVM never
01252     // generates code that does this, it is always safe to set.
01253     O << "\t.subsections_via_symbols\n";
01254   }
01255 
01256   return AsmPrinter::doFinalization(M);
01257 }
01258 
01259 // Force static initialization.
01260 extern "C" void LLVMInitializeARMAsmPrinter() { 
01261   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
01262   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
01263 }