blob: 9c2097b299520bd6d55e53021e403892ce3e11fe [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework ---------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
15#ifndef LLVM_CODEGEN_ASMPRINTER_H
16#define LLVM_CODEGEN_ASMPRINTER_H
17
18#include "llvm/ADT/MapVector.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010022#include "llvm/CodeGen/AsmPrinterHandler.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023#include "llvm/CodeGen/DwarfStringPoolEntry.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/IR/InlineAsm.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/SourceMgr.h"
29#include <cstdint>
30#include <memory>
31#include <utility>
32#include <vector>
33
34namespace llvm {
35
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010036class BasicBlock;
37class BlockAddress;
38class Constant;
39class ConstantArray;
40class DataLayout;
41class DIE;
42class DIEAbbrev;
43class DwarfDebug;
44class GCMetadataPrinter;
45class GCStrategy;
46class GlobalIndirectSymbol;
47class GlobalObject;
48class GlobalValue;
49class GlobalVariable;
50class MachineBasicBlock;
51class MachineConstantPoolValue;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052class MachineDominatorTree;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053class MachineFunction;
54class MachineInstr;
55class MachineJumpTableInfo;
56class MachineLoopInfo;
57class MachineModuleInfo;
58class MachineOptimizationRemarkEmitter;
59class MCAsmInfo;
60class MCCFIInstruction;
61struct MCCodePaddingContext;
62class MCContext;
63class MCExpr;
64class MCInst;
65class MCSection;
66class MCStreamer;
67class MCSubtargetInfo;
68class MCSymbol;
69class MCTargetOptions;
70class MDNode;
71class Module;
72class raw_ostream;
Andrew Walbran16937d02019-10-22 13:54:20 +010073class StackMaps;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074class TargetLoweringObjectFile;
75class TargetMachine;
76
77/// This class is intended to be used as a driving class for all asm writers.
78class AsmPrinter : public MachineFunctionPass {
79public:
80 /// Target machine description.
81 TargetMachine &TM;
82
83 /// Target Asm Printer information.
84 const MCAsmInfo *MAI;
85
86 /// This is the context for the output file that we are streaming. This owns
87 /// all of the global MC-related objects for the generated translation unit.
88 MCContext &OutContext;
89
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
95 /// The current machine function.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010096 MachineFunction *MF = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097
98 /// This is a pointer to the current MachineModuleInfo.
99 MachineModuleInfo *MMI = nullptr;
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 /// This is a pointer to the current MachineLoopInfo.
102 MachineDominatorTree *MDT = nullptr;
103
104 /// This is a pointer to the current MachineLoopInfo.
105 MachineLoopInfo *MLI = nullptr;
106
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107 /// Optimization remark emitter.
108 MachineOptimizationRemarkEmitter *ORE;
109
110 /// The symbol for the current function. This is recalculated at the beginning
111 /// of each call to runOnMachineFunction().
112 MCSymbol *CurrentFnSym = nullptr;
113
114 /// The symbol used to represent the start of the current function for the
115 /// purpose of calculating its size (e.g. using the .size directive). By
116 /// default, this is equal to CurrentFnSym.
117 MCSymbol *CurrentFnSymForSize = nullptr;
118
119 /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
120 /// its number of uses by other globals.
121 using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;
122 MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
123
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100124private:
125 MCSymbol *CurrentFnBegin = nullptr;
126 MCSymbol *CurrentFnEnd = nullptr;
127 MCSymbol *CurExceptionSym = nullptr;
128
129 // The garbage collection metadata printer table.
130 void *GCMetadataPrinters = nullptr; // Really a DenseMap.
131
132 /// Emit comments in assembly output if this is true.
133 bool VerboseAsm;
134
135 static char ID;
136
Andrew Walbran16937d02019-10-22 13:54:20 +0100137protected:
138 /// Protected struct HandlerInfo and Handlers permit target extended
139 /// AsmPrinter adds their own handlers.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140 struct HandlerInfo {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100141 std::unique_ptr<AsmPrinterHandler> Handler;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100142 const char *TimerName;
143 const char *TimerDescription;
144 const char *TimerGroupName;
145 const char *TimerGroupDescription;
146
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100147 HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler,
148 const char *TimerName, const char *TimerDescription,
149 const char *TimerGroupName, const char *TimerGroupDescription)
150 : Handler(std::move(Handler)), TimerName(TimerName),
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
152 TimerGroupDescription(TimerGroupDescription) {}
153 };
154
155 /// A vector of all debug/EH info emitters we should use. This vector
156 /// maintains ownership of the emitters.
157 SmallVector<HandlerInfo, 1> Handlers;
158
159public:
160 struct SrcMgrDiagInfo {
161 SourceMgr SrcMgr;
162 std::vector<const MDNode *> LocInfos;
163 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
164 void *DiagContext;
165 };
166
167private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100168 /// If generated on the fly this own the instance.
169 std::unique_ptr<MachineDominatorTree> OwnedMDT;
170
171 /// If generated on the fly this own the instance.
172 std::unique_ptr<MachineLoopInfo> OwnedMLI;
173
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 /// Structure for generating diagnostics for inline assembly. Only initialised
175 /// when necessary.
176 mutable std::unique_ptr<SrcMgrDiagInfo> DiagInfo;
177
178 /// If the target supports dwarf debug info, this pointer is non-null.
179 DwarfDebug *DD = nullptr;
180
181 /// If the current module uses dwarf CFI annotations strictly for debugging.
182 bool isCFIMoveForDebugging = false;
183
184protected:
185 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
186
187public:
188 ~AsmPrinter() override;
189
190 DwarfDebug *getDwarfDebug() { return DD; }
191 DwarfDebug *getDwarfDebug() const { return DD; }
192
193 uint16_t getDwarfVersion() const;
194 void setDwarfVersion(uint16_t Version);
195
196 bool isPositionIndependent() const;
197
198 /// Return true if assembly output should contain comments.
199 bool isVerbose() const { return VerboseAsm; }
200
201 /// Return a unique ID for the current function.
202 unsigned getFunctionNumber() const;
203
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100204 /// Return symbol for the function pseudo stack if the stack frame is not a
205 /// register based.
206 virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; }
207
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
209 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
210 MCSymbol *getCurExceptionSym();
211
212 /// Return information about object file lowering.
213 const TargetLoweringObjectFile &getObjFileLowering() const;
214
215 /// Return information about data layout.
216 const DataLayout &getDataLayout() const;
217
218 /// Return the pointer size from the TargetMachine
219 unsigned getPointerSize() const;
220
221 /// Return information about subtarget.
222 const MCSubtargetInfo &getSubtargetInfo() const;
223
224 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
225
Andrew Walbran16937d02019-10-22 13:54:20 +0100226 /// Emits inital debug location directive.
227 void emitInitialRawDwarfLocDirective(const MachineFunction &MF);
228
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229 /// Return the current section we are emitting to.
230 const MCSection *getCurrentSection() const;
231
232 void getNameWithPrefix(SmallVectorImpl<char> &Name,
233 const GlobalValue *GV) const;
234
235 MCSymbol *getSymbol(const GlobalValue *GV) const;
236
237 //===------------------------------------------------------------------===//
238 // XRay instrumentation implementation.
239 //===------------------------------------------------------------------===//
240public:
241 // This describes the kind of sled we're storing in the XRay table.
242 enum class SledKind : uint8_t {
243 FUNCTION_ENTER = 0,
244 FUNCTION_EXIT = 1,
245 TAIL_CALL = 2,
246 LOG_ARGS_ENTER = 3,
247 CUSTOM_EVENT = 4,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100248 TYPED_EVENT = 5,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249 };
250
251 // The table will contain these structs that point to the sled, the function
252 // containing the sled, and what kind of sled (and whether they should always
253 // be instrumented). We also use a version identifier that the runtime can use
254 // to decide what to do with the sled, depending on the version of the sled.
255 struct XRayFunctionEntry {
256 const MCSymbol *Sled;
257 const MCSymbol *Function;
258 SledKind Kind;
259 bool AlwaysInstrument;
260 const class Function *Fn;
261 uint8_t Version;
262
263 void emit(int, MCStreamer *, const MCSymbol *) const;
264 };
265
266 // All the sleds to be emitted.
267 SmallVector<XRayFunctionEntry, 4> Sleds;
268
269 // A unique ID used for ELF sections associated with a particular function.
270 unsigned XRayFnUniqueID = 0;
271
272 // Helper function to record a given XRay sled.
273 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind,
274 uint8_t Version = 0);
275
276 /// Emit a table with all XRay instrumentation points.
277 void emitXRayTable();
278
279 //===------------------------------------------------------------------===//
280 // MachineFunctionPass Implementation.
281 //===------------------------------------------------------------------===//
282
283 /// Record analysis usage.
284 void getAnalysisUsage(AnalysisUsage &AU) const override;
285
286 /// Set up the AsmPrinter when we are working on a new module. If your pass
287 /// overrides this, it must make sure to explicitly call this implementation.
288 bool doInitialization(Module &M) override;
289
290 /// Shut down the asmprinter. If you override this in your pass, you must make
291 /// sure to call it explicitly.
292 bool doFinalization(Module &M) override;
293
294 /// Emit the specified function out to the OutStreamer.
295 bool runOnMachineFunction(MachineFunction &MF) override {
296 SetupMachineFunction(MF);
297 EmitFunctionBody();
298 return false;
299 }
300
301 //===------------------------------------------------------------------===//
302 // Coarse grained IR lowering routines.
303 //===------------------------------------------------------------------===//
304
305 /// This should be called when a new MachineFunction is being processed from
306 /// runOnMachineFunction.
307 void SetupMachineFunction(MachineFunction &MF);
308
309 /// This method emits the body and trailer for a function.
310 void EmitFunctionBody();
311
312 void emitCFIInstruction(const MachineInstr &MI);
313
314 void emitFrameAlloc(const MachineInstr &MI);
315
316 void emitStackSizeSection(const MachineFunction &MF);
317
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100318 void emitRemarksSection(Module &M);
319
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100320 enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
321 CFIMoveType needsCFIMoves() const;
322
323 /// Returns false if needsCFIMoves() == CFI_M_EH for any function
324 /// in the module.
325 bool needsOnlyDebugCFIMoves() const { return isCFIMoveForDebugging; }
326
327 bool needsSEHMoves();
328
329 /// Print to the current output stream assembly representations of the
330 /// constants in the constant pool MCP. This is used to print out constants
331 /// which have been "spilled to memory" by the code generator.
332 virtual void EmitConstantPool();
333
334 /// Print assembly representations of the jump tables used by the current
335 /// function to the current output stream.
336 virtual void EmitJumpTableInfo();
337
338 /// Emit the specified global variable to the .s file.
339 virtual void EmitGlobalVariable(const GlobalVariable *GV);
340
341 /// Check to see if the specified global is a special global used by LLVM. If
342 /// so, emit it and return true, otherwise do nothing and return false.
343 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
344
345 /// Emit an alignment directive to the specified power of two boundary. For
346 /// example, if you pass in 3 here, you will get an 8 byte alignment. If a
347 /// global value is specified, and if that global has an explicit alignment
348 /// requested, it will override the alignment request if required for
349 /// correctness.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100350 void EmitAlignment(unsigned NumBits, const GlobalObject *GV = nullptr) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100351
352 /// Lower the specified LLVM Constant to an MCExpr.
353 virtual const MCExpr *lowerConstant(const Constant *CV);
354
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100355 /// Print a general LLVM constant to the .s file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100356 void EmitGlobalConstant(const DataLayout &DL, const Constant *CV);
357
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100358 /// Unnamed constant global variables solely contaning a pointer to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359 /// another globals variable act like a global variable "proxy", or GOT
360 /// equivalents, i.e., it's only used to hold the address of the latter. One
361 /// optimization is to replace accesses to these proxies by using the GOT
362 /// entry for the final global instead. Hence, we select GOT equivalent
363 /// candidates among all the module global variables, avoid emitting them
364 /// unnecessarily and finally replace references to them by pc relative
365 /// accesses to GOT entries.
366 void computeGlobalGOTEquivs(Module &M);
367
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100368 /// Constant expressions using GOT equivalent globals may not be
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100369 /// eligible for PC relative GOT entry conversion, in such cases we need to
370 /// emit the proxies we previously omitted in EmitGlobalVariable.
371 void emitGlobalGOTEquivs();
372
Andrew Walbran16937d02019-10-22 13:54:20 +0100373 /// Emit the stack maps.
374 void emitStackMaps(StackMaps &SM);
375
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100376 //===------------------------------------------------------------------===//
377 // Overridable Hooks
378 //===------------------------------------------------------------------===//
379
380 // Targets can, or in the case of EmitInstruction, must implement these to
381 // customize output.
382
383 /// This virtual method can be overridden by targets that want to emit
384 /// something at the start of their file.
385 virtual void EmitStartOfAsmFile(Module &) {}
386
387 /// This virtual method can be overridden by targets that want to emit
388 /// something at the end of their file.
389 virtual void EmitEndOfAsmFile(Module &) {}
390
391 /// Targets can override this to emit stuff before the first basic block in
392 /// the function.
393 virtual void EmitFunctionBodyStart() {}
394
395 /// Targets can override this to emit stuff after the last basic block in the
396 /// function.
397 virtual void EmitFunctionBodyEnd() {}
398
399 /// Targets can override this to emit stuff at the start of a basic block.
400 /// By default, this method prints the label for the specified
401 /// MachineBasicBlock, an alignment (if present) and a comment describing it
402 /// if appropriate.
403 virtual void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
404
405 /// Targets can override this to emit stuff at the end of a basic block.
406 virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB);
407
408 /// Targets should implement this to emit instructions.
409 virtual void EmitInstruction(const MachineInstr *) {
410 llvm_unreachable("EmitInstruction not implemented");
411 }
412
413 /// Return the symbol for the specified constant pool entry.
414 virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
415
416 virtual void EmitFunctionEntryLabel();
417
418 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
419
420 /// Targets can override this to change how global constants that are part of
421 /// a C++ static/global constructor list are emitted.
422 virtual void EmitXXStructor(const DataLayout &DL, const Constant *CV) {
423 EmitGlobalConstant(DL, CV);
424 }
425
426 /// Return true if the basic block has exactly one predecessor and the control
427 /// transfer mechanism between the predecessor and this block is a
428 /// fall-through.
429 virtual bool
430 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
431
432 /// Targets can override this to customize the output of IMPLICIT_DEF
433 /// instructions in verbose mode.
434 virtual void emitImplicitDef(const MachineInstr *MI) const;
435
436 //===------------------------------------------------------------------===//
437 // Symbol Lowering Routines.
438 //===------------------------------------------------------------------===//
439
440 MCSymbol *createTempSymbol(const Twine &Name) const;
441
442 /// Return the MCSymbol for a private symbol with global value name as its
443 /// base, with the specified suffix.
444 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
445 StringRef Suffix) const;
446
447 /// Return the MCSymbol for the specified ExternalSymbol.
448 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
449
450 /// Return the symbol for the specified jump table entry.
451 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
452
453 /// Return the symbol for the specified jump table .set
454 /// FIXME: privatize to AsmPrinter.
455 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
456
457 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
458 /// basic block.
459 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
460 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
461
462 //===------------------------------------------------------------------===//
463 // Emission Helper Routines.
464 //===------------------------------------------------------------------===//
465
466 /// This is just convenient handler for printing offsets.
467 void printOffset(int64_t Offset, raw_ostream &OS) const;
468
469 /// Emit a byte directive and value.
470 void emitInt8(int Value) const;
471
472 /// Emit a short directive and value.
473 void emitInt16(int Value) const;
474
475 /// Emit a long directive and value.
476 void emitInt32(int Value) const;
477
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100478 /// Emit a long long directive and value.
479 void emitInt64(uint64_t Value) const;
480
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100481 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
482 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
483 /// .set if it is available.
484 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
485 unsigned Size) const;
486
487 /// Emit something like ".uleb128 Hi-Lo".
488 void EmitLabelDifferenceAsULEB128(const MCSymbol *Hi,
489 const MCSymbol *Lo) const;
490
491 /// Emit something like ".long Label+Offset" where the size in bytes of the
492 /// directive is specified by Size and Label specifies the label. This
493 /// implicitly uses .set if it is available.
494 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
495 unsigned Size, bool IsSectionRelative = false) const;
496
497 /// Emit something like ".long Label" where the size in bytes of the directive
498 /// is specified by Size and Label specifies the label.
499 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
500 bool IsSectionRelative = false) const {
501 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
502 }
503
504 /// Emit something like ".long Label + Offset".
505 void EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const;
506
507 //===------------------------------------------------------------------===//
508 // Dwarf Emission Helper Routines
509 //===------------------------------------------------------------------===//
510
511 /// Emit the specified signed leb128 value.
512 void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
513
514 /// Emit the specified unsigned leb128 value.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100515 void EmitULEB128(uint64_t Value, const char *Desc = nullptr, unsigned PadTo = 0) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100516
517 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose
518 /// assembly output is enabled, we output comments describing the encoding.
519 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
520 void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
521
522 /// Return the size of the encoding in bytes.
523 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
524
525 /// Emit reference to a ttype global with a specified encoding.
526 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
527
528 /// Emit a reference to a symbol for use in dwarf. Different object formats
529 /// represent this in different ways. Some use a relocation others encode
530 /// the label offset in its section.
531 void emitDwarfSymbolReference(const MCSymbol *Label,
532 bool ForceOffset = false) const;
533
534 /// Emit the 4-byte offset of a string from the start of its section.
535 ///
536 /// When possible, emit a DwarfStringPool section offset without any
537 /// relocations, and without using the symbol. Otherwise, defers to \a
538 /// emitDwarfSymbolReference().
539 void emitDwarfStringOffset(DwarfStringPoolEntry S) const;
540
541 /// Emit the 4-byte offset of a string from the start of its section.
542 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
543 emitDwarfStringOffset(S.getEntry());
544 }
545
546 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
547 virtual unsigned getISAEncoding() { return 0; }
548
549 /// Emit the directive and value for debug thread local expression
550 ///
551 /// \p Value - The value to emit.
552 /// \p Size - The size of the integer (in bytes) to emit.
Andrew Walbran16937d02019-10-22 13:54:20 +0100553 virtual void EmitDebugValue(const MCExpr *Value, unsigned Size) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100554
555 //===------------------------------------------------------------------===//
556 // Dwarf Lowering Routines
557 //===------------------------------------------------------------------===//
558
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100559 /// Emit frame instruction to describe the layout of the frame.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100560 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
561
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100562 /// Emit Dwarf abbreviation table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100563 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
564 // For each abbreviation.
565 for (const auto &Abbrev : Abbrevs)
566 emitDwarfAbbrev(*Abbrev);
567
568 // Mark end of abbreviations.
569 EmitULEB128(0, "EOM(3)");
570 }
571
572 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
573
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100574 /// Recursively emit Dwarf DIE tree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100575 void emitDwarfDIE(const DIE &Die) const;
576
577 //===------------------------------------------------------------------===//
578 // Inline Asm Support
579 //===------------------------------------------------------------------===//
580
581 // These are hooks that targets can override to implement inline asm
582 // support. These should probably be moved out of AsmPrinter someday.
583
584 /// Print information related to the specified machine instr that is
585 /// independent of the operand, and may be independent of the instr itself.
586 /// This can be useful for portably encoding the comment character or other
587 /// bits of target-specific knowledge into the asmstrings. The syntax used is
588 /// ${:comment}. Targets can override this to add support for their own
589 /// strange codes.
590 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
591 const char *Code) const;
592
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100593 /// Print the MachineOperand as a symbol. Targets with complex handling of
594 /// symbol references should override the base implementation.
595 virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS);
596
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100597 /// Print the specified operand of MI, an INLINEASM instruction, using the
598 /// specified assembler variant. Targets should override this to format as
599 /// appropriate. This method can return true if the operand is erroneous.
600 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100601 const char *ExtraCode, raw_ostream &OS);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100602
603 /// Print the specified operand of MI, an INLINEASM instruction, using the
604 /// specified assembler variant as an address. Targets should override this to
605 /// format as appropriate. This method can return true if the operand is
606 /// erroneous.
607 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100608 const char *ExtraCode, raw_ostream &OS);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100609
610 /// Let the target do anything it needs to do before emitting inlineasm.
611 /// \p StartInfo - the subtarget info before parsing inline asm
612 virtual void emitInlineAsmStart() const;
613
614 /// Let the target do anything it needs to do after emitting inlineasm.
615 /// This callback can be used restore the original mode in case the
616 /// inlineasm contains directives to switch modes.
617 /// \p StartInfo - the original subtarget info before inline asm
618 /// \p EndInfo - the final subtarget info after parsing the inline asm,
619 /// or NULL if the value is unknown.
620 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
621 const MCSubtargetInfo *EndInfo) const;
622
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100623 /// This emits visibility information about symbol, if this is supported by
624 /// the target.
625 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
626 bool IsDefinition = true) const;
627
628 /// This emits linkage information about \p GVSym based on \p GV, if this is
629 /// supported by the target.
630 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
631
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100632private:
633 /// Private state for PrintSpecial()
634 // Assign a unique ID to this machine instruction.
635 mutable const MachineInstr *LastMI = nullptr;
636 mutable unsigned LastFn = 0;
637 mutable unsigned Counter = ~0U;
638
639 /// This method emits the header for the current function.
640 virtual void EmitFunctionHeader();
641
642 /// Emit a blob of inline asm to the output streamer.
643 void
644 EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
645 const MCTargetOptions &MCOptions,
646 const MDNode *LocMDNode = nullptr,
647 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
648
649 /// This method formats and emits the specified machine instruction that is an
650 /// inline asm.
651 void EmitInlineAsm(const MachineInstr *MI) const;
652
Andrew Scull0372a572018-11-16 15:47:06 +0000653 /// Add inline assembly info to the diagnostics machinery, so we can
654 /// emit file and position info. Returns SrcMgr memory buffer position.
655 unsigned addInlineAsmDiagBuffer(StringRef AsmStr,
656 const MDNode *LocMDNode) const;
657
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100658 //===------------------------------------------------------------------===//
659 // Internal Implementation Details
660 //===------------------------------------------------------------------===//
661
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100662 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
663 const MachineBasicBlock *MBB, unsigned uid) const;
664 void EmitLLVMUsedList(const ConstantArray *InitList);
665 /// Emit llvm.ident metadata in an '.ident' directive.
666 void EmitModuleIdents(Module &M);
Andrew Walbran16937d02019-10-22 13:54:20 +0100667 /// Emit bytes for llvm.commandline metadata.
668 void EmitModuleCommandLines(Module &M);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100669 void EmitXXStructorList(const DataLayout &DL, const Constant *List,
670 bool isCtor);
671
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100672 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100673 /// Emit GlobalAlias or GlobalIFunc.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100674 void emitGlobalIndirectSymbol(Module &M, const GlobalIndirectSymbol &GIS);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100675 void setupCodePaddingContext(const MachineBasicBlock &MBB,
676 MCCodePaddingContext &Context) const;
677};
678
679} // end namespace llvm
680
681#endif // LLVM_CODEGEN_ASMPRINTER_H