LLVM 23.0.0git
loongarch.h
Go to the documentation of this file.
1//= loongarch.h - Generic JITLink loongarch edge kinds, utilities -*- 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// Generic utilities for graphs representing loongarch objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
14#define LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
15
16#include "TableManager.h"
21#include "llvm/Support/LEB128.h"
22
23namespace llvm {
24namespace jitlink {
25namespace loongarch {
26
27/// Represents loongarch fixups.
28enum EdgeKind_loongarch : Edge::Kind {
29 /// A plain 64-bit pointer value relocation.
30 ///
31 /// Fixup expression:
32 /// Fixup <- Target + Addend : uint64
33 ///
34 Pointer64 = Edge::FirstRelocation,
35
36 /// A plain 32-bit pointer value relocation.
37 ///
38 /// Fixup expression:
39 /// Fixup <- Target + Addend : uint32
40 ///
41 /// Errors:
42 /// - The target must reside in the low 32-bits of the address space,
43 /// otherwise an out-of-range error will be returned.
44 ///
46
47 /// A 16-bit PC-relative branch.
48 ///
49 /// Represents a PC-relative branch to a target within +/-128Kb. The target
50 /// must be 4-byte aligned.
51 ///
52 /// Fixup expression:
53 /// Fixup <- (Target - Fixup + Addend) >> 2 : int16
54 ///
55 /// Notes:
56 /// The '16' in the name refers to the number operand bits and follows the
57 /// naming convention used by the corresponding ELF relocations. Since the low
58 /// two bits must be zero (because of the 4-byte alignment of the target) the
59 /// operand is effectively a signed 18-bit number.
60 ///
61 /// Errors:
62 /// - The result of the unshifted part of the fixup expression must be
63 /// 4-byte aligned otherwise an alignment error will be returned.
64 /// - The result of the fixup expression must fit into an int16 otherwise an
65 /// out-of-range error will be returned.
66 ///
68
69 /// A 21-bit PC-relative branch.
70 ///
71 /// Represents a PC-relative branch to a target within +/-4Mb. The Target must
72 /// be 4-byte aligned.
73 ///
74 /// Fixup expression:
75 /// Fixup <- (Target - Fixup + Addend) >> 2 : int21
76 ///
77 /// Notes:
78 /// The '21' in the name refers to the number operand bits and follows the
79 /// naming convention used by the corresponding ELF relocations. Since the low
80 /// two bits must be zero (because of the 4-byte alignment of the target) the
81 /// operand is effectively a signed 23-bit number.
82 ///
83 /// Errors:
84 /// - The result of the unshifted part of the fixup expression must be
85 /// 4-byte aligned otherwise an alignment error will be returned.
86 /// - The result of the fixup expression must fit into an int21 otherwise an
87 /// out-of-range error will be returned.
88 ///
90
91 /// A 26-bit PC-relative branch.
92 ///
93 /// Represents a PC-relative call or branch to a target within +/-128Mb. The
94 /// target must be 4-byte aligned.
95 ///
96 /// Fixup expression:
97 /// Fixup <- (Target - Fixup + Addend) >> 2 : int26
98 ///
99 /// Notes:
100 /// The '26' in the name refers to the number operand bits and follows the
101 /// naming convention used by the corresponding ELF relocations. Since the low
102 /// two bits must be zero (because of the 4-byte alignment of the target) the
103 /// operand is effectively a signed 28-bit number.
104 ///
105 /// Errors:
106 /// - The result of the unshifted part of the fixup expression must be
107 /// 4-byte aligned otherwise an alignment error will be returned.
108 /// - The result of the fixup expression must fit into an int26 otherwise an
109 /// out-of-range error will be returned.
110 ///
112
113 /// A 32-bit delta.
114 ///
115 /// Delta from the fixup to the target.
116 ///
117 /// Fixup expression:
118 /// Fixup <- Target - Fixup + Addend : int32
119 ///
120 /// Errors:
121 /// - The result of the fixup expression must fit into an int32, otherwise
122 /// an out-of-range error will be returned.
123 ///
125
126 /// A 32-bit negative delta.
127 ///
128 /// Delta from the target back to the fixup.
129 ///
130 /// Fixup expression:
131 /// Fixup <- Fixup - Target + Addend : int32
132 ///
133 /// Errors:
134 /// - The result of the fixup expression must fit into an int32, otherwise
135 /// an out-of-range error will be returned.
136 ///
138
139 /// A 64-bit delta.
140 ///
141 /// Delta from the fixup to the target.
142 ///
143 /// Fixup expression:
144 /// Fixup <- Target - Fixup + Addend : int64
145 ///
147
148 /// The signed 20-bit delta from the fixup page to the page containing the
149 /// target.
150 ///
151 /// Fixup expression:
152 /// Fixup <- (((Target + Addend + ((Target + Addend) & 0x800)) & ~0xfff)
153 // - (Fixup & ~0xfff)) >> 12 : int20
154 ///
155 /// Notes:
156 /// For PCALAU12I fixups.
157 ///
158 /// Errors:
159 /// - The result of the fixup expression must fit into an int20 otherwise an
160 /// out-of-range error will be returned.
161 ///
163
164 /// The 12-bit offset of the target within its page.
165 ///
166 /// Typically used to fix up ADDI/LD_W/LD_D immediates.
167 ///
168 /// Fixup expression:
169 /// Fixup <- ((Target + Addend) >> Shift) & 0xfff : int12
170 ///
172
173 /// A GOT entry getter/constructor, transformed to Page20 pointing at the GOT
174 /// entry for the original target.
175 ///
176 /// Indicates that this edge should be transformed into a Page20 targeting
177 /// the GOT entry for the edge's current target, maintaining the same addend.
178 /// A GOT entry for the target should be created if one does not already
179 /// exist.
180 ///
181 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
182 /// by default.
183 ///
184 /// Fixup expression:
185 /// NONE
186 ///
187 /// Errors:
188 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
189 /// phase will result in an assert/unreachable during the fixup phase.
190 ///
192
193 /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
194 /// the GOT entry for the original target.
195 ///
196 /// Indicates that this edge should be transformed into a PageOffset12
197 /// targeting the GOT entry for the edge's current target, maintaining the
198 /// same addend. A GOT entry for the target should be created if one does not
199 /// already exist.
200 ///
201 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
202 /// by default.
203 ///
204 /// Fixup expression:
205 /// NONE
206 ///
208
209 /// A 36-bit PC-relative call.
210 ///
211 /// Represents a PC-relative call to a target within [-128G - 0x20000, +128G
212 /// - 0x20000). The target must be 4-byte aligned. For adjacent pcaddu18i+jirl
213 /// instruction pairs.
214 ///
215 /// Fixup expression:
216 /// Fixup <- (Target - Fixup + Addend) >> 2 : int36
217 ///
218 /// Notes:
219 /// The '36' in the name refers to the number operand bits and follows the
220 /// naming convention used by the corresponding ELF relocations. Since the low
221 /// two bits must be zero (because of the 4-byte alignment of the target) the
222 /// operand is effectively a signed 38-bit number.
223 ///
224 /// Errors:
225 /// - The result of the unshifted part of the fixup expression must be
226 /// 4-byte aligned otherwise an alignment error will be returned.
227 /// - The result of the fixup expression must fit into an int36 otherwise an
228 /// out-of-range error will be returned.
229 ///
231
232 /// low 6 bits label addition
233 ///
234 /// Fixup expression:
235 /// Fixup <- (*{1}Fixup + (Target + Addend) & 0x3f) : int8
236 ///
238
239 /// 8 bits label addition
240 ///
241 /// Fixup expression:
242 /// Fixup <- (*{1}Fixup + Target + Addend) : int8
243 ///
245
246 /// 16 bits label addition
247 ///
248 /// Fixup expression:
249 /// Fixup <- (*{2}Fixup + Target + Addend) : int16
250 ///
252
253 /// 32 bits label addition
254 ///
255 /// Fixup expression:
256 /// Fixup <- (*{4}Fixup + Target + Addend) : int32
257 ///
259
260 /// 64 bits label addition
261 ///
262 /// Fixup expression:
263 /// Fixup <- (*{8}Fixup + Target + Addend) : int64
264 ///
266
267 /// ULEB128 bits label addition
268 ///
269 /// Fixup expression:
270 /// Fixup <- (Fixup + Target + Addend) : uleb128
271 ///
273
274 /// low 6 bits label subtraction
275 ///
276 /// Fixup expression:
277 /// Fixup <- (*{1}Fixup - (Target + Addend) & 0x3f) : int8
278 ///
280
281 /// 8 bits label subtraction
282 ///
283 /// Fixup expression:
284 /// Fixup <- (*{1}Fixup - Target - Addend) : int8
285 ///
287
288 /// 16 bits label subtraction
289 ///
290 /// Fixup expression:
291 /// Fixup <- (*{2}Fixup - Target - Addend) : int16
292 ///
294
295 /// 32 bits label subtraction
296 ///
297 /// Fixup expression:
298 /// Fixup <- (*{4}Fixup - Target - Addend) : int32
299 ///
301
302 /// 64 bits label subtraction
303 ///
304 /// Fixup expression:
305 /// Fixup <- (*{8}Fixup - Target - Addend) : int64
306 ///
308
309 /// ULEB128 bits label subtraction
310 ///
311 /// Fixup expression:
312 /// Fixup <- (Fixup - Target - Addend) : uleb128
313 ///
315
316 /// Alignment requirement used by linker relaxation.
317 ///
318 /// Linker relaxation will use this to ensure all code sequences are properly
319 /// aligned and then remove these edges from the graph.
320 ///
322};
323
324/// Returns a string name for the given loongarch edge. For debugging purposes
325/// only.
326LLVM_ABI const char *getEdgeKindName(Edge::Kind K);
327
328// Returns extract bits Val[Hi:Lo].
329inline uint32_t extractBits(uint64_t Val, unsigned Hi, unsigned Lo) {
330 return Hi == 63 ? Val >> Lo : (Val & ((((uint64_t)1 << (Hi + 1)) - 1))) >> Lo;
331}
332
333/// loongarch null pointer content.
334LLVM_ABI extern const char NullPointerContent[8];
336 return {reinterpret_cast<const char *>(NullPointerContent),
337 G.getPointerSize()};
338}
339
340/// loongarch stub content.
341///
342/// Contains the instruction sequence for an indirect jump via an in-memory
343/// pointer:
344/// pcalau12i $t8, %page20(ptr)
345/// ld.[w/d] $t8, %pageoff12(ptr)
346/// jr $t8
347constexpr size_t StubEntrySize = 12;
351 auto StubContent =
352 G.getPointerSize() == 8 ? LA64StubContent : LA32StubContent;
353 return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
354}
355
356/// Creates a new pointer block in the given section and returns an
357/// Anonymous symbol pointing to it.
358///
359/// If InitialTarget is given then an Pointer64 relocation will be added to the
360/// block pointing at InitialTarget.
361///
362/// The pointer block will have the following default values:
363/// alignment: PointerSize
364/// alignment-offset: 0
366 Symbol *InitialTarget = nullptr,
367 uint64_t InitialAddend = 0) {
368 auto &B = G.createContentBlock(PointerSection, getGOTEntryBlockContent(G),
369 orc::ExecutorAddr(), G.getPointerSize(), 0);
370 if (InitialTarget)
371 B.addEdge(G.getPointerSize() == 8 ? Pointer64 : Pointer32, 0,
372 *InitialTarget, InitialAddend);
373 return G.addAnonymousSymbol(B, 0, G.getPointerSize(), false, false);
374}
375
376/// Create a jump stub that jumps via the pointer at the given symbol and
377/// an anonymous symbol pointing to it. Return the anonymous symbol.
379 Section &StubSection,
380 Symbol &PointerSymbol) {
381 Block &StubContentBlock = G.createContentBlock(
382 StubSection, getStubBlockContent(G), orc::ExecutorAddr(), 4, 0);
383 StubContentBlock.addEdge(Page20, 0, PointerSymbol, 0);
384 StubContentBlock.addEdge(PageOffset12, 4, PointerSymbol, 0);
385 return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, false);
386}
387
388/// Global Offset Table Builder.
389class GOTTableManager : public TableManager<GOTTableManager> {
390public:
391 static StringRef getSectionName() { return "$__GOT"; }
392
394 Edge::Kind KindToSet = Edge::Invalid;
395 switch (E.getKind()) {
397 KindToSet = Page20;
398 break;
400 KindToSet = PageOffset12;
401 break;
402 default:
403 return false;
404 }
405 assert(KindToSet != Edge::Invalid &&
406 "Fell through switch, but no new kind to set");
407 DEBUG_WITH_TYPE("jitlink", {
408 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
409 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
410 << formatv("{0:x}", E.getOffset()) << ")\n";
411 });
412 E.setKind(KindToSet);
413 E.setTarget(getEntryForTarget(G, E.getTarget()));
414 return true;
415 }
416
418 return createAnonymousPointer(G, getGOTSection(G), &Target);
419 }
420
421private:
422 Section &getGOTSection(LinkGraph &G) {
423 if (!GOTSection)
424 GOTSection = &G.createSection(getSectionName(),
426 return *GOTSection;
427 }
428
429 Section *GOTSection = nullptr;
430};
431
432/// Procedure Linkage Table Builder.
433class PLTTableManager : public TableManager<PLTTableManager> {
434public:
436
437 static StringRef getSectionName() { return "$__STUBS"; }
438
440 if ((E.getKind() == Branch26PCRel || E.getKind() == Call36PCRel) &&
441 !E.getTarget().isDefined()) {
442 DEBUG_WITH_TYPE("jitlink", {
443 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
444 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
445 << formatv("{0:x}", E.getOffset()) << ")\n";
446 });
447 E.setTarget(getEntryForTarget(G, E.getTarget()));
448 return true;
449 }
450 return false;
451 }
452
455 GOT.getEntryForTarget(G, Target));
456 }
457
458public:
465
468};
469
470} // namespace loongarch
471} // namespace jitlink
472} // namespace llvm
473
474#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define G(x, y, z)
Definition MD5.cpp:55
This file contains some functions that are useful when dealing with strings.
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Target - Wrapper for Target specific information.
Represents an address in the executor process.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207