AsmPrinter.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains a class to be used as the base class for target specific
  15. // asm writers. This class primarily handles common functionality used by
  16. // all asm writers.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_ASMPRINTER_H
  20. #define LLVM_CODEGEN_ASMPRINTER_H
  21. #include "llvm/ADT/MapVector.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/BinaryFormat/Dwarf.h"
  24. #include "llvm/CodeGen/AsmPrinterHandler.h"
  25. #include "llvm/CodeGen/DwarfStringPoolEntry.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/IR/InlineAsm.h"
  28. #include "llvm/IR/LLVMContext.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/SourceMgr.h"
  31. #include <cstdint>
  32. #include <memory>
  33. #include <utility>
  34. #include <vector>
  35. namespace llvm {
  36. class BasicBlock;
  37. class BlockAddress;
  38. class Constant;
  39. class ConstantArray;
  40. class DataLayout;
  41. class DIE;
  42. class DIEAbbrev;
  43. class DwarfDebug;
  44. class GCMetadataPrinter;
  45. class GCStrategy;
  46. class GlobalObject;
  47. class GlobalValue;
  48. class GlobalVariable;
  49. class MachineBasicBlock;
  50. class MachineConstantPoolValue;
  51. class MachineDominatorTree;
  52. class MachineFunction;
  53. class MachineInstr;
  54. class MachineJumpTableInfo;
  55. class MachineLoopInfo;
  56. class MachineModuleInfo;
  57. class MachineOptimizationRemarkEmitter;
  58. class MCAsmInfo;
  59. class MCCFIInstruction;
  60. class MCContext;
  61. class MCExpr;
  62. class MCInst;
  63. class MCSection;
  64. class MCStreamer;
  65. class MCSubtargetInfo;
  66. class MCSymbol;
  67. class MCTargetOptions;
  68. class MDNode;
  69. class Module;
  70. class PseudoProbeHandler;
  71. class raw_ostream;
  72. class StackMaps;
  73. class StringRef;
  74. class TargetLoweringObjectFile;
  75. class TargetMachine;
  76. class Twine;
  77. namespace remarks {
  78. class RemarkStreamer;
  79. }
  80. /// This class is intended to be used as a driving class for all asm writers.
  81. class AsmPrinter : public MachineFunctionPass {
  82. public:
  83. /// Target machine description.
  84. TargetMachine &TM;
  85. /// Target Asm Printer information.
  86. const MCAsmInfo *MAI;
  87. /// This is the context for the output file that we are streaming. This owns
  88. /// all of the global MC-related objects for the generated translation unit.
  89. MCContext &OutContext;
  90. /// This is the MCStreamer object for the file we are generating. This
  91. /// contains the transient state for the current translation unit that we are
  92. /// generating (such as the current section etc).
  93. std::unique_ptr<MCStreamer> OutStreamer;
  94. /// The current machine function.
  95. MachineFunction *MF = nullptr;
  96. /// This is a pointer to the current MachineModuleInfo.
  97. MachineModuleInfo *MMI = nullptr;
  98. /// This is a pointer to the current MachineDominatorTree.
  99. MachineDominatorTree *MDT = nullptr;
  100. /// This is a pointer to the current MachineLoopInfo.
  101. MachineLoopInfo *MLI = nullptr;
  102. /// Optimization remark emitter.
  103. MachineOptimizationRemarkEmitter *ORE;
  104. /// The symbol for the entry in __patchable_function_entires.
  105. MCSymbol *CurrentPatchableFunctionEntrySym = nullptr;
  106. /// The symbol for the current function. This is recalculated at the beginning
  107. /// of each call to runOnMachineFunction().
  108. MCSymbol *CurrentFnSym = nullptr;
  109. /// The symbol for the current function descriptor on AIX. This is created
  110. /// at the beginning of each call to SetupMachineFunction().
  111. MCSymbol *CurrentFnDescSym = nullptr;
  112. /// The symbol used to represent the start of the current function for the
  113. /// purpose of calculating its size (e.g. using the .size directive). By
  114. /// default, this is equal to CurrentFnSym.
  115. MCSymbol *CurrentFnSymForSize = nullptr;
  116. /// Map a basic block section ID to the begin and end symbols of that section
  117. /// which determine the section's range.
  118. struct MBBSectionRange {
  119. MCSymbol *BeginLabel, *EndLabel;
  120. };
  121. MapVector<unsigned, MBBSectionRange> MBBSectionRanges;
  122. /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
  123. /// its number of uses by other globals.
  124. using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;
  125. MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
  126. /// struct HandlerInfo and Handlers permit users or target extended
  127. /// AsmPrinter to add their own handlers.
  128. struct HandlerInfo {
  129. std::unique_ptr<AsmPrinterHandler> Handler;
  130. StringRef TimerName;
  131. StringRef TimerDescription;
  132. StringRef TimerGroupName;
  133. StringRef TimerGroupDescription;
  134. HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler, StringRef TimerName,
  135. StringRef TimerDescription, StringRef TimerGroupName,
  136. StringRef TimerGroupDescription)
  137. : Handler(std::move(Handler)), TimerName(TimerName),
  138. TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
  139. TimerGroupDescription(TimerGroupDescription) {}
  140. };
  141. // Flags representing which CFI section is required for a function/module.
  142. enum class CFISection : unsigned {
  143. None = 0, ///< Do not emit either .eh_frame or .debug_frame
  144. EH = 1, ///< Emit .eh_frame
  145. Debug = 2 ///< Emit .debug_frame
  146. };
  147. private:
  148. MCSymbol *CurrentFnEnd = nullptr;
  149. /// Map a basic block section ID to the exception symbol associated with that
  150. /// section. Map entries are assigned and looked up via
  151. /// AsmPrinter::getMBBExceptionSym.
  152. DenseMap<unsigned, MCSymbol *> MBBSectionExceptionSyms;
  153. // The symbol used to represent the start of the current BB section of the
  154. // function. This is used to calculate the size of the BB section.
  155. MCSymbol *CurrentSectionBeginSym = nullptr;
  156. // The garbage collection metadata printer table.
  157. void *GCMetadataPrinters = nullptr; // Really a DenseMap.
  158. /// Emit comments in assembly output if this is true.
  159. bool VerboseAsm;
  160. /// Output stream for the stack usage file (i.e., .su file).
  161. std::unique_ptr<raw_fd_ostream> StackUsageStream;
  162. static char ID;
  163. protected:
  164. MCSymbol *CurrentFnBegin = nullptr;
  165. /// A vector of all debug/EH info emitters we should use. This vector
  166. /// maintains ownership of the emitters.
  167. std::vector<HandlerInfo> Handlers;
  168. size_t NumUserHandlers = 0;
  169. private:
  170. /// If generated on the fly this own the instance.
  171. std::unique_ptr<MachineDominatorTree> OwnedMDT;
  172. /// If generated on the fly this own the instance.
  173. std::unique_ptr<MachineLoopInfo> OwnedMLI;
  174. /// If the target supports dwarf debug info, this pointer is non-null.
  175. DwarfDebug *DD = nullptr;
  176. /// A handler that supports pseudo probe emission with embedded inline
  177. /// context.
  178. PseudoProbeHandler *PP = nullptr;
  179. /// CFISection type the module needs i.e. either .eh_frame or .debug_frame.
  180. CFISection ModuleCFISection = CFISection::None;
  181. protected:
  182. explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
  183. public:
  184. ~AsmPrinter() override;
  185. DwarfDebug *getDwarfDebug() { return DD; }
  186. DwarfDebug *getDwarfDebug() const { return DD; }
  187. uint16_t getDwarfVersion() const;
  188. void setDwarfVersion(uint16_t Version);
  189. bool isDwarf64() const;
  190. /// Returns 4 for DWARF32 and 8 for DWARF64.
  191. unsigned int getDwarfOffsetByteSize() const;
  192. /// Returns 4 for DWARF32 and 12 for DWARF64.
  193. unsigned int getUnitLengthFieldByteSize() const;
  194. /// Returns information about the byte size of DW_FORM values.
  195. dwarf::FormParams getDwarfFormParams() const;
  196. bool isPositionIndependent() const;
  197. /// Return true if assembly output should contain comments.
  198. bool isVerbose() const { return VerboseAsm; }
  199. /// Return a unique ID for the current function.
  200. unsigned getFunctionNumber() const;
  201. /// Return symbol for the function pseudo stack if the stack frame is not a
  202. /// register based.
  203. virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; }
  204. MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
  205. MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
  206. // Return the exception symbol associated with the MBB section containing a
  207. // given basic block.
  208. MCSymbol *getMBBExceptionSym(const MachineBasicBlock &MBB);
  209. /// Return information about object file lowering.
  210. const TargetLoweringObjectFile &getObjFileLowering() const;
  211. /// Return information about data layout.
  212. const DataLayout &getDataLayout() const;
  213. /// Return the pointer size from the TargetMachine
  214. unsigned getPointerSize() const;
  215. /// Return information about subtarget.
  216. const MCSubtargetInfo &getSubtargetInfo() const;
  217. void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
  218. /// Emits inital debug location directive.
  219. void emitInitialRawDwarfLocDirective(const MachineFunction &MF);
  220. /// Return the current section we are emitting to.
  221. const MCSection *getCurrentSection() const;
  222. void getNameWithPrefix(SmallVectorImpl<char> &Name,
  223. const GlobalValue *GV) const;
  224. MCSymbol *getSymbol(const GlobalValue *GV) const;
  225. /// Similar to getSymbol() but preferred for references. On ELF, this uses a
  226. /// local symbol if a reference to GV is guaranteed to be resolved to the
  227. /// definition in the same module.
  228. MCSymbol *getSymbolPreferLocal(const GlobalValue &GV) const;
  229. //===------------------------------------------------------------------===//
  230. // XRay instrumentation implementation.
  231. //===------------------------------------------------------------------===//
  232. public:
  233. // This describes the kind of sled we're storing in the XRay table.
  234. enum class SledKind : uint8_t {
  235. FUNCTION_ENTER = 0,
  236. FUNCTION_EXIT = 1,
  237. TAIL_CALL = 2,
  238. LOG_ARGS_ENTER = 3,
  239. CUSTOM_EVENT = 4,
  240. TYPED_EVENT = 5,
  241. };
  242. // The table will contain these structs that point to the sled, the function
  243. // containing the sled, and what kind of sled (and whether they should always
  244. // be instrumented). We also use a version identifier that the runtime can use
  245. // to decide what to do with the sled, depending on the version of the sled.
  246. struct XRayFunctionEntry {
  247. const MCSymbol *Sled;
  248. const MCSymbol *Function;
  249. SledKind Kind;
  250. bool AlwaysInstrument;
  251. const class Function *Fn;
  252. uint8_t Version;
  253. void emit(int, MCStreamer *) const;
  254. };
  255. // All the sleds to be emitted.
  256. SmallVector<XRayFunctionEntry, 4> Sleds;
  257. // Helper function to record a given XRay sled.
  258. void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind,
  259. uint8_t Version = 0);
  260. /// Emit a table with all XRay instrumentation points.
  261. void emitXRayTable();
  262. void emitPatchableFunctionEntries();
  263. //===------------------------------------------------------------------===//
  264. // MachineFunctionPass Implementation.
  265. //===------------------------------------------------------------------===//
  266. /// Record analysis usage.
  267. void getAnalysisUsage(AnalysisUsage &AU) const override;
  268. /// Set up the AsmPrinter when we are working on a new module. If your pass
  269. /// overrides this, it must make sure to explicitly call this implementation.
  270. bool doInitialization(Module &M) override;
  271. /// Shut down the asmprinter. If you override this in your pass, you must make
  272. /// sure to call it explicitly.
  273. bool doFinalization(Module &M) override;
  274. /// Emit the specified function out to the OutStreamer.
  275. bool runOnMachineFunction(MachineFunction &MF) override {
  276. SetupMachineFunction(MF);
  277. emitFunctionBody();
  278. return false;
  279. }
  280. //===------------------------------------------------------------------===//
  281. // Coarse grained IR lowering routines.
  282. //===------------------------------------------------------------------===//
  283. /// This should be called when a new MachineFunction is being processed from
  284. /// runOnMachineFunction.
  285. virtual void SetupMachineFunction(MachineFunction &MF);
  286. /// This method emits the body and trailer for a function.
  287. void emitFunctionBody();
  288. void emitCFIInstruction(const MachineInstr &MI);
  289. void emitFrameAlloc(const MachineInstr &MI);
  290. void emitStackSizeSection(const MachineFunction &MF);
  291. void emitStackUsage(const MachineFunction &MF);
  292. void emitBBAddrMapSection(const MachineFunction &MF);
  293. void emitPseudoProbe(const MachineInstr &MI);
  294. void emitRemarksSection(remarks::RemarkStreamer &RS);
  295. /// Get the CFISection type for a function.
  296. CFISection getFunctionCFISectionType(const Function &F) const;
  297. /// Get the CFISection type for a function.
  298. CFISection getFunctionCFISectionType(const MachineFunction &MF) const;
  299. /// Get the CFISection type for the module.
  300. CFISection getModuleCFISectionType() const { return ModuleCFISection; }
  301. bool needsSEHMoves();
  302. /// Since emitting CFI unwind information is entangled with supporting the
  303. /// exceptions, this returns true for platforms which use CFI unwind
  304. /// information for debugging purpose when
  305. /// `MCAsmInfo::ExceptionsType == ExceptionHandling::None`.
  306. bool needsCFIForDebug() const;
  307. /// Print to the current output stream assembly representations of the
  308. /// constants in the constant pool MCP. This is used to print out constants
  309. /// which have been "spilled to memory" by the code generator.
  310. virtual void emitConstantPool();
  311. /// Print assembly representations of the jump tables used by the current
  312. /// function to the current output stream.
  313. virtual void emitJumpTableInfo();
  314. /// Emit the specified global variable to the .s file.
  315. virtual void emitGlobalVariable(const GlobalVariable *GV);
  316. /// Check to see if the specified global is a special global used by LLVM. If
  317. /// so, emit it and return true, otherwise do nothing and return false.
  318. bool emitSpecialLLVMGlobal(const GlobalVariable *GV);
  319. /// `llvm.global_ctors` and `llvm.global_dtors` are arrays of Structor
  320. /// structs.
  321. ///
  322. /// Priority - init priority
  323. /// Func - global initialization or global clean-up function
  324. /// ComdatKey - associated data
  325. struct Structor {
  326. int Priority = 0;
  327. Constant *Func = nullptr;
  328. GlobalValue *ComdatKey = nullptr;
  329. Structor() = default;
  330. };
  331. /// This method gathers an array of Structors and then sorts them out by
  332. /// Priority.
  333. /// @param List The initializer of `llvm.global_ctors` or `llvm.global_dtors`
  334. /// array.
  335. /// @param[out] Structors Sorted Structor structs by Priority.
  336. void preprocessXXStructorList(const DataLayout &DL, const Constant *List,
  337. SmallVector<Structor, 8> &Structors);
  338. /// This method emits `llvm.global_ctors` or `llvm.global_dtors` list.
  339. virtual void emitXXStructorList(const DataLayout &DL, const Constant *List,
  340. bool IsCtor);
  341. /// Emit an alignment directive to the specified power of two boundary. If a
  342. /// global value is specified, and if that global has an explicit alignment
  343. /// requested, it will override the alignment request if required for
  344. /// correctness.
  345. void emitAlignment(Align Alignment, const GlobalObject *GV = nullptr,
  346. unsigned MaxBytesToEmit = 0) const;
  347. /// Lower the specified LLVM Constant to an MCExpr.
  348. virtual const MCExpr *lowerConstant(const Constant *CV);
  349. /// Print a general LLVM constant to the .s file.
  350. void emitGlobalConstant(const DataLayout &DL, const Constant *CV);
  351. /// Unnamed constant global variables solely contaning a pointer to
  352. /// another globals variable act like a global variable "proxy", or GOT
  353. /// equivalents, i.e., it's only used to hold the address of the latter. One
  354. /// optimization is to replace accesses to these proxies by using the GOT
  355. /// entry for the final global instead. Hence, we select GOT equivalent
  356. /// candidates among all the module global variables, avoid emitting them
  357. /// unnecessarily and finally replace references to them by pc relative
  358. /// accesses to GOT entries.
  359. void computeGlobalGOTEquivs(Module &M);
  360. /// Constant expressions using GOT equivalent globals may not be
  361. /// eligible for PC relative GOT entry conversion, in such cases we need to
  362. /// emit the proxies we previously omitted in EmitGlobalVariable.
  363. void emitGlobalGOTEquivs();
  364. /// Emit the stack maps.
  365. void emitStackMaps(StackMaps &SM);
  366. //===------------------------------------------------------------------===//
  367. // Overridable Hooks
  368. //===------------------------------------------------------------------===//
  369. void addAsmPrinterHandler(HandlerInfo Handler) {
  370. Handlers.insert(Handlers.begin(), std::move(Handler));
  371. NumUserHandlers++;
  372. }
  373. // Targets can, or in the case of EmitInstruction, must implement these to
  374. // customize output.
  375. /// This virtual method can be overridden by targets that want to emit
  376. /// something at the start of their file.
  377. virtual void emitStartOfAsmFile(Module &) {}
  378. /// This virtual method can be overridden by targets that want to emit
  379. /// something at the end of their file.
  380. virtual void emitEndOfAsmFile(Module &) {}
  381. /// Targets can override this to emit stuff before the first basic block in
  382. /// the function.
  383. virtual void emitFunctionBodyStart() {}
  384. /// Targets can override this to emit stuff after the last basic block in the
  385. /// function.
  386. virtual void emitFunctionBodyEnd() {}
  387. /// Targets can override this to emit stuff at the start of a basic block.
  388. /// By default, this method prints the label for the specified
  389. /// MachineBasicBlock, an alignment (if present) and a comment describing it
  390. /// if appropriate.
  391. virtual void emitBasicBlockStart(const MachineBasicBlock &MBB);
  392. /// Targets can override this to emit stuff at the end of a basic block.
  393. virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB);
  394. /// Targets should implement this to emit instructions.
  395. virtual void emitInstruction(const MachineInstr *) {
  396. llvm_unreachable("EmitInstruction not implemented");
  397. }
  398. /// Return the symbol for the specified constant pool entry.
  399. virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
  400. virtual void emitFunctionEntryLabel();
  401. virtual void emitFunctionDescriptor() {
  402. llvm_unreachable("Function descriptor is target-specific.");
  403. }
  404. virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
  405. /// Targets can override this to change how global constants that are part of
  406. /// a C++ static/global constructor list are emitted.
  407. virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) {
  408. emitGlobalConstant(DL, CV);
  409. }
  410. /// Return true if the basic block has exactly one predecessor and the control
  411. /// transfer mechanism between the predecessor and this block is a
  412. /// fall-through.
  413. virtual bool
  414. isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
  415. /// Targets can override this to customize the output of IMPLICIT_DEF
  416. /// instructions in verbose mode.
  417. virtual void emitImplicitDef(const MachineInstr *MI) const;
  418. /// Emit N NOP instructions.
  419. void emitNops(unsigned N);
  420. //===------------------------------------------------------------------===//
  421. // Symbol Lowering Routines.
  422. //===------------------------------------------------------------------===//
  423. MCSymbol *createTempSymbol(const Twine &Name) const;
  424. /// Return the MCSymbol for a private symbol with global value name as its
  425. /// base, with the specified suffix.
  426. MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
  427. StringRef Suffix) const;
  428. /// Return the MCSymbol for the specified ExternalSymbol.
  429. MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
  430. /// Return the symbol for the specified jump table entry.
  431. MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
  432. /// Return the symbol for the specified jump table .set
  433. /// FIXME: privatize to AsmPrinter.
  434. MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
  435. /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
  436. /// basic block.
  437. MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
  438. MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
  439. //===------------------------------------------------------------------===//
  440. // Emission Helper Routines.
  441. //===------------------------------------------------------------------===//
  442. /// This is just convenient handler for printing offsets.
  443. void printOffset(int64_t Offset, raw_ostream &OS) const;
  444. /// Emit a byte directive and value.
  445. void emitInt8(int Value) const;
  446. /// Emit a short directive and value.
  447. void emitInt16(int Value) const;
  448. /// Emit a long directive and value.
  449. void emitInt32(int Value) const;
  450. /// Emit a long long directive and value.
  451. void emitInt64(uint64_t Value) const;
  452. /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
  453. /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
  454. /// .set if it is available.
  455. void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
  456. unsigned Size) const;
  457. /// Emit something like ".uleb128 Hi-Lo".
  458. void emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
  459. const MCSymbol *Lo) const;
  460. /// Emit something like ".long Label+Offset" where the size in bytes of the
  461. /// directive is specified by Size and Label specifies the label. This
  462. /// implicitly uses .set if it is available.
  463. void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
  464. unsigned Size, bool IsSectionRelative = false) const;
  465. /// Emit something like ".long Label" where the size in bytes of the directive
  466. /// is specified by Size and Label specifies the label.
  467. void emitLabelReference(const MCSymbol *Label, unsigned Size,
  468. bool IsSectionRelative = false) const {
  469. emitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
  470. }
  471. //===------------------------------------------------------------------===//
  472. // Dwarf Emission Helper Routines
  473. //===------------------------------------------------------------------===//
  474. /// Emit the specified signed leb128 value.
  475. void emitSLEB128(int64_t Value, const char *Desc = nullptr) const;
  476. /// Emit the specified unsigned leb128 value.
  477. void emitULEB128(uint64_t Value, const char *Desc = nullptr,
  478. unsigned PadTo = 0) const;
  479. /// Emit a .byte 42 directive that corresponds to an encoding. If verbose
  480. /// assembly output is enabled, we output comments describing the encoding.
  481. /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
  482. void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
  483. /// Return the size of the encoding in bytes.
  484. unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
  485. /// Emit reference to a ttype global with a specified encoding.
  486. virtual void emitTTypeReference(const GlobalValue *GV, unsigned Encoding);
  487. /// Emit a reference to a symbol for use in dwarf. Different object formats
  488. /// represent this in different ways. Some use a relocation others encode
  489. /// the label offset in its section.
  490. void emitDwarfSymbolReference(const MCSymbol *Label,
  491. bool ForceOffset = false) const;
  492. /// Emit the 4- or 8-byte offset of a string from the start of its section.
  493. ///
  494. /// When possible, emit a DwarfStringPool section offset without any
  495. /// relocations, and without using the symbol. Otherwise, defers to \a
  496. /// emitDwarfSymbolReference().
  497. ///
  498. /// The length of the emitted value depends on the DWARF format.
  499. void emitDwarfStringOffset(DwarfStringPoolEntry S) const;
  500. /// Emit the 4-or 8-byte offset of a string from the start of its section.
  501. void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
  502. emitDwarfStringOffset(S.getEntry());
  503. }
  504. /// Emit something like ".long Label + Offset" or ".quad Label + Offset"
  505. /// depending on the DWARF format.
  506. void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const;
  507. /// Emit 32- or 64-bit value depending on the DWARF format.
  508. void emitDwarfLengthOrOffset(uint64_t Value) const;
  509. /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
  510. /// according to the settings.
  511. void emitDwarfUnitLength(uint64_t Length, const Twine &Comment) const;
  512. /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
  513. /// according to the settings.
  514. /// Return the end symbol generated inside, the caller needs to emit it.
  515. MCSymbol *emitDwarfUnitLength(const Twine &Prefix,
  516. const Twine &Comment) const;
  517. /// Emit reference to a call site with a specified encoding
  518. void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
  519. unsigned Encoding) const;
  520. /// Emit an integer value corresponding to the call site encoding
  521. void emitCallSiteValue(uint64_t Value, unsigned Encoding) const;
  522. /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
  523. virtual unsigned getISAEncoding() { return 0; }
  524. /// Emit the directive and value for debug thread local expression
  525. ///
  526. /// \p Value - The value to emit.
  527. /// \p Size - The size of the integer (in bytes) to emit.
  528. virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const;
  529. //===------------------------------------------------------------------===//
  530. // Dwarf Lowering Routines
  531. //===------------------------------------------------------------------===//
  532. /// Emit frame instruction to describe the layout of the frame.
  533. void emitCFIInstruction(const MCCFIInstruction &Inst) const;
  534. /// Emit Dwarf abbreviation table.
  535. template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
  536. // For each abbreviation.
  537. for (const auto &Abbrev : Abbrevs)
  538. emitDwarfAbbrev(*Abbrev);
  539. // Mark end of abbreviations.
  540. emitULEB128(0, "EOM(3)");
  541. }
  542. void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
  543. /// Recursively emit Dwarf DIE tree.
  544. void emitDwarfDIE(const DIE &Die) const;
  545. //===------------------------------------------------------------------===//
  546. // Inline Asm Support
  547. //===------------------------------------------------------------------===//
  548. // These are hooks that targets can override to implement inline asm
  549. // support. These should probably be moved out of AsmPrinter someday.
  550. /// Print information related to the specified machine instr that is
  551. /// independent of the operand, and may be independent of the instr itself.
  552. /// This can be useful for portably encoding the comment character or other
  553. /// bits of target-specific knowledge into the asmstrings. The syntax used is
  554. /// ${:comment}. Targets can override this to add support for their own
  555. /// strange codes.
  556. virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
  557. StringRef Code) const;
  558. /// Print the MachineOperand as a symbol. Targets with complex handling of
  559. /// symbol references should override the base implementation.
  560. virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS);
  561. /// Print the specified operand of MI, an INLINEASM instruction, using the
  562. /// specified assembler variant. Targets should override this to format as
  563. /// appropriate. This method can return true if the operand is erroneous.
  564. virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
  565. const char *ExtraCode, raw_ostream &OS);
  566. /// Print the specified operand of MI, an INLINEASM instruction, using the
  567. /// specified assembler variant as an address. Targets should override this to
  568. /// format as appropriate. This method can return true if the operand is
  569. /// erroneous.
  570. virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
  571. const char *ExtraCode, raw_ostream &OS);
  572. /// Let the target do anything it needs to do before emitting inlineasm.
  573. /// \p StartInfo - the subtarget info before parsing inline asm
  574. virtual void emitInlineAsmStart() const;
  575. /// Let the target do anything it needs to do after emitting inlineasm.
  576. /// This callback can be used restore the original mode in case the
  577. /// inlineasm contains directives to switch modes.
  578. /// \p StartInfo - the original subtarget info before inline asm
  579. /// \p EndInfo - the final subtarget info after parsing the inline asm,
  580. /// or NULL if the value is unknown.
  581. virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
  582. const MCSubtargetInfo *EndInfo) const;
  583. /// This emits visibility information about symbol, if this is supported by
  584. /// the target.
  585. void emitVisibility(MCSymbol *Sym, unsigned Visibility,
  586. bool IsDefinition = true) const;
  587. /// This emits linkage information about \p GVSym based on \p GV, if this is
  588. /// supported by the target.
  589. virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
  590. /// Return the alignment for the specified \p GV.
  591. static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
  592. Align InAlign = Align(1));
  593. private:
  594. /// Private state for PrintSpecial()
  595. // Assign a unique ID to this machine instruction.
  596. mutable const MachineInstr *LastMI = nullptr;
  597. mutable unsigned LastFn = 0;
  598. mutable unsigned Counter = ~0U;
  599. /// This method emits the header for the current function.
  600. virtual void emitFunctionHeader();
  601. /// This method emits a comment next to header for the current function.
  602. virtual void emitFunctionHeaderComment();
  603. /// Emit a blob of inline asm to the output streamer.
  604. void
  605. emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
  606. const MCTargetOptions &MCOptions,
  607. const MDNode *LocMDNode = nullptr,
  608. InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
  609. /// This method formats and emits the specified machine instruction that is an
  610. /// inline asm.
  611. void emitInlineAsm(const MachineInstr *MI) const;
  612. /// Add inline assembly info to the diagnostics machinery, so we can
  613. /// emit file and position info. Returns SrcMgr memory buffer position.
  614. unsigned addInlineAsmDiagBuffer(StringRef AsmStr,
  615. const MDNode *LocMDNode) const;
  616. //===------------------------------------------------------------------===//
  617. // Internal Implementation Details
  618. //===------------------------------------------------------------------===//
  619. void emitJumpTableEntry(const MachineJumpTableInfo *MJTI,
  620. const MachineBasicBlock *MBB, unsigned uid) const;
  621. void emitLLVMUsedList(const ConstantArray *InitList);
  622. /// Emit llvm.ident metadata in an '.ident' directive.
  623. void emitModuleIdents(Module &M);
  624. /// Emit bytes for llvm.commandline metadata.
  625. void emitModuleCommandLines(Module &M);
  626. GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S);
  627. void emitGlobalAlias(Module &M, const GlobalAlias &GA);
  628. void emitGlobalIFunc(Module &M, const GlobalIFunc &GI);
  629. /// This method decides whether the specified basic block requires a label.
  630. bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const;
  631. protected:
  632. virtual bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const {
  633. return false;
  634. }
  635. };
  636. } // end namespace llvm
  637. #endif // LLVM_CODEGEN_ASMPRINTER_H
  638. #ifdef __GNUC__
  639. #pragma GCC diagnostic pop
  640. #endif