hoist handling of external globals and special globals up to common code.
[oota-llvm.git] / lib / Target / ARM / AsmPrinter / ARMAsmPrinter.cpp
1 //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format ARM assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "ARM.h"
17 #include "ARMBuildAttrs.h"
18 #include "ARMAddressingModes.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMInstPrinter.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "ARMMCInstLower.h"
23 #include "ARMTargetMachine.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Module.h"
26 #include "llvm/Type.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/CodeGen/AsmPrinter.h"
29 #include "llvm/CodeGen/DwarfWriter.h"
30 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineJumpTableInfo.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/MC/MCContext.h"
35 #include "llvm/MC/MCInst.h"
36 #include "llvm/MC/MCSectionMachO.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Target/TargetData.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
43 #include "llvm/Target/TargetRegistry.h"
44 #include "llvm/ADT/SmallPtrSet.h"
45 #include "llvm/ADT/SmallString.h"
46 #include "llvm/ADT/Statistic.h"
47 #include "llvm/ADT/StringExtras.h"
48 #include "llvm/ADT/StringSet.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Support/FormattedStream.h"
52 #include "llvm/Support/MathExtras.h"
53 #include <cctype>
54 using namespace llvm;
55
56 STATISTIC(EmittedInsts, "Number of machine instrs printed");
57
58 static cl::opt<bool>
59 EnableMCInst("enable-arm-mcinst-printer", cl::Hidden,
60             cl::desc("enable experimental asmprinter gunk in the arm backend"));
61
62 namespace {
63   class ARMAsmPrinter : public AsmPrinter {
64
65     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
66     /// make the right decision when printing asm code for different targets.
67     const ARMSubtarget *Subtarget;
68
69     /// AFI - Keep a pointer to ARMFunctionInfo for the current
70     /// MachineFunction.
71     ARMFunctionInfo *AFI;
72
73     /// MCP - Keep a pointer to constantpool entries of the current
74     /// MachineFunction.
75     const MachineConstantPool *MCP;
76
77   public:
78     explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
79                            const MCAsmInfo *T, bool V)
80       : AsmPrinter(O, TM, T, V), AFI(NULL), MCP(NULL) {
81       Subtarget = &TM.getSubtarget<ARMSubtarget>();
82     }
83
84     virtual const char *getPassName() const {
85       return "ARM Assembly Printer";
86     }
87     
88     void printMCInst(const MCInst *MI) {
89       ARMInstPrinter(O, *MAI, VerboseAsm).printInstruction(MI);
90     }
91     
92     void printInstructionThroughMCStreamer(const MachineInstr *MI);
93     
94
95     void printOperand(const MachineInstr *MI, int OpNum,
96                       const char *Modifier = 0);
97     void printSOImmOperand(const MachineInstr *MI, int OpNum);
98     void printSOImm2PartOperand(const MachineInstr *MI, int OpNum);
99     void printSORegOperand(const MachineInstr *MI, int OpNum);
100     void printAddrMode2Operand(const MachineInstr *MI, int OpNum);
101     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum);
102     void printAddrMode3Operand(const MachineInstr *MI, int OpNum);
103     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum);
104     void printAddrMode4Operand(const MachineInstr *MI, int OpNum,
105                                const char *Modifier = 0);
106     void printAddrMode5Operand(const MachineInstr *MI, int OpNum,
107                                const char *Modifier = 0);
108     void printAddrMode6Operand(const MachineInstr *MI, int OpNum);
109     void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
110                                 const char *Modifier = 0);
111     void printBitfieldInvMaskImmOperand (const MachineInstr *MI, int OpNum);
112
113     void printThumbS4ImmOperand(const MachineInstr *MI, int OpNum);
114     void printThumbITMask(const MachineInstr *MI, int OpNum);
115     void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum);
116     void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
117                                       unsigned Scale);
118     void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum);
119     void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum);
120     void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum);
121     void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum);
122
123     void printT2SOOperand(const MachineInstr *MI, int OpNum);
124     void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum);
125     void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum);
126     void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum);
127     void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum);
128     void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum);
129
130     void printPredicateOperand(const MachineInstr *MI, int OpNum);
131     void printSBitModifierOperand(const MachineInstr *MI, int OpNum);
132     void printPCLabel(const MachineInstr *MI, int OpNum);
133     void printRegisterList(const MachineInstr *MI, int OpNum);
134     void printCPInstOperand(const MachineInstr *MI, int OpNum,
135                             const char *Modifier);
136     void printJTBlockOperand(const MachineInstr *MI, int OpNum);
137     void printJT2BlockOperand(const MachineInstr *MI, int OpNum);
138     void printTBAddrMode(const MachineInstr *MI, int OpNum);
139     void printNoHashImmediate(const MachineInstr *MI, int OpNum);
140     void printVFPf32ImmOperand(const MachineInstr *MI, int OpNum);
141     void printVFPf64ImmOperand(const MachineInstr *MI, int OpNum);
142
143     void printHex8ImmOperand(const MachineInstr *MI, int OpNum) {
144       O << "#0x" << utohexstr(MI->getOperand(OpNum).getImm() & 0xff);
145     }
146     void printHex16ImmOperand(const MachineInstr *MI, int OpNum) {
147       O << "#0x" << utohexstr(MI->getOperand(OpNum).getImm() & 0xffff);
148     }
149     void printHex32ImmOperand(const MachineInstr *MI, int OpNum) {
150       O << "#0x" << utohexstr(MI->getOperand(OpNum).getImm() & 0xffffffff);
151     }
152     void printHex64ImmOperand(const MachineInstr *MI, int OpNum) {
153       O << "#0x" << utohexstr(MI->getOperand(OpNum).getImm());
154     }
155
156     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
157                                  unsigned AsmVariant, const char *ExtraCode);
158     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
159                                        unsigned AsmVariant,
160                                        const char *ExtraCode);
161
162     void PrintGlobalVariable(const GlobalVariable* GVar);
163     void printInstruction(const MachineInstr *MI);  // autogenerated.
164     static const char *getRegisterName(unsigned RegNo);
165
166     void printMachineInstruction(const MachineInstr *MI);
167     bool runOnMachineFunction(MachineFunction &F);
168     void EmitStartOfAsmFile(Module &M);
169     void EmitEndOfAsmFile(Module &M);
170
171     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
172     /// the .s file.
173     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
174       printDataDirective(MCPV->getType());
175
176       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
177       SmallString<128> TmpNameStr;
178
179       if (ACPV->isLSDA()) {
180         raw_svector_ostream(TmpNameStr) << MAI->getPrivateGlobalPrefix() <<
181           "_LSDA_" << getFunctionNumber();
182         O << TmpNameStr.str();
183       } else if (ACPV->isBlockAddress()) {
184         O << GetBlockAddressSymbol(ACPV->getBlockAddress())->getName();
185       } else if (ACPV->isGlobalValue()) {
186         GlobalValue *GV = ACPV->getGV();
187         bool isIndirect = Subtarget->isTargetDarwin() &&
188           Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
189         if (!isIndirect)
190           O << *GetGlobalValueSymbol(GV);
191         else {
192           // FIXME: Remove this when Darwin transition to @GOT like syntax.
193           MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
194           O << *Sym;
195           
196           MachineModuleInfoMachO &MMIMachO =
197             MMI->getObjFileInfo<MachineModuleInfoMachO>();
198           const MCSymbol *&StubSym =
199             GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
200                                         MMIMachO.getGVStubEntry(Sym);
201           if (StubSym == 0)
202             StubSym = GetGlobalValueSymbol(GV);
203         }
204       } else {
205         assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
206         O << *GetExternalSymbolSymbol(ACPV->getSymbol());
207       }
208
209       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
210       if (ACPV->getPCAdjustment() != 0) {
211         O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
212           << getFunctionNumber() << "_"  << ACPV->getLabelId()
213           << "+" << (unsigned)ACPV->getPCAdjustment();
214          if (ACPV->mustAddCurrentAddress())
215            O << "-.";
216          O << ')';
217       }
218       O << '\n';
219     }
220
221     void getAnalysisUsage(AnalysisUsage &AU) const {
222       AsmPrinter::getAnalysisUsage(AU);
223       AU.setPreservesAll();
224       AU.addRequired<MachineModuleInfo>();
225       AU.addRequired<DwarfWriter>();
226     }
227   };
228 } // end of anonymous namespace
229
230 #include "ARMGenAsmWriter.inc"
231
232 /// runOnMachineFunction - This uses the printInstruction()
233 /// method to print assembly for each instruction.
234 ///
235 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
236   this->MF = &MF;
237
238   AFI = MF.getInfo<ARMFunctionInfo>();
239   MCP = MF.getConstantPool();
240
241   SetupMachineFunction(MF);
242   O << "\n";
243
244   // NOTE: we don't print out constant pools here, they are handled as
245   // instructions.
246
247   O << '\n';
248
249   // Print out labels for the function.
250   const Function *F = MF.getFunction();
251   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
252
253   switch (F->getLinkage()) {
254   default: llvm_unreachable("Unknown linkage type!");
255   case Function::PrivateLinkage:
256   case Function::InternalLinkage:
257     break;
258   case Function::ExternalLinkage:
259     O << "\t.globl\t" << *CurrentFnSym << "\n";
260     break;
261   case Function::LinkerPrivateLinkage:
262   case Function::WeakAnyLinkage:
263   case Function::WeakODRLinkage:
264   case Function::LinkOnceAnyLinkage:
265   case Function::LinkOnceODRLinkage:
266     if (Subtarget->isTargetDarwin()) {
267       O << "\t.globl\t" << *CurrentFnSym << "\n";
268       O << "\t.weak_definition\t" << *CurrentFnSym << "\n";
269     } else {
270       O << MAI->getWeakRefDirective() << *CurrentFnSym << "\n";
271     }
272     break;
273   }
274
275   printVisibility(CurrentFnSym, F->getVisibility());
276
277   unsigned FnAlign = 1 << MF.getAlignment();  // MF alignment is log2.
278   if (AFI->isThumbFunction()) {
279     EmitAlignment(FnAlign, F, AFI->getAlign());
280     O << "\t.code\t16\n";
281     O << "\t.thumb_func";
282     if (Subtarget->isTargetDarwin())
283       O << "\t" << *CurrentFnSym;
284     O << "\n";
285   } else {
286     EmitAlignment(FnAlign, F);
287   }
288
289   O << *CurrentFnSym << ":\n";
290   // Emit pre-function debug information.
291   DW->BeginFunction(&MF);
292
293   if (Subtarget->isTargetDarwin()) {
294     // If the function is empty, then we need to emit *something*. Otherwise,
295     // the function's label might be associated with something that it wasn't
296     // meant to be associated with. We emit a noop in this situation.
297     MachineFunction::iterator I = MF.begin();
298
299     if (++I == MF.end() && MF.front().empty())
300       O << "\tnop\n";
301   }
302
303   // Print out code for the function.
304   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
305        I != E; ++I) {
306     // Print a label for the basic block.
307     if (I != MF.begin())
308       EmitBasicBlockStart(I);
309
310     // Print the assembly for the instruction.
311     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
312          II != E; ++II)
313       printMachineInstruction(II);
314   }
315
316   if (MAI->hasDotTypeDotSizeDirective())
317     O << "\t.size " << *CurrentFnSym << ", .-" << *CurrentFnSym << "\n";
318
319   // Emit post-function debug information.
320   DW->EndFunction(&MF);
321
322   return false;
323 }
324
325 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
326                                  const char *Modifier) {
327   const MachineOperand &MO = MI->getOperand(OpNum);
328   unsigned TF = MO.getTargetFlags();
329
330   switch (MO.getType()) {
331   default:
332     assert(0 && "<unknown operand type>");
333   case MachineOperand::MO_Register: {
334     unsigned Reg = MO.getReg();
335     assert(TargetRegisterInfo::isPhysicalRegister(Reg));
336     if (Modifier && strcmp(Modifier, "dregpair") == 0) {
337       unsigned DRegLo = TRI->getSubReg(Reg, 5); // arm_dsubreg_0
338       unsigned DRegHi = TRI->getSubReg(Reg, 6); // arm_dsubreg_1
339       O << '{'
340         << getRegisterName(DRegLo) << ',' << getRegisterName(DRegHi)
341         << '}';
342     } else if (Modifier && strcmp(Modifier, "lane") == 0) {
343       unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
344       unsigned DReg = TRI->getMatchingSuperReg(Reg, RegNum & 1 ? 2 : 1,
345                                                &ARM::DPR_VFP2RegClass);
346       O << getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
347     } else {
348       assert(!MO.getSubReg() && "Subregs should be eliminated!");
349       O << getRegisterName(Reg);
350     }
351     break;
352   }
353   case MachineOperand::MO_Immediate: {
354     int64_t Imm = MO.getImm();
355     O << '#';
356     if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
357         (TF & ARMII::MO_LO16))
358       O << ":lower16:";
359     else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
360              (TF & ARMII::MO_HI16))
361       O << ":upper16:";
362     O << Imm;
363     break;
364   }
365   case MachineOperand::MO_MachineBasicBlock:
366     O << *GetMBBSymbol(MO.getMBB()->getNumber());
367     return;
368   case MachineOperand::MO_GlobalAddress: {
369     bool isCallOp = Modifier && !strcmp(Modifier, "call");
370     GlobalValue *GV = MO.getGlobal();
371
372     if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
373         (TF & ARMII::MO_LO16))
374       O << ":lower16:";
375     else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
376              (TF & ARMII::MO_HI16))
377       O << ":upper16:";
378     O << *GetGlobalValueSymbol(GV);
379
380     printOffset(MO.getOffset());
381
382     if (isCallOp && Subtarget->isTargetELF() &&
383         TM.getRelocationModel() == Reloc::PIC_)
384       O << "(PLT)";
385     break;
386   }
387   case MachineOperand::MO_ExternalSymbol: {
388     bool isCallOp = Modifier && !strcmp(Modifier, "call");
389     O << *GetExternalSymbolSymbol(MO.getSymbolName());
390     
391     if (isCallOp && Subtarget->isTargetELF() &&
392         TM.getRelocationModel() == Reloc::PIC_)
393       O << "(PLT)";
394     break;
395   }
396   case MachineOperand::MO_ConstantPoolIndex:
397     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
398       << '_' << MO.getIndex();
399     break;
400   case MachineOperand::MO_JumpTableIndex:
401     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
402       << '_' << MO.getIndex();
403     break;
404   }
405 }
406
407 static void printSOImm(formatted_raw_ostream &O, int64_t V, bool VerboseAsm,
408                        const MCAsmInfo *MAI) {
409   // Break it up into two parts that make up a shifter immediate.
410   V = ARM_AM::getSOImmVal(V);
411   assert(V != -1 && "Not a valid so_imm value!");
412
413   unsigned Imm = ARM_AM::getSOImmValImm(V);
414   unsigned Rot = ARM_AM::getSOImmValRot(V);
415
416   // Print low-level immediate formation info, per
417   // A5.1.3: "Data-processing operands - Immediate".
418   if (Rot) {
419     O << "#" << Imm << ", " << Rot;
420     // Pretty printed version.
421     if (VerboseAsm) {
422       O.PadToColumn(MAI->getCommentColumn());
423       O << MAI->getCommentString() << ' ';
424       O << (int)ARM_AM::rotr32(Imm, Rot);
425     }
426   } else {
427     O << "#" << Imm;
428   }
429 }
430
431 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
432 /// immediate in bits 0-7.
433 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
434   const MachineOperand &MO = MI->getOperand(OpNum);
435   assert(MO.isImm() && "Not a valid so_imm value!");
436   printSOImm(O, MO.getImm(), VerboseAsm, MAI);
437 }
438
439 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
440 /// followed by an 'orr' to materialize.
441 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
442   const MachineOperand &MO = MI->getOperand(OpNum);
443   assert(MO.isImm() && "Not a valid so_imm value!");
444   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
445   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
446   printSOImm(O, V1, VerboseAsm, MAI);
447   O << "\n\torr";
448   printPredicateOperand(MI, 2);
449   O << "\t";
450   printOperand(MI, 0);
451   O << ", ";
452   printOperand(MI, 0);
453   O << ", ";
454   printSOImm(O, V2, VerboseAsm, MAI);
455 }
456
457 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
458 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
459 //    REG 0   0           - e.g. R5
460 //    REG REG 0,SH_OPC    - e.g. R5, ROR R3
461 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
462 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
463   const MachineOperand &MO1 = MI->getOperand(Op);
464   const MachineOperand &MO2 = MI->getOperand(Op+1);
465   const MachineOperand &MO3 = MI->getOperand(Op+2);
466
467   O << getRegisterName(MO1.getReg());
468
469   // Print the shift opc.
470   O << ", "
471     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
472     << " ";
473
474   if (MO2.getReg()) {
475     O << getRegisterName(MO2.getReg());
476     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
477   } else {
478     O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
479   }
480 }
481
482 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
483   const MachineOperand &MO1 = MI->getOperand(Op);
484   const MachineOperand &MO2 = MI->getOperand(Op+1);
485   const MachineOperand &MO3 = MI->getOperand(Op+2);
486
487   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
488     printOperand(MI, Op);
489     return;
490   }
491
492   O << "[" << getRegisterName(MO1.getReg());
493
494   if (!MO2.getReg()) {
495     if (ARM_AM::getAM2Offset(MO3.getImm()))  // Don't print +0.
496       O << ", #"
497         << (char)ARM_AM::getAM2Op(MO3.getImm())
498         << ARM_AM::getAM2Offset(MO3.getImm());
499     O << "]";
500     return;
501   }
502
503   O << ", "
504     << (char)ARM_AM::getAM2Op(MO3.getImm())
505     << getRegisterName(MO2.getReg());
506
507   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
508     O << ", "
509       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
510       << " #" << ShImm;
511   O << "]";
512 }
513
514 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
515   const MachineOperand &MO1 = MI->getOperand(Op);
516   const MachineOperand &MO2 = MI->getOperand(Op+1);
517
518   if (!MO1.getReg()) {
519     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
520     assert(ImmOffs && "Malformed indexed load / store!");
521     O << "#"
522       << (char)ARM_AM::getAM2Op(MO2.getImm())
523       << ImmOffs;
524     return;
525   }
526
527   O << (char)ARM_AM::getAM2Op(MO2.getImm())
528     << getRegisterName(MO1.getReg());
529
530   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
531     O << ", "
532       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
533       << " #" << ShImm;
534 }
535
536 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
537   const MachineOperand &MO1 = MI->getOperand(Op);
538   const MachineOperand &MO2 = MI->getOperand(Op+1);
539   const MachineOperand &MO3 = MI->getOperand(Op+2);
540
541   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
542   O << "[" << getRegisterName(MO1.getReg());
543
544   if (MO2.getReg()) {
545     O << ", "
546       << (char)ARM_AM::getAM3Op(MO3.getImm())
547       << getRegisterName(MO2.getReg())
548       << "]";
549     return;
550   }
551
552   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
553     O << ", #"
554       << (char)ARM_AM::getAM3Op(MO3.getImm())
555       << ImmOffs;
556   O << "]";
557 }
558
559 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
560   const MachineOperand &MO1 = MI->getOperand(Op);
561   const MachineOperand &MO2 = MI->getOperand(Op+1);
562
563   if (MO1.getReg()) {
564     O << (char)ARM_AM::getAM3Op(MO2.getImm())
565       << getRegisterName(MO1.getReg());
566     return;
567   }
568
569   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
570   assert(ImmOffs && "Malformed indexed load / store!");
571   O << "#"
572     << (char)ARM_AM::getAM3Op(MO2.getImm())
573     << ImmOffs;
574 }
575
576 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
577                                           const char *Modifier) {
578   const MachineOperand &MO1 = MI->getOperand(Op);
579   const MachineOperand &MO2 = MI->getOperand(Op+1);
580   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
581   if (Modifier && strcmp(Modifier, "submode") == 0) {
582     if (MO1.getReg() == ARM::SP) {
583       // FIXME
584       bool isLDM = (MI->getOpcode() == ARM::LDM ||
585                     MI->getOpcode() == ARM::LDM_RET ||
586                     MI->getOpcode() == ARM::t2LDM ||
587                     MI->getOpcode() == ARM::t2LDM_RET);
588       O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
589     } else
590       O << ARM_AM::getAMSubModeStr(Mode);
591   } else if (Modifier && strcmp(Modifier, "wide") == 0) {
592     ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
593     if (Mode == ARM_AM::ia)
594       O << ".w";
595   } else {
596     printOperand(MI, Op);
597     if (ARM_AM::getAM4WBFlag(MO2.getImm()))
598       O << "!";
599   }
600 }
601
602 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
603                                           const char *Modifier) {
604   const MachineOperand &MO1 = MI->getOperand(Op);
605   const MachineOperand &MO2 = MI->getOperand(Op+1);
606
607   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
608     printOperand(MI, Op);
609     return;
610   }
611
612   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
613
614   if (Modifier && strcmp(Modifier, "submode") == 0) {
615     ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
616     O << ARM_AM::getAMSubModeStr(Mode);
617     return;
618   } else if (Modifier && strcmp(Modifier, "base") == 0) {
619     // Used for FSTM{D|S} and LSTM{D|S} operations.
620     O << getRegisterName(MO1.getReg());
621     if (ARM_AM::getAM5WBFlag(MO2.getImm()))
622       O << "!";
623     return;
624   }
625
626   O << "[" << getRegisterName(MO1.getReg());
627
628   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
629     O << ", #"
630       << (char)ARM_AM::getAM5Op(MO2.getImm())
631       << ImmOffs*4;
632   }
633   O << "]";
634 }
635
636 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op) {
637   const MachineOperand &MO1 = MI->getOperand(Op);
638   const MachineOperand &MO2 = MI->getOperand(Op+1);
639   const MachineOperand &MO3 = MI->getOperand(Op+2);
640   const MachineOperand &MO4 = MI->getOperand(Op+3);
641
642   O << "[" << getRegisterName(MO1.getReg());
643   if (MO4.getImm()) {
644     // FIXME: Both darwin as and GNU as violate ARM docs here.
645     O << ", :" << MO4.getImm();
646   }
647   O << "]";
648
649   if (ARM_AM::getAM6WBFlag(MO3.getImm())) {
650     if (MO2.getReg() == 0)
651       O << "!";
652     else
653       O << ", " << getRegisterName(MO2.getReg());
654   }
655 }
656
657 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
658                                            const char *Modifier) {
659   if (Modifier && strcmp(Modifier, "label") == 0) {
660     printPCLabel(MI, Op+1);
661     return;
662   }
663
664   const MachineOperand &MO1 = MI->getOperand(Op);
665   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
666   O << "[pc, +" << getRegisterName(MO1.getReg()) << "]";
667 }
668
669 void
670 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
671   const MachineOperand &MO = MI->getOperand(Op);
672   uint32_t v = ~MO.getImm();
673   int32_t lsb = CountTrailingZeros_32(v);
674   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
675   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
676   O << "#" << lsb << ", #" << width;
677 }
678
679 //===--------------------------------------------------------------------===//
680
681 void ARMAsmPrinter::printThumbS4ImmOperand(const MachineInstr *MI, int Op) {
682   O << "#" <<  MI->getOperand(Op).getImm() * 4;
683 }
684
685 void
686 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
687   // (3 - the number of trailing zeros) is the number of then / else.
688   unsigned Mask = MI->getOperand(Op).getImm();
689   unsigned NumTZ = CountTrailingZeros_32(Mask);
690   assert(NumTZ <= 3 && "Invalid IT mask!");
691   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
692     bool T = (Mask & (1 << Pos)) == 0;
693     if (T)
694       O << 't';
695     else
696       O << 'e';
697   }
698 }
699
700 void
701 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
702   const MachineOperand &MO1 = MI->getOperand(Op);
703   const MachineOperand &MO2 = MI->getOperand(Op+1);
704   O << "[" << getRegisterName(MO1.getReg());
705   O << ", " << getRegisterName(MO2.getReg()) << "]";
706 }
707
708 void
709 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
710                                             unsigned Scale) {
711   const MachineOperand &MO1 = MI->getOperand(Op);
712   const MachineOperand &MO2 = MI->getOperand(Op+1);
713   const MachineOperand &MO3 = MI->getOperand(Op+2);
714
715   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
716     printOperand(MI, Op);
717     return;
718   }
719
720   O << "[" << getRegisterName(MO1.getReg());
721   if (MO3.getReg())
722     O << ", " << getRegisterName(MO3.getReg());
723   else if (unsigned ImmOffs = MO2.getImm())
724     O << ", #+" << ImmOffs * Scale;
725   O << "]";
726 }
727
728 void
729 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
730   printThumbAddrModeRI5Operand(MI, Op, 1);
731 }
732 void
733 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
734   printThumbAddrModeRI5Operand(MI, Op, 2);
735 }
736 void
737 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
738   printThumbAddrModeRI5Operand(MI, Op, 4);
739 }
740
741 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
742   const MachineOperand &MO1 = MI->getOperand(Op);
743   const MachineOperand &MO2 = MI->getOperand(Op+1);
744   O << "[" << getRegisterName(MO1.getReg());
745   if (unsigned ImmOffs = MO2.getImm())
746     O << ", #+" << ImmOffs*4;
747   O << "]";
748 }
749
750 //===--------------------------------------------------------------------===//
751
752 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
753 // register with shift forms.
754 // REG 0   0           - e.g. R5
755 // REG IMM, SH_OPC     - e.g. R5, LSL #3
756 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
757   const MachineOperand &MO1 = MI->getOperand(OpNum);
758   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
759
760   unsigned Reg = MO1.getReg();
761   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
762   O << getRegisterName(Reg);
763
764   // Print the shift opc.
765   O << ", "
766     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
767     << " ";
768
769   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
770   O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
771 }
772
773 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
774                                                 int OpNum) {
775   const MachineOperand &MO1 = MI->getOperand(OpNum);
776   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
777
778   O << "[" << getRegisterName(MO1.getReg());
779
780   unsigned OffImm = MO2.getImm();
781   if (OffImm)  // Don't print +0.
782     O << ", #+" << OffImm;
783   O << "]";
784 }
785
786 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
787                                                int OpNum) {
788   const MachineOperand &MO1 = MI->getOperand(OpNum);
789   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
790
791   O << "[" << getRegisterName(MO1.getReg());
792
793   int32_t OffImm = (int32_t)MO2.getImm();
794   // Don't print +0.
795   if (OffImm < 0)
796     O << ", #-" << -OffImm;
797   else if (OffImm > 0)
798     O << ", #+" << OffImm;
799   O << "]";
800 }
801
802 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
803                                                  int OpNum) {
804   const MachineOperand &MO1 = MI->getOperand(OpNum);
805   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
806
807   O << "[" << getRegisterName(MO1.getReg());
808
809   int32_t OffImm = (int32_t)MO2.getImm() / 4;
810   // Don't print +0.
811   if (OffImm < 0)
812     O << ", #-" << -OffImm * 4;
813   else if (OffImm > 0)
814     O << ", #+" << OffImm * 4;
815   O << "]";
816 }
817
818 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
819                                                      int OpNum) {
820   const MachineOperand &MO1 = MI->getOperand(OpNum);
821   int32_t OffImm = (int32_t)MO1.getImm();
822   // Don't print +0.
823   if (OffImm < 0)
824     O << "#-" << -OffImm;
825   else if (OffImm > 0)
826     O << "#+" << OffImm;
827 }
828
829 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
830                                                 int OpNum) {
831   const MachineOperand &MO1 = MI->getOperand(OpNum);
832   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
833   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
834
835   O << "[" << getRegisterName(MO1.getReg());
836
837   assert(MO2.getReg() && "Invalid so_reg load / store address!");
838   O << ", " << getRegisterName(MO2.getReg());
839
840   unsigned ShAmt = MO3.getImm();
841   if (ShAmt) {
842     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
843     O << ", lsl #" << ShAmt;
844   }
845   O << "]";
846 }
847
848
849 //===--------------------------------------------------------------------===//
850
851 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
852   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
853   if (CC != ARMCC::AL)
854     O << ARMCondCodeToString(CC);
855 }
856
857 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
858   unsigned Reg = MI->getOperand(OpNum).getReg();
859   if (Reg) {
860     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
861     O << 's';
862   }
863 }
864
865 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
866   int Id = (int)MI->getOperand(OpNum).getImm();
867   O << MAI->getPrivateGlobalPrefix()
868     << "PC" << getFunctionNumber() << "_" << Id;
869 }
870
871 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
872   O << "{";
873   // Always skip the first operand, it's the optional (and implicit writeback).
874   for (unsigned i = OpNum+1, e = MI->getNumOperands(); i != e; ++i) {
875     if (MI->getOperand(i).isImplicit())
876       continue;
877     if ((int)i != OpNum+1) O << ", ";
878     printOperand(MI, i);
879   }
880   O << "}";
881 }
882
883 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
884                                        const char *Modifier) {
885   assert(Modifier && "This operand only works with a modifier!");
886   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
887   // data itself.
888   if (!strcmp(Modifier, "label")) {
889     unsigned ID = MI->getOperand(OpNum).getImm();
890     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
891       << '_' << ID << ":\n";
892   } else {
893     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
894     unsigned CPI = MI->getOperand(OpNum).getIndex();
895
896     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
897
898     if (MCPE.isMachineConstantPoolEntry()) {
899       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
900     } else {
901       EmitGlobalConstant(MCPE.Val.ConstVal);
902     }
903   }
904 }
905
906 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
907   assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
908
909   const MachineOperand &MO1 = MI->getOperand(OpNum);
910   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
911   unsigned JTI = MO1.getIndex();
912   O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
913     << '_' << JTI << '_' << MO2.getImm() << ":\n";
914
915   const char *JTEntryDirective = MAI->getData32bitsDirective();
916
917   const MachineFunction *MF = MI->getParent()->getParent();
918   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
919   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
920   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
921   bool UseSet= MAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
922   SmallPtrSet<MachineBasicBlock*, 8> JTSets;
923   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
924     MachineBasicBlock *MBB = JTBBs[i];
925     bool isNew = JTSets.insert(MBB);
926
927     if (UseSet && isNew)
928       printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
929
930     O << JTEntryDirective << ' ';
931     if (UseSet)
932       O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
933         << '_' << JTI << '_' << MO2.getImm()
934         << "_set_" << MBB->getNumber();
935     else if (TM.getRelocationModel() == Reloc::PIC_) {
936       O << *GetMBBSymbol(MBB->getNumber())
937         << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
938         << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
939     } else {
940       O << *GetMBBSymbol(MBB->getNumber());
941     }
942     if (i != e-1)
943       O << '\n';
944   }
945 }
946
947 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum) {
948   const MachineOperand &MO1 = MI->getOperand(OpNum);
949   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
950   unsigned JTI = MO1.getIndex();
951   O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
952     << '_' << JTI << '_' << MO2.getImm() << ":\n";
953
954   const MachineFunction *MF = MI->getParent()->getParent();
955   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
956   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
957   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
958   bool ByteOffset = false, HalfWordOffset = false;
959   if (MI->getOpcode() == ARM::t2TBB)
960     ByteOffset = true;
961   else if (MI->getOpcode() == ARM::t2TBH)
962     HalfWordOffset = true;
963
964   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
965     MachineBasicBlock *MBB = JTBBs[i];
966     if (ByteOffset)
967       O << MAI->getData8bitsDirective();
968     else if (HalfWordOffset)
969       O << MAI->getData16bitsDirective();
970     if (ByteOffset || HalfWordOffset) {
971       O << '(' << *GetMBBSymbol(MBB->getNumber());
972       O << "-" << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
973         << '_' << JTI << '_' << MO2.getImm() << ")/2";
974     } else {
975       O << "\tb.w " << *GetMBBSymbol(MBB->getNumber());
976     }
977     if (i != e-1)
978       O << '\n';
979   }
980
981   // Make sure the instruction that follows TBB is 2-byte aligned.
982   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
983   if (ByteOffset && (JTBBs.size() & 1)) {
984     O << '\n';
985     EmitAlignment(1);
986   }
987 }
988
989 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum) {
990   O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
991   if (MI->getOpcode() == ARM::t2TBH)
992     O << ", lsl #1";
993   O << ']';
994 }
995
996 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum) {
997   O << MI->getOperand(OpNum).getImm();
998 }
999
1000 void ARMAsmPrinter::printVFPf32ImmOperand(const MachineInstr *MI, int OpNum) {
1001   const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
1002   O << '#' << FP->getValueAPF().convertToFloat();
1003   if (VerboseAsm) {
1004     O.PadToColumn(MAI->getCommentColumn());
1005     O << MAI->getCommentString() << ' ';
1006     WriteAsOperand(O, FP, /*PrintType=*/false);
1007   }
1008 }
1009
1010 void ARMAsmPrinter::printVFPf64ImmOperand(const MachineInstr *MI, int OpNum) {
1011   const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
1012   O << '#' << FP->getValueAPF().convertToDouble();
1013   if (VerboseAsm) {
1014     O.PadToColumn(MAI->getCommentColumn());
1015     O << MAI->getCommentString() << ' ';
1016     WriteAsOperand(O, FP, /*PrintType=*/false);
1017   }
1018 }
1019
1020 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
1021                                     unsigned AsmVariant, const char *ExtraCode){
1022   // Does this asm operand have a single letter operand modifier?
1023   if (ExtraCode && ExtraCode[0]) {
1024     if (ExtraCode[1] != 0) return true; // Unknown modifier.
1025
1026     switch (ExtraCode[0]) {
1027     default: return true;  // Unknown modifier.
1028     case 'a': // Print as a memory address.
1029       if (MI->getOperand(OpNum).isReg()) {
1030         O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
1031         return false;
1032       }
1033       // Fallthrough
1034     case 'c': // Don't print "#" before an immediate operand.
1035       if (!MI->getOperand(OpNum).isImm())
1036         return true;
1037       printNoHashImmediate(MI, OpNum);
1038       return false;
1039     case 'P': // Print a VFP double precision register.
1040     case 'q': // Print a NEON quad precision register.
1041       printOperand(MI, OpNum);
1042       return false;
1043     case 'Q':
1044       if (TM.getTargetData()->isLittleEndian())
1045         break;
1046       // Fallthrough
1047     case 'R':
1048       if (TM.getTargetData()->isBigEndian())
1049         break;
1050       // Fallthrough
1051     case 'H': // Write second word of DI / DF reference.
1052       // Verify that this operand has two consecutive registers.
1053       if (!MI->getOperand(OpNum).isReg() ||
1054           OpNum+1 == MI->getNumOperands() ||
1055           !MI->getOperand(OpNum+1).isReg())
1056         return true;
1057       ++OpNum;   // Return the high-part.
1058     }
1059   }
1060
1061   printOperand(MI, OpNum);
1062   return false;
1063 }
1064
1065 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1066                                           unsigned OpNum, unsigned AsmVariant,
1067                                           const char *ExtraCode) {
1068   if (ExtraCode && ExtraCode[0])
1069     return true; // Unknown modifier.
1070
1071   const MachineOperand &MO = MI->getOperand(OpNum);
1072   assert(MO.isReg() && "unexpected inline asm memory operand");
1073   O << "[" << getRegisterName(MO.getReg()) << "]";
1074   return false;
1075 }
1076
1077 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
1078   ++EmittedInsts;
1079
1080   // Call the autogenerated instruction printer routines.
1081   processDebugLoc(MI, true);
1082   
1083   if (EnableMCInst) {
1084     printInstructionThroughMCStreamer(MI);
1085   } else {
1086     int Opc = MI->getOpcode();
1087     if (Opc == ARM::CONSTPOOL_ENTRY)
1088       EmitAlignment(2);
1089     
1090     printInstruction(MI);
1091   }
1092   
1093   if (VerboseAsm)
1094     EmitComments(*MI);
1095   O << '\n';
1096   processDebugLoc(MI, false);
1097 }
1098
1099 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
1100   if (Subtarget->isTargetDarwin()) {
1101     Reloc::Model RelocM = TM.getRelocationModel();
1102     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1103       // Declare all the text sections up front (before the DWARF sections
1104       // emitted by AsmPrinter::doInitialization) so the assembler will keep
1105       // them together at the beginning of the object file.  This helps
1106       // avoid out-of-range branches that are due a fundamental limitation of
1107       // the way symbol offsets are encoded with the current Darwin ARM
1108       // relocations.
1109       TargetLoweringObjectFileMachO &TLOFMacho = 
1110         static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1111       OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1112       OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1113       OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1114       if (RelocM == Reloc::DynamicNoPIC) {
1115         const MCSection *sect =
1116           TLOFMacho.getMachOSection("__TEXT", "__symbol_stub4",
1117                                     MCSectionMachO::S_SYMBOL_STUBS,
1118                                     12, SectionKind::getText());
1119         OutStreamer.SwitchSection(sect);
1120       } else {
1121         const MCSection *sect =
1122           TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub4",
1123                                     MCSectionMachO::S_SYMBOL_STUBS,
1124                                     16, SectionKind::getText());
1125         OutStreamer.SwitchSection(sect);
1126       }
1127     }
1128   }
1129
1130   // Use unified assembler syntax.
1131   O << "\t.syntax unified\n";
1132
1133   // Emit ARM Build Attributes
1134   if (Subtarget->isTargetELF()) {
1135     // CPU Type
1136     std::string CPUString = Subtarget->getCPUString();
1137     if (CPUString != "generic")
1138       O << "\t.cpu " << CPUString << '\n';
1139
1140     // FIXME: Emit FPU type
1141     if (Subtarget->hasVFP2())
1142       O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
1143
1144     // Signal various FP modes.
1145     if (!UnsafeFPMath)
1146       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
1147         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
1148
1149     if (FiniteOnlyFPMath())
1150       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
1151     else
1152       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
1153
1154     // 8-bytes alignment stuff.
1155     O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
1156       << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
1157
1158     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
1159     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard)
1160       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_HardFP_use << ", 3\n"
1161         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_VFP_args << ", 1\n";
1162
1163     // FIXME: Should we signal R9 usage?
1164   }
1165 }
1166
1167 void ARMAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
1168   const TargetData *TD = TM.getTargetData();
1169
1170   MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
1171   
1172   Constant *C = GVar->getInitializer();
1173   const Type *Type = C->getType();
1174   unsigned Size = TD->getTypeAllocSize(Type);
1175   unsigned Align = TD->getPreferredAlignmentLog(GVar);
1176   bool isDarwin = Subtarget->isTargetDarwin();
1177
1178   printVisibility(GVarSym, GVar->getVisibility());
1179
1180   if (Subtarget->isTargetELF())
1181     O << "\t.type " << *GVarSym << ",%object\n";
1182
1183   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
1184
1185   // Handle normal common symbols.
1186   if (GVKind.isCommon()) {
1187     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
1188     
1189     O << ".comm " << *GVarSym << ',' << Size;
1190     if (MAI->getCOMMDirectiveTakesAlignment())
1191       O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1192     
1193     if (VerboseAsm) {
1194       O << "\t\t" << MAI->getCommentString() << " '";
1195       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1196       O << '\'';
1197     }
1198     O << '\n';
1199     return;
1200   }
1201   
1202   if (GVKind.isBSSLocal()) {
1203     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
1204     
1205     if (isDarwin) {
1206       O << MAI->getLCOMMDirective() << *GVarSym << ',' << Size
1207       << ',' << Align;
1208     } else if (MAI->getLCOMMDirective() != NULL) {
1209       O << MAI->getLCOMMDirective() << *GVarSym << "," << Size;
1210     } else {
1211       if (GVar->hasLocalLinkage())
1212         O << "\t.local\t" << *GVarSym << '\n';
1213       O << MAI->getCOMMDirective() << *GVarSym << "," << Size;
1214       if (MAI->getCOMMDirectiveTakesAlignment())
1215         O << "," << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1216     }
1217     if (VerboseAsm) {
1218       O.PadToColumn(MAI->getCommentColumn());
1219       O << MAI->getCommentString() << ' ';
1220       WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1221     }
1222     O << "\n";
1223     return;
1224   }
1225   
1226   const MCSection *TheSection =
1227     getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
1228   
1229   // Handle the zerofill directive on darwin, which is a special form of BSS
1230   // emission.
1231   if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
1232     // .globl _foo
1233     OutStreamer.EmitSymbolAttribute(GVarSym, MCStreamer::Global);
1234     // .zerofill __DATA, __common, _foo, 400, 5
1235     OutStreamer.EmitZerofill(TheSection, GVarSym, Size, 1 << Align);
1236     return;
1237   }
1238   
1239   OutStreamer.SwitchSection(TheSection);
1240
1241   switch (GVar->getLinkage()) {
1242   case GlobalValue::CommonLinkage:
1243   case GlobalValue::LinkOnceAnyLinkage:
1244   case GlobalValue::LinkOnceODRLinkage:
1245   case GlobalValue::WeakAnyLinkage:
1246   case GlobalValue::WeakODRLinkage:
1247   case GlobalValue::LinkerPrivateLinkage:
1248     if (isDarwin) {
1249       O << "\t.globl " << *GVarSym
1250         << "\n\t.weak_definition " << *GVarSym << "\n";
1251     } else {
1252       O << "\t.weak " << *GVarSym << "\n";
1253     }
1254     break;
1255   case GlobalValue::AppendingLinkage:
1256   // FIXME: appending linkage variables should go into a section of
1257   // their name or something.  For now, just emit them as external.
1258   case GlobalValue::ExternalLinkage:
1259     O << "\t.globl " << *GVarSym << "\n";
1260     break;
1261   case GlobalValue::PrivateLinkage:
1262   case GlobalValue::InternalLinkage:
1263     break;
1264   default:
1265     llvm_unreachable("Unknown linkage type!");
1266   }
1267
1268   EmitAlignment(Align, GVar);
1269   O << *GVarSym << ":";
1270   if (VerboseAsm) {
1271     O.PadToColumn(MAI->getCommentColumn());
1272     O << MAI->getCommentString() << ' ';
1273     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1274   }
1275   O << "\n";
1276   if (MAI->hasDotTypeDotSizeDirective())
1277     O << "\t.size " << *GVarSym << ", " << Size << "\n";
1278
1279   EmitGlobalConstant(C);
1280   O << '\n';
1281 }
1282
1283
1284 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
1285   if (Subtarget->isTargetDarwin()) {
1286     // All darwin targets use mach-o.
1287     TargetLoweringObjectFileMachO &TLOFMacho =
1288       static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1289     MachineModuleInfoMachO &MMIMacho =
1290       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1291
1292     O << '\n';
1293
1294     // Output non-lazy-pointers for external and common global variables.
1295     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1296     
1297     if (!Stubs.empty()) {
1298       // Switch with ".non_lazy_symbol_pointer" directive.
1299       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1300       EmitAlignment(2);
1301       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1302         O << *Stubs[i].first << ":\n\t.indirect_symbol ";
1303         O << *Stubs[i].second << "\n\t.long\t0\n";
1304       }
1305     }
1306
1307     Stubs = MMIMacho.GetHiddenGVStubList();
1308     if (!Stubs.empty()) {
1309       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1310       EmitAlignment(2);
1311       for (unsigned i = 0, e = Stubs.size(); i != e; ++i)
1312         O << *Stubs[i].first << ":\n\t.long " << *Stubs[i].second << "\n";
1313     }
1314
1315     // Funny Darwin hack: This flag tells the linker that no global symbols
1316     // contain code that falls through to other global symbols (e.g. the obvious
1317     // implementation of multiple entry points).  If this doesn't occur, the
1318     // linker can safely perform dead code stripping.  Since LLVM never
1319     // generates code that does this, it is always safe to set.
1320     OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1321   }
1322 }
1323
1324 //===----------------------------------------------------------------------===//
1325
1326 void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
1327   ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
1328   switch (MI->getOpcode()) {
1329   case ARM::t2MOVi32imm:
1330     assert(0 && "Should be lowered by thumb2it pass");
1331   default: break;
1332   case TargetInstrInfo::DBG_LABEL:
1333   case TargetInstrInfo::EH_LABEL:
1334   case TargetInstrInfo::GC_LABEL:
1335     printLabel(MI);
1336     return;
1337   case TargetInstrInfo::KILL:
1338     printKill(MI);
1339     return;
1340   case TargetInstrInfo::INLINEASM:
1341     printInlineAsm(MI);
1342     return;
1343   case TargetInstrInfo::IMPLICIT_DEF:
1344     printImplicitDef(MI);
1345     return;
1346   case ARM::PICADD: { // FIXME: Remove asm string from td file.
1347     // This is a pseudo op for a label + instruction sequence, which looks like:
1348     // LPC0:
1349     //     add r0, pc, r0
1350     // This adds the address of LPC0 to r0.
1351     
1352     // Emit the label.
1353     // FIXME: MOVE TO SHARED PLACE.
1354     unsigned Id = (unsigned)MI->getOperand(2).getImm();
1355     const char *Prefix = MAI->getPrivateGlobalPrefix();
1356     MCSymbol *Label =OutContext.GetOrCreateSymbol(Twine(Prefix)
1357                          + "PC" + Twine(getFunctionNumber()) + "_" + Twine(Id));
1358     OutStreamer.EmitLabel(Label);
1359     
1360     
1361     // Form and emit tha dd.
1362     MCInst AddInst;
1363     AddInst.setOpcode(ARM::ADDrr);
1364     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1365     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1366     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1367     printMCInst(&AddInst);
1368     return;
1369   }
1370   case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
1371     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1372     /// in the function.  The first operand is the ID# for this instruction, the
1373     /// second is the index into the MachineConstantPool that this is, the third
1374     /// is the size in bytes of this constant pool entry.
1375     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1376     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1377
1378     EmitAlignment(2);
1379
1380     const char *Prefix = MAI->getPrivateGlobalPrefix();
1381     MCSymbol *Label = OutContext.GetOrCreateSymbol(Twine(Prefix)+"CPI"+
1382                                                    Twine(getFunctionNumber())+
1383                                                    "_"+ Twine(LabelId));
1384     OutStreamer.EmitLabel(Label);
1385
1386     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1387     if (MCPE.isMachineConstantPoolEntry())
1388       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1389     else
1390       EmitGlobalConstant(MCPE.Val.ConstVal);
1391     
1392     return;
1393   }
1394   case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
1395     // This is a hack that lowers as a two instruction sequence.
1396     unsigned DstReg = MI->getOperand(0).getReg();
1397     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1398
1399     unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
1400     unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
1401     
1402     {
1403       MCInst TmpInst;
1404       TmpInst.setOpcode(ARM::MOVi);
1405       TmpInst.addOperand(MCOperand::CreateReg(DstReg));
1406       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
1407       
1408       // Predicate.
1409       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1410       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1411
1412       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1413       printMCInst(&TmpInst);
1414       O << '\n';
1415     }
1416
1417     {
1418       MCInst TmpInst;
1419       TmpInst.setOpcode(ARM::ORRri);
1420       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // dstreg
1421       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // inreg
1422       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
1423       // Predicate.
1424       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1425       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1426       
1427       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1428       printMCInst(&TmpInst);
1429     }
1430     return; 
1431   }
1432   case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
1433     // This is a hack that lowers as a two instruction sequence.
1434     unsigned DstReg = MI->getOperand(0).getReg();
1435     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1436     
1437     {
1438       MCInst TmpInst;
1439       TmpInst.setOpcode(ARM::MOVi16);
1440       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1441       TmpInst.addOperand(MCOperand::CreateImm(ImmVal & 65535)); // lower16(imm)
1442       
1443       // Predicate.
1444       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1445       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1446       
1447       printMCInst(&TmpInst);
1448       O << '\n';
1449     }
1450     
1451     {
1452       MCInst TmpInst;
1453       TmpInst.setOpcode(ARM::MOVTi16);
1454       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1455       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // srcreg
1456       TmpInst.addOperand(MCOperand::CreateImm(ImmVal >> 16));   // upper16(imm)
1457       
1458       // Predicate.
1459       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1460       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1461       
1462       printMCInst(&TmpInst);
1463     }
1464     
1465     return;
1466   }
1467   }
1468       
1469   MCInst TmpInst;
1470   MCInstLowering.Lower(MI, TmpInst);
1471   
1472   printMCInst(&TmpInst);
1473 }
1474
1475 //===----------------------------------------------------------------------===//
1476 // Target Registry Stuff
1477 //===----------------------------------------------------------------------===//
1478
1479 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1480                                              unsigned SyntaxVariant,
1481                                              const MCAsmInfo &MAI,
1482                                              raw_ostream &O) {
1483   if (SyntaxVariant == 0)
1484     return new ARMInstPrinter(O, MAI, false);
1485   return 0;
1486 }
1487
1488 // Force static initialization.
1489 extern "C" void LLVMInitializeARMAsmPrinter() {
1490   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1491   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1492
1493   TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1494   TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1495 }
1496