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