blob: 76486b0b48ced703f6ea479dc4d1c9f846323e4f [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"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010020#include "llvm/CodeGen/AsmPrinterHandler.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021#include "llvm/CodeGen/DwarfStringPoolEntry.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/IR/InlineAsm.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/SourceMgr.h"
27#include <cstdint>
28#include <memory>
29#include <utility>
30#include <vector>
31
32namespace llvm {
33
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034class BasicBlock;
35class BlockAddress;
36class Constant;
37class ConstantArray;
38class DataLayout;
39class DIE;
40class DIEAbbrev;
41class DwarfDebug;
42class GCMetadataPrinter;
43class GCStrategy;
44class GlobalIndirectSymbol;
45class GlobalObject;
46class GlobalValue;
47class GlobalVariable;
48class MachineBasicBlock;
49class MachineConstantPoolValue;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050class MachineDominatorTree;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051class MachineFunction;
52class MachineInstr;
53class MachineJumpTableInfo;
54class MachineLoopInfo;
55class MachineModuleInfo;
56class MachineOptimizationRemarkEmitter;
57class MCAsmInfo;
58class MCCFIInstruction;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059class MCContext;
60class MCExpr;
61class MCInst;
62class MCSection;
63class MCStreamer;
64class MCSubtargetInfo;
65class MCSymbol;
66class MCTargetOptions;
67class MDNode;
68class Module;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069class PseudoProbeHandler;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070class raw_ostream;
Andrew Walbran16937d02019-10-22 13:54:20 +010071class StackMaps;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020072class StringRef;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073class TargetLoweringObjectFile;
74class TargetMachine;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020075class Twine;
76
77namespace remarks {
78class RemarkStreamer;
79}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080
81/// This class is intended to be used as a driving class for all asm writers.
82class AsmPrinter : public MachineFunctionPass {
83public:
84 /// Target machine description.
85 TargetMachine &TM;
86
87 /// Target Asm Printer information.
88 const MCAsmInfo *MAI;
89
90 /// This is the context for the output file that we are streaming. This owns
91 /// all of the global MC-related objects for the generated translation unit.
92 MCContext &OutContext;
93
94 /// This is the MCStreamer object for the file we are generating. This
95 /// contains the transient state for the current translation unit that we are
96 /// generating (such as the current section etc).
97 std::unique_ptr<MCStreamer> OutStreamer;
98
99 /// The current machine function.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100100 MachineFunction *MF = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100101
102 /// This is a pointer to the current MachineModuleInfo.
103 MachineModuleInfo *MMI = nullptr;
104
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200105 /// This is a pointer to the current MachineDominatorTree.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100106 MachineDominatorTree *MDT = nullptr;
107
108 /// This is a pointer to the current MachineLoopInfo.
109 MachineLoopInfo *MLI = nullptr;
110
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100111 /// Optimization remark emitter.
112 MachineOptimizationRemarkEmitter *ORE;
113
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200114 /// The symbol for the entry in __patchable_function_entires.
115 MCSymbol *CurrentPatchableFunctionEntrySym = nullptr;
116
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 /// The symbol for the current function. This is recalculated at the beginning
118 /// of each call to runOnMachineFunction().
119 MCSymbol *CurrentFnSym = nullptr;
120
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200121 /// The symbol for the current function descriptor on AIX. This is created
122 /// at the beginning of each call to SetupMachineFunction().
123 MCSymbol *CurrentFnDescSym = nullptr;
124
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125 /// The symbol used to represent the start of the current function for the
126 /// purpose of calculating its size (e.g. using the .size directive). By
127 /// default, this is equal to CurrentFnSym.
128 MCSymbol *CurrentFnSymForSize = nullptr;
129
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200130 /// Map a basic block section ID to the begin and end symbols of that section
131 /// which determine the section's range.
132 struct MBBSectionRange {
133 MCSymbol *BeginLabel, *EndLabel;
134 };
135
136 MapVector<unsigned, MBBSectionRange> MBBSectionRanges;
137
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100138 /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
139 /// its number of uses by other globals.
140 using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;
141 MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
142
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200143 /// struct HandlerInfo and Handlers permit users or target extended
144 /// AsmPrinter to add their own handlers.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100145 struct HandlerInfo {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100146 std::unique_ptr<AsmPrinterHandler> Handler;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147 const char *TimerName;
148 const char *TimerDescription;
149 const char *TimerGroupName;
150 const char *TimerGroupDescription;
151
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100152 HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler,
153 const char *TimerName, const char *TimerDescription,
154 const char *TimerGroupName, const char *TimerGroupDescription)
155 : Handler(std::move(Handler)), TimerName(TimerName),
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
157 TimerGroupDescription(TimerGroupDescription) {}
158 };
159
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200160private:
161 MCSymbol *CurrentFnEnd = nullptr;
162
163 /// Map a basic block section ID to the exception symbol associated with that
164 /// section. Map entries are assigned and looked up via
165 /// AsmPrinter::getMBBExceptionSym.
166 DenseMap<unsigned, MCSymbol *> MBBSectionExceptionSyms;
167
168 // The symbol used to represent the start of the current BB section of the
169 // function. This is used to calculate the size of the BB section.
170 MCSymbol *CurrentSectionBeginSym = nullptr;
171
172 // The garbage collection metadata printer table.
173 void *GCMetadataPrinters = nullptr; // Really a DenseMap.
174
175 /// Emit comments in assembly output if this is true.
176 bool VerboseAsm;
177
178 static char ID;
179
180protected:
181 MCSymbol *CurrentFnBegin = nullptr;
182
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183 /// A vector of all debug/EH info emitters we should use. This vector
184 /// maintains ownership of the emitters.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200185 std::vector<HandlerInfo> Handlers;
186 size_t NumUserHandlers = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187
188public:
189 struct SrcMgrDiagInfo {
190 SourceMgr SrcMgr;
191 std::vector<const MDNode *> LocInfos;
192 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
193 void *DiagContext;
194 };
195
196private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100197 /// If generated on the fly this own the instance.
198 std::unique_ptr<MachineDominatorTree> OwnedMDT;
199
200 /// If generated on the fly this own the instance.
201 std::unique_ptr<MachineLoopInfo> OwnedMLI;
202
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100203 /// Structure for generating diagnostics for inline assembly. Only initialised
204 /// when necessary.
205 mutable std::unique_ptr<SrcMgrDiagInfo> DiagInfo;
206
207 /// If the target supports dwarf debug info, this pointer is non-null.
208 DwarfDebug *DD = nullptr;
209
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200210 /// A handler that supports pseudo probe emission with embedded inline
211 /// context.
212 PseudoProbeHandler *PP = nullptr;
213
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100214 /// If the current module uses dwarf CFI annotations strictly for debugging.
215 bool isCFIMoveForDebugging = false;
216
217protected:
218 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
219
220public:
221 ~AsmPrinter() override;
222
223 DwarfDebug *getDwarfDebug() { return DD; }
224 DwarfDebug *getDwarfDebug() const { return DD; }
225
226 uint16_t getDwarfVersion() const;
227 void setDwarfVersion(uint16_t Version);
228
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200229 bool isDwarf64() const;
230
231 /// Returns 4 for DWARF32 and 8 for DWARF64.
232 unsigned int getDwarfOffsetByteSize() const;
233
234 /// Returns 4 for DWARF32 and 12 for DWARF64.
235 unsigned int getUnitLengthFieldByteSize() const;
236
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100237 bool isPositionIndependent() const;
238
239 /// Return true if assembly output should contain comments.
240 bool isVerbose() const { return VerboseAsm; }
241
242 /// Return a unique ID for the current function.
243 unsigned getFunctionNumber() const;
244
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100245 /// Return symbol for the function pseudo stack if the stack frame is not a
246 /// register based.
247 virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; }
248
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
250 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200251
252 // Return the exception symbol associated with the MBB section containing a
253 // given basic block.
254 MCSymbol *getMBBExceptionSym(const MachineBasicBlock &MBB);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100255
256 /// Return information about object file lowering.
257 const TargetLoweringObjectFile &getObjFileLowering() const;
258
259 /// Return information about data layout.
260 const DataLayout &getDataLayout() const;
261
262 /// Return the pointer size from the TargetMachine
263 unsigned getPointerSize() const;
264
265 /// Return information about subtarget.
266 const MCSubtargetInfo &getSubtargetInfo() const;
267
268 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
269
Andrew Walbran16937d02019-10-22 13:54:20 +0100270 /// Emits inital debug location directive.
271 void emitInitialRawDwarfLocDirective(const MachineFunction &MF);
272
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100273 /// Return the current section we are emitting to.
274 const MCSection *getCurrentSection() const;
275
276 void getNameWithPrefix(SmallVectorImpl<char> &Name,
277 const GlobalValue *GV) const;
278
279 MCSymbol *getSymbol(const GlobalValue *GV) const;
280
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200281 /// Similar to getSymbol() but preferred for references. On ELF, this uses a
282 /// local symbol if a reference to GV is guaranteed to be resolved to the
283 /// definition in the same module.
284 MCSymbol *getSymbolPreferLocal(const GlobalValue &GV) const;
285
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286 //===------------------------------------------------------------------===//
287 // XRay instrumentation implementation.
288 //===------------------------------------------------------------------===//
289public:
290 // This describes the kind of sled we're storing in the XRay table.
291 enum class SledKind : uint8_t {
292 FUNCTION_ENTER = 0,
293 FUNCTION_EXIT = 1,
294 TAIL_CALL = 2,
295 LOG_ARGS_ENTER = 3,
296 CUSTOM_EVENT = 4,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100297 TYPED_EVENT = 5,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298 };
299
300 // The table will contain these structs that point to the sled, the function
301 // containing the sled, and what kind of sled (and whether they should always
302 // be instrumented). We also use a version identifier that the runtime can use
303 // to decide what to do with the sled, depending on the version of the sled.
304 struct XRayFunctionEntry {
305 const MCSymbol *Sled;
306 const MCSymbol *Function;
307 SledKind Kind;
308 bool AlwaysInstrument;
309 const class Function *Fn;
310 uint8_t Version;
311
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200312 void emit(int, MCStreamer *) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100313 };
314
315 // All the sleds to be emitted.
316 SmallVector<XRayFunctionEntry, 4> Sleds;
317
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100318 // Helper function to record a given XRay sled.
319 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind,
320 uint8_t Version = 0);
321
322 /// Emit a table with all XRay instrumentation points.
323 void emitXRayTable();
324
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200325 void emitPatchableFunctionEntries();
326
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100327 //===------------------------------------------------------------------===//
328 // MachineFunctionPass Implementation.
329 //===------------------------------------------------------------------===//
330
331 /// Record analysis usage.
332 void getAnalysisUsage(AnalysisUsage &AU) const override;
333
334 /// Set up the AsmPrinter when we are working on a new module. If your pass
335 /// overrides this, it must make sure to explicitly call this implementation.
336 bool doInitialization(Module &M) override;
337
338 /// Shut down the asmprinter. If you override this in your pass, you must make
339 /// sure to call it explicitly.
340 bool doFinalization(Module &M) override;
341
342 /// Emit the specified function out to the OutStreamer.
343 bool runOnMachineFunction(MachineFunction &MF) override {
344 SetupMachineFunction(MF);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200345 emitFunctionBody();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100346 return false;
347 }
348
349 //===------------------------------------------------------------------===//
350 // Coarse grained IR lowering routines.
351 //===------------------------------------------------------------------===//
352
353 /// This should be called when a new MachineFunction is being processed from
354 /// runOnMachineFunction.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200355 virtual void SetupMachineFunction(MachineFunction &MF);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100356
357 /// This method emits the body and trailer for a function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200358 void emitFunctionBody();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359
360 void emitCFIInstruction(const MachineInstr &MI);
361
362 void emitFrameAlloc(const MachineInstr &MI);
363
364 void emitStackSizeSection(const MachineFunction &MF);
365
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200366 void emitBBAddrMapSection(const MachineFunction &MF);
367
368 void emitPseudoProbe(const MachineInstr &MI);
369
370 void emitRemarksSection(remarks::RemarkStreamer &RS);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100371
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100372 enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
373 CFIMoveType needsCFIMoves() const;
374
375 /// Returns false if needsCFIMoves() == CFI_M_EH for any function
376 /// in the module.
377 bool needsOnlyDebugCFIMoves() const { return isCFIMoveForDebugging; }
378
379 bool needsSEHMoves();
380
381 /// Print to the current output stream assembly representations of the
382 /// constants in the constant pool MCP. This is used to print out constants
383 /// which have been "spilled to memory" by the code generator.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200384 virtual void emitConstantPool();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100385
386 /// Print assembly representations of the jump tables used by the current
387 /// function to the current output stream.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200388 virtual void emitJumpTableInfo();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100389
390 /// Emit the specified global variable to the .s file.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200391 virtual void emitGlobalVariable(const GlobalVariable *GV);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100392
393 /// Check to see if the specified global is a special global used by LLVM. If
394 /// so, emit it and return true, otherwise do nothing and return false.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200395 bool emitSpecialLLVMGlobal(const GlobalVariable *GV);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100396
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200397 /// `llvm.global_ctors` and `llvm.global_dtors` are arrays of Structor
398 /// structs.
399 ///
400 /// Priority - init priority
401 /// Func - global initialization or global clean-up function
402 /// ComdatKey - associated data
403 struct Structor {
404 int Priority = 0;
405 Constant *Func = nullptr;
406 GlobalValue *ComdatKey = nullptr;
407
408 Structor() = default;
409 };
410
411 /// This method gathers an array of Structors and then sorts them out by
412 /// Priority.
413 /// @param List The initializer of `llvm.global_ctors` or `llvm.global_dtors`
414 /// array.
415 /// @param[out] Structors Sorted Structor structs by Priority.
416 void preprocessXXStructorList(const DataLayout &DL, const Constant *List,
417 SmallVector<Structor, 8> &Structors);
418
419 /// This method emits `llvm.global_ctors` or `llvm.global_dtors` list.
420 virtual void emitXXStructorList(const DataLayout &DL, const Constant *List,
421 bool IsCtor);
422
423 /// Emit an alignment directive to the specified power of two boundary. If a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100424 /// global value is specified, and if that global has an explicit alignment
425 /// requested, it will override the alignment request if required for
426 /// correctness.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200427 void emitAlignment(Align Alignment, const GlobalObject *GV = nullptr) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100428
429 /// Lower the specified LLVM Constant to an MCExpr.
430 virtual const MCExpr *lowerConstant(const Constant *CV);
431
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100432 /// Print a general LLVM constant to the .s file.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200433 void emitGlobalConstant(const DataLayout &DL, const Constant *CV);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100434
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100435 /// Unnamed constant global variables solely contaning a pointer to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100436 /// another globals variable act like a global variable "proxy", or GOT
437 /// equivalents, i.e., it's only used to hold the address of the latter. One
438 /// optimization is to replace accesses to these proxies by using the GOT
439 /// entry for the final global instead. Hence, we select GOT equivalent
440 /// candidates among all the module global variables, avoid emitting them
441 /// unnecessarily and finally replace references to them by pc relative
442 /// accesses to GOT entries.
443 void computeGlobalGOTEquivs(Module &M);
444
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100445 /// Constant expressions using GOT equivalent globals may not be
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100446 /// eligible for PC relative GOT entry conversion, in such cases we need to
447 /// emit the proxies we previously omitted in EmitGlobalVariable.
448 void emitGlobalGOTEquivs();
449
Andrew Walbran16937d02019-10-22 13:54:20 +0100450 /// Emit the stack maps.
451 void emitStackMaps(StackMaps &SM);
452
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100453 //===------------------------------------------------------------------===//
454 // Overridable Hooks
455 //===------------------------------------------------------------------===//
456
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200457 void addAsmPrinterHandler(HandlerInfo Handler) {
458 Handlers.insert(Handlers.begin(), std::move(Handler));
459 NumUserHandlers++;
460 }
461
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100462 // Targets can, or in the case of EmitInstruction, must implement these to
463 // customize output.
464
465 /// This virtual method can be overridden by targets that want to emit
466 /// something at the start of their file.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200467 virtual void emitStartOfAsmFile(Module &) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100468
469 /// This virtual method can be overridden by targets that want to emit
470 /// something at the end of their file.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200471 virtual void emitEndOfAsmFile(Module &) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100472
473 /// Targets can override this to emit stuff before the first basic block in
474 /// the function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200475 virtual void emitFunctionBodyStart() {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100476
477 /// Targets can override this to emit stuff after the last basic block in the
478 /// function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200479 virtual void emitFunctionBodyEnd() {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100480
481 /// Targets can override this to emit stuff at the start of a basic block.
482 /// By default, this method prints the label for the specified
483 /// MachineBasicBlock, an alignment (if present) and a comment describing it
484 /// if appropriate.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200485 virtual void emitBasicBlockStart(const MachineBasicBlock &MBB);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100486
487 /// Targets can override this to emit stuff at the end of a basic block.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200488 virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100489
490 /// Targets should implement this to emit instructions.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200491 virtual void emitInstruction(const MachineInstr *) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100492 llvm_unreachable("EmitInstruction not implemented");
493 }
494
495 /// Return the symbol for the specified constant pool entry.
496 virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
497
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200498 virtual void emitFunctionEntryLabel();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100499
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200500 virtual void emitFunctionDescriptor() {
501 llvm_unreachable("Function descriptor is target-specific.");
502 }
503
504 virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100505
506 /// Targets can override this to change how global constants that are part of
507 /// a C++ static/global constructor list are emitted.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200508 virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) {
509 emitGlobalConstant(DL, CV);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100510 }
511
512 /// Return true if the basic block has exactly one predecessor and the control
513 /// transfer mechanism between the predecessor and this block is a
514 /// fall-through.
515 virtual bool
516 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
517
518 /// Targets can override this to customize the output of IMPLICIT_DEF
519 /// instructions in verbose mode.
520 virtual void emitImplicitDef(const MachineInstr *MI) const;
521
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200522 /// Emit N NOP instructions.
523 void emitNops(unsigned N);
524
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100525 //===------------------------------------------------------------------===//
526 // Symbol Lowering Routines.
527 //===------------------------------------------------------------------===//
528
529 MCSymbol *createTempSymbol(const Twine &Name) const;
530
531 /// Return the MCSymbol for a private symbol with global value name as its
532 /// base, with the specified suffix.
533 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
534 StringRef Suffix) const;
535
536 /// Return the MCSymbol for the specified ExternalSymbol.
537 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
538
539 /// Return the symbol for the specified jump table entry.
540 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
541
542 /// Return the symbol for the specified jump table .set
543 /// FIXME: privatize to AsmPrinter.
544 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
545
546 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
547 /// basic block.
548 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
549 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
550
551 //===------------------------------------------------------------------===//
552 // Emission Helper Routines.
553 //===------------------------------------------------------------------===//
554
555 /// This is just convenient handler for printing offsets.
556 void printOffset(int64_t Offset, raw_ostream &OS) const;
557
558 /// Emit a byte directive and value.
559 void emitInt8(int Value) const;
560
561 /// Emit a short directive and value.
562 void emitInt16(int Value) const;
563
564 /// Emit a long directive and value.
565 void emitInt32(int Value) const;
566
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100567 /// Emit a long long directive and value.
568 void emitInt64(uint64_t Value) const;
569
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100570 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
571 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
572 /// .set if it is available.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200573 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100574 unsigned Size) const;
575
576 /// Emit something like ".uleb128 Hi-Lo".
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200577 void emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100578 const MCSymbol *Lo) const;
579
580 /// Emit something like ".long Label+Offset" where the size in bytes of the
581 /// directive is specified by Size and Label specifies the label. This
582 /// implicitly uses .set if it is available.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200583 void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100584 unsigned Size, bool IsSectionRelative = false) const;
585
586 /// Emit something like ".long Label" where the size in bytes of the directive
587 /// is specified by Size and Label specifies the label.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200588 void emitLabelReference(const MCSymbol *Label, unsigned Size,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100589 bool IsSectionRelative = false) const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200590 emitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100591 }
592
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100593 //===------------------------------------------------------------------===//
594 // Dwarf Emission Helper Routines
595 //===------------------------------------------------------------------===//
596
597 /// Emit the specified signed leb128 value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200598 void emitSLEB128(int64_t Value, const char *Desc = nullptr) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100599
600 /// Emit the specified unsigned leb128 value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200601 void emitULEB128(uint64_t Value, const char *Desc = nullptr,
602 unsigned PadTo = 0) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100603
604 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose
605 /// assembly output is enabled, we output comments describing the encoding.
606 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200607 void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100608
609 /// Return the size of the encoding in bytes.
610 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
611
612 /// Emit reference to a ttype global with a specified encoding.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200613 virtual void emitTTypeReference(const GlobalValue *GV, unsigned Encoding);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100614
615 /// Emit a reference to a symbol for use in dwarf. Different object formats
616 /// represent this in different ways. Some use a relocation others encode
617 /// the label offset in its section.
618 void emitDwarfSymbolReference(const MCSymbol *Label,
619 bool ForceOffset = false) const;
620
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200621 /// Emit the 4- or 8-byte offset of a string from the start of its section.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100622 ///
623 /// When possible, emit a DwarfStringPool section offset without any
624 /// relocations, and without using the symbol. Otherwise, defers to \a
625 /// emitDwarfSymbolReference().
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200626 ///
627 /// The length of the emitted value depends on the DWARF format.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100628 void emitDwarfStringOffset(DwarfStringPoolEntry S) const;
629
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200630 /// Emit the 4-or 8-byte offset of a string from the start of its section.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100631 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
632 emitDwarfStringOffset(S.getEntry());
633 }
634
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200635 /// Emit something like ".long Label + Offset" or ".quad Label + Offset"
636 /// depending on the DWARF format.
637 void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const;
638
639 /// Emit 32- or 64-bit value depending on the DWARF format.
640 void emitDwarfLengthOrOffset(uint64_t Value) const;
641
642 /// Emit a special value of 0xffffffff if producing 64-bit debugging info.
643 void maybeEmitDwarf64Mark() const;
644
645 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
646 /// according to the settings.
647 void emitDwarfUnitLength(uint64_t Length, const Twine &Comment) const;
648
649 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
650 /// according to the settings.
651 void emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
652 const Twine &Comment) const;
653
654 /// Emit reference to a call site with a specified encoding
655 void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
656 unsigned Encoding) const;
657 /// Emit an integer value corresponding to the call site encoding
658 void emitCallSiteValue(uint64_t Value, unsigned Encoding) const;
659
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100660 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
661 virtual unsigned getISAEncoding() { return 0; }
662
663 /// Emit the directive and value for debug thread local expression
664 ///
665 /// \p Value - The value to emit.
666 /// \p Size - The size of the integer (in bytes) to emit.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200667 virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100668
669 //===------------------------------------------------------------------===//
670 // Dwarf Lowering Routines
671 //===------------------------------------------------------------------===//
672
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100673 /// Emit frame instruction to describe the layout of the frame.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100674 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
675
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100676 /// Emit Dwarf abbreviation table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100677 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
678 // For each abbreviation.
679 for (const auto &Abbrev : Abbrevs)
680 emitDwarfAbbrev(*Abbrev);
681
682 // Mark end of abbreviations.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200683 emitULEB128(0, "EOM(3)");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100684 }
685
686 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
687
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100688 /// Recursively emit Dwarf DIE tree.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100689 void emitDwarfDIE(const DIE &Die) const;
690
691 //===------------------------------------------------------------------===//
692 // Inline Asm Support
693 //===------------------------------------------------------------------===//
694
695 // These are hooks that targets can override to implement inline asm
696 // support. These should probably be moved out of AsmPrinter someday.
697
698 /// Print information related to the specified machine instr that is
699 /// independent of the operand, and may be independent of the instr itself.
700 /// This can be useful for portably encoding the comment character or other
701 /// bits of target-specific knowledge into the asmstrings. The syntax used is
702 /// ${:comment}. Targets can override this to add support for their own
703 /// strange codes.
704 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
705 const char *Code) const;
706
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100707 /// Print the MachineOperand as a symbol. Targets with complex handling of
708 /// symbol references should override the base implementation.
709 virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS);
710
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100711 /// Print the specified operand of MI, an INLINEASM instruction, using the
712 /// specified assembler variant. Targets should override this to format as
713 /// appropriate. This method can return true if the operand is erroneous.
714 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100715 const char *ExtraCode, raw_ostream &OS);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100716
717 /// Print the specified operand of MI, an INLINEASM instruction, using the
718 /// specified assembler variant as an address. Targets should override this to
719 /// format as appropriate. This method can return true if the operand is
720 /// erroneous.
721 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100722 const char *ExtraCode, raw_ostream &OS);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100723
724 /// Let the target do anything it needs to do before emitting inlineasm.
725 /// \p StartInfo - the subtarget info before parsing inline asm
726 virtual void emitInlineAsmStart() const;
727
728 /// Let the target do anything it needs to do after emitting inlineasm.
729 /// This callback can be used restore the original mode in case the
730 /// inlineasm contains directives to switch modes.
731 /// \p StartInfo - the original subtarget info before inline asm
732 /// \p EndInfo - the final subtarget info after parsing the inline asm,
733 /// or NULL if the value is unknown.
734 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
735 const MCSubtargetInfo *EndInfo) const;
736
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100737 /// This emits visibility information about symbol, if this is supported by
738 /// the target.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200739 void emitVisibility(MCSymbol *Sym, unsigned Visibility,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100740 bool IsDefinition = true) const;
741
742 /// This emits linkage information about \p GVSym based on \p GV, if this is
743 /// supported by the target.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200744 virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
745
746 /// Return the alignment for the specified \p GV.
747 static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
748 Align InAlign = Align(1));
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100749
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100750private:
751 /// Private state for PrintSpecial()
752 // Assign a unique ID to this machine instruction.
753 mutable const MachineInstr *LastMI = nullptr;
754 mutable unsigned LastFn = 0;
755 mutable unsigned Counter = ~0U;
756
757 /// This method emits the header for the current function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200758 virtual void emitFunctionHeader();
759
760 /// This method emits a comment next to header for the current function.
761 virtual void emitFunctionHeaderComment();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100762
763 /// Emit a blob of inline asm to the output streamer.
764 void
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200765 emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100766 const MCTargetOptions &MCOptions,
767 const MDNode *LocMDNode = nullptr,
768 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
769
770 /// This method formats and emits the specified machine instruction that is an
771 /// inline asm.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200772 void emitInlineAsm(const MachineInstr *MI) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100773
Andrew Scull0372a572018-11-16 15:47:06 +0000774 /// Add inline assembly info to the diagnostics machinery, so we can
775 /// emit file and position info. Returns SrcMgr memory buffer position.
776 unsigned addInlineAsmDiagBuffer(StringRef AsmStr,
777 const MDNode *LocMDNode) const;
778
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100779 //===------------------------------------------------------------------===//
780 // Internal Implementation Details
781 //===------------------------------------------------------------------===//
782
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200783 void emitJumpTableEntry(const MachineJumpTableInfo *MJTI,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100784 const MachineBasicBlock *MBB, unsigned uid) const;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200785 void emitLLVMUsedList(const ConstantArray *InitList);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100786 /// Emit llvm.ident metadata in an '.ident' directive.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200787 void emitModuleIdents(Module &M);
Andrew Walbran16937d02019-10-22 13:54:20 +0100788 /// Emit bytes for llvm.commandline metadata.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200789 void emitModuleCommandLines(Module &M);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100790
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100791 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100792 /// Emit GlobalAlias or GlobalIFunc.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100793 void emitGlobalIndirectSymbol(Module &M, const GlobalIndirectSymbol &GIS);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200794
795 /// This method decides whether the specified basic block requires a label.
796 bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100797};
798
799} // end namespace llvm
800
801#endif // LLVM_CODEGEN_ASMPRINTER_H