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