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 #define DEBUG_TYPE "asm-printer" 16 #include "ARMAsmPrinter.h" 17 #include "ARM.h" 18 #include "ARMBuildAttrs.h" 19 #include "ARMConstantPoolValue.h" 20 #include "ARMMachineFunctionInfo.h" 21 #include "ARMTargetMachine.h" 22 #include "ARMTargetObjectFile.h" 23 #include "InstPrinter/ARMInstPrinter.h" 24 #include "MCTargetDesc/ARMAddressingModes.h" 25 #include "MCTargetDesc/ARMMCExpr.h" 26 #include "llvm/ADT/SetVector.h" 27 #include "llvm/ADT/SmallString.h" 28 #include "llvm/Assembly/Writer.h" 29 #include "llvm/CodeGen/MachineFunctionPass.h" 30 #include "llvm/CodeGen/MachineJumpTableInfo.h" 31 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 32 #include "llvm/DebugInfo.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/DataLayout.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/CommandLine.h" 48 #include "llvm/Support/Debug.h" 49 #include "llvm/Support/ELF.h" 50 #include "llvm/Support/ErrorHandling.h" 51 #include "llvm/Support/TargetRegistry.h" 52 #include "llvm/Support/raw_ostream.h" 53 #include "llvm/Target/Mangler.h" 54 #include "llvm/Target/TargetMachine.h" 55 #include <cctype> 56 using namespace llvm; 57 58 namespace { 59 60 // Per section and per symbol attributes are not supported. 61 // To implement them we would need the ability to delay this emission 62 // until the assembly file is fully parsed/generated as only then do we 63 // know the symbol and section numbers. 64 class AttributeEmitter { 65 public: 66 virtual void MaybeSwitchVendor(StringRef Vendor) = 0; 67 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0; 68 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0; 69 virtual void Finish() = 0; 70 virtual ~AttributeEmitter() {} 71 }; 72 73 class AsmAttributeEmitter : public AttributeEmitter { 74 MCStreamer &Streamer; 75 76 public: 77 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {} 78 void MaybeSwitchVendor(StringRef Vendor) { } 79 80 void EmitAttribute(unsigned Attribute, unsigned Value) { 81 Streamer.EmitRawText("\t.eabi_attribute " + 82 Twine(Attribute) + ", " + Twine(Value)); 83 } 84 85 void EmitTextAttribute(unsigned Attribute, StringRef String) { 86 switch (Attribute) { 87 default: llvm_unreachable("Unsupported Text attribute in ASM Mode"); 88 case ARMBuildAttrs::CPU_name: 89 Streamer.EmitRawText(StringRef("\t.cpu ") + String.lower()); 90 break; 91 /* GAS requires .fpu to be emitted regardless of EABI attribute */ 92 case ARMBuildAttrs::Advanced_SIMD_arch: 93 case ARMBuildAttrs::VFP_arch: 94 Streamer.EmitRawText(StringRef("\t.fpu ") + String.lower()); 95 break; 96 } 97 } 98 void Finish() { } 99 }; 100 101 class ObjectAttributeEmitter : public AttributeEmitter { 102 // This structure holds all attributes, accounting for 103 // their string/numeric value, so we can later emmit them 104 // in declaration order, keeping all in the same vector 105 struct AttributeItemType { 106 enum { 107 HiddenAttribute = 0, 108 NumericAttribute, 109 TextAttribute 110 } Type; 111 unsigned Tag; 112 unsigned IntValue; 113 StringRef StringValue; 114 } AttributeItem; 115 116 MCObjectStreamer &Streamer; 117 StringRef CurrentVendor; 118 SmallVector<AttributeItemType, 64> Contents; 119 120 // Account for the ULEB/String size of each item, 121 // not just the number of items 122 size_t ContentsSize; 123 // FIXME: this should be in a more generic place, but 124 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf 125 size_t getULEBSize(int Value) { 126 size_t Size = 0; 127 do { 128 Value >>= 7; 129 Size += sizeof(int8_t); // Is this really necessary? 130 } while (Value); 131 return Size; 132 } 133 134 public: 135 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) : 136 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { } 137 138 void MaybeSwitchVendor(StringRef Vendor) { 139 assert(!Vendor.empty() && "Vendor cannot be empty."); 140 141 if (CurrentVendor.empty()) 142 CurrentVendor = Vendor; 143 else if (CurrentVendor == Vendor) 144 return; 145 else 146 Finish(); 147 148 CurrentVendor = Vendor; 149 150 assert(Contents.size() == 0); 151 } 152 153 void EmitAttribute(unsigned Attribute, unsigned Value) { 154 AttributeItemType attr = { 155 AttributeItemType::NumericAttribute, 156 Attribute, 157 Value, 158 StringRef("") 159 }; 160 ContentsSize += getULEBSize(Attribute); 161 ContentsSize += getULEBSize(Value); 162 Contents.push_back(attr); 163 } 164 165 void EmitTextAttribute(unsigned Attribute, StringRef String) { 166 AttributeItemType attr = { 167 AttributeItemType::TextAttribute, 168 Attribute, 169 0, 170 String 171 }; 172 ContentsSize += getULEBSize(Attribute); 173 // String + \0 174 ContentsSize += String.size()+1; 175 176 Contents.push_back(attr); 177 } 178 179 void Finish() { 180 // Vendor size + Vendor name + '\0' 181 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1; 182 183 // Tag + Tag Size 184 const size_t TagHeaderSize = 1 + 4; 185 186 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4); 187 Streamer.EmitBytes(CurrentVendor); 188 Streamer.EmitIntValue(0, 1); // '\0' 189 190 Streamer.EmitIntValue(ARMBuildAttrs::File, 1); 191 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4); 192 193 // Size should have been accounted for already, now 194 // emit each field as its type (ULEB or String) 195 for (unsigned int i=0; i<Contents.size(); ++i) { 196 AttributeItemType item = Contents[i]; 197 Streamer.EmitULEB128IntValue(item.Tag); 198 switch (item.Type) { 199 default: llvm_unreachable("Invalid attribute type"); 200 case AttributeItemType::NumericAttribute: 201 Streamer.EmitULEB128IntValue(item.IntValue); 202 break; 203 case AttributeItemType::TextAttribute: 204 Streamer.EmitBytes(item.StringValue.upper()); 205 Streamer.EmitIntValue(0, 1); // '\0' 206 break; 207 } 208 } 209 210 Contents.clear(); 211 } 212 }; 213 214 } // end of anonymous namespace 215 216 /// EmitDwarfRegOp - Emit dwarf register operation. 217 void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc, 218 bool Indirect) const { 219 const TargetRegisterInfo *RI = TM.getRegisterInfo(); 220 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1) { 221 AsmPrinter::EmitDwarfRegOp(MLoc, Indirect); 222 return; 223 } 224 assert(MLoc.isReg() && !Indirect && 225 "This doesn't support offset/indirection - implement it if needed"); 226 unsigned Reg = MLoc.getReg(); 227 if (Reg >= ARM::S0 && Reg <= ARM::S31) { 228 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering"); 229 // S registers are described as bit-pieces of a register 230 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0) 231 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32) 232 233 unsigned SReg = Reg - ARM::S0; 234 bool odd = SReg & 0x1; 235 unsigned Rx = 256 + (SReg >> 1); 236 237 OutStreamer.AddComment("DW_OP_regx for S register"); 238 EmitInt8(dwarf::DW_OP_regx); 239 240 OutStreamer.AddComment(Twine(SReg)); 241 EmitULEB128(Rx); 242 243 if (odd) { 244 OutStreamer.AddComment("DW_OP_bit_piece 32 32"); 245 EmitInt8(dwarf::DW_OP_bit_piece); 246 EmitULEB128(32); 247 EmitULEB128(32); 248 } else { 249 OutStreamer.AddComment("DW_OP_bit_piece 32 0"); 250 EmitInt8(dwarf::DW_OP_bit_piece); 251 EmitULEB128(32); 252 EmitULEB128(0); 253 } 254 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) { 255 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering"); 256 // Q registers Q0-Q15 are described by composing two D registers together. 257 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1) 258 // DW_OP_piece(8) 259 260 unsigned QReg = Reg - ARM::Q0; 261 unsigned D1 = 256 + 2 * QReg; 262 unsigned D2 = D1 + 1; 263 264 OutStreamer.AddComment("DW_OP_regx for Q register: D1"); 265 EmitInt8(dwarf::DW_OP_regx); 266 EmitULEB128(D1); 267 OutStreamer.AddComment("DW_OP_piece 8"); 268 EmitInt8(dwarf::DW_OP_piece); 269 EmitULEB128(8); 270 271 OutStreamer.AddComment("DW_OP_regx for Q register: D2"); 272 EmitInt8(dwarf::DW_OP_regx); 273 EmitULEB128(D2); 274 OutStreamer.AddComment("DW_OP_piece 8"); 275 EmitInt8(dwarf::DW_OP_piece); 276 EmitULEB128(8); 277 } 278 } 279 280 void ARMAsmPrinter::EmitFunctionBodyEnd() { 281 // Make sure to terminate any constant pools that were at the end 282 // of the function. 283 if (!InConstantPool) 284 return; 285 InConstantPool = false; 286 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd); 287 } 288 289 void ARMAsmPrinter::EmitFunctionEntryLabel() { 290 if (AFI->isThumbFunction()) { 291 OutStreamer.EmitAssemblerFlag(MCAF_Code16); 292 OutStreamer.EmitThumbFunc(CurrentFnSym); 293 } 294 295 OutStreamer.EmitLabel(CurrentFnSym); 296 } 297 298 void ARMAsmPrinter::EmitXXStructor(const Constant *CV) { 299 uint64_t Size = TM.getDataLayout()->getTypeAllocSize(CV->getType()); 300 assert(Size && "C++ constructor pointer had zero size!"); 301 302 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts()); 303 assert(GV && "C++ constructor pointer was not a GlobalValue!"); 304 305 const MCExpr *E = MCSymbolRefExpr::Create(Mang->getSymbol(GV), 306 (Subtarget->isTargetDarwin() 307 ? MCSymbolRefExpr::VK_None 308 : MCSymbolRefExpr::VK_ARM_TARGET1), 309 OutContext); 310 311 OutStreamer.EmitValue(E, Size); 312 } 313 314 /// runOnMachineFunction - This uses the EmitInstruction() 315 /// method to print assembly for each instruction. 316 /// 317 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 318 AFI = MF.getInfo<ARMFunctionInfo>(); 319 MCP = MF.getConstantPool(); 320 321 return AsmPrinter::runOnMachineFunction(MF); 322 } 323 324 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum, 325 raw_ostream &O, const char *Modifier) { 326 const MachineOperand &MO = MI->getOperand(OpNum); 327 unsigned TF = MO.getTargetFlags(); 328 329 switch (MO.getType()) { 330 default: llvm_unreachable("<unknown operand type>"); 331 case MachineOperand::MO_Register: { 332 unsigned Reg = MO.getReg(); 333 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 334 assert(!MO.getSubReg() && "Subregs should be eliminated!"); 335 if(ARM::GPRPairRegClass.contains(Reg)) { 336 const MachineFunction &MF = *MI->getParent()->getParent(); 337 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); 338 Reg = TRI->getSubReg(Reg, ARM::gsub_0); 339 } 340 O << ARMInstPrinter::getRegisterName(Reg); 341 break; 342 } 343 case MachineOperand::MO_Immediate: { 344 int64_t Imm = MO.getImm(); 345 O << '#'; 346 if ((Modifier && strcmp(Modifier, "lo16") == 0) || 347 (TF == ARMII::MO_LO16)) 348 O << ":lower16:"; 349 else if ((Modifier && strcmp(Modifier, "hi16") == 0) || 350 (TF == ARMII::MO_HI16)) 351 O << ":upper16:"; 352 O << Imm; 353 break; 354 } 355 case MachineOperand::MO_MachineBasicBlock: 356 O << *MO.getMBB()->getSymbol(); 357 return; 358 case MachineOperand::MO_GlobalAddress: { 359 const GlobalValue *GV = MO.getGlobal(); 360 if ((Modifier && strcmp(Modifier, "lo16") == 0) || 361 (TF & ARMII::MO_LO16)) 362 O << ":lower16:"; 363 else if ((Modifier && strcmp(Modifier, "hi16") == 0) || 364 (TF & ARMII::MO_HI16)) 365 O << ":upper16:"; 366 O << *Mang->getSymbol(GV); 367 368 printOffset(MO.getOffset(), O); 369 if (TF == ARMII::MO_PLT) 370 O << "(PLT)"; 371 break; 372 } 373 case MachineOperand::MO_ExternalSymbol: { 374 O << *GetExternalSymbolSymbol(MO.getSymbolName()); 375 if (TF == ARMII::MO_PLT) 376 O << "(PLT)"; 377 break; 378 } 379 case MachineOperand::MO_ConstantPoolIndex: 380 O << *GetCPISymbol(MO.getIndex()); 381 break; 382 case MachineOperand::MO_JumpTableIndex: 383 O << *GetJTISymbol(MO.getIndex()); 384 break; 385 } 386 } 387 388 //===--------------------------------------------------------------------===// 389 390 MCSymbol *ARMAsmPrinter:: 391 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const { 392 SmallString<60> Name; 393 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI" 394 << getFunctionNumber() << '_' << uid << '_' << uid2; 395 return OutContext.GetOrCreateSymbol(Name.str()); 396 } 397 398 399 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const { 400 SmallString<60> Name; 401 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH" 402 << getFunctionNumber(); 403 return OutContext.GetOrCreateSymbol(Name.str()); 404 } 405 406 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 407 unsigned AsmVariant, const char *ExtraCode, 408 raw_ostream &O) { 409 // Does this asm operand have a single letter operand modifier? 410 if (ExtraCode && ExtraCode[0]) { 411 if (ExtraCode[1] != 0) return true; // Unknown modifier. 412 413 switch (ExtraCode[0]) { 414 default: 415 // See if this is a generic print operand 416 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O); 417 case 'a': // Print as a memory address. 418 if (MI->getOperand(OpNum).isReg()) { 419 O << "[" 420 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg()) 421 << "]"; 422 return false; 423 } 424 // Fallthrough 425 case 'c': // Don't print "#" before an immediate operand. 426 if (!MI->getOperand(OpNum).isImm()) 427 return true; 428 O << MI->getOperand(OpNum).getImm(); 429 return false; 430 case 'P': // Print a VFP double precision register. 431 case 'q': // Print a NEON quad precision register. 432 printOperand(MI, OpNum, O); 433 return false; 434 case 'y': // Print a VFP single precision register as indexed double. 435 if (MI->getOperand(OpNum).isReg()) { 436 unsigned Reg = MI->getOperand(OpNum).getReg(); 437 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 438 // Find the 'd' register that has this 's' register as a sub-register, 439 // and determine the lane number. 440 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) { 441 if (!ARM::DPRRegClass.contains(*SR)) 442 continue; 443 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg; 444 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]"); 445 return false; 446 } 447 } 448 return true; 449 case 'B': // Bitwise inverse of integer or symbol without a preceding #. 450 if (!MI->getOperand(OpNum).isImm()) 451 return true; 452 O << ~(MI->getOperand(OpNum).getImm()); 453 return false; 454 case 'L': // The low 16 bits of an immediate constant. 455 if (!MI->getOperand(OpNum).isImm()) 456 return true; 457 O << (MI->getOperand(OpNum).getImm() & 0xffff); 458 return false; 459 case 'M': { // A register range suitable for LDM/STM. 460 if (!MI->getOperand(OpNum).isReg()) 461 return true; 462 const MachineOperand &MO = MI->getOperand(OpNum); 463 unsigned RegBegin = MO.getReg(); 464 // This takes advantage of the 2 operand-ness of ldm/stm and that we've 465 // already got the operands in registers that are operands to the 466 // inline asm statement. 467 O << "{"; 468 if (ARM::GPRPairRegClass.contains(RegBegin)) { 469 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 470 unsigned Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0); 471 O << ARMInstPrinter::getRegisterName(Reg0) << ", ";; 472 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1); 473 } 474 O << ARMInstPrinter::getRegisterName(RegBegin); 475 476 // FIXME: The register allocator not only may not have given us the 477 // registers in sequence, but may not be in ascending registers. This 478 // will require changes in the register allocator that'll need to be 479 // propagated down here if the operands change. 480 unsigned RegOps = OpNum + 1; 481 while (MI->getOperand(RegOps).isReg()) { 482 O << ", " 483 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg()); 484 RegOps++; 485 } 486 487 O << "}"; 488 489 return false; 490 } 491 case 'R': // The most significant register of a pair. 492 case 'Q': { // The least significant register of a pair. 493 if (OpNum == 0) 494 return true; 495 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1); 496 if (!FlagsOP.isImm()) 497 return true; 498 unsigned Flags = FlagsOP.getImm(); 499 500 // This operand may not be the one that actually provides the register. If 501 // it's tied to a previous one then we should refer instead to that one 502 // for registers and their classes. 503 unsigned TiedIdx; 504 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) { 505 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) { 506 unsigned OpFlags = MI->getOperand(OpNum).getImm(); 507 OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1; 508 } 509 Flags = MI->getOperand(OpNum).getImm(); 510 511 // Later code expects OpNum to be pointing at the register rather than 512 // the flags. 513 OpNum += 1; 514 } 515 516 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 517 unsigned RC; 518 InlineAsm::hasRegClassConstraint(Flags, RC); 519 if (RC == ARM::GPRPairRegClassID) { 520 if (NumVals != 1) 521 return true; 522 const MachineOperand &MO = MI->getOperand(OpNum); 523 if (!MO.isReg()) 524 return true; 525 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 526 unsigned Reg = TRI->getSubReg(MO.getReg(), ExtraCode[0] == 'Q' ? 527 ARM::gsub_0 : ARM::gsub_1); 528 O << ARMInstPrinter::getRegisterName(Reg); 529 return false; 530 } 531 if (NumVals != 2) 532 return true; 533 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1; 534 if (RegOp >= MI->getNumOperands()) 535 return true; 536 const MachineOperand &MO = MI->getOperand(RegOp); 537 if (!MO.isReg()) 538 return true; 539 unsigned Reg = MO.getReg(); 540 O << ARMInstPrinter::getRegisterName(Reg); 541 return false; 542 } 543 544 case 'e': // The low doubleword register of a NEON quad register. 545 case 'f': { // The high doubleword register of a NEON quad register. 546 if (!MI->getOperand(OpNum).isReg()) 547 return true; 548 unsigned Reg = MI->getOperand(OpNum).getReg(); 549 if (!ARM::QPRRegClass.contains(Reg)) 550 return true; 551 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 552 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ? 553 ARM::dsub_0 : ARM::dsub_1); 554 O << ARMInstPrinter::getRegisterName(SubReg); 555 return false; 556 } 557 558 // This modifier is not yet supported. 559 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1. 560 return true; 561 case 'H': { // The highest-numbered register of a pair. 562 const MachineOperand &MO = MI->getOperand(OpNum); 563 if (!MO.isReg()) 564 return true; 565 const MachineFunction &MF = *MI->getParent()->getParent(); 566 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); 567 unsigned Reg = MO.getReg(); 568 if(!ARM::GPRPairRegClass.contains(Reg)) 569 return false; 570 Reg = TRI->getSubReg(Reg, ARM::gsub_1); 571 O << ARMInstPrinter::getRegisterName(Reg); 572 return false; 573 } 574 } 575 } 576 577 printOperand(MI, OpNum, O); 578 return false; 579 } 580 581 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 582 unsigned OpNum, unsigned AsmVariant, 583 const char *ExtraCode, 584 raw_ostream &O) { 585 // Does this asm operand have a single letter operand modifier? 586 if (ExtraCode && ExtraCode[0]) { 587 if (ExtraCode[1] != 0) return true; // Unknown modifier. 588 589 switch (ExtraCode[0]) { 590 case 'A': // A memory operand for a VLD1/VST1 instruction. 591 default: return true; // Unknown modifier. 592 case 'm': // The base register of a memory operand. 593 if (!MI->getOperand(OpNum).isReg()) 594 return true; 595 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg()); 596 return false; 597 } 598 } 599 600 const MachineOperand &MO = MI->getOperand(OpNum); 601 assert(MO.isReg() && "unexpected inline asm memory operand"); 602 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]"; 603 return false; 604 } 605 606 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) { 607 if (Subtarget->isTargetDarwin()) { 608 Reloc::Model RelocM = TM.getRelocationModel(); 609 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) { 610 // Declare all the text sections up front (before the DWARF sections 611 // emitted by AsmPrinter::doInitialization) so the assembler will keep 612 // them together at the beginning of the object file. This helps 613 // avoid out-of-range branches that are due a fundamental limitation of 614 // the way symbol offsets are encoded with the current Darwin ARM 615 // relocations. 616 const TargetLoweringObjectFileMachO &TLOFMacho = 617 static_cast<const TargetLoweringObjectFileMachO &>( 618 getObjFileLowering()); 619 620 // Collect the set of sections our functions will go into. 621 SetVector<const MCSection *, SmallVector<const MCSection *, 8>, 622 SmallPtrSet<const MCSection *, 8> > TextSections; 623 // Default text section comes first. 624 TextSections.insert(TLOFMacho.getTextSection()); 625 // Now any user defined text sections from function attributes. 626 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F) 627 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) 628 TextSections.insert(TLOFMacho.SectionForGlobal(F, Mang, TM)); 629 // Now the coalescable sections. 630 TextSections.insert(TLOFMacho.getTextCoalSection()); 631 TextSections.insert(TLOFMacho.getConstTextCoalSection()); 632 633 // Emit the sections in the .s file header to fix the order. 634 for (unsigned i = 0, e = TextSections.size(); i != e; ++i) 635 OutStreamer.SwitchSection(TextSections[i]); 636 637 if (RelocM == Reloc::DynamicNoPIC) { 638 const MCSection *sect = 639 OutContext.getMachOSection("__TEXT", "__symbol_stub4", 640 MCSectionMachO::S_SYMBOL_STUBS, 641 12, SectionKind::getText()); 642 OutStreamer.SwitchSection(sect); 643 } else { 644 const MCSection *sect = 645 OutContext.getMachOSection("__TEXT", "__picsymbolstub4", 646 MCSectionMachO::S_SYMBOL_STUBS, 647 16, SectionKind::getText()); 648 OutStreamer.SwitchSection(sect); 649 } 650 const MCSection *StaticInitSect = 651 OutContext.getMachOSection("__TEXT", "__StaticInit", 652 MCSectionMachO::S_REGULAR | 653 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 654 SectionKind::getText()); 655 OutStreamer.SwitchSection(StaticInitSect); 656 } 657 } 658 659 // Use unified assembler syntax. 660 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified); 661 662 // Emit ARM Build Attributes 663 if (Subtarget->isTargetELF()) 664 emitAttributes(); 665 } 666 667 668 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) { 669 if (Subtarget->isTargetDarwin()) { 670 // All darwin targets use mach-o. 671 const TargetLoweringObjectFileMachO &TLOFMacho = 672 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 673 MachineModuleInfoMachO &MMIMacho = 674 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 675 676 // Output non-lazy-pointers for external and common global variables. 677 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList(); 678 679 if (!Stubs.empty()) { 680 // Switch with ".non_lazy_symbol_pointer" directive. 681 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection()); 682 EmitAlignment(2); 683 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 684 // L_foo$stub: 685 OutStreamer.EmitLabel(Stubs[i].first); 686 // .indirect_symbol _foo 687 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second; 688 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol); 689 690 if (MCSym.getInt()) 691 // External to current translation unit. 692 OutStreamer.EmitIntValue(0, 4/*size*/); 693 else 694 // Internal to current translation unit. 695 // 696 // When we place the LSDA into the TEXT section, the type info 697 // pointers need to be indirect and pc-rel. We accomplish this by 698 // using NLPs; however, sometimes the types are local to the file. 699 // We need to fill in the value for the NLP in those cases. 700 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(), 701 OutContext), 702 4/*size*/); 703 } 704 705 Stubs.clear(); 706 OutStreamer.AddBlankLine(); 707 } 708 709 Stubs = MMIMacho.GetHiddenGVStubList(); 710 if (!Stubs.empty()) { 711 OutStreamer.SwitchSection(getObjFileLowering().getDataSection()); 712 EmitAlignment(2); 713 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 714 // L_foo$stub: 715 OutStreamer.EmitLabel(Stubs[i].first); 716 // .long _foo 717 OutStreamer.EmitValue(MCSymbolRefExpr:: 718 Create(Stubs[i].second.getPointer(), 719 OutContext), 720 4/*size*/); 721 } 722 723 Stubs.clear(); 724 OutStreamer.AddBlankLine(); 725 } 726 727 // Funny Darwin hack: This flag tells the linker that no global symbols 728 // contain code that falls through to other global symbols (e.g. the obvious 729 // implementation of multiple entry points). If this doesn't occur, the 730 // linker can safely perform dead code stripping. Since LLVM never 731 // generates code that does this, it is always safe to set. 732 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 733 } 734 // FIXME: This should eventually end up somewhere else where more 735 // intelligent flag decisions can be made. For now we are just maintaining 736 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default. 737 if (MCELFStreamer *MES = dyn_cast<MCELFStreamer>(&OutStreamer)) 738 MES->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5); 739 } 740 741 //===----------------------------------------------------------------------===// 742 // Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile() 743 // FIXME: 744 // The following seem like one-off assembler flags, but they actually need 745 // to appear in the .ARM.attributes section in ELF. 746 // Instead of subclassing the MCELFStreamer, we do the work here. 747 748 void ARMAsmPrinter::emitAttributes() { 749 750 emitARMAttributeSection(); 751 752 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */ 753 bool emitFPU = false; 754 AttributeEmitter *AttrEmitter; 755 if (OutStreamer.hasRawTextSupport()) { 756 AttrEmitter = new AsmAttributeEmitter(OutStreamer); 757 emitFPU = true; 758 } else { 759 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer); 760 AttrEmitter = new ObjectAttributeEmitter(O); 761 } 762 763 AttrEmitter->MaybeSwitchVendor("aeabi"); 764 765 std::string CPUString = Subtarget->getCPUString(); 766 767 if (CPUString == "cortex-a8" || 768 Subtarget->isCortexA8()) { 769 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8"); 770 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7); 771 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile, 772 ARMBuildAttrs::ApplicationProfile); 773 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use, 774 ARMBuildAttrs::Allowed); 775 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use, 776 ARMBuildAttrs::AllowThumb32); 777 // Fixme: figure out when this is emitted. 778 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch, 779 // ARMBuildAttrs::AllowWMMXv1); 780 // 781 782 /// ADD additional Else-cases here! 783 } else if (CPUString == "xscale") { 784 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ); 785 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use, 786 ARMBuildAttrs::Allowed); 787 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use, 788 ARMBuildAttrs::Allowed); 789 } else if (Subtarget->hasV8Ops()) 790 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v8); 791 else if (Subtarget->hasV7Ops()) { 792 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7); 793 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use, 794 ARMBuildAttrs::AllowThumb32); 795 } else if (Subtarget->hasV6T2Ops()) 796 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6T2); 797 else if (Subtarget->hasV6Ops()) 798 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6); 799 else if (Subtarget->hasV5TEOps()) 800 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TE); 801 else if (Subtarget->hasV5TOps()) 802 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5T); 803 else if (Subtarget->hasV4TOps()) 804 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T); 805 else 806 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4); 807 808 if (Subtarget->hasNEON() && emitFPU) { 809 /* NEON is not exactly a VFP architecture, but GAS emit one of 810 * neon/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */ 811 if (Subtarget->hasVFP4()) 812 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, 813 "neon-vfpv4"); 814 else 815 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon"); 816 /* If emitted for NEON, omit from VFP below, since you can have both 817 * NEON and VFP in build attributes but only one .fpu */ 818 emitFPU = false; 819 } 820 821 /* V8FP + .fpu */ 822 if (Subtarget->hasV8FP()) { 823 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 824 ARMBuildAttrs::AllowV8FPA); 825 if (emitFPU) 826 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "v8fp"); 827 /* VFPv4 + .fpu */ 828 } else if (Subtarget->hasVFP4()) { 829 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 830 ARMBuildAttrs::AllowFPv4A); 831 if (emitFPU) 832 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv4"); 833 834 /* VFPv3 + .fpu */ 835 } else if (Subtarget->hasVFP3()) { 836 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 837 ARMBuildAttrs::AllowFPv3A); 838 if (emitFPU) 839 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3"); 840 841 /* VFPv2 + .fpu */ 842 } else if (Subtarget->hasVFP2()) { 843 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 844 ARMBuildAttrs::AllowFPv2); 845 if (emitFPU) 846 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2"); 847 } 848 849 /* TODO: ARMBuildAttrs::Allowed is not completely accurate, 850 * since NEON can have 1 (allowed) or 2 (MAC operations) */ 851 if (Subtarget->hasNEON()) { 852 if (Subtarget->hasV8Ops()) 853 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch, 854 ARMBuildAttrs::AllowedNeonV8); 855 else 856 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch, 857 ARMBuildAttrs::Allowed); 858 } 859 860 // Signal various FP modes. 861 if (!TM.Options.UnsafeFPMath) { 862 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal, 863 ARMBuildAttrs::Allowed); 864 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 865 ARMBuildAttrs::Allowed); 866 } 867 868 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath) 869 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 870 ARMBuildAttrs::Allowed); 871 else 872 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 873 ARMBuildAttrs::AllowIEE754); 874 875 // FIXME: add more flags to ARMBuildAttrs.h 876 // 8-bytes alignment stuff. 877 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1); 878 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1); 879 880 // Hard float. Use both S and D registers and conform to AAPCS-VFP. 881 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) { 882 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3); 883 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1); 884 } 885 // FIXME: Should we signal R9 usage? 886 887 if (Subtarget->hasDivide()) 888 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1); 889 890 AttrEmitter->Finish(); 891 delete AttrEmitter; 892 } 893 894 void ARMAsmPrinter::emitARMAttributeSection() { 895 // <format-version> 896 // [ <section-length> "vendor-name" 897 // [ <file-tag> <size> <attribute>* 898 // | <section-tag> <size> <section-number>* 0 <attribute>* 899 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>* 900 // ]+ 901 // ]* 902 903 if (OutStreamer.hasRawTextSupport()) 904 return; 905 906 const ARMElfTargetObjectFile &TLOFELF = 907 static_cast<const ARMElfTargetObjectFile &> 908 (getObjFileLowering()); 909 910 OutStreamer.SwitchSection(TLOFELF.getAttributesSection()); 911 912 // Format version 913 OutStreamer.EmitIntValue(0x41, 1); 914 } 915 916 //===----------------------------------------------------------------------===// 917 918 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber, 919 unsigned LabelId, MCContext &Ctx) { 920 921 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix) 922 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId)); 923 return Label; 924 } 925 926 static MCSymbolRefExpr::VariantKind 927 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) { 928 switch (Modifier) { 929 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None; 930 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD; 931 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF; 932 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF; 933 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT; 934 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF; 935 } 936 llvm_unreachable("Invalid ARMCPModifier!"); 937 } 938 939 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) { 940 bool isIndirect = Subtarget->isTargetDarwin() && 941 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()); 942 if (!isIndirect) 943 return Mang->getSymbol(GV); 944 945 // FIXME: Remove this when Darwin transition to @GOT like syntax. 946 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 947 MachineModuleInfoMachO &MMIMachO = 948 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 949 MachineModuleInfoImpl::StubValueTy &StubSym = 950 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) : 951 MMIMachO.getGVStubEntry(MCSym); 952 if (StubSym.getPointer() == 0) 953 StubSym = MachineModuleInfoImpl:: 954 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage()); 955 return MCSym; 956 } 957 958 void ARMAsmPrinter:: 959 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { 960 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType()); 961 962 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV); 963 964 MCSymbol *MCSym; 965 if (ACPV->isLSDA()) { 966 SmallString<128> Str; 967 raw_svector_ostream OS(Str); 968 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber(); 969 MCSym = OutContext.GetOrCreateSymbol(OS.str()); 970 } else if (ACPV->isBlockAddress()) { 971 const BlockAddress *BA = 972 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(); 973 MCSym = GetBlockAddressSymbol(BA); 974 } else if (ACPV->isGlobalValue()) { 975 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV(); 976 MCSym = GetARMGVSymbol(GV); 977 } else if (ACPV->isMachineBasicBlock()) { 978 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB(); 979 MCSym = MBB->getSymbol(); 980 } else { 981 assert(ACPV->isExtSymbol() && "unrecognized constant pool value"); 982 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(); 983 MCSym = GetExternalSymbolSymbol(Sym); 984 } 985 986 // Create an MCSymbol for the reference. 987 const MCExpr *Expr = 988 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()), 989 OutContext); 990 991 if (ACPV->getPCAdjustment()) { 992 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(), 993 getFunctionNumber(), 994 ACPV->getLabelId(), 995 OutContext); 996 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext); 997 PCRelExpr = 998 MCBinaryExpr::CreateAdd(PCRelExpr, 999 MCConstantExpr::Create(ACPV->getPCAdjustment(), 1000 OutContext), 1001 OutContext); 1002 if (ACPV->mustAddCurrentAddress()) { 1003 // We want "(<expr> - .)", but MC doesn't have a concept of the '.' 1004 // label, so just emit a local label end reference that instead. 1005 MCSymbol *DotSym = OutContext.CreateTempSymbol(); 1006 OutStreamer.EmitLabel(DotSym); 1007 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext); 1008 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext); 1009 } 1010 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext); 1011 } 1012 OutStreamer.EmitValue(Expr, Size); 1013 } 1014 1015 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) { 1016 unsigned Opcode = MI->getOpcode(); 1017 int OpNum = 1; 1018 if (Opcode == ARM::BR_JTadd) 1019 OpNum = 2; 1020 else if (Opcode == ARM::BR_JTm) 1021 OpNum = 3; 1022 1023 const MachineOperand &MO1 = MI->getOperand(OpNum); 1024 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id 1025 unsigned JTI = MO1.getIndex(); 1026 1027 // Emit a label for the jump table. 1028 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm()); 1029 OutStreamer.EmitLabel(JTISymbol); 1030 1031 // Mark the jump table as data-in-code. 1032 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32); 1033 1034 // Emit each entry of the table. 1035 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1036 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 1037 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 1038 1039 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) { 1040 MachineBasicBlock *MBB = JTBBs[i]; 1041 // Construct an MCExpr for the entry. We want a value of the form: 1042 // (BasicBlockAddr - TableBeginAddr) 1043 // 1044 // For example, a table with entries jumping to basic blocks BB0 and BB1 1045 // would look like: 1046 // LJTI_0_0: 1047 // .word (LBB0 - LJTI_0_0) 1048 // .word (LBB1 - LJTI_0_0) 1049 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext); 1050 1051 if (TM.getRelocationModel() == Reloc::PIC_) 1052 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol, 1053 OutContext), 1054 OutContext); 1055 // If we're generating a table of Thumb addresses in static relocation 1056 // model, we need to add one to keep interworking correctly. 1057 else if (AFI->isThumbFunction()) 1058 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext), 1059 OutContext); 1060 OutStreamer.EmitValue(Expr, 4); 1061 } 1062 // Mark the end of jump table data-in-code region. 1063 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd); 1064 } 1065 1066 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) { 1067 unsigned Opcode = MI->getOpcode(); 1068 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1; 1069 const MachineOperand &MO1 = MI->getOperand(OpNum); 1070 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id 1071 unsigned JTI = MO1.getIndex(); 1072 1073 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm()); 1074 OutStreamer.EmitLabel(JTISymbol); 1075 1076 // Emit each entry of the table. 1077 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1078 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 1079 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 1080 unsigned OffsetWidth = 4; 1081 if (MI->getOpcode() == ARM::t2TBB_JT) { 1082 OffsetWidth = 1; 1083 // Mark the jump table as data-in-code. 1084 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8); 1085 } else if (MI->getOpcode() == ARM::t2TBH_JT) { 1086 OffsetWidth = 2; 1087 // Mark the jump table as data-in-code. 1088 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16); 1089 } 1090 1091 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) { 1092 MachineBasicBlock *MBB = JTBBs[i]; 1093 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(), 1094 OutContext); 1095 // If this isn't a TBB or TBH, the entries are direct branch instructions. 1096 if (OffsetWidth == 4) { 1097 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2B) 1098 .addExpr(MBBSymbolExpr) 1099 .addImm(ARMCC::AL) 1100 .addReg(0)); 1101 continue; 1102 } 1103 // Otherwise it's an offset from the dispatch instruction. Construct an 1104 // MCExpr for the entry. We want a value of the form: 1105 // (BasicBlockAddr - TableBeginAddr) / 2 1106 // 1107 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1 1108 // would look like: 1109 // LJTI_0_0: 1110 // .byte (LBB0 - LJTI_0_0) / 2 1111 // .byte (LBB1 - LJTI_0_0) / 2 1112 const MCExpr *Expr = 1113 MCBinaryExpr::CreateSub(MBBSymbolExpr, 1114 MCSymbolRefExpr::Create(JTISymbol, OutContext), 1115 OutContext); 1116 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext), 1117 OutContext); 1118 OutStreamer.EmitValue(Expr, OffsetWidth); 1119 } 1120 // Mark the end of jump table data-in-code region. 32-bit offsets use 1121 // actual branch instructions here, so we don't mark those as a data-region 1122 // at all. 1123 if (OffsetWidth != 4) 1124 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd); 1125 } 1126 1127 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) { 1128 assert(MI->getFlag(MachineInstr::FrameSetup) && 1129 "Only instruction which are involved into frame setup code are allowed"); 1130 1131 const MachineFunction &MF = *MI->getParent()->getParent(); 1132 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 1133 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>(); 1134 1135 unsigned FramePtr = RegInfo->getFrameRegister(MF); 1136 unsigned Opc = MI->getOpcode(); 1137 unsigned SrcReg, DstReg; 1138 1139 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) { 1140 // Two special cases: 1141 // 1) tPUSH does not have src/dst regs. 1142 // 2) for Thumb1 code we sometimes materialize the constant via constpool 1143 // load. Yes, this is pretty fragile, but for now I don't see better 1144 // way... :( 1145 SrcReg = DstReg = ARM::SP; 1146 } else { 1147 SrcReg = MI->getOperand(1).getReg(); 1148 DstReg = MI->getOperand(0).getReg(); 1149 } 1150 1151 // Try to figure out the unwinding opcode out of src / dst regs. 1152 if (MI->mayStore()) { 1153 // Register saves. 1154 assert(DstReg == ARM::SP && 1155 "Only stack pointer as a destination reg is supported"); 1156 1157 SmallVector<unsigned, 4> RegList; 1158 // Skip src & dst reg, and pred ops. 1159 unsigned StartOp = 2 + 2; 1160 // Use all the operands. 1161 unsigned NumOffset = 0; 1162 1163 switch (Opc) { 1164 default: 1165 MI->dump(); 1166 llvm_unreachable("Unsupported opcode for unwinding information"); 1167 case ARM::tPUSH: 1168 // Special case here: no src & dst reg, but two extra imp ops. 1169 StartOp = 2; NumOffset = 2; 1170 case ARM::STMDB_UPD: 1171 case ARM::t2STMDB_UPD: 1172 case ARM::VSTMDDB_UPD: 1173 assert(SrcReg == ARM::SP && 1174 "Only stack pointer as a source reg is supported"); 1175 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset; 1176 i != NumOps; ++i) { 1177 const MachineOperand &MO = MI->getOperand(i); 1178 // Actually, there should never be any impdef stuff here. Skip it 1179 // temporary to workaround PR11902. 1180 if (MO.isImplicit()) 1181 continue; 1182 RegList.push_back(MO.getReg()); 1183 } 1184 break; 1185 case ARM::STR_PRE_IMM: 1186 case ARM::STR_PRE_REG: 1187 case ARM::t2STR_PRE: 1188 assert(MI->getOperand(2).getReg() == ARM::SP && 1189 "Only stack pointer as a source reg is supported"); 1190 RegList.push_back(SrcReg); 1191 break; 1192 } 1193 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD); 1194 } else { 1195 // Changes of stack / frame pointer. 1196 if (SrcReg == ARM::SP) { 1197 int64_t Offset = 0; 1198 switch (Opc) { 1199 default: 1200 MI->dump(); 1201 llvm_unreachable("Unsupported opcode for unwinding information"); 1202 case ARM::MOVr: 1203 case ARM::tMOVr: 1204 Offset = 0; 1205 break; 1206 case ARM::ADDri: 1207 Offset = -MI->getOperand(2).getImm(); 1208 break; 1209 case ARM::SUBri: 1210 case ARM::t2SUBri: 1211 Offset = MI->getOperand(2).getImm(); 1212 break; 1213 case ARM::tSUBspi: 1214 Offset = MI->getOperand(2).getImm()*4; 1215 break; 1216 case ARM::tADDspi: 1217 case ARM::tADDrSPi: 1218 Offset = -MI->getOperand(2).getImm()*4; 1219 break; 1220 case ARM::tLDRpci: { 1221 // Grab the constpool index and check, whether it corresponds to 1222 // original or cloned constpool entry. 1223 unsigned CPI = MI->getOperand(1).getIndex(); 1224 const MachineConstantPool *MCP = MF.getConstantPool(); 1225 if (CPI >= MCP->getConstants().size()) 1226 CPI = AFI.getOriginalCPIdx(CPI); 1227 assert(CPI != -1U && "Invalid constpool index"); 1228 1229 // Derive the actual offset. 1230 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI]; 1231 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry"); 1232 // FIXME: Check for user, it should be "add" instruction! 1233 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue(); 1234 break; 1235 } 1236 } 1237 1238 if (DstReg == FramePtr && FramePtr != ARM::SP) 1239 // Set-up of the frame pointer. Positive values correspond to "add" 1240 // instruction. 1241 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset); 1242 else if (DstReg == ARM::SP) { 1243 // Change of SP by an offset. Positive values correspond to "sub" 1244 // instruction. 1245 OutStreamer.EmitPad(Offset); 1246 } else { 1247 MI->dump(); 1248 llvm_unreachable("Unsupported opcode for unwinding information"); 1249 } 1250 } else if (DstReg == ARM::SP) { 1251 // FIXME: .movsp goes here 1252 MI->dump(); 1253 llvm_unreachable("Unsupported opcode for unwinding information"); 1254 } 1255 else { 1256 MI->dump(); 1257 llvm_unreachable("Unsupported opcode for unwinding information"); 1258 } 1259 } 1260 } 1261 1262 extern cl::opt<bool> EnableARMEHABI; 1263 1264 // Simple pseudo-instructions have their lowering (with expansion to real 1265 // instructions) auto-generated. 1266 #include "ARMGenMCPseudoLowering.inc" 1267 1268 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) { 1269 // If we just ended a constant pool, mark it as such. 1270 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) { 1271 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd); 1272 InConstantPool = false; 1273 } 1274 1275 // Emit unwinding stuff for frame-related instructions 1276 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup)) 1277 EmitUnwindingInstruction(MI); 1278 1279 // Do any auto-generated pseudo lowerings. 1280 if (emitPseudoExpansionLowering(OutStreamer, MI)) 1281 return; 1282 1283 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) && 1284 "Pseudo flag setting opcode should be expanded early"); 1285 1286 // Check for manual lowerings. 1287 unsigned Opc = MI->getOpcode(); 1288 switch (Opc) { 1289 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass"); 1290 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing"); 1291 case ARM::LEApcrel: 1292 case ARM::tLEApcrel: 1293 case ARM::t2LEApcrel: { 1294 // FIXME: Need to also handle globals and externals 1295 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex()); 1296 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() == 1297 ARM::t2LEApcrel ? ARM::t2ADR 1298 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR 1299 : ARM::ADR)) 1300 .addReg(MI->getOperand(0).getReg()) 1301 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext)) 1302 // Add predicate operands. 1303 .addImm(MI->getOperand(2).getImm()) 1304 .addReg(MI->getOperand(3).getReg())); 1305 return; 1306 } 1307 case ARM::LEApcrelJT: 1308 case ARM::tLEApcrelJT: 1309 case ARM::t2LEApcrelJT: { 1310 MCSymbol *JTIPICSymbol = 1311 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(), 1312 MI->getOperand(2).getImm()); 1313 OutStreamer.EmitInstruction(MCInstBuilder(MI->getOpcode() == 1314 ARM::t2LEApcrelJT ? ARM::t2ADR 1315 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR 1316 : ARM::ADR)) 1317 .addReg(MI->getOperand(0).getReg()) 1318 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext)) 1319 // Add predicate operands. 1320 .addImm(MI->getOperand(3).getImm()) 1321 .addReg(MI->getOperand(4).getReg())); 1322 return; 1323 } 1324 // Darwin call instructions are just normal call instructions with different 1325 // clobber semantics (they clobber R9). 1326 case ARM::BX_CALL: { 1327 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr) 1328 .addReg(ARM::LR) 1329 .addReg(ARM::PC) 1330 // Add predicate operands. 1331 .addImm(ARMCC::AL) 1332 .addReg(0) 1333 // Add 's' bit operand (always reg0 for this) 1334 .addReg(0)); 1335 1336 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX) 1337 .addReg(MI->getOperand(0).getReg())); 1338 return; 1339 } 1340 case ARM::tBX_CALL: { 1341 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr) 1342 .addReg(ARM::LR) 1343 .addReg(ARM::PC) 1344 // Add predicate operands. 1345 .addImm(ARMCC::AL) 1346 .addReg(0)); 1347 1348 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX) 1349 .addReg(MI->getOperand(0).getReg()) 1350 // Add predicate operands. 1351 .addImm(ARMCC::AL) 1352 .addReg(0)); 1353 return; 1354 } 1355 case ARM::BMOVPCRX_CALL: { 1356 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr) 1357 .addReg(ARM::LR) 1358 .addReg(ARM::PC) 1359 // Add predicate operands. 1360 .addImm(ARMCC::AL) 1361 .addReg(0) 1362 // Add 's' bit operand (always reg0 for this) 1363 .addReg(0)); 1364 1365 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr) 1366 .addReg(ARM::PC) 1367 .addReg(MI->getOperand(0).getReg()) 1368 // Add predicate operands. 1369 .addImm(ARMCC::AL) 1370 .addReg(0) 1371 // Add 's' bit operand (always reg0 for this) 1372 .addReg(0)); 1373 return; 1374 } 1375 case ARM::BMOVPCB_CALL: { 1376 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVr) 1377 .addReg(ARM::LR) 1378 .addReg(ARM::PC) 1379 // Add predicate operands. 1380 .addImm(ARMCC::AL) 1381 .addReg(0) 1382 // Add 's' bit operand (always reg0 for this) 1383 .addReg(0)); 1384 1385 const GlobalValue *GV = MI->getOperand(0).getGlobal(); 1386 MCSymbol *GVSym = Mang->getSymbol(GV); 1387 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext); 1388 OutStreamer.EmitInstruction(MCInstBuilder(ARM::Bcc) 1389 .addExpr(GVSymExpr) 1390 // Add predicate operands. 1391 .addImm(ARMCC::AL) 1392 .addReg(0)); 1393 return; 1394 } 1395 case ARM::MOVi16_ga_pcrel: 1396 case ARM::t2MOVi16_ga_pcrel: { 1397 MCInst TmpInst; 1398 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16); 1399 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 1400 1401 unsigned TF = MI->getOperand(1).getTargetFlags(); 1402 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC; 1403 const GlobalValue *GV = MI->getOperand(1).getGlobal(); 1404 MCSymbol *GVSym = GetARMGVSymbol(GV); 1405 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext); 1406 if (isPIC) { 1407 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(), 1408 getFunctionNumber(), 1409 MI->getOperand(2).getImm(), OutContext); 1410 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext); 1411 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4; 1412 const MCExpr *PCRelExpr = 1413 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr, 1414 MCBinaryExpr::CreateAdd(LabelSymExpr, 1415 MCConstantExpr::Create(PCAdj, OutContext), 1416 OutContext), OutContext), OutContext); 1417 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr)); 1418 } else { 1419 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext); 1420 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr)); 1421 } 1422 1423 // Add predicate operands. 1424 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL)); 1425 TmpInst.addOperand(MCOperand::CreateReg(0)); 1426 // Add 's' bit operand (always reg0 for this) 1427 TmpInst.addOperand(MCOperand::CreateReg(0)); 1428 OutStreamer.EmitInstruction(TmpInst); 1429 return; 1430 } 1431 case ARM::MOVTi16_ga_pcrel: 1432 case ARM::t2MOVTi16_ga_pcrel: { 1433 MCInst TmpInst; 1434 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel 1435 ? ARM::MOVTi16 : ARM::t2MOVTi16); 1436 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 1437 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg())); 1438 1439 unsigned TF = MI->getOperand(2).getTargetFlags(); 1440 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC; 1441 const GlobalValue *GV = MI->getOperand(2).getGlobal(); 1442 MCSymbol *GVSym = GetARMGVSymbol(GV); 1443 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext); 1444 if (isPIC) { 1445 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(), 1446 getFunctionNumber(), 1447 MI->getOperand(3).getImm(), OutContext); 1448 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext); 1449 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4; 1450 const MCExpr *PCRelExpr = 1451 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr, 1452 MCBinaryExpr::CreateAdd(LabelSymExpr, 1453 MCConstantExpr::Create(PCAdj, OutContext), 1454 OutContext), OutContext), OutContext); 1455 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr)); 1456 } else { 1457 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext); 1458 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr)); 1459 } 1460 // Add predicate operands. 1461 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL)); 1462 TmpInst.addOperand(MCOperand::CreateReg(0)); 1463 // Add 's' bit operand (always reg0 for this) 1464 TmpInst.addOperand(MCOperand::CreateReg(0)); 1465 OutStreamer.EmitInstruction(TmpInst); 1466 return; 1467 } 1468 case ARM::tPICADD: { 1469 // This is a pseudo op for a label + instruction sequence, which looks like: 1470 // LPC0: 1471 // add r0, pc 1472 // This adds the address of LPC0 to r0. 1473 1474 // Emit the label. 1475 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(), 1476 getFunctionNumber(), MI->getOperand(2).getImm(), 1477 OutContext)); 1478 1479 // Form and emit the add. 1480 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDhirr) 1481 .addReg(MI->getOperand(0).getReg()) 1482 .addReg(MI->getOperand(0).getReg()) 1483 .addReg(ARM::PC) 1484 // Add predicate operands. 1485 .addImm(ARMCC::AL) 1486 .addReg(0)); 1487 return; 1488 } 1489 case ARM::PICADD: { 1490 // This is a pseudo op for a label + instruction sequence, which looks like: 1491 // LPC0: 1492 // add r0, pc, r0 1493 // This adds the address of LPC0 to r0. 1494 1495 // Emit the label. 1496 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(), 1497 getFunctionNumber(), MI->getOperand(2).getImm(), 1498 OutContext)); 1499 1500 // Form and emit the add. 1501 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr) 1502 .addReg(MI->getOperand(0).getReg()) 1503 .addReg(ARM::PC) 1504 .addReg(MI->getOperand(1).getReg()) 1505 // Add predicate operands. 1506 .addImm(MI->getOperand(3).getImm()) 1507 .addReg(MI->getOperand(4).getReg()) 1508 // Add 's' bit operand (always reg0 for this) 1509 .addReg(0)); 1510 return; 1511 } 1512 case ARM::PICSTR: 1513 case ARM::PICSTRB: 1514 case ARM::PICSTRH: 1515 case ARM::PICLDR: 1516 case ARM::PICLDRB: 1517 case ARM::PICLDRH: 1518 case ARM::PICLDRSB: 1519 case ARM::PICLDRSH: { 1520 // This is a pseudo op for a label + instruction sequence, which looks like: 1521 // LPC0: 1522 // OP r0, [pc, r0] 1523 // The LCP0 label is referenced by a constant pool entry in order to get 1524 // a PC-relative address at the ldr instruction. 1525 1526 // Emit the label. 1527 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(), 1528 getFunctionNumber(), MI->getOperand(2).getImm(), 1529 OutContext)); 1530 1531 // Form and emit the load 1532 unsigned Opcode; 1533 switch (MI->getOpcode()) { 1534 default: 1535 llvm_unreachable("Unexpected opcode!"); 1536 case ARM::PICSTR: Opcode = ARM::STRrs; break; 1537 case ARM::PICSTRB: Opcode = ARM::STRBrs; break; 1538 case ARM::PICSTRH: Opcode = ARM::STRH; break; 1539 case ARM::PICLDR: Opcode = ARM::LDRrs; break; 1540 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break; 1541 case ARM::PICLDRH: Opcode = ARM::LDRH; break; 1542 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break; 1543 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break; 1544 } 1545 OutStreamer.EmitInstruction(MCInstBuilder(Opcode) 1546 .addReg(MI->getOperand(0).getReg()) 1547 .addReg(ARM::PC) 1548 .addReg(MI->getOperand(1).getReg()) 1549 .addImm(0) 1550 // Add predicate operands. 1551 .addImm(MI->getOperand(3).getImm()) 1552 .addReg(MI->getOperand(4).getReg())); 1553 1554 return; 1555 } 1556 case ARM::CONSTPOOL_ENTRY: { 1557 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool 1558 /// in the function. The first operand is the ID# for this instruction, the 1559 /// second is the index into the MachineConstantPool that this is, the third 1560 /// is the size in bytes of this constant pool entry. 1561 /// The required alignment is specified on the basic block holding this MI. 1562 unsigned LabelId = (unsigned)MI->getOperand(0).getImm(); 1563 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex(); 1564 1565 // If this is the first entry of the pool, mark it. 1566 if (!InConstantPool) { 1567 OutStreamer.EmitDataRegion(MCDR_DataRegion); 1568 InConstantPool = true; 1569 } 1570 1571 OutStreamer.EmitLabel(GetCPISymbol(LabelId)); 1572 1573 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx]; 1574 if (MCPE.isMachineConstantPoolEntry()) 1575 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal); 1576 else 1577 EmitGlobalConstant(MCPE.Val.ConstVal); 1578 return; 1579 } 1580 case ARM::t2BR_JT: { 1581 // Lower and emit the instruction itself, then the jump table following it. 1582 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr) 1583 .addReg(ARM::PC) 1584 .addReg(MI->getOperand(0).getReg()) 1585 // Add predicate operands. 1586 .addImm(ARMCC::AL) 1587 .addReg(0)); 1588 1589 // Output the data for the jump table itself 1590 EmitJump2Table(MI); 1591 return; 1592 } 1593 case ARM::t2TBB_JT: { 1594 // Lower and emit the instruction itself, then the jump table following it. 1595 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBB) 1596 .addReg(ARM::PC) 1597 .addReg(MI->getOperand(0).getReg()) 1598 // Add predicate operands. 1599 .addImm(ARMCC::AL) 1600 .addReg(0)); 1601 1602 // Output the data for the jump table itself 1603 EmitJump2Table(MI); 1604 // Make sure the next instruction is 2-byte aligned. 1605 EmitAlignment(1); 1606 return; 1607 } 1608 case ARM::t2TBH_JT: { 1609 // Lower and emit the instruction itself, then the jump table following it. 1610 OutStreamer.EmitInstruction(MCInstBuilder(ARM::t2TBH) 1611 .addReg(ARM::PC) 1612 .addReg(MI->getOperand(0).getReg()) 1613 // Add predicate operands. 1614 .addImm(ARMCC::AL) 1615 .addReg(0)); 1616 1617 // Output the data for the jump table itself 1618 EmitJump2Table(MI); 1619 return; 1620 } 1621 case ARM::tBR_JTr: 1622 case ARM::BR_JTr: { 1623 // Lower and emit the instruction itself, then the jump table following it. 1624 // mov pc, target 1625 MCInst TmpInst; 1626 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ? 1627 ARM::MOVr : ARM::tMOVr; 1628 TmpInst.setOpcode(Opc); 1629 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC)); 1630 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 1631 // Add predicate operands. 1632 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL)); 1633 TmpInst.addOperand(MCOperand::CreateReg(0)); 1634 // Add 's' bit operand (always reg0 for this) 1635 if (Opc == ARM::MOVr) 1636 TmpInst.addOperand(MCOperand::CreateReg(0)); 1637 OutStreamer.EmitInstruction(TmpInst); 1638 1639 // Make sure the Thumb jump table is 4-byte aligned. 1640 if (Opc == ARM::tMOVr) 1641 EmitAlignment(2); 1642 1643 // Output the data for the jump table itself 1644 EmitJumpTable(MI); 1645 return; 1646 } 1647 case ARM::BR_JTm: { 1648 // Lower and emit the instruction itself, then the jump table following it. 1649 // ldr pc, target 1650 MCInst TmpInst; 1651 if (MI->getOperand(1).getReg() == 0) { 1652 // literal offset 1653 TmpInst.setOpcode(ARM::LDRi12); 1654 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC)); 1655 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 1656 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm())); 1657 } else { 1658 TmpInst.setOpcode(ARM::LDRrs); 1659 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC)); 1660 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 1661 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg())); 1662 TmpInst.addOperand(MCOperand::CreateImm(0)); 1663 } 1664 // Add predicate operands. 1665 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL)); 1666 TmpInst.addOperand(MCOperand::CreateReg(0)); 1667 OutStreamer.EmitInstruction(TmpInst); 1668 1669 // Output the data for the jump table itself 1670 EmitJumpTable(MI); 1671 return; 1672 } 1673 case ARM::BR_JTadd: { 1674 // Lower and emit the instruction itself, then the jump table following it. 1675 // add pc, target, idx 1676 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDrr) 1677 .addReg(ARM::PC) 1678 .addReg(MI->getOperand(0).getReg()) 1679 .addReg(MI->getOperand(1).getReg()) 1680 // Add predicate operands. 1681 .addImm(ARMCC::AL) 1682 .addReg(0) 1683 // Add 's' bit operand (always reg0 for this) 1684 .addReg(0)); 1685 1686 // Output the data for the jump table itself 1687 EmitJumpTable(MI); 1688 return; 1689 } 1690 case ARM::TRAP: { 1691 // Non-Darwin binutils don't yet support the "trap" mnemonic. 1692 // FIXME: Remove this special case when they do. 1693 if (!Subtarget->isTargetDarwin()) { 1694 //.long 0xe7ffdefe @ trap 1695 uint32_t Val = 0xe7ffdefeUL; 1696 OutStreamer.AddComment("trap"); 1697 OutStreamer.EmitIntValue(Val, 4); 1698 return; 1699 } 1700 break; 1701 } 1702 case ARM::TRAPNaCl: { 1703 //.long 0xe7fedef0 @ trap 1704 uint32_t Val = 0xe7fedef0UL; 1705 OutStreamer.AddComment("trap"); 1706 OutStreamer.EmitIntValue(Val, 4); 1707 return; 1708 } 1709 case ARM::tTRAP: { 1710 // Non-Darwin binutils don't yet support the "trap" mnemonic. 1711 // FIXME: Remove this special case when they do. 1712 if (!Subtarget->isTargetDarwin()) { 1713 //.short 57086 @ trap 1714 uint16_t Val = 0xdefe; 1715 OutStreamer.AddComment("trap"); 1716 OutStreamer.EmitIntValue(Val, 2); 1717 return; 1718 } 1719 break; 1720 } 1721 case ARM::t2Int_eh_sjlj_setjmp: 1722 case ARM::t2Int_eh_sjlj_setjmp_nofp: 1723 case ARM::tInt_eh_sjlj_setjmp: { 1724 // Two incoming args: GPR:$src, GPR:$val 1725 // mov $val, pc 1726 // adds $val, #7 1727 // str $val, [$src, #4] 1728 // movs r0, #0 1729 // b 1f 1730 // movs r0, #1 1731 // 1: 1732 unsigned SrcReg = MI->getOperand(0).getReg(); 1733 unsigned ValReg = MI->getOperand(1).getReg(); 1734 MCSymbol *Label = GetARMSJLJEHLabel(); 1735 OutStreamer.AddComment("eh_setjmp begin"); 1736 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr) 1737 .addReg(ValReg) 1738 .addReg(ARM::PC) 1739 // Predicate. 1740 .addImm(ARMCC::AL) 1741 .addReg(0)); 1742 1743 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tADDi3) 1744 .addReg(ValReg) 1745 // 's' bit operand 1746 .addReg(ARM::CPSR) 1747 .addReg(ValReg) 1748 .addImm(7) 1749 // Predicate. 1750 .addImm(ARMCC::AL) 1751 .addReg(0)); 1752 1753 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tSTRi) 1754 .addReg(ValReg) 1755 .addReg(SrcReg) 1756 // The offset immediate is #4. The operand value is scaled by 4 for the 1757 // tSTR instruction. 1758 .addImm(1) 1759 // Predicate. 1760 .addImm(ARMCC::AL) 1761 .addReg(0)); 1762 1763 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8) 1764 .addReg(ARM::R0) 1765 .addReg(ARM::CPSR) 1766 .addImm(0) 1767 // Predicate. 1768 .addImm(ARMCC::AL) 1769 .addReg(0)); 1770 1771 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext); 1772 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tB) 1773 .addExpr(SymbolExpr) 1774 .addImm(ARMCC::AL) 1775 .addReg(0)); 1776 1777 OutStreamer.AddComment("eh_setjmp end"); 1778 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVi8) 1779 .addReg(ARM::R0) 1780 .addReg(ARM::CPSR) 1781 .addImm(1) 1782 // Predicate. 1783 .addImm(ARMCC::AL) 1784 .addReg(0)); 1785 1786 OutStreamer.EmitLabel(Label); 1787 return; 1788 } 1789 1790 case ARM::Int_eh_sjlj_setjmp_nofp: 1791 case ARM::Int_eh_sjlj_setjmp: { 1792 // Two incoming args: GPR:$src, GPR:$val 1793 // add $val, pc, #8 1794 // str $val, [$src, #+4] 1795 // mov r0, #0 1796 // add pc, pc, #0 1797 // mov r0, #1 1798 unsigned SrcReg = MI->getOperand(0).getReg(); 1799 unsigned ValReg = MI->getOperand(1).getReg(); 1800 1801 OutStreamer.AddComment("eh_setjmp begin"); 1802 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri) 1803 .addReg(ValReg) 1804 .addReg(ARM::PC) 1805 .addImm(8) 1806 // Predicate. 1807 .addImm(ARMCC::AL) 1808 .addReg(0) 1809 // 's' bit operand (always reg0 for this). 1810 .addReg(0)); 1811 1812 OutStreamer.EmitInstruction(MCInstBuilder(ARM::STRi12) 1813 .addReg(ValReg) 1814 .addReg(SrcReg) 1815 .addImm(4) 1816 // Predicate. 1817 .addImm(ARMCC::AL) 1818 .addReg(0)); 1819 1820 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi) 1821 .addReg(ARM::R0) 1822 .addImm(0) 1823 // Predicate. 1824 .addImm(ARMCC::AL) 1825 .addReg(0) 1826 // 's' bit operand (always reg0 for this). 1827 .addReg(0)); 1828 1829 OutStreamer.EmitInstruction(MCInstBuilder(ARM::ADDri) 1830 .addReg(ARM::PC) 1831 .addReg(ARM::PC) 1832 .addImm(0) 1833 // Predicate. 1834 .addImm(ARMCC::AL) 1835 .addReg(0) 1836 // 's' bit operand (always reg0 for this). 1837 .addReg(0)); 1838 1839 OutStreamer.AddComment("eh_setjmp end"); 1840 OutStreamer.EmitInstruction(MCInstBuilder(ARM::MOVi) 1841 .addReg(ARM::R0) 1842 .addImm(1) 1843 // Predicate. 1844 .addImm(ARMCC::AL) 1845 .addReg(0) 1846 // 's' bit operand (always reg0 for this). 1847 .addReg(0)); 1848 return; 1849 } 1850 case ARM::Int_eh_sjlj_longjmp: { 1851 // ldr sp, [$src, #8] 1852 // ldr $scratch, [$src, #4] 1853 // ldr r7, [$src] 1854 // bx $scratch 1855 unsigned SrcReg = MI->getOperand(0).getReg(); 1856 unsigned ScratchReg = MI->getOperand(1).getReg(); 1857 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12) 1858 .addReg(ARM::SP) 1859 .addReg(SrcReg) 1860 .addImm(8) 1861 // Predicate. 1862 .addImm(ARMCC::AL) 1863 .addReg(0)); 1864 1865 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12) 1866 .addReg(ScratchReg) 1867 .addReg(SrcReg) 1868 .addImm(4) 1869 // Predicate. 1870 .addImm(ARMCC::AL) 1871 .addReg(0)); 1872 1873 OutStreamer.EmitInstruction(MCInstBuilder(ARM::LDRi12) 1874 .addReg(ARM::R7) 1875 .addReg(SrcReg) 1876 .addImm(0) 1877 // Predicate. 1878 .addImm(ARMCC::AL) 1879 .addReg(0)); 1880 1881 OutStreamer.EmitInstruction(MCInstBuilder(ARM::BX) 1882 .addReg(ScratchReg) 1883 // Predicate. 1884 .addImm(ARMCC::AL) 1885 .addReg(0)); 1886 return; 1887 } 1888 case ARM::tInt_eh_sjlj_longjmp: { 1889 // ldr $scratch, [$src, #8] 1890 // mov sp, $scratch 1891 // ldr $scratch, [$src, #4] 1892 // ldr r7, [$src] 1893 // bx $scratch 1894 unsigned SrcReg = MI->getOperand(0).getReg(); 1895 unsigned ScratchReg = MI->getOperand(1).getReg(); 1896 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi) 1897 .addReg(ScratchReg) 1898 .addReg(SrcReg) 1899 // The offset immediate is #8. The operand value is scaled by 4 for the 1900 // tLDR instruction. 1901 .addImm(2) 1902 // Predicate. 1903 .addImm(ARMCC::AL) 1904 .addReg(0)); 1905 1906 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tMOVr) 1907 .addReg(ARM::SP) 1908 .addReg(ScratchReg) 1909 // Predicate. 1910 .addImm(ARMCC::AL) 1911 .addReg(0)); 1912 1913 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi) 1914 .addReg(ScratchReg) 1915 .addReg(SrcReg) 1916 .addImm(1) 1917 // Predicate. 1918 .addImm(ARMCC::AL) 1919 .addReg(0)); 1920 1921 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tLDRi) 1922 .addReg(ARM::R7) 1923 .addReg(SrcReg) 1924 .addImm(0) 1925 // Predicate. 1926 .addImm(ARMCC::AL) 1927 .addReg(0)); 1928 1929 OutStreamer.EmitInstruction(MCInstBuilder(ARM::tBX) 1930 .addReg(ScratchReg) 1931 // Predicate. 1932 .addImm(ARMCC::AL) 1933 .addReg(0)); 1934 return; 1935 } 1936 } 1937 1938 MCInst TmpInst; 1939 LowerARMMachineInstrToMCInst(MI, TmpInst, *this); 1940 1941 OutStreamer.EmitInstruction(TmpInst); 1942 } 1943 1944 //===----------------------------------------------------------------------===// 1945 // Target Registry Stuff 1946 //===----------------------------------------------------------------------===// 1947 1948 // Force static initialization. 1949 extern "C" void LLVMInitializeARMAsmPrinter() { 1950 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget); 1951 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget); 1952 } 1953