1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===// 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 X86 machine code. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "X86AsmPrinter.h" 16 #include "InstPrinter/X86ATTInstPrinter.h" 17 #include "MCTargetDesc/X86BaseInfo.h" 18 #include "MCTargetDesc/X86TargetStreamer.h" 19 #include "X86InstrInfo.h" 20 #include "X86MachineFunctionInfo.h" 21 #include "llvm/BinaryFormat/COFF.h" 22 #include "llvm/CodeGen/MachineConstantPool.h" 23 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 24 #include "llvm/CodeGen/MachineValueType.h" 25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 26 #include "llvm/IR/DebugInfo.h" 27 #include "llvm/IR/DerivedTypes.h" 28 #include "llvm/IR/Mangler.h" 29 #include "llvm/IR/Module.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/MC/MCAsmInfo.h" 32 #include "llvm/MC/MCCodeEmitter.h" 33 #include "llvm/MC/MCContext.h" 34 #include "llvm/MC/MCExpr.h" 35 #include "llvm/MC/MCSectionCOFF.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/TargetRegistry.h" 42 using namespace llvm; 43 44 X86AsmPrinter::X86AsmPrinter(TargetMachine &TM, 45 std::unique_ptr<MCStreamer> Streamer) 46 : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this) {} 47 48 //===----------------------------------------------------------------------===// 49 // Primitive Helper Functions. 50 //===----------------------------------------------------------------------===// 51 52 /// runOnMachineFunction - Emit the function body. 53 /// 54 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 55 Subtarget = &MF.getSubtarget<X86Subtarget>(); 56 57 SMShadowTracker.startFunction(MF); 58 CodeEmitter.reset(TM.getTarget().createMCCodeEmitter( 59 *Subtarget->getInstrInfo(), *Subtarget->getRegisterInfo(), 60 MF.getContext())); 61 62 EmitFPOData = 63 Subtarget->isTargetWin32() && MF.getMMI().getModule()->getCodeViewFlag(); 64 65 SetupMachineFunction(MF); 66 67 if (Subtarget->isTargetCOFF()) { 68 bool Local = MF.getFunction()->hasLocalLinkage(); 69 OutStreamer->BeginCOFFSymbolDef(CurrentFnSym); 70 OutStreamer->EmitCOFFSymbolStorageClass( 71 Local ? COFF::IMAGE_SYM_CLASS_STATIC : COFF::IMAGE_SYM_CLASS_EXTERNAL); 72 OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION 73 << COFF::SCT_COMPLEX_TYPE_SHIFT); 74 OutStreamer->EndCOFFSymbolDef(); 75 } 76 77 // Emit the rest of the function body. 78 EmitFunctionBody(); 79 80 // Emit the XRay table for this function. 81 emitXRayTable(); 82 83 EmitFPOData = false; 84 85 // We didn't modify anything. 86 return false; 87 } 88 89 void X86AsmPrinter::EmitFunctionBodyStart() { 90 if (EmitFPOData) { 91 X86TargetStreamer *XTS = 92 static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()); 93 unsigned ParamsSize = 94 MF->getInfo<X86MachineFunctionInfo>()->getArgumentStackSize(); 95 XTS->emitFPOProc(CurrentFnSym, ParamsSize); 96 } 97 } 98 99 void X86AsmPrinter::EmitFunctionBodyEnd() { 100 if (EmitFPOData) { 101 X86TargetStreamer *XTS = 102 static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()); 103 XTS->emitFPOEndProc(); 104 } 105 } 106 107 /// printSymbolOperand - Print a raw symbol reference operand. This handles 108 /// jump tables, constant pools, global address and external symbols, all of 109 /// which print to a label with various suffixes for relocation types etc. 110 static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO, 111 raw_ostream &O) { 112 switch (MO.getType()) { 113 default: llvm_unreachable("unknown symbol type!"); 114 case MachineOperand::MO_ConstantPoolIndex: 115 P.GetCPISymbol(MO.getIndex())->print(O, P.MAI); 116 P.printOffset(MO.getOffset(), O); 117 break; 118 case MachineOperand::MO_GlobalAddress: { 119 const GlobalValue *GV = MO.getGlobal(); 120 121 MCSymbol *GVSym; 122 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || 123 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) 124 GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 125 else 126 GVSym = P.getSymbol(GV); 127 128 // Handle dllimport linkage. 129 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) 130 GVSym = 131 P.OutContext.getOrCreateSymbol(Twine("__imp_") + GVSym->getName()); 132 133 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || 134 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) { 135 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 136 MachineModuleInfoImpl::StubValueTy &StubSym = 137 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym); 138 if (!StubSym.getPointer()) 139 StubSym = MachineModuleInfoImpl:: 140 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage()); 141 } 142 143 // If the name begins with a dollar-sign, enclose it in parens. We do this 144 // to avoid having it look like an integer immediate to the assembler. 145 if (GVSym->getName()[0] != '$') 146 GVSym->print(O, P.MAI); 147 else { 148 O << '('; 149 GVSym->print(O, P.MAI); 150 O << ')'; 151 } 152 P.printOffset(MO.getOffset(), O); 153 break; 154 } 155 } 156 157 switch (MO.getTargetFlags()) { 158 default: 159 llvm_unreachable("Unknown target flag on GV operand"); 160 case X86II::MO_NO_FLAG: // No flag. 161 break; 162 case X86II::MO_DARWIN_NONLAZY: 163 case X86II::MO_DLLIMPORT: 164 // These affect the name of the symbol, not any suffix. 165 break; 166 case X86II::MO_GOT_ABSOLUTE_ADDRESS: 167 O << " + [.-"; 168 P.MF->getPICBaseSymbol()->print(O, P.MAI); 169 O << ']'; 170 break; 171 case X86II::MO_PIC_BASE_OFFSET: 172 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: 173 O << '-'; 174 P.MF->getPICBaseSymbol()->print(O, P.MAI); 175 break; 176 case X86II::MO_TLSGD: O << "@TLSGD"; break; 177 case X86II::MO_TLSLD: O << "@TLSLD"; break; 178 case X86II::MO_TLSLDM: O << "@TLSLDM"; break; 179 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break; 180 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break; 181 case X86II::MO_TPOFF: O << "@TPOFF"; break; 182 case X86II::MO_DTPOFF: O << "@DTPOFF"; break; 183 case X86II::MO_NTPOFF: O << "@NTPOFF"; break; 184 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break; 185 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break; 186 case X86II::MO_GOT: O << "@GOT"; break; 187 case X86II::MO_GOTOFF: O << "@GOTOFF"; break; 188 case X86II::MO_PLT: O << "@PLT"; break; 189 case X86II::MO_TLVP: O << "@TLVP"; break; 190 case X86II::MO_TLVP_PIC_BASE: 191 O << "@TLVP" << '-'; 192 P.MF->getPICBaseSymbol()->print(O, P.MAI); 193 break; 194 case X86II::MO_SECREL: O << "@SECREL32"; break; 195 } 196 } 197 198 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI, 199 unsigned OpNo, raw_ostream &O, 200 const char *Modifier = nullptr, unsigned AsmVariant = 0); 201 202 /// printPCRelImm - This is used to print an immediate value that ends up 203 /// being encoded as a pc-relative value. These print slightly differently, for 204 /// example, a $ is not emitted. 205 static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI, 206 unsigned OpNo, raw_ostream &O) { 207 const MachineOperand &MO = MI->getOperand(OpNo); 208 switch (MO.getType()) { 209 default: llvm_unreachable("Unknown pcrel immediate operand"); 210 case MachineOperand::MO_Register: 211 // pc-relativeness was handled when computing the value in the reg. 212 printOperand(P, MI, OpNo, O); 213 return; 214 case MachineOperand::MO_Immediate: 215 O << MO.getImm(); 216 return; 217 case MachineOperand::MO_GlobalAddress: 218 printSymbolOperand(P, MO, O); 219 return; 220 } 221 } 222 223 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI, 224 unsigned OpNo, raw_ostream &O, const char *Modifier, 225 unsigned AsmVariant) { 226 const MachineOperand &MO = MI->getOperand(OpNo); 227 switch (MO.getType()) { 228 default: llvm_unreachable("unknown operand type!"); 229 case MachineOperand::MO_Register: { 230 // FIXME: Enumerating AsmVariant, so we can remove magic number. 231 if (AsmVariant == 0) O << '%'; 232 unsigned Reg = MO.getReg(); 233 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { 234 unsigned Size = (strcmp(Modifier+6,"64") == 0) ? 64 : 235 (strcmp(Modifier+6,"32") == 0) ? 32 : 236 (strcmp(Modifier+6,"16") == 0) ? 16 : 8; 237 Reg = getX86SubSuperRegister(Reg, Size); 238 } 239 O << X86ATTInstPrinter::getRegisterName(Reg); 240 return; 241 } 242 243 case MachineOperand::MO_Immediate: 244 if (AsmVariant == 0) O << '$'; 245 O << MO.getImm(); 246 return; 247 248 case MachineOperand::MO_GlobalAddress: { 249 if (AsmVariant == 0) O << '$'; 250 printSymbolOperand(P, MO, O); 251 break; 252 } 253 } 254 } 255 256 static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI, 257 unsigned Op, raw_ostream &O, 258 const char *Modifier = nullptr) { 259 const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg); 260 const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg); 261 const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp); 262 263 // If we really don't want to print out (rip), don't. 264 bool HasBaseReg = BaseReg.getReg() != 0; 265 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") && 266 BaseReg.getReg() == X86::RIP) 267 HasBaseReg = false; 268 269 // HasParenPart - True if we will print out the () part of the mem ref. 270 bool HasParenPart = IndexReg.getReg() || HasBaseReg; 271 272 switch (DispSpec.getType()) { 273 default: 274 llvm_unreachable("unknown operand type!"); 275 case MachineOperand::MO_Immediate: { 276 int DispVal = DispSpec.getImm(); 277 if (DispVal || !HasParenPart) 278 O << DispVal; 279 break; 280 } 281 case MachineOperand::MO_GlobalAddress: 282 case MachineOperand::MO_ConstantPoolIndex: 283 printSymbolOperand(P, DispSpec, O); 284 } 285 286 if (Modifier && strcmp(Modifier, "H") == 0) 287 O << "+8"; 288 289 if (HasParenPart) { 290 assert(IndexReg.getReg() != X86::ESP && 291 "X86 doesn't allow scaling by ESP"); 292 293 O << '('; 294 if (HasBaseReg) 295 printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier); 296 297 if (IndexReg.getReg()) { 298 O << ','; 299 printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier); 300 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm(); 301 if (ScaleVal != 1) 302 O << ',' << ScaleVal; 303 } 304 O << ')'; 305 } 306 } 307 308 static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI, 309 unsigned Op, raw_ostream &O, 310 const char *Modifier = nullptr) { 311 assert(isMem(*MI, Op) && "Invalid memory reference!"); 312 const MachineOperand &Segment = MI->getOperand(Op+X86::AddrSegmentReg); 313 if (Segment.getReg()) { 314 printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier); 315 O << ':'; 316 } 317 printLeaMemReference(P, MI, Op, O, Modifier); 318 } 319 320 static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI, 321 unsigned Op, raw_ostream &O, 322 const char *Modifier = nullptr, 323 unsigned AsmVariant = 1) { 324 const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg); 325 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm(); 326 const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg); 327 const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp); 328 const MachineOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg); 329 330 // If this has a segment register, print it. 331 if (SegReg.getReg()) { 332 printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier, AsmVariant); 333 O << ':'; 334 } 335 336 O << '['; 337 338 bool NeedPlus = false; 339 if (BaseReg.getReg()) { 340 printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier, AsmVariant); 341 NeedPlus = true; 342 } 343 344 if (IndexReg.getReg()) { 345 if (NeedPlus) O << " + "; 346 if (ScaleVal != 1) 347 O << ScaleVal << '*'; 348 printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier, AsmVariant); 349 NeedPlus = true; 350 } 351 352 if (!DispSpec.isImm()) { 353 if (NeedPlus) O << " + "; 354 printOperand(P, MI, Op+X86::AddrDisp, O, Modifier, AsmVariant); 355 } else { 356 int64_t DispVal = DispSpec.getImm(); 357 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) { 358 if (NeedPlus) { 359 if (DispVal > 0) 360 O << " + "; 361 else { 362 O << " - "; 363 DispVal = -DispVal; 364 } 365 } 366 O << DispVal; 367 } 368 } 369 O << ']'; 370 } 371 372 static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO, 373 char Mode, raw_ostream &O) { 374 unsigned Reg = MO.getReg(); 375 switch (Mode) { 376 default: return true; // Unknown mode. 377 case 'b': // Print QImode register 378 Reg = getX86SubSuperRegister(Reg, 8); 379 break; 380 case 'h': // Print QImode high register 381 Reg = getX86SubSuperRegister(Reg, 8, true); 382 break; 383 case 'w': // Print HImode register 384 Reg = getX86SubSuperRegister(Reg, 16); 385 break; 386 case 'k': // Print SImode register 387 Reg = getX86SubSuperRegister(Reg, 32); 388 break; 389 case 'q': 390 // Print 64-bit register names if 64-bit integer registers are available. 391 // Otherwise, print 32-bit register names. 392 Reg = getX86SubSuperRegister(Reg, P.getSubtarget().is64Bit() ? 64 : 32); 393 break; 394 } 395 396 O << '%' << X86ATTInstPrinter::getRegisterName(Reg); 397 return false; 398 } 399 400 /// PrintAsmOperand - Print out an operand for an inline asm expression. 401 /// 402 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 403 unsigned AsmVariant, 404 const char *ExtraCode, raw_ostream &O) { 405 // Does this asm operand have a single letter operand modifier? 406 if (ExtraCode && ExtraCode[0]) { 407 if (ExtraCode[1] != 0) return true; // Unknown modifier. 408 409 const MachineOperand &MO = MI->getOperand(OpNo); 410 411 switch (ExtraCode[0]) { 412 default: 413 // See if this is a generic print operand 414 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O); 415 case 'a': // This is an address. Currently only 'i' and 'r' are expected. 416 switch (MO.getType()) { 417 default: 418 return true; 419 case MachineOperand::MO_Immediate: 420 O << MO.getImm(); 421 return false; 422 case MachineOperand::MO_ConstantPoolIndex: 423 case MachineOperand::MO_JumpTableIndex: 424 case MachineOperand::MO_ExternalSymbol: 425 llvm_unreachable("unexpected operand type!"); 426 case MachineOperand::MO_GlobalAddress: 427 printSymbolOperand(*this, MO, O); 428 if (Subtarget->isPICStyleRIPRel()) 429 O << "(%rip)"; 430 return false; 431 case MachineOperand::MO_Register: 432 O << '('; 433 printOperand(*this, MI, OpNo, O); 434 O << ')'; 435 return false; 436 } 437 438 case 'c': // Don't print "$" before a global var name or constant. 439 switch (MO.getType()) { 440 default: 441 printOperand(*this, MI, OpNo, O); 442 break; 443 case MachineOperand::MO_Immediate: 444 O << MO.getImm(); 445 break; 446 case MachineOperand::MO_ConstantPoolIndex: 447 case MachineOperand::MO_JumpTableIndex: 448 case MachineOperand::MO_ExternalSymbol: 449 llvm_unreachable("unexpected operand type!"); 450 case MachineOperand::MO_GlobalAddress: 451 printSymbolOperand(*this, MO, O); 452 break; 453 } 454 return false; 455 456 case 'A': // Print '*' before a register (it must be a register) 457 if (MO.isReg()) { 458 O << '*'; 459 printOperand(*this, MI, OpNo, O); 460 return false; 461 } 462 return true; 463 464 case 'b': // Print QImode register 465 case 'h': // Print QImode high register 466 case 'w': // Print HImode register 467 case 'k': // Print SImode register 468 case 'q': // Print DImode register 469 if (MO.isReg()) 470 return printAsmMRegister(*this, MO, ExtraCode[0], O); 471 printOperand(*this, MI, OpNo, O); 472 return false; 473 474 case 'P': // This is the operand of a call, treat specially. 475 printPCRelImm(*this, MI, OpNo, O); 476 return false; 477 478 case 'n': // Negate the immediate or print a '-' before the operand. 479 // Note: this is a temporary solution. It should be handled target 480 // independently as part of the 'MC' work. 481 if (MO.isImm()) { 482 O << -MO.getImm(); 483 return false; 484 } 485 O << '-'; 486 } 487 } 488 489 printOperand(*this, MI, OpNo, O, /*Modifier*/ nullptr, AsmVariant); 490 return false; 491 } 492 493 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 494 unsigned OpNo, unsigned AsmVariant, 495 const char *ExtraCode, 496 raw_ostream &O) { 497 if (AsmVariant) { 498 printIntelMemReference(*this, MI, OpNo, O); 499 return false; 500 } 501 502 if (ExtraCode && ExtraCode[0]) { 503 if (ExtraCode[1] != 0) return true; // Unknown modifier. 504 505 switch (ExtraCode[0]) { 506 default: return true; // Unknown modifier. 507 case 'b': // Print QImode register 508 case 'h': // Print QImode high register 509 case 'w': // Print HImode register 510 case 'k': // Print SImode register 511 case 'q': // Print SImode register 512 // These only apply to registers, ignore on mem. 513 break; 514 case 'H': 515 printMemReference(*this, MI, OpNo, O, "H"); 516 return false; 517 case 'P': // Don't print @PLT, but do print as memory. 518 printMemReference(*this, MI, OpNo, O, "no-rip"); 519 return false; 520 } 521 } 522 printMemReference(*this, MI, OpNo, O); 523 return false; 524 } 525 526 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) { 527 const Triple &TT = TM.getTargetTriple(); 528 529 if (TT.isOSBinFormatMachO()) 530 OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); 531 532 if (TT.isOSBinFormatCOFF()) { 533 // Emit an absolute @feat.00 symbol. This appears to be some kind of 534 // compiler features bitfield read by link.exe. 535 if (TT.getArch() == Triple::x86) { 536 MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00")); 537 OutStreamer->BeginCOFFSymbolDef(S); 538 OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC); 539 OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL); 540 OutStreamer->EndCOFFSymbolDef(); 541 // According to the PE-COFF spec, the LSB of this value marks the object 542 // for "registered SEH". This means that all SEH handler entry points 543 // must be registered in .sxdata. Use of any unregistered handlers will 544 // cause the process to terminate immediately. LLVM does not know how to 545 // register any SEH handlers, so its object files should be safe. 546 OutStreamer->EmitSymbolAttribute(S, MCSA_Global); 547 OutStreamer->EmitAssignment( 548 S, MCConstantExpr::create(int64_t(1), MMI->getContext())); 549 } 550 } 551 OutStreamer->EmitSyntaxDirective(); 552 553 // If this is not inline asm and we're in 16-bit 554 // mode prefix assembly with .code16. 555 bool is16 = TT.getEnvironment() == Triple::CODE16; 556 if (M.getModuleInlineAsm().empty() && is16) 557 OutStreamer->EmitAssemblerFlag(MCAF_Code16); 558 } 559 560 static void 561 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel, 562 MachineModuleInfoImpl::StubValueTy &MCSym) { 563 // L_foo$stub: 564 OutStreamer.EmitLabel(StubLabel); 565 // .indirect_symbol _foo 566 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol); 567 568 if (MCSym.getInt()) 569 // External to current translation unit. 570 OutStreamer.EmitIntValue(0, 4/*size*/); 571 else 572 // Internal to current translation unit. 573 // 574 // When we place the LSDA into the TEXT section, the type info 575 // pointers need to be indirect and pc-rel. We accomplish this by 576 // using NLPs; however, sometimes the types are local to the file. 577 // We need to fill in the value for the NLP in those cases. 578 OutStreamer.EmitValue( 579 MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()), 580 4 /*size*/); 581 } 582 583 MCSymbol *X86AsmPrinter::GetCPISymbol(unsigned CPID) const { 584 if (Subtarget->isTargetKnownWindowsMSVC()) { 585 const MachineConstantPoolEntry &CPE = 586 MF->getConstantPool()->getConstants()[CPID]; 587 if (!CPE.isMachineConstantPoolEntry()) { 588 const DataLayout &DL = MF->getDataLayout(); 589 SectionKind Kind = CPE.getSectionKind(&DL); 590 const Constant *C = CPE.Val.ConstVal; 591 unsigned Align = CPE.Alignment; 592 if (const MCSectionCOFF *S = dyn_cast<MCSectionCOFF>( 593 getObjFileLowering().getSectionForConstant(DL, Kind, C, Align))) { 594 if (MCSymbol *Sym = S->getCOMDATSymbol()) { 595 if (Sym->isUndefined()) 596 OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global); 597 return Sym; 598 } 599 } 600 } 601 } 602 603 return AsmPrinter::GetCPISymbol(CPID); 604 } 605 606 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) { 607 const Triple &TT = TM.getTargetTriple(); 608 609 if (TT.isOSBinFormatMachO()) { 610 // All darwin targets use mach-o. 611 MachineModuleInfoMachO &MMIMacho = 612 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 613 614 // Output stubs for dynamically-linked functions. 615 MachineModuleInfoMachO::SymbolListTy Stubs; 616 617 // Output stubs for external and common global variables. 618 Stubs = MMIMacho.GetGVStubList(); 619 if (!Stubs.empty()) { 620 MCSection *TheSection = OutContext.getMachOSection( 621 "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS, 622 SectionKind::getMetadata()); 623 OutStreamer->SwitchSection(TheSection); 624 625 for (auto &Stub : Stubs) 626 emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second); 627 628 Stubs.clear(); 629 OutStreamer->AddBlankLine(); 630 } 631 632 SM.serializeToStackMapSection(); 633 FM.serializeToFaultMapSection(); 634 635 // Funny Darwin hack: This flag tells the linker that no global symbols 636 // contain code that falls through to other global symbols (e.g. the obvious 637 // implementation of multiple entry points). If this doesn't occur, the 638 // linker can safely perform dead code stripping. Since LLVM never 639 // generates code that does this, it is always safe to set. 640 OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 641 } 642 643 if (TT.isKnownWindowsMSVCEnvironment() && MMI->usesVAFloatArgument()) { 644 StringRef SymbolName = 645 (TT.getArch() == Triple::x86_64) ? "_fltused" : "__fltused"; 646 MCSymbol *S = MMI->getContext().getOrCreateSymbol(SymbolName); 647 OutStreamer->EmitSymbolAttribute(S, MCSA_Global); 648 } 649 650 if (TT.isOSBinFormatCOFF()) { 651 const TargetLoweringObjectFileCOFF &TLOFCOFF = 652 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering()); 653 654 std::string Flags; 655 raw_string_ostream FlagsOS(Flags); 656 657 for (const auto &Function : M) 658 TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Function); 659 for (const auto &Global : M.globals()) 660 TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Global); 661 for (const auto &Alias : M.aliases()) 662 TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Alias); 663 664 FlagsOS.flush(); 665 666 // Output collected flags. 667 if (!Flags.empty()) { 668 OutStreamer->SwitchSection(TLOFCOFF.getDrectveSection()); 669 OutStreamer->EmitBytes(Flags); 670 } 671 672 SM.serializeToStackMapSection(); 673 } 674 675 if (TT.isOSBinFormatELF()) { 676 SM.serializeToStackMapSection(); 677 FM.serializeToFaultMapSection(); 678 } 679 } 680 681 //===----------------------------------------------------------------------===// 682 // Target Registry Stuff 683 //===----------------------------------------------------------------------===// 684 685 // Force static initialization. 686 extern "C" void LLVMInitializeX86AsmPrinter() { 687 RegisterAsmPrinter<X86AsmPrinter> X(getTheX86_32Target()); 688 RegisterAsmPrinter<X86AsmPrinter> Y(getTheX86_64Target()); 689 } 690