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