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 #include "ARMAsmPrinter.h"
16 #include "ARM.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMTargetMachine.h"
20 #include "ARMTargetObjectFile.h"
21 #include "InstPrinter/ARMInstPrinter.h"
22 #include "MCTargetDesc/ARMAddressingModes.h"
23 #include "MCTargetDesc/ARMMCExpr.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/BinaryFormat/COFF.h"
27 #include "llvm/BinaryFormat/ELF.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/DebugInfo.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/Debug.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/TargetParser.h"
51 #include "llvm/Support/TargetRegistry.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include <cctype>
55 using namespace llvm;
56 
57 #define DEBUG_TYPE "asm-printer"
58 
59 ARMAsmPrinter::ARMAsmPrinter(TargetMachine &TM,
60                              std::unique_ptr<MCStreamer> Streamer)
61     : AsmPrinter(TM, std::move(Streamer)), AFI(nullptr), MCP(nullptr),
62       InConstantPool(false), OptimizationGoals(-1) {}
63 
64 void ARMAsmPrinter::EmitFunctionBodyEnd() {
65   // Make sure to terminate any constant pools that were at the end
66   // of the function.
67   if (!InConstantPool)
68     return;
69   InConstantPool = false;
70   OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
71 }
72 
73 void ARMAsmPrinter::EmitFunctionEntryLabel() {
74   if (AFI->isThumbFunction()) {
75     OutStreamer->EmitAssemblerFlag(MCAF_Code16);
76     OutStreamer->EmitThumbFunc(CurrentFnSym);
77   } else {
78     OutStreamer->EmitAssemblerFlag(MCAF_Code32);
79   }
80   OutStreamer->EmitLabel(CurrentFnSym);
81 }
82 
83 void ARMAsmPrinter::EmitXXStructor(const DataLayout &DL, const Constant *CV) {
84   uint64_t Size = getDataLayout().getTypeAllocSize(CV->getType());
85   assert(Size && "C++ constructor pointer had zero size!");
86 
87   const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
88   assert(GV && "C++ constructor pointer was not a GlobalValue!");
89 
90   const MCExpr *E = MCSymbolRefExpr::create(GetARMGVSymbol(GV,
91                                                            ARMII::MO_NO_FLAG),
92                                             (Subtarget->isTargetELF()
93                                              ? MCSymbolRefExpr::VK_ARM_TARGET1
94                                              : MCSymbolRefExpr::VK_None),
95                                             OutContext);
96 
97   OutStreamer->EmitValue(E, Size);
98 }
99 
100 void ARMAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
101   if (PromotedGlobals.count(GV))
102     // The global was promoted into a constant pool. It should not be emitted.
103     return;
104   AsmPrinter::EmitGlobalVariable(GV);
105 }
106 
107 /// runOnMachineFunction - This uses the EmitInstruction()
108 /// method to print assembly for each instruction.
109 ///
110 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
111   AFI = MF.getInfo<ARMFunctionInfo>();
112   MCP = MF.getConstantPool();
113   Subtarget = &MF.getSubtarget<ARMSubtarget>();
114 
115   SetupMachineFunction(MF);
116   const Function* F = MF.getFunction();
117   const TargetMachine& TM = MF.getTarget();
118 
119   // Collect all globals that had their storage promoted to a constant pool.
120   // Functions are emitted before variables, so this accumulates promoted
121   // globals from all functions in PromotedGlobals.
122   for (auto *GV : AFI->getGlobalsPromotedToConstantPool())
123     PromotedGlobals.insert(GV);
124 
125   // Calculate this function's optimization goal.
126   unsigned OptimizationGoal;
127   if (F->hasFnAttribute(Attribute::OptimizeNone))
128     // For best debugging illusion, speed and small size sacrificed
129     OptimizationGoal = 6;
130   else if (F->optForMinSize())
131     // Aggressively for small size, speed and debug illusion sacrificed
132     OptimizationGoal = 4;
133   else if (F->optForSize())
134     // For small size, but speed and debugging illusion preserved
135     OptimizationGoal = 3;
136   else if (TM.getOptLevel() == CodeGenOpt::Aggressive)
137     // Aggressively for speed, small size and debug illusion sacrificed
138     OptimizationGoal = 2;
139   else if (TM.getOptLevel() > CodeGenOpt::None)
140     // For speed, but small size and good debug illusion preserved
141     OptimizationGoal = 1;
142   else // TM.getOptLevel() == CodeGenOpt::None
143     // For good debugging, but speed and small size preserved
144     OptimizationGoal = 5;
145 
146   // Combine a new optimization goal with existing ones.
147   if (OptimizationGoals == -1) // uninitialized goals
148     OptimizationGoals = OptimizationGoal;
149   else if (OptimizationGoals != (int)OptimizationGoal) // conflicting goals
150     OptimizationGoals = 0;
151 
152   if (Subtarget->isTargetCOFF()) {
153     bool Internal = F->hasInternalLinkage();
154     COFF::SymbolStorageClass Scl = Internal ? COFF::IMAGE_SYM_CLASS_STATIC
155                                             : COFF::IMAGE_SYM_CLASS_EXTERNAL;
156     int Type = COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT;
157 
158     OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
159     OutStreamer->EmitCOFFSymbolStorageClass(Scl);
160     OutStreamer->EmitCOFFSymbolType(Type);
161     OutStreamer->EndCOFFSymbolDef();
162   }
163 
164   // Emit the rest of the function body.
165   EmitFunctionBody();
166 
167   // Emit the XRay table for this function.
168   emitXRayTable();
169 
170   // If we need V4T thumb mode Register Indirect Jump pads, emit them.
171   // These are created per function, rather than per TU, since it's
172   // relatively easy to exceed the thumb branch range within a TU.
173   if (! ThumbIndirectPads.empty()) {
174     OutStreamer->EmitAssemblerFlag(MCAF_Code16);
175     EmitAlignment(1);
176     for (unsigned i = 0, e = ThumbIndirectPads.size(); i < e; i++) {
177       OutStreamer->EmitLabel(ThumbIndirectPads[i].second);
178       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX)
179         .addReg(ThumbIndirectPads[i].first)
180         // Add predicate operands.
181         .addImm(ARMCC::AL)
182         .addReg(0));
183     }
184     ThumbIndirectPads.clear();
185   }
186 
187   // We didn't modify anything.
188   return false;
189 }
190 
191 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
192                                  raw_ostream &O) {
193   const MachineOperand &MO = MI->getOperand(OpNum);
194   unsigned TF = MO.getTargetFlags();
195 
196   switch (MO.getType()) {
197   default: llvm_unreachable("<unknown operand type>");
198   case MachineOperand::MO_Register: {
199     unsigned Reg = MO.getReg();
200     assert(TargetRegisterInfo::isPhysicalRegister(Reg));
201     assert(!MO.getSubReg() && "Subregs should be eliminated!");
202     if(ARM::GPRPairRegClass.contains(Reg)) {
203       const MachineFunction &MF = *MI->getParent()->getParent();
204       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
205       Reg = TRI->getSubReg(Reg, ARM::gsub_0);
206     }
207     O << ARMInstPrinter::getRegisterName(Reg);
208     break;
209   }
210   case MachineOperand::MO_Immediate: {
211     int64_t Imm = MO.getImm();
212     O << '#';
213     if (TF == ARMII::MO_LO16)
214       O << ":lower16:";
215     else if (TF == ARMII::MO_HI16)
216       O << ":upper16:";
217     O << Imm;
218     break;
219   }
220   case MachineOperand::MO_MachineBasicBlock:
221     MO.getMBB()->getSymbol()->print(O, MAI);
222     return;
223   case MachineOperand::MO_GlobalAddress: {
224     const GlobalValue *GV = MO.getGlobal();
225     if (TF & ARMII::MO_LO16)
226       O << ":lower16:";
227     else if (TF & ARMII::MO_HI16)
228       O << ":upper16:";
229     GetARMGVSymbol(GV, TF)->print(O, MAI);
230 
231     printOffset(MO.getOffset(), O);
232     break;
233   }
234   case MachineOperand::MO_ConstantPoolIndex:
235     if (Subtarget->genExecuteOnly())
236       llvm_unreachable("execute-only should not generate constant pools");
237     GetCPISymbol(MO.getIndex())->print(O, MAI);
238     break;
239   }
240 }
241 
242 //===--------------------------------------------------------------------===//
243 
244 MCSymbol *ARMAsmPrinter::
245 GetARMJTIPICJumpTableLabel(unsigned uid) const {
246   const DataLayout &DL = getDataLayout();
247   SmallString<60> Name;
248   raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
249                             << getFunctionNumber() << '_' << uid;
250   return OutContext.getOrCreateSymbol(Name);
251 }
252 
253 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
254                                     unsigned AsmVariant, const char *ExtraCode,
255                                     raw_ostream &O) {
256   // Does this asm operand have a single letter operand modifier?
257   if (ExtraCode && ExtraCode[0]) {
258     if (ExtraCode[1] != 0) return true; // Unknown modifier.
259 
260     switch (ExtraCode[0]) {
261     default:
262       // See if this is a generic print operand
263       return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
264     case 'a': // Print as a memory address.
265       if (MI->getOperand(OpNum).isReg()) {
266         O << "["
267           << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
268           << "]";
269         return false;
270       }
271       LLVM_FALLTHROUGH;
272     case 'c': // Don't print "#" before an immediate operand.
273       if (!MI->getOperand(OpNum).isImm())
274         return true;
275       O << MI->getOperand(OpNum).getImm();
276       return false;
277     case 'P': // Print a VFP double precision register.
278     case 'q': // Print a NEON quad precision register.
279       printOperand(MI, OpNum, O);
280       return false;
281     case 'y': // Print a VFP single precision register as indexed double.
282       if (MI->getOperand(OpNum).isReg()) {
283         unsigned Reg = MI->getOperand(OpNum).getReg();
284         const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
285         // Find the 'd' register that has this 's' register as a sub-register,
286         // and determine the lane number.
287         for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
288           if (!ARM::DPRRegClass.contains(*SR))
289             continue;
290           bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
291           O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
292           return false;
293         }
294       }
295       return true;
296     case 'B': // Bitwise inverse of integer or symbol without a preceding #.
297       if (!MI->getOperand(OpNum).isImm())
298         return true;
299       O << ~(MI->getOperand(OpNum).getImm());
300       return false;
301     case 'L': // The low 16 bits of an immediate constant.
302       if (!MI->getOperand(OpNum).isImm())
303         return true;
304       O << (MI->getOperand(OpNum).getImm() & 0xffff);
305       return false;
306     case 'M': { // A register range suitable for LDM/STM.
307       if (!MI->getOperand(OpNum).isReg())
308         return true;
309       const MachineOperand &MO = MI->getOperand(OpNum);
310       unsigned RegBegin = MO.getReg();
311       // This takes advantage of the 2 operand-ness of ldm/stm and that we've
312       // already got the operands in registers that are operands to the
313       // inline asm statement.
314       O << "{";
315       if (ARM::GPRPairRegClass.contains(RegBegin)) {
316         const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
317         unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
318         O << ARMInstPrinter::getRegisterName(Reg0) << ", ";
319         RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
320       }
321       O << ARMInstPrinter::getRegisterName(RegBegin);
322 
323       // FIXME: The register allocator not only may not have given us the
324       // registers in sequence, but may not be in ascending registers. This
325       // will require changes in the register allocator that'll need to be
326       // propagated down here if the operands change.
327       unsigned RegOps = OpNum + 1;
328       while (MI->getOperand(RegOps).isReg()) {
329         O << ", "
330           << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
331         RegOps++;
332       }
333 
334       O << "}";
335 
336       return false;
337     }
338     case 'R': // The most significant register of a pair.
339     case 'Q': { // The least significant register of a pair.
340       if (OpNum == 0)
341         return true;
342       const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
343       if (!FlagsOP.isImm())
344         return true;
345       unsigned Flags = FlagsOP.getImm();
346 
347       // This operand may not be the one that actually provides the register. If
348       // it's tied to a previous one then we should refer instead to that one
349       // for registers and their classes.
350       unsigned TiedIdx;
351       if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
352         for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
353           unsigned OpFlags = MI->getOperand(OpNum).getImm();
354           OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
355         }
356         Flags = MI->getOperand(OpNum).getImm();
357 
358         // Later code expects OpNum to be pointing at the register rather than
359         // the flags.
360         OpNum += 1;
361       }
362 
363       unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
364       unsigned RC;
365       InlineAsm::hasRegClassConstraint(Flags, RC);
366       if (RC == ARM::GPRPairRegClassID) {
367         if (NumVals != 1)
368           return true;
369         const MachineOperand &MO = MI->getOperand(OpNum);
370         if (!MO.isReg())
371           return true;
372         const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
373         unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ?
374             ARM::gsub_0 : ARM::gsub_1);
375         O << ARMInstPrinter::getRegisterName(Reg);
376         return false;
377       }
378       if (NumVals != 2)
379         return true;
380       unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
381       if (RegOp >= MI->getNumOperands())
382         return true;
383       const MachineOperand &MO = MI->getOperand(RegOp);
384       if (!MO.isReg())
385         return true;
386       unsigned Reg = MO.getReg();
387       O << ARMInstPrinter::getRegisterName(Reg);
388       return false;
389     }
390 
391     case 'e': // The low doubleword register of a NEON quad register.
392     case 'f': { // The high doubleword register of a NEON quad register.
393       if (!MI->getOperand(OpNum).isReg())
394         return true;
395       unsigned Reg = MI->getOperand(OpNum).getReg();
396       if (!ARM::QPRRegClass.contains(Reg))
397         return true;
398       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
399       unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
400                                        ARM::dsub_0 : ARM::dsub_1);
401       O << ARMInstPrinter::getRegisterName(SubReg);
402       return false;
403     }
404 
405     // This modifier is not yet supported.
406     case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
407       return true;
408     case 'H': { // The highest-numbered register of a pair.
409       const MachineOperand &MO = MI->getOperand(OpNum);
410       if (!MO.isReg())
411         return true;
412       const MachineFunction &MF = *MI->getParent()->getParent();
413       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
414       unsigned Reg = MO.getReg();
415       if(!ARM::GPRPairRegClass.contains(Reg))
416         return false;
417       Reg = TRI->getSubReg(Reg, ARM::gsub_1);
418       O << ARMInstPrinter::getRegisterName(Reg);
419       return false;
420     }
421     }
422   }
423 
424   printOperand(MI, OpNum, O);
425   return false;
426 }
427 
428 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
429                                           unsigned OpNum, unsigned AsmVariant,
430                                           const char *ExtraCode,
431                                           raw_ostream &O) {
432   // Does this asm operand have a single letter operand modifier?
433   if (ExtraCode && ExtraCode[0]) {
434     if (ExtraCode[1] != 0) return true; // Unknown modifier.
435 
436     switch (ExtraCode[0]) {
437       case 'A': // A memory operand for a VLD1/VST1 instruction.
438       default: return true;  // Unknown modifier.
439       case 'm': // The base register of a memory operand.
440         if (!MI->getOperand(OpNum).isReg())
441           return true;
442         O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
443         return false;
444     }
445   }
446 
447   const MachineOperand &MO = MI->getOperand(OpNum);
448   assert(MO.isReg() && "unexpected inline asm memory operand");
449   O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
450   return false;
451 }
452 
453 static bool isThumb(const MCSubtargetInfo& STI) {
454   return STI.getFeatureBits()[ARM::ModeThumb];
455 }
456 
457 void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
458                                      const MCSubtargetInfo *EndInfo) const {
459   // If either end mode is unknown (EndInfo == NULL) or different than
460   // the start mode, then restore the start mode.
461   const bool WasThumb = isThumb(StartInfo);
462   if (!EndInfo || WasThumb != isThumb(*EndInfo)) {
463     OutStreamer->EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
464   }
465 }
466 
467 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
468   const Triple &TT = TM.getTargetTriple();
469   // Use unified assembler syntax.
470   OutStreamer->EmitAssemblerFlag(MCAF_SyntaxUnified);
471 
472   // Emit ARM Build Attributes
473   if (TT.isOSBinFormatELF())
474     emitAttributes();
475 
476   // Use the triple's architecture and subarchitecture to determine
477   // if we're thumb for the purposes of the top level code16 assembler
478   // flag.
479   if (!M.getModuleInlineAsm().empty() && TT.isThumb())
480     OutStreamer->EmitAssemblerFlag(MCAF_Code16);
481 }
482 
483 static void
484 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
485                          MachineModuleInfoImpl::StubValueTy &MCSym) {
486   // L_foo$stub:
487   OutStreamer.EmitLabel(StubLabel);
488   //   .indirect_symbol _foo
489   OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
490 
491   if (MCSym.getInt())
492     // External to current translation unit.
493     OutStreamer.EmitIntValue(0, 4/*size*/);
494   else
495     // Internal to current translation unit.
496     //
497     // When we place the LSDA into the TEXT section, the type info
498     // pointers need to be indirect and pc-rel. We accomplish this by
499     // using NLPs; however, sometimes the types are local to the file.
500     // We need to fill in the value for the NLP in those cases.
501     OutStreamer.EmitValue(
502         MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
503         4 /*size*/);
504 }
505 
506 
507 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
508   const Triple &TT = TM.getTargetTriple();
509   if (TT.isOSBinFormatMachO()) {
510     // All darwin targets use mach-o.
511     const TargetLoweringObjectFileMachO &TLOFMacho =
512       static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
513     MachineModuleInfoMachO &MMIMacho =
514       MMI->getObjFileInfo<MachineModuleInfoMachO>();
515 
516     // Output non-lazy-pointers for external and common global variables.
517     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
518 
519     if (!Stubs.empty()) {
520       // Switch with ".non_lazy_symbol_pointer" directive.
521       OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
522       EmitAlignment(2);
523 
524       for (auto &Stub : Stubs)
525         emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
526 
527       Stubs.clear();
528       OutStreamer->AddBlankLine();
529     }
530 
531     Stubs = MMIMacho.GetThreadLocalGVStubList();
532     if (!Stubs.empty()) {
533       // Switch with ".non_lazy_symbol_pointer" directive.
534       OutStreamer->SwitchSection(TLOFMacho.getThreadLocalPointerSection());
535       EmitAlignment(2);
536 
537       for (auto &Stub : Stubs)
538         emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
539 
540       Stubs.clear();
541       OutStreamer->AddBlankLine();
542     }
543 
544     // Funny Darwin hack: This flag tells the linker that no global symbols
545     // contain code that falls through to other global symbols (e.g. the obvious
546     // implementation of multiple entry points).  If this doesn't occur, the
547     // linker can safely perform dead code stripping.  Since LLVM never
548     // generates code that does this, it is always safe to set.
549     OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
550   }
551 
552   if (TT.isOSBinFormatCOFF()) {
553     const auto &TLOF =
554         static_cast<const TargetLoweringObjectFileCOFF &>(getObjFileLowering());
555 
556     std::string Flags;
557     raw_string_ostream OS(Flags);
558 
559     for (const auto &Function : M)
560       TLOF.emitLinkerFlagsForGlobal(OS, &Function);
561     for (const auto &Global : M.globals())
562       TLOF.emitLinkerFlagsForGlobal(OS, &Global);
563     for (const auto &Alias : M.aliases())
564       TLOF.emitLinkerFlagsForGlobal(OS, &Alias);
565 
566     OS.flush();
567 
568     // Output collected flags
569     if (!Flags.empty()) {
570       OutStreamer->SwitchSection(TLOF.getDrectveSection());
571       OutStreamer->EmitBytes(Flags);
572     }
573   }
574 
575   // The last attribute to be emitted is ABI_optimization_goals
576   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
577   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
578 
579   if (OptimizationGoals > 0 &&
580       (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
581        Subtarget->isTargetMuslAEABI()))
582     ATS.emitAttribute(ARMBuildAttrs::ABI_optimization_goals, OptimizationGoals);
583   OptimizationGoals = -1;
584 
585   ATS.finishAttributeSection();
586 }
587 
588 //===----------------------------------------------------------------------===//
589 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
590 // FIXME:
591 // The following seem like one-off assembler flags, but they actually need
592 // to appear in the .ARM.attributes section in ELF.
593 // Instead of subclassing the MCELFStreamer, we do the work here.
594 
595 // Returns true if all functions have the same function attribute value.
596 // It also returns true when the module has no functions.
597 static bool checkFunctionsAttributeConsistency(const Module &M, StringRef Attr,
598                                                StringRef Value) {
599   return !any_of(M, [&](const Function &F) {
600     return F.getFnAttribute(Attr).getValueAsString() != Value;
601   });
602 }
603 
604 void ARMAsmPrinter::emitAttributes() {
605   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
606   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
607 
608   ATS.emitTextAttribute(ARMBuildAttrs::conformance, "2.09");
609 
610   ATS.switchVendor("aeabi");
611 
612   // Compute ARM ELF Attributes based on the default subtarget that
613   // we'd have constructed. The existing ARM behavior isn't LTO clean
614   // anyhow.
615   // FIXME: For ifunc related functions we could iterate over and look
616   // for a feature string that doesn't match the default one.
617   const Triple &TT = TM.getTargetTriple();
618   StringRef CPU = TM.getTargetCPU();
619   StringRef FS = TM.getTargetFeatureString();
620   std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
621   if (!FS.empty()) {
622     if (!ArchFS.empty())
623       ArchFS = (Twine(ArchFS) + "," + FS).str();
624     else
625       ArchFS = FS;
626   }
627   const ARMBaseTargetMachine &ATM =
628       static_cast<const ARMBaseTargetMachine &>(TM);
629   const ARMSubtarget STI(TT, CPU, ArchFS, ATM, ATM.isLittleEndian());
630 
631   // Emit build attributes for the available hardware.
632   ATS.emitTargetAttributes(STI);
633 
634   // RW data addressing.
635   if (isPositionIndependent()) {
636     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
637                       ARMBuildAttrs::AddressRWPCRel);
638   } else if (STI.isRWPI()) {
639     // RWPI specific attributes.
640     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
641                       ARMBuildAttrs::AddressRWSBRel);
642   }
643 
644   // RO data addressing.
645   if (isPositionIndependent() || STI.isROPI()) {
646     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data,
647                       ARMBuildAttrs::AddressROPCRel);
648   }
649 
650   // GOT use.
651   if (isPositionIndependent()) {
652     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
653                       ARMBuildAttrs::AddressGOT);
654   } else {
655     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
656                       ARMBuildAttrs::AddressDirect);
657   }
658 
659   // Set FP Denormals.
660   if (checkFunctionsAttributeConsistency(*MMI->getModule(),
661                                          "denormal-fp-math",
662                                          "preserve-sign") ||
663       TM.Options.FPDenormalMode == FPDenormal::PreserveSign)
664     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
665                       ARMBuildAttrs::PreserveFPSign);
666   else if (checkFunctionsAttributeConsistency(*MMI->getModule(),
667                                               "denormal-fp-math",
668                                               "positive-zero") ||
669            TM.Options.FPDenormalMode == FPDenormal::PositiveZero)
670     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
671                       ARMBuildAttrs::PositiveZero);
672   else if (!TM.Options.UnsafeFPMath)
673     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
674                       ARMBuildAttrs::IEEEDenormals);
675   else {
676     if (!STI.hasVFP2()) {
677       // When the target doesn't have an FPU (by design or
678       // intention), the assumptions made on the software support
679       // mirror that of the equivalent hardware support *if it
680       // existed*. For v7 and better we indicate that denormals are
681       // flushed preserving sign, and for V6 we indicate that
682       // denormals are flushed to positive zero.
683       if (STI.hasV7Ops())
684         ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
685                           ARMBuildAttrs::PreserveFPSign);
686     } else if (STI.hasVFP3()) {
687       // In VFPv4, VFPv4U, VFPv3, or VFPv3U, it is preserved. That is,
688       // the sign bit of the zero matches the sign bit of the input or
689       // result that is being flushed to zero.
690       ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
691                         ARMBuildAttrs::PreserveFPSign);
692     }
693     // For VFPv2 implementations it is implementation defined as
694     // to whether denormals are flushed to positive zero or to
695     // whatever the sign of zero is (ARM v7AR ARM 2.7.5). Historically
696     // LLVM has chosen to flush this to positive zero (most likely for
697     // GCC compatibility), so that's the chosen value here (the
698     // absence of its emission implies zero).
699   }
700 
701   // Set FP exceptions and rounding
702   if (checkFunctionsAttributeConsistency(*MMI->getModule(),
703                                          "no-trapping-math", "true") ||
704       TM.Options.NoTrappingFPMath)
705     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
706                       ARMBuildAttrs::Not_Allowed);
707   else if (!TM.Options.UnsafeFPMath) {
708     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, ARMBuildAttrs::Allowed);
709 
710     // If the user has permitted this code to choose the IEEE 754
711     // rounding at run-time, emit the rounding attribute.
712     if (TM.Options.HonorSignDependentRoundingFPMathOption)
713       ATS.emitAttribute(ARMBuildAttrs::ABI_FP_rounding, ARMBuildAttrs::Allowed);
714   }
715 
716   // TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath is the
717   // equivalent of GCC's -ffinite-math-only flag.
718   if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
719     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
720                       ARMBuildAttrs::Allowed);
721   else
722     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
723                       ARMBuildAttrs::AllowIEEE754);
724 
725   // FIXME: add more flags to ARMBuildAttributes.h
726   // 8-bytes alignment stuff.
727   ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1);
728   ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1);
729 
730   // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
731   if (STI.isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
732     ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
733 
734   // FIXME: To support emitting this build attribute as GCC does, the
735   // -mfp16-format option and associated plumbing must be
736   // supported. For now the __fp16 type is exposed by default, so this
737   // attribute should be emitted with value 1.
738   ATS.emitAttribute(ARMBuildAttrs::ABI_FP_16bit_format,
739                     ARMBuildAttrs::FP16FormatIEEE);
740 
741   if (MMI) {
742     if (const Module *SourceModule = MMI->getModule()) {
743       // ABI_PCS_wchar_t to indicate wchar_t width
744       // FIXME: There is no way to emit value 0 (wchar_t prohibited).
745       if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>(
746               SourceModule->getModuleFlag("wchar_size"))) {
747         int WCharWidth = WCharWidthValue->getZExtValue();
748         assert((WCharWidth == 2 || WCharWidth == 4) &&
749                "wchar_t width must be 2 or 4 bytes");
750         ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t, WCharWidth);
751       }
752 
753       // ABI_enum_size to indicate enum width
754       // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
755       //        (all enums contain a value needing 32 bits to encode).
756       if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>(
757               SourceModule->getModuleFlag("min_enum_size"))) {
758         int EnumWidth = EnumWidthValue->getZExtValue();
759         assert((EnumWidth == 1 || EnumWidth == 4) &&
760                "Minimum enum width must be 1 or 4 bytes");
761         int EnumBuildAttr = EnumWidth == 1 ? 1 : 2;
762         ATS.emitAttribute(ARMBuildAttrs::ABI_enum_size, EnumBuildAttr);
763       }
764     }
765   }
766 
767   // We currently do not support using R9 as the TLS pointer.
768   if (STI.isRWPI())
769     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
770                       ARMBuildAttrs::R9IsSB);
771   else if (STI.isR9Reserved())
772     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
773                       ARMBuildAttrs::R9Reserved);
774   else
775     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
776                       ARMBuildAttrs::R9IsGPR);
777 }
778 
779 //===----------------------------------------------------------------------===//
780 
781 static MCSymbol *getPICLabel(StringRef Prefix, unsigned FunctionNumber,
782                              unsigned LabelId, MCContext &Ctx) {
783 
784   MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
785                        + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
786   return Label;
787 }
788 
789 static MCSymbolRefExpr::VariantKind
790 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
791   switch (Modifier) {
792   case ARMCP::no_modifier:
793     return MCSymbolRefExpr::VK_None;
794   case ARMCP::TLSGD:
795     return MCSymbolRefExpr::VK_TLSGD;
796   case ARMCP::TPOFF:
797     return MCSymbolRefExpr::VK_TPOFF;
798   case ARMCP::GOTTPOFF:
799     return MCSymbolRefExpr::VK_GOTTPOFF;
800   case ARMCP::SBREL:
801     return MCSymbolRefExpr::VK_ARM_SBREL;
802   case ARMCP::GOT_PREL:
803     return MCSymbolRefExpr::VK_ARM_GOT_PREL;
804   case ARMCP::SECREL:
805     return MCSymbolRefExpr::VK_SECREL;
806   }
807   llvm_unreachable("Invalid ARMCPModifier!");
808 }
809 
810 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
811                                         unsigned char TargetFlags) {
812   if (Subtarget->isTargetMachO()) {
813     bool IsIndirect =
814         (TargetFlags & ARMII::MO_NONLAZY) && Subtarget->isGVIndirectSymbol(GV);
815 
816     if (!IsIndirect)
817       return getSymbol(GV);
818 
819     // FIXME: Remove this when Darwin transition to @GOT like syntax.
820     MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
821     MachineModuleInfoMachO &MMIMachO =
822       MMI->getObjFileInfo<MachineModuleInfoMachO>();
823     MachineModuleInfoImpl::StubValueTy &StubSym =
824         GV->isThreadLocal() ? MMIMachO.getThreadLocalGVStubEntry(MCSym)
825                             : MMIMachO.getGVStubEntry(MCSym);
826 
827     if (!StubSym.getPointer())
828       StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
829                                                    !GV->hasInternalLinkage());
830     return MCSym;
831   } else if (Subtarget->isTargetCOFF()) {
832     assert(Subtarget->isTargetWindows() &&
833            "Windows is the only supported COFF target");
834 
835     bool IsIndirect = (TargetFlags & ARMII::MO_DLLIMPORT);
836     if (!IsIndirect)
837       return getSymbol(GV);
838 
839     SmallString<128> Name;
840     Name = "__imp_";
841     getNameWithPrefix(Name, GV);
842 
843     return OutContext.getOrCreateSymbol(Name);
844   } else if (Subtarget->isTargetELF()) {
845     return getSymbol(GV);
846   }
847   llvm_unreachable("unexpected target");
848 }
849 
850 void ARMAsmPrinter::
851 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
852   const DataLayout &DL = getDataLayout();
853   int Size = DL.getTypeAllocSize(MCPV->getType());
854 
855   ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
856 
857   if (ACPV->isPromotedGlobal()) {
858     // This constant pool entry is actually a global whose storage has been
859     // promoted into the constant pool. This global may be referenced still
860     // by debug information, and due to the way AsmPrinter is set up, the debug
861     // info is immutable by the time we decide to promote globals to constant
862     // pools. Because of this, we need to ensure we emit a symbol for the global
863     // with private linkage (the default) so debug info can refer to it.
864     //
865     // However, if this global is promoted into several functions we must ensure
866     // we don't try and emit duplicate symbols!
867     auto *ACPC = cast<ARMConstantPoolConstant>(ACPV);
868     auto *GV = ACPC->getPromotedGlobal();
869     if (!EmittedPromotedGlobalLabels.count(GV)) {
870       MCSymbol *GVSym = getSymbol(GV);
871       OutStreamer->EmitLabel(GVSym);
872       EmittedPromotedGlobalLabels.insert(GV);
873     }
874     return EmitGlobalConstant(DL, ACPC->getPromotedGlobalInit());
875   }
876 
877   MCSymbol *MCSym;
878   if (ACPV->isLSDA()) {
879     MCSym = getCurExceptionSym();
880   } else if (ACPV->isBlockAddress()) {
881     const BlockAddress *BA =
882       cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
883     MCSym = GetBlockAddressSymbol(BA);
884   } else if (ACPV->isGlobalValue()) {
885     const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
886 
887     // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
888     // flag the global as MO_NONLAZY.
889     unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
890     MCSym = GetARMGVSymbol(GV, TF);
891   } else if (ACPV->isMachineBasicBlock()) {
892     const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
893     MCSym = MBB->getSymbol();
894   } else {
895     assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
896     auto Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
897     MCSym = GetExternalSymbolSymbol(Sym);
898   }
899 
900   // Create an MCSymbol for the reference.
901   const MCExpr *Expr =
902     MCSymbolRefExpr::create(MCSym, getModifierVariantKind(ACPV->getModifier()),
903                             OutContext);
904 
905   if (ACPV->getPCAdjustment()) {
906     MCSymbol *PCLabel =
907         getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
908                     ACPV->getLabelId(), OutContext);
909     const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
910     PCRelExpr =
911       MCBinaryExpr::createAdd(PCRelExpr,
912                               MCConstantExpr::create(ACPV->getPCAdjustment(),
913                                                      OutContext),
914                               OutContext);
915     if (ACPV->mustAddCurrentAddress()) {
916       // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
917       // label, so just emit a local label end reference that instead.
918       MCSymbol *DotSym = OutContext.createTempSymbol();
919       OutStreamer->EmitLabel(DotSym);
920       const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
921       PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
922     }
923     Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
924   }
925   OutStreamer->EmitValue(Expr, Size);
926 }
927 
928 void ARMAsmPrinter::EmitJumpTableAddrs(const MachineInstr *MI) {
929   const MachineOperand &MO1 = MI->getOperand(1);
930   unsigned JTI = MO1.getIndex();
931 
932   // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
933   // ARM mode tables.
934   EmitAlignment(2);
935 
936   // Emit a label for the jump table.
937   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
938   OutStreamer->EmitLabel(JTISymbol);
939 
940   // Mark the jump table as data-in-code.
941   OutStreamer->EmitDataRegion(MCDR_DataRegionJT32);
942 
943   // Emit each entry of the table.
944   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
945   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
946   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
947 
948   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
949     MachineBasicBlock *MBB = JTBBs[i];
950     // Construct an MCExpr for the entry. We want a value of the form:
951     // (BasicBlockAddr - TableBeginAddr)
952     //
953     // For example, a table with entries jumping to basic blocks BB0 and BB1
954     // would look like:
955     // LJTI_0_0:
956     //    .word (LBB0 - LJTI_0_0)
957     //    .word (LBB1 - LJTI_0_0)
958     const MCExpr *Expr = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
959 
960     if (isPositionIndependent() || Subtarget->isROPI())
961       Expr = MCBinaryExpr::createSub(Expr, MCSymbolRefExpr::create(JTISymbol,
962                                                                    OutContext),
963                                      OutContext);
964     // If we're generating a table of Thumb addresses in static relocation
965     // model, we need to add one to keep interworking correctly.
966     else if (AFI->isThumbFunction())
967       Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(1,OutContext),
968                                      OutContext);
969     OutStreamer->EmitValue(Expr, 4);
970   }
971   // Mark the end of jump table data-in-code region.
972   OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
973 }
974 
975 void ARMAsmPrinter::EmitJumpTableInsts(const MachineInstr *MI) {
976   const MachineOperand &MO1 = MI->getOperand(1);
977   unsigned JTI = MO1.getIndex();
978 
979   // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
980   // ARM mode tables.
981   EmitAlignment(2);
982 
983   // Emit a label for the jump table.
984   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
985   OutStreamer->EmitLabel(JTISymbol);
986 
987   // Emit each entry of the table.
988   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
989   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
990   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
991 
992   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
993     MachineBasicBlock *MBB = JTBBs[i];
994     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
995                                                           OutContext);
996     // If this isn't a TBB or TBH, the entries are direct branch instructions.
997     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2B)
998         .addExpr(MBBSymbolExpr)
999         .addImm(ARMCC::AL)
1000         .addReg(0));
1001   }
1002 }
1003 
1004 void ARMAsmPrinter::EmitJumpTableTBInst(const MachineInstr *MI,
1005                                         unsigned OffsetWidth) {
1006   assert((OffsetWidth == 1 || OffsetWidth == 2) && "invalid tbb/tbh width");
1007   const MachineOperand &MO1 = MI->getOperand(1);
1008   unsigned JTI = MO1.getIndex();
1009 
1010   if (Subtarget->isThumb1Only())
1011     EmitAlignment(2);
1012 
1013   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
1014   OutStreamer->EmitLabel(JTISymbol);
1015 
1016   // Emit each entry of the table.
1017   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1018   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1019   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1020 
1021   // Mark the jump table as data-in-code.
1022   OutStreamer->EmitDataRegion(OffsetWidth == 1 ? MCDR_DataRegionJT8
1023                                                : MCDR_DataRegionJT16);
1024 
1025   for (auto MBB : JTBBs) {
1026     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
1027                                                           OutContext);
1028     // Otherwise it's an offset from the dispatch instruction. Construct an
1029     // MCExpr for the entry. We want a value of the form:
1030     // (BasicBlockAddr - TBBInstAddr + 4) / 2
1031     //
1032     // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1033     // would look like:
1034     // LJTI_0_0:
1035     //    .byte (LBB0 - (LCPI0_0 + 4)) / 2
1036     //    .byte (LBB1 - (LCPI0_0 + 4)) / 2
1037     // where LCPI0_0 is a label defined just before the TBB instruction using
1038     // this table.
1039     MCSymbol *TBInstPC = GetCPISymbol(MI->getOperand(0).getImm());
1040     const MCExpr *Expr = MCBinaryExpr::createAdd(
1041         MCSymbolRefExpr::create(TBInstPC, OutContext),
1042         MCConstantExpr::create(4, OutContext), OutContext);
1043     Expr = MCBinaryExpr::createSub(MBBSymbolExpr, Expr, OutContext);
1044     Expr = MCBinaryExpr::createDiv(Expr, MCConstantExpr::create(2, OutContext),
1045                                    OutContext);
1046     OutStreamer->EmitValue(Expr, OffsetWidth);
1047   }
1048   // Mark the end of jump table data-in-code region. 32-bit offsets use
1049   // actual branch instructions here, so we don't mark those as a data-region
1050   // at all.
1051   OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
1052 
1053   // Make sure the next instruction is 2-byte aligned.
1054   EmitAlignment(1);
1055 }
1056 
1057 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1058   assert(MI->getFlag(MachineInstr::FrameSetup) &&
1059       "Only instruction which are involved into frame setup code are allowed");
1060 
1061   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
1062   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1063   const MachineFunction &MF = *MI->getParent()->getParent();
1064   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
1065   const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
1066 
1067   unsigned FramePtr = RegInfo->getFrameRegister(MF);
1068   unsigned Opc = MI->getOpcode();
1069   unsigned SrcReg, DstReg;
1070 
1071   if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1072     // Two special cases:
1073     // 1) tPUSH does not have src/dst regs.
1074     // 2) for Thumb1 code we sometimes materialize the constant via constpool
1075     // load. Yes, this is pretty fragile, but for now I don't see better
1076     // way... :(
1077     SrcReg = DstReg = ARM::SP;
1078   } else {
1079     SrcReg = MI->getOperand(1).getReg();
1080     DstReg = MI->getOperand(0).getReg();
1081   }
1082 
1083   // Try to figure out the unwinding opcode out of src / dst regs.
1084   if (MI->mayStore()) {
1085     // Register saves.
1086     assert(DstReg == ARM::SP &&
1087            "Only stack pointer as a destination reg is supported");
1088 
1089     SmallVector<unsigned, 4> RegList;
1090     // Skip src & dst reg, and pred ops.
1091     unsigned StartOp = 2 + 2;
1092     // Use all the operands.
1093     unsigned NumOffset = 0;
1094 
1095     switch (Opc) {
1096     default:
1097       MI->print(errs());
1098       llvm_unreachable("Unsupported opcode for unwinding information");
1099     case ARM::tPUSH:
1100       // Special case here: no src & dst reg, but two extra imp ops.
1101       StartOp = 2; NumOffset = 2;
1102       LLVM_FALLTHROUGH;
1103     case ARM::STMDB_UPD:
1104     case ARM::t2STMDB_UPD:
1105     case ARM::VSTMDDB_UPD:
1106       assert(SrcReg == ARM::SP &&
1107              "Only stack pointer as a source reg is supported");
1108       for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1109            i != NumOps; ++i) {
1110         const MachineOperand &MO = MI->getOperand(i);
1111         // Actually, there should never be any impdef stuff here. Skip it
1112         // temporary to workaround PR11902.
1113         if (MO.isImplicit())
1114           continue;
1115         RegList.push_back(MO.getReg());
1116       }
1117       break;
1118     case ARM::STR_PRE_IMM:
1119     case ARM::STR_PRE_REG:
1120     case ARM::t2STR_PRE:
1121       assert(MI->getOperand(2).getReg() == ARM::SP &&
1122              "Only stack pointer as a source reg is supported");
1123       RegList.push_back(SrcReg);
1124       break;
1125     }
1126     if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM)
1127       ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1128   } else {
1129     // Changes of stack / frame pointer.
1130     if (SrcReg == ARM::SP) {
1131       int64_t Offset = 0;
1132       switch (Opc) {
1133       default:
1134         MI->print(errs());
1135         llvm_unreachable("Unsupported opcode for unwinding information");
1136       case ARM::MOVr:
1137       case ARM::tMOVr:
1138         Offset = 0;
1139         break;
1140       case ARM::ADDri:
1141       case ARM::t2ADDri:
1142         Offset = -MI->getOperand(2).getImm();
1143         break;
1144       case ARM::SUBri:
1145       case ARM::t2SUBri:
1146         Offset = MI->getOperand(2).getImm();
1147         break;
1148       case ARM::tSUBspi:
1149         Offset = MI->getOperand(2).getImm()*4;
1150         break;
1151       case ARM::tADDspi:
1152       case ARM::tADDrSPi:
1153         Offset = -MI->getOperand(2).getImm()*4;
1154         break;
1155       case ARM::tLDRpci: {
1156         // Grab the constpool index and check, whether it corresponds to
1157         // original or cloned constpool entry.
1158         unsigned CPI = MI->getOperand(1).getIndex();
1159         const MachineConstantPool *MCP = MF.getConstantPool();
1160         if (CPI >= MCP->getConstants().size())
1161           CPI = AFI.getOriginalCPIdx(CPI);
1162         assert(CPI != -1U && "Invalid constpool index");
1163 
1164         // Derive the actual offset.
1165         const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1166         assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1167         // FIXME: Check for user, it should be "add" instruction!
1168         Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1169         break;
1170       }
1171       }
1172 
1173       if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
1174         if (DstReg == FramePtr && FramePtr != ARM::SP)
1175           // Set-up of the frame pointer. Positive values correspond to "add"
1176           // instruction.
1177           ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
1178         else if (DstReg == ARM::SP) {
1179           // Change of SP by an offset. Positive values correspond to "sub"
1180           // instruction.
1181           ATS.emitPad(Offset);
1182         } else {
1183           // Move of SP to a register.  Positive values correspond to an "add"
1184           // instruction.
1185           ATS.emitMovSP(DstReg, -Offset);
1186         }
1187       }
1188     } else if (DstReg == ARM::SP) {
1189       MI->print(errs());
1190       llvm_unreachable("Unsupported opcode for unwinding information");
1191     }
1192     else {
1193       MI->print(errs());
1194       llvm_unreachable("Unsupported opcode for unwinding information");
1195     }
1196   }
1197 }
1198 
1199 // Simple pseudo-instructions have their lowering (with expansion to real
1200 // instructions) auto-generated.
1201 #include "ARMGenMCPseudoLowering.inc"
1202 
1203 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1204   const DataLayout &DL = getDataLayout();
1205   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
1206   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1207 
1208   // If we just ended a constant pool, mark it as such.
1209   if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1210     OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
1211     InConstantPool = false;
1212   }
1213 
1214   // Emit unwinding stuff for frame-related instructions
1215   if (Subtarget->isTargetEHABICompatible() &&
1216        MI->getFlag(MachineInstr::FrameSetup))
1217     EmitUnwindingInstruction(MI);
1218 
1219   // Do any auto-generated pseudo lowerings.
1220   if (emitPseudoExpansionLowering(*OutStreamer, MI))
1221     return;
1222 
1223   assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1224          "Pseudo flag setting opcode should be expanded early");
1225 
1226   // Check for manual lowerings.
1227   unsigned Opc = MI->getOpcode();
1228   switch (Opc) {
1229   case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
1230   case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
1231   case ARM::LEApcrel:
1232   case ARM::tLEApcrel:
1233   case ARM::t2LEApcrel: {
1234     // FIXME: Need to also handle globals and externals
1235     MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1236     EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
1237                                                ARM::t2LEApcrel ? ARM::t2ADR
1238                   : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1239                      : ARM::ADR))
1240       .addReg(MI->getOperand(0).getReg())
1241       .addExpr(MCSymbolRefExpr::create(CPISymbol, OutContext))
1242       // Add predicate operands.
1243       .addImm(MI->getOperand(2).getImm())
1244       .addReg(MI->getOperand(3).getReg()));
1245     return;
1246   }
1247   case ARM::LEApcrelJT:
1248   case ARM::tLEApcrelJT:
1249   case ARM::t2LEApcrelJT: {
1250     MCSymbol *JTIPICSymbol =
1251       GetARMJTIPICJumpTableLabel(MI->getOperand(1).getIndex());
1252     EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
1253                                                ARM::t2LEApcrelJT ? ARM::t2ADR
1254                   : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1255                      : ARM::ADR))
1256       .addReg(MI->getOperand(0).getReg())
1257       .addExpr(MCSymbolRefExpr::create(JTIPICSymbol, OutContext))
1258       // Add predicate operands.
1259       .addImm(MI->getOperand(2).getImm())
1260       .addReg(MI->getOperand(3).getReg()));
1261     return;
1262   }
1263   // Darwin call instructions are just normal call instructions with different
1264   // clobber semantics (they clobber R9).
1265   case ARM::BX_CALL: {
1266     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
1267       .addReg(ARM::LR)
1268       .addReg(ARM::PC)
1269       // Add predicate operands.
1270       .addImm(ARMCC::AL)
1271       .addReg(0)
1272       // Add 's' bit operand (always reg0 for this)
1273       .addReg(0));
1274 
1275     assert(Subtarget->hasV4TOps());
1276     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX)
1277       .addReg(MI->getOperand(0).getReg()));
1278     return;
1279   }
1280   case ARM::tBX_CALL: {
1281     if (Subtarget->hasV5TOps())
1282       llvm_unreachable("Expected BLX to be selected for v5t+");
1283 
1284     // On ARM v4t, when doing a call from thumb mode, we need to ensure
1285     // that the saved lr has its LSB set correctly (the arch doesn't
1286     // have blx).
1287     // So here we generate a bl to a small jump pad that does bx rN.
1288     // The jump pads are emitted after the function body.
1289 
1290     unsigned TReg = MI->getOperand(0).getReg();
1291     MCSymbol *TRegSym = nullptr;
1292     for (unsigned i = 0, e = ThumbIndirectPads.size(); i < e; i++) {
1293       if (ThumbIndirectPads[i].first == TReg) {
1294         TRegSym = ThumbIndirectPads[i].second;
1295         break;
1296       }
1297     }
1298 
1299     if (!TRegSym) {
1300       TRegSym = OutContext.createTempSymbol();
1301       ThumbIndirectPads.push_back(std::make_pair(TReg, TRegSym));
1302     }
1303 
1304     // Create a link-saving branch to the Reg Indirect Jump Pad.
1305     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBL)
1306         // Predicate comes first here.
1307         .addImm(ARMCC::AL).addReg(0)
1308         .addExpr(MCSymbolRefExpr::create(TRegSym, OutContext)));
1309     return;
1310   }
1311   case ARM::BMOVPCRX_CALL: {
1312     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
1313       .addReg(ARM::LR)
1314       .addReg(ARM::PC)
1315       // Add predicate operands.
1316       .addImm(ARMCC::AL)
1317       .addReg(0)
1318       // Add 's' bit operand (always reg0 for this)
1319       .addReg(0));
1320 
1321     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
1322       .addReg(ARM::PC)
1323       .addReg(MI->getOperand(0).getReg())
1324       // Add predicate operands.
1325       .addImm(ARMCC::AL)
1326       .addReg(0)
1327       // Add 's' bit operand (always reg0 for this)
1328       .addReg(0));
1329     return;
1330   }
1331   case ARM::BMOVPCB_CALL: {
1332     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
1333       .addReg(ARM::LR)
1334       .addReg(ARM::PC)
1335       // Add predicate operands.
1336       .addImm(ARMCC::AL)
1337       .addReg(0)
1338       // Add 's' bit operand (always reg0 for this)
1339       .addReg(0));
1340 
1341     const MachineOperand &Op = MI->getOperand(0);
1342     const GlobalValue *GV = Op.getGlobal();
1343     const unsigned TF = Op.getTargetFlags();
1344     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1345     const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
1346     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::Bcc)
1347       .addExpr(GVSymExpr)
1348       // Add predicate operands.
1349       .addImm(ARMCC::AL)
1350       .addReg(0));
1351     return;
1352   }
1353   case ARM::MOVi16_ga_pcrel:
1354   case ARM::t2MOVi16_ga_pcrel: {
1355     MCInst TmpInst;
1356     TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
1357     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1358 
1359     unsigned TF = MI->getOperand(1).getTargetFlags();
1360     const GlobalValue *GV = MI->getOperand(1).getGlobal();
1361     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1362     const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
1363 
1364     MCSymbol *LabelSym =
1365         getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
1366                     MI->getOperand(2).getImm(), OutContext);
1367     const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
1368     unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1369     const MCExpr *PCRelExpr =
1370       ARMMCExpr::createLower16(MCBinaryExpr::createSub(GVSymExpr,
1371                                       MCBinaryExpr::createAdd(LabelSymExpr,
1372                                       MCConstantExpr::create(PCAdj, OutContext),
1373                                       OutContext), OutContext), OutContext);
1374       TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
1375 
1376     // Add predicate operands.
1377     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
1378     TmpInst.addOperand(MCOperand::createReg(0));
1379     // Add 's' bit operand (always reg0 for this)
1380     TmpInst.addOperand(MCOperand::createReg(0));
1381     EmitToStreamer(*OutStreamer, TmpInst);
1382     return;
1383   }
1384   case ARM::MOVTi16_ga_pcrel:
1385   case ARM::t2MOVTi16_ga_pcrel: {
1386     MCInst TmpInst;
1387     TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1388                       ? ARM::MOVTi16 : ARM::t2MOVTi16);
1389     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1390     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
1391 
1392     unsigned TF = MI->getOperand(2).getTargetFlags();
1393     const GlobalValue *GV = MI->getOperand(2).getGlobal();
1394     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
1395     const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
1396 
1397     MCSymbol *LabelSym =
1398         getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
1399                     MI->getOperand(3).getImm(), OutContext);
1400     const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
1401     unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1402     const MCExpr *PCRelExpr =
1403         ARMMCExpr::createUpper16(MCBinaryExpr::createSub(GVSymExpr,
1404                                    MCBinaryExpr::createAdd(LabelSymExpr,
1405                                       MCConstantExpr::create(PCAdj, OutContext),
1406                                           OutContext), OutContext), OutContext);
1407       TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
1408     // Add predicate operands.
1409     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
1410     TmpInst.addOperand(MCOperand::createReg(0));
1411     // Add 's' bit operand (always reg0 for this)
1412     TmpInst.addOperand(MCOperand::createReg(0));
1413     EmitToStreamer(*OutStreamer, TmpInst);
1414     return;
1415   }
1416   case ARM::tPICADD: {
1417     // This is a pseudo op for a label + instruction sequence, which looks like:
1418     // LPC0:
1419     //     add r0, pc
1420     // This adds the address of LPC0 to r0.
1421 
1422     // Emit the label.
1423     OutStreamer->EmitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
1424                                        getFunctionNumber(),
1425                                        MI->getOperand(2).getImm(), OutContext));
1426 
1427     // Form and emit the add.
1428     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
1429       .addReg(MI->getOperand(0).getReg())
1430       .addReg(MI->getOperand(0).getReg())
1431       .addReg(ARM::PC)
1432       // Add predicate operands.
1433       .addImm(ARMCC::AL)
1434       .addReg(0));
1435     return;
1436   }
1437   case ARM::PICADD: {
1438     // This is a pseudo op for a label + instruction sequence, which looks like:
1439     // LPC0:
1440     //     add r0, pc, r0
1441     // This adds the address of LPC0 to r0.
1442 
1443     // Emit the label.
1444     OutStreamer->EmitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
1445                                        getFunctionNumber(),
1446                                        MI->getOperand(2).getImm(), OutContext));
1447 
1448     // Form and emit the add.
1449     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr)
1450       .addReg(MI->getOperand(0).getReg())
1451       .addReg(ARM::PC)
1452       .addReg(MI->getOperand(1).getReg())
1453       // Add predicate operands.
1454       .addImm(MI->getOperand(3).getImm())
1455       .addReg(MI->getOperand(4).getReg())
1456       // Add 's' bit operand (always reg0 for this)
1457       .addReg(0));
1458     return;
1459   }
1460   case ARM::PICSTR:
1461   case ARM::PICSTRB:
1462   case ARM::PICSTRH:
1463   case ARM::PICLDR:
1464   case ARM::PICLDRB:
1465   case ARM::PICLDRH:
1466   case ARM::PICLDRSB:
1467   case ARM::PICLDRSH: {
1468     // This is a pseudo op for a label + instruction sequence, which looks like:
1469     // LPC0:
1470     //     OP r0, [pc, r0]
1471     // The LCP0 label is referenced by a constant pool entry in order to get
1472     // a PC-relative address at the ldr instruction.
1473 
1474     // Emit the label.
1475     OutStreamer->EmitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
1476                                        getFunctionNumber(),
1477                                        MI->getOperand(2).getImm(), OutContext));
1478 
1479     // Form and emit the load
1480     unsigned Opcode;
1481     switch (MI->getOpcode()) {
1482     default:
1483       llvm_unreachable("Unexpected opcode!");
1484     case ARM::PICSTR:   Opcode = ARM::STRrs; break;
1485     case ARM::PICSTRB:  Opcode = ARM::STRBrs; break;
1486     case ARM::PICSTRH:  Opcode = ARM::STRH; break;
1487     case ARM::PICLDR:   Opcode = ARM::LDRrs; break;
1488     case ARM::PICLDRB:  Opcode = ARM::LDRBrs; break;
1489     case ARM::PICLDRH:  Opcode = ARM::LDRH; break;
1490     case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1491     case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1492     }
1493     EmitToStreamer(*OutStreamer, MCInstBuilder(Opcode)
1494       .addReg(MI->getOperand(0).getReg())
1495       .addReg(ARM::PC)
1496       .addReg(MI->getOperand(1).getReg())
1497       .addImm(0)
1498       // Add predicate operands.
1499       .addImm(MI->getOperand(3).getImm())
1500       .addReg(MI->getOperand(4).getReg()));
1501 
1502     return;
1503   }
1504   case ARM::CONSTPOOL_ENTRY: {
1505     if (Subtarget->genExecuteOnly())
1506       llvm_unreachable("execute-only should not generate constant pools");
1507 
1508     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1509     /// in the function.  The first operand is the ID# for this instruction, the
1510     /// second is the index into the MachineConstantPool that this is, the third
1511     /// is the size in bytes of this constant pool entry.
1512     /// The required alignment is specified on the basic block holding this MI.
1513     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1514     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1515 
1516     // If this is the first entry of the pool, mark it.
1517     if (!InConstantPool) {
1518       OutStreamer->EmitDataRegion(MCDR_DataRegion);
1519       InConstantPool = true;
1520     }
1521 
1522     OutStreamer->EmitLabel(GetCPISymbol(LabelId));
1523 
1524     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1525     if (MCPE.isMachineConstantPoolEntry())
1526       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1527     else
1528       EmitGlobalConstant(DL, MCPE.Val.ConstVal);
1529     return;
1530   }
1531   case ARM::JUMPTABLE_ADDRS:
1532     EmitJumpTableAddrs(MI);
1533     return;
1534   case ARM::JUMPTABLE_INSTS:
1535     EmitJumpTableInsts(MI);
1536     return;
1537   case ARM::JUMPTABLE_TBB:
1538   case ARM::JUMPTABLE_TBH:
1539     EmitJumpTableTBInst(MI, MI->getOpcode() == ARM::JUMPTABLE_TBB ? 1 : 2);
1540     return;
1541   case ARM::t2BR_JT: {
1542     // Lower and emit the instruction itself, then the jump table following it.
1543     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
1544       .addReg(ARM::PC)
1545       .addReg(MI->getOperand(0).getReg())
1546       // Add predicate operands.
1547       .addImm(ARMCC::AL)
1548       .addReg(0));
1549     return;
1550   }
1551   case ARM::t2TBB_JT:
1552   case ARM::t2TBH_JT: {
1553     unsigned Opc = MI->getOpcode() == ARM::t2TBB_JT ? ARM::t2TBB : ARM::t2TBH;
1554     // Lower and emit the PC label, then the instruction itself.
1555     OutStreamer->EmitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
1556     EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
1557                                      .addReg(MI->getOperand(0).getReg())
1558                                      .addReg(MI->getOperand(1).getReg())
1559                                      // Add predicate operands.
1560                                      .addImm(ARMCC::AL)
1561                                      .addReg(0));
1562     return;
1563   }
1564   case ARM::tTBB_JT:
1565   case ARM::tTBH_JT: {
1566 
1567     bool Is8Bit = MI->getOpcode() == ARM::tTBB_JT;
1568     unsigned Base = MI->getOperand(0).getReg();
1569     unsigned Idx = MI->getOperand(1).getReg();
1570     assert(MI->getOperand(1).isKill() && "We need the index register as scratch!");
1571 
1572     // Multiply up idx if necessary.
1573     if (!Is8Bit)
1574       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
1575                                        .addReg(Idx)
1576                                        .addReg(ARM::CPSR)
1577                                        .addReg(Idx)
1578                                        .addImm(1)
1579                                        // Add predicate operands.
1580                                        .addImm(ARMCC::AL)
1581                                        .addReg(0));
1582 
1583     if (Base == ARM::PC) {
1584       // TBB [base, idx] =
1585       //    ADDS idx, idx, base
1586       //    LDRB idx, [idx, #4] ; or LDRH if TBH
1587       //    LSLS idx, #1
1588       //    ADDS pc, pc, idx
1589 
1590       // When using PC as the base, it's important that there is no padding
1591       // between the last ADDS and the start of the jump table. The jump table
1592       // is 4-byte aligned, so we ensure we're 4 byte aligned here too.
1593       //
1594       // FIXME: Ideally we could vary the LDRB index based on the padding
1595       // between the sequence and jump table, however that relies on MCExprs
1596       // for load indexes which are currently not supported.
1597       OutStreamer->EmitCodeAlignment(4);
1598       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
1599                                        .addReg(Idx)
1600                                        .addReg(Idx)
1601                                        .addReg(Base)
1602                                        // Add predicate operands.
1603                                        .addImm(ARMCC::AL)
1604                                        .addReg(0));
1605 
1606       unsigned Opc = Is8Bit ? ARM::tLDRBi : ARM::tLDRHi;
1607       EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
1608                                        .addReg(Idx)
1609                                        .addReg(Idx)
1610                                        .addImm(Is8Bit ? 4 : 2)
1611                                        // Add predicate operands.
1612                                        .addImm(ARMCC::AL)
1613                                        .addReg(0));
1614     } else {
1615       // TBB [base, idx] =
1616       //    LDRB idx, [base, idx] ; or LDRH if TBH
1617       //    LSLS idx, #1
1618       //    ADDS pc, pc, idx
1619 
1620       unsigned Opc = Is8Bit ? ARM::tLDRBr : ARM::tLDRHr;
1621       EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
1622                                        .addReg(Idx)
1623                                        .addReg(Base)
1624                                        .addReg(Idx)
1625                                        // Add predicate operands.
1626                                        .addImm(ARMCC::AL)
1627                                        .addReg(0));
1628     }
1629 
1630     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
1631                                      .addReg(Idx)
1632                                      .addReg(ARM::CPSR)
1633                                      .addReg(Idx)
1634                                      .addImm(1)
1635                                      // Add predicate operands.
1636                                      .addImm(ARMCC::AL)
1637                                      .addReg(0));
1638 
1639     OutStreamer->EmitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
1640     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
1641                                      .addReg(ARM::PC)
1642                                      .addReg(ARM::PC)
1643                                      .addReg(Idx)
1644                                      // Add predicate operands.
1645                                      .addImm(ARMCC::AL)
1646                                      .addReg(0));
1647     return;
1648   }
1649   case ARM::tBR_JTr:
1650   case ARM::BR_JTr: {
1651     // Lower and emit the instruction itself, then the jump table following it.
1652     // mov pc, target
1653     MCInst TmpInst;
1654     unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
1655       ARM::MOVr : ARM::tMOVr;
1656     TmpInst.setOpcode(Opc);
1657     TmpInst.addOperand(MCOperand::createReg(ARM::PC));
1658     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1659     // Add predicate operands.
1660     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
1661     TmpInst.addOperand(MCOperand::createReg(0));
1662     // Add 's' bit operand (always reg0 for this)
1663     if (Opc == ARM::MOVr)
1664       TmpInst.addOperand(MCOperand::createReg(0));
1665     EmitToStreamer(*OutStreamer, TmpInst);
1666     return;
1667   }
1668   case ARM::BR_JTm: {
1669     // Lower and emit the instruction itself, then the jump table following it.
1670     // ldr pc, target
1671     MCInst TmpInst;
1672     if (MI->getOperand(1).getReg() == 0) {
1673       // literal offset
1674       TmpInst.setOpcode(ARM::LDRi12);
1675       TmpInst.addOperand(MCOperand::createReg(ARM::PC));
1676       TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1677       TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
1678     } else {
1679       TmpInst.setOpcode(ARM::LDRrs);
1680       TmpInst.addOperand(MCOperand::createReg(ARM::PC));
1681       TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1682       TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
1683       TmpInst.addOperand(MCOperand::createImm(0));
1684     }
1685     // Add predicate operands.
1686     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
1687     TmpInst.addOperand(MCOperand::createReg(0));
1688     EmitToStreamer(*OutStreamer, TmpInst);
1689     return;
1690   }
1691   case ARM::BR_JTadd: {
1692     // Lower and emit the instruction itself, then the jump table following it.
1693     // add pc, target, idx
1694     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr)
1695       .addReg(ARM::PC)
1696       .addReg(MI->getOperand(0).getReg())
1697       .addReg(MI->getOperand(1).getReg())
1698       // Add predicate operands.
1699       .addImm(ARMCC::AL)
1700       .addReg(0)
1701       // Add 's' bit operand (always reg0 for this)
1702       .addReg(0));
1703     return;
1704   }
1705   case ARM::SPACE:
1706     OutStreamer->EmitZeros(MI->getOperand(1).getImm());
1707     return;
1708   case ARM::TRAP: {
1709     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1710     // FIXME: Remove this special case when they do.
1711     if (!Subtarget->isTargetMachO()) {
1712       uint32_t Val = 0xe7ffdefeUL;
1713       OutStreamer->AddComment("trap");
1714       ATS.emitInst(Val);
1715       return;
1716     }
1717     break;
1718   }
1719   case ARM::TRAPNaCl: {
1720     uint32_t Val = 0xe7fedef0UL;
1721     OutStreamer->AddComment("trap");
1722     ATS.emitInst(Val);
1723     return;
1724   }
1725   case ARM::tTRAP: {
1726     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1727     // FIXME: Remove this special case when they do.
1728     if (!Subtarget->isTargetMachO()) {
1729       uint16_t Val = 0xdefe;
1730       OutStreamer->AddComment("trap");
1731       ATS.emitInst(Val, 'n');
1732       return;
1733     }
1734     break;
1735   }
1736   case ARM::t2Int_eh_sjlj_setjmp:
1737   case ARM::t2Int_eh_sjlj_setjmp_nofp:
1738   case ARM::tInt_eh_sjlj_setjmp: {
1739     // Two incoming args: GPR:$src, GPR:$val
1740     // mov $val, pc
1741     // adds $val, #7
1742     // str $val, [$src, #4]
1743     // movs r0, #0
1744     // b LSJLJEH
1745     // movs r0, #1
1746     // LSJLJEH:
1747     unsigned SrcReg = MI->getOperand(0).getReg();
1748     unsigned ValReg = MI->getOperand(1).getReg();
1749     MCSymbol *Label = OutContext.createTempSymbol("SJLJEH", false, true);
1750     OutStreamer->AddComment("eh_setjmp begin");
1751     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
1752       .addReg(ValReg)
1753       .addReg(ARM::PC)
1754       // Predicate.
1755       .addImm(ARMCC::AL)
1756       .addReg(0));
1757 
1758     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi3)
1759       .addReg(ValReg)
1760       // 's' bit operand
1761       .addReg(ARM::CPSR)
1762       .addReg(ValReg)
1763       .addImm(7)
1764       // Predicate.
1765       .addImm(ARMCC::AL)
1766       .addReg(0));
1767 
1768     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tSTRi)
1769       .addReg(ValReg)
1770       .addReg(SrcReg)
1771       // The offset immediate is #4. The operand value is scaled by 4 for the
1772       // tSTR instruction.
1773       .addImm(1)
1774       // Predicate.
1775       .addImm(ARMCC::AL)
1776       .addReg(0));
1777 
1778     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
1779       .addReg(ARM::R0)
1780       .addReg(ARM::CPSR)
1781       .addImm(0)
1782       // Predicate.
1783       .addImm(ARMCC::AL)
1784       .addReg(0));
1785 
1786     const MCExpr *SymbolExpr = MCSymbolRefExpr::create(Label, OutContext);
1787     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tB)
1788       .addExpr(SymbolExpr)
1789       .addImm(ARMCC::AL)
1790       .addReg(0));
1791 
1792     OutStreamer->AddComment("eh_setjmp end");
1793     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
1794       .addReg(ARM::R0)
1795       .addReg(ARM::CPSR)
1796       .addImm(1)
1797       // Predicate.
1798       .addImm(ARMCC::AL)
1799       .addReg(0));
1800 
1801     OutStreamer->EmitLabel(Label);
1802     return;
1803   }
1804 
1805   case ARM::Int_eh_sjlj_setjmp_nofp:
1806   case ARM::Int_eh_sjlj_setjmp: {
1807     // Two incoming args: GPR:$src, GPR:$val
1808     // add $val, pc, #8
1809     // str $val, [$src, #+4]
1810     // mov r0, #0
1811     // add pc, pc, #0
1812     // mov r0, #1
1813     unsigned SrcReg = MI->getOperand(0).getReg();
1814     unsigned ValReg = MI->getOperand(1).getReg();
1815 
1816     OutStreamer->AddComment("eh_setjmp begin");
1817     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri)
1818       .addReg(ValReg)
1819       .addReg(ARM::PC)
1820       .addImm(8)
1821       // Predicate.
1822       .addImm(ARMCC::AL)
1823       .addReg(0)
1824       // 's' bit operand (always reg0 for this).
1825       .addReg(0));
1826 
1827     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::STRi12)
1828       .addReg(ValReg)
1829       .addReg(SrcReg)
1830       .addImm(4)
1831       // Predicate.
1832       .addImm(ARMCC::AL)
1833       .addReg(0));
1834 
1835     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi)
1836       .addReg(ARM::R0)
1837       .addImm(0)
1838       // Predicate.
1839       .addImm(ARMCC::AL)
1840       .addReg(0)
1841       // 's' bit operand (always reg0 for this).
1842       .addReg(0));
1843 
1844     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri)
1845       .addReg(ARM::PC)
1846       .addReg(ARM::PC)
1847       .addImm(0)
1848       // Predicate.
1849       .addImm(ARMCC::AL)
1850       .addReg(0)
1851       // 's' bit operand (always reg0 for this).
1852       .addReg(0));
1853 
1854     OutStreamer->AddComment("eh_setjmp end");
1855     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi)
1856       .addReg(ARM::R0)
1857       .addImm(1)
1858       // Predicate.
1859       .addImm(ARMCC::AL)
1860       .addReg(0)
1861       // 's' bit operand (always reg0 for this).
1862       .addReg(0));
1863     return;
1864   }
1865   case ARM::Int_eh_sjlj_longjmp: {
1866     // ldr sp, [$src, #8]
1867     // ldr $scratch, [$src, #4]
1868     // ldr r7, [$src]
1869     // bx $scratch
1870     unsigned SrcReg = MI->getOperand(0).getReg();
1871     unsigned ScratchReg = MI->getOperand(1).getReg();
1872     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
1873       .addReg(ARM::SP)
1874       .addReg(SrcReg)
1875       .addImm(8)
1876       // Predicate.
1877       .addImm(ARMCC::AL)
1878       .addReg(0));
1879 
1880     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
1881       .addReg(ScratchReg)
1882       .addReg(SrcReg)
1883       .addImm(4)
1884       // Predicate.
1885       .addImm(ARMCC::AL)
1886       .addReg(0));
1887 
1888     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
1889       .addReg(ARM::R7)
1890       .addReg(SrcReg)
1891       .addImm(0)
1892       // Predicate.
1893       .addImm(ARMCC::AL)
1894       .addReg(0));
1895 
1896     assert(Subtarget->hasV4TOps());
1897     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX)
1898       .addReg(ScratchReg)
1899       // Predicate.
1900       .addImm(ARMCC::AL)
1901       .addReg(0));
1902     return;
1903   }
1904   case ARM::tInt_eh_sjlj_longjmp: {
1905     // ldr $scratch, [$src, #8]
1906     // mov sp, $scratch
1907     // ldr $scratch, [$src, #4]
1908     // ldr r7, [$src]
1909     // bx $scratch
1910     unsigned SrcReg = MI->getOperand(0).getReg();
1911     unsigned ScratchReg = MI->getOperand(1).getReg();
1912 
1913     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
1914       .addReg(ScratchReg)
1915       .addReg(SrcReg)
1916       // The offset immediate is #8. The operand value is scaled by 4 for the
1917       // tLDR instruction.
1918       .addImm(2)
1919       // Predicate.
1920       .addImm(ARMCC::AL)
1921       .addReg(0));
1922 
1923     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
1924       .addReg(ARM::SP)
1925       .addReg(ScratchReg)
1926       // Predicate.
1927       .addImm(ARMCC::AL)
1928       .addReg(0));
1929 
1930     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
1931       .addReg(ScratchReg)
1932       .addReg(SrcReg)
1933       .addImm(1)
1934       // Predicate.
1935       .addImm(ARMCC::AL)
1936       .addReg(0));
1937 
1938     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
1939       .addReg(ARM::R7)
1940       .addReg(SrcReg)
1941       .addImm(0)
1942       // Predicate.
1943       .addImm(ARMCC::AL)
1944       .addReg(0));
1945 
1946     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX)
1947       .addReg(ScratchReg)
1948       // Predicate.
1949       .addImm(ARMCC::AL)
1950       .addReg(0));
1951     return;
1952   }
1953   case ARM::tInt_WIN_eh_sjlj_longjmp: {
1954     // ldr.w r11, [$src, #0]
1955     // ldr.w  sp, [$src, #8]
1956     // ldr.w  pc, [$src, #4]
1957 
1958     unsigned SrcReg = MI->getOperand(0).getReg();
1959 
1960     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
1961                                      .addReg(ARM::R11)
1962                                      .addReg(SrcReg)
1963                                      .addImm(0)
1964                                      // Predicate
1965                                      .addImm(ARMCC::AL)
1966                                      .addReg(0));
1967     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
1968                                      .addReg(ARM::SP)
1969                                      .addReg(SrcReg)
1970                                      .addImm(8)
1971                                      // Predicate
1972                                      .addImm(ARMCC::AL)
1973                                      .addReg(0));
1974     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
1975                                      .addReg(ARM::PC)
1976                                      .addReg(SrcReg)
1977                                      .addImm(4)
1978                                      // Predicate
1979                                      .addImm(ARMCC::AL)
1980                                      .addReg(0));
1981     return;
1982   }
1983   case ARM::PATCHABLE_FUNCTION_ENTER:
1984     LowerPATCHABLE_FUNCTION_ENTER(*MI);
1985     return;
1986   case ARM::PATCHABLE_FUNCTION_EXIT:
1987     LowerPATCHABLE_FUNCTION_EXIT(*MI);
1988     return;
1989   case ARM::PATCHABLE_TAIL_CALL:
1990     LowerPATCHABLE_TAIL_CALL(*MI);
1991     return;
1992   }
1993 
1994   MCInst TmpInst;
1995   LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1996 
1997   EmitToStreamer(*OutStreamer, TmpInst);
1998 }
1999 
2000 //===----------------------------------------------------------------------===//
2001 // Target Registry Stuff
2002 //===----------------------------------------------------------------------===//
2003 
2004 // Force static initialization.
2005 extern "C" void LLVMInitializeARMAsmPrinter() {
2006   RegisterAsmPrinter<ARMAsmPrinter> X(getTheARMLETarget());
2007   RegisterAsmPrinter<ARMAsmPrinter> Y(getTheARMBETarget());
2008   RegisterAsmPrinter<ARMAsmPrinter> A(getTheThumbLETarget());
2009   RegisterAsmPrinter<ARMAsmPrinter> B(getTheThumbBETarget());
2010 }
2011