1//===-- ARMAsmPrinter.h - ARM implementation of AsmPrinter ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_TARGET_ARM_ARMASMPRINTER_H
10#define LLVM_LIB_TARGET_ARM_ARMASMPRINTER_H
11
12#include "llvm/CodeGen/AsmPrinter.h"
13#include "llvm/Target/TargetMachine.h"
14
15namespace llvm {
16
17class ARMFunctionInfo;
18class ARMBaseTargetMachine;
19class MCOperand;
20class MachineConstantPool;
21class MachineOperand;
22class MCSymbol;
23
24namespace ARM {
25 enum DW_ISA {
26 DW_ISA_ARM_thumb = 1,
27 DW_ISA_ARM_arm = 2
28 };
29}
30
31class LLVM_LIBRARY_VISIBILITY ARMAsmPrinter : public AsmPrinter {
32public:
33 static char ID;
34
35private:
36 /// AFI - Keep a pointer to ARMFunctionInfo for the current
37 /// MachineFunction.
38 ARMFunctionInfo *AFI;
39
40 /// MCP - Keep a pointer to constantpool entries of the current
41 /// MachineFunction.
42 const MachineConstantPool *MCP;
43
44 /// InConstantPool - Maintain state when emitting a sequence of constant
45 /// pool entries so we can properly mark them as data regions.
46 bool InConstantPool;
47
48 /// ThumbIndirectPads - These maintain a per-function list of jump pad
49 /// labels used for ARMv4t thumb code to make register indirect calls.
50 SmallVector<std::pair<unsigned, MCSymbol*>, 4> ThumbIndirectPads;
51
52 /// OptimizationGoals - Maintain a combined optimization goal for all
53 /// functions in a module: one of Tag_ABI_optimization_goals values,
54 /// -1 if uninitialized, 0 if conflicting goals
55 int OptimizationGoals;
56
57 /// List of globals that have had their storage promoted to a constant
58 /// pool. This lives between calls to runOnMachineFunction and collects
59 /// data from every MachineFunction. It is used during doFinalization
60 /// when all non-function globals are emitted.
61 SmallPtrSet<const GlobalVariable*,2> PromotedGlobals;
62 /// Set of globals in PromotedGlobals that we've emitted labels for.
63 /// We need to emit labels even for promoted globals so that DWARF
64 /// debug info can link properly.
65 SmallPtrSet<const GlobalVariable*,2> EmittedPromotedGlobalLabels;
66
67public:
68 explicit ARMAsmPrinter(TargetMachine &TM,
69 std::unique_ptr<MCStreamer> Streamer);
70
71 StringRef getPassName() const override {
72 return "ARM Assembly Printer";
73 }
74
75 const ARMBaseTargetMachine &getTM() const;
76
77 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
78
79 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
80 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
81 const char *ExtraCode, raw_ostream &O) override;
82 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
83 const char *ExtraCode, raw_ostream &O) override;
84
85 void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
86 const MCSubtargetInfo *EndInfo,
87 const MachineInstr *MI) override;
88
89 void emitJumpTableAddrs(const MachineInstr *MI);
90 void emitJumpTableInsts(const MachineInstr *MI);
91 void emitJumpTableTBInst(const MachineInstr *MI, unsigned OffsetWidth);
92 void emitInstruction(const MachineInstr *MI) override;
93 bool runOnMachineFunction(MachineFunction &F) override;
94 std::tuple<const MCSymbol *, uint64_t, const MCSymbol *,
95 codeview::JumpTableEntrySize>
96 getCodeViewJumpTableInfo(int JTI, const MachineInstr *BranchInstr,
97 const MCSymbol *BranchLabel) const override;
98
99 void emitConstantPool() override {
100 // we emit constant pools customly!
101 }
102 void emitFunctionBodyEnd() override;
103 void emitFunctionEntryLabel() override;
104 void emitStartOfAsmFile(Module &M) override;
105 void emitEndOfAsmFile(Module &M) override;
106 void emitXXStructor(const DataLayout &DL, const Constant *CV) override;
107 void emitGlobalVariable(const GlobalVariable *GV) override;
108 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
109
110 MCSymbol *GetCPISymbol(unsigned CPID) const override;
111
112 // lowerOperand - Convert a MachineOperand into the equivalent MCOperand.
113 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
114
115 //===------------------------------------------------------------------===//
116 // XRay implementation
117 //===------------------------------------------------------------------===//
118public:
119 // XRay-specific lowering for ARM.
120 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI);
121 void LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI);
122 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI);
123
124 // KCFI check lowering
125 void LowerKCFI_CHECK(const MachineInstr &MI);
126
127private:
128 void EmitSled(const MachineInstr &MI, SledKind Kind);
129
130 // KCFI check emission helpers
131 void EmitKCFI_CHECK_ARM32(Register AddrReg, int64_t Type,
132 const MachineInstr &Call, int64_t PrefixNops);
133 void EmitKCFI_CHECK_Thumb2(Register AddrReg, int64_t Type,
134 const MachineInstr &Call, int64_t PrefixNops);
135 void EmitKCFI_CHECK_Thumb1(Register AddrReg, int64_t Type,
136 const MachineInstr &Call, int64_t PrefixNops);
137
138 // Helpers for emitStartOfAsmFile() and emitEndOfAsmFile()
139 void emitAttributes();
140
141 void EmitUnwindingInstruction(const MachineInstr *MI);
142
143 // tblgen'erated.
144 bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
145
146public:
147 unsigned getISAEncoding() override {
148 // ARM/Darwin adds ISA to the DWARF info for each function.
149 const Triple &TT = TM.getTargetTriple();
150 if (!TT.isOSBinFormatMachO())
151 return 0;
152 bool isThumb = TT.isThumb() ||
153 TT.getSubArch() == Triple::ARMSubArch_v7m ||
154 TT.getSubArch() == Triple::ARMSubArch_v6m;
155 return isThumb ? ARM::DW_ISA_ARM_thumb : ARM::DW_ISA_ARM_arm;
156 }
157
158private:
159 MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol);
160 MCSymbol *GetARMJTIPICJumpTableLabel(unsigned uid) const;
161
162 MCSymbol *GetARMGVSymbol(const GlobalValue *GV, unsigned char TargetFlags);
163
164 void emitCMSEVeneerAlias(const GlobalAlias &GA);
165
166public:
167 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
168 /// the .s file.
169 void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
170};
171
172} // end namespace llvm
173
174#endif
175