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