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