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 /// The upper 20 bits of the offset from the fixup to the target.
174 ///
175 /// Fixup expression:
176 /// Fixup <- (Target + Addend - Fixup + 0x800) >> 12 : int20
177 ///
178 /// Notes:
179 /// For PCADDU12I fixups.
180 ///
181 /// Errors:
182 /// - The result of the fixup expression must fit into an int20 otherwise an
183 /// out-of-range error will be returned.
184 ///
186
187 /// The lower 12 bits of the offset from the paired PCADDU12I (the initial
188 /// target) to the final target it points to.
189 ///
190 /// Typically used to fix up ADDI/LD_W/LD_D immediates.
191 ///
192 /// Fixup expression:
193 /// Fixup <- (FinalTarget - InitialTarget) & 0xfff : int12
194 ///
196
197 /// A GOT entry getter/constructor, transformed to Page20 pointing at the GOT
198 /// entry for the original target.
199 ///
200 /// Indicates that this edge should be transformed into a Page20 targeting
201 /// the GOT entry for the edge's current target, maintaining the same addend.
202 /// A GOT entry for the target should be created if one does not already
203 /// exist.
204 ///
205 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
206 /// by default.
207 ///
208 /// Fixup expression:
209 /// NONE
210 ///
211 /// Errors:
212 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
213 /// phase will result in an assert/unreachable during the fixup phase.
214 ///
216
217 /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
218 /// the GOT entry for the original target.
219 ///
220 /// Indicates that this edge should be transformed into a PageOffset12
221 /// targeting the GOT entry for the edge's current target, maintaining the
222 /// same addend. A GOT entry for the target should be created if one does not
223 /// already exist.
224 ///
225 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
226 /// by default.
227 ///
228 /// Fixup expression:
229 /// NONE
230 ///
232
233 /// A GOT entry getter/constructor, transformed to PCAddHi20 pointing at the
234 /// GOT entry for the original target.
235 ///
236 /// Indicates that this edge should be transformed into a PCAddHi20 targeting
237 /// the GOT entry for the edge's current target, maintaining the same addend.
238 /// A GOT entry for the target should be created if one does not already
239 /// exist.
240 ///
241 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
242 /// by default.
243 ///
244 /// Fixup expression:
245 /// NONE
246 ///
247 /// Errors:
248 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
249 /// phase will result in an assert/unreachable during the fixup phase.
250 ///
252
253 /// A 30-bit PC-relative call.
254 ///
255 /// Represents a PC-relative call to a target within [-4G, +4G)
256 /// The target must be 4-byte aligned. For adjacent pcaddu12i+jirl
257 /// instruction pairs.
258 ///
259 /// Fixup expression:
260 /// Fixup <- (Target - Fixup + Addend) >> 2 : int30
261 ///
262 /// Notes:
263 /// The '30' in the name refers to the number operand bits and follows the
264 /// naming convention used by the corresponding ELF relocations. Since the low
265 /// two bits must be zero (because of the 4-byte alignment of the target) the
266 /// operand is effectively a signed 32-bit number.
267 ///
268 /// Errors:
269 /// - The result of the unshifted part of the fixup expression must be
270 /// 4-byte aligned otherwise an alignment error will be returned.
271 /// - The result of the fixup expression must fit into an int30 otherwise an
272 /// out-of-range error will be returned.
273 ///
275
276 /// A 36-bit PC-relative call.
277 ///
278 /// Represents a PC-relative call to a target within [-128G - 0x20000, +128G
279 /// - 0x20000). The target must be 4-byte aligned. For adjacent pcaddu18i+jirl
280 /// instruction pairs.
281 ///
282 /// Fixup expression:
283 /// Fixup <- (Target - Fixup + Addend) >> 2 : int36
284 ///
285 /// Notes:
286 /// The '36' in the name refers to the number operand bits and follows the
287 /// naming convention used by the corresponding ELF relocations. Since the low
288 /// two bits must be zero (because of the 4-byte alignment of the target) the
289 /// operand is effectively a signed 38-bit number.
290 ///
291 /// Errors:
292 /// - The result of the unshifted part of the fixup expression must be
293 /// 4-byte aligned otherwise an alignment error will be returned.
294 /// - The result of the fixup expression must fit into an int36 otherwise an
295 /// out-of-range error will be returned.
296 ///
298
299 /// low 6 bits label addition
300 ///
301 /// Fixup expression:
302 /// Fixup <- (*{1}Fixup + (Target + Addend) & 0x3f) : int8
303 ///
305
306 /// 8 bits label addition
307 ///
308 /// Fixup expression:
309 /// Fixup <- (*{1}Fixup + Target + Addend) : int8
310 ///
312
313 /// 16 bits label addition
314 ///
315 /// Fixup expression:
316 /// Fixup <- (*{2}Fixup + Target + Addend) : int16
317 ///
319
320 /// 32 bits label addition
321 ///
322 /// Fixup expression:
323 /// Fixup <- (*{4}Fixup + Target + Addend) : int32
324 ///
326
327 /// 64 bits label addition
328 ///
329 /// Fixup expression:
330 /// Fixup <- (*{8}Fixup + Target + Addend) : int64
331 ///
333
334 /// ULEB128 bits label addition
335 ///
336 /// Fixup expression:
337 /// Fixup <- (Fixup + Target + Addend) : uleb128
338 ///
340
341 /// low 6 bits label subtraction
342 ///
343 /// Fixup expression:
344 /// Fixup <- (*{1}Fixup - (Target + Addend) & 0x3f) : int8
345 ///
347
348 /// 8 bits label subtraction
349 ///
350 /// Fixup expression:
351 /// Fixup <- (*{1}Fixup - Target - Addend) : int8
352 ///
354
355 /// 16 bits label subtraction
356 ///
357 /// Fixup expression:
358 /// Fixup <- (*{2}Fixup - Target - Addend) : int16
359 ///
361
362 /// 32 bits label subtraction
363 ///
364 /// Fixup expression:
365 /// Fixup <- (*{4}Fixup - Target - Addend) : int32
366 ///
368
369 /// 64 bits label subtraction
370 ///
371 /// Fixup expression:
372 /// Fixup <- (*{8}Fixup - Target - Addend) : int64
373 ///
375
376 /// ULEB128 bits label subtraction
377 ///
378 /// Fixup expression:
379 /// Fixup <- (Fixup - Target - Addend) : uleb128
380 ///
382
383 /// Alignment requirement used by linker relaxation.
384 ///
385 /// Linker relaxation will use this to ensure all code sequences are properly
386 /// aligned and then remove these edges from the graph.
387 ///
389};
390
391/// Returns a string name for the given loongarch edge. For debugging purposes
392/// only.
393LLVM_ABI const char *getEdgeKindName(Edge::Kind K);
394
395// Returns extract bits Val[Hi:Lo].
396inline uint32_t extractBits(uint64_t Val, unsigned Hi, unsigned Lo) {
397 return Hi == 63 ? Val >> Lo : (Val & ((((uint64_t)1 << (Hi + 1)) - 1))) >> Lo;
398}
399
400/// loongarch null pointer content.
401LLVM_ABI extern const char NullPointerContent[8];
403 return {reinterpret_cast<const char *>(NullPointerContent),
404 G.getPointerSize()};
405}
406
407/// loongarch stub content.
408///
409/// Contains the instruction sequence for an indirect jump via an in-memory
410/// pointer:
411/// pcalau12i $t8, %page20(ptr)
412/// ld.[w/d] $t8, %pageoff12(ptr)
413/// jr $t8
414constexpr size_t StubEntrySize = 12;
418 auto StubContent =
419 G.getPointerSize() == 8 ? LA64StubContent : LA32StubContent;
420 return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
421}
422
423/// Creates a new pointer block in the given section and returns an
424/// Anonymous symbol pointing to it.
425///
426/// If InitialTarget is given then an Pointer64 relocation will be added to the
427/// block pointing at InitialTarget.
428///
429/// The pointer block will have the following default values:
430/// alignment: PointerSize
431/// alignment-offset: 0
433 Symbol *InitialTarget = nullptr,
434 uint64_t InitialAddend = 0) {
435 auto &B = G.createContentBlock(PointerSection, getGOTEntryBlockContent(G),
436 orc::ExecutorAddr(), G.getPointerSize(), 0);
437 if (InitialTarget)
438 B.addEdge(G.getPointerSize() == 8 ? Pointer64 : Pointer32, 0,
439 *InitialTarget, InitialAddend);
440 return G.addAnonymousSymbol(B, 0, G.getPointerSize(), false, false);
441}
442
443/// Create a jump stub that jumps via the pointer at the given symbol and
444/// an anonymous symbol pointing to it. Return the anonymous symbol.
446 Section &StubSection,
447 Symbol &PointerSymbol) {
448 Block &StubContentBlock = G.createContentBlock(
449 StubSection, getStubBlockContent(G), orc::ExecutorAddr(), 4, 0);
450 StubContentBlock.addEdge(Page20, 0, PointerSymbol, 0);
451 StubContentBlock.addEdge(PageOffset12, 4, PointerSymbol, 0);
452 return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, false);
453}
454
455/// Global Offset Table Builder.
456class GOTTableManager : public TableManager<GOTTableManager> {
457public:
458 static StringRef getSectionName() { return "$__GOT"; }
459
461 Edge::Kind KindToSet = Edge::Invalid;
462 switch (E.getKind()) {
464 KindToSet = Page20;
465 break;
467 KindToSet = PageOffset12;
468 break;
470 KindToSet = PCAddHi20;
471 break;
472 default:
473 return false;
474 }
475 assert(KindToSet != Edge::Invalid &&
476 "Fell through switch, but no new kind to set");
477 DEBUG_WITH_TYPE("jitlink", {
478 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
479 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
480 << formatv("{0:x}", E.getOffset()) << ")\n";
481 });
482 E.setKind(KindToSet);
483 E.setTarget(getEntryForTarget(G, E.getTarget()));
484 return true;
485 }
486
488 return createAnonymousPointer(G, getGOTSection(G), &Target);
489 }
490
491private:
492 Section &getGOTSection(LinkGraph &G) {
493 if (!GOTSection)
494 GOTSection = &G.createSection(getSectionName(),
496 return *GOTSection;
497 }
498
499 Section *GOTSection = nullptr;
500};
501
502/// Procedure Linkage Table Builder.
503class PLTTableManager : public TableManager<PLTTableManager> {
504public:
506
507 static StringRef getSectionName() { return "$__STUBS"; }
508
510 if ((E.getKind() == Branch26PCRel || E.getKind() == Call36PCRel ||
511 E.getKind() == Call30PCRel) &&
512 !E.getTarget().isDefined()) {
513 DEBUG_WITH_TYPE("jitlink", {
514 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
515 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
516 << formatv("{0:x}", E.getOffset()) << ")\n";
517 });
518 E.setTarget(getEntryForTarget(G, E.getTarget()));
519 return true;
520 }
521 return false;
522 }
523
526 GOT.getEntryForTarget(G, Target));
527 }
528
529public:
536
539};
540
541} // namespace loongarch
542} // namespace jitlink
543} // namespace llvm
544
545#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