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