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