LLVM API Documentation
00001 //===-- AArch64AsmPrinter.cpp - AArch64 LLVM assembly writer --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains a printer that converts from our internal representation 00011 // of machine-dependent LLVM code to the AArch64 assembly language. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "AArch64.h" 00016 #include "AArch64MCInstLower.h" 00017 #include "AArch64MachineFunctionInfo.h" 00018 #include "AArch64RegisterInfo.h" 00019 #include "AArch64Subtarget.h" 00020 #include "InstPrinter/AArch64InstPrinter.h" 00021 #include "llvm/ADT/SmallString.h" 00022 #include "llvm/ADT/StringSwitch.h" 00023 #include "llvm/ADT/Twine.h" 00024 #include "llvm/CodeGen/AsmPrinter.h" 00025 #include "llvm/CodeGen/MachineInstr.h" 00026 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 00027 #include "llvm/CodeGen/StackMaps.h" 00028 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 00029 #include "llvm/IR/DataLayout.h" 00030 #include "llvm/IR/DebugInfo.h" 00031 #include "llvm/MC/MCAsmInfo.h" 00032 #include "llvm/MC/MCContext.h" 00033 #include "llvm/MC/MCInst.h" 00034 #include "llvm/MC/MCInstBuilder.h" 00035 #include "llvm/MC/MCLinkerOptimizationHint.h" 00036 #include "llvm/MC/MCStreamer.h" 00037 #include "llvm/Support/Debug.h" 00038 #include "llvm/Support/TargetRegistry.h" 00039 using namespace llvm; 00040 00041 #define DEBUG_TYPE "asm-printer" 00042 00043 namespace { 00044 00045 class AArch64AsmPrinter : public AsmPrinter { 00046 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can 00047 /// make the right decision when printing asm code for different targets. 00048 const AArch64Subtarget *Subtarget; 00049 00050 AArch64MCInstLower MCInstLowering; 00051 StackMaps SM; 00052 00053 public: 00054 AArch64AsmPrinter(TargetMachine &TM, MCStreamer &Streamer) 00055 : AsmPrinter(TM, Streamer), 00056 Subtarget(&TM.getSubtarget<AArch64Subtarget>()), 00057 MCInstLowering(OutContext, *this), SM(*this), AArch64FI(nullptr), 00058 LOHLabelCounter(0) {} 00059 00060 const char *getPassName() const override { 00061 return "AArch64 Assembly Printer"; 00062 } 00063 00064 /// \brief Wrapper for MCInstLowering.lowerOperand() for the 00065 /// tblgen'erated pseudo lowering. 00066 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const { 00067 return MCInstLowering.lowerOperand(MO, MCOp); 00068 } 00069 00070 void LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM, 00071 const MachineInstr &MI); 00072 void LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM, 00073 const MachineInstr &MI); 00074 /// \brief tblgen'erated driver function for lowering simple MI->MC 00075 /// pseudo instructions. 00076 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, 00077 const MachineInstr *MI); 00078 00079 void EmitInstruction(const MachineInstr *MI) override; 00080 00081 void getAnalysisUsage(AnalysisUsage &AU) const override { 00082 AsmPrinter::getAnalysisUsage(AU); 00083 AU.setPreservesAll(); 00084 } 00085 00086 bool runOnMachineFunction(MachineFunction &F) override { 00087 AArch64FI = F.getInfo<AArch64FunctionInfo>(); 00088 return AsmPrinter::runOnMachineFunction(F); 00089 } 00090 00091 private: 00092 MachineLocation getDebugValueLocation(const MachineInstr *MI) const; 00093 void printOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O); 00094 bool printAsmMRegister(const MachineOperand &MO, char Mode, raw_ostream &O); 00095 bool printAsmRegInClass(const MachineOperand &MO, 00096 const TargetRegisterClass *RC, bool isVector, 00097 raw_ostream &O); 00098 00099 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 00100 unsigned AsmVariant, const char *ExtraCode, 00101 raw_ostream &O) override; 00102 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, 00103 unsigned AsmVariant, const char *ExtraCode, 00104 raw_ostream &O) override; 00105 00106 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS); 00107 00108 void EmitFunctionBodyEnd() override; 00109 00110 MCSymbol *GetCPISymbol(unsigned CPID) const override; 00111 void EmitEndOfAsmFile(Module &M) override; 00112 AArch64FunctionInfo *AArch64FI; 00113 00114 /// \brief Emit the LOHs contained in AArch64FI. 00115 void EmitLOHs(); 00116 00117 typedef std::map<const MachineInstr *, MCSymbol *> MInstToMCSymbol; 00118 MInstToMCSymbol LOHInstToLabel; 00119 unsigned LOHLabelCounter; 00120 }; 00121 00122 } // end of anonymous namespace 00123 00124 //===----------------------------------------------------------------------===// 00125 00126 void AArch64AsmPrinter::EmitEndOfAsmFile(Module &M) { 00127 if (Subtarget->isTargetMachO()) { 00128 // Funny Darwin hack: This flag tells the linker that no global symbols 00129 // contain code that falls through to other global symbols (e.g. the obvious 00130 // implementation of multiple entry points). If this doesn't occur, the 00131 // linker can safely perform dead code stripping. Since LLVM never 00132 // generates code that does this, it is always safe to set. 00133 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 00134 SM.serializeToStackMapSection(); 00135 } 00136 00137 // Emit a .data.rel section containing any stubs that were created. 00138 if (Subtarget->isTargetELF()) { 00139 const TargetLoweringObjectFileELF &TLOFELF = 00140 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering()); 00141 00142 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>(); 00143 00144 // Output stubs for external and common global variables. 00145 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList(); 00146 if (!Stubs.empty()) { 00147 OutStreamer.SwitchSection(TLOFELF.getDataRelSection()); 00148 const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout(); 00149 00150 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 00151 OutStreamer.EmitLabel(Stubs[i].first); 00152 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(), 00153 TD->getPointerSize(0)); 00154 } 00155 Stubs.clear(); 00156 } 00157 } 00158 00159 } 00160 00161 MachineLocation 00162 AArch64AsmPrinter::getDebugValueLocation(const MachineInstr *MI) const { 00163 MachineLocation Location; 00164 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!"); 00165 // Frame address. Currently handles register +- offset only. 00166 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) 00167 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm()); 00168 else { 00169 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n"); 00170 } 00171 return Location; 00172 } 00173 00174 void AArch64AsmPrinter::EmitLOHs() { 00175 SmallVector<MCSymbol *, 3> MCArgs; 00176 00177 for (const auto &D : AArch64FI->getLOHContainer()) { 00178 for (const MachineInstr *MI : D.getArgs()) { 00179 MInstToMCSymbol::iterator LabelIt = LOHInstToLabel.find(MI); 00180 assert(LabelIt != LOHInstToLabel.end() && 00181 "Label hasn't been inserted for LOH related instruction"); 00182 MCArgs.push_back(LabelIt->second); 00183 } 00184 OutStreamer.EmitLOHDirective(D.getKind(), MCArgs); 00185 MCArgs.clear(); 00186 } 00187 } 00188 00189 void AArch64AsmPrinter::EmitFunctionBodyEnd() { 00190 if (!AArch64FI->getLOHRelated().empty()) 00191 EmitLOHs(); 00192 } 00193 00194 /// GetCPISymbol - Return the symbol for the specified constant pool entry. 00195 MCSymbol *AArch64AsmPrinter::GetCPISymbol(unsigned CPID) const { 00196 // Darwin uses a linker-private symbol name for constant-pools (to 00197 // avoid addends on the relocation?), ELF has no such concept and 00198 // uses a normal private symbol. 00199 if (getDataLayout().getLinkerPrivateGlobalPrefix()[0]) 00200 return OutContext.GetOrCreateSymbol( 00201 Twine(getDataLayout().getLinkerPrivateGlobalPrefix()) + "CPI" + 00202 Twine(getFunctionNumber()) + "_" + Twine(CPID)); 00203 00204 return OutContext.GetOrCreateSymbol( 00205 Twine(getDataLayout().getPrivateGlobalPrefix()) + "CPI" + 00206 Twine(getFunctionNumber()) + "_" + Twine(CPID)); 00207 } 00208 00209 void AArch64AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNum, 00210 raw_ostream &O) { 00211 const MachineOperand &MO = MI->getOperand(OpNum); 00212 switch (MO.getType()) { 00213 default: 00214 llvm_unreachable("<unknown operand type>"); 00215 case MachineOperand::MO_Register: { 00216 unsigned Reg = MO.getReg(); 00217 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 00218 assert(!MO.getSubReg() && "Subregs should be eliminated!"); 00219 O << AArch64InstPrinter::getRegisterName(Reg); 00220 break; 00221 } 00222 case MachineOperand::MO_Immediate: { 00223 int64_t Imm = MO.getImm(); 00224 O << '#' << Imm; 00225 break; 00226 } 00227 } 00228 } 00229 00230 bool AArch64AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode, 00231 raw_ostream &O) { 00232 unsigned Reg = MO.getReg(); 00233 switch (Mode) { 00234 default: 00235 return true; // Unknown mode. 00236 case 'w': 00237 Reg = getWRegFromXReg(Reg); 00238 break; 00239 case 'x': 00240 Reg = getXRegFromWReg(Reg); 00241 break; 00242 } 00243 00244 O << AArch64InstPrinter::getRegisterName(Reg); 00245 return false; 00246 } 00247 00248 // Prints the register in MO using class RC using the offset in the 00249 // new register class. This should not be used for cross class 00250 // printing. 00251 bool AArch64AsmPrinter::printAsmRegInClass(const MachineOperand &MO, 00252 const TargetRegisterClass *RC, 00253 bool isVector, raw_ostream &O) { 00254 assert(MO.isReg() && "Should only get here with a register!"); 00255 const AArch64RegisterInfo *RI = static_cast<const AArch64RegisterInfo *>( 00256 TM.getSubtargetImpl()->getRegisterInfo()); 00257 unsigned Reg = MO.getReg(); 00258 unsigned RegToPrint = RC->getRegister(RI->getEncodingValue(Reg)); 00259 assert(RI->regsOverlap(RegToPrint, Reg)); 00260 O << AArch64InstPrinter::getRegisterName( 00261 RegToPrint, isVector ? AArch64::vreg : AArch64::NoRegAltName); 00262 return false; 00263 } 00264 00265 bool AArch64AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 00266 unsigned AsmVariant, 00267 const char *ExtraCode, raw_ostream &O) { 00268 const MachineOperand &MO = MI->getOperand(OpNum); 00269 00270 // First try the generic code, which knows about modifiers like 'c' and 'n'. 00271 if (!AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O)) 00272 return false; 00273 00274 // Does this asm operand have a single letter operand modifier? 00275 if (ExtraCode && ExtraCode[0]) { 00276 if (ExtraCode[1] != 0) 00277 return true; // Unknown modifier. 00278 00279 switch (ExtraCode[0]) { 00280 default: 00281 return true; // Unknown modifier. 00282 case 'w': // Print W register 00283 case 'x': // Print X register 00284 if (MO.isReg()) 00285 return printAsmMRegister(MO, ExtraCode[0], O); 00286 if (MO.isImm() && MO.getImm() == 0) { 00287 unsigned Reg = ExtraCode[0] == 'w' ? AArch64::WZR : AArch64::XZR; 00288 O << AArch64InstPrinter::getRegisterName(Reg); 00289 return false; 00290 } 00291 printOperand(MI, OpNum, O); 00292 return false; 00293 case 'b': // Print B register. 00294 case 'h': // Print H register. 00295 case 's': // Print S register. 00296 case 'd': // Print D register. 00297 case 'q': // Print Q register. 00298 if (MO.isReg()) { 00299 const TargetRegisterClass *RC; 00300 switch (ExtraCode[0]) { 00301 case 'b': 00302 RC = &AArch64::FPR8RegClass; 00303 break; 00304 case 'h': 00305 RC = &AArch64::FPR16RegClass; 00306 break; 00307 case 's': 00308 RC = &AArch64::FPR32RegClass; 00309 break; 00310 case 'd': 00311 RC = &AArch64::FPR64RegClass; 00312 break; 00313 case 'q': 00314 RC = &AArch64::FPR128RegClass; 00315 break; 00316 default: 00317 return true; 00318 } 00319 return printAsmRegInClass(MO, RC, false /* vector */, O); 00320 } 00321 printOperand(MI, OpNum, O); 00322 return false; 00323 } 00324 } 00325 00326 // According to ARM, we should emit x and v registers unless we have a 00327 // modifier. 00328 if (MO.isReg()) { 00329 unsigned Reg = MO.getReg(); 00330 00331 // If this is a w or x register, print an x register. 00332 if (AArch64::GPR32allRegClass.contains(Reg) || 00333 AArch64::GPR64allRegClass.contains(Reg)) 00334 return printAsmMRegister(MO, 'x', O); 00335 00336 // If this is a b, h, s, d, or q register, print it as a v register. 00337 return printAsmRegInClass(MO, &AArch64::FPR128RegClass, true /* vector */, 00338 O); 00339 } 00340 00341 printOperand(MI, OpNum, O); 00342 return false; 00343 } 00344 00345 bool AArch64AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 00346 unsigned OpNum, 00347 unsigned AsmVariant, 00348 const char *ExtraCode, 00349 raw_ostream &O) { 00350 if (ExtraCode && ExtraCode[0]) 00351 return true; // Unknown modifier. 00352 00353 const MachineOperand &MO = MI->getOperand(OpNum); 00354 assert(MO.isReg() && "unexpected inline asm memory operand"); 00355 O << "[" << AArch64InstPrinter::getRegisterName(MO.getReg()) << "]"; 00356 return false; 00357 } 00358 00359 void AArch64AsmPrinter::PrintDebugValueComment(const MachineInstr *MI, 00360 raw_ostream &OS) { 00361 unsigned NOps = MI->getNumOperands(); 00362 assert(NOps == 4); 00363 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: "; 00364 // cast away const; DIetc do not take const operands for some reason. 00365 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps - 1).getMetadata())); 00366 OS << V.getName(); 00367 OS << " <- "; 00368 // Frame address. Currently handles register +- offset only. 00369 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm()); 00370 OS << '['; 00371 printOperand(MI, 0, OS); 00372 OS << '+'; 00373 printOperand(MI, 1, OS); 00374 OS << ']'; 00375 OS << "+"; 00376 printOperand(MI, NOps - 2, OS); 00377 } 00378 00379 void AArch64AsmPrinter::LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM, 00380 const MachineInstr &MI) { 00381 unsigned NumNOPBytes = MI.getOperand(1).getImm(); 00382 00383 SM.recordStackMap(MI); 00384 // Emit padding. 00385 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!"); 00386 for (unsigned i = 0; i < NumNOPBytes; i += 4) 00387 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::HINT).addImm(0)); 00388 } 00389 00390 // Lower a patchpoint of the form: 00391 // [<def>], <id>, <numBytes>, <target>, <numArgs> 00392 void AArch64AsmPrinter::LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM, 00393 const MachineInstr &MI) { 00394 SM.recordPatchPoint(MI); 00395 00396 PatchPointOpers Opers(&MI); 00397 00398 int64_t CallTarget = Opers.getMetaOper(PatchPointOpers::TargetPos).getImm(); 00399 unsigned EncodedBytes = 0; 00400 if (CallTarget) { 00401 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget && 00402 "High 16 bits of call target should be zero."); 00403 unsigned ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg(); 00404 EncodedBytes = 16; 00405 // Materialize the jump address: 00406 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::MOVZWi) 00407 .addReg(ScratchReg) 00408 .addImm((CallTarget >> 32) & 0xFFFF) 00409 .addImm(32)); 00410 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::MOVKWi) 00411 .addReg(ScratchReg) 00412 .addReg(ScratchReg) 00413 .addImm((CallTarget >> 16) & 0xFFFF) 00414 .addImm(16)); 00415 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::MOVKWi) 00416 .addReg(ScratchReg) 00417 .addReg(ScratchReg) 00418 .addImm(CallTarget & 0xFFFF) 00419 .addImm(0)); 00420 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::BLR).addReg(ScratchReg)); 00421 } 00422 // Emit padding. 00423 unsigned NumBytes = Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm(); 00424 assert(NumBytes >= EncodedBytes && 00425 "Patchpoint can't request size less than the length of a call."); 00426 assert((NumBytes - EncodedBytes) % 4 == 0 && 00427 "Invalid number of NOP bytes requested!"); 00428 for (unsigned i = EncodedBytes; i < NumBytes; i += 4) 00429 EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::HINT).addImm(0)); 00430 } 00431 00432 // Simple pseudo-instructions have their lowering (with expansion to real 00433 // instructions) auto-generated. 00434 #include "AArch64GenMCPseudoLowering.inc" 00435 00436 void AArch64AsmPrinter::EmitInstruction(const MachineInstr *MI) { 00437 // Do any auto-generated pseudo lowerings. 00438 if (emitPseudoExpansionLowering(OutStreamer, MI)) 00439 return; 00440 00441 if (AArch64FI->getLOHRelated().count(MI)) { 00442 // Generate a label for LOH related instruction 00443 MCSymbol *LOHLabel = GetTempSymbol("loh", LOHLabelCounter++); 00444 // Associate the instruction with the label 00445 LOHInstToLabel[MI] = LOHLabel; 00446 OutStreamer.EmitLabel(LOHLabel); 00447 } 00448 00449 // Do any manual lowerings. 00450 switch (MI->getOpcode()) { 00451 default: 00452 break; 00453 case AArch64::DBG_VALUE: { 00454 if (isVerbose() && OutStreamer.hasRawTextSupport()) { 00455 SmallString<128> TmpStr; 00456 raw_svector_ostream OS(TmpStr); 00457 PrintDebugValueComment(MI, OS); 00458 OutStreamer.EmitRawText(StringRef(OS.str())); 00459 } 00460 return; 00461 } 00462 00463 // Tail calls use pseudo instructions so they have the proper code-gen 00464 // attributes (isCall, isReturn, etc.). We lower them to the real 00465 // instruction here. 00466 case AArch64::TCRETURNri: { 00467 MCInst TmpInst; 00468 TmpInst.setOpcode(AArch64::BR); 00469 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 00470 EmitToStreamer(OutStreamer, TmpInst); 00471 return; 00472 } 00473 case AArch64::TCRETURNdi: { 00474 MCOperand Dest; 00475 MCInstLowering.lowerOperand(MI->getOperand(0), Dest); 00476 MCInst TmpInst; 00477 TmpInst.setOpcode(AArch64::B); 00478 TmpInst.addOperand(Dest); 00479 EmitToStreamer(OutStreamer, TmpInst); 00480 return; 00481 } 00482 case AArch64::TLSDESC_BLR: { 00483 MCOperand Callee, Sym; 00484 MCInstLowering.lowerOperand(MI->getOperand(0), Callee); 00485 MCInstLowering.lowerOperand(MI->getOperand(1), Sym); 00486 00487 // First emit a relocation-annotation. This expands to no code, but requests 00488 // the following instruction gets an R_AARCH64_TLSDESC_CALL. 00489 MCInst TLSDescCall; 00490 TLSDescCall.setOpcode(AArch64::TLSDESCCALL); 00491 TLSDescCall.addOperand(Sym); 00492 EmitToStreamer(OutStreamer, TLSDescCall); 00493 00494 // Other than that it's just a normal indirect call to the function loaded 00495 // from the descriptor. 00496 MCInst BLR; 00497 BLR.setOpcode(AArch64::BLR); 00498 BLR.addOperand(Callee); 00499 EmitToStreamer(OutStreamer, BLR); 00500 00501 return; 00502 } 00503 00504 case TargetOpcode::STACKMAP: 00505 return LowerSTACKMAP(OutStreamer, SM, *MI); 00506 00507 case TargetOpcode::PATCHPOINT: 00508 return LowerPATCHPOINT(OutStreamer, SM, *MI); 00509 } 00510 00511 // Finally, do the automated lowerings for everything else. 00512 MCInst TmpInst; 00513 MCInstLowering.Lower(MI, TmpInst); 00514 EmitToStreamer(OutStreamer, TmpInst); 00515 } 00516 00517 // Force static initialization. 00518 extern "C" void LLVMInitializeAArch64AsmPrinter() { 00519 RegisterAsmPrinter<AArch64AsmPrinter> X(TheAArch64leTarget); 00520 RegisterAsmPrinter<AArch64AsmPrinter> Y(TheAArch64beTarget); 00521 RegisterAsmPrinter<AArch64AsmPrinter> Z(TheARM64Target); 00522 }