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