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