xref: /freebsd/contrib/llvm-project/llvm/lib/Target/ARM/ARMAsmPrinter.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a printer that converts from our internal representation
100b57cec5SDimitry Andric // of machine-dependent LLVM code to GAS-format ARM assembly language.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "ARMAsmPrinter.h"
150b57cec5SDimitry Andric #include "ARM.h"
160b57cec5SDimitry Andric #include "ARMConstantPoolValue.h"
170b57cec5SDimitry Andric #include "ARMMachineFunctionInfo.h"
180b57cec5SDimitry Andric #include "ARMTargetMachine.h"
190b57cec5SDimitry Andric #include "ARMTargetObjectFile.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h"
210b57cec5SDimitry Andric #include "MCTargetDesc/ARMInstPrinter.h"
220b57cec5SDimitry Andric #include "MCTargetDesc/ARMMCExpr.h"
230b57cec5SDimitry Andric #include "TargetInfo/ARMTargetInfo.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
260b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
300b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
310b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
320b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
330b57cec5SDimitry Andric #include "llvm/IR/Module.h"
340b57cec5SDimitry Andric #include "llvm/IR/Type.h"
350b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
360b57cec5SDimitry Andric #include "llvm/MC/MCAssembler.h"
370b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
380b57cec5SDimitry Andric #include "llvm/MC/MCELFStreamer.h"
390b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
400b57cec5SDimitry Andric #include "llvm/MC/MCInstBuilder.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCObjectStreamer.h"
420b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
440b57cec5SDimitry Andric #include "llvm/Support/ARMBuildAttributes.h"
450b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
460b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
470b57cec5SDimitry Andric #include "llvm/Support/TargetParser.h"
480b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
490b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
500b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
510b57cec5SDimitry Andric using namespace llvm;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer"
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric ARMAsmPrinter::ARMAsmPrinter(TargetMachine &TM,
560b57cec5SDimitry Andric                              std::unique_ptr<MCStreamer> Streamer)
570b57cec5SDimitry Andric     : AsmPrinter(TM, std::move(Streamer)), AFI(nullptr), MCP(nullptr),
580b57cec5SDimitry Andric       InConstantPool(false), OptimizationGoals(-1) {}
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric void ARMAsmPrinter::EmitFunctionBodyEnd() {
610b57cec5SDimitry Andric   // Make sure to terminate any constant pools that were at the end
620b57cec5SDimitry Andric   // of the function.
630b57cec5SDimitry Andric   if (!InConstantPool)
640b57cec5SDimitry Andric     return;
650b57cec5SDimitry Andric   InConstantPool = false;
660b57cec5SDimitry Andric   OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric void ARMAsmPrinter::EmitFunctionEntryLabel() {
700b57cec5SDimitry Andric   if (AFI->isThumbFunction()) {
710b57cec5SDimitry Andric     OutStreamer->EmitAssemblerFlag(MCAF_Code16);
720b57cec5SDimitry Andric     OutStreamer->EmitThumbFunc(CurrentFnSym);
730b57cec5SDimitry Andric   } else {
740b57cec5SDimitry Andric     OutStreamer->EmitAssemblerFlag(MCAF_Code32);
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric   OutStreamer->EmitLabel(CurrentFnSym);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric void ARMAsmPrinter::EmitXXStructor(const DataLayout &DL, const Constant *CV) {
800b57cec5SDimitry Andric   uint64_t Size = getDataLayout().getTypeAllocSize(CV->getType());
810b57cec5SDimitry Andric   assert(Size && "C++ constructor pointer had zero size!");
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
840b57cec5SDimitry Andric   assert(GV && "C++ constructor pointer was not a GlobalValue!");
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   const MCExpr *E = MCSymbolRefExpr::create(GetARMGVSymbol(GV,
870b57cec5SDimitry Andric                                                            ARMII::MO_NO_FLAG),
880b57cec5SDimitry Andric                                             (Subtarget->isTargetELF()
890b57cec5SDimitry Andric                                              ? MCSymbolRefExpr::VK_ARM_TARGET1
900b57cec5SDimitry Andric                                              : MCSymbolRefExpr::VK_None),
910b57cec5SDimitry Andric                                             OutContext);
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   OutStreamer->EmitValue(E, Size);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric void ARMAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
970b57cec5SDimitry Andric   if (PromotedGlobals.count(GV))
980b57cec5SDimitry Andric     // The global was promoted into a constant pool. It should not be emitted.
990b57cec5SDimitry Andric     return;
1000b57cec5SDimitry Andric   AsmPrinter::EmitGlobalVariable(GV);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric /// runOnMachineFunction - This uses the EmitInstruction()
1040b57cec5SDimitry Andric /// method to print assembly for each instruction.
1050b57cec5SDimitry Andric ///
1060b57cec5SDimitry Andric bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
1070b57cec5SDimitry Andric   AFI = MF.getInfo<ARMFunctionInfo>();
1080b57cec5SDimitry Andric   MCP = MF.getConstantPool();
1090b57cec5SDimitry Andric   Subtarget = &MF.getSubtarget<ARMSubtarget>();
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   SetupMachineFunction(MF);
1120b57cec5SDimitry Andric   const Function &F = MF.getFunction();
1130b57cec5SDimitry Andric   const TargetMachine& TM = MF.getTarget();
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // Collect all globals that had their storage promoted to a constant pool.
1160b57cec5SDimitry Andric   // Functions are emitted before variables, so this accumulates promoted
1170b57cec5SDimitry Andric   // globals from all functions in PromotedGlobals.
1180b57cec5SDimitry Andric   for (auto *GV : AFI->getGlobalsPromotedToConstantPool())
1190b57cec5SDimitry Andric     PromotedGlobals.insert(GV);
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   // Calculate this function's optimization goal.
1220b57cec5SDimitry Andric   unsigned OptimizationGoal;
1230b57cec5SDimitry Andric   if (F.hasOptNone())
1240b57cec5SDimitry Andric     // For best debugging illusion, speed and small size sacrificed
1250b57cec5SDimitry Andric     OptimizationGoal = 6;
1260b57cec5SDimitry Andric   else if (F.hasMinSize())
1270b57cec5SDimitry Andric     // Aggressively for small size, speed and debug illusion sacrificed
1280b57cec5SDimitry Andric     OptimizationGoal = 4;
1290b57cec5SDimitry Andric   else if (F.hasOptSize())
1300b57cec5SDimitry Andric     // For small size, but speed and debugging illusion preserved
1310b57cec5SDimitry Andric     OptimizationGoal = 3;
1320b57cec5SDimitry Andric   else if (TM.getOptLevel() == CodeGenOpt::Aggressive)
1330b57cec5SDimitry Andric     // Aggressively for speed, small size and debug illusion sacrificed
1340b57cec5SDimitry Andric     OptimizationGoal = 2;
1350b57cec5SDimitry Andric   else if (TM.getOptLevel() > CodeGenOpt::None)
1360b57cec5SDimitry Andric     // For speed, but small size and good debug illusion preserved
1370b57cec5SDimitry Andric     OptimizationGoal = 1;
1380b57cec5SDimitry Andric   else // TM.getOptLevel() == CodeGenOpt::None
1390b57cec5SDimitry Andric     // For good debugging, but speed and small size preserved
1400b57cec5SDimitry Andric     OptimizationGoal = 5;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   // Combine a new optimization goal with existing ones.
1430b57cec5SDimitry Andric   if (OptimizationGoals == -1) // uninitialized goals
1440b57cec5SDimitry Andric     OptimizationGoals = OptimizationGoal;
1450b57cec5SDimitry Andric   else if (OptimizationGoals != (int)OptimizationGoal) // conflicting goals
1460b57cec5SDimitry Andric     OptimizationGoals = 0;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   if (Subtarget->isTargetCOFF()) {
1490b57cec5SDimitry Andric     bool Internal = F.hasInternalLinkage();
1500b57cec5SDimitry Andric     COFF::SymbolStorageClass Scl = Internal ? COFF::IMAGE_SYM_CLASS_STATIC
1510b57cec5SDimitry Andric                                             : COFF::IMAGE_SYM_CLASS_EXTERNAL;
1520b57cec5SDimitry Andric     int Type = COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
1550b57cec5SDimitry Andric     OutStreamer->EmitCOFFSymbolStorageClass(Scl);
1560b57cec5SDimitry Andric     OutStreamer->EmitCOFFSymbolType(Type);
1570b57cec5SDimitry Andric     OutStreamer->EndCOFFSymbolDef();
1580b57cec5SDimitry Andric   }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Emit the rest of the function body.
1610b57cec5SDimitry Andric   EmitFunctionBody();
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   // Emit the XRay table for this function.
1640b57cec5SDimitry Andric   emitXRayTable();
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // If we need V4T thumb mode Register Indirect Jump pads, emit them.
1670b57cec5SDimitry Andric   // These are created per function, rather than per TU, since it's
1680b57cec5SDimitry Andric   // relatively easy to exceed the thumb branch range within a TU.
1690b57cec5SDimitry Andric   if (! ThumbIndirectPads.empty()) {
1700b57cec5SDimitry Andric     OutStreamer->EmitAssemblerFlag(MCAF_Code16);
171*8bcb0991SDimitry Andric     EmitAlignment(Align(2));
1720b57cec5SDimitry Andric     for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) {
1730b57cec5SDimitry Andric       OutStreamer->EmitLabel(TIP.second);
1740b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX)
1750b57cec5SDimitry Andric         .addReg(TIP.first)
1760b57cec5SDimitry Andric         // Add predicate operands.
1770b57cec5SDimitry Andric         .addImm(ARMCC::AL)
1780b57cec5SDimitry Andric         .addReg(0));
1790b57cec5SDimitry Andric     }
1800b57cec5SDimitry Andric     ThumbIndirectPads.clear();
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   // We didn't modify anything.
1840b57cec5SDimitry Andric   return false;
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric void ARMAsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
1880b57cec5SDimitry Andric                                        raw_ostream &O) {
1890b57cec5SDimitry Andric   assert(MO.isGlobal() && "caller should check MO.isGlobal");
1900b57cec5SDimitry Andric   unsigned TF = MO.getTargetFlags();
1910b57cec5SDimitry Andric   if (TF & ARMII::MO_LO16)
1920b57cec5SDimitry Andric     O << ":lower16:";
1930b57cec5SDimitry Andric   else if (TF & ARMII::MO_HI16)
1940b57cec5SDimitry Andric     O << ":upper16:";
1950b57cec5SDimitry Andric   GetARMGVSymbol(MO.getGlobal(), TF)->print(O, MAI);
1960b57cec5SDimitry Andric   printOffset(MO.getOffset(), O);
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
2000b57cec5SDimitry Andric                                  raw_ostream &O) {
2010b57cec5SDimitry Andric   const MachineOperand &MO = MI->getOperand(OpNum);
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   switch (MO.getType()) {
2040b57cec5SDimitry Andric   default: llvm_unreachable("<unknown operand type>");
2050b57cec5SDimitry Andric   case MachineOperand::MO_Register: {
206*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
207*8bcb0991SDimitry Andric     assert(Register::isPhysicalRegister(Reg));
2080b57cec5SDimitry Andric     assert(!MO.getSubReg() && "Subregs should be eliminated!");
2090b57cec5SDimitry Andric     if(ARM::GPRPairRegClass.contains(Reg)) {
2100b57cec5SDimitry Andric       const MachineFunction &MF = *MI->getParent()->getParent();
2110b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2120b57cec5SDimitry Andric       Reg = TRI->getSubReg(Reg, ARM::gsub_0);
2130b57cec5SDimitry Andric     }
2140b57cec5SDimitry Andric     O << ARMInstPrinter::getRegisterName(Reg);
2150b57cec5SDimitry Andric     break;
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric   case MachineOperand::MO_Immediate: {
2180b57cec5SDimitry Andric     O << '#';
2190b57cec5SDimitry Andric     unsigned TF = MO.getTargetFlags();
2200b57cec5SDimitry Andric     if (TF == ARMII::MO_LO16)
2210b57cec5SDimitry Andric       O << ":lower16:";
2220b57cec5SDimitry Andric     else if (TF == ARMII::MO_HI16)
2230b57cec5SDimitry Andric       O << ":upper16:";
2240b57cec5SDimitry Andric     O << MO.getImm();
2250b57cec5SDimitry Andric     break;
2260b57cec5SDimitry Andric   }
2270b57cec5SDimitry Andric   case MachineOperand::MO_MachineBasicBlock:
2280b57cec5SDimitry Andric     MO.getMBB()->getSymbol()->print(O, MAI);
2290b57cec5SDimitry Andric     return;
2300b57cec5SDimitry Andric   case MachineOperand::MO_GlobalAddress: {
2310b57cec5SDimitry Andric     PrintSymbolOperand(MO, O);
2320b57cec5SDimitry Andric     break;
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric   case MachineOperand::MO_ConstantPoolIndex:
2350b57cec5SDimitry Andric     if (Subtarget->genExecuteOnly())
2360b57cec5SDimitry Andric       llvm_unreachable("execute-only should not generate constant pools");
2370b57cec5SDimitry Andric     GetCPISymbol(MO.getIndex())->print(O, MAI);
2380b57cec5SDimitry Andric     break;
2390b57cec5SDimitry Andric   }
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric MCSymbol *ARMAsmPrinter::GetCPISymbol(unsigned CPID) const {
2430b57cec5SDimitry Andric   // The AsmPrinter::GetCPISymbol superclass method tries to use CPID as
2440b57cec5SDimitry Andric   // indexes in MachineConstantPool, which isn't in sync with indexes used here.
2450b57cec5SDimitry Andric   const DataLayout &DL = getDataLayout();
2460b57cec5SDimitry Andric   return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
2470b57cec5SDimitry Andric                                       "CPI" + Twine(getFunctionNumber()) + "_" +
2480b57cec5SDimitry Andric                                       Twine(CPID));
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric //===--------------------------------------------------------------------===//
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric MCSymbol *ARMAsmPrinter::
2540b57cec5SDimitry Andric GetARMJTIPICJumpTableLabel(unsigned uid) const {
2550b57cec5SDimitry Andric   const DataLayout &DL = getDataLayout();
2560b57cec5SDimitry Andric   SmallString<60> Name;
2570b57cec5SDimitry Andric   raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
2580b57cec5SDimitry Andric                             << getFunctionNumber() << '_' << uid;
2590b57cec5SDimitry Andric   return OutContext.getOrCreateSymbol(Name);
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
2630b57cec5SDimitry Andric                                     const char *ExtraCode, raw_ostream &O) {
2640b57cec5SDimitry Andric   // Does this asm operand have a single letter operand modifier?
2650b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
2660b57cec5SDimitry Andric     if (ExtraCode[1] != 0) return true; // Unknown modifier.
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric     switch (ExtraCode[0]) {
2690b57cec5SDimitry Andric     default:
2700b57cec5SDimitry Andric       // See if this is a generic print operand
2710b57cec5SDimitry Andric       return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
2720b57cec5SDimitry Andric     case 'P': // Print a VFP double precision register.
2730b57cec5SDimitry Andric     case 'q': // Print a NEON quad precision register.
2740b57cec5SDimitry Andric       printOperand(MI, OpNum, O);
2750b57cec5SDimitry Andric       return false;
2760b57cec5SDimitry Andric     case 'y': // Print a VFP single precision register as indexed double.
2770b57cec5SDimitry Andric       if (MI->getOperand(OpNum).isReg()) {
278*8bcb0991SDimitry Andric         Register Reg = MI->getOperand(OpNum).getReg();
2790b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
2800b57cec5SDimitry Andric         // Find the 'd' register that has this 's' register as a sub-register,
2810b57cec5SDimitry Andric         // and determine the lane number.
2820b57cec5SDimitry Andric         for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
2830b57cec5SDimitry Andric           if (!ARM::DPRRegClass.contains(*SR))
2840b57cec5SDimitry Andric             continue;
2850b57cec5SDimitry Andric           bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
2860b57cec5SDimitry Andric           O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
2870b57cec5SDimitry Andric           return false;
2880b57cec5SDimitry Andric         }
2890b57cec5SDimitry Andric       }
2900b57cec5SDimitry Andric       return true;
2910b57cec5SDimitry Andric     case 'B': // Bitwise inverse of integer or symbol without a preceding #.
2920b57cec5SDimitry Andric       if (!MI->getOperand(OpNum).isImm())
2930b57cec5SDimitry Andric         return true;
2940b57cec5SDimitry Andric       O << ~(MI->getOperand(OpNum).getImm());
2950b57cec5SDimitry Andric       return false;
2960b57cec5SDimitry Andric     case 'L': // The low 16 bits of an immediate constant.
2970b57cec5SDimitry Andric       if (!MI->getOperand(OpNum).isImm())
2980b57cec5SDimitry Andric         return true;
2990b57cec5SDimitry Andric       O << (MI->getOperand(OpNum).getImm() & 0xffff);
3000b57cec5SDimitry Andric       return false;
3010b57cec5SDimitry Andric     case 'M': { // A register range suitable for LDM/STM.
3020b57cec5SDimitry Andric       if (!MI->getOperand(OpNum).isReg())
3030b57cec5SDimitry Andric         return true;
3040b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(OpNum);
305*8bcb0991SDimitry Andric       Register RegBegin = MO.getReg();
3060b57cec5SDimitry Andric       // This takes advantage of the 2 operand-ness of ldm/stm and that we've
3070b57cec5SDimitry Andric       // already got the operands in registers that are operands to the
3080b57cec5SDimitry Andric       // inline asm statement.
3090b57cec5SDimitry Andric       O << "{";
3100b57cec5SDimitry Andric       if (ARM::GPRPairRegClass.contains(RegBegin)) {
3110b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
312*8bcb0991SDimitry Andric         Register Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
3130b57cec5SDimitry Andric         O << ARMInstPrinter::getRegisterName(Reg0) << ", ";
3140b57cec5SDimitry Andric         RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
3150b57cec5SDimitry Andric       }
3160b57cec5SDimitry Andric       O << ARMInstPrinter::getRegisterName(RegBegin);
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric       // FIXME: The register allocator not only may not have given us the
3190b57cec5SDimitry Andric       // registers in sequence, but may not be in ascending registers. This
3200b57cec5SDimitry Andric       // will require changes in the register allocator that'll need to be
3210b57cec5SDimitry Andric       // propagated down here if the operands change.
3220b57cec5SDimitry Andric       unsigned RegOps = OpNum + 1;
3230b57cec5SDimitry Andric       while (MI->getOperand(RegOps).isReg()) {
3240b57cec5SDimitry Andric         O << ", "
3250b57cec5SDimitry Andric           << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
3260b57cec5SDimitry Andric         RegOps++;
3270b57cec5SDimitry Andric       }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric       O << "}";
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric       return false;
3320b57cec5SDimitry Andric     }
3330b57cec5SDimitry Andric     case 'R': // The most significant register of a pair.
3340b57cec5SDimitry Andric     case 'Q': { // The least significant register of a pair.
3350b57cec5SDimitry Andric       if (OpNum == 0)
3360b57cec5SDimitry Andric         return true;
3370b57cec5SDimitry Andric       const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
3380b57cec5SDimitry Andric       if (!FlagsOP.isImm())
3390b57cec5SDimitry Andric         return true;
3400b57cec5SDimitry Andric       unsigned Flags = FlagsOP.getImm();
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric       // This operand may not be the one that actually provides the register. If
3430b57cec5SDimitry Andric       // it's tied to a previous one then we should refer instead to that one
3440b57cec5SDimitry Andric       // for registers and their classes.
3450b57cec5SDimitry Andric       unsigned TiedIdx;
3460b57cec5SDimitry Andric       if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
3470b57cec5SDimitry Andric         for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
3480b57cec5SDimitry Andric           unsigned OpFlags = MI->getOperand(OpNum).getImm();
3490b57cec5SDimitry Andric           OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
3500b57cec5SDimitry Andric         }
3510b57cec5SDimitry Andric         Flags = MI->getOperand(OpNum).getImm();
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric         // Later code expects OpNum to be pointing at the register rather than
3540b57cec5SDimitry Andric         // the flags.
3550b57cec5SDimitry Andric         OpNum += 1;
3560b57cec5SDimitry Andric       }
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric       unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
3590b57cec5SDimitry Andric       unsigned RC;
3600b57cec5SDimitry Andric       bool FirstHalf;
3610b57cec5SDimitry Andric       const ARMBaseTargetMachine &ATM =
3620b57cec5SDimitry Andric         static_cast<const ARMBaseTargetMachine &>(TM);
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric       // 'Q' should correspond to the low order register and 'R' to the high
3650b57cec5SDimitry Andric       // order register.  Whether this corresponds to the upper or lower half
3660b57cec5SDimitry Andric       // depends on the endianess mode.
3670b57cec5SDimitry Andric       if (ExtraCode[0] == 'Q')
3680b57cec5SDimitry Andric         FirstHalf = ATM.isLittleEndian();
3690b57cec5SDimitry Andric       else
3700b57cec5SDimitry Andric         // ExtraCode[0] == 'R'.
3710b57cec5SDimitry Andric         FirstHalf = !ATM.isLittleEndian();
3720b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
3730b57cec5SDimitry Andric       if (InlineAsm::hasRegClassConstraint(Flags, RC) &&
3740b57cec5SDimitry Andric           ARM::GPRPairRegClass.hasSubClassEq(TRI->getRegClass(RC))) {
3750b57cec5SDimitry Andric         if (NumVals != 1)
3760b57cec5SDimitry Andric           return true;
3770b57cec5SDimitry Andric         const MachineOperand &MO = MI->getOperand(OpNum);
3780b57cec5SDimitry Andric         if (!MO.isReg())
3790b57cec5SDimitry Andric           return true;
3800b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
381*8bcb0991SDimitry Andric         Register Reg =
382*8bcb0991SDimitry Andric             TRI->getSubReg(MO.getReg(), FirstHalf ? ARM::gsub_0 : ARM::gsub_1);
3830b57cec5SDimitry Andric         O << ARMInstPrinter::getRegisterName(Reg);
3840b57cec5SDimitry Andric         return false;
3850b57cec5SDimitry Andric       }
3860b57cec5SDimitry Andric       if (NumVals != 2)
3870b57cec5SDimitry Andric         return true;
3880b57cec5SDimitry Andric       unsigned RegOp = FirstHalf ? OpNum : OpNum + 1;
3890b57cec5SDimitry Andric       if (RegOp >= MI->getNumOperands())
3900b57cec5SDimitry Andric         return true;
3910b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(RegOp);
3920b57cec5SDimitry Andric       if (!MO.isReg())
3930b57cec5SDimitry Andric         return true;
394*8bcb0991SDimitry Andric       Register Reg = MO.getReg();
3950b57cec5SDimitry Andric       O << ARMInstPrinter::getRegisterName(Reg);
3960b57cec5SDimitry Andric       return false;
3970b57cec5SDimitry Andric     }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric     case 'e': // The low doubleword register of a NEON quad register.
4000b57cec5SDimitry Andric     case 'f': { // The high doubleword register of a NEON quad register.
4010b57cec5SDimitry Andric       if (!MI->getOperand(OpNum).isReg())
4020b57cec5SDimitry Andric         return true;
403*8bcb0991SDimitry Andric       Register Reg = MI->getOperand(OpNum).getReg();
4040b57cec5SDimitry Andric       if (!ARM::QPRRegClass.contains(Reg))
4050b57cec5SDimitry Andric         return true;
4060b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
407*8bcb0991SDimitry Andric       Register SubReg =
408*8bcb0991SDimitry Andric           TRI->getSubReg(Reg, ExtraCode[0] == 'e' ? ARM::dsub_0 : ARM::dsub_1);
4090b57cec5SDimitry Andric       O << ARMInstPrinter::getRegisterName(SubReg);
4100b57cec5SDimitry Andric       return false;
4110b57cec5SDimitry Andric     }
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric     // This modifier is not yet supported.
4140b57cec5SDimitry Andric     case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
4150b57cec5SDimitry Andric       return true;
4160b57cec5SDimitry Andric     case 'H': { // The highest-numbered register of a pair.
4170b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(OpNum);
4180b57cec5SDimitry Andric       if (!MO.isReg())
4190b57cec5SDimitry Andric         return true;
4200b57cec5SDimitry Andric       const MachineFunction &MF = *MI->getParent()->getParent();
4210b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
422*8bcb0991SDimitry Andric       Register Reg = MO.getReg();
4230b57cec5SDimitry Andric       if(!ARM::GPRPairRegClass.contains(Reg))
4240b57cec5SDimitry Andric         return false;
4250b57cec5SDimitry Andric       Reg = TRI->getSubReg(Reg, ARM::gsub_1);
4260b57cec5SDimitry Andric       O << ARMInstPrinter::getRegisterName(Reg);
4270b57cec5SDimitry Andric       return false;
4280b57cec5SDimitry Andric     }
4290b57cec5SDimitry Andric     }
4300b57cec5SDimitry Andric   }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   printOperand(MI, OpNum, O);
4330b57cec5SDimitry Andric   return false;
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
4370b57cec5SDimitry Andric                                           unsigned OpNum, const char *ExtraCode,
4380b57cec5SDimitry Andric                                           raw_ostream &O) {
4390b57cec5SDimitry Andric   // Does this asm operand have a single letter operand modifier?
4400b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
4410b57cec5SDimitry Andric     if (ExtraCode[1] != 0) return true; // Unknown modifier.
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric     switch (ExtraCode[0]) {
4440b57cec5SDimitry Andric       case 'A': // A memory operand for a VLD1/VST1 instruction.
4450b57cec5SDimitry Andric       default: return true;  // Unknown modifier.
4460b57cec5SDimitry Andric       case 'm': // The base register of a memory operand.
4470b57cec5SDimitry Andric         if (!MI->getOperand(OpNum).isReg())
4480b57cec5SDimitry Andric           return true;
4490b57cec5SDimitry Andric         O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
4500b57cec5SDimitry Andric         return false;
4510b57cec5SDimitry Andric     }
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   const MachineOperand &MO = MI->getOperand(OpNum);
4550b57cec5SDimitry Andric   assert(MO.isReg() && "unexpected inline asm memory operand");
4560b57cec5SDimitry Andric   O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
4570b57cec5SDimitry Andric   return false;
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric static bool isThumb(const MCSubtargetInfo& STI) {
4610b57cec5SDimitry Andric   return STI.getFeatureBits()[ARM::ModeThumb];
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
4650b57cec5SDimitry Andric                                      const MCSubtargetInfo *EndInfo) const {
4660b57cec5SDimitry Andric   // If either end mode is unknown (EndInfo == NULL) or different than
4670b57cec5SDimitry Andric   // the start mode, then restore the start mode.
4680b57cec5SDimitry Andric   const bool WasThumb = isThumb(StartInfo);
4690b57cec5SDimitry Andric   if (!EndInfo || WasThumb != isThumb(*EndInfo)) {
4700b57cec5SDimitry Andric     OutStreamer->EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
4710b57cec5SDimitry Andric   }
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
4750b57cec5SDimitry Andric   const Triple &TT = TM.getTargetTriple();
4760b57cec5SDimitry Andric   // Use unified assembler syntax.
4770b57cec5SDimitry Andric   OutStreamer->EmitAssemblerFlag(MCAF_SyntaxUnified);
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   // Emit ARM Build Attributes
4800b57cec5SDimitry Andric   if (TT.isOSBinFormatELF())
4810b57cec5SDimitry Andric     emitAttributes();
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   // Use the triple's architecture and subarchitecture to determine
4840b57cec5SDimitry Andric   // if we're thumb for the purposes of the top level code16 assembler
4850b57cec5SDimitry Andric   // flag.
4860b57cec5SDimitry Andric   if (!M.getModuleInlineAsm().empty() && TT.isThumb())
4870b57cec5SDimitry Andric     OutStreamer->EmitAssemblerFlag(MCAF_Code16);
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric static void
4910b57cec5SDimitry Andric emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
4920b57cec5SDimitry Andric                          MachineModuleInfoImpl::StubValueTy &MCSym) {
4930b57cec5SDimitry Andric   // L_foo$stub:
4940b57cec5SDimitry Andric   OutStreamer.EmitLabel(StubLabel);
4950b57cec5SDimitry Andric   //   .indirect_symbol _foo
4960b57cec5SDimitry Andric   OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   if (MCSym.getInt())
4990b57cec5SDimitry Andric     // External to current translation unit.
5000b57cec5SDimitry Andric     OutStreamer.EmitIntValue(0, 4/*size*/);
5010b57cec5SDimitry Andric   else
5020b57cec5SDimitry Andric     // Internal to current translation unit.
5030b57cec5SDimitry Andric     //
5040b57cec5SDimitry Andric     // When we place the LSDA into the TEXT section, the type info
5050b57cec5SDimitry Andric     // pointers need to be indirect and pc-rel. We accomplish this by
5060b57cec5SDimitry Andric     // using NLPs; however, sometimes the types are local to the file.
5070b57cec5SDimitry Andric     // We need to fill in the value for the NLP in those cases.
5080b57cec5SDimitry Andric     OutStreamer.EmitValue(
5090b57cec5SDimitry Andric         MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
5100b57cec5SDimitry Andric         4 /*size*/);
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
5150b57cec5SDimitry Andric   const Triple &TT = TM.getTargetTriple();
5160b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO()) {
5170b57cec5SDimitry Andric     // All darwin targets use mach-o.
5180b57cec5SDimitry Andric     const TargetLoweringObjectFileMachO &TLOFMacho =
5190b57cec5SDimitry Andric       static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
5200b57cec5SDimitry Andric     MachineModuleInfoMachO &MMIMacho =
5210b57cec5SDimitry Andric       MMI->getObjFileInfo<MachineModuleInfoMachO>();
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric     // Output non-lazy-pointers for external and common global variables.
5240b57cec5SDimitry Andric     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric     if (!Stubs.empty()) {
5270b57cec5SDimitry Andric       // Switch with ".non_lazy_symbol_pointer" directive.
5280b57cec5SDimitry Andric       OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
529*8bcb0991SDimitry Andric       EmitAlignment(Align(4));
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric       for (auto &Stub : Stubs)
5320b57cec5SDimitry Andric         emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric       Stubs.clear();
5350b57cec5SDimitry Andric       OutStreamer->AddBlankLine();
5360b57cec5SDimitry Andric     }
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric     Stubs = MMIMacho.GetThreadLocalGVStubList();
5390b57cec5SDimitry Andric     if (!Stubs.empty()) {
5400b57cec5SDimitry Andric       // Switch with ".non_lazy_symbol_pointer" directive.
5410b57cec5SDimitry Andric       OutStreamer->SwitchSection(TLOFMacho.getThreadLocalPointerSection());
542*8bcb0991SDimitry Andric       EmitAlignment(Align(4));
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric       for (auto &Stub : Stubs)
5450b57cec5SDimitry Andric         emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric       Stubs.clear();
5480b57cec5SDimitry Andric       OutStreamer->AddBlankLine();
5490b57cec5SDimitry Andric     }
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric     // Funny Darwin hack: This flag tells the linker that no global symbols
5520b57cec5SDimitry Andric     // contain code that falls through to other global symbols (e.g. the obvious
5530b57cec5SDimitry Andric     // implementation of multiple entry points).  If this doesn't occur, the
5540b57cec5SDimitry Andric     // linker can safely perform dead code stripping.  Since LLVM never
5550b57cec5SDimitry Andric     // generates code that does this, it is always safe to set.
5560b57cec5SDimitry Andric     OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
5570b57cec5SDimitry Andric   }
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric   // The last attribute to be emitted is ABI_optimization_goals
5600b57cec5SDimitry Andric   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
5610b57cec5SDimitry Andric   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   if (OptimizationGoals > 0 &&
5640b57cec5SDimitry Andric       (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
5650b57cec5SDimitry Andric        Subtarget->isTargetMuslAEABI()))
5660b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_optimization_goals, OptimizationGoals);
5670b57cec5SDimitry Andric   OptimizationGoals = -1;
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   ATS.finishAttributeSection();
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5730b57cec5SDimitry Andric // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
5740b57cec5SDimitry Andric // FIXME:
5750b57cec5SDimitry Andric // The following seem like one-off assembler flags, but they actually need
5760b57cec5SDimitry Andric // to appear in the .ARM.attributes section in ELF.
5770b57cec5SDimitry Andric // Instead of subclassing the MCELFStreamer, we do the work here.
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric // Returns true if all functions have the same function attribute value.
5800b57cec5SDimitry Andric // It also returns true when the module has no functions.
5810b57cec5SDimitry Andric static bool checkFunctionsAttributeConsistency(const Module &M, StringRef Attr,
5820b57cec5SDimitry Andric                                                StringRef Value) {
5830b57cec5SDimitry Andric   return !any_of(M, [&](const Function &F) {
5840b57cec5SDimitry Andric     return F.getFnAttribute(Attr).getValueAsString() != Value;
5850b57cec5SDimitry Andric   });
5860b57cec5SDimitry Andric }
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric void ARMAsmPrinter::emitAttributes() {
5890b57cec5SDimitry Andric   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
5900b57cec5SDimitry Andric   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   ATS.emitTextAttribute(ARMBuildAttrs::conformance, "2.09");
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric   ATS.switchVendor("aeabi");
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   // Compute ARM ELF Attributes based on the default subtarget that
5970b57cec5SDimitry Andric   // we'd have constructed. The existing ARM behavior isn't LTO clean
5980b57cec5SDimitry Andric   // anyhow.
5990b57cec5SDimitry Andric   // FIXME: For ifunc related functions we could iterate over and look
6000b57cec5SDimitry Andric   // for a feature string that doesn't match the default one.
6010b57cec5SDimitry Andric   const Triple &TT = TM.getTargetTriple();
6020b57cec5SDimitry Andric   StringRef CPU = TM.getTargetCPU();
6030b57cec5SDimitry Andric   StringRef FS = TM.getTargetFeatureString();
6040b57cec5SDimitry Andric   std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
6050b57cec5SDimitry Andric   if (!FS.empty()) {
6060b57cec5SDimitry Andric     if (!ArchFS.empty())
6070b57cec5SDimitry Andric       ArchFS = (Twine(ArchFS) + "," + FS).str();
6080b57cec5SDimitry Andric     else
6090b57cec5SDimitry Andric       ArchFS = FS;
6100b57cec5SDimitry Andric   }
6110b57cec5SDimitry Andric   const ARMBaseTargetMachine &ATM =
6120b57cec5SDimitry Andric       static_cast<const ARMBaseTargetMachine &>(TM);
6130b57cec5SDimitry Andric   const ARMSubtarget STI(TT, CPU, ArchFS, ATM, ATM.isLittleEndian());
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric   // Emit build attributes for the available hardware.
6160b57cec5SDimitry Andric   ATS.emitTargetAttributes(STI);
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   // RW data addressing.
6190b57cec5SDimitry Andric   if (isPositionIndependent()) {
6200b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
6210b57cec5SDimitry Andric                       ARMBuildAttrs::AddressRWPCRel);
6220b57cec5SDimitry Andric   } else if (STI.isRWPI()) {
6230b57cec5SDimitry Andric     // RWPI specific attributes.
6240b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
6250b57cec5SDimitry Andric                       ARMBuildAttrs::AddressRWSBRel);
6260b57cec5SDimitry Andric   }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   // RO data addressing.
6290b57cec5SDimitry Andric   if (isPositionIndependent() || STI.isROPI()) {
6300b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data,
6310b57cec5SDimitry Andric                       ARMBuildAttrs::AddressROPCRel);
6320b57cec5SDimitry Andric   }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric   // GOT use.
6350b57cec5SDimitry Andric   if (isPositionIndependent()) {
6360b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
6370b57cec5SDimitry Andric                       ARMBuildAttrs::AddressGOT);
6380b57cec5SDimitry Andric   } else {
6390b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
6400b57cec5SDimitry Andric                       ARMBuildAttrs::AddressDirect);
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric   // Set FP Denormals.
6440b57cec5SDimitry Andric   if (checkFunctionsAttributeConsistency(*MMI->getModule(),
6450b57cec5SDimitry Andric                                          "denormal-fp-math",
6460b57cec5SDimitry Andric                                          "preserve-sign") ||
6470b57cec5SDimitry Andric       TM.Options.FPDenormalMode == FPDenormal::PreserveSign)
6480b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
6490b57cec5SDimitry Andric                       ARMBuildAttrs::PreserveFPSign);
6500b57cec5SDimitry Andric   else if (checkFunctionsAttributeConsistency(*MMI->getModule(),
6510b57cec5SDimitry Andric                                               "denormal-fp-math",
6520b57cec5SDimitry Andric                                               "positive-zero") ||
6530b57cec5SDimitry Andric            TM.Options.FPDenormalMode == FPDenormal::PositiveZero)
6540b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
6550b57cec5SDimitry Andric                       ARMBuildAttrs::PositiveZero);
6560b57cec5SDimitry Andric   else if (!TM.Options.UnsafeFPMath)
6570b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
6580b57cec5SDimitry Andric                       ARMBuildAttrs::IEEEDenormals);
6590b57cec5SDimitry Andric   else {
6600b57cec5SDimitry Andric     if (!STI.hasVFP2Base()) {
6610b57cec5SDimitry Andric       // When the target doesn't have an FPU (by design or
6620b57cec5SDimitry Andric       // intention), the assumptions made on the software support
6630b57cec5SDimitry Andric       // mirror that of the equivalent hardware support *if it
6640b57cec5SDimitry Andric       // existed*. For v7 and better we indicate that denormals are
6650b57cec5SDimitry Andric       // flushed preserving sign, and for V6 we indicate that
6660b57cec5SDimitry Andric       // denormals are flushed to positive zero.
6670b57cec5SDimitry Andric       if (STI.hasV7Ops())
6680b57cec5SDimitry Andric         ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
6690b57cec5SDimitry Andric                           ARMBuildAttrs::PreserveFPSign);
6700b57cec5SDimitry Andric     } else if (STI.hasVFP3Base()) {
6710b57cec5SDimitry Andric       // In VFPv4, VFPv4U, VFPv3, or VFPv3U, it is preserved. That is,
6720b57cec5SDimitry Andric       // the sign bit of the zero matches the sign bit of the input or
6730b57cec5SDimitry Andric       // result that is being flushed to zero.
6740b57cec5SDimitry Andric       ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
6750b57cec5SDimitry Andric                         ARMBuildAttrs::PreserveFPSign);
6760b57cec5SDimitry Andric     }
6770b57cec5SDimitry Andric     // For VFPv2 implementations it is implementation defined as
6780b57cec5SDimitry Andric     // to whether denormals are flushed to positive zero or to
6790b57cec5SDimitry Andric     // whatever the sign of zero is (ARM v7AR ARM 2.7.5). Historically
6800b57cec5SDimitry Andric     // LLVM has chosen to flush this to positive zero (most likely for
6810b57cec5SDimitry Andric     // GCC compatibility), so that's the chosen value here (the
6820b57cec5SDimitry Andric     // absence of its emission implies zero).
6830b57cec5SDimitry Andric   }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   // Set FP exceptions and rounding
6860b57cec5SDimitry Andric   if (checkFunctionsAttributeConsistency(*MMI->getModule(),
6870b57cec5SDimitry Andric                                          "no-trapping-math", "true") ||
6880b57cec5SDimitry Andric       TM.Options.NoTrappingFPMath)
6890b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
6900b57cec5SDimitry Andric                       ARMBuildAttrs::Not_Allowed);
6910b57cec5SDimitry Andric   else if (!TM.Options.UnsafeFPMath) {
6920b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, ARMBuildAttrs::Allowed);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric     // If the user has permitted this code to choose the IEEE 754
6950b57cec5SDimitry Andric     // rounding at run-time, emit the rounding attribute.
6960b57cec5SDimitry Andric     if (TM.Options.HonorSignDependentRoundingFPMathOption)
6970b57cec5SDimitry Andric       ATS.emitAttribute(ARMBuildAttrs::ABI_FP_rounding, ARMBuildAttrs::Allowed);
6980b57cec5SDimitry Andric   }
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric   // TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath is the
7010b57cec5SDimitry Andric   // equivalent of GCC's -ffinite-math-only flag.
7020b57cec5SDimitry Andric   if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
7030b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
7040b57cec5SDimitry Andric                       ARMBuildAttrs::Allowed);
7050b57cec5SDimitry Andric   else
7060b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
7070b57cec5SDimitry Andric                       ARMBuildAttrs::AllowIEEE754);
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric   // FIXME: add more flags to ARMBuildAttributes.h
7100b57cec5SDimitry Andric   // 8-bytes alignment stuff.
7110b57cec5SDimitry Andric   ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1);
7120b57cec5SDimitry Andric   ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1);
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric   // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
7150b57cec5SDimitry Andric   if (STI.isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
7160b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   // FIXME: To support emitting this build attribute as GCC does, the
7190b57cec5SDimitry Andric   // -mfp16-format option and associated plumbing must be
7200b57cec5SDimitry Andric   // supported. For now the __fp16 type is exposed by default, so this
7210b57cec5SDimitry Andric   // attribute should be emitted with value 1.
7220b57cec5SDimitry Andric   ATS.emitAttribute(ARMBuildAttrs::ABI_FP_16bit_format,
7230b57cec5SDimitry Andric                     ARMBuildAttrs::FP16FormatIEEE);
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric   if (MMI) {
7260b57cec5SDimitry Andric     if (const Module *SourceModule = MMI->getModule()) {
7270b57cec5SDimitry Andric       // ABI_PCS_wchar_t to indicate wchar_t width
7280b57cec5SDimitry Andric       // FIXME: There is no way to emit value 0 (wchar_t prohibited).
7290b57cec5SDimitry Andric       if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>(
7300b57cec5SDimitry Andric               SourceModule->getModuleFlag("wchar_size"))) {
7310b57cec5SDimitry Andric         int WCharWidth = WCharWidthValue->getZExtValue();
7320b57cec5SDimitry Andric         assert((WCharWidth == 2 || WCharWidth == 4) &&
7330b57cec5SDimitry Andric                "wchar_t width must be 2 or 4 bytes");
7340b57cec5SDimitry Andric         ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t, WCharWidth);
7350b57cec5SDimitry Andric       }
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric       // ABI_enum_size to indicate enum width
7380b57cec5SDimitry Andric       // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
7390b57cec5SDimitry Andric       //        (all enums contain a value needing 32 bits to encode).
7400b57cec5SDimitry Andric       if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>(
7410b57cec5SDimitry Andric               SourceModule->getModuleFlag("min_enum_size"))) {
7420b57cec5SDimitry Andric         int EnumWidth = EnumWidthValue->getZExtValue();
7430b57cec5SDimitry Andric         assert((EnumWidth == 1 || EnumWidth == 4) &&
7440b57cec5SDimitry Andric                "Minimum enum width must be 1 or 4 bytes");
7450b57cec5SDimitry Andric         int EnumBuildAttr = EnumWidth == 1 ? 1 : 2;
7460b57cec5SDimitry Andric         ATS.emitAttribute(ARMBuildAttrs::ABI_enum_size, EnumBuildAttr);
7470b57cec5SDimitry Andric       }
7480b57cec5SDimitry Andric     }
7490b57cec5SDimitry Andric   }
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric   // We currently do not support using R9 as the TLS pointer.
7520b57cec5SDimitry Andric   if (STI.isRWPI())
7530b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
7540b57cec5SDimitry Andric                       ARMBuildAttrs::R9IsSB);
7550b57cec5SDimitry Andric   else if (STI.isR9Reserved())
7560b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
7570b57cec5SDimitry Andric                       ARMBuildAttrs::R9Reserved);
7580b57cec5SDimitry Andric   else
7590b57cec5SDimitry Andric     ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
7600b57cec5SDimitry Andric                       ARMBuildAttrs::R9IsGPR);
7610b57cec5SDimitry Andric }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric static MCSymbol *getBFLabel(StringRef Prefix, unsigned FunctionNumber,
7660b57cec5SDimitry Andric                              unsigned LabelId, MCContext &Ctx) {
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
7690b57cec5SDimitry Andric                        + "BF" + Twine(FunctionNumber) + "_" + Twine(LabelId));
7700b57cec5SDimitry Andric   return Label;
7710b57cec5SDimitry Andric }
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric static MCSymbol *getPICLabel(StringRef Prefix, unsigned FunctionNumber,
7740b57cec5SDimitry Andric                              unsigned LabelId, MCContext &Ctx) {
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric   MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
7770b57cec5SDimitry Andric                        + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
7780b57cec5SDimitry Andric   return Label;
7790b57cec5SDimitry Andric }
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric static MCSymbolRefExpr::VariantKind
7820b57cec5SDimitry Andric getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
7830b57cec5SDimitry Andric   switch (Modifier) {
7840b57cec5SDimitry Andric   case ARMCP::no_modifier:
7850b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_None;
7860b57cec5SDimitry Andric   case ARMCP::TLSGD:
7870b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_TLSGD;
7880b57cec5SDimitry Andric   case ARMCP::TPOFF:
7890b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_TPOFF;
7900b57cec5SDimitry Andric   case ARMCP::GOTTPOFF:
7910b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_GOTTPOFF;
7920b57cec5SDimitry Andric   case ARMCP::SBREL:
7930b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_ARM_SBREL;
7940b57cec5SDimitry Andric   case ARMCP::GOT_PREL:
7950b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_ARM_GOT_PREL;
7960b57cec5SDimitry Andric   case ARMCP::SECREL:
7970b57cec5SDimitry Andric     return MCSymbolRefExpr::VK_SECREL;
7980b57cec5SDimitry Andric   }
7990b57cec5SDimitry Andric   llvm_unreachable("Invalid ARMCPModifier!");
8000b57cec5SDimitry Andric }
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
8030b57cec5SDimitry Andric                                         unsigned char TargetFlags) {
8040b57cec5SDimitry Andric   if (Subtarget->isTargetMachO()) {
8050b57cec5SDimitry Andric     bool IsIndirect =
8060b57cec5SDimitry Andric         (TargetFlags & ARMII::MO_NONLAZY) && Subtarget->isGVIndirectSymbol(GV);
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric     if (!IsIndirect)
8090b57cec5SDimitry Andric       return getSymbol(GV);
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric     // FIXME: Remove this when Darwin transition to @GOT like syntax.
8120b57cec5SDimitry Andric     MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
8130b57cec5SDimitry Andric     MachineModuleInfoMachO &MMIMachO =
8140b57cec5SDimitry Andric       MMI->getObjFileInfo<MachineModuleInfoMachO>();
8150b57cec5SDimitry Andric     MachineModuleInfoImpl::StubValueTy &StubSym =
8160b57cec5SDimitry Andric         GV->isThreadLocal() ? MMIMachO.getThreadLocalGVStubEntry(MCSym)
8170b57cec5SDimitry Andric                             : MMIMachO.getGVStubEntry(MCSym);
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric     if (!StubSym.getPointer())
8200b57cec5SDimitry Andric       StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
8210b57cec5SDimitry Andric                                                    !GV->hasInternalLinkage());
8220b57cec5SDimitry Andric     return MCSym;
8230b57cec5SDimitry Andric   } else if (Subtarget->isTargetCOFF()) {
8240b57cec5SDimitry Andric     assert(Subtarget->isTargetWindows() &&
8250b57cec5SDimitry Andric            "Windows is the only supported COFF target");
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric     bool IsIndirect =
8280b57cec5SDimitry Andric         (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB));
8290b57cec5SDimitry Andric     if (!IsIndirect)
8300b57cec5SDimitry Andric       return getSymbol(GV);
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric     SmallString<128> Name;
8330b57cec5SDimitry Andric     if (TargetFlags & ARMII::MO_DLLIMPORT)
8340b57cec5SDimitry Andric       Name = "__imp_";
8350b57cec5SDimitry Andric     else if (TargetFlags & ARMII::MO_COFFSTUB)
8360b57cec5SDimitry Andric       Name = ".refptr.";
8370b57cec5SDimitry Andric     getNameWithPrefix(Name, GV);
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric     MCSymbol *MCSym = OutContext.getOrCreateSymbol(Name);
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric     if (TargetFlags & ARMII::MO_COFFSTUB) {
8420b57cec5SDimitry Andric       MachineModuleInfoCOFF &MMICOFF =
8430b57cec5SDimitry Andric           MMI->getObjFileInfo<MachineModuleInfoCOFF>();
8440b57cec5SDimitry Andric       MachineModuleInfoImpl::StubValueTy &StubSym =
8450b57cec5SDimitry Andric           MMICOFF.getGVStubEntry(MCSym);
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric       if (!StubSym.getPointer())
8480b57cec5SDimitry Andric         StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV), true);
8490b57cec5SDimitry Andric     }
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric     return MCSym;
8520b57cec5SDimitry Andric   } else if (Subtarget->isTargetELF()) {
8530b57cec5SDimitry Andric     return getSymbol(GV);
8540b57cec5SDimitry Andric   }
8550b57cec5SDimitry Andric   llvm_unreachable("unexpected target");
8560b57cec5SDimitry Andric }
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric void ARMAsmPrinter::
8590b57cec5SDimitry Andric EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
8600b57cec5SDimitry Andric   const DataLayout &DL = getDataLayout();
8610b57cec5SDimitry Andric   int Size = DL.getTypeAllocSize(MCPV->getType());
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric   ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric   if (ACPV->isPromotedGlobal()) {
8660b57cec5SDimitry Andric     // This constant pool entry is actually a global whose storage has been
8670b57cec5SDimitry Andric     // promoted into the constant pool. This global may be referenced still
8680b57cec5SDimitry Andric     // by debug information, and due to the way AsmPrinter is set up, the debug
8690b57cec5SDimitry Andric     // info is immutable by the time we decide to promote globals to constant
8700b57cec5SDimitry Andric     // pools. Because of this, we need to ensure we emit a symbol for the global
8710b57cec5SDimitry Andric     // with private linkage (the default) so debug info can refer to it.
8720b57cec5SDimitry Andric     //
8730b57cec5SDimitry Andric     // However, if this global is promoted into several functions we must ensure
8740b57cec5SDimitry Andric     // we don't try and emit duplicate symbols!
8750b57cec5SDimitry Andric     auto *ACPC = cast<ARMConstantPoolConstant>(ACPV);
8760b57cec5SDimitry Andric     for (const auto *GV : ACPC->promotedGlobals()) {
8770b57cec5SDimitry Andric       if (!EmittedPromotedGlobalLabels.count(GV)) {
8780b57cec5SDimitry Andric         MCSymbol *GVSym = getSymbol(GV);
8790b57cec5SDimitry Andric         OutStreamer->EmitLabel(GVSym);
8800b57cec5SDimitry Andric         EmittedPromotedGlobalLabels.insert(GV);
8810b57cec5SDimitry Andric       }
8820b57cec5SDimitry Andric     }
8830b57cec5SDimitry Andric     return EmitGlobalConstant(DL, ACPC->getPromotedGlobalInit());
8840b57cec5SDimitry Andric   }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric   MCSymbol *MCSym;
8870b57cec5SDimitry Andric   if (ACPV->isLSDA()) {
8880b57cec5SDimitry Andric     MCSym = getCurExceptionSym();
8890b57cec5SDimitry Andric   } else if (ACPV->isBlockAddress()) {
8900b57cec5SDimitry Andric     const BlockAddress *BA =
8910b57cec5SDimitry Andric       cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
8920b57cec5SDimitry Andric     MCSym = GetBlockAddressSymbol(BA);
8930b57cec5SDimitry Andric   } else if (ACPV->isGlobalValue()) {
8940b57cec5SDimitry Andric     const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric     // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
8970b57cec5SDimitry Andric     // flag the global as MO_NONLAZY.
8980b57cec5SDimitry Andric     unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
8990b57cec5SDimitry Andric     MCSym = GetARMGVSymbol(GV, TF);
9000b57cec5SDimitry Andric   } else if (ACPV->isMachineBasicBlock()) {
9010b57cec5SDimitry Andric     const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
9020b57cec5SDimitry Andric     MCSym = MBB->getSymbol();
9030b57cec5SDimitry Andric   } else {
9040b57cec5SDimitry Andric     assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
9050b57cec5SDimitry Andric     auto Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
9060b57cec5SDimitry Andric     MCSym = GetExternalSymbolSymbol(Sym);
9070b57cec5SDimitry Andric   }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric   // Create an MCSymbol for the reference.
9100b57cec5SDimitry Andric   const MCExpr *Expr =
9110b57cec5SDimitry Andric     MCSymbolRefExpr::create(MCSym, getModifierVariantKind(ACPV->getModifier()),
9120b57cec5SDimitry Andric                             OutContext);
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric   if (ACPV->getPCAdjustment()) {
9150b57cec5SDimitry Andric     MCSymbol *PCLabel =
9160b57cec5SDimitry Andric         getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
9170b57cec5SDimitry Andric                     ACPV->getLabelId(), OutContext);
9180b57cec5SDimitry Andric     const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
9190b57cec5SDimitry Andric     PCRelExpr =
9200b57cec5SDimitry Andric       MCBinaryExpr::createAdd(PCRelExpr,
9210b57cec5SDimitry Andric                               MCConstantExpr::create(ACPV->getPCAdjustment(),
9220b57cec5SDimitry Andric                                                      OutContext),
9230b57cec5SDimitry Andric                               OutContext);
9240b57cec5SDimitry Andric     if (ACPV->mustAddCurrentAddress()) {
9250b57cec5SDimitry Andric       // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
9260b57cec5SDimitry Andric       // label, so just emit a local label end reference that instead.
9270b57cec5SDimitry Andric       MCSymbol *DotSym = OutContext.createTempSymbol();
9280b57cec5SDimitry Andric       OutStreamer->EmitLabel(DotSym);
9290b57cec5SDimitry Andric       const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
9300b57cec5SDimitry Andric       PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
9310b57cec5SDimitry Andric     }
9320b57cec5SDimitry Andric     Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
9330b57cec5SDimitry Andric   }
9340b57cec5SDimitry Andric   OutStreamer->EmitValue(Expr, Size);
9350b57cec5SDimitry Andric }
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric void ARMAsmPrinter::EmitJumpTableAddrs(const MachineInstr *MI) {
9380b57cec5SDimitry Andric   const MachineOperand &MO1 = MI->getOperand(1);
9390b57cec5SDimitry Andric   unsigned JTI = MO1.getIndex();
9400b57cec5SDimitry Andric 
9410b57cec5SDimitry Andric   // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
9420b57cec5SDimitry Andric   // ARM mode tables.
943*8bcb0991SDimitry Andric   EmitAlignment(Align(4));
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric   // Emit a label for the jump table.
9460b57cec5SDimitry Andric   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
9470b57cec5SDimitry Andric   OutStreamer->EmitLabel(JTISymbol);
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric   // Mark the jump table as data-in-code.
9500b57cec5SDimitry Andric   OutStreamer->EmitDataRegion(MCDR_DataRegionJT32);
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   // Emit each entry of the table.
9530b57cec5SDimitry Andric   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
9540b57cec5SDimitry Andric   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
9550b57cec5SDimitry Andric   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
9560b57cec5SDimitry Andric 
9570b57cec5SDimitry Andric   for (MachineBasicBlock *MBB : JTBBs) {
9580b57cec5SDimitry Andric     // Construct an MCExpr for the entry. We want a value of the form:
9590b57cec5SDimitry Andric     // (BasicBlockAddr - TableBeginAddr)
9600b57cec5SDimitry Andric     //
9610b57cec5SDimitry Andric     // For example, a table with entries jumping to basic blocks BB0 and BB1
9620b57cec5SDimitry Andric     // would look like:
9630b57cec5SDimitry Andric     // LJTI_0_0:
9640b57cec5SDimitry Andric     //    .word (LBB0 - LJTI_0_0)
9650b57cec5SDimitry Andric     //    .word (LBB1 - LJTI_0_0)
9660b57cec5SDimitry Andric     const MCExpr *Expr = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric     if (isPositionIndependent() || Subtarget->isROPI())
9690b57cec5SDimitry Andric       Expr = MCBinaryExpr::createSub(Expr, MCSymbolRefExpr::create(JTISymbol,
9700b57cec5SDimitry Andric                                                                    OutContext),
9710b57cec5SDimitry Andric                                      OutContext);
9720b57cec5SDimitry Andric     // If we're generating a table of Thumb addresses in static relocation
9730b57cec5SDimitry Andric     // model, we need to add one to keep interworking correctly.
9740b57cec5SDimitry Andric     else if (AFI->isThumbFunction())
9750b57cec5SDimitry Andric       Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(1,OutContext),
9760b57cec5SDimitry Andric                                      OutContext);
9770b57cec5SDimitry Andric     OutStreamer->EmitValue(Expr, 4);
9780b57cec5SDimitry Andric   }
9790b57cec5SDimitry Andric   // Mark the end of jump table data-in-code region.
9800b57cec5SDimitry Andric   OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
9810b57cec5SDimitry Andric }
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric void ARMAsmPrinter::EmitJumpTableInsts(const MachineInstr *MI) {
9840b57cec5SDimitry Andric   const MachineOperand &MO1 = MI->getOperand(1);
9850b57cec5SDimitry Andric   unsigned JTI = MO1.getIndex();
9860b57cec5SDimitry Andric 
9870b57cec5SDimitry Andric   // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
9880b57cec5SDimitry Andric   // ARM mode tables.
989*8bcb0991SDimitry Andric   EmitAlignment(Align(4));
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   // Emit a label for the jump table.
9920b57cec5SDimitry Andric   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
9930b57cec5SDimitry Andric   OutStreamer->EmitLabel(JTISymbol);
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric   // Emit each entry of the table.
9960b57cec5SDimitry Andric   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
9970b57cec5SDimitry Andric   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
9980b57cec5SDimitry Andric   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric   for (MachineBasicBlock *MBB : JTBBs) {
10010b57cec5SDimitry Andric     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
10020b57cec5SDimitry Andric                                                           OutContext);
10030b57cec5SDimitry Andric     // If this isn't a TBB or TBH, the entries are direct branch instructions.
10040b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2B)
10050b57cec5SDimitry Andric         .addExpr(MBBSymbolExpr)
10060b57cec5SDimitry Andric         .addImm(ARMCC::AL)
10070b57cec5SDimitry Andric         .addReg(0));
10080b57cec5SDimitry Andric   }
10090b57cec5SDimitry Andric }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric void ARMAsmPrinter::EmitJumpTableTBInst(const MachineInstr *MI,
10120b57cec5SDimitry Andric                                         unsigned OffsetWidth) {
10130b57cec5SDimitry Andric   assert((OffsetWidth == 1 || OffsetWidth == 2) && "invalid tbb/tbh width");
10140b57cec5SDimitry Andric   const MachineOperand &MO1 = MI->getOperand(1);
10150b57cec5SDimitry Andric   unsigned JTI = MO1.getIndex();
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric   if (Subtarget->isThumb1Only())
1018*8bcb0991SDimitry Andric     EmitAlignment(Align(4));
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
10210b57cec5SDimitry Andric   OutStreamer->EmitLabel(JTISymbol);
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   // Emit each entry of the table.
10240b57cec5SDimitry Andric   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
10250b57cec5SDimitry Andric   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
10260b57cec5SDimitry Andric   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric   // Mark the jump table as data-in-code.
10290b57cec5SDimitry Andric   OutStreamer->EmitDataRegion(OffsetWidth == 1 ? MCDR_DataRegionJT8
10300b57cec5SDimitry Andric                                                : MCDR_DataRegionJT16);
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric   for (auto MBB : JTBBs) {
10330b57cec5SDimitry Andric     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
10340b57cec5SDimitry Andric                                                           OutContext);
10350b57cec5SDimitry Andric     // Otherwise it's an offset from the dispatch instruction. Construct an
10360b57cec5SDimitry Andric     // MCExpr for the entry. We want a value of the form:
10370b57cec5SDimitry Andric     // (BasicBlockAddr - TBBInstAddr + 4) / 2
10380b57cec5SDimitry Andric     //
10390b57cec5SDimitry Andric     // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
10400b57cec5SDimitry Andric     // would look like:
10410b57cec5SDimitry Andric     // LJTI_0_0:
10420b57cec5SDimitry Andric     //    .byte (LBB0 - (LCPI0_0 + 4)) / 2
10430b57cec5SDimitry Andric     //    .byte (LBB1 - (LCPI0_0 + 4)) / 2
10440b57cec5SDimitry Andric     // where LCPI0_0 is a label defined just before the TBB instruction using
10450b57cec5SDimitry Andric     // this table.
10460b57cec5SDimitry Andric     MCSymbol *TBInstPC = GetCPISymbol(MI->getOperand(0).getImm());
10470b57cec5SDimitry Andric     const MCExpr *Expr = MCBinaryExpr::createAdd(
10480b57cec5SDimitry Andric         MCSymbolRefExpr::create(TBInstPC, OutContext),
10490b57cec5SDimitry Andric         MCConstantExpr::create(4, OutContext), OutContext);
10500b57cec5SDimitry Andric     Expr = MCBinaryExpr::createSub(MBBSymbolExpr, Expr, OutContext);
10510b57cec5SDimitry Andric     Expr = MCBinaryExpr::createDiv(Expr, MCConstantExpr::create(2, OutContext),
10520b57cec5SDimitry Andric                                    OutContext);
10530b57cec5SDimitry Andric     OutStreamer->EmitValue(Expr, OffsetWidth);
10540b57cec5SDimitry Andric   }
10550b57cec5SDimitry Andric   // Mark the end of jump table data-in-code region. 32-bit offsets use
10560b57cec5SDimitry Andric   // actual branch instructions here, so we don't mark those as a data-region
10570b57cec5SDimitry Andric   // at all.
10580b57cec5SDimitry Andric   OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   // Make sure the next instruction is 2-byte aligned.
1061*8bcb0991SDimitry Andric   EmitAlignment(Align(2));
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
10650b57cec5SDimitry Andric   assert(MI->getFlag(MachineInstr::FrameSetup) &&
10660b57cec5SDimitry Andric       "Only instruction which are involved into frame setup code are allowed");
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
10690b57cec5SDimitry Andric   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
10700b57cec5SDimitry Andric   const MachineFunction &MF = *MI->getParent()->getParent();
10710b57cec5SDimitry Andric   const TargetRegisterInfo *TargetRegInfo =
10720b57cec5SDimitry Andric     MF.getSubtarget().getRegisterInfo();
10730b57cec5SDimitry Andric   const MachineRegisterInfo &MachineRegInfo = MF.getRegInfo();
10740b57cec5SDimitry Andric 
1075*8bcb0991SDimitry Andric   Register FramePtr = TargetRegInfo->getFrameRegister(MF);
10760b57cec5SDimitry Andric   unsigned Opc = MI->getOpcode();
10770b57cec5SDimitry Andric   unsigned SrcReg, DstReg;
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
10800b57cec5SDimitry Andric     // Two special cases:
10810b57cec5SDimitry Andric     // 1) tPUSH does not have src/dst regs.
10820b57cec5SDimitry Andric     // 2) for Thumb1 code we sometimes materialize the constant via constpool
10830b57cec5SDimitry Andric     // load. Yes, this is pretty fragile, but for now I don't see better
10840b57cec5SDimitry Andric     // way... :(
10850b57cec5SDimitry Andric     SrcReg = DstReg = ARM::SP;
10860b57cec5SDimitry Andric   } else {
10870b57cec5SDimitry Andric     SrcReg = MI->getOperand(1).getReg();
10880b57cec5SDimitry Andric     DstReg = MI->getOperand(0).getReg();
10890b57cec5SDimitry Andric   }
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric   // Try to figure out the unwinding opcode out of src / dst regs.
10920b57cec5SDimitry Andric   if (MI->mayStore()) {
10930b57cec5SDimitry Andric     // Register saves.
10940b57cec5SDimitry Andric     assert(DstReg == ARM::SP &&
10950b57cec5SDimitry Andric            "Only stack pointer as a destination reg is supported");
10960b57cec5SDimitry Andric 
10970b57cec5SDimitry Andric     SmallVector<unsigned, 4> RegList;
10980b57cec5SDimitry Andric     // Skip src & dst reg, and pred ops.
10990b57cec5SDimitry Andric     unsigned StartOp = 2 + 2;
11000b57cec5SDimitry Andric     // Use all the operands.
11010b57cec5SDimitry Andric     unsigned NumOffset = 0;
11020b57cec5SDimitry Andric     // Amount of SP adjustment folded into a push.
11030b57cec5SDimitry Andric     unsigned Pad = 0;
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric     switch (Opc) {
11060b57cec5SDimitry Andric     default:
11070b57cec5SDimitry Andric       MI->print(errs());
11080b57cec5SDimitry Andric       llvm_unreachable("Unsupported opcode for unwinding information");
11090b57cec5SDimitry Andric     case ARM::tPUSH:
11100b57cec5SDimitry Andric       // Special case here: no src & dst reg, but two extra imp ops.
11110b57cec5SDimitry Andric       StartOp = 2; NumOffset = 2;
11120b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
11130b57cec5SDimitry Andric     case ARM::STMDB_UPD:
11140b57cec5SDimitry Andric     case ARM::t2STMDB_UPD:
11150b57cec5SDimitry Andric     case ARM::VSTMDDB_UPD:
11160b57cec5SDimitry Andric       assert(SrcReg == ARM::SP &&
11170b57cec5SDimitry Andric              "Only stack pointer as a source reg is supported");
11180b57cec5SDimitry Andric       for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
11190b57cec5SDimitry Andric            i != NumOps; ++i) {
11200b57cec5SDimitry Andric         const MachineOperand &MO = MI->getOperand(i);
11210b57cec5SDimitry Andric         // Actually, there should never be any impdef stuff here. Skip it
11220b57cec5SDimitry Andric         // temporary to workaround PR11902.
11230b57cec5SDimitry Andric         if (MO.isImplicit())
11240b57cec5SDimitry Andric           continue;
11250b57cec5SDimitry Andric         // Registers, pushed as a part of folding an SP update into the
11260b57cec5SDimitry Andric         // push instruction are marked as undef and should not be
11270b57cec5SDimitry Andric         // restored when unwinding, because the function can modify the
11280b57cec5SDimitry Andric         // corresponding stack slots.
11290b57cec5SDimitry Andric         if (MO.isUndef()) {
11300b57cec5SDimitry Andric           assert(RegList.empty() &&
11310b57cec5SDimitry Andric                  "Pad registers must come before restored ones");
11320b57cec5SDimitry Andric           unsigned Width =
11330b57cec5SDimitry Andric             TargetRegInfo->getRegSizeInBits(MO.getReg(), MachineRegInfo) / 8;
11340b57cec5SDimitry Andric           Pad += Width;
11350b57cec5SDimitry Andric           continue;
11360b57cec5SDimitry Andric         }
11370b57cec5SDimitry Andric         // Check for registers that are remapped (for a Thumb1 prologue that
11380b57cec5SDimitry Andric         // saves high registers).
1139*8bcb0991SDimitry Andric         Register Reg = MO.getReg();
11400b57cec5SDimitry Andric         if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(Reg))
11410b57cec5SDimitry Andric           Reg = RemappedReg;
11420b57cec5SDimitry Andric         RegList.push_back(Reg);
11430b57cec5SDimitry Andric       }
11440b57cec5SDimitry Andric       break;
11450b57cec5SDimitry Andric     case ARM::STR_PRE_IMM:
11460b57cec5SDimitry Andric     case ARM::STR_PRE_REG:
11470b57cec5SDimitry Andric     case ARM::t2STR_PRE:
11480b57cec5SDimitry Andric       assert(MI->getOperand(2).getReg() == ARM::SP &&
11490b57cec5SDimitry Andric              "Only stack pointer as a source reg is supported");
11500b57cec5SDimitry Andric       RegList.push_back(SrcReg);
11510b57cec5SDimitry Andric       break;
11520b57cec5SDimitry Andric     }
11530b57cec5SDimitry Andric     if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
11540b57cec5SDimitry Andric       ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
11550b57cec5SDimitry Andric       // Account for the SP adjustment, folded into the push.
11560b57cec5SDimitry Andric       if (Pad)
11570b57cec5SDimitry Andric         ATS.emitPad(Pad);
11580b57cec5SDimitry Andric     }
11590b57cec5SDimitry Andric   } else {
11600b57cec5SDimitry Andric     // Changes of stack / frame pointer.
11610b57cec5SDimitry Andric     if (SrcReg == ARM::SP) {
11620b57cec5SDimitry Andric       int64_t Offset = 0;
11630b57cec5SDimitry Andric       switch (Opc) {
11640b57cec5SDimitry Andric       default:
11650b57cec5SDimitry Andric         MI->print(errs());
11660b57cec5SDimitry Andric         llvm_unreachable("Unsupported opcode for unwinding information");
11670b57cec5SDimitry Andric       case ARM::MOVr:
11680b57cec5SDimitry Andric       case ARM::tMOVr:
11690b57cec5SDimitry Andric         Offset = 0;
11700b57cec5SDimitry Andric         break;
11710b57cec5SDimitry Andric       case ARM::ADDri:
11720b57cec5SDimitry Andric       case ARM::t2ADDri:
11730b57cec5SDimitry Andric         Offset = -MI->getOperand(2).getImm();
11740b57cec5SDimitry Andric         break;
11750b57cec5SDimitry Andric       case ARM::SUBri:
11760b57cec5SDimitry Andric       case ARM::t2SUBri:
11770b57cec5SDimitry Andric         Offset = MI->getOperand(2).getImm();
11780b57cec5SDimitry Andric         break;
11790b57cec5SDimitry Andric       case ARM::tSUBspi:
11800b57cec5SDimitry Andric         Offset = MI->getOperand(2).getImm()*4;
11810b57cec5SDimitry Andric         break;
11820b57cec5SDimitry Andric       case ARM::tADDspi:
11830b57cec5SDimitry Andric       case ARM::tADDrSPi:
11840b57cec5SDimitry Andric         Offset = -MI->getOperand(2).getImm()*4;
11850b57cec5SDimitry Andric         break;
11860b57cec5SDimitry Andric       case ARM::tLDRpci: {
11870b57cec5SDimitry Andric         // Grab the constpool index and check, whether it corresponds to
11880b57cec5SDimitry Andric         // original or cloned constpool entry.
11890b57cec5SDimitry Andric         unsigned CPI = MI->getOperand(1).getIndex();
11900b57cec5SDimitry Andric         const MachineConstantPool *MCP = MF.getConstantPool();
11910b57cec5SDimitry Andric         if (CPI >= MCP->getConstants().size())
11920b57cec5SDimitry Andric           CPI = AFI->getOriginalCPIdx(CPI);
11930b57cec5SDimitry Andric         assert(CPI != -1U && "Invalid constpool index");
11940b57cec5SDimitry Andric 
11950b57cec5SDimitry Andric         // Derive the actual offset.
11960b57cec5SDimitry Andric         const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
11970b57cec5SDimitry Andric         assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
11980b57cec5SDimitry Andric         // FIXME: Check for user, it should be "add" instruction!
11990b57cec5SDimitry Andric         Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
12000b57cec5SDimitry Andric         break;
12010b57cec5SDimitry Andric       }
12020b57cec5SDimitry Andric       }
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric       if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
12050b57cec5SDimitry Andric         if (DstReg == FramePtr && FramePtr != ARM::SP)
12060b57cec5SDimitry Andric           // Set-up of the frame pointer. Positive values correspond to "add"
12070b57cec5SDimitry Andric           // instruction.
12080b57cec5SDimitry Andric           ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
12090b57cec5SDimitry Andric         else if (DstReg == ARM::SP) {
12100b57cec5SDimitry Andric           // Change of SP by an offset. Positive values correspond to "sub"
12110b57cec5SDimitry Andric           // instruction.
12120b57cec5SDimitry Andric           ATS.emitPad(Offset);
12130b57cec5SDimitry Andric         } else {
12140b57cec5SDimitry Andric           // Move of SP to a register.  Positive values correspond to an "add"
12150b57cec5SDimitry Andric           // instruction.
12160b57cec5SDimitry Andric           ATS.emitMovSP(DstReg, -Offset);
12170b57cec5SDimitry Andric         }
12180b57cec5SDimitry Andric       }
12190b57cec5SDimitry Andric     } else if (DstReg == ARM::SP) {
12200b57cec5SDimitry Andric       MI->print(errs());
12210b57cec5SDimitry Andric       llvm_unreachable("Unsupported opcode for unwinding information");
12220b57cec5SDimitry Andric     } else if (Opc == ARM::tMOVr) {
12230b57cec5SDimitry Andric       // If a Thumb1 function spills r8-r11, we copy the values to low
12240b57cec5SDimitry Andric       // registers before pushing them. Record the copy so we can emit the
12250b57cec5SDimitry Andric       // correct ".save" later.
12260b57cec5SDimitry Andric       AFI->EHPrologueRemappedRegs[DstReg] = SrcReg;
12270b57cec5SDimitry Andric     } else {
12280b57cec5SDimitry Andric       MI->print(errs());
12290b57cec5SDimitry Andric       llvm_unreachable("Unsupported opcode for unwinding information");
12300b57cec5SDimitry Andric     }
12310b57cec5SDimitry Andric   }
12320b57cec5SDimitry Andric }
12330b57cec5SDimitry Andric 
12340b57cec5SDimitry Andric // Simple pseudo-instructions have their lowering (with expansion to real
12350b57cec5SDimitry Andric // instructions) auto-generated.
12360b57cec5SDimitry Andric #include "ARMGenMCPseudoLowering.inc"
12370b57cec5SDimitry Andric 
12380b57cec5SDimitry Andric void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
12390b57cec5SDimitry Andric   const DataLayout &DL = getDataLayout();
12400b57cec5SDimitry Andric   MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
12410b57cec5SDimitry Andric   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
12420b57cec5SDimitry Andric 
12430b57cec5SDimitry Andric   const MachineFunction &MF = *MI->getParent()->getParent();
12440b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
12450b57cec5SDimitry Andric   unsigned FramePtr = STI.useR7AsFramePointer() ? ARM::R7 : ARM::R11;
12460b57cec5SDimitry Andric 
12470b57cec5SDimitry Andric   // If we just ended a constant pool, mark it as such.
12480b57cec5SDimitry Andric   if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
12490b57cec5SDimitry Andric     OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
12500b57cec5SDimitry Andric     InConstantPool = false;
12510b57cec5SDimitry Andric   }
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric   // Emit unwinding stuff for frame-related instructions
12540b57cec5SDimitry Andric   if (Subtarget->isTargetEHABICompatible() &&
12550b57cec5SDimitry Andric        MI->getFlag(MachineInstr::FrameSetup))
12560b57cec5SDimitry Andric     EmitUnwindingInstruction(MI);
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric   // Do any auto-generated pseudo lowerings.
12590b57cec5SDimitry Andric   if (emitPseudoExpansionLowering(*OutStreamer, MI))
12600b57cec5SDimitry Andric     return;
12610b57cec5SDimitry Andric 
12620b57cec5SDimitry Andric   assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
12630b57cec5SDimitry Andric          "Pseudo flag setting opcode should be expanded early");
12640b57cec5SDimitry Andric 
12650b57cec5SDimitry Andric   // Check for manual lowerings.
12660b57cec5SDimitry Andric   unsigned Opc = MI->getOpcode();
12670b57cec5SDimitry Andric   switch (Opc) {
12680b57cec5SDimitry Andric   case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
12690b57cec5SDimitry Andric   case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
12700b57cec5SDimitry Andric   case ARM::LEApcrel:
12710b57cec5SDimitry Andric   case ARM::tLEApcrel:
12720b57cec5SDimitry Andric   case ARM::t2LEApcrel: {
12730b57cec5SDimitry Andric     // FIXME: Need to also handle globals and externals
12740b57cec5SDimitry Andric     MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
12750b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
12760b57cec5SDimitry Andric                                                ARM::t2LEApcrel ? ARM::t2ADR
12770b57cec5SDimitry Andric                   : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
12780b57cec5SDimitry Andric                      : ARM::ADR))
12790b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
12800b57cec5SDimitry Andric       .addExpr(MCSymbolRefExpr::create(CPISymbol, OutContext))
12810b57cec5SDimitry Andric       // Add predicate operands.
12820b57cec5SDimitry Andric       .addImm(MI->getOperand(2).getImm())
12830b57cec5SDimitry Andric       .addReg(MI->getOperand(3).getReg()));
12840b57cec5SDimitry Andric     return;
12850b57cec5SDimitry Andric   }
12860b57cec5SDimitry Andric   case ARM::LEApcrelJT:
12870b57cec5SDimitry Andric   case ARM::tLEApcrelJT:
12880b57cec5SDimitry Andric   case ARM::t2LEApcrelJT: {
12890b57cec5SDimitry Andric     MCSymbol *JTIPICSymbol =
12900b57cec5SDimitry Andric       GetARMJTIPICJumpTableLabel(MI->getOperand(1).getIndex());
12910b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
12920b57cec5SDimitry Andric                                                ARM::t2LEApcrelJT ? ARM::t2ADR
12930b57cec5SDimitry Andric                   : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
12940b57cec5SDimitry Andric                      : ARM::ADR))
12950b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
12960b57cec5SDimitry Andric       .addExpr(MCSymbolRefExpr::create(JTIPICSymbol, OutContext))
12970b57cec5SDimitry Andric       // Add predicate operands.
12980b57cec5SDimitry Andric       .addImm(MI->getOperand(2).getImm())
12990b57cec5SDimitry Andric       .addReg(MI->getOperand(3).getReg()));
13000b57cec5SDimitry Andric     return;
13010b57cec5SDimitry Andric   }
13020b57cec5SDimitry Andric   // Darwin call instructions are just normal call instructions with different
13030b57cec5SDimitry Andric   // clobber semantics (they clobber R9).
13040b57cec5SDimitry Andric   case ARM::BX_CALL: {
13050b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
13060b57cec5SDimitry Andric       .addReg(ARM::LR)
13070b57cec5SDimitry Andric       .addReg(ARM::PC)
13080b57cec5SDimitry Andric       // Add predicate operands.
13090b57cec5SDimitry Andric       .addImm(ARMCC::AL)
13100b57cec5SDimitry Andric       .addReg(0)
13110b57cec5SDimitry Andric       // Add 's' bit operand (always reg0 for this)
13120b57cec5SDimitry Andric       .addReg(0));
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric     assert(Subtarget->hasV4TOps());
13150b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX)
13160b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg()));
13170b57cec5SDimitry Andric     return;
13180b57cec5SDimitry Andric   }
13190b57cec5SDimitry Andric   case ARM::tBX_CALL: {
13200b57cec5SDimitry Andric     if (Subtarget->hasV5TOps())
13210b57cec5SDimitry Andric       llvm_unreachable("Expected BLX to be selected for v5t+");
13220b57cec5SDimitry Andric 
13230b57cec5SDimitry Andric     // On ARM v4t, when doing a call from thumb mode, we need to ensure
13240b57cec5SDimitry Andric     // that the saved lr has its LSB set correctly (the arch doesn't
13250b57cec5SDimitry Andric     // have blx).
13260b57cec5SDimitry Andric     // So here we generate a bl to a small jump pad that does bx rN.
13270b57cec5SDimitry Andric     // The jump pads are emitted after the function body.
13280b57cec5SDimitry Andric 
1329*8bcb0991SDimitry Andric     Register TReg = MI->getOperand(0).getReg();
13300b57cec5SDimitry Andric     MCSymbol *TRegSym = nullptr;
13310b57cec5SDimitry Andric     for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) {
13320b57cec5SDimitry Andric       if (TIP.first == TReg) {
13330b57cec5SDimitry Andric         TRegSym = TIP.second;
13340b57cec5SDimitry Andric         break;
13350b57cec5SDimitry Andric       }
13360b57cec5SDimitry Andric     }
13370b57cec5SDimitry Andric 
13380b57cec5SDimitry Andric     if (!TRegSym) {
13390b57cec5SDimitry Andric       TRegSym = OutContext.createTempSymbol();
13400b57cec5SDimitry Andric       ThumbIndirectPads.push_back(std::make_pair(TReg, TRegSym));
13410b57cec5SDimitry Andric     }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric     // Create a link-saving branch to the Reg Indirect Jump Pad.
13440b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBL)
13450b57cec5SDimitry Andric         // Predicate comes first here.
13460b57cec5SDimitry Andric         .addImm(ARMCC::AL).addReg(0)
13470b57cec5SDimitry Andric         .addExpr(MCSymbolRefExpr::create(TRegSym, OutContext)));
13480b57cec5SDimitry Andric     return;
13490b57cec5SDimitry Andric   }
13500b57cec5SDimitry Andric   case ARM::BMOVPCRX_CALL: {
13510b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
13520b57cec5SDimitry Andric       .addReg(ARM::LR)
13530b57cec5SDimitry Andric       .addReg(ARM::PC)
13540b57cec5SDimitry Andric       // Add predicate operands.
13550b57cec5SDimitry Andric       .addImm(ARMCC::AL)
13560b57cec5SDimitry Andric       .addReg(0)
13570b57cec5SDimitry Andric       // Add 's' bit operand (always reg0 for this)
13580b57cec5SDimitry Andric       .addReg(0));
13590b57cec5SDimitry Andric 
13600b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
13610b57cec5SDimitry Andric       .addReg(ARM::PC)
13620b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
13630b57cec5SDimitry Andric       // Add predicate operands.
13640b57cec5SDimitry Andric       .addImm(ARMCC::AL)
13650b57cec5SDimitry Andric       .addReg(0)
13660b57cec5SDimitry Andric       // Add 's' bit operand (always reg0 for this)
13670b57cec5SDimitry Andric       .addReg(0));
13680b57cec5SDimitry Andric     return;
13690b57cec5SDimitry Andric   }
13700b57cec5SDimitry Andric   case ARM::BMOVPCB_CALL: {
13710b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
13720b57cec5SDimitry Andric       .addReg(ARM::LR)
13730b57cec5SDimitry Andric       .addReg(ARM::PC)
13740b57cec5SDimitry Andric       // Add predicate operands.
13750b57cec5SDimitry Andric       .addImm(ARMCC::AL)
13760b57cec5SDimitry Andric       .addReg(0)
13770b57cec5SDimitry Andric       // Add 's' bit operand (always reg0 for this)
13780b57cec5SDimitry Andric       .addReg(0));
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric     const MachineOperand &Op = MI->getOperand(0);
13810b57cec5SDimitry Andric     const GlobalValue *GV = Op.getGlobal();
13820b57cec5SDimitry Andric     const unsigned TF = Op.getTargetFlags();
13830b57cec5SDimitry Andric     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
13840b57cec5SDimitry Andric     const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
13850b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::Bcc)
13860b57cec5SDimitry Andric       .addExpr(GVSymExpr)
13870b57cec5SDimitry Andric       // Add predicate operands.
13880b57cec5SDimitry Andric       .addImm(ARMCC::AL)
13890b57cec5SDimitry Andric       .addReg(0));
13900b57cec5SDimitry Andric     return;
13910b57cec5SDimitry Andric   }
13920b57cec5SDimitry Andric   case ARM::MOVi16_ga_pcrel:
13930b57cec5SDimitry Andric   case ARM::t2MOVi16_ga_pcrel: {
13940b57cec5SDimitry Andric     MCInst TmpInst;
13950b57cec5SDimitry Andric     TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
13960b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric     unsigned TF = MI->getOperand(1).getTargetFlags();
13990b57cec5SDimitry Andric     const GlobalValue *GV = MI->getOperand(1).getGlobal();
14000b57cec5SDimitry Andric     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
14010b57cec5SDimitry Andric     const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric     MCSymbol *LabelSym =
14040b57cec5SDimitry Andric         getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
14050b57cec5SDimitry Andric                     MI->getOperand(2).getImm(), OutContext);
14060b57cec5SDimitry Andric     const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
14070b57cec5SDimitry Andric     unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
14080b57cec5SDimitry Andric     const MCExpr *PCRelExpr =
14090b57cec5SDimitry Andric       ARMMCExpr::createLower16(MCBinaryExpr::createSub(GVSymExpr,
14100b57cec5SDimitry Andric                                       MCBinaryExpr::createAdd(LabelSymExpr,
14110b57cec5SDimitry Andric                                       MCConstantExpr::create(PCAdj, OutContext),
14120b57cec5SDimitry Andric                                       OutContext), OutContext), OutContext);
14130b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric     // Add predicate operands.
14160b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
14170b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
14180b57cec5SDimitry Andric     // Add 's' bit operand (always reg0 for this)
14190b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
14200b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, TmpInst);
14210b57cec5SDimitry Andric     return;
14220b57cec5SDimitry Andric   }
14230b57cec5SDimitry Andric   case ARM::MOVTi16_ga_pcrel:
14240b57cec5SDimitry Andric   case ARM::t2MOVTi16_ga_pcrel: {
14250b57cec5SDimitry Andric     MCInst TmpInst;
14260b57cec5SDimitry Andric     TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
14270b57cec5SDimitry Andric                       ? ARM::MOVTi16 : ARM::t2MOVTi16);
14280b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
14290b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
14300b57cec5SDimitry Andric 
14310b57cec5SDimitry Andric     unsigned TF = MI->getOperand(2).getTargetFlags();
14320b57cec5SDimitry Andric     const GlobalValue *GV = MI->getOperand(2).getGlobal();
14330b57cec5SDimitry Andric     MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
14340b57cec5SDimitry Andric     const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
14350b57cec5SDimitry Andric 
14360b57cec5SDimitry Andric     MCSymbol *LabelSym =
14370b57cec5SDimitry Andric         getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
14380b57cec5SDimitry Andric                     MI->getOperand(3).getImm(), OutContext);
14390b57cec5SDimitry Andric     const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
14400b57cec5SDimitry Andric     unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
14410b57cec5SDimitry Andric     const MCExpr *PCRelExpr =
14420b57cec5SDimitry Andric         ARMMCExpr::createUpper16(MCBinaryExpr::createSub(GVSymExpr,
14430b57cec5SDimitry Andric                                    MCBinaryExpr::createAdd(LabelSymExpr,
14440b57cec5SDimitry Andric                                       MCConstantExpr::create(PCAdj, OutContext),
14450b57cec5SDimitry Andric                                           OutContext), OutContext), OutContext);
14460b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
14470b57cec5SDimitry Andric     // Add predicate operands.
14480b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
14490b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
14500b57cec5SDimitry Andric     // Add 's' bit operand (always reg0 for this)
14510b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
14520b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, TmpInst);
14530b57cec5SDimitry Andric     return;
14540b57cec5SDimitry Andric   }
14550b57cec5SDimitry Andric   case ARM::t2BFi:
14560b57cec5SDimitry Andric   case ARM::t2BFic:
14570b57cec5SDimitry Andric   case ARM::t2BFLi:
14580b57cec5SDimitry Andric   case ARM::t2BFr:
14590b57cec5SDimitry Andric   case ARM::t2BFLr: {
14600b57cec5SDimitry Andric     // This is a Branch Future instruction.
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric     const MCExpr *BranchLabel = MCSymbolRefExpr::create(
14630b57cec5SDimitry Andric         getBFLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
14640b57cec5SDimitry Andric                    MI->getOperand(0).getIndex(), OutContext),
14650b57cec5SDimitry Andric         OutContext);
14660b57cec5SDimitry Andric 
14670b57cec5SDimitry Andric     auto MCInst = MCInstBuilder(Opc).addExpr(BranchLabel);
14680b57cec5SDimitry Andric     if (MI->getOperand(1).isReg()) {
14690b57cec5SDimitry Andric       // For BFr/BFLr
14700b57cec5SDimitry Andric       MCInst.addReg(MI->getOperand(1).getReg());
14710b57cec5SDimitry Andric     } else {
14720b57cec5SDimitry Andric       // For BFi/BFLi/BFic
14730b57cec5SDimitry Andric       const MCExpr *BranchTarget;
14740b57cec5SDimitry Andric       if (MI->getOperand(1).isMBB())
14750b57cec5SDimitry Andric         BranchTarget = MCSymbolRefExpr::create(
14760b57cec5SDimitry Andric             MI->getOperand(1).getMBB()->getSymbol(), OutContext);
14770b57cec5SDimitry Andric       else if (MI->getOperand(1).isGlobal()) {
14780b57cec5SDimitry Andric         const GlobalValue *GV = MI->getOperand(1).getGlobal();
14790b57cec5SDimitry Andric         BranchTarget = MCSymbolRefExpr::create(
14800b57cec5SDimitry Andric             GetARMGVSymbol(GV, MI->getOperand(1).getTargetFlags()), OutContext);
14810b57cec5SDimitry Andric       } else if (MI->getOperand(1).isSymbol()) {
14820b57cec5SDimitry Andric         BranchTarget = MCSymbolRefExpr::create(
14830b57cec5SDimitry Andric             GetExternalSymbolSymbol(MI->getOperand(1).getSymbolName()),
14840b57cec5SDimitry Andric             OutContext);
14850b57cec5SDimitry Andric       } else
14860b57cec5SDimitry Andric         llvm_unreachable("Unhandled operand kind in Branch Future instruction");
14870b57cec5SDimitry Andric 
14880b57cec5SDimitry Andric       MCInst.addExpr(BranchTarget);
14890b57cec5SDimitry Andric     }
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric       if (Opc == ARM::t2BFic) {
14920b57cec5SDimitry Andric         const MCExpr *ElseLabel = MCSymbolRefExpr::create(
14930b57cec5SDimitry Andric             getBFLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
14940b57cec5SDimitry Andric                        MI->getOperand(2).getIndex(), OutContext),
14950b57cec5SDimitry Andric             OutContext);
14960b57cec5SDimitry Andric         MCInst.addExpr(ElseLabel);
14970b57cec5SDimitry Andric         MCInst.addImm(MI->getOperand(3).getImm());
14980b57cec5SDimitry Andric       } else {
14990b57cec5SDimitry Andric         MCInst.addImm(MI->getOperand(2).getImm())
15000b57cec5SDimitry Andric             .addReg(MI->getOperand(3).getReg());
15010b57cec5SDimitry Andric       }
15020b57cec5SDimitry Andric 
15030b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInst);
15040b57cec5SDimitry Andric     return;
15050b57cec5SDimitry Andric   }
15060b57cec5SDimitry Andric   case ARM::t2BF_LabelPseudo: {
15070b57cec5SDimitry Andric     // This is a pseudo op for a label used by a branch future instruction
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric     // Emit the label.
15100b57cec5SDimitry Andric     OutStreamer->EmitLabel(getBFLabel(DL.getPrivateGlobalPrefix(),
15110b57cec5SDimitry Andric                                        getFunctionNumber(),
15120b57cec5SDimitry Andric                                        MI->getOperand(0).getIndex(), OutContext));
15130b57cec5SDimitry Andric     return;
15140b57cec5SDimitry Andric   }
15150b57cec5SDimitry Andric   case ARM::tPICADD: {
15160b57cec5SDimitry Andric     // This is a pseudo op for a label + instruction sequence, which looks like:
15170b57cec5SDimitry Andric     // LPC0:
15180b57cec5SDimitry Andric     //     add r0, pc
15190b57cec5SDimitry Andric     // This adds the address of LPC0 to r0.
15200b57cec5SDimitry Andric 
15210b57cec5SDimitry Andric     // Emit the label.
15220b57cec5SDimitry Andric     OutStreamer->EmitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
15230b57cec5SDimitry Andric                                        getFunctionNumber(),
15240b57cec5SDimitry Andric                                        MI->getOperand(2).getImm(), OutContext));
15250b57cec5SDimitry Andric 
15260b57cec5SDimitry Andric     // Form and emit the add.
15270b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
15280b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
15290b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
15300b57cec5SDimitry Andric       .addReg(ARM::PC)
15310b57cec5SDimitry Andric       // Add predicate operands.
15320b57cec5SDimitry Andric       .addImm(ARMCC::AL)
15330b57cec5SDimitry Andric       .addReg(0));
15340b57cec5SDimitry Andric     return;
15350b57cec5SDimitry Andric   }
15360b57cec5SDimitry Andric   case ARM::PICADD: {
15370b57cec5SDimitry Andric     // This is a pseudo op for a label + instruction sequence, which looks like:
15380b57cec5SDimitry Andric     // LPC0:
15390b57cec5SDimitry Andric     //     add r0, pc, r0
15400b57cec5SDimitry Andric     // This adds the address of LPC0 to r0.
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric     // Emit the label.
15430b57cec5SDimitry Andric     OutStreamer->EmitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
15440b57cec5SDimitry Andric                                        getFunctionNumber(),
15450b57cec5SDimitry Andric                                        MI->getOperand(2).getImm(), OutContext));
15460b57cec5SDimitry Andric 
15470b57cec5SDimitry Andric     // Form and emit the add.
15480b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr)
15490b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
15500b57cec5SDimitry Andric       .addReg(ARM::PC)
15510b57cec5SDimitry Andric       .addReg(MI->getOperand(1).getReg())
15520b57cec5SDimitry Andric       // Add predicate operands.
15530b57cec5SDimitry Andric       .addImm(MI->getOperand(3).getImm())
15540b57cec5SDimitry Andric       .addReg(MI->getOperand(4).getReg())
15550b57cec5SDimitry Andric       // Add 's' bit operand (always reg0 for this)
15560b57cec5SDimitry Andric       .addReg(0));
15570b57cec5SDimitry Andric     return;
15580b57cec5SDimitry Andric   }
15590b57cec5SDimitry Andric   case ARM::PICSTR:
15600b57cec5SDimitry Andric   case ARM::PICSTRB:
15610b57cec5SDimitry Andric   case ARM::PICSTRH:
15620b57cec5SDimitry Andric   case ARM::PICLDR:
15630b57cec5SDimitry Andric   case ARM::PICLDRB:
15640b57cec5SDimitry Andric   case ARM::PICLDRH:
15650b57cec5SDimitry Andric   case ARM::PICLDRSB:
15660b57cec5SDimitry Andric   case ARM::PICLDRSH: {
15670b57cec5SDimitry Andric     // This is a pseudo op for a label + instruction sequence, which looks like:
15680b57cec5SDimitry Andric     // LPC0:
15690b57cec5SDimitry Andric     //     OP r0, [pc, r0]
15700b57cec5SDimitry Andric     // The LCP0 label is referenced by a constant pool entry in order to get
15710b57cec5SDimitry Andric     // a PC-relative address at the ldr instruction.
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric     // Emit the label.
15740b57cec5SDimitry Andric     OutStreamer->EmitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
15750b57cec5SDimitry Andric                                        getFunctionNumber(),
15760b57cec5SDimitry Andric                                        MI->getOperand(2).getImm(), OutContext));
15770b57cec5SDimitry Andric 
15780b57cec5SDimitry Andric     // Form and emit the load
15790b57cec5SDimitry Andric     unsigned Opcode;
15800b57cec5SDimitry Andric     switch (MI->getOpcode()) {
15810b57cec5SDimitry Andric     default:
15820b57cec5SDimitry Andric       llvm_unreachable("Unexpected opcode!");
15830b57cec5SDimitry Andric     case ARM::PICSTR:   Opcode = ARM::STRrs; break;
15840b57cec5SDimitry Andric     case ARM::PICSTRB:  Opcode = ARM::STRBrs; break;
15850b57cec5SDimitry Andric     case ARM::PICSTRH:  Opcode = ARM::STRH; break;
15860b57cec5SDimitry Andric     case ARM::PICLDR:   Opcode = ARM::LDRrs; break;
15870b57cec5SDimitry Andric     case ARM::PICLDRB:  Opcode = ARM::LDRBrs; break;
15880b57cec5SDimitry Andric     case ARM::PICLDRH:  Opcode = ARM::LDRH; break;
15890b57cec5SDimitry Andric     case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
15900b57cec5SDimitry Andric     case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
15910b57cec5SDimitry Andric     }
15920b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(Opcode)
15930b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
15940b57cec5SDimitry Andric       .addReg(ARM::PC)
15950b57cec5SDimitry Andric       .addReg(MI->getOperand(1).getReg())
15960b57cec5SDimitry Andric       .addImm(0)
15970b57cec5SDimitry Andric       // Add predicate operands.
15980b57cec5SDimitry Andric       .addImm(MI->getOperand(3).getImm())
15990b57cec5SDimitry Andric       .addReg(MI->getOperand(4).getReg()));
16000b57cec5SDimitry Andric 
16010b57cec5SDimitry Andric     return;
16020b57cec5SDimitry Andric   }
16030b57cec5SDimitry Andric   case ARM::CONSTPOOL_ENTRY: {
16040b57cec5SDimitry Andric     if (Subtarget->genExecuteOnly())
16050b57cec5SDimitry Andric       llvm_unreachable("execute-only should not generate constant pools");
16060b57cec5SDimitry Andric 
16070b57cec5SDimitry Andric     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
16080b57cec5SDimitry Andric     /// in the function.  The first operand is the ID# for this instruction, the
16090b57cec5SDimitry Andric     /// second is the index into the MachineConstantPool that this is, the third
16100b57cec5SDimitry Andric     /// is the size in bytes of this constant pool entry.
16110b57cec5SDimitry Andric     /// The required alignment is specified on the basic block holding this MI.
16120b57cec5SDimitry Andric     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
16130b57cec5SDimitry Andric     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric     // If this is the first entry of the pool, mark it.
16160b57cec5SDimitry Andric     if (!InConstantPool) {
16170b57cec5SDimitry Andric       OutStreamer->EmitDataRegion(MCDR_DataRegion);
16180b57cec5SDimitry Andric       InConstantPool = true;
16190b57cec5SDimitry Andric     }
16200b57cec5SDimitry Andric 
16210b57cec5SDimitry Andric     OutStreamer->EmitLabel(GetCPISymbol(LabelId));
16220b57cec5SDimitry Andric 
16230b57cec5SDimitry Andric     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
16240b57cec5SDimitry Andric     if (MCPE.isMachineConstantPoolEntry())
16250b57cec5SDimitry Andric       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
16260b57cec5SDimitry Andric     else
16270b57cec5SDimitry Andric       EmitGlobalConstant(DL, MCPE.Val.ConstVal);
16280b57cec5SDimitry Andric     return;
16290b57cec5SDimitry Andric   }
16300b57cec5SDimitry Andric   case ARM::JUMPTABLE_ADDRS:
16310b57cec5SDimitry Andric     EmitJumpTableAddrs(MI);
16320b57cec5SDimitry Andric     return;
16330b57cec5SDimitry Andric   case ARM::JUMPTABLE_INSTS:
16340b57cec5SDimitry Andric     EmitJumpTableInsts(MI);
16350b57cec5SDimitry Andric     return;
16360b57cec5SDimitry Andric   case ARM::JUMPTABLE_TBB:
16370b57cec5SDimitry Andric   case ARM::JUMPTABLE_TBH:
16380b57cec5SDimitry Andric     EmitJumpTableTBInst(MI, MI->getOpcode() == ARM::JUMPTABLE_TBB ? 1 : 2);
16390b57cec5SDimitry Andric     return;
16400b57cec5SDimitry Andric   case ARM::t2BR_JT: {
16410b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
16420b57cec5SDimitry Andric       .addReg(ARM::PC)
16430b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
16440b57cec5SDimitry Andric       // Add predicate operands.
16450b57cec5SDimitry Andric       .addImm(ARMCC::AL)
16460b57cec5SDimitry Andric       .addReg(0));
16470b57cec5SDimitry Andric     return;
16480b57cec5SDimitry Andric   }
16490b57cec5SDimitry Andric   case ARM::t2TBB_JT:
16500b57cec5SDimitry Andric   case ARM::t2TBH_JT: {
16510b57cec5SDimitry Andric     unsigned Opc = MI->getOpcode() == ARM::t2TBB_JT ? ARM::t2TBB : ARM::t2TBH;
16520b57cec5SDimitry Andric     // Lower and emit the PC label, then the instruction itself.
16530b57cec5SDimitry Andric     OutStreamer->EmitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
16540b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
16550b57cec5SDimitry Andric                                      .addReg(MI->getOperand(0).getReg())
16560b57cec5SDimitry Andric                                      .addReg(MI->getOperand(1).getReg())
16570b57cec5SDimitry Andric                                      // Add predicate operands.
16580b57cec5SDimitry Andric                                      .addImm(ARMCC::AL)
16590b57cec5SDimitry Andric                                      .addReg(0));
16600b57cec5SDimitry Andric     return;
16610b57cec5SDimitry Andric   }
16620b57cec5SDimitry Andric   case ARM::tTBB_JT:
16630b57cec5SDimitry Andric   case ARM::tTBH_JT: {
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric     bool Is8Bit = MI->getOpcode() == ARM::tTBB_JT;
1666*8bcb0991SDimitry Andric     Register Base = MI->getOperand(0).getReg();
1667*8bcb0991SDimitry Andric     Register Idx = MI->getOperand(1).getReg();
16680b57cec5SDimitry Andric     assert(MI->getOperand(1).isKill() && "We need the index register as scratch!");
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric     // Multiply up idx if necessary.
16710b57cec5SDimitry Andric     if (!Is8Bit)
16720b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
16730b57cec5SDimitry Andric                                        .addReg(Idx)
16740b57cec5SDimitry Andric                                        .addReg(ARM::CPSR)
16750b57cec5SDimitry Andric                                        .addReg(Idx)
16760b57cec5SDimitry Andric                                        .addImm(1)
16770b57cec5SDimitry Andric                                        // Add predicate operands.
16780b57cec5SDimitry Andric                                        .addImm(ARMCC::AL)
16790b57cec5SDimitry Andric                                        .addReg(0));
16800b57cec5SDimitry Andric 
16810b57cec5SDimitry Andric     if (Base == ARM::PC) {
16820b57cec5SDimitry Andric       // TBB [base, idx] =
16830b57cec5SDimitry Andric       //    ADDS idx, idx, base
16840b57cec5SDimitry Andric       //    LDRB idx, [idx, #4] ; or LDRH if TBH
16850b57cec5SDimitry Andric       //    LSLS idx, #1
16860b57cec5SDimitry Andric       //    ADDS pc, pc, idx
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric       // When using PC as the base, it's important that there is no padding
16890b57cec5SDimitry Andric       // between the last ADDS and the start of the jump table. The jump table
16900b57cec5SDimitry Andric       // is 4-byte aligned, so we ensure we're 4 byte aligned here too.
16910b57cec5SDimitry Andric       //
16920b57cec5SDimitry Andric       // FIXME: Ideally we could vary the LDRB index based on the padding
16930b57cec5SDimitry Andric       // between the sequence and jump table, however that relies on MCExprs
16940b57cec5SDimitry Andric       // for load indexes which are currently not supported.
16950b57cec5SDimitry Andric       OutStreamer->EmitCodeAlignment(4);
16960b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
16970b57cec5SDimitry Andric                                        .addReg(Idx)
16980b57cec5SDimitry Andric                                        .addReg(Idx)
16990b57cec5SDimitry Andric                                        .addReg(Base)
17000b57cec5SDimitry Andric                                        // Add predicate operands.
17010b57cec5SDimitry Andric                                        .addImm(ARMCC::AL)
17020b57cec5SDimitry Andric                                        .addReg(0));
17030b57cec5SDimitry Andric 
17040b57cec5SDimitry Andric       unsigned Opc = Is8Bit ? ARM::tLDRBi : ARM::tLDRHi;
17050b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
17060b57cec5SDimitry Andric                                        .addReg(Idx)
17070b57cec5SDimitry Andric                                        .addReg(Idx)
17080b57cec5SDimitry Andric                                        .addImm(Is8Bit ? 4 : 2)
17090b57cec5SDimitry Andric                                        // Add predicate operands.
17100b57cec5SDimitry Andric                                        .addImm(ARMCC::AL)
17110b57cec5SDimitry Andric                                        .addReg(0));
17120b57cec5SDimitry Andric     } else {
17130b57cec5SDimitry Andric       // TBB [base, idx] =
17140b57cec5SDimitry Andric       //    LDRB idx, [base, idx] ; or LDRH if TBH
17150b57cec5SDimitry Andric       //    LSLS idx, #1
17160b57cec5SDimitry Andric       //    ADDS pc, pc, idx
17170b57cec5SDimitry Andric 
17180b57cec5SDimitry Andric       unsigned Opc = Is8Bit ? ARM::tLDRBr : ARM::tLDRHr;
17190b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
17200b57cec5SDimitry Andric                                        .addReg(Idx)
17210b57cec5SDimitry Andric                                        .addReg(Base)
17220b57cec5SDimitry Andric                                        .addReg(Idx)
17230b57cec5SDimitry Andric                                        // Add predicate operands.
17240b57cec5SDimitry Andric                                        .addImm(ARMCC::AL)
17250b57cec5SDimitry Andric                                        .addReg(0));
17260b57cec5SDimitry Andric     }
17270b57cec5SDimitry Andric 
17280b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
17290b57cec5SDimitry Andric                                      .addReg(Idx)
17300b57cec5SDimitry Andric                                      .addReg(ARM::CPSR)
17310b57cec5SDimitry Andric                                      .addReg(Idx)
17320b57cec5SDimitry Andric                                      .addImm(1)
17330b57cec5SDimitry Andric                                      // Add predicate operands.
17340b57cec5SDimitry Andric                                      .addImm(ARMCC::AL)
17350b57cec5SDimitry Andric                                      .addReg(0));
17360b57cec5SDimitry Andric 
17370b57cec5SDimitry Andric     OutStreamer->EmitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
17380b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
17390b57cec5SDimitry Andric                                      .addReg(ARM::PC)
17400b57cec5SDimitry Andric                                      .addReg(ARM::PC)
17410b57cec5SDimitry Andric                                      .addReg(Idx)
17420b57cec5SDimitry Andric                                      // Add predicate operands.
17430b57cec5SDimitry Andric                                      .addImm(ARMCC::AL)
17440b57cec5SDimitry Andric                                      .addReg(0));
17450b57cec5SDimitry Andric     return;
17460b57cec5SDimitry Andric   }
17470b57cec5SDimitry Andric   case ARM::tBR_JTr:
17480b57cec5SDimitry Andric   case ARM::BR_JTr: {
17490b57cec5SDimitry Andric     // mov pc, target
17500b57cec5SDimitry Andric     MCInst TmpInst;
17510b57cec5SDimitry Andric     unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
17520b57cec5SDimitry Andric       ARM::MOVr : ARM::tMOVr;
17530b57cec5SDimitry Andric     TmpInst.setOpcode(Opc);
17540b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(ARM::PC));
17550b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
17560b57cec5SDimitry Andric     // Add predicate operands.
17570b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
17580b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
17590b57cec5SDimitry Andric     // Add 's' bit operand (always reg0 for this)
17600b57cec5SDimitry Andric     if (Opc == ARM::MOVr)
17610b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createReg(0));
17620b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, TmpInst);
17630b57cec5SDimitry Andric     return;
17640b57cec5SDimitry Andric   }
17650b57cec5SDimitry Andric   case ARM::BR_JTm_i12: {
17660b57cec5SDimitry Andric     // ldr pc, target
17670b57cec5SDimitry Andric     MCInst TmpInst;
17680b57cec5SDimitry Andric     TmpInst.setOpcode(ARM::LDRi12);
17690b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(ARM::PC));
17700b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
17710b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
17720b57cec5SDimitry Andric     // Add predicate operands.
17730b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
17740b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
17750b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, TmpInst);
17760b57cec5SDimitry Andric     return;
17770b57cec5SDimitry Andric   }
17780b57cec5SDimitry Andric   case ARM::BR_JTm_rs: {
17790b57cec5SDimitry Andric     // ldr pc, target
17800b57cec5SDimitry Andric     MCInst TmpInst;
17810b57cec5SDimitry Andric     TmpInst.setOpcode(ARM::LDRrs);
17820b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(ARM::PC));
17830b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
17840b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
17850b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
17860b57cec5SDimitry Andric     // Add predicate operands.
17870b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
17880b57cec5SDimitry Andric     TmpInst.addOperand(MCOperand::createReg(0));
17890b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, TmpInst);
17900b57cec5SDimitry Andric     return;
17910b57cec5SDimitry Andric   }
17920b57cec5SDimitry Andric   case ARM::BR_JTadd: {
17930b57cec5SDimitry Andric     // add pc, target, idx
17940b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr)
17950b57cec5SDimitry Andric       .addReg(ARM::PC)
17960b57cec5SDimitry Andric       .addReg(MI->getOperand(0).getReg())
17970b57cec5SDimitry Andric       .addReg(MI->getOperand(1).getReg())
17980b57cec5SDimitry Andric       // Add predicate operands.
17990b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18000b57cec5SDimitry Andric       .addReg(0)
18010b57cec5SDimitry Andric       // Add 's' bit operand (always reg0 for this)
18020b57cec5SDimitry Andric       .addReg(0));
18030b57cec5SDimitry Andric     return;
18040b57cec5SDimitry Andric   }
18050b57cec5SDimitry Andric   case ARM::SPACE:
18060b57cec5SDimitry Andric     OutStreamer->EmitZeros(MI->getOperand(1).getImm());
18070b57cec5SDimitry Andric     return;
18080b57cec5SDimitry Andric   case ARM::TRAP: {
18090b57cec5SDimitry Andric     // Non-Darwin binutils don't yet support the "trap" mnemonic.
18100b57cec5SDimitry Andric     // FIXME: Remove this special case when they do.
18110b57cec5SDimitry Andric     if (!Subtarget->isTargetMachO()) {
18120b57cec5SDimitry Andric       uint32_t Val = 0xe7ffdefeUL;
18130b57cec5SDimitry Andric       OutStreamer->AddComment("trap");
18140b57cec5SDimitry Andric       ATS.emitInst(Val);
18150b57cec5SDimitry Andric       return;
18160b57cec5SDimitry Andric     }
18170b57cec5SDimitry Andric     break;
18180b57cec5SDimitry Andric   }
18190b57cec5SDimitry Andric   case ARM::TRAPNaCl: {
18200b57cec5SDimitry Andric     uint32_t Val = 0xe7fedef0UL;
18210b57cec5SDimitry Andric     OutStreamer->AddComment("trap");
18220b57cec5SDimitry Andric     ATS.emitInst(Val);
18230b57cec5SDimitry Andric     return;
18240b57cec5SDimitry Andric   }
18250b57cec5SDimitry Andric   case ARM::tTRAP: {
18260b57cec5SDimitry Andric     // Non-Darwin binutils don't yet support the "trap" mnemonic.
18270b57cec5SDimitry Andric     // FIXME: Remove this special case when they do.
18280b57cec5SDimitry Andric     if (!Subtarget->isTargetMachO()) {
18290b57cec5SDimitry Andric       uint16_t Val = 0xdefe;
18300b57cec5SDimitry Andric       OutStreamer->AddComment("trap");
18310b57cec5SDimitry Andric       ATS.emitInst(Val, 'n');
18320b57cec5SDimitry Andric       return;
18330b57cec5SDimitry Andric     }
18340b57cec5SDimitry Andric     break;
18350b57cec5SDimitry Andric   }
18360b57cec5SDimitry Andric   case ARM::t2Int_eh_sjlj_setjmp:
18370b57cec5SDimitry Andric   case ARM::t2Int_eh_sjlj_setjmp_nofp:
18380b57cec5SDimitry Andric   case ARM::tInt_eh_sjlj_setjmp: {
18390b57cec5SDimitry Andric     // Two incoming args: GPR:$src, GPR:$val
18400b57cec5SDimitry Andric     // mov $val, pc
18410b57cec5SDimitry Andric     // adds $val, #7
18420b57cec5SDimitry Andric     // str $val, [$src, #4]
18430b57cec5SDimitry Andric     // movs r0, #0
18440b57cec5SDimitry Andric     // b LSJLJEH
18450b57cec5SDimitry Andric     // movs r0, #1
18460b57cec5SDimitry Andric     // LSJLJEH:
1847*8bcb0991SDimitry Andric     Register SrcReg = MI->getOperand(0).getReg();
1848*8bcb0991SDimitry Andric     Register ValReg = MI->getOperand(1).getReg();
18490b57cec5SDimitry Andric     MCSymbol *Label = OutContext.createTempSymbol("SJLJEH", false, true);
18500b57cec5SDimitry Andric     OutStreamer->AddComment("eh_setjmp begin");
18510b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
18520b57cec5SDimitry Andric       .addReg(ValReg)
18530b57cec5SDimitry Andric       .addReg(ARM::PC)
18540b57cec5SDimitry Andric       // Predicate.
18550b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18560b57cec5SDimitry Andric       .addReg(0));
18570b57cec5SDimitry Andric 
18580b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi3)
18590b57cec5SDimitry Andric       .addReg(ValReg)
18600b57cec5SDimitry Andric       // 's' bit operand
18610b57cec5SDimitry Andric       .addReg(ARM::CPSR)
18620b57cec5SDimitry Andric       .addReg(ValReg)
18630b57cec5SDimitry Andric       .addImm(7)
18640b57cec5SDimitry Andric       // Predicate.
18650b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18660b57cec5SDimitry Andric       .addReg(0));
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tSTRi)
18690b57cec5SDimitry Andric       .addReg(ValReg)
18700b57cec5SDimitry Andric       .addReg(SrcReg)
18710b57cec5SDimitry Andric       // The offset immediate is #4. The operand value is scaled by 4 for the
18720b57cec5SDimitry Andric       // tSTR instruction.
18730b57cec5SDimitry Andric       .addImm(1)
18740b57cec5SDimitry Andric       // Predicate.
18750b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18760b57cec5SDimitry Andric       .addReg(0));
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
18790b57cec5SDimitry Andric       .addReg(ARM::R0)
18800b57cec5SDimitry Andric       .addReg(ARM::CPSR)
18810b57cec5SDimitry Andric       .addImm(0)
18820b57cec5SDimitry Andric       // Predicate.
18830b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18840b57cec5SDimitry Andric       .addReg(0));
18850b57cec5SDimitry Andric 
18860b57cec5SDimitry Andric     const MCExpr *SymbolExpr = MCSymbolRefExpr::create(Label, OutContext);
18870b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tB)
18880b57cec5SDimitry Andric       .addExpr(SymbolExpr)
18890b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18900b57cec5SDimitry Andric       .addReg(0));
18910b57cec5SDimitry Andric 
18920b57cec5SDimitry Andric     OutStreamer->AddComment("eh_setjmp end");
18930b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
18940b57cec5SDimitry Andric       .addReg(ARM::R0)
18950b57cec5SDimitry Andric       .addReg(ARM::CPSR)
18960b57cec5SDimitry Andric       .addImm(1)
18970b57cec5SDimitry Andric       // Predicate.
18980b57cec5SDimitry Andric       .addImm(ARMCC::AL)
18990b57cec5SDimitry Andric       .addReg(0));
19000b57cec5SDimitry Andric 
19010b57cec5SDimitry Andric     OutStreamer->EmitLabel(Label);
19020b57cec5SDimitry Andric     return;
19030b57cec5SDimitry Andric   }
19040b57cec5SDimitry Andric 
19050b57cec5SDimitry Andric   case ARM::Int_eh_sjlj_setjmp_nofp:
19060b57cec5SDimitry Andric   case ARM::Int_eh_sjlj_setjmp: {
19070b57cec5SDimitry Andric     // Two incoming args: GPR:$src, GPR:$val
19080b57cec5SDimitry Andric     // add $val, pc, #8
19090b57cec5SDimitry Andric     // str $val, [$src, #+4]
19100b57cec5SDimitry Andric     // mov r0, #0
19110b57cec5SDimitry Andric     // add pc, pc, #0
19120b57cec5SDimitry Andric     // mov r0, #1
1913*8bcb0991SDimitry Andric     Register SrcReg = MI->getOperand(0).getReg();
1914*8bcb0991SDimitry Andric     Register ValReg = MI->getOperand(1).getReg();
19150b57cec5SDimitry Andric 
19160b57cec5SDimitry Andric     OutStreamer->AddComment("eh_setjmp begin");
19170b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri)
19180b57cec5SDimitry Andric       .addReg(ValReg)
19190b57cec5SDimitry Andric       .addReg(ARM::PC)
19200b57cec5SDimitry Andric       .addImm(8)
19210b57cec5SDimitry Andric       // Predicate.
19220b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19230b57cec5SDimitry Andric       .addReg(0)
19240b57cec5SDimitry Andric       // 's' bit operand (always reg0 for this).
19250b57cec5SDimitry Andric       .addReg(0));
19260b57cec5SDimitry Andric 
19270b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::STRi12)
19280b57cec5SDimitry Andric       .addReg(ValReg)
19290b57cec5SDimitry Andric       .addReg(SrcReg)
19300b57cec5SDimitry Andric       .addImm(4)
19310b57cec5SDimitry Andric       // Predicate.
19320b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19330b57cec5SDimitry Andric       .addReg(0));
19340b57cec5SDimitry Andric 
19350b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi)
19360b57cec5SDimitry Andric       .addReg(ARM::R0)
19370b57cec5SDimitry Andric       .addImm(0)
19380b57cec5SDimitry Andric       // Predicate.
19390b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19400b57cec5SDimitry Andric       .addReg(0)
19410b57cec5SDimitry Andric       // 's' bit operand (always reg0 for this).
19420b57cec5SDimitry Andric       .addReg(0));
19430b57cec5SDimitry Andric 
19440b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri)
19450b57cec5SDimitry Andric       .addReg(ARM::PC)
19460b57cec5SDimitry Andric       .addReg(ARM::PC)
19470b57cec5SDimitry Andric       .addImm(0)
19480b57cec5SDimitry Andric       // Predicate.
19490b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19500b57cec5SDimitry Andric       .addReg(0)
19510b57cec5SDimitry Andric       // 's' bit operand (always reg0 for this).
19520b57cec5SDimitry Andric       .addReg(0));
19530b57cec5SDimitry Andric 
19540b57cec5SDimitry Andric     OutStreamer->AddComment("eh_setjmp end");
19550b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi)
19560b57cec5SDimitry Andric       .addReg(ARM::R0)
19570b57cec5SDimitry Andric       .addImm(1)
19580b57cec5SDimitry Andric       // Predicate.
19590b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19600b57cec5SDimitry Andric       .addReg(0)
19610b57cec5SDimitry Andric       // 's' bit operand (always reg0 for this).
19620b57cec5SDimitry Andric       .addReg(0));
19630b57cec5SDimitry Andric     return;
19640b57cec5SDimitry Andric   }
19650b57cec5SDimitry Andric   case ARM::Int_eh_sjlj_longjmp: {
19660b57cec5SDimitry Andric     // ldr sp, [$src, #8]
19670b57cec5SDimitry Andric     // ldr $scratch, [$src, #4]
19680b57cec5SDimitry Andric     // ldr r7, [$src]
19690b57cec5SDimitry Andric     // bx $scratch
1970*8bcb0991SDimitry Andric     Register SrcReg = MI->getOperand(0).getReg();
1971*8bcb0991SDimitry Andric     Register ScratchReg = MI->getOperand(1).getReg();
19720b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
19730b57cec5SDimitry Andric       .addReg(ARM::SP)
19740b57cec5SDimitry Andric       .addReg(SrcReg)
19750b57cec5SDimitry Andric       .addImm(8)
19760b57cec5SDimitry Andric       // Predicate.
19770b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19780b57cec5SDimitry Andric       .addReg(0));
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
19810b57cec5SDimitry Andric       .addReg(ScratchReg)
19820b57cec5SDimitry Andric       .addReg(SrcReg)
19830b57cec5SDimitry Andric       .addImm(4)
19840b57cec5SDimitry Andric       // Predicate.
19850b57cec5SDimitry Andric       .addImm(ARMCC::AL)
19860b57cec5SDimitry Andric       .addReg(0));
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric     if (STI.isTargetDarwin() || STI.isTargetWindows()) {
19890b57cec5SDimitry Andric       // These platforms always use the same frame register
19900b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
19910b57cec5SDimitry Andric         .addReg(FramePtr)
19920b57cec5SDimitry Andric         .addReg(SrcReg)
19930b57cec5SDimitry Andric         .addImm(0)
19940b57cec5SDimitry Andric         // Predicate.
19950b57cec5SDimitry Andric         .addImm(ARMCC::AL)
19960b57cec5SDimitry Andric         .addReg(0));
19970b57cec5SDimitry Andric     } else {
19980b57cec5SDimitry Andric       // If the calling code might use either R7 or R11 as
19990b57cec5SDimitry Andric       // frame pointer register, restore it into both.
20000b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
20010b57cec5SDimitry Andric         .addReg(ARM::R7)
20020b57cec5SDimitry Andric         .addReg(SrcReg)
20030b57cec5SDimitry Andric         .addImm(0)
20040b57cec5SDimitry Andric         // Predicate.
20050b57cec5SDimitry Andric         .addImm(ARMCC::AL)
20060b57cec5SDimitry Andric         .addReg(0));
20070b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
20080b57cec5SDimitry Andric         .addReg(ARM::R11)
20090b57cec5SDimitry Andric         .addReg(SrcReg)
20100b57cec5SDimitry Andric         .addImm(0)
20110b57cec5SDimitry Andric         // Predicate.
20120b57cec5SDimitry Andric         .addImm(ARMCC::AL)
20130b57cec5SDimitry Andric         .addReg(0));
20140b57cec5SDimitry Andric     }
20150b57cec5SDimitry Andric 
20160b57cec5SDimitry Andric     assert(Subtarget->hasV4TOps());
20170b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX)
20180b57cec5SDimitry Andric       .addReg(ScratchReg)
20190b57cec5SDimitry Andric       // Predicate.
20200b57cec5SDimitry Andric       .addImm(ARMCC::AL)
20210b57cec5SDimitry Andric       .addReg(0));
20220b57cec5SDimitry Andric     return;
20230b57cec5SDimitry Andric   }
20240b57cec5SDimitry Andric   case ARM::tInt_eh_sjlj_longjmp: {
20250b57cec5SDimitry Andric     // ldr $scratch, [$src, #8]
20260b57cec5SDimitry Andric     // mov sp, $scratch
20270b57cec5SDimitry Andric     // ldr $scratch, [$src, #4]
20280b57cec5SDimitry Andric     // ldr r7, [$src]
20290b57cec5SDimitry Andric     // bx $scratch
2030*8bcb0991SDimitry Andric     Register SrcReg = MI->getOperand(0).getReg();
2031*8bcb0991SDimitry Andric     Register ScratchReg = MI->getOperand(1).getReg();
20320b57cec5SDimitry Andric 
20330b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
20340b57cec5SDimitry Andric       .addReg(ScratchReg)
20350b57cec5SDimitry Andric       .addReg(SrcReg)
20360b57cec5SDimitry Andric       // The offset immediate is #8. The operand value is scaled by 4 for the
20370b57cec5SDimitry Andric       // tLDR instruction.
20380b57cec5SDimitry Andric       .addImm(2)
20390b57cec5SDimitry Andric       // Predicate.
20400b57cec5SDimitry Andric       .addImm(ARMCC::AL)
20410b57cec5SDimitry Andric       .addReg(0));
20420b57cec5SDimitry Andric 
20430b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
20440b57cec5SDimitry Andric       .addReg(ARM::SP)
20450b57cec5SDimitry Andric       .addReg(ScratchReg)
20460b57cec5SDimitry Andric       // Predicate.
20470b57cec5SDimitry Andric       .addImm(ARMCC::AL)
20480b57cec5SDimitry Andric       .addReg(0));
20490b57cec5SDimitry Andric 
20500b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
20510b57cec5SDimitry Andric       .addReg(ScratchReg)
20520b57cec5SDimitry Andric       .addReg(SrcReg)
20530b57cec5SDimitry Andric       .addImm(1)
20540b57cec5SDimitry Andric       // Predicate.
20550b57cec5SDimitry Andric       .addImm(ARMCC::AL)
20560b57cec5SDimitry Andric       .addReg(0));
20570b57cec5SDimitry Andric 
20580b57cec5SDimitry Andric     if (STI.isTargetDarwin() || STI.isTargetWindows()) {
20590b57cec5SDimitry Andric       // These platforms always use the same frame register
20600b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
20610b57cec5SDimitry Andric         .addReg(FramePtr)
20620b57cec5SDimitry Andric         .addReg(SrcReg)
20630b57cec5SDimitry Andric         .addImm(0)
20640b57cec5SDimitry Andric         // Predicate.
20650b57cec5SDimitry Andric         .addImm(ARMCC::AL)
20660b57cec5SDimitry Andric         .addReg(0));
20670b57cec5SDimitry Andric     } else {
20680b57cec5SDimitry Andric       // If the calling code might use either R7 or R11 as
20690b57cec5SDimitry Andric       // frame pointer register, restore it into both.
20700b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
20710b57cec5SDimitry Andric         .addReg(ARM::R7)
20720b57cec5SDimitry Andric         .addReg(SrcReg)
20730b57cec5SDimitry Andric         .addImm(0)
20740b57cec5SDimitry Andric         // Predicate.
20750b57cec5SDimitry Andric         .addImm(ARMCC::AL)
20760b57cec5SDimitry Andric         .addReg(0));
20770b57cec5SDimitry Andric       EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
20780b57cec5SDimitry Andric         .addReg(ARM::R11)
20790b57cec5SDimitry Andric         .addReg(SrcReg)
20800b57cec5SDimitry Andric         .addImm(0)
20810b57cec5SDimitry Andric         // Predicate.
20820b57cec5SDimitry Andric         .addImm(ARMCC::AL)
20830b57cec5SDimitry Andric         .addReg(0));
20840b57cec5SDimitry Andric     }
20850b57cec5SDimitry Andric 
20860b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX)
20870b57cec5SDimitry Andric       .addReg(ScratchReg)
20880b57cec5SDimitry Andric       // Predicate.
20890b57cec5SDimitry Andric       .addImm(ARMCC::AL)
20900b57cec5SDimitry Andric       .addReg(0));
20910b57cec5SDimitry Andric     return;
20920b57cec5SDimitry Andric   }
20930b57cec5SDimitry Andric   case ARM::tInt_WIN_eh_sjlj_longjmp: {
20940b57cec5SDimitry Andric     // ldr.w r11, [$src, #0]
20950b57cec5SDimitry Andric     // ldr.w  sp, [$src, #8]
20960b57cec5SDimitry Andric     // ldr.w  pc, [$src, #4]
20970b57cec5SDimitry Andric 
2098*8bcb0991SDimitry Andric     Register SrcReg = MI->getOperand(0).getReg();
20990b57cec5SDimitry Andric 
21000b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
21010b57cec5SDimitry Andric                                      .addReg(ARM::R11)
21020b57cec5SDimitry Andric                                      .addReg(SrcReg)
21030b57cec5SDimitry Andric                                      .addImm(0)
21040b57cec5SDimitry Andric                                      // Predicate
21050b57cec5SDimitry Andric                                      .addImm(ARMCC::AL)
21060b57cec5SDimitry Andric                                      .addReg(0));
21070b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
21080b57cec5SDimitry Andric                                      .addReg(ARM::SP)
21090b57cec5SDimitry Andric                                      .addReg(SrcReg)
21100b57cec5SDimitry Andric                                      .addImm(8)
21110b57cec5SDimitry Andric                                      // Predicate
21120b57cec5SDimitry Andric                                      .addImm(ARMCC::AL)
21130b57cec5SDimitry Andric                                      .addReg(0));
21140b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
21150b57cec5SDimitry Andric                                      .addReg(ARM::PC)
21160b57cec5SDimitry Andric                                      .addReg(SrcReg)
21170b57cec5SDimitry Andric                                      .addImm(4)
21180b57cec5SDimitry Andric                                      // Predicate
21190b57cec5SDimitry Andric                                      .addImm(ARMCC::AL)
21200b57cec5SDimitry Andric                                      .addReg(0));
21210b57cec5SDimitry Andric     return;
21220b57cec5SDimitry Andric   }
21230b57cec5SDimitry Andric   case ARM::PATCHABLE_FUNCTION_ENTER:
21240b57cec5SDimitry Andric     LowerPATCHABLE_FUNCTION_ENTER(*MI);
21250b57cec5SDimitry Andric     return;
21260b57cec5SDimitry Andric   case ARM::PATCHABLE_FUNCTION_EXIT:
21270b57cec5SDimitry Andric     LowerPATCHABLE_FUNCTION_EXIT(*MI);
21280b57cec5SDimitry Andric     return;
21290b57cec5SDimitry Andric   case ARM::PATCHABLE_TAIL_CALL:
21300b57cec5SDimitry Andric     LowerPATCHABLE_TAIL_CALL(*MI);
21310b57cec5SDimitry Andric     return;
21320b57cec5SDimitry Andric   }
21330b57cec5SDimitry Andric 
21340b57cec5SDimitry Andric   MCInst TmpInst;
21350b57cec5SDimitry Andric   LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
21360b57cec5SDimitry Andric 
21370b57cec5SDimitry Andric   EmitToStreamer(*OutStreamer, TmpInst);
21380b57cec5SDimitry Andric }
21390b57cec5SDimitry Andric 
21400b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
21410b57cec5SDimitry Andric // Target Registry Stuff
21420b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
21430b57cec5SDimitry Andric 
21440b57cec5SDimitry Andric // Force static initialization.
21450b57cec5SDimitry Andric extern "C" void LLVMInitializeARMAsmPrinter() {
21460b57cec5SDimitry Andric   RegisterAsmPrinter<ARMAsmPrinter> X(getTheARMLETarget());
21470b57cec5SDimitry Andric   RegisterAsmPrinter<ARMAsmPrinter> Y(getTheARMBETarget());
21480b57cec5SDimitry Andric   RegisterAsmPrinter<ARMAsmPrinter> A(getTheThumbLETarget());
21490b57cec5SDimitry Andric   RegisterAsmPrinter<ARMAsmPrinter> B(getTheThumbBETarget());
21500b57cec5SDimitry Andric }
2151