1 //===- MCLinkerOptimizationHint.h - LOH interface ---------------*- C++ -*-===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 // This file declares some helpers classes to handle Linker Optimization Hint
13 // FIXME: LOH interface supports only MachO format at the moment.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
17 #define LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/raw_ostream.h"
28 class MachObjectWriter
;
32 /// Linker Optimization Hint Type.
34 MCLOH_AdrpAdrp
= 0x1u
, ///< Adrp xY, _v1@PAGE -> Adrp xY, _v2@PAGE.
35 MCLOH_AdrpLdr
= 0x2u
, ///< Adrp _v@PAGE -> Ldr _v@PAGEOFF.
36 MCLOH_AdrpAddLdr
= 0x3u
, ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Ldr.
37 MCLOH_AdrpLdrGotLdr
= 0x4u
, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Ldr.
38 MCLOH_AdrpAddStr
= 0x5u
, ///< Adrp _v@PAGE -> Add _v@PAGEOFF -> Str.
39 MCLOH_AdrpLdrGotStr
= 0x6u
, ///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF -> Str.
40 MCLOH_AdrpAdd
= 0x7u
, ///< Adrp _v@PAGE -> Add _v@PAGEOFF.
41 MCLOH_AdrpLdrGot
= 0x8u
///< Adrp _v@GOTPAGE -> Ldr _v@GOTPAGEOFF.
44 static inline StringRef
MCLOHDirectiveName() {
45 return StringRef(".loh");
48 static inline bool isValidMCLOHType(unsigned Kind
) {
49 return Kind
>= MCLOH_AdrpAdrp
&& Kind
<= MCLOH_AdrpLdrGot
;
52 static inline int MCLOHNameToId(StringRef Name
) {
53 #define MCLOHCaseNameToId(Name) .Case(#Name, MCLOH_ ## Name)
54 return StringSwitch
<int>(Name
)
55 MCLOHCaseNameToId(AdrpAdrp
)
56 MCLOHCaseNameToId(AdrpLdr
)
57 MCLOHCaseNameToId(AdrpAddLdr
)
58 MCLOHCaseNameToId(AdrpLdrGotLdr
)
59 MCLOHCaseNameToId(AdrpAddStr
)
60 MCLOHCaseNameToId(AdrpLdrGotStr
)
61 MCLOHCaseNameToId(AdrpAdd
)
62 MCLOHCaseNameToId(AdrpLdrGot
)
64 #undef MCLOHCaseNameToId
67 static inline StringRef
MCLOHIdToName(MCLOHType Kind
) {
68 #define MCLOHCaseIdToName(Name) case MCLOH_ ## Name: return StringRef(#Name);
70 MCLOHCaseIdToName(AdrpAdrp
);
71 MCLOHCaseIdToName(AdrpLdr
);
72 MCLOHCaseIdToName(AdrpAddLdr
);
73 MCLOHCaseIdToName(AdrpLdrGotLdr
);
74 MCLOHCaseIdToName(AdrpAddStr
);
75 MCLOHCaseIdToName(AdrpLdrGotStr
);
76 MCLOHCaseIdToName(AdrpAdd
);
77 MCLOHCaseIdToName(AdrpLdrGot
);
80 #undef MCLOHCaseIdToName
83 static inline int MCLOHIdToNbArgs(MCLOHType Kind
) {
85 // LOH with two arguments
89 case MCLOH_AdrpLdrGot
:
91 // LOH with three arguments
92 case MCLOH_AdrpAddLdr
:
93 case MCLOH_AdrpLdrGotLdr
:
94 case MCLOH_AdrpAddStr
:
95 case MCLOH_AdrpLdrGotStr
:
101 /// Store Linker Optimization Hint information (LOH).
102 class MCLOHDirective
{
105 /// Arguments of this directive. Order matters.
106 SmallVector
<MCSymbol
*, 3> Args
;
108 /// Emit this directive in \p OutStream using the information available
109 /// in the given \p ObjWriter and \p Layout to get the address of the
110 /// arguments within the object file.
111 void emit_impl(raw_ostream
&OutStream
, const MachObjectWriter
&ObjWriter
,
112 const MCAsmLayout
&Layout
) const;
115 using LOHArgs
= SmallVectorImpl
<MCSymbol
*>;
117 MCLOHDirective(MCLOHType Kind
, const LOHArgs
&Args
)
118 : Kind(Kind
), Args(Args
.begin(), Args
.end()) {
119 assert(isValidMCLOHType(Kind
) && "Invalid LOH directive type!");
122 MCLOHType
getKind() const { return Kind
; }
124 const LOHArgs
&getArgs() const { return Args
; }
126 /// Emit this directive as:
127 /// <kind, numArgs, addr1, ..., addrN>
128 void emit(MachObjectWriter
&ObjWriter
, const MCAsmLayout
&Layout
) const;
130 /// Get the size in bytes of this directive if emitted in \p ObjWriter with
131 /// the given \p Layout.
132 uint64_t getEmitSize(const MachObjectWriter
&ObjWriter
,
133 const MCAsmLayout
&Layout
) const;
136 class MCLOHContainer
{
137 /// Keep track of the emit size of all the LOHs.
138 mutable uint64_t EmitSize
= 0;
140 /// Keep track of all LOH directives.
141 SmallVector
<MCLOHDirective
, 32> Directives
;
144 using LOHDirectives
= SmallVectorImpl
<MCLOHDirective
>;
146 MCLOHContainer() = default;
148 /// Const accessor to the directives.
149 const LOHDirectives
&getDirectives() const {
153 /// Add the directive of the given kind \p Kind with the given arguments
154 /// \p Args to the container.
155 void addDirective(MCLOHType Kind
, const MCLOHDirective::LOHArgs
&Args
) {
156 Directives
.push_back(MCLOHDirective(Kind
, Args
));
159 /// Get the size of the directives if emitted.
160 uint64_t getEmitSize(const MachObjectWriter
&ObjWriter
,
161 const MCAsmLayout
&Layout
) const {
163 for (const MCLOHDirective
&D
: Directives
)
164 EmitSize
+= D
.getEmitSize(ObjWriter
, Layout
);
169 /// Emit all Linker Optimization Hint in one big table.
170 /// Each line of the table is emitted by LOHDirective::emit.
171 void emit(MachObjectWriter
&ObjWriter
, const MCAsmLayout
&Layout
) const {
172 for (const MCLOHDirective
&D
: Directives
)
173 D
.emit(ObjWriter
, Layout
);
182 // Add types for specialized template using MCSymbol.
183 using MCLOHArgs
= MCLOHDirective::LOHArgs
;
184 using MCLOHDirectives
= MCLOHContainer::LOHDirectives
;
186 } // end namespace llvm
188 #endif // LLVM_MC_MCLINKEROPTIMIZATIONHINT_H