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