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