AsmPrinter.h 30 KB

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