ARM IAS: support .movsp
[oota-llvm.git] / lib / Target / ARM / 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 "ARMAsmPrinter.h"
17 #include "ARM.h"
18 #include "ARMConstantPoolValue.h"
19 #include "ARMFPUName.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "ARMTargetMachine.h"
22 #include "ARMTargetObjectFile.h"
23 #include "InstPrinter/ARMInstPrinter.h"
24 #include "MCTargetDesc/ARMAddressingModes.h"
25 #include "MCTargetDesc/ARMMCExpr.h"
26 #include "llvm/ADT/SetVector.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
31 #include "llvm/DebugInfo.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/Mangler.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/MCAssembler.h"
39 #include "llvm/MC/MCContext.h"
40 #include "llvm/MC/MCELFStreamer.h"
41 #include "llvm/MC/MCInst.h"
42 #include "llvm/MC/MCInstBuilder.h"
43 #include "llvm/MC/MCObjectStreamer.h"
44 #include "llvm/MC/MCSectionMachO.h"
45 #include "llvm/MC/MCStreamer.h"
46 #include "llvm/MC/MCSymbol.h"
47 #include "llvm/Support/ARMBuildAttributes.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/Debug.h"
50 #include "llvm/Support/ELF.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/TargetRegistry.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include "llvm/Target/TargetMachine.h"
55 #include <cctype>
56 using namespace llvm;
57
58 /// EmitDwarfRegOp - Emit dwarf register operation.
59 void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc,
60                                    bool Indirect) const {
61   const TargetRegisterInfo *RI = TM.getRegisterInfo();
62   if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1) {
63     AsmPrinter::EmitDwarfRegOp(MLoc, Indirect);
64     return;
65   }
66   assert(MLoc.isReg() && !Indirect &&
67          "This doesn't support offset/indirection - implement it if needed");
68   unsigned Reg = MLoc.getReg();
69   if (Reg >= ARM::S0 && Reg <= ARM::S31) {
70     assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
71     // S registers are described as bit-pieces of a register
72     // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
73     // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
74
75     unsigned SReg = Reg - ARM::S0;
76     bool odd = SReg & 0x1;
77     unsigned Rx = 256 + (SReg >> 1);
78
79     OutStreamer.AddComment("DW_OP_regx for S register");
80     EmitInt8(dwarf::DW_OP_regx);
81
82     OutStreamer.AddComment(Twine(SReg));
83     EmitULEB128(Rx);
84
85     if (odd) {
86       OutStreamer.AddComment("DW_OP_bit_piece 32 32");
87       EmitInt8(dwarf::DW_OP_bit_piece);
88       EmitULEB128(32);
89       EmitULEB128(32);
90     } else {
91       OutStreamer.AddComment("DW_OP_bit_piece 32 0");
92       EmitInt8(dwarf::DW_OP_bit_piece);
93       EmitULEB128(32);
94       EmitULEB128(0);
95     }
96   } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
97     assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
98     // Q registers Q0-Q15 are described by composing two D registers together.
99     // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
100     // DW_OP_piece(8)
101
102     unsigned QReg = Reg - ARM::Q0;
103     unsigned D1 = 256 + 2 * QReg;
104     unsigned D2 = D1 + 1;
105
106     OutStreamer.AddComment("DW_OP_regx for Q register: D1");
107     EmitInt8(dwarf::DW_OP_regx);
108     EmitULEB128(D1);
109     OutStreamer.AddComment("DW_OP_piece 8");
110     EmitInt8(dwarf::DW_OP_piece);
111     EmitULEB128(8);
112
113     OutStreamer.AddComment("DW_OP_regx for Q register: D2");
114     EmitInt8(dwarf::DW_OP_regx);
115     EmitULEB128(D2);
116     OutStreamer.AddComment("DW_OP_piece 8");
117     EmitInt8(dwarf::DW_OP_piece);
118     EmitULEB128(8);
119   }
120 }
121
122 void ARMAsmPrinter::EmitFunctionBodyEnd() {
123   // Make sure to terminate any constant pools that were at the end
124   // of the function.
125   if (!InConstantPool)
126     return;
127   InConstantPool = false;
128   OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
129 }
130
131 void ARMAsmPrinter::EmitFunctionEntryLabel() {
132   if (AFI->isThumbFunction()) {
133     OutStreamer.EmitAssemblerFlag(MCAF_Code16);
134     OutStreamer.EmitThumbFunc(CurrentFnSym);
135   }
136
137   OutStreamer.EmitLabel(CurrentFnSym);
138 }
139
140 void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
141   uint64_t Size = TM.getDataLayout()->getTypeAllocSize(CV->getType());
142   assert(Size && "C++ constructor pointer had zero size!");
143
144   const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
145   assert(GV && "C++ constructor pointer was not a GlobalValue!");
146
147   const MCExpr *E = MCSymbolRefExpr::Create(getSymbol(GV),
148                                             (Subtarget->isTargetELF()
149                                              ? MCSymbolRefExpr::VK_ARM_TARGET1
150                                              : MCSymbolRefExpr::VK_None),
151                                             OutContext);
152   
153   OutStreamer.EmitValue(E, Size);
154 }
155
156 /// runOnMachineFunction - This uses the EmitInstruction()
157 /// method to print assembly for each instruction.
158 ///
159 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
160   AFI = MF.getInfo<ARMFunctionInfo>();
161   MCP = MF.getConstantPool();
162
163   return AsmPrinter::runOnMachineFunction(MF);
164 }
165
166 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
167                                  raw_ostream &O, const char *Modifier) {
168   const MachineOperand &MO = MI->getOperand(OpNum);
169   unsigned TF = MO.getTargetFlags();
170
171   switch (MO.getType()) {
172   default: llvm_unreachable("<unknown operand type>");
173   case MachineOperand::MO_Register: {
174     unsigned Reg = MO.getReg();
175     assert(TargetRegisterInfo::isPhysicalRegister(Reg));
176     assert(!MO.getSubReg() && "Subregs should be eliminated!");
177     if(ARM::GPRPairRegClass.contains(Reg)) {
178       const MachineFunction &MF = *MI->getParent()->getParent();
179       const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
180       Reg = TRI->getSubReg(Reg, ARM::gsub_0);
181     }
182     O << ARMInstPrinter::getRegisterName(Reg);
183     break;
184   }
185   case MachineOperand::MO_Immediate: {
186     int64_t Imm = MO.getImm();
187     O << '#';
188     if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
189         (TF == ARMII::MO_LO16))
190       O << ":lower16:";
191     else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
192              (TF == ARMII::MO_HI16))
193       O << ":upper16:";
194     O << Imm;
195     break;
196   }
197   case MachineOperand::MO_MachineBasicBlock:
198     O << *MO.getMBB()->getSymbol();
199     return;
200   case MachineOperand::MO_GlobalAddress: {
201     const GlobalValue *GV = MO.getGlobal();
202     if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
203         (TF & ARMII::MO_LO16))
204       O << ":lower16:";
205     else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
206              (TF & ARMII::MO_HI16))
207       O << ":upper16:";
208     O << *getSymbol(GV);
209
210     printOffset(MO.getOffset(), O);
211     if (TF == ARMII::MO_PLT)
212       O << "(PLT)";
213     break;
214   }
215   case MachineOperand::MO_ConstantPoolIndex:
216     O << *GetCPISymbol(MO.getIndex());
217     break;
218   }
219 }
220
221 //===--------------------------------------------------------------------===//
222
223 MCSymbol *ARMAsmPrinter::
224 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
225   const DataLayout *DL = TM.getDataLayout();
226   SmallString<60> Name;
227   raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "JTI"
228     << getFunctionNumber() << '_' << uid << '_' << uid2;
229   return OutContext.GetOrCreateSymbol(Name.str());
230 }
231
232
233 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const {
234   const DataLayout *DL = TM.getDataLayout();
235   SmallString<60> Name;
236   raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "SJLJEH"
237     << getFunctionNumber();
238   return OutContext.GetOrCreateSymbol(Name.str());
239 }
240
241 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
242                                     unsigned AsmVariant, const char *ExtraCode,
243                                     raw_ostream &O) {
244   // Does this asm operand have a single letter operand modifier?
245   if (ExtraCode && ExtraCode[0]) {
246     if (ExtraCode[1] != 0) return true; // Unknown modifier.
247
248     switch (ExtraCode[0]) {
249     default:
250       // See if this is a generic print operand
251       return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
252     case 'a': // Print as a memory address.
253       if (MI->getOperand(OpNum).isReg()) {
254         O << "["
255           << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
256           << "]";
257         return false;
258       }
259       // Fallthrough
260     case 'c': // Don't print "#" before an immediate operand.
261       if (!MI->getOperand(OpNum).isImm())
262         return true;
263       O << MI->getOperand(OpNum).getImm();
264       return false;
265     case 'P': // Print a VFP double precision register.
266     case 'q': // Print a NEON quad precision register.
267       printOperand(MI, OpNum, O);
268       return false;
269     case 'y': // Print a VFP single precision register as indexed double.
270       if (MI->getOperand(OpNum).isReg()) {
271         unsigned Reg = MI->getOperand(OpNum).getReg();
272         const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
273         // Find the 'd' register that has this 's' register as a sub-register,
274         // and determine the lane number.
275         for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
276           if (!ARM::DPRRegClass.contains(*SR))
277             continue;
278           bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
279           O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
280           return false;
281         }
282       }
283       return true;
284     case 'B': // Bitwise inverse of integer or symbol without a preceding #.
285       if (!MI->getOperand(OpNum).isImm())
286         return true;
287       O << ~(MI->getOperand(OpNum).getImm());
288       return false;
289     case 'L': // The low 16 bits of an immediate constant.
290       if (!MI->getOperand(OpNum).isImm())
291         return true;
292       O << (MI->getOperand(OpNum).getImm() & 0xffff);
293       return false;
294     case 'M': { // A register range suitable for LDM/STM.
295       if (!MI->getOperand(OpNum).isReg())
296         return true;
297       const MachineOperand &MO = MI->getOperand(OpNum);
298       unsigned RegBegin = MO.getReg();
299       // This takes advantage of the 2 operand-ness of ldm/stm and that we've
300       // already got the operands in registers that are operands to the
301       // inline asm statement.
302       O << "{";
303       if (ARM::GPRPairRegClass.contains(RegBegin)) {
304         const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
305         unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
306         O << ARMInstPrinter::getRegisterName(Reg0) << ", ";;
307         RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
308       }
309       O << ARMInstPrinter::getRegisterName(RegBegin);
310
311       // FIXME: The register allocator not only may not have given us the
312       // registers in sequence, but may not be in ascending registers. This
313       // will require changes in the register allocator that'll need to be
314       // propagated down here if the operands change.
315       unsigned RegOps = OpNum + 1;
316       while (MI->getOperand(RegOps).isReg()) {
317         O << ", "
318           << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
319         RegOps++;
320       }
321
322       O << "}";
323
324       return false;
325     }
326     case 'R': // The most significant register of a pair.
327     case 'Q': { // The least significant register of a pair.
328       if (OpNum == 0)
329         return true;
330       const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
331       if (!FlagsOP.isImm())
332         return true;
333       unsigned Flags = FlagsOP.getImm();
334
335       // This operand may not be the one that actually provides the register. If
336       // it's tied to a previous one then we should refer instead to that one
337       // for registers and their classes.
338       unsigned TiedIdx;
339       if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
340         for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
341           unsigned OpFlags = MI->getOperand(OpNum).getImm();
342           OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
343         }
344         Flags = MI->getOperand(OpNum).getImm();
345
346         // Later code expects OpNum to be pointing at the register rather than
347         // the flags.
348         OpNum += 1;
349       }
350
351       unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
352       unsigned RC;
353       InlineAsm::hasRegClassConstraint(Flags, RC);
354       if (RC == ARM::GPRPairRegClassID) {
355         if (NumVals != 1)
356           return true;
357         const MachineOperand &MO = MI->getOperand(OpNum);
358         if (!MO.isReg())
359           return true;
360         const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
361         unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ?
362             ARM::gsub_0 : ARM::gsub_1);
363         O << ARMInstPrinter::getRegisterName(Reg);
364         return false;
365       }
366       if (NumVals != 2)
367         return true;
368       unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
369       if (RegOp >= MI->getNumOperands())
370         return true;
371       const MachineOperand &MO = MI->getOperand(RegOp);
372       if (!MO.isReg())
373         return true;
374       unsigned Reg = MO.getReg();
375       O << ARMInstPrinter::getRegisterName(Reg);
376       return false;
377     }
378
379     case 'e': // The low doubleword register of a NEON quad register.
380     case 'f': { // The high doubleword register of a NEON quad register.
381       if (!MI->getOperand(OpNum).isReg())
382         return true;
383       unsigned Reg = MI->getOperand(OpNum).getReg();
384       if (!ARM::QPRRegClass.contains(Reg))
385         return true;
386       const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
387       unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
388                                        ARM::dsub_0 : ARM::dsub_1);
389       O << ARMInstPrinter::getRegisterName(SubReg);
390       return false;
391     }
392
393     // This modifier is not yet supported.
394     case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
395       return true;
396     case 'H': { // The highest-numbered register of a pair.
397       const MachineOperand &MO = MI->getOperand(OpNum);
398       if (!MO.isReg())
399         return true;
400       const MachineFunction &MF = *MI->getParent()->getParent();
401       const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
402       unsigned Reg = MO.getReg();
403       if(!ARM::GPRPairRegClass.contains(Reg))
404         return false;
405       Reg = TRI->getSubReg(Reg, ARM::gsub_1);
406       O << ARMInstPrinter::getRegisterName(Reg);
407       return false;
408     }
409     }
410   }
411
412   printOperand(MI, OpNum, O);
413   return false;
414 }
415
416 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
417                                           unsigned OpNum, unsigned AsmVariant,
418                                           const char *ExtraCode,
419                                           raw_ostream &O) {
420   // Does this asm operand have a single letter operand modifier?
421   if (ExtraCode && ExtraCode[0]) {
422     if (ExtraCode[1] != 0) return true; // Unknown modifier.
423
424     switch (ExtraCode[0]) {
425       case 'A': // A memory operand for a VLD1/VST1 instruction.
426       default: return true;  // Unknown modifier.
427       case 'm': // The base register of a memory operand.
428         if (!MI->getOperand(OpNum).isReg())
429           return true;
430         O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
431         return false;
432     }
433   }
434
435   const MachineOperand &MO = MI->getOperand(OpNum);
436   assert(MO.isReg() && "unexpected inline asm memory operand");
437   O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
438   return false;
439 }
440
441 static bool isThumb(const MCSubtargetInfo& STI) {
442   return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
443 }
444
445 void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
446                                      MCSubtargetInfo *EndInfo) const {
447   // If either end mode is unknown (EndInfo == NULL) or different than
448   // the start mode, then restore the start mode.
449   const bool WasThumb = isThumb(StartInfo);
450   if (EndInfo == NULL || WasThumb != isThumb(*EndInfo)) {
451     OutStreamer.EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
452     if (EndInfo)
453       EndInfo->ToggleFeature(ARM::ModeThumb);
454   }
455 }
456
457 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
458   if (Subtarget->isTargetMachO()) {
459     Reloc::Model RelocM = TM.getRelocationModel();
460     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
461       // Declare all the text sections up front (before the DWARF sections
462       // emitted by AsmPrinter::doInitialization) so the assembler will keep
463       // them together at the beginning of the object file.  This helps
464       // avoid out-of-range branches that are due a fundamental limitation of
465       // the way symbol offsets are encoded with the current Darwin ARM
466       // relocations.
467       const TargetLoweringObjectFileMachO &TLOFMacho =
468         static_cast<const TargetLoweringObjectFileMachO &>(
469           getObjFileLowering());
470
471       // Collect the set of sections our functions will go into.
472       SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
473         SmallPtrSet<const MCSection *, 8> > TextSections;
474       // Default text section comes first.
475       TextSections.insert(TLOFMacho.getTextSection());
476       // Now any user defined text sections from function attributes.
477       for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
478         if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
479           TextSections.insert(TLOFMacho.SectionForGlobal(F, Mang, TM));
480       // Now the coalescable sections.
481       TextSections.insert(TLOFMacho.getTextCoalSection());
482       TextSections.insert(TLOFMacho.getConstTextCoalSection());
483
484       // Emit the sections in the .s file header to fix the order.
485       for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
486         OutStreamer.SwitchSection(TextSections[i]);
487
488       if (RelocM == Reloc::DynamicNoPIC) {
489         const MCSection *sect =
490           OutContext.getMachOSection("__TEXT", "__symbol_stub4",
491                                      MCSectionMachO::S_SYMBOL_STUBS,
492                                      12, SectionKind::getText());
493         OutStreamer.SwitchSection(sect);
494       } else {
495         const MCSection *sect =
496           OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
497                                      MCSectionMachO::S_SYMBOL_STUBS,
498                                      16, SectionKind::getText());
499         OutStreamer.SwitchSection(sect);
500       }
501       const MCSection *StaticInitSect =
502         OutContext.getMachOSection("__TEXT", "__StaticInit",
503                                    MCSectionMachO::S_REGULAR |
504                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
505                                    SectionKind::getText());
506       OutStreamer.SwitchSection(StaticInitSect);
507     }
508
509     // Compiling with debug info should not affect the code
510     // generation.  Ensure the cstring section comes before the
511     // optional __DWARF secion. Otherwise, PC-relative loads would
512     // have to use different instruction sequences at "-g" in order to
513     // reach global data in the same object file.
514     OutStreamer.SwitchSection(getObjFileLowering().getCStringSection());
515   }
516
517   // Use unified assembler syntax.
518   OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
519
520   // Emit ARM Build Attributes
521   if (Subtarget->isTargetELF())
522     emitAttributes();
523 }
524
525
526 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
527   if (Subtarget->isTargetMachO()) {
528     // All darwin targets use mach-o.
529     const TargetLoweringObjectFileMachO &TLOFMacho =
530       static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
531     MachineModuleInfoMachO &MMIMacho =
532       MMI->getObjFileInfo<MachineModuleInfoMachO>();
533
534     // Output non-lazy-pointers for external and common global variables.
535     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
536
537     if (!Stubs.empty()) {
538       // Switch with ".non_lazy_symbol_pointer" directive.
539       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
540       EmitAlignment(2);
541       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
542         // L_foo$stub:
543         OutStreamer.EmitLabel(Stubs[i].first);
544         //   .indirect_symbol _foo
545         MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
546         OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
547
548         if (MCSym.getInt())
549           // External to current translation unit.
550           OutStreamer.EmitIntValue(0, 4/*size*/);
551         else
552           // Internal to current translation unit.
553           //
554           // When we place the LSDA into the TEXT section, the type info
555           // pointers need to be indirect and pc-rel. We accomplish this by
556           // using NLPs; however, sometimes the types are local to the file.
557           // We need to fill in the value for the NLP in those cases.
558           OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
559                                                         OutContext),
560                                 4/*size*/);
561       }
562
563       Stubs.clear();
564       OutStreamer.AddBlankLine();
565     }
566
567     Stubs = MMIMacho.GetHiddenGVStubList();
568     if (!Stubs.empty()) {
569       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
570       EmitAlignment(2);
571       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
572         // L_foo$stub:
573         OutStreamer.EmitLabel(Stubs[i].first);
574         //   .long _foo
575         OutStreamer.EmitValue(MCSymbolRefExpr::
576                               Create(Stubs[i].second.getPointer(),
577                                      OutContext),
578                               4/*size*/);
579       }
580
581       Stubs.clear();
582       OutStreamer.AddBlankLine();
583     }
584
585     // Funny Darwin hack: This flag tells the linker that no global symbols
586     // contain code that falls through to other global symbols (e.g. the obvious
587     // implementation of multiple entry points).  If this doesn't occur, the
588     // linker can safely perform dead code stripping.  Since LLVM never
589     // generates code that does this, it is always safe to set.
590     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
591   }
592 }
593
594 //===----------------------------------------------------------------------===//
595 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
596 // FIXME:
597 // The following seem like one-off assembler flags, but they actually need
598 // to appear in the .ARM.attributes section in ELF.
599 // Instead of subclassing the MCELFStreamer, we do the work here.
600
601 static ARMBuildAttrs::CPUArch getArchForCPU(StringRef CPU,
602                                             const ARMSubtarget *Subtarget) {
603   if (CPU == "xscale")
604     return ARMBuildAttrs::v5TEJ;
605
606   if (Subtarget->hasV8Ops())
607     return ARMBuildAttrs::v8;
608   else if (Subtarget->hasV7Ops()) {
609     if (Subtarget->isMClass() && Subtarget->hasThumb2DSP())
610       return ARMBuildAttrs::v7E_M;
611     return ARMBuildAttrs::v7;
612   } else if (Subtarget->hasV6T2Ops())
613     return ARMBuildAttrs::v6T2;
614   else if (Subtarget->hasV6MOps())
615     return ARMBuildAttrs::v6S_M;
616   else if (Subtarget->hasV6Ops())
617     return ARMBuildAttrs::v6;
618   else if (Subtarget->hasV5TEOps())
619     return ARMBuildAttrs::v5TE;
620   else if (Subtarget->hasV5TOps())
621     return ARMBuildAttrs::v5T;
622   else if (Subtarget->hasV4TOps())
623     return ARMBuildAttrs::v4T;
624   else
625     return ARMBuildAttrs::v4;
626 }
627
628 void ARMAsmPrinter::emitAttributes() {
629   MCTargetStreamer &TS = *OutStreamer.getTargetStreamer();
630   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
631
632   ATS.switchVendor("aeabi");
633
634   std::string CPUString = Subtarget->getCPUString();
635
636   // FIXME: remove krait check when GNU tools support krait cpu
637   if (CPUString != "generic" && CPUString != "krait")
638     ATS.emitTextAttribute(ARMBuildAttrs::CPU_name, CPUString);
639
640   ATS.emitAttribute(ARMBuildAttrs::CPU_arch,
641                     getArchForCPU(CPUString, Subtarget));
642
643   // Tag_CPU_arch_profile must have the default value of 0 when "Architecture
644   // profile is not applicable (e.g. pre v7, or cross-profile code)". 
645   if (Subtarget->hasV7Ops()) {
646     if (Subtarget->isAClass()) {
647       ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
648                         ARMBuildAttrs::ApplicationProfile);
649     } else if (Subtarget->isRClass()) {
650       ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
651                         ARMBuildAttrs::RealTimeProfile);
652     } else if (Subtarget->isMClass()) {
653       ATS.emitAttribute(ARMBuildAttrs::CPU_arch_profile,
654                         ARMBuildAttrs::MicroControllerProfile);
655     }
656   }
657
658   ATS.emitAttribute(ARMBuildAttrs::ARM_ISA_use, Subtarget->hasARMOps() ?
659                       ARMBuildAttrs::Allowed : ARMBuildAttrs::Not_Allowed);
660   if (Subtarget->isThumb1Only()) {
661     ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
662                       ARMBuildAttrs::Allowed);
663   } else if (Subtarget->hasThumb2()) {
664     ATS.emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
665                       ARMBuildAttrs::AllowThumb32);
666   }
667
668   if (Subtarget->hasNEON()) {
669     /* NEON is not exactly a VFP architecture, but GAS emit one of
670      * neon/neon-fp-armv8/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
671     if (Subtarget->hasFPARMv8()) {
672       if (Subtarget->hasCrypto())
673         ATS.emitFPU(ARM::CRYPTO_NEON_FP_ARMV8);
674       else
675         ATS.emitFPU(ARM::NEON_FP_ARMV8);
676     }
677     else if (Subtarget->hasVFP4())
678       ATS.emitFPU(ARM::NEON_VFPV4);
679     else
680       ATS.emitFPU(ARM::NEON);
681     // Emit Tag_Advanced_SIMD_arch for ARMv8 architecture
682     if (Subtarget->hasV8Ops())
683       ATS.emitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
684                         ARMBuildAttrs::AllowNeonARMv8);
685   } else {
686     if (Subtarget->hasFPARMv8())
687       ATS.emitFPU(ARM::FP_ARMV8);
688     else if (Subtarget->hasVFP4())
689       ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV4_D16 : ARM::VFPV4);
690     else if (Subtarget->hasVFP3())
691       ATS.emitFPU(Subtarget->hasD16() ? ARM::VFPV3_D16 : ARM::VFPV3);
692     else if (Subtarget->hasVFP2())
693       ATS.emitFPU(ARM::VFPV2);
694   }
695
696   // Signal various FP modes.
697   if (!TM.Options.UnsafeFPMath) {
698     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, ARMBuildAttrs::Allowed);
699     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
700                       ARMBuildAttrs::Allowed);
701   }
702
703   if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
704     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
705                       ARMBuildAttrs::Allowed);
706   else
707     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
708                       ARMBuildAttrs::AllowIEE754);
709
710   // FIXME: add more flags to ARMBuildAttributes.h
711   // 8-bytes alignment stuff.
712   ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1);
713   ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1);
714
715   // ABI_HardFP_use attribute to indicate single precision FP.
716   if (Subtarget->isFPOnlySP())
717     ATS.emitAttribute(ARMBuildAttrs::ABI_HardFP_use,
718                       ARMBuildAttrs::HardFPSinglePrecision);
719
720   // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
721   if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
722     ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
723
724   // FIXME: Should we signal R9 usage?
725
726   if (Subtarget->hasFP16())
727       ATS.emitAttribute(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP);
728
729   if (Subtarget->hasMPExtension())
730       ATS.emitAttribute(ARMBuildAttrs::MPextension_use, ARMBuildAttrs::AllowMP);
731
732   // Hardware divide in ARM mode is part of base arch, starting from ARMv8.
733   // If only Thumb hwdiv is present, it must also be in base arch (ARMv7-R/M).
734   // It is not possible to produce DisallowDIV: if hwdiv is present in the base
735   // arch, supplying -hwdiv downgrades the effective arch, via ClearImpliedBits.
736   // AllowDIVExt is only emitted if hwdiv isn't available in the base arch;
737   // otherwise, the default value (AllowDIVIfExists) applies.
738   if (Subtarget->hasDivideInARMMode() && !Subtarget->hasV8Ops())
739       ATS.emitAttribute(ARMBuildAttrs::DIV_use, ARMBuildAttrs::AllowDIVExt);
740
741   if (Subtarget->hasTrustZone() && Subtarget->hasVirtualization())
742       ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
743                         ARMBuildAttrs::AllowTZVirtualization);
744   else if (Subtarget->hasTrustZone())
745       ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
746                         ARMBuildAttrs::AllowTZ);
747   else if (Subtarget->hasVirtualization())
748       ATS.emitAttribute(ARMBuildAttrs::Virtualization_use,
749                         ARMBuildAttrs::AllowVirtualization);
750
751   ATS.finishAttributeSection();
752 }
753
754 //===----------------------------------------------------------------------===//
755
756 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
757                              unsigned LabelId, MCContext &Ctx) {
758
759   MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
760                        + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
761   return Label;
762 }
763
764 static MCSymbolRefExpr::VariantKind
765 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
766   switch (Modifier) {
767   case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
768   case ARMCP::TLSGD:       return MCSymbolRefExpr::VK_TLSGD;
769   case ARMCP::TPOFF:       return MCSymbolRefExpr::VK_TPOFF;
770   case ARMCP::GOTTPOFF:    return MCSymbolRefExpr::VK_GOTTPOFF;
771   case ARMCP::GOT:         return MCSymbolRefExpr::VK_GOT;
772   case ARMCP::GOTOFF:      return MCSymbolRefExpr::VK_GOTOFF;
773   }
774   llvm_unreachable("Invalid ARMCPModifier!");
775 }
776
777 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
778                                         unsigned char TargetFlags) {
779   bool isIndirect = Subtarget->isTargetMachO() &&
780     (TargetFlags & ARMII::MO_NONLAZY) &&
781     Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
782   if (!isIndirect)
783     return getSymbol(GV);
784
785   // FIXME: Remove this when Darwin transition to @GOT like syntax.
786   MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
787   MachineModuleInfoMachO &MMIMachO =
788     MMI->getObjFileInfo<MachineModuleInfoMachO>();
789   MachineModuleInfoImpl::StubValueTy &StubSym =
790     GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
791     MMIMachO.getGVStubEntry(MCSym);
792   if (StubSym.getPointer() == 0)
793     StubSym = MachineModuleInfoImpl::
794       StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
795   return MCSym;
796 }
797
798 void ARMAsmPrinter::
799 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
800   const DataLayout *DL = TM.getDataLayout();
801   int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
802
803   ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
804
805   MCSymbol *MCSym;
806   if (ACPV->isLSDA()) {
807     SmallString<128> Str;
808     raw_svector_ostream OS(Str);
809     OS << DL->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
810     MCSym = OutContext.GetOrCreateSymbol(OS.str());
811   } else if (ACPV->isBlockAddress()) {
812     const BlockAddress *BA =
813       cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
814     MCSym = GetBlockAddressSymbol(BA);
815   } else if (ACPV->isGlobalValue()) {
816     const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
817
818     // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
819     // flag the global as MO_NONLAZY.
820     unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
821     MCSym = GetARMGVSymbol(GV, TF);
822   } else if (ACPV->isMachineBasicBlock()) {
823     const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
824     MCSym = MBB->getSymbol();
825   } else {
826     assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
827     const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
828     MCSym = GetExternalSymbolSymbol(Sym);
829   }
830
831   // Create an MCSymbol for the reference.
832   const MCExpr *Expr =
833     MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
834                             OutContext);
835
836   if (ACPV->getPCAdjustment()) {
837     MCSymbol *PCLabel = getPICLabel(DL->getPrivateGlobalPrefix(),
838                                     getFunctionNumber(),
839                                     ACPV->getLabelId(),
840                                     OutContext);
841     const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
842     PCRelExpr =
843       MCBinaryExpr::CreateAdd(PCRelExpr,
844                               MCConstantExpr::Create(ACPV->getPCAdjustment(),
845                                                      OutContext),
846                               OutContext);
847     if (ACPV->mustAddCurrentAddress()) {
848       // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
849       // label, so just emit a local label end reference that instead.
850       MCSymbol *DotSym = OutContext.CreateTempSymbol();
851       OutStreamer.EmitLabel(DotSym);
852       const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
853       PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
854     }
855     Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
856   }
857   OutStreamer.EmitValue(Expr, Size);
858 }
859
860 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
861   unsigned Opcode = MI->getOpcode();
862   int OpNum = 1;
863   if (Opcode == ARM::BR_JTadd)
864     OpNum = 2;
865   else if (Opcode == ARM::BR_JTm)
866     OpNum = 3;
867
868   const MachineOperand &MO1 = MI->getOperand(OpNum);
869   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
870   unsigned JTI = MO1.getIndex();
871
872   // Emit a label for the jump table.
873   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
874   OutStreamer.EmitLabel(JTISymbol);
875
876   // Mark the jump table as data-in-code.
877   OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
878
879   // Emit each entry of the table.
880   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
881   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
882   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
883
884   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
885     MachineBasicBlock *MBB = JTBBs[i];
886     // Construct an MCExpr for the entry. We want a value of the form:
887     // (BasicBlockAddr - TableBeginAddr)
888     //
889     // For example, a table with entries jumping to basic blocks BB0 and BB1
890     // would look like:
891     // LJTI_0_0:
892     //    .word (LBB0 - LJTI_0_0)
893     //    .word (LBB1 - LJTI_0_0)
894     const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
895
896     if (TM.getRelocationModel() == Reloc::PIC_)
897       Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
898                                                                    OutContext),
899                                      OutContext);
900     // If we're generating a table of Thumb addresses in static relocation
901     // model, we need to add one to keep interworking correctly.
902     else if (AFI->isThumbFunction())
903       Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
904                                      OutContext);
905     OutStreamer.EmitValue(Expr, 4);
906   }
907   // Mark the end of jump table data-in-code region.
908   OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
909 }
910
911 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
912   unsigned Opcode = MI->getOpcode();
913   int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
914   const MachineOperand &MO1 = MI->getOperand(OpNum);
915   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
916   unsigned JTI = MO1.getIndex();
917
918   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
919   OutStreamer.EmitLabel(JTISymbol);
920
921   // Emit each entry of the table.
922   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
923   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
924   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
925   unsigned OffsetWidth = 4;
926   if (MI->getOpcode() == ARM::t2TBB_JT) {
927     OffsetWidth = 1;
928     // Mark the jump table as data-in-code.
929     OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
930   } else if (MI->getOpcode() == ARM::t2TBH_JT) {
931     OffsetWidth = 2;
932     // Mark the jump table as data-in-code.
933     OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
934   }
935
936   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
937     MachineBasicBlock *MBB = JTBBs[i];
938     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
939                                                       OutContext);
940     // If this isn't a TBB or TBH, the entries are direct branch instructions.
941     if (OffsetWidth == 4) {
942       EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2B)
943         .addExpr(MBBSymbolExpr)
944         .addImm(ARMCC::AL)
945         .addReg(0));
946       continue;
947     }
948     // Otherwise it's an offset from the dispatch instruction. Construct an
949     // MCExpr for the entry. We want a value of the form:
950     // (BasicBlockAddr - TableBeginAddr) / 2
951     //
952     // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
953     // would look like:
954     // LJTI_0_0:
955     //    .byte (LBB0 - LJTI_0_0) / 2
956     //    .byte (LBB1 - LJTI_0_0) / 2
957     const MCExpr *Expr =
958       MCBinaryExpr::CreateSub(MBBSymbolExpr,
959                               MCSymbolRefExpr::Create(JTISymbol, OutContext),
960                               OutContext);
961     Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
962                                    OutContext);
963     OutStreamer.EmitValue(Expr, OffsetWidth);
964   }
965   // Mark the end of jump table data-in-code region. 32-bit offsets use
966   // actual branch instructions here, so we don't mark those as a data-region
967   // at all.
968   if (OffsetWidth != 4)
969     OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
970 }
971
972 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
973   assert(MI->getFlag(MachineInstr::FrameSetup) &&
974       "Only instruction which are involved into frame setup code are allowed");
975
976   MCTargetStreamer &TS = *OutStreamer.getTargetStreamer();
977   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
978   const MachineFunction &MF = *MI->getParent()->getParent();
979   const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
980   const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
981
982   unsigned FramePtr = RegInfo->getFrameRegister(MF);
983   unsigned Opc = MI->getOpcode();
984   unsigned SrcReg, DstReg;
985
986   if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
987     // Two special cases:
988     // 1) tPUSH does not have src/dst regs.
989     // 2) for Thumb1 code we sometimes materialize the constant via constpool
990     // load. Yes, this is pretty fragile, but for now I don't see better
991     // way... :(
992     SrcReg = DstReg = ARM::SP;
993   } else {
994     SrcReg = MI->getOperand(1).getReg();
995     DstReg = MI->getOperand(0).getReg();
996   }
997
998   // Try to figure out the unwinding opcode out of src / dst regs.
999   if (MI->mayStore()) {
1000     // Register saves.
1001     assert(DstReg == ARM::SP &&
1002            "Only stack pointer as a destination reg is supported");
1003
1004     SmallVector<unsigned, 4> RegList;
1005     // Skip src & dst reg, and pred ops.
1006     unsigned StartOp = 2 + 2;
1007     // Use all the operands.
1008     unsigned NumOffset = 0;
1009
1010     switch (Opc) {
1011     default:
1012       MI->dump();
1013       llvm_unreachable("Unsupported opcode for unwinding information");
1014     case ARM::tPUSH:
1015       // Special case here: no src & dst reg, but two extra imp ops.
1016       StartOp = 2; NumOffset = 2;
1017     case ARM::STMDB_UPD:
1018     case ARM::t2STMDB_UPD:
1019     case ARM::VSTMDDB_UPD:
1020       assert(SrcReg == ARM::SP &&
1021              "Only stack pointer as a source reg is supported");
1022       for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1023            i != NumOps; ++i) {
1024         const MachineOperand &MO = MI->getOperand(i);
1025         // Actually, there should never be any impdef stuff here. Skip it
1026         // temporary to workaround PR11902.
1027         if (MO.isImplicit())
1028           continue;
1029         RegList.push_back(MO.getReg());
1030       }
1031       break;
1032     case ARM::STR_PRE_IMM:
1033     case ARM::STR_PRE_REG:
1034     case ARM::t2STR_PRE:
1035       assert(MI->getOperand(2).getReg() == ARM::SP &&
1036              "Only stack pointer as a source reg is supported");
1037       RegList.push_back(SrcReg);
1038       break;
1039     }
1040     ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1041   } else {
1042     // Changes of stack / frame pointer.
1043     if (SrcReg == ARM::SP) {
1044       int64_t Offset = 0;
1045       switch (Opc) {
1046       default:
1047         MI->dump();
1048         llvm_unreachable("Unsupported opcode for unwinding information");
1049       case ARM::MOVr:
1050       case ARM::tMOVr:
1051         Offset = 0;
1052         break;
1053       case ARM::ADDri:
1054         Offset = -MI->getOperand(2).getImm();
1055         break;
1056       case ARM::SUBri:
1057       case ARM::t2SUBri:
1058         Offset = MI->getOperand(2).getImm();
1059         break;
1060       case ARM::tSUBspi:
1061         Offset = MI->getOperand(2).getImm()*4;
1062         break;
1063       case ARM::tADDspi:
1064       case ARM::tADDrSPi:
1065         Offset = -MI->getOperand(2).getImm()*4;
1066         break;
1067       case ARM::tLDRpci: {
1068         // Grab the constpool index and check, whether it corresponds to
1069         // original or cloned constpool entry.
1070         unsigned CPI = MI->getOperand(1).getIndex();
1071         const MachineConstantPool *MCP = MF.getConstantPool();
1072         if (CPI >= MCP->getConstants().size())
1073           CPI = AFI.getOriginalCPIdx(CPI);
1074         assert(CPI != -1U && "Invalid constpool index");
1075
1076         // Derive the actual offset.
1077         const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1078         assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1079         // FIXME: Check for user, it should be "add" instruction!
1080         Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1081         break;
1082       }
1083       }
1084
1085       if (DstReg == FramePtr && FramePtr != ARM::SP)
1086         // Set-up of the frame pointer. Positive values correspond to "add"
1087         // instruction.
1088         ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
1089       else if (DstReg == ARM::SP) {
1090         // Change of SP by an offset. Positive values correspond to "sub"
1091         // instruction.
1092         ATS.emitPad(Offset);
1093       } else {
1094         // Move of SP to a register.  Positive values correspond to an "add"
1095         // instruction.
1096         ATS.emitMovSP(DstReg, -Offset);
1097       }
1098     } else if (DstReg == ARM::SP) {
1099       MI->dump();
1100       llvm_unreachable("Unsupported opcode for unwinding information");
1101     }
1102     else {
1103       MI->dump();
1104       llvm_unreachable("Unsupported opcode for unwinding information");
1105     }
1106   }
1107 }
1108
1109 extern cl::opt<bool> DisableARMEHABI;
1110
1111 // Simple pseudo-instructions have their lowering (with expansion to real
1112 // instructions) auto-generated.
1113 #include "ARMGenMCPseudoLowering.inc"
1114
1115 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1116   const DataLayout *DL = TM.getDataLayout();
1117
1118   // If we just ended a constant pool, mark it as such.
1119   if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1120     OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1121     InConstantPool = false;
1122   }
1123
1124   // Emit unwinding stuff for frame-related instructions
1125   if (Subtarget->isTargetEHABICompatible() && !DisableARMEHABI &&
1126        MI->getFlag(MachineInstr::FrameSetup))
1127     EmitUnwindingInstruction(MI);
1128
1129   // Do any auto-generated pseudo lowerings.
1130   if (emitPseudoExpansionLowering(OutStreamer, MI))
1131     return;
1132
1133   assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1134          "Pseudo flag setting opcode should be expanded early");
1135
1136   // Check for manual lowerings.
1137   unsigned Opc = MI->getOpcode();
1138   switch (Opc) {
1139   case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1140   case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1141   case ARM::LEApcrel:
1142   case ARM::tLEApcrel:
1143   case ARM::t2LEApcrel: {
1144     // FIXME: Need to also handle globals and externals
1145     MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1146     EmitToStreamer(OutStreamer, MCInstBuilder(MI->getOpcode() ==
1147                                               ARM::t2LEApcrel ? ARM::t2ADR
1148                   : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1149                      : ARM::ADR))
1150       .addReg(MI->getOperand(0).getReg())
1151       .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1152       // Add predicate operands.
1153       .addImm(MI->getOperand(2).getImm())
1154       .addReg(MI->getOperand(3).getReg()));
1155     return;
1156   }
1157   case ARM::LEApcrelJT:
1158   case ARM::tLEApcrelJT:
1159   case ARM::t2LEApcrelJT: {
1160     MCSymbol *JTIPICSymbol =
1161       GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1162                                   MI->getOperand(2).getImm());
1163     EmitToStreamer(OutStreamer, MCInstBuilder(MI->getOpcode() ==
1164                                               ARM::t2LEApcrelJT ? ARM::t2ADR
1165                   : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1166                      : ARM::ADR))
1167       .addReg(MI->getOperand(0).getReg())
1168       .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1169       // Add predicate operands.
1170       .addImm(MI->getOperand(3).getImm())
1171       .addReg(MI->getOperand(4).getReg()));
1172     return;
1173   }
1174   // Darwin call instructions are just normal call instructions with different
1175   // clobber semantics (they clobber R9).
1176   case ARM::BX_CALL: {
1177     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1178       .addReg(ARM::LR)
1179       .addReg(ARM::PC)
1180       // Add predicate operands.
1181       .addImm(ARMCC::AL)
1182       .addReg(0)
1183       // Add 's' bit operand (always reg0 for this)
1184       .addReg(0));
1185
1186     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::BX)
1187       .addReg(MI->getOperand(0).getReg()));
1188     return;
1189   }
1190   case ARM::tBX_CALL: {
1191     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1192       .addReg(ARM::LR)
1193       .addReg(ARM::PC)
1194       // Add predicate operands.
1195       .addImm(ARMCC::AL)
1196       .addReg(0));
1197
1198     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tBX)
1199       .addReg(MI->getOperand(0).getReg())
1200       // Add predicate operands.
1201       .addImm(ARMCC::AL)
1202       .addReg(0));
1203     return;
1204   }
1205   case ARM::BMOVPCRX_CALL: {
1206     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1207       .addReg(ARM::LR)
1208       .addReg(ARM::PC)
1209       // Add predicate operands.
1210       .addImm(ARMCC::AL)
1211       .addReg(0)
1212       // Add 's' bit operand (always reg0 for this)
1213       .addReg(0));
1214
1215     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1216       .addReg(ARM::PC)
1217       .addReg(MI->getOperand(0).getReg())
1218       // Add predicate operands.
1219       .addImm(ARMCC::AL)
1220       .addReg(0)
1221       // Add 's' bit operand (always reg0 for this)
1222       .addReg(0));
1223     return;
1224   }
1225   case ARM::BMOVPCB_CALL: {
1226     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVr)
1227       .addReg(ARM::LR)
1228       .addReg(ARM::PC)
1229       // Add predicate operands.
1230       .addImm(ARMCC::AL)
1231       .addReg(0)
1232       // Add 's' bit operand (always reg0 for this)
1233       .addReg(0));
1234
1235     const GlobalValue *GV = MI->getOperand(0).getGlobal();
1236     MCSymbol *GVSym = getSymbol(GV);
1237     const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1238     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::Bcc)
1239       .addExpr(GVSymExpr)
1240       // Add predicate operands.
1241       .addImm(ARMCC::AL)
1242       .addReg(0));
1243     return;
1244   }
1245   case ARM::MOVi16_ga_pcrel:
1246   case ARM::t2MOVi16_ga_pcrel: {
1247     MCInst TmpInst;
1248     TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1249     TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1250
1251     unsigned TF = MI->getOperand(1).getTargetFlags();
1252     const GlobalValue *GV = MI->getOperand(1).getGlobal();
1253     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1254     const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1255
1256     MCSymbol *LabelSym = getPICLabel(DL->getPrivateGlobalPrefix(),
1257                                      getFunctionNumber(),
1258                                      MI->getOperand(2).getImm(), OutContext);
1259     const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1260     unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1261     const MCExpr *PCRelExpr =
1262       ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1263                                       MCBinaryExpr::CreateAdd(LabelSymExpr,
1264                                       MCConstantExpr::Create(PCAdj, OutContext),
1265                                       OutContext), OutContext), OutContext);
1266       TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1267
1268     // Add predicate operands.
1269     TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1270     TmpInst.addOperand(MCOperand::CreateReg(0));
1271     // Add 's' bit operand (always reg0 for this)
1272     TmpInst.addOperand(MCOperand::CreateReg(0));
1273     EmitToStreamer(OutStreamer, TmpInst);
1274     return;
1275   }
1276   case ARM::MOVTi16_ga_pcrel:
1277   case ARM::t2MOVTi16_ga_pcrel: {
1278     MCInst TmpInst;
1279     TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1280                       ? ARM::MOVTi16 : ARM::t2MOVTi16);
1281     TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1282     TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1283
1284     unsigned TF = MI->getOperand(2).getTargetFlags();
1285     const GlobalValue *GV = MI->getOperand(2).getGlobal();
1286     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1287     const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1288
1289     MCSymbol *LabelSym = getPICLabel(DL->getPrivateGlobalPrefix(),
1290                                      getFunctionNumber(),
1291                                      MI->getOperand(3).getImm(), OutContext);
1292     const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1293     unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1294     const MCExpr *PCRelExpr =
1295         ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1296                                    MCBinaryExpr::CreateAdd(LabelSymExpr,
1297                                       MCConstantExpr::Create(PCAdj, OutContext),
1298                                           OutContext), OutContext), OutContext);
1299       TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1300     // Add predicate operands.
1301     TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1302     TmpInst.addOperand(MCOperand::CreateReg(0));
1303     // Add 's' bit operand (always reg0 for this)
1304     TmpInst.addOperand(MCOperand::CreateReg(0));
1305     EmitToStreamer(OutStreamer, TmpInst);
1306     return;
1307   }
1308   case ARM::tPICADD: {
1309     // This is a pseudo op for a label + instruction sequence, which looks like:
1310     // LPC0:
1311     //     add r0, pc
1312     // This adds the address of LPC0 to r0.
1313
1314     // Emit the label.
1315     OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1316                           getFunctionNumber(), MI->getOperand(2).getImm(),
1317                           OutContext));
1318
1319     // Form and emit the add.
1320     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tADDhirr)
1321       .addReg(MI->getOperand(0).getReg())
1322       .addReg(MI->getOperand(0).getReg())
1323       .addReg(ARM::PC)
1324       // Add predicate operands.
1325       .addImm(ARMCC::AL)
1326       .addReg(0));
1327     return;
1328   }
1329   case ARM::PICADD: {
1330     // This is a pseudo op for a label + instruction sequence, which looks like:
1331     // LPC0:
1332     //     add r0, pc, r0
1333     // This adds the address of LPC0 to r0.
1334
1335     // Emit the label.
1336     OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1337                           getFunctionNumber(), MI->getOperand(2).getImm(),
1338                           OutContext));
1339
1340     // Form and emit the add.
1341     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDrr)
1342       .addReg(MI->getOperand(0).getReg())
1343       .addReg(ARM::PC)
1344       .addReg(MI->getOperand(1).getReg())
1345       // Add predicate operands.
1346       .addImm(MI->getOperand(3).getImm())
1347       .addReg(MI->getOperand(4).getReg())
1348       // Add 's' bit operand (always reg0 for this)
1349       .addReg(0));
1350     return;
1351   }
1352   case ARM::PICSTR:
1353   case ARM::PICSTRB:
1354   case ARM::PICSTRH:
1355   case ARM::PICLDR:
1356   case ARM::PICLDRB:
1357   case ARM::PICLDRH:
1358   case ARM::PICLDRSB:
1359   case ARM::PICLDRSH: {
1360     // This is a pseudo op for a label + instruction sequence, which looks like:
1361     // LPC0:
1362     //     OP r0, [pc, r0]
1363     // The LCP0 label is referenced by a constant pool entry in order to get
1364     // a PC-relative address at the ldr instruction.
1365
1366     // Emit the label.
1367     OutStreamer.EmitLabel(getPICLabel(DL->getPrivateGlobalPrefix(),
1368                           getFunctionNumber(), MI->getOperand(2).getImm(),
1369                           OutContext));
1370
1371     // Form and emit the load
1372     unsigned Opcode;
1373     switch (MI->getOpcode()) {
1374     default:
1375       llvm_unreachable("Unexpected opcode!");
1376     case ARM::PICSTR:   Opcode = ARM::STRrs; break;
1377     case ARM::PICSTRB:  Opcode = ARM::STRBrs; break;
1378     case ARM::PICSTRH:  Opcode = ARM::STRH; break;
1379     case ARM::PICLDR:   Opcode = ARM::LDRrs; break;
1380     case ARM::PICLDRB:  Opcode = ARM::LDRBrs; break;
1381     case ARM::PICLDRH:  Opcode = ARM::LDRH; break;
1382     case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1383     case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1384     }
1385     EmitToStreamer(OutStreamer, MCInstBuilder(Opcode)
1386       .addReg(MI->getOperand(0).getReg())
1387       .addReg(ARM::PC)
1388       .addReg(MI->getOperand(1).getReg())
1389       .addImm(0)
1390       // Add predicate operands.
1391       .addImm(MI->getOperand(3).getImm())
1392       .addReg(MI->getOperand(4).getReg()));
1393
1394     return;
1395   }
1396   case ARM::CONSTPOOL_ENTRY: {
1397     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1398     /// in the function.  The first operand is the ID# for this instruction, the
1399     /// second is the index into the MachineConstantPool that this is, the third
1400     /// is the size in bytes of this constant pool entry.
1401     /// The required alignment is specified on the basic block holding this MI.
1402     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1403     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1404
1405     // If this is the first entry of the pool, mark it.
1406     if (!InConstantPool) {
1407       OutStreamer.EmitDataRegion(MCDR_DataRegion);
1408       InConstantPool = true;
1409     }
1410
1411     OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1412
1413     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1414     if (MCPE.isMachineConstantPoolEntry())
1415       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1416     else
1417       EmitGlobalConstant(MCPE.Val.ConstVal);
1418     return;
1419   }
1420   case ARM::t2BR_JT: {
1421     // Lower and emit the instruction itself, then the jump table following it.
1422     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1423       .addReg(ARM::PC)
1424       .addReg(MI->getOperand(0).getReg())
1425       // Add predicate operands.
1426       .addImm(ARMCC::AL)
1427       .addReg(0));
1428
1429     // Output the data for the jump table itself
1430     EmitJump2Table(MI);
1431     return;
1432   }
1433   case ARM::t2TBB_JT: {
1434     // Lower and emit the instruction itself, then the jump table following it.
1435     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2TBB)
1436       .addReg(ARM::PC)
1437       .addReg(MI->getOperand(0).getReg())
1438       // Add predicate operands.
1439       .addImm(ARMCC::AL)
1440       .addReg(0));
1441
1442     // Output the data for the jump table itself
1443     EmitJump2Table(MI);
1444     // Make sure the next instruction is 2-byte aligned.
1445     EmitAlignment(1);
1446     return;
1447   }
1448   case ARM::t2TBH_JT: {
1449     // Lower and emit the instruction itself, then the jump table following it.
1450     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::t2TBH)
1451       .addReg(ARM::PC)
1452       .addReg(MI->getOperand(0).getReg())
1453       // Add predicate operands.
1454       .addImm(ARMCC::AL)
1455       .addReg(0));
1456
1457     // Output the data for the jump table itself
1458     EmitJump2Table(MI);
1459     return;
1460   }
1461   case ARM::tBR_JTr:
1462   case ARM::BR_JTr: {
1463     // Lower and emit the instruction itself, then the jump table following it.
1464     // mov pc, target
1465     MCInst TmpInst;
1466     unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1467       ARM::MOVr : ARM::tMOVr;
1468     TmpInst.setOpcode(Opc);
1469     TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1470     TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1471     // Add predicate operands.
1472     TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1473     TmpInst.addOperand(MCOperand::CreateReg(0));
1474     // Add 's' bit operand (always reg0 for this)
1475     if (Opc == ARM::MOVr)
1476       TmpInst.addOperand(MCOperand::CreateReg(0));
1477     EmitToStreamer(OutStreamer, TmpInst);
1478
1479     // Make sure the Thumb jump table is 4-byte aligned.
1480     if (Opc == ARM::tMOVr)
1481       EmitAlignment(2);
1482
1483     // Output the data for the jump table itself
1484     EmitJumpTable(MI);
1485     return;
1486   }
1487   case ARM::BR_JTm: {
1488     // Lower and emit the instruction itself, then the jump table following it.
1489     // ldr pc, target
1490     MCInst TmpInst;
1491     if (MI->getOperand(1).getReg() == 0) {
1492       // literal offset
1493       TmpInst.setOpcode(ARM::LDRi12);
1494       TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1495       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1496       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1497     } else {
1498       TmpInst.setOpcode(ARM::LDRrs);
1499       TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1500       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1501       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1502       TmpInst.addOperand(MCOperand::CreateImm(0));
1503     }
1504     // Add predicate operands.
1505     TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1506     TmpInst.addOperand(MCOperand::CreateReg(0));
1507     EmitToStreamer(OutStreamer, TmpInst);
1508
1509     // Output the data for the jump table itself
1510     EmitJumpTable(MI);
1511     return;
1512   }
1513   case ARM::BR_JTadd: {
1514     // Lower and emit the instruction itself, then the jump table following it.
1515     // add pc, target, idx
1516     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDrr)
1517       .addReg(ARM::PC)
1518       .addReg(MI->getOperand(0).getReg())
1519       .addReg(MI->getOperand(1).getReg())
1520       // Add predicate operands.
1521       .addImm(ARMCC::AL)
1522       .addReg(0)
1523       // Add 's' bit operand (always reg0 for this)
1524       .addReg(0));
1525
1526     // Output the data for the jump table itself
1527     EmitJumpTable(MI);
1528     return;
1529   }
1530   case ARM::TRAP: {
1531     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1532     // FIXME: Remove this special case when they do.
1533     if (!Subtarget->isTargetMachO()) {
1534       //.long 0xe7ffdefe @ trap
1535       uint32_t Val = 0xe7ffdefeUL;
1536       OutStreamer.AddComment("trap");
1537       OutStreamer.EmitIntValue(Val, 4);
1538       return;
1539     }
1540     break;
1541   }
1542   case ARM::TRAPNaCl: {
1543     //.long 0xe7fedef0 @ trap
1544     uint32_t Val = 0xe7fedef0UL;
1545     OutStreamer.AddComment("trap");
1546     OutStreamer.EmitIntValue(Val, 4);
1547     return;
1548   }
1549   case ARM::tTRAP: {
1550     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1551     // FIXME: Remove this special case when they do.
1552     if (!Subtarget->isTargetMachO()) {
1553       //.short 57086 @ trap
1554       uint16_t Val = 0xdefe;
1555       OutStreamer.AddComment("trap");
1556       OutStreamer.EmitIntValue(Val, 2);
1557       return;
1558     }
1559     break;
1560   }
1561   case ARM::t2Int_eh_sjlj_setjmp:
1562   case ARM::t2Int_eh_sjlj_setjmp_nofp:
1563   case ARM::tInt_eh_sjlj_setjmp: {
1564     // Two incoming args: GPR:$src, GPR:$val
1565     // mov $val, pc
1566     // adds $val, #7
1567     // str $val, [$src, #4]
1568     // movs r0, #0
1569     // b 1f
1570     // movs r0, #1
1571     // 1:
1572     unsigned SrcReg = MI->getOperand(0).getReg();
1573     unsigned ValReg = MI->getOperand(1).getReg();
1574     MCSymbol *Label = GetARMSJLJEHLabel();
1575     OutStreamer.AddComment("eh_setjmp begin");
1576     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1577       .addReg(ValReg)
1578       .addReg(ARM::PC)
1579       // Predicate.
1580       .addImm(ARMCC::AL)
1581       .addReg(0));
1582
1583     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tADDi3)
1584       .addReg(ValReg)
1585       // 's' bit operand
1586       .addReg(ARM::CPSR)
1587       .addReg(ValReg)
1588       .addImm(7)
1589       // Predicate.
1590       .addImm(ARMCC::AL)
1591       .addReg(0));
1592
1593     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tSTRi)
1594       .addReg(ValReg)
1595       .addReg(SrcReg)
1596       // The offset immediate is #4. The operand value is scaled by 4 for the
1597       // tSTR instruction.
1598       .addImm(1)
1599       // Predicate.
1600       .addImm(ARMCC::AL)
1601       .addReg(0));
1602
1603     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVi8)
1604       .addReg(ARM::R0)
1605       .addReg(ARM::CPSR)
1606       .addImm(0)
1607       // Predicate.
1608       .addImm(ARMCC::AL)
1609       .addReg(0));
1610
1611     const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1612     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tB)
1613       .addExpr(SymbolExpr)
1614       .addImm(ARMCC::AL)
1615       .addReg(0));
1616
1617     OutStreamer.AddComment("eh_setjmp end");
1618     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVi8)
1619       .addReg(ARM::R0)
1620       .addReg(ARM::CPSR)
1621       .addImm(1)
1622       // Predicate.
1623       .addImm(ARMCC::AL)
1624       .addReg(0));
1625
1626     OutStreamer.EmitLabel(Label);
1627     return;
1628   }
1629
1630   case ARM::Int_eh_sjlj_setjmp_nofp:
1631   case ARM::Int_eh_sjlj_setjmp: {
1632     // Two incoming args: GPR:$src, GPR:$val
1633     // add $val, pc, #8
1634     // str $val, [$src, #+4]
1635     // mov r0, #0
1636     // add pc, pc, #0
1637     // mov r0, #1
1638     unsigned SrcReg = MI->getOperand(0).getReg();
1639     unsigned ValReg = MI->getOperand(1).getReg();
1640
1641     OutStreamer.AddComment("eh_setjmp begin");
1642     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDri)
1643       .addReg(ValReg)
1644       .addReg(ARM::PC)
1645       .addImm(8)
1646       // Predicate.
1647       .addImm(ARMCC::AL)
1648       .addReg(0)
1649       // 's' bit operand (always reg0 for this).
1650       .addReg(0));
1651
1652     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::STRi12)
1653       .addReg(ValReg)
1654       .addReg(SrcReg)
1655       .addImm(4)
1656       // Predicate.
1657       .addImm(ARMCC::AL)
1658       .addReg(0));
1659
1660     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVi)
1661       .addReg(ARM::R0)
1662       .addImm(0)
1663       // Predicate.
1664       .addImm(ARMCC::AL)
1665       .addReg(0)
1666       // 's' bit operand (always reg0 for this).
1667       .addReg(0));
1668
1669     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::ADDri)
1670       .addReg(ARM::PC)
1671       .addReg(ARM::PC)
1672       .addImm(0)
1673       // Predicate.
1674       .addImm(ARMCC::AL)
1675       .addReg(0)
1676       // 's' bit operand (always reg0 for this).
1677       .addReg(0));
1678
1679     OutStreamer.AddComment("eh_setjmp end");
1680     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::MOVi)
1681       .addReg(ARM::R0)
1682       .addImm(1)
1683       // Predicate.
1684       .addImm(ARMCC::AL)
1685       .addReg(0)
1686       // 's' bit operand (always reg0 for this).
1687       .addReg(0));
1688     return;
1689   }
1690   case ARM::Int_eh_sjlj_longjmp: {
1691     // ldr sp, [$src, #8]
1692     // ldr $scratch, [$src, #4]
1693     // ldr r7, [$src]
1694     // bx $scratch
1695     unsigned SrcReg = MI->getOperand(0).getReg();
1696     unsigned ScratchReg = MI->getOperand(1).getReg();
1697     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1698       .addReg(ARM::SP)
1699       .addReg(SrcReg)
1700       .addImm(8)
1701       // Predicate.
1702       .addImm(ARMCC::AL)
1703       .addReg(0));
1704
1705     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1706       .addReg(ScratchReg)
1707       .addReg(SrcReg)
1708       .addImm(4)
1709       // Predicate.
1710       .addImm(ARMCC::AL)
1711       .addReg(0));
1712
1713     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::LDRi12)
1714       .addReg(ARM::R7)
1715       .addReg(SrcReg)
1716       .addImm(0)
1717       // Predicate.
1718       .addImm(ARMCC::AL)
1719       .addReg(0));
1720
1721     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::BX)
1722       .addReg(ScratchReg)
1723       // Predicate.
1724       .addImm(ARMCC::AL)
1725       .addReg(0));
1726     return;
1727   }
1728   case ARM::tInt_eh_sjlj_longjmp: {
1729     // ldr $scratch, [$src, #8]
1730     // mov sp, $scratch
1731     // ldr $scratch, [$src, #4]
1732     // ldr r7, [$src]
1733     // bx $scratch
1734     unsigned SrcReg = MI->getOperand(0).getReg();
1735     unsigned ScratchReg = MI->getOperand(1).getReg();
1736     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1737       .addReg(ScratchReg)
1738       .addReg(SrcReg)
1739       // The offset immediate is #8. The operand value is scaled by 4 for the
1740       // tLDR instruction.
1741       .addImm(2)
1742       // Predicate.
1743       .addImm(ARMCC::AL)
1744       .addReg(0));
1745
1746     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tMOVr)
1747       .addReg(ARM::SP)
1748       .addReg(ScratchReg)
1749       // Predicate.
1750       .addImm(ARMCC::AL)
1751       .addReg(0));
1752
1753     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1754       .addReg(ScratchReg)
1755       .addReg(SrcReg)
1756       .addImm(1)
1757       // Predicate.
1758       .addImm(ARMCC::AL)
1759       .addReg(0));
1760
1761     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tLDRi)
1762       .addReg(ARM::R7)
1763       .addReg(SrcReg)
1764       .addImm(0)
1765       // Predicate.
1766       .addImm(ARMCC::AL)
1767       .addReg(0));
1768
1769     EmitToStreamer(OutStreamer, MCInstBuilder(ARM::tBX)
1770       .addReg(ScratchReg)
1771       // Predicate.
1772       .addImm(ARMCC::AL)
1773       .addReg(0));
1774     return;
1775   }
1776   }
1777
1778   MCInst TmpInst;
1779   LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1780
1781   EmitToStreamer(OutStreamer, TmpInst);
1782 }
1783
1784 //===----------------------------------------------------------------------===//
1785 // Target Registry Stuff
1786 //===----------------------------------------------------------------------===//
1787
1788 // Force static initialization.
1789 extern "C" void LLVMInitializeARMAsmPrinter() {
1790   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1791   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1792 }