AsmPrinter.h revision 360784
124143Sjoerg//===- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework ---------*- C++ -*-===//
224143Sjoerg//
324143Sjoerg// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
447901Sn_hibma// See https://llvm.org/LICENSE.txt for license information.
524143Sjoerg// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
624143Sjoerg//
724143Sjoerg//===----------------------------------------------------------------------===//
824143Sjoerg//
938278Swosch// This file contains a class to be used as the base class for target specific
1038278Swosch// asm writers.  This class primarily handles common functionality used by
1124143Sjoerg// all asm writers.
1224143Sjoerg//
1324143Sjoerg//===----------------------------------------------------------------------===//
1447901Sn_hibma
1524143Sjoerg#ifndef LLVM_CODEGEN_ASMPRINTER_H
1624143Sjoerg#define LLVM_CODEGEN_ASMPRINTER_H
1724143Sjoerg
1824143Sjoerg#include "llvm/ADT/MapVector.h"
1924143Sjoerg#include "llvm/ADT/SmallVector.h"
2024143Sjoerg#include "llvm/ADT/StringRef.h"
2172951Srwatson#include "llvm/ADT/Twine.h"
2224143Sjoerg#include "llvm/CodeGen/AsmPrinterHandler.h"
2350477Speter#include "llvm/CodeGen/DwarfStringPoolEntry.h"
2424143Sjoerg#include "llvm/CodeGen/MachineFunctionPass.h"
2524143Sjoerg#include "llvm/IR/InlineAsm.h"
2624143Sjoerg#include "llvm/IR/LLVMContext.h"
2724143Sjoerg#include "llvm/Support/ErrorHandling.h"
28131621Sdes#include "llvm/Support/SourceMgr.h"
29131621Sdes#include <cstdint>
30131621Sdes#include <memory>
31131621Sdes#include <utility>
32131621Sdes#include <vector>
3324143Sjoerg
3424143Sjoergnamespace llvm {
3524143Sjoerg
3624143Sjoergclass BasicBlock;
3724143Sjoergclass BlockAddress;
38131621Sdesclass Constant;
39131621Sdesclass ConstantArray;
40131621Sdesclass DataLayout;
41131626Sdesclass DIE;
42131621Sdesclass DIEAbbrev;
43131621Sdesclass DwarfDebug;
4424143Sjoergclass GCMetadataPrinter;
45144636Sstefanfclass GCStrategy;
46144636Sstefanfclass GlobalIndirectSymbol;
4772951Srwatsonclass GlobalObject;
48168710Sstasclass GlobalValue;
4924143Sjoergclass GlobalVariable;
5024143Sjoergclass MachineBasicBlock;
5124143Sjoergclass MachineBlockFrequencyInfo;
5269375Sjhbclass MachineConstantPoolValue;
5372951Srwatsonclass MachineDominatorTree;
5424143Sjoergclass MachineFunction;
5572951Srwatsonclass MachineInstr;
56146291Sobrienclass MachineJumpTableInfo;
57146291Sobrienclass MachineLoopInfo;
5872951Srwatsonclass MachineModuleInfo;
59145073Skeramidaclass MachineOptimizationRemarkEmitter;
6092922Simpclass MCAsmInfo;
6127340Speterclass MCCFIInstruction;
62131402Salfredclass MCContext;
63146291Sobrienclass MCExpr;
6469375Sjhbclass MCInst;
6524143Sjoergclass MCSection;
6672951Srwatsonclass MCStreamer;
6792922Simpclass MCSubtargetInfo;
6824143Sjoergclass MCSymbol;
6924143Sjoergclass MCTargetOptions;
7024143Sjoergclass MDNode;
71158280Sbdeclass Module;
72131623Sdesclass ProfileSummaryInfo;
73131623Sdesclass raw_ostream;
7424143Sjoergclass RemarkStreamer;
7524143Sjoergclass StackMaps;
7624143Sjoergclass TargetLoweringObjectFile;
7724143Sjoergclass TargetMachine;
7824143Sjoerg
7924143Sjoerg/// This class is intended to be used as a driving class for all asm writers.
8069896Smckusickclass AsmPrinter : public MachineFunctionPass {
8169896Smckusickpublic:
8224143Sjoerg  /// Target machine description.
8324143Sjoerg  TargetMachine &TM;
8469896Smckusick
8524143Sjoerg  /// Target Asm Printer information.
86131402Salfred  const MCAsmInfo *MAI;
87131402Salfred
88131402Salfred  /// This is the context for the output file that we are streaming. This owns
89131402Salfred  /// all of the global MC-related objects for the generated translation unit.
90131402Salfred  MCContext &OutContext;
9124143Sjoerg
9224143Sjoerg  /// This is the MCStreamer object for the file we are generating. This
9324143Sjoerg  /// contains the transient state for the current translation unit that we are
9424143Sjoerg  /// generating (such as the current section etc).
9524143Sjoerg  std::unique_ptr<MCStreamer> OutStreamer;
9624143Sjoerg
97131402Salfred  /// The current machine function.
98168799Srafan  MachineFunction *MF = nullptr;
99131402Salfred
100131402Salfred  /// This is a pointer to the current MachineModuleInfo.
101168799Srafan  MachineModuleInfo *MMI = nullptr;
102131402Salfred
103145073Skeramida  /// This is a pointer to the current MachineLoopInfo.
104168799Srafan  MachineDominatorTree *MDT = nullptr;
10527340Speter
106168799Srafan  /// This is a pointer to the current MachineLoopInfo.
10724143Sjoerg  MachineLoopInfo *MLI = nullptr;
10827340Speter
109168799Srafan  /// Optimization remark emitter.
11024143Sjoerg  MachineOptimizationRemarkEmitter *ORE;
111145073Skeramida
112168799Srafan  MachineBlockFrequencyInfo *MBFI;
11327340Speter
114168799Srafan  ProfileSummaryInfo *PSI;
11524143Sjoerg
11627340Speter  /// The symbol for the entry in __patchable_function_entires.
117168799Srafan  MCSymbol *CurrentPatchableFunctionEntrySym = nullptr;
11824143Sjoerg
11924143Sjoerg  /// The symbol for the current function. This is recalculated at the beginning
12024143Sjoerg  /// of each call to runOnMachineFunction().
12124143Sjoerg  MCSymbol *CurrentFnSym = nullptr;
12224143Sjoerg
12324143Sjoerg  /// The symbol for the current function descriptor on AIX. This is created
124158280Sbde  /// at the beginning of each call to SetupMachineFunction().
125131623Sdes  MCSymbol *CurrentFnDescSym = nullptr;
12624143Sjoerg
12724143Sjoerg  /// The symbol used to represent the start of the current function for the
12824143Sjoerg  /// purpose of calculating its size (e.g. using the .size directive). By
12924143Sjoerg  /// default, this is equal to CurrentFnSym.
13024143Sjoerg  MCSymbol *CurrentFnSymForSize = nullptr;
13124143Sjoerg
13224143Sjoerg  /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
13324143Sjoerg  /// its number of uses by other globals.
13424143Sjoerg  using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;
13524143Sjoerg  MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
13624143Sjoerg
13724143Sjoergprivate:
13824143Sjoerg  MCSymbol *CurrentFnEnd = nullptr;
13972951Srwatson  MCSymbol *CurExceptionSym = nullptr;
14024143Sjoerg
14172951Srwatson  // The garbage collection metadata printer table.
14224143Sjoerg  void *GCMetadataPrinters = nullptr; // Really a DenseMap.
14324143Sjoerg
14424143Sjoerg  /// Emit comments in assembly output if this is true.
14524143Sjoerg  bool VerboseAsm;
14624143Sjoerg
14724143Sjoerg  static char ID;
14824143Sjoerg
14924143Sjoergprotected:
15024143Sjoerg  MCSymbol *CurrentFnBegin = nullptr;
15165557Sjasone
15224143Sjoerg  /// Protected struct HandlerInfo and Handlers permit target extended
153131623Sdes  /// AsmPrinter adds their own handlers.
154131623Sdes  struct HandlerInfo {
155131623Sdes    std::unique_ptr<AsmPrinterHandler> Handler;
15624143Sjoerg    const char *TimerName;
15724143Sjoerg    const char *TimerDescription;
15824143Sjoerg    const char *TimerGroupName;
15924143Sjoerg    const char *TimerGroupDescription;
16024143Sjoerg
16124143Sjoerg    HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler,
162131623Sdes                const char *TimerName, const char *TimerDescription,
16324143Sjoerg                const char *TimerGroupName, const char *TimerGroupDescription)
16424143Sjoerg        : Handler(std::move(Handler)), TimerName(TimerName),
16524143Sjoerg          TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
16624143Sjoerg          TimerGroupDescription(TimerGroupDescription) {}
16724143Sjoerg  };
16824143Sjoerg
169158280Sbde  /// A vector of all debug/EH info emitters we should use. This vector
170158280Sbde  /// maintains ownership of the emitters.
17124143Sjoerg  SmallVector<HandlerInfo, 1> Handlers;
17224143Sjoerg
17324143Sjoergpublic:
17424143Sjoerg  struct SrcMgrDiagInfo {
175131623Sdes    SourceMgr SrcMgr;
176131623Sdes    std::vector<const MDNode *> LocInfos;
17724143Sjoerg    LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
17824143Sjoerg    void *DiagContext;
17924143Sjoerg  };
18024143Sjoerg
18124143Sjoergprivate:
18224143Sjoerg  /// If generated on the fly this own the instance.
18324143Sjoerg  std::unique_ptr<MachineDominatorTree> OwnedMDT;
18424143Sjoerg
18524143Sjoerg  /// If generated on the fly this own the instance.
18624143Sjoerg  std::unique_ptr<MachineLoopInfo> OwnedMLI;
187131402Salfred
188131402Salfred  /// Structure for generating diagnostics for inline assembly. Only initialised
189131402Salfred  /// when necessary.
190131402Salfred  mutable std::unique_ptr<SrcMgrDiagInfo> DiagInfo;
19124143Sjoerg
192131412Salfred  /// If the target supports dwarf debug info, this pointer is non-null.
193131412Salfred  DwarfDebug *DD = nullptr;
194131412Salfred
195131412Salfred  /// If the current module uses dwarf CFI annotations strictly for debugging.
196131412Salfred  bool isCFIMoveForDebugging = false;
19724143Sjoerg
19824143Sjoergprotected:
19924143Sjoerg  explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
20024143Sjoerg
20124143Sjoergpublic:
20224143Sjoerg  ~AsmPrinter() override;
20324143Sjoerg
20424143Sjoerg  DwarfDebug *getDwarfDebug() { return DD; }
20524143Sjoerg  DwarfDebug *getDwarfDebug() const { return DD; }
20624143Sjoerg
20724143Sjoerg  uint16_t getDwarfVersion() const;
20838278Swosch  void setDwarfVersion(uint16_t Version);
209131829Skeramida
210145073Skeramida  bool isPositionIndependent() const;
211131829Skeramida
212133817Salfred  /// Return true if assembly output should contain comments.
213145073Skeramida  bool isVerbose() const { return VerboseAsm; }
214168799Srafan
215168799Srafan  /// Return a unique ID for the current function.
21638278Swosch  unsigned getFunctionNumber() const;
21738278Swosch
21838278Swosch  /// Return symbol for the function pseudo stack if the stack frame is not a
219168799Srafan  /// register based.
220158282Sbde  virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; }
221158282Sbde
222158282Sbde  MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
223158282Sbde  MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
224158282Sbde  MCSymbol *getCurExceptionSym();
22524143Sjoerg
226131622Sdes  /// Return information about object file lowering.
22724143Sjoerg  const TargetLoweringObjectFile &getObjFileLowering() const;
228131623Sdes
229131623Sdes  /// Return information about data layout.
230131623Sdes  const DataLayout &getDataLayout() const;
23124143Sjoerg
232131623Sdes  /// Return the pointer size from the TargetMachine
233158280Sbde  unsigned getPointerSize() const;
234158280Sbde
235158280Sbde  /// Return information about subtarget.
236158280Sbde  const MCSubtargetInfo &getSubtargetInfo() const;
237131623Sdes
238131623Sdes  void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
23927340Speter
240131623Sdes  /// Emits inital debug location directive.
241131623Sdes  void emitInitialRawDwarfLocDirective(const MachineFunction &MF);
242131623Sdes
243131623Sdes  /// Return the current section we are emitting to.
244146291Sobrien  const MCSection *getCurrentSection() const;
245146291Sobrien
246146291Sobrien  void getNameWithPrefix(SmallVectorImpl<char> &Name,
247146291Sobrien                         const GlobalValue *GV) const;
24827390Speter
249131626Sdes  MCSymbol *getSymbol(const GlobalValue *GV) const;
250131626Sdes
251131626Sdes  //===------------------------------------------------------------------===//
25224143Sjoerg  // XRay instrumentation implementation.
253131623Sdes  //===------------------------------------------------------------------===//
25424143Sjoergpublic:
255131623Sdes  // This describes the kind of sled we're storing in the XRay table.
256131623Sdes  enum class SledKind : uint8_t {
25724143Sjoerg    FUNCTION_ENTER = 0,
258131623Sdes    FUNCTION_EXIT = 1,
259131623Sdes    TAIL_CALL = 2,
260131623Sdes    LOG_ARGS_ENTER = 3,
261131623Sdes    CUSTOM_EVENT = 4,
262158280Sbde    TYPED_EVENT = 5,
263158280Sbde  };
264131623Sdes
265131623Sdes  // The table will contain these structs that point to the sled, the function
266131626Sdes  // containing the sled, and what kind of sled (and whether they should always
267131623Sdes  // be instrumented). We also use a version identifier that the runtime can use
268131623Sdes  // to decide what to do with the sled, depending on the version of the sled.
269131623Sdes  struct XRayFunctionEntry {
27024143Sjoerg    const MCSymbol *Sled;
271131623Sdes    const MCSymbol *Function;
272131623Sdes    SledKind Kind;
27324143Sjoerg    bool AlwaysInstrument;
274131623Sdes    const class Function *Fn;
275131623Sdes    uint8_t Version;
276131623Sdes
277131623Sdes    void emit(int, MCStreamer *, const MCSymbol *) const;
278131623Sdes  };
27938278Swosch
280133817Salfred  // All the sleds to be emitted.
28138278Swosch  SmallVector<XRayFunctionEntry, 4> Sleds;
28224143Sjoerg
283131623Sdes  // A unique ID used for ELF sections associated with a particular function.
284131623Sdes  unsigned XRayFnUniqueID = 0;
28524143Sjoerg
28624143Sjoerg  // Helper function to record a given XRay sled.
287131310Salfred  void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind,
288131622Sdes                  uint8_t Version = 0);
28924143Sjoerg
290131623Sdes  /// Emit a table with all XRay instrumentation points.
291131623Sdes  void emitXRayTable();
29224143Sjoerg
293131623Sdes  DenseMap<const MCSection *, unsigned> PatchableFunctionEntryID;
294131623Sdes  void emitPatchableFunctionEntries();
295145073Skeramida
296145073Skeramida  //===------------------------------------------------------------------===//
297145073Skeramida  // MachineFunctionPass Implementation.
298145073Skeramida  //===------------------------------------------------------------------===//
299145073Skeramida
300145073Skeramida  /// Record analysis usage.
301145073Skeramida  void getAnalysisUsage(AnalysisUsage &AU) const override;
302145073Skeramida
303145073Skeramida  /// Set up the AsmPrinter when we are working on a new module. If your pass
304146342Skeramida  /// overrides this, it must make sure to explicitly call this implementation.
305168799Srafan  bool doInitialization(Module &M) override;
306146342Skeramida
307146342Skeramida  /// Shut down the asmprinter. If you override this in your pass, you must make
308131623Sdes  /// sure to call it explicitly.
309131623Sdes  bool doFinalization(Module &M) override;
310131623Sdes
311146342Skeramida  /// Emit the specified function out to the OutStreamer.
312168799Srafan  bool runOnMachineFunction(MachineFunction &MF) override {
313146342Skeramida    SetupMachineFunction(MF);
314131623Sdes    EmitFunctionBody();
315131623Sdes    return false;
316131623Sdes  }
317131623Sdes
31824143Sjoerg  //===------------------------------------------------------------------===//
31924143Sjoerg  // Coarse grained IR lowering routines.
32024143Sjoerg  //===------------------------------------------------------------------===//
32124143Sjoerg
32224143Sjoerg  /// This should be called when a new MachineFunction is being processed from
32324143Sjoerg  /// runOnMachineFunction.
32424143Sjoerg  virtual void SetupMachineFunction(MachineFunction &MF);
325131622Sdes
32624143Sjoerg  /// This method emits the body and trailer for a function.
327131623Sdes  void EmitFunctionBody();
328131623Sdes
329131623Sdes  void emitCFIInstruction(const MachineInstr &MI);
330131623Sdes
331131623Sdes  void emitFrameAlloc(const MachineInstr &MI);
332131626Sdes
33324143Sjoerg  void emitStackSizeSection(const MachineFunction &MF);
334131623Sdes
335131623Sdes  void emitRemarksSection(RemarkStreamer &RS);
336131623Sdes
337131623Sdes  enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
33824143Sjoerg  CFIMoveType needsCFIMoves() const;
339131623Sdes
340131626Sdes  /// Returns false if needsCFIMoves() == CFI_M_EH for any function
341131626Sdes  /// in the module.
34224143Sjoerg  bool needsOnlyDebugCFIMoves() const { return isCFIMoveForDebugging; }
343131623Sdes
344131623Sdes  bool needsSEHMoves();
34524143Sjoerg
346131623Sdes  /// Print to the current output stream assembly representations of the
347131623Sdes  /// constants in the constant pool MCP. This is used to print out constants
348131623Sdes  /// which have been "spilled to memory" by the code generator.
349131623Sdes  virtual void EmitConstantPool();
350131623Sdes
351131623Sdes  /// Print assembly representations of the jump tables used by the current
352131623Sdes  /// function to the current output stream.
35324143Sjoerg  virtual void EmitJumpTableInfo();
354131623Sdes
355131623Sdes  /// Emit the specified global variable to the .s file.
356131623Sdes  virtual void EmitGlobalVariable(const GlobalVariable *GV);
357131623Sdes
358131623Sdes  /// Check to see if the specified global is a special global used by LLVM. If
359131623Sdes  /// so, emit it and return true, otherwise do nothing and return false.
360131623Sdes  bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
361131623Sdes
362131623Sdes  /// Emit an alignment directive to the specified power of two boundary. If a
363131623Sdes  /// global value is specified, and if that global has an explicit alignment
364131623Sdes  /// requested, it will override the alignment request if required for
365131623Sdes  /// correctness.
366131623Sdes  void EmitAlignment(Align Alignment, const GlobalObject *GV = nullptr) const;
367131623Sdes
368131623Sdes  /// Lower the specified LLVM Constant to an MCExpr.
369131623Sdes  virtual const MCExpr *lowerConstant(const Constant *CV);
37024143Sjoerg
371131623Sdes  /// Print a general LLVM constant to the .s file.
372131623Sdes  void EmitGlobalConstant(const DataLayout &DL, const Constant *CV);
373131623Sdes
374131623Sdes  /// Unnamed constant global variables solely contaning a pointer to
375131623Sdes  /// another globals variable act like a global variable "proxy", or GOT
37624143Sjoerg  /// equivalents, i.e., it's only used to hold the address of the latter. One
377131623Sdes  /// optimization is to replace accesses to these proxies by using the GOT
378131623Sdes  /// entry for the final global instead. Hence, we select GOT equivalent
379131623Sdes  /// candidates among all the module global variables, avoid emitting them
380131623Sdes  /// unnecessarily and finally replace references to them by pc relative
381131623Sdes  /// accesses to GOT entries.
38224143Sjoerg  void computeGlobalGOTEquivs(Module &M);
383131623Sdes
384131623Sdes  /// Constant expressions using GOT equivalent globals may not be
38524143Sjoerg  /// eligible for PC relative GOT entry conversion, in such cases we need to
386131623Sdes  /// emit the proxies we previously omitted in EmitGlobalVariable.
387131623Sdes  void emitGlobalGOTEquivs();
388131623Sdes
389131623Sdes  /// Emit the stack maps.
390131623Sdes  void emitStackMaps(StackMaps &SM);
391131623Sdes
392131623Sdes  //===------------------------------------------------------------------===//
393131623Sdes  // Overridable Hooks
394131623Sdes  //===------------------------------------------------------------------===//
39524143Sjoerg
39624143Sjoerg  // Targets can, or in the case of EmitInstruction, must implement these to
397131623Sdes  // customize output.
398131623Sdes
399131623Sdes  /// This virtual method can be overridden by targets that want to emit
400131623Sdes  /// something at the start of their file.
40124143Sjoerg  virtual void EmitStartOfAsmFile(Module &) {}
40224143Sjoerg
403131623Sdes  /// This virtual method can be overridden by targets that want to emit
404131623Sdes  /// something at the end of their file.
405131623Sdes  virtual void EmitEndOfAsmFile(Module &) {}
406131623Sdes
407131623Sdes  /// Targets can override this to emit stuff before the first basic block in
40842447Sobrien  /// the function.
409131623Sdes  virtual void EmitFunctionBodyStart() {}
410131623Sdes
411131623Sdes  /// Targets can override this to emit stuff after the last basic block in the
412131623Sdes  /// function.
413131623Sdes  virtual void EmitFunctionBodyEnd() {}
414131623Sdes
415131623Sdes  /// Targets can override this to emit stuff at the start of a basic block.
416131623Sdes  /// By default, this method prints the label for the specified
417131623Sdes  /// MachineBasicBlock, an alignment (if present) and a comment describing it
418131623Sdes  /// if appropriate.
419131623Sdes  virtual void EmitBasicBlockStart(const MachineBasicBlock &MBB);
420131623Sdes
421131623Sdes  /// Targets can override this to emit stuff at the end of a basic block.
42224143Sjoerg  virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB);
42324143Sjoerg
424132015Salfred  /// Targets should implement this to emit instructions.
425132015Salfred  virtual void EmitInstruction(const MachineInstr *) {
426132955Salfred    llvm_unreachable("EmitInstruction not implemented");
427132955Salfred  }
428132955Salfred
429132955Salfred  /// Return the symbol for the specified constant pool entry.
430132955Salfred  virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
431132955Salfred
432132955Salfred  virtual void EmitFunctionEntryLabel();
433132955Salfred
434132955Salfred  virtual void EmitFunctionDescriptor() {
435131402Salfred    llvm_unreachable("Function descriptor is target-specific.");
436131402Salfred  }
437131402Salfred
438131402Salfred  virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
439131402Salfred
440132955Salfred  /// Targets can override this to change how global constants that are part of
441132955Salfred  /// a C++ static/global constructor list are emitted.
442132955Salfred  virtual void EmitXXStructor(const DataLayout &DL, const Constant *CV) {
443132955Salfred    EmitGlobalConstant(DL, CV);
444131402Salfred  }
445131402Salfred
446132955Salfred  /// Return true if the basic block has exactly one predecessor and the control
447132015Salfred  /// transfer mechanism between the predecessor and this block is a
448132015Salfred  /// fall-through.
449132955Salfred  virtual bool
450132015Salfred  isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
451132015Salfred
452132955Salfred  /// Targets can override this to customize the output of IMPLICIT_DEF
453132955Salfred  /// instructions in verbose mode.
454132955Salfred  virtual void emitImplicitDef(const MachineInstr *MI) const;
455132955Salfred
456132955Salfred  /// Emit N NOP instructions.
457132955Salfred  void emitNops(unsigned N);
458132955Salfred
459131402Salfred  //===------------------------------------------------------------------===//
460131626Sdes  // Symbol Lowering Routines.
461132015Salfred  //===------------------------------------------------------------------===//
462132015Salfred
463131402Salfred  MCSymbol *createTempSymbol(const Twine &Name) const;
464132015Salfred
465131402Salfred  /// Return the MCSymbol for a private symbol with global value name as its
466132015Salfred  /// base, with the specified suffix.
467132015Salfred  MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
468131402Salfred                                         StringRef Suffix) const;
469132015Salfred
470132015Salfred  /// Return the MCSymbol for the specified ExternalSymbol.
471131402Salfred  MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
472131402Salfred
473131402Salfred  /// Return the symbol for the specified jump table entry.
474132955Salfred  MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
475132955Salfred
476132955Salfred  /// Return the symbol for the specified jump table .set
477132955Salfred  /// FIXME: privatize to AsmPrinter.
478131402Salfred  MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
479158280Sbde
480158280Sbde  /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
481131402Salfred  /// basic block.
482131402Salfred  MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
483131402Salfred  MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
484131402Salfred
485131402Salfred  //===------------------------------------------------------------------===//
486131402Salfred  // Emission Helper Routines.
487131402Salfred  //===------------------------------------------------------------------===//
488131402Salfred
489131402Salfred  /// This is just convenient handler for printing offsets.
490131402Salfred  void printOffset(int64_t Offset, raw_ostream &OS) const;
491131412Salfred
492131412Salfred  /// Emit a byte directive and value.
493131412Salfred  void emitInt8(int Value) const;
494133817Salfred
495133817Salfred  /// Emit a short directive and value.
496131402Salfred  void emitInt16(int Value) const;
497131402Salfred
498131402Salfred  /// Emit a long directive and value.
499131402Salfred  void emitInt32(int Value) const;
500131402Salfred
501131402Salfred  /// Emit a long long directive and value.
502131402Salfred  void emitInt64(uint64_t Value) const;
503132955Salfred
504132955Salfred  /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
505132955Salfred  /// is specified by Size and Hi/Lo specify the labels.  This implicitly uses
506131412Salfred  /// .set if it is available.
507131412Salfred  void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
508131412Salfred                           unsigned Size) const;
509131412Salfred
510131412Salfred  /// Emit something like ".uleb128 Hi-Lo".
511133817Salfred  void EmitLabelDifferenceAsULEB128(const MCSymbol *Hi,
512131412Salfred                                    const MCSymbol *Lo) const;
513131412Salfred
51424143Sjoerg  /// Emit something like ".long Label+Offset" where the size in bytes of the
51524143Sjoerg  /// directive is specified by Size and Label specifies the label.  This
516131310Salfred  /// implicitly uses .set if it is available.
517131622Sdes  void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
518131626Sdes                           unsigned Size, bool IsSectionRelative = false) const;
51924143Sjoerg
520131623Sdes  /// Emit something like ".long Label" where the size in bytes of the directive
521131623Sdes  /// is specified by Size and Label specifies the label.
522131623Sdes  void EmitLabelReference(const MCSymbol *Label, unsigned Size,
523133817Salfred                          bool IsSectionRelative = false) const {
524131623Sdes    EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
525131623Sdes  }
526131623Sdes
527131623Sdes  /// Emit something like ".long Label + Offset".
52824143Sjoerg  void EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const;
529131623Sdes
530131623Sdes  //===------------------------------------------------------------------===//
531131623Sdes  // Dwarf Emission Helper Routines
532131623Sdes  //===------------------------------------------------------------------===//
533131623Sdes
534131623Sdes  /// Emit the specified signed leb128 value.
53524143Sjoerg  void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
536131623Sdes
537131623Sdes  /// Emit the specified unsigned leb128 value.
538131623Sdes  void EmitULEB128(uint64_t Value, const char *Desc = nullptr, unsigned PadTo = 0) const;
539131623Sdes
540131623Sdes  /// Emit a .byte 42 directive that corresponds to an encoding.  If verbose
541131626Sdes  /// assembly output is enabled, we output comments describing the encoding.
542131623Sdes  /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
543131626Sdes  void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
544131623Sdes
545131623Sdes  /// Return the size of the encoding in bytes.
546131623Sdes  unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
547131623Sdes
548131623Sdes  /// Emit reference to a ttype global with a specified encoding.
549131623Sdes  void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
550131623Sdes
551131623Sdes  /// Emit a reference to a symbol for use in dwarf. Different object formats
552131623Sdes  /// represent this in different ways. Some use a relocation others encode
553131626Sdes  /// the label offset in its section.
554158280Sbde  void emitDwarfSymbolReference(const MCSymbol *Label,
555158280Sbde                                bool ForceOffset = false) const;
556131623Sdes
557131623Sdes  /// Emit the 4-byte offset of a string from the start of its section.
558131402Salfred  ///
559131623Sdes  /// When possible, emit a DwarfStringPool section offset without any
560131623Sdes  /// relocations, and without using the symbol.  Otherwise, defers to \a
561131626Sdes  /// emitDwarfSymbolReference().
562131623Sdes  void emitDwarfStringOffset(DwarfStringPoolEntry S) const;
563131623Sdes
564131623Sdes  /// Emit the 4-byte offset of a string from the start of its section.
565131623Sdes  void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
566131623Sdes    emitDwarfStringOffset(S.getEntry());
567131623Sdes  }
56824143Sjoerg
569131623Sdes  /// Emit reference to a call site with a specified encoding
570131623Sdes  void EmitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
571132024Sdes                          unsigned Encoding) const;
572131623Sdes  /// Emit an integer value corresponding to the call site encoding
573131623Sdes  void EmitCallSiteValue(uint64_t Value, unsigned Encoding) const;
574131623Sdes
57524143Sjoerg  /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
576131623Sdes  virtual unsigned getISAEncoding() { return 0; }
577131623Sdes
578131623Sdes  /// Emit the directive and value for debug thread local expression
579131623Sdes  ///
580131623Sdes  /// \p Value - The value to emit.
581131623Sdes  /// \p Size - The size of the integer (in bytes) to emit.
582131623Sdes  virtual void EmitDebugValue(const MCExpr *Value, unsigned Size) const;
583131623Sdes
584131626Sdes  //===------------------------------------------------------------------===//
585131619Sdes  // Dwarf Lowering Routines
586131623Sdes  //===------------------------------------------------------------------===//
587132024Sdes
588132024Sdes  /// Emit frame instruction to describe the layout of the frame.
589131619Sdes  void emitCFIInstruction(const MCCFIInstruction &Inst) const;
590131623Sdes
591131623Sdes  /// Emit Dwarf abbreviation table.
592131623Sdes  template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
593131619Sdes    // For each abbreviation.
594131623Sdes    for (const auto &Abbrev : Abbrevs)
595131623Sdes      emitDwarfAbbrev(*Abbrev);
596131623Sdes
597131619Sdes    // Mark end of abbreviations.
598158280Sbde    EmitULEB128(0, "EOM(3)");
599158280Sbde  }
600131623Sdes
601131623Sdes  void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
602131623Sdes
603131623Sdes  /// Recursively emit Dwarf DIE tree.
604131626Sdes  void emitDwarfDIE(const DIE &Die) const;
605131619Sdes
606131623Sdes  //===------------------------------------------------------------------===//
607131623Sdes  // Inline Asm Support
608131623Sdes  //===------------------------------------------------------------------===//
609131619Sdes
610131623Sdes  // These are hooks that targets can override to implement inline asm
611159520Sse  // support.  These should probably be moved out of AsmPrinter someday.
612159520Sse
613131623Sdes  /// Print information related to the specified machine instr that is
614131623Sdes  /// independent of the operand, and may be independent of the instr itself.
615131619Sdes  /// This can be useful for portably encoding the comment character or other
616131623Sdes  /// bits of target-specific knowledge into the asmstrings.  The syntax used is
617131623Sdes  /// ${:comment}.  Targets can override this to add support for their own
618131623Sdes  /// strange codes.
619131619Sdes  virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
620131623Sdes                            const char *Code) const;
621158280Sbde
622131623Sdes  /// Print the MachineOperand as a symbol. Targets with complex handling of
623131619Sdes  /// symbol references should override the base implementation.
624131623Sdes  virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS);
625131623Sdes
626131623Sdes  /// Print the specified operand of MI, an INLINEASM instruction, using the
627131623Sdes  /// specified assembler variant.  Targets should override this to format as
628131623Sdes  /// appropriate.  This method can return true if the operand is erroneous.
629131623Sdes  virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
630131626Sdes                               const char *ExtraCode, raw_ostream &OS);
631131626Sdes
632131623Sdes  /// Print the specified operand of MI, an INLINEASM instruction, using the
633131623Sdes  /// specified assembler variant as an address. Targets should override this to
634131623Sdes  /// format as appropriate.  This method can return true if the operand is
635131623Sdes  /// erroneous.
636131623Sdes  virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
637131623Sdes                                     const char *ExtraCode, raw_ostream &OS);
638131623Sdes
639131623Sdes  /// Let the target do anything it needs to do before emitting inlineasm.
640131623Sdes  /// \p StartInfo - the subtarget info before parsing inline asm
641131623Sdes  virtual void emitInlineAsmStart() const;
642131626Sdes
64324143Sjoerg  /// Let the target do anything it needs to do after emitting inlineasm.
644131623Sdes  /// This callback can be used restore the original mode in case the
645131623Sdes  /// inlineasm contains directives to switch modes.
646131623Sdes  /// \p StartInfo - the original subtarget info before inline asm
64724143Sjoerg  /// \p EndInfo   - the final subtarget info after parsing the inline asm,
648131623Sdes  ///                or NULL if the value is unknown.
649131623Sdes  virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
650131623Sdes                                const MCSubtargetInfo *EndInfo) const;
651131623Sdes
65224143Sjoerg  /// This emits visibility information about symbol, if this is supported by
65324143Sjoerg  /// the target.
654131626Sdes  void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
65524143Sjoerg                      bool IsDefinition = true) const;
656131310Salfred
657168710Sstas  /// This emits linkage information about \p GVSym based on \p GV, if this is
65824143Sjoerg  /// supported by the target.
659131623Sdes  void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
660131623Sdes
661131623Sdes  /// Return the alignment for the specified \p GV.
662131623Sdes  static Align getGVAlignment(const GlobalValue *GV, const DataLayout &DL,
663131623Sdes                              Align InAlign = Align::None());
664131623Sdes
665131623Sdesprivate:
666131623Sdes  /// Private state for PrintSpecial()
667131623Sdes  // Assign a unique ID to this machine instruction.
668168799Srafan  mutable const MachineInstr *LastMI = nullptr;
669168710Sstas  mutable unsigned LastFn = 0;
670168710Sstas  mutable unsigned Counter = ~0U;
67124143Sjoerg
672131623Sdes  /// This method emits the header for the current function.
673131623Sdes  virtual void EmitFunctionHeader();
674131623Sdes
675131623Sdes  /// Emit a blob of inline asm to the output streamer.
676131620Sdes  void
677131623Sdes  EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
678172207Sjeff                const MCTargetOptions &MCOptions,
679131623Sdes                const MDNode *LocMDNode = nullptr,
680131623Sdes                InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
681131623Sdes
682158280Sbde  /// This method formats and emits the specified machine instruction that is an
683158280Sbde  /// inline asm.
684158280Sbde  void EmitInlineAsm(const MachineInstr *MI) const;
685131626Sdes
686131626Sdes  /// Add inline assembly info to the diagnostics machinery, so we can
687131626Sdes  /// emit file and position info. Returns SrcMgr memory buffer position.
688131626Sdes  unsigned addInlineAsmDiagBuffer(StringRef AsmStr,
689131626Sdes                                  const MDNode *LocMDNode) const;
690131626Sdes
691131623Sdes  //===------------------------------------------------------------------===//
692131623Sdes  // Internal Implementation Details
69324143Sjoerg  //===------------------------------------------------------------------===//
694131623Sdes
695131623Sdes  void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
696131623Sdes                          const MachineBasicBlock *MBB, unsigned uid) const;
69724143Sjoerg  void EmitLLVMUsedList(const ConstantArray *InitList);
698131623Sdes  /// Emit llvm.ident metadata in an '.ident' directive.
69924143Sjoerg  void EmitModuleIdents(Module &M);
700131623Sdes  /// Emit bytes for llvm.commandline metadata.
701131623Sdes  void EmitModuleCommandLines(Module &M);
70224143Sjoerg  void EmitXXStructorList(const DataLayout &DL, const Constant *List,
703131623Sdes                          bool isCtor);
704131623Sdes
70524143Sjoerg  GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S);
706131623Sdes  /// Emit GlobalAlias or GlobalIFunc.
707131623Sdes  void emitGlobalIndirectSymbol(Module &M, const GlobalIndirectSymbol &GIS);
708131623Sdes};
709131623Sdes
710131623Sdes} // end namespace llvm
711104388Sjhb
712131623Sdes#endif // LLVM_CODEGEN_ASMPRINTER_H
713131623Sdes