1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains a printer that converts from our internal representation 10 // of machine-dependent LLVM code to X86 machine code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "X86AsmPrinter.h" 15 #include "MCTargetDesc/X86ATTInstPrinter.h" 16 #include "MCTargetDesc/X86BaseInfo.h" 17 #include "MCTargetDesc/X86TargetStreamer.h" 18 #include "TargetInfo/X86TargetInfo.h" 19 #include "X86InstrInfo.h" 20 #include "X86MachineFunctionInfo.h" 21 #include "llvm/BinaryFormat/COFF.h" 22 #include "llvm/BinaryFormat/ELF.h" 23 #include "llvm/CodeGen/MachineConstantPool.h" 24 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 26 #include "llvm/IR/DerivedTypes.h" 27 #include "llvm/IR/InlineAsm.h" 28 #include "llvm/IR/Mangler.h" 29 #include "llvm/IR/Module.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/MC/MCCodeEmitter.h" 32 #include "llvm/MC/MCContext.h" 33 #include "llvm/MC/MCExpr.h" 34 #include "llvm/MC/MCSectionCOFF.h" 35 #include "llvm/MC/MCSectionELF.h" 36 #include "llvm/MC/MCSectionMachO.h" 37 #include "llvm/MC/MCStreamer.h" 38 #include "llvm/MC/MCSymbol.h" 39 #include "llvm/Support/Debug.h" 40 #include "llvm/Support/ErrorHandling.h" 41 #include "llvm/Support/MachineValueType.h" 42 #include "llvm/Support/TargetRegistry.h" 43 using namespace llvm; 44 45 X86AsmPrinter::X86AsmPrinter(TargetMachine &TM, 46 std::unique_ptr<MCStreamer> Streamer) 47 : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this) {} 48 49 //===----------------------------------------------------------------------===// 50 // Primitive Helper Functions. 51 //===----------------------------------------------------------------------===// 52 53 /// runOnMachineFunction - Emit the function body. 54 /// 55 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 56 Subtarget = &MF.getSubtarget<X86Subtarget>(); 57 58 SMShadowTracker.startFunction(MF); 59 CodeEmitter.reset(TM.getTarget().createMCCodeEmitter( 60 *Subtarget->getInstrInfo(), *Subtarget->getRegisterInfo(), 61 MF.getContext())); 62 63 EmitFPOData = 64 Subtarget->isTargetWin32() && MF.getMMI().getModule()->getCodeViewFlag(); 65 66 SetupMachineFunction(MF); 67 68 if (Subtarget->isTargetCOFF()) { 69 bool Local = MF.getFunction().hasLocalLinkage(); 70 OutStreamer->BeginCOFFSymbolDef(CurrentFnSym); 71 OutStreamer->EmitCOFFSymbolStorageClass( 72 Local ? COFF::IMAGE_SYM_CLASS_STATIC : COFF::IMAGE_SYM_CLASS_EXTERNAL); 73 OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION 74 << COFF::SCT_COMPLEX_TYPE_SHIFT); 75 OutStreamer->EndCOFFSymbolDef(); 76 } 77 78 // Emit the rest of the function body. 79 EmitFunctionBody(); 80 81 // Emit the XRay table for this function. 82 emitXRayTable(); 83 84 EmitFPOData = false; 85 86 // We didn't modify anything. 87 return false; 88 } 89 90 void X86AsmPrinter::EmitFunctionBodyStart() { 91 if (EmitFPOData) { 92 if (auto *XTS = 93 static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer())) 94 XTS->emitFPOProc( 95 CurrentFnSym, 96 MF->getInfo<X86MachineFunctionInfo>()->getArgumentStackSize()); 97 } 98 } 99 100 void X86AsmPrinter::EmitFunctionBodyEnd() { 101 if (EmitFPOData) { 102 if (auto *XTS = 103 static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer())) 104 XTS->emitFPOEndProc(); 105 } 106 } 107 108 /// PrintSymbolOperand - Print a raw symbol reference operand. This handles 109 /// jump tables, constant pools, global address and external symbols, all of 110 /// which print to a label with various suffixes for relocation types etc. 111 void X86AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, 112 raw_ostream &O) { 113 switch (MO.getType()) { 114 default: llvm_unreachable("unknown symbol type!"); 115 case MachineOperand::MO_ConstantPoolIndex: 116 GetCPISymbol(MO.getIndex())->print(O, MAI); 117 printOffset(MO.getOffset(), O); 118 break; 119 case MachineOperand::MO_GlobalAddress: { 120 const GlobalValue *GV = MO.getGlobal(); 121 122 MCSymbol *GVSym; 123 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || 124 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) 125 GVSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 126 else 127 GVSym = getSymbol(GV); 128 129 // Handle dllimport linkage. 130 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) 131 GVSym = OutContext.getOrCreateSymbol(Twine("__imp_") + GVSym->getName()); 132 else if (MO.getTargetFlags() == X86II::MO_COFFSTUB) 133 GVSym = 134 OutContext.getOrCreateSymbol(Twine(".refptr.") + GVSym->getName()); 135 136 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || 137 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) { 138 MCSymbol *Sym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 139 MachineModuleInfoImpl::StubValueTy &StubSym = 140 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym); 141 if (!StubSym.getPointer()) 142 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV), 143 !GV->hasInternalLinkage()); 144 } 145 146 // If the name begins with a dollar-sign, enclose it in parens. We do this 147 // to avoid having it look like an integer immediate to the assembler. 148 if (GVSym->getName()[0] != '$') 149 GVSym->print(O, MAI); 150 else { 151 O << '('; 152 GVSym->print(O, MAI); 153 O << ')'; 154 } 155 printOffset(MO.getOffset(), O); 156 break; 157 } 158 } 159 160 switch (MO.getTargetFlags()) { 161 default: 162 llvm_unreachable("Unknown target flag on GV operand"); 163 case X86II::MO_NO_FLAG: // No flag. 164 break; 165 case X86II::MO_DARWIN_NONLAZY: 166 case X86II::MO_DLLIMPORT: 167 case X86II::MO_COFFSTUB: 168 // These affect the name of the symbol, not any suffix. 169 break; 170 case X86II::MO_GOT_ABSOLUTE_ADDRESS: 171 O << " + [.-"; 172 MF->getPICBaseSymbol()->print(O, MAI); 173 O << ']'; 174 break; 175 case X86II::MO_PIC_BASE_OFFSET: 176 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: 177 O << '-'; 178 MF->getPICBaseSymbol()->print(O, MAI); 179 break; 180 case X86II::MO_TLSGD: O << "@TLSGD"; break; 181 case X86II::MO_TLSLD: O << "@TLSLD"; break; 182 case X86II::MO_TLSLDM: O << "@TLSLDM"; break; 183 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break; 184 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break; 185 case X86II::MO_TPOFF: O << "@TPOFF"; break; 186 case X86II::MO_DTPOFF: O << "@DTPOFF"; break; 187 case X86II::MO_NTPOFF: O << "@NTPOFF"; break; 188 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break; 189 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break; 190 case X86II::MO_GOT: O << "@GOT"; break; 191 case X86II::MO_GOTOFF: O << "@GOTOFF"; break; 192 case X86II::MO_PLT: O << "@PLT"; break; 193 case X86II::MO_TLVP: O << "@TLVP"; break; 194 case X86II::MO_TLVP_PIC_BASE: 195 O << "@TLVP" << '-'; 196 MF->getPICBaseSymbol()->print(O, MAI); 197 break; 198 case X86II::MO_SECREL: O << "@SECREL32"; break; 199 } 200 } 201 202 void X86AsmPrinter::PrintOperand(const MachineInstr *MI, unsigned OpNo, 203 raw_ostream &O) { 204 const MachineOperand &MO = MI->getOperand(OpNo); 205 const bool IsATT = MI->getInlineAsmDialect() == InlineAsm::AD_ATT; 206 switch (MO.getType()) { 207 default: llvm_unreachable("unknown operand type!"); 208 case MachineOperand::MO_Register: { 209 if (IsATT) 210 O << '%'; 211 O << X86ATTInstPrinter::getRegisterName(MO.getReg()); 212 return; 213 } 214 215 case MachineOperand::MO_Immediate: 216 if (IsATT) 217 O << '$'; 218 O << MO.getImm(); 219 return; 220 221 case MachineOperand::MO_ConstantPoolIndex: 222 case MachineOperand::MO_GlobalAddress: { 223 switch (MI->getInlineAsmDialect()) { 224 case InlineAsm::AD_ATT: 225 O << '$'; 226 break; 227 case InlineAsm::AD_Intel: 228 O << "offset "; 229 break; 230 } 231 PrintSymbolOperand(MO, O); 232 break; 233 } 234 case MachineOperand::MO_BlockAddress: { 235 MCSymbol *Sym = GetBlockAddressSymbol(MO.getBlockAddress()); 236 Sym->print(O, MAI); 237 break; 238 } 239 } 240 } 241 242 /// PrintModifiedOperand - Print subregisters based on supplied modifier, 243 /// deferring to PrintOperand() if no modifier was supplied or if operand is not 244 /// a register. 245 void X86AsmPrinter::PrintModifiedOperand(const MachineInstr *MI, unsigned OpNo, 246 raw_ostream &O, const char *Modifier) { 247 const MachineOperand &MO = MI->getOperand(OpNo); 248 if (!Modifier || MO.getType() != MachineOperand::MO_Register) 249 return PrintOperand(MI, OpNo, O); 250 if (MI->getInlineAsmDialect() == InlineAsm::AD_ATT) 251 O << '%'; 252 Register Reg = MO.getReg(); 253 if (strncmp(Modifier, "subreg", strlen("subreg")) == 0) { 254 unsigned Size = (strcmp(Modifier+6,"64") == 0) ? 64 : 255 (strcmp(Modifier+6,"32") == 0) ? 32 : 256 (strcmp(Modifier+6,"16") == 0) ? 16 : 8; 257 Reg = getX86SubSuperRegister(Reg, Size); 258 } 259 O << X86ATTInstPrinter::getRegisterName(Reg); 260 } 261 262 /// PrintPCRelImm - This is used to print an immediate value that ends up 263 /// being encoded as a pc-relative value. These print slightly differently, for 264 /// example, a $ is not emitted. 265 void X86AsmPrinter::PrintPCRelImm(const MachineInstr *MI, unsigned OpNo, 266 raw_ostream &O) { 267 const MachineOperand &MO = MI->getOperand(OpNo); 268 switch (MO.getType()) { 269 default: llvm_unreachable("Unknown pcrel immediate operand"); 270 case MachineOperand::MO_Register: 271 // pc-relativeness was handled when computing the value in the reg. 272 PrintOperand(MI, OpNo, O); 273 return; 274 case MachineOperand::MO_Immediate: 275 O << MO.getImm(); 276 return; 277 case MachineOperand::MO_GlobalAddress: 278 PrintSymbolOperand(MO, O); 279 return; 280 } 281 } 282 283 void X86AsmPrinter::PrintLeaMemReference(const MachineInstr *MI, unsigned OpNo, 284 raw_ostream &O, const char *Modifier) { 285 const MachineOperand &BaseReg = MI->getOperand(OpNo + X86::AddrBaseReg); 286 const MachineOperand &IndexReg = MI->getOperand(OpNo + X86::AddrIndexReg); 287 const MachineOperand &DispSpec = MI->getOperand(OpNo + X86::AddrDisp); 288 289 // If we really don't want to print out (rip), don't. 290 bool HasBaseReg = BaseReg.getReg() != 0; 291 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") && 292 BaseReg.getReg() == X86::RIP) 293 HasBaseReg = false; 294 295 // HasParenPart - True if we will print out the () part of the mem ref. 296 bool HasParenPart = IndexReg.getReg() || HasBaseReg; 297 298 switch (DispSpec.getType()) { 299 default: 300 llvm_unreachable("unknown operand type!"); 301 case MachineOperand::MO_Immediate: { 302 int DispVal = DispSpec.getImm(); 303 if (DispVal || !HasParenPart) 304 O << DispVal; 305 break; 306 } 307 case MachineOperand::MO_GlobalAddress: 308 case MachineOperand::MO_ConstantPoolIndex: 309 PrintSymbolOperand(DispSpec, O); 310 break; 311 } 312 313 if (Modifier && strcmp(Modifier, "H") == 0) 314 O << "+8"; 315 316 if (HasParenPart) { 317 assert(IndexReg.getReg() != X86::ESP && 318 "X86 doesn't allow scaling by ESP"); 319 320 O << '('; 321 if (HasBaseReg) 322 PrintModifiedOperand(MI, OpNo + X86::AddrBaseReg, O, Modifier); 323 324 if (IndexReg.getReg()) { 325 O << ','; 326 PrintModifiedOperand(MI, OpNo + X86::AddrIndexReg, O, Modifier); 327 unsigned ScaleVal = MI->getOperand(OpNo + X86::AddrScaleAmt).getImm(); 328 if (ScaleVal != 1) 329 O << ',' << ScaleVal; 330 } 331 O << ')'; 332 } 333 } 334 335 void X86AsmPrinter::PrintMemReference(const MachineInstr *MI, unsigned OpNo, 336 raw_ostream &O, const char *Modifier) { 337 assert(isMem(*MI, OpNo) && "Invalid memory reference!"); 338 const MachineOperand &Segment = MI->getOperand(OpNo + X86::AddrSegmentReg); 339 if (Segment.getReg()) { 340 PrintModifiedOperand(MI, OpNo + X86::AddrSegmentReg, O, Modifier); 341 O << ':'; 342 } 343 PrintLeaMemReference(MI, OpNo, O, Modifier); 344 } 345 346 347 void X86AsmPrinter::PrintIntelMemReference(const MachineInstr *MI, 348 unsigned OpNo, raw_ostream &O, 349 const char *Modifier) { 350 const MachineOperand &BaseReg = MI->getOperand(OpNo + X86::AddrBaseReg); 351 unsigned ScaleVal = MI->getOperand(OpNo + X86::AddrScaleAmt).getImm(); 352 const MachineOperand &IndexReg = MI->getOperand(OpNo + X86::AddrIndexReg); 353 const MachineOperand &DispSpec = MI->getOperand(OpNo + X86::AddrDisp); 354 const MachineOperand &SegReg = MI->getOperand(OpNo + X86::AddrSegmentReg); 355 356 // If we really don't want to print out (rip), don't. 357 bool HasBaseReg = BaseReg.getReg() != 0; 358 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") && 359 BaseReg.getReg() == X86::RIP) 360 HasBaseReg = false; 361 362 // If this has a segment register, print it. 363 if (SegReg.getReg()) { 364 PrintOperand(MI, OpNo + X86::AddrSegmentReg, O); 365 O << ':'; 366 } 367 368 O << '['; 369 370 bool NeedPlus = false; 371 if (HasBaseReg) { 372 PrintOperand(MI, OpNo + X86::AddrBaseReg, O); 373 NeedPlus = true; 374 } 375 376 if (IndexReg.getReg()) { 377 if (NeedPlus) O << " + "; 378 if (ScaleVal != 1) 379 O << ScaleVal << '*'; 380 PrintOperand(MI, OpNo + X86::AddrIndexReg, O); 381 NeedPlus = true; 382 } 383 384 if (!DispSpec.isImm()) { 385 if (NeedPlus) O << " + "; 386 PrintOperand(MI, OpNo + X86::AddrDisp, O); 387 } else { 388 int64_t DispVal = DispSpec.getImm(); 389 if (DispVal || (!IndexReg.getReg() && !HasBaseReg)) { 390 if (NeedPlus) { 391 if (DispVal > 0) 392 O << " + "; 393 else { 394 O << " - "; 395 DispVal = -DispVal; 396 } 397 } 398 O << DispVal; 399 } 400 } 401 O << ']'; 402 } 403 404 static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO, 405 char Mode, raw_ostream &O) { 406 Register Reg = MO.getReg(); 407 bool EmitPercent = MO.getParent()->getInlineAsmDialect() == InlineAsm::AD_ATT; 408 409 if (!X86::GR8RegClass.contains(Reg) && 410 !X86::GR16RegClass.contains(Reg) && 411 !X86::GR32RegClass.contains(Reg) && 412 !X86::GR64RegClass.contains(Reg)) 413 return true; 414 415 switch (Mode) { 416 default: return true; // Unknown mode. 417 case 'b': // Print QImode register 418 Reg = getX86SubSuperRegister(Reg, 8); 419 break; 420 case 'h': // Print QImode high register 421 Reg = getX86SubSuperRegister(Reg, 8, true); 422 break; 423 case 'w': // Print HImode register 424 Reg = getX86SubSuperRegister(Reg, 16); 425 break; 426 case 'k': // Print SImode register 427 Reg = getX86SubSuperRegister(Reg, 32); 428 break; 429 case 'V': 430 EmitPercent = false; 431 LLVM_FALLTHROUGH; 432 case 'q': 433 // Print 64-bit register names if 64-bit integer registers are available. 434 // Otherwise, print 32-bit register names. 435 Reg = getX86SubSuperRegister(Reg, P.getSubtarget().is64Bit() ? 64 : 32); 436 break; 437 } 438 439 if (EmitPercent) 440 O << '%'; 441 442 O << X86ATTInstPrinter::getRegisterName(Reg); 443 return false; 444 } 445 446 static bool printAsmVRegister(X86AsmPrinter &P, const MachineOperand &MO, 447 char Mode, raw_ostream &O) { 448 unsigned Reg = MO.getReg(); 449 bool EmitPercent = MO.getParent()->getInlineAsmDialect() == InlineAsm::AD_ATT; 450 451 unsigned Index; 452 if (X86::VR128XRegClass.contains(Reg)) 453 Index = Reg - X86::XMM0; 454 else if (X86::VR256XRegClass.contains(Reg)) 455 Index = Reg - X86::YMM0; 456 else if (X86::VR512RegClass.contains(Reg)) 457 Index = Reg - X86::ZMM0; 458 else 459 return true; 460 461 switch (Mode) { 462 default: // Unknown mode. 463 return true; 464 case 'x': // Print V4SFmode register 465 Reg = X86::XMM0 + Index; 466 break; 467 case 't': // Print V8SFmode register 468 Reg = X86::YMM0 + Index; 469 break; 470 case 'g': // Print V16SFmode register 471 Reg = X86::ZMM0 + Index; 472 break; 473 } 474 475 if (EmitPercent) 476 O << '%'; 477 478 O << X86ATTInstPrinter::getRegisterName(Reg); 479 return false; 480 } 481 482 /// PrintAsmOperand - Print out an operand for an inline asm expression. 483 /// 484 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 485 const char *ExtraCode, raw_ostream &O) { 486 // Does this asm operand have a single letter operand modifier? 487 if (ExtraCode && ExtraCode[0]) { 488 if (ExtraCode[1] != 0) return true; // Unknown modifier. 489 490 const MachineOperand &MO = MI->getOperand(OpNo); 491 492 switch (ExtraCode[0]) { 493 default: 494 // See if this is a generic print operand 495 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O); 496 case 'a': // This is an address. Currently only 'i' and 'r' are expected. 497 switch (MO.getType()) { 498 default: 499 return true; 500 case MachineOperand::MO_Immediate: 501 O << MO.getImm(); 502 return false; 503 case MachineOperand::MO_ConstantPoolIndex: 504 case MachineOperand::MO_JumpTableIndex: 505 case MachineOperand::MO_ExternalSymbol: 506 llvm_unreachable("unexpected operand type!"); 507 case MachineOperand::MO_GlobalAddress: 508 PrintSymbolOperand(MO, O); 509 if (Subtarget->isPICStyleRIPRel()) 510 O << "(%rip)"; 511 return false; 512 case MachineOperand::MO_Register: 513 O << '('; 514 PrintOperand(MI, OpNo, O); 515 O << ')'; 516 return false; 517 } 518 519 case 'c': // Don't print "$" before a global var name or constant. 520 switch (MO.getType()) { 521 default: 522 PrintOperand(MI, OpNo, O); 523 break; 524 case MachineOperand::MO_Immediate: 525 O << MO.getImm(); 526 break; 527 case MachineOperand::MO_ConstantPoolIndex: 528 case MachineOperand::MO_JumpTableIndex: 529 case MachineOperand::MO_ExternalSymbol: 530 llvm_unreachable("unexpected operand type!"); 531 case MachineOperand::MO_GlobalAddress: 532 PrintSymbolOperand(MO, O); 533 break; 534 } 535 return false; 536 537 case 'A': // Print '*' before a register (it must be a register) 538 if (MO.isReg()) { 539 O << '*'; 540 PrintOperand(MI, OpNo, O); 541 return false; 542 } 543 return true; 544 545 case 'b': // Print QImode register 546 case 'h': // Print QImode high register 547 case 'w': // Print HImode register 548 case 'k': // Print SImode register 549 case 'q': // Print DImode register 550 case 'V': // Print native register without '%' 551 if (MO.isReg()) 552 return printAsmMRegister(*this, MO, ExtraCode[0], O); 553 PrintOperand(MI, OpNo, O); 554 return false; 555 556 case 'x': // Print V4SFmode register 557 case 't': // Print V8SFmode register 558 case 'g': // Print V16SFmode register 559 if (MO.isReg()) 560 return printAsmVRegister(*this, MO, ExtraCode[0], O); 561 PrintOperand(MI, OpNo, O); 562 return false; 563 564 case 'P': // This is the operand of a call, treat specially. 565 PrintPCRelImm(MI, OpNo, O); 566 return false; 567 568 case 'n': // Negate the immediate or print a '-' before the operand. 569 // Note: this is a temporary solution. It should be handled target 570 // independently as part of the 'MC' work. 571 if (MO.isImm()) { 572 O << -MO.getImm(); 573 return false; 574 } 575 O << '-'; 576 } 577 } 578 579 PrintOperand(MI, OpNo, O); 580 return false; 581 } 582 583 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 584 const char *ExtraCode, 585 raw_ostream &O) { 586 if (ExtraCode && ExtraCode[0]) { 587 if (ExtraCode[1] != 0) return true; // Unknown modifier. 588 589 switch (ExtraCode[0]) { 590 default: return true; // Unknown modifier. 591 case 'b': // Print QImode register 592 case 'h': // Print QImode high register 593 case 'w': // Print HImode register 594 case 'k': // Print SImode register 595 case 'q': // Print SImode register 596 // These only apply to registers, ignore on mem. 597 break; 598 case 'H': 599 if (MI->getInlineAsmDialect() == InlineAsm::AD_Intel) { 600 return true; // Unsupported modifier in Intel inline assembly. 601 } else { 602 PrintMemReference(MI, OpNo, O, "H"); 603 } 604 return false; 605 case 'P': // Don't print @PLT, but do print as memory. 606 if (MI->getInlineAsmDialect() == InlineAsm::AD_Intel) { 607 PrintIntelMemReference(MI, OpNo, O, "no-rip"); 608 } else { 609 PrintMemReference(MI, OpNo, O, "no-rip"); 610 } 611 return false; 612 } 613 } 614 if (MI->getInlineAsmDialect() == InlineAsm::AD_Intel) { 615 PrintIntelMemReference(MI, OpNo, O, nullptr); 616 } else { 617 PrintMemReference(MI, OpNo, O, nullptr); 618 } 619 return false; 620 } 621 622 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) { 623 const Triple &TT = TM.getTargetTriple(); 624 625 if (TT.isOSBinFormatELF()) { 626 // Assemble feature flags that may require creation of a note section. 627 unsigned FeatureFlagsAnd = 0; 628 if (M.getModuleFlag("cf-protection-branch")) 629 FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT; 630 if (M.getModuleFlag("cf-protection-return")) 631 FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK; 632 633 if (FeatureFlagsAnd) { 634 // Emit a .note.gnu.property section with the flags. 635 if (!TT.isArch32Bit() && !TT.isArch64Bit()) 636 llvm_unreachable("CFProtection used on invalid architecture!"); 637 MCSection *Cur = OutStreamer->getCurrentSectionOnly(); 638 MCSection *Nt = MMI->getContext().getELFSection( 639 ".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC); 640 OutStreamer->SwitchSection(Nt); 641 642 // Emitting note header. 643 int WordSize = TT.isArch64Bit() ? 8 : 4; 644 EmitAlignment(WordSize == 4 ? Align(4) : Align(8)); 645 OutStreamer->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0" 646 OutStreamer->EmitIntValue(8 + WordSize, 4 /*size*/); // Elf_Prop size 647 OutStreamer->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4 /*size*/); 648 OutStreamer->EmitBytes(StringRef("GNU", 4)); // note name 649 650 // Emitting an Elf_Prop for the CET properties. 651 OutStreamer->EmitIntValue(ELF::GNU_PROPERTY_X86_FEATURE_1_AND, 4); 652 OutStreamer->EmitIntValue(4, 4); // data size 653 OutStreamer->EmitIntValue(FeatureFlagsAnd, 4); // data 654 EmitAlignment(WordSize == 4 ? Align(4) : Align(8)); // padding 655 656 OutStreamer->endSection(Nt); 657 OutStreamer->SwitchSection(Cur); 658 } 659 } 660 661 if (TT.isOSBinFormatMachO()) 662 OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); 663 664 if (TT.isOSBinFormatCOFF()) { 665 // Emit an absolute @feat.00 symbol. This appears to be some kind of 666 // compiler features bitfield read by link.exe. 667 MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00")); 668 OutStreamer->BeginCOFFSymbolDef(S); 669 OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC); 670 OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL); 671 OutStreamer->EndCOFFSymbolDef(); 672 int64_t Feat00Flags = 0; 673 674 if (TT.getArch() == Triple::x86) { 675 // According to the PE-COFF spec, the LSB of this value marks the object 676 // for "registered SEH". This means that all SEH handler entry points 677 // must be registered in .sxdata. Use of any unregistered handlers will 678 // cause the process to terminate immediately. LLVM does not know how to 679 // register any SEH handlers, so its object files should be safe. 680 Feat00Flags |= 1; 681 } 682 683 if (M.getModuleFlag("cfguard")) 684 Feat00Flags |= 0x800; // Object is CFG-aware. 685 686 OutStreamer->EmitSymbolAttribute(S, MCSA_Global); 687 OutStreamer->EmitAssignment( 688 S, MCConstantExpr::create(Feat00Flags, MMI->getContext())); 689 } 690 OutStreamer->EmitSyntaxDirective(); 691 692 // If this is not inline asm and we're in 16-bit 693 // mode prefix assembly with .code16. 694 bool is16 = TT.getEnvironment() == Triple::CODE16; 695 if (M.getModuleInlineAsm().empty() && is16) 696 OutStreamer->EmitAssemblerFlag(MCAF_Code16); 697 } 698 699 static void 700 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel, 701 MachineModuleInfoImpl::StubValueTy &MCSym) { 702 // L_foo$stub: 703 OutStreamer.EmitLabel(StubLabel); 704 // .indirect_symbol _foo 705 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol); 706 707 if (MCSym.getInt()) 708 // External to current translation unit. 709 OutStreamer.EmitIntValue(0, 4/*size*/); 710 else 711 // Internal to current translation unit. 712 // 713 // When we place the LSDA into the TEXT section, the type info 714 // pointers need to be indirect and pc-rel. We accomplish this by 715 // using NLPs; however, sometimes the types are local to the file. 716 // We need to fill in the value for the NLP in those cases. 717 OutStreamer.EmitValue( 718 MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()), 719 4 /*size*/); 720 } 721 722 static void emitNonLazyStubs(MachineModuleInfo *MMI, MCStreamer &OutStreamer) { 723 724 MachineModuleInfoMachO &MMIMacho = 725 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 726 727 // Output stubs for dynamically-linked functions. 728 MachineModuleInfoMachO::SymbolListTy Stubs; 729 730 // Output stubs for external and common global variables. 731 Stubs = MMIMacho.GetGVStubList(); 732 if (!Stubs.empty()) { 733 OutStreamer.SwitchSection(MMI->getContext().getMachOSection( 734 "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS, 735 SectionKind::getMetadata())); 736 737 for (auto &Stub : Stubs) 738 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second); 739 740 Stubs.clear(); 741 OutStreamer.AddBlankLine(); 742 } 743 } 744 745 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) { 746 const Triple &TT = TM.getTargetTriple(); 747 748 if (TT.isOSBinFormatMachO()) { 749 // Mach-O uses non-lazy symbol stubs to encode per-TU information into 750 // global table for symbol lookup. 751 emitNonLazyStubs(MMI, *OutStreamer); 752 753 // Emit stack and fault map information. 754 emitStackMaps(SM); 755 FM.serializeToFaultMapSection(); 756 757 // This flag tells the linker that no global symbols contain code that fall 758 // through to other global symbols (e.g. an implementation of multiple entry 759 // points). If this doesn't occur, the linker can safely perform dead code 760 // stripping. Since LLVM never generates code that does this, it is always 761 // safe to set. 762 OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 763 } else if (TT.isOSBinFormatCOFF()) { 764 if (MMI->usesMSVCFloatingPoint()) { 765 // In Windows' libcmt.lib, there is a file which is linked in only if the 766 // symbol _fltused is referenced. Linking this in causes some 767 // side-effects: 768 // 769 // 1. For x86-32, it will set the x87 rounding mode to 53-bit instead of 770 // 64-bit mantissas at program start. 771 // 772 // 2. It links in support routines for floating-point in scanf and printf. 773 // 774 // MSVC emits an undefined reference to _fltused when there are any 775 // floating point operations in the program (including calls). A program 776 // that only has: `scanf("%f", &global_float);` may fail to trigger this, 777 // but oh well...that's a documented issue. 778 StringRef SymbolName = 779 (TT.getArch() == Triple::x86) ? "__fltused" : "_fltused"; 780 MCSymbol *S = MMI->getContext().getOrCreateSymbol(SymbolName); 781 OutStreamer->EmitSymbolAttribute(S, MCSA_Global); 782 return; 783 } 784 emitStackMaps(SM); 785 } else if (TT.isOSBinFormatELF()) { 786 emitStackMaps(SM); 787 FM.serializeToFaultMapSection(); 788 } 789 } 790 791 //===----------------------------------------------------------------------===// 792 // Target Registry Stuff 793 //===----------------------------------------------------------------------===// 794 795 // Force static initialization. 796 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86AsmPrinter() { 797 RegisterAsmPrinter<X86AsmPrinter> X(getTheX86_32Target()); 798 RegisterAsmPrinter<X86AsmPrinter> Y(getTheX86_64Target()); 799 } 800