LLVM 23.0.0git
RISCVAsmBackend.cpp
Go to the documentation of this file.
1//===-- RISCVAsmBackend.cpp - RISC-V Assembler Backend --------------------===//
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#include "RISCVAsmBackend.h"
10#include "RISCVFixupKinds.h"
11#include "llvm/ADT/APInt.h"
12#include "llvm/MC/MCAsmInfo.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/MC/MCValue.h"
24#include "llvm/Support/LEB128.h"
26
27using namespace llvm;
28
29// Temporary workaround for old linkers that do not support ULEB128 relocations,
30// which are abused by DWARF v5 DW_LLE_offset_pair/DW_RLE_offset_pair
31// implemented in Clang/LLVM.
33 "riscv-uleb128-reloc", cl::init(true), cl::Hidden,
34 cl::desc("Emit R_RISCV_SET_ULEB128/E_RISCV_SUB_ULEB128 if appropriate"));
35
36static cl::opt<bool>
37 AlignRvc("riscv-align-rvc", cl::init(true), cl::Hidden,
38 cl::desc("When generating R_RISCV_ALIGN, insert $alignment-2 "
39 "bytes of NOPs even in norvc code"));
40
42 bool Is64Bit, bool IsLittleEndian,
44 : MCAsmBackend(IsLittleEndian ? llvm::endianness::little
45 : llvm::endianness::big),
47 RISCVFeatures::validate(STI.getTargetTriple(), STI.getFeatureBits());
48}
49
50std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
51 if (STI.getTargetTriple().isOSBinFormatELF()) {
52 unsigned Type;
54#define ELF_RELOC(NAME, ID) .Case(#NAME, ID)
55#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
56#undef ELF_RELOC
57#define ELF_RISCV_NONSTANDARD_RELOC(_VENDOR, NAME, ID) .Case(#NAME, ID)
58#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
59#undef ELF_RISCV_NONSTANDARD_RELOC
60 .Case("BFD_RELOC_NONE", ELF::R_RISCV_NONE)
61 .Case("BFD_RELOC_32", ELF::R_RISCV_32)
62 .Case("BFD_RELOC_64", ELF::R_RISCV_64)
63 .Default(-1u);
64 if (Type != -1u)
65 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
66 }
67 return std::nullopt;
68}
69
71 const static MCFixupKindInfo Infos[] = {
72 // This table *must* be in the order that the fixup_* kinds are defined in
73 // RISCVFixupKinds.h.
74 //
75 // name offset bits flags
76 {"fixup_riscv_hi20", 12, 20, 0},
77 {"fixup_riscv_lo12_i", 20, 12, 0},
78 {"fixup_riscv_12_i", 20, 12, 0},
79 {"fixup_riscv_lo12_s", 0, 32, 0},
80 {"fixup_riscv_pcrel_hi20", 12, 20, 0},
81 {"fixup_riscv_pcrel_lo12_i", 20, 12, 0},
82 {"fixup_riscv_pcrel_lo12_s", 0, 32, 0},
83 {"fixup_riscv_jal", 12, 20, 0},
84 {"fixup_riscv_branch", 0, 32, 0},
85 {"fixup_riscv_rvc_jump", 2, 11, 0},
86 {"fixup_riscv_rvc_branch", 0, 16, 0},
87 {"fixup_riscv_rvc_imm", 0, 16, 0},
88 {"fixup_riscv_call", 0, 64, 0},
89 {"fixup_riscv_call_plt", 0, 64, 0},
90
91 // Qualcomm fixups
92 {"fixup_riscv_qc_e_branch", 0, 48, 0},
93 {"fixup_riscv_qc_e_32", 16, 32, 0},
94 {"fixup_riscv_qc_abs20_u", 0, 32, 0},
95 {"fixup_riscv_qc_e_call_plt", 0, 48, 0},
96 {"fixup_qc_access_16", 0, 0, 0},
97 {"fixup_qc_access_32", 0, 0, 0},
98
99 // Andes fixups
100 {"fixup_riscv_nds_branch_10", 0, 32, 0},
101 };
102 static_assert((std::size(Infos)) == RISCV::NumTargetFixupKinds,
103 "Not all fixup kinds added to Infos array");
104
105 // Fixup kinds from raw relocation types and .reloc directives force
106 // relocations and do not use these fields.
107 if (mc::isRelocation(Kind))
108 return {};
109
110 if (Kind < FirstTargetFixupKind)
112
114 "Invalid kind!");
115 return Infos[Kind - FirstTargetFixupKind];
116}
117
119 const MCFixup &Fixup,
120 const MCValue &,
122 bool Resolved) const {
123 int64_t Offset = int64_t(Value);
124 auto Kind = Fixup.getKind();
125
126 // Return true if the symbol is unresolved.
127 if (!Resolved)
128 return true;
129
130 switch (Kind) {
131 default:
132 return false;
134 // For compressed branch instructions the immediate must be
135 // in the range [-256, 254].
136 return Offset > 254 || Offset < -256;
138 // For compressed jump instructions the immediate must be
139 // in the range [-2048, 2046].
140 return Offset > 2046 || Offset < -2048;
143 // For conditional branch instructions the immediate must be
144 // in the range [-4096, 4094].
145 return Offset > 4094 || Offset < -4096;
147 // For jump instructions the immediate must be in the range
148 // [-1048576, 1048574]
149 return Offset > 1048574 || Offset < -1048576;
151 // This fixup can never be emitted as a relocation, so always needs to be
152 // relaxed.
153 return true;
154 }
155}
156
157// Given a compressed control flow instruction this function returns
158// the expanded instruction, or the original instruction code if no
159// expansion is available.
160static unsigned getRelaxedOpcode(unsigned Opcode, ArrayRef<MCOperand> Operands,
161 const MCSubtargetInfo &STI) {
162 switch (Opcode) {
163 case RISCV::C_BEQZ:
164 return RISCV::BEQ;
165 case RISCV::C_BNEZ:
166 return RISCV::BNE;
167 case RISCV::C_J:
168 case RISCV::C_JAL: // fall through.
169 // This only relaxes one "step" - i.e. from C.J to JAL, not from C.J to
170 // QC.E.J, because we can always relax again if needed.
171 return RISCV::JAL;
172 case RISCV::C_LI:
173 if (!STI.hasFeature(RISCV::FeatureVendorXqcili))
174 break;
175 // We only need this because `QC.E.LI` can be compressed into a `C.LI`. This
176 // happens because the `simm6` MCOperandPredicate accepts bare symbols, and
177 // `QC.E.LI` is the only instruction that accepts bare symbols at parse-time
178 // and compresses to `C.LI`. `C.LI` does not itself accept bare symbols at
179 // parse time.
180 //
181 // If we have a bare symbol, we need to turn this back to a `QC.E.LI`, as we
182 // have no way to emit a relocation on a `C.LI` instruction.
183 return RISCV::QC_E_LI;
184 case RISCV::JAL: {
185 // We can only relax JAL if we have Xqcilb
186 if (!STI.hasFeature(RISCV::FeatureVendorXqcilb))
187 break;
188
189 // And only if it is using X0 or X1 for rd.
190 MCRegister Reg = Operands[0].getReg();
191 if (Reg == RISCV::X0)
192 return RISCV::QC_E_J;
193 if (Reg == RISCV::X1)
194 return RISCV::QC_E_JAL;
195
196 break;
197 }
198 case RISCV::BEQ:
199 return RISCV::PseudoLongBEQ;
200 case RISCV::BNE:
201 return RISCV::PseudoLongBNE;
202 case RISCV::BEQI:
203 return RISCV::PseudoLongBEQI;
204 case RISCV::BNEI:
205 return RISCV::PseudoLongBNEI;
206 case RISCV::BLT:
207 return RISCV::PseudoLongBLT;
208 case RISCV::BGE:
209 return RISCV::PseudoLongBGE;
210 case RISCV::BLTU:
211 return RISCV::PseudoLongBLTU;
212 case RISCV::BGEU:
213 return RISCV::PseudoLongBGEU;
214 case RISCV::QC_BEQI:
215 return RISCV::PseudoLongQC_BEQI;
216 case RISCV::QC_BNEI:
217 return RISCV::PseudoLongQC_BNEI;
218 case RISCV::QC_BLTI:
219 return RISCV::PseudoLongQC_BLTI;
220 case RISCV::QC_BGEI:
221 return RISCV::PseudoLongQC_BGEI;
222 case RISCV::QC_BLTUI:
223 return RISCV::PseudoLongQC_BLTUI;
224 case RISCV::QC_BGEUI:
225 return RISCV::PseudoLongQC_BGEUI;
226 case RISCV::QC_E_BEQI:
227 return RISCV::PseudoLongQC_E_BEQI;
228 case RISCV::QC_E_BNEI:
229 return RISCV::PseudoLongQC_E_BNEI;
230 case RISCV::QC_E_BLTI:
231 return RISCV::PseudoLongQC_E_BLTI;
232 case RISCV::QC_E_BGEI:
233 return RISCV::PseudoLongQC_E_BGEI;
234 case RISCV::QC_E_BLTUI:
235 return RISCV::PseudoLongQC_E_BLTUI;
236 case RISCV::QC_E_BGEUI:
237 return RISCV::PseudoLongQC_E_BGEUI;
238 }
239
240 // Returning the original opcode means we cannot relax the instruction.
241 return Opcode;
242}
243
245 const MCSubtargetInfo &STI) const {
246 if (STI.hasFeature(RISCV::FeatureExactAssembly))
247 return;
248
249 MCInst Res;
250 switch (Inst.getOpcode()) {
251 default:
252 llvm_unreachable("Opcode not expected!");
253 case RISCV::C_BEQZ:
254 case RISCV::C_BNEZ:
255 case RISCV::C_J:
256 case RISCV::C_JAL: {
257 [[maybe_unused]] bool Success = RISCVRVC::uncompress(Res, Inst, STI);
258 assert(Success && "Can't uncompress instruction");
259 assert(Res.getOpcode() ==
260 getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI) &&
261 "Branch Relaxation Error");
262 break;
263 }
264 case RISCV::JAL: {
265 // This has to be written manually because the QC.E.J -> JAL is
266 // compression-only, so that it is not used when printing disassembly.
267 assert(STI.hasFeature(RISCV::FeatureVendorXqcilb) &&
268 "JAL is only relaxable with Xqcilb");
269 assert((Inst.getOperand(0).getReg() == RISCV::X0 ||
270 Inst.getOperand(0).getReg() == RISCV::X1) &&
271 "JAL only relaxable with rd=x0 or rd=x1");
272 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
273 Res.addOperand(Inst.getOperand(1));
274 break;
275 }
276 case RISCV::C_LI: {
277 // This should only be hit when trying to relax a `C.LI` into a `QC.E.LI`
278 // because the `C.LI` has a bare symbol. We cannot use
279 // `RISCVRVC::uncompress` because it will use decompression patterns. The
280 // `QC.E.LI` compression pattern to `C.LI` is compression-only (because we
281 // don't want `c.li` ever printed as `qc.e.li`, which might be done if the
282 // pattern applied to decompression), but that doesn't help much becuase
283 // `C.LI` with a bare symbol will decompress to an `ADDI` anyway (because
284 // `simm12`'s MCOperandPredicate accepts a bare symbol and that pattern
285 // comes first), and we still cannot emit an `ADDI` with a bare symbol.
286 assert(STI.hasFeature(RISCV::FeatureVendorXqcili) &&
287 "C.LI is only relaxable with Xqcili");
288 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
289 Res.addOperand(Inst.getOperand(0));
290 Res.addOperand(Inst.getOperand(1));
291 break;
292 }
293 case RISCV::BEQ:
294 case RISCV::BNE:
295 case RISCV::BEQI:
296 case RISCV::BNEI:
297 case RISCV::BLT:
298 case RISCV::BGE:
299 case RISCV::BLTU:
300 case RISCV::BGEU:
301 case RISCV::QC_BEQI:
302 case RISCV::QC_BNEI:
303 case RISCV::QC_BLTI:
304 case RISCV::QC_BGEI:
305 case RISCV::QC_BLTUI:
306 case RISCV::QC_BGEUI:
307 case RISCV::QC_E_BEQI:
308 case RISCV::QC_E_BNEI:
309 case RISCV::QC_E_BLTI:
310 case RISCV::QC_E_BGEI:
311 case RISCV::QC_E_BLTUI:
312 case RISCV::QC_E_BGEUI:
313 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
314 Res.addOperand(Inst.getOperand(0));
315 Res.addOperand(Inst.getOperand(1));
316 Res.addOperand(Inst.getOperand(2));
317 break;
318 }
319 Inst = std::move(Res);
320}
321
322// Check if an R_RISCV_ALIGN relocation is needed for an alignment directive.
323// If conditions are met, compute the padding size and create a fixup encoding
324// the padding size in the addend.
326 // Alignments before the first linker-relaxable instruction have fixed sizes
327 // and do not require relocations. Alignments after a linker-relaxable
328 // instruction require a relocation, even if the STI specifies norelax.
329 //
330 // firstLinkerRelaxable is the layout order within the subsection, which may
331 // be smaller than the section's order. Therefore, alignments in a
332 // lower-numbered subsection may be unnecessarily treated as linker-relaxable.
333 auto *Sec = F.getParent();
334 if (F.getLayoutOrder() <= Sec->firstLinkerRelaxable())
335 return false;
336
337 // Use default handling unless the alignment is larger than the nop size.
338 const MCSubtargetInfo *STI = F.getSubtargetInfo();
339 unsigned MinNopLen =
340 AlignRvc || STI->hasFeature(RISCV::FeatureStdExtZca) ? 2 : 4;
341 if (F.getAlignment() <= MinNopLen)
342 return false;
343
344 Size = F.getAlignment().value() - MinNopLen;
345 auto *Expr = MCConstantExpr::create(Size, getContext());
346 MCFixup Fixup =
347 MCFixup::create(0, Expr, FirstLiteralRelocationKind + ELF::R_RISCV_ALIGN);
348 F.setVarFixups({Fixup});
349 F.setLinkerRelaxable();
350 return true;
351}
352
354 int64_t LineDelta = F.getDwarfLineDelta();
355 const MCExpr &AddrDelta = F.getDwarfAddrDelta();
356 int64_t Value;
357 // If the label difference can be resolved, use the default handling, which
358 // utilizes a shorter special opcode.
359 if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
360 return false;
361 [[maybe_unused]] bool IsAbsolute =
362 AddrDelta.evaluateKnownAbsolute(Value, *Asm);
363 assert(IsAbsolute && "CFA with invalid expression");
364
367
368 // INT64_MAX is a signal that this is actually a DW_LNE_end_sequence.
369 if (LineDelta != INT64_MAX) {
370 OS << uint8_t(dwarf::DW_LNS_advance_line);
371 encodeSLEB128(LineDelta, OS);
372 }
373
374 // According to the DWARF specification, the `DW_LNS_fixed_advance_pc` opcode
375 // takes a single unsigned half (unencoded) operand. The maximum encodable
376 // value is therefore 65535. Set a conservative upper bound for relaxation.
377 unsigned PCBytes;
378 if (Value > 60000) {
380 OS << uint8_t(dwarf::DW_LNS_extended_op) << uint8_t(PCBytes + 1)
381 << uint8_t(dwarf::DW_LNE_set_address);
382 OS.write_zeros(PCBytes);
383 } else {
384 PCBytes = 2;
385 OS << uint8_t(dwarf::DW_LNS_fixed_advance_pc);
387 }
388 auto Offset = OS.tell() - PCBytes;
389
390 if (LineDelta == INT64_MAX) {
391 OS << uint8_t(dwarf::DW_LNS_extended_op);
392 OS << uint8_t(1);
393 OS << uint8_t(dwarf::DW_LNE_end_sequence);
394 } else {
395 OS << uint8_t(dwarf::DW_LNS_copy);
396 }
397
398 F.setVarContents(Data);
399 F.setVarFixups({MCFixup::create(Offset, &AddrDelta,
400 MCFixup::getDataKindForSize(PCBytes))});
401 return true;
402}
403
405 const MCExpr &AddrDelta = F.getDwarfAddrDelta();
407 int64_t Value;
408 if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
409 return false;
410 [[maybe_unused]] bool IsAbsolute =
411 AddrDelta.evaluateKnownAbsolute(Value, *Asm);
412 assert(IsAbsolute && "CFA with invalid expression");
413
414 assert(getContext().getAsmInfo().getMinInstAlignment() == 1 &&
415 "expected 1-byte alignment");
416 if (Value == 0) {
417 F.clearVarContents();
418 F.clearVarFixups();
419 return true;
420 }
421
422 auto AddFixups = [&Fixups, &AddrDelta](unsigned Offset,
423 std::pair<unsigned, unsigned> Fixup) {
424 const MCBinaryExpr &MBE = cast<MCBinaryExpr>(AddrDelta);
425 Fixups.push_back(MCFixup::create(Offset, MBE.getLHS(), std::get<0>(Fixup)));
426 Fixups.push_back(MCFixup::create(Offset, MBE.getRHS(), std::get<1>(Fixup)));
427 };
428
431 if (isUIntN(6, Value)) {
432 OS << uint8_t(dwarf::DW_CFA_advance_loc);
433 AddFixups(0, {ELF::R_RISCV_SET6, ELF::R_RISCV_SUB6});
434 } else if (isUInt<8>(Value)) {
435 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
437 AddFixups(1, {ELF::R_RISCV_SET8, ELF::R_RISCV_SUB8});
438 } else if (isUInt<16>(Value)) {
439 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
441 AddFixups(1, {ELF::R_RISCV_SET16, ELF::R_RISCV_SUB16});
442 } else if (isUInt<32>(Value)) {
443 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
445 AddFixups(1, {ELF::R_RISCV_SET32, ELF::R_RISCV_SUB32});
446 } else {
447 llvm_unreachable("unsupported CFA encoding");
448 }
449 F.setVarContents(Data);
450 F.setVarFixups(Fixups);
451 return true;
452}
453
454std::pair<bool, bool> RISCVAsmBackend::relaxLEB128(MCFragment &LF,
455 int64_t &Value) const {
456 if (LF.isLEBSigned())
457 return std::make_pair(false, false);
458 const MCExpr &Expr = LF.getLEBValue();
459 if (ULEB128Reloc) {
461 }
462 return std::make_pair(Expr.evaluateKnownAbsolute(Value, *Asm), false);
463}
464
466 ArrayRef<MCOperand> Operands,
467 const MCSubtargetInfo &STI) const {
468 // This function has access to two STIs, the member of the AsmBackend, and the
469 // one passed as an argument. The latter is more specific, so we query it for
470 // specific features.
471 if (STI.hasFeature(RISCV::FeatureExactAssembly))
472 return false;
473
474 return getRelaxedOpcode(Opcode, Operands, STI) != Opcode;
475}
476
478 const MCSubtargetInfo *STI) const {
479 // We mostly follow binutils' convention here: align to even boundary with a
480 // 0-fill padding. We emit up to 1 2-byte nop, though we use c.nop if RVC is
481 // enabled or 0-fill otherwise. The remainder is now padded with 4-byte nops.
482
483 // Instructions always are at even addresses. We must be in a data area or
484 // be unaligned due to some other reason.
485 if (Count % 2) {
486 OS.write("\0", 1);
487 Count -= 1;
488 }
489
490 // TODO: emit a mapping symbol right here
491
492 if (Count % 4 == 2) {
493 // The canonical nop with Zca is c.nop. For .balign 4, we generate a 2-byte
494 // c.nop even in a norvc region.
495 OS.write("\x01\0", 2);
496 Count -= 2;
497 }
498
499 // The canonical nop on RISC-V is addi x0, x0, 0.
500 for (; Count >= 4; Count -= 4)
501 OS.write("\x13\0\0\0", 4);
502
503 return true;
504}
505
507 MCContext &Ctx) {
508 switch (Fixup.getKind()) {
509 default:
510 llvm_unreachable("Unknown fixup kind!");
511 case FK_Data_1:
512 case FK_Data_2:
513 case FK_Data_4:
514 case FK_Data_8:
515 case FK_Data_leb128:
516 return Value;
519 return Value & 0xfff;
521 if (!isInt<12>(Value)) {
522 Ctx.reportError(Fixup.getLoc(),
523 "operand must be a constant 12-bit integer");
524 }
525 return Value & 0xfff;
528 return (((Value >> 5) & 0x7f) << 25) | ((Value & 0x1f) << 7);
531 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
532 return ((Value + 0x800) >> 12) & 0xfffff;
534 if (!isInt<21>(Value))
535 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
536 if (Value & 0x1)
537 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
538 // Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
539 unsigned Sbit = (Value >> 20) & 0x1;
540 unsigned Hi8 = (Value >> 12) & 0xff;
541 unsigned Mid1 = (Value >> 11) & 0x1;
542 unsigned Lo10 = (Value >> 1) & 0x3ff;
543 // Inst{31} = Sbit;
544 // Inst{30-21} = Lo10;
545 // Inst{20} = Mid1;
546 // Inst{19-12} = Hi8;
547 Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
548 return Value;
549 }
552 if (!isInt<13>(Value))
553 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
554 if (Value & 0x1)
555 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
556 // Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
557 // Value.
558 unsigned Sbit = (Value >> 12) & 0x1;
559 unsigned Hi1 = (Value >> 11) & 0x1;
560 unsigned Mid6 = (Value >> 5) & 0x3f;
561 unsigned Lo4 = (Value >> 1) & 0xf;
562 // Inst{31} = Sbit;
563 // Inst{30-25} = Mid6;
564 // Inst{11-8} = Lo4;
565 // Inst{7} = Hi1;
566 Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
567 return Value;
568 }
571 // Jalr will add UpperImm with the sign-extended 12-bit LowerImm,
572 // we need to add 0x800ULL before extract upper bits to reflect the
573 // effect of the sign extension.
574 uint64_t UpperImm = (Value + 0x800ULL) & 0xfffff000ULL;
575 uint64_t LowerImm = Value & 0xfffULL;
576 return UpperImm | ((LowerImm << 20) << 32);
577 }
579 if (!isInt<12>(Value))
580 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
581 // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value.
582 unsigned Bit11 = (Value >> 11) & 0x1;
583 unsigned Bit4 = (Value >> 4) & 0x1;
584 unsigned Bit9_8 = (Value >> 8) & 0x3;
585 unsigned Bit10 = (Value >> 10) & 0x1;
586 unsigned Bit6 = (Value >> 6) & 0x1;
587 unsigned Bit7 = (Value >> 7) & 0x1;
588 unsigned Bit3_1 = (Value >> 1) & 0x7;
589 unsigned Bit5 = (Value >> 5) & 0x1;
590 Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
591 (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
592 return Value;
593 }
595 if (!isInt<9>(Value))
596 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
597 // Need to produce offset[8|4:3], [reg 3 bit], offset[7:6|2:1|5]
598 unsigned Bit8 = (Value >> 8) & 0x1;
599 unsigned Bit7_6 = (Value >> 6) & 0x3;
600 unsigned Bit5 = (Value >> 5) & 0x1;
601 unsigned Bit4_3 = (Value >> 3) & 0x3;
602 unsigned Bit2_1 = (Value >> 1) & 0x3;
603 Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
604 (Bit5 << 2);
605 return Value;
606 }
608 if (!isInt<6>(Value))
609 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
610 unsigned Bit5 = (Value >> 5) & 0x1;
611 unsigned Bit4_0 = Value & 0x1f;
612 Value = (Bit5 << 12) | (Bit4_0 << 2);
613 return Value;
614 }
616 if (!isInt<32>(Value))
617 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
618 return Value & 0xffffffffu;
619 }
621 if (!isInt<20>(Value))
622 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
623 unsigned Bit19 = (Value >> 19) & 0x1;
624 unsigned Bit14_0 = Value & 0x7fff;
625 unsigned Bit18_15 = (Value >> 15) & 0xf;
626 Value = (Bit19 << 31) | (Bit14_0 << 16) | (Bit18_15 << 12);
627 return Value;
628 }
630 if (!isInt<32>(Value))
631 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
632 if (Value & 0x1)
633 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
634 uint64_t Bit31_16 = (Value >> 16) & 0xffff;
635 uint64_t Bit12 = (Value >> 12) & 0x1;
636 uint64_t Bit10_5 = (Value >> 5) & 0x3f;
637 uint64_t Bit15_13 = (Value >> 13) & 0x7;
638 uint64_t Bit4_1 = (Value >> 1) & 0xf;
639 uint64_t Bit11 = (Value >> 11) & 0x1;
640 Value = (Bit31_16 << 32ull) | (Bit12 << 31) | (Bit10_5 << 25) |
641 (Bit15_13 << 17) | (Bit4_1 << 8) | (Bit11 << 7);
642 return Value;
643 }
646 return 0;
648 if (!isInt<11>(Value))
649 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
650 if (Value & 0x1)
651 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
652 // Need to extract imm[10], imm[9:5], imm[4:1] from the 11-bit Value.
653 unsigned Sbit = (Value >> 10) & 0x1;
654 unsigned Hi5 = (Value >> 5) & 0x1f;
655 unsigned Lo4 = (Value >> 1) & 0xf;
656 // Inst{31} = Sbit;
657 // Inst{29-25} = Hi5;
658 // Inst{11-8} = Lo4;
659 Value = (Sbit << 31) | (Hi5 << 25) | (Lo4 << 8);
660 return Value;
661 }
662 }
663}
664
666 const MCFragment &F) {
667 // If the section does not contain linker-relaxable fragments, PC-relative
668 // fixups can be resolved.
669 if (!F.getParent()->isLinkerRelaxable())
670 return true;
671
672 // Otherwise, check if the offset between the symbol and fragment is fully
673 // resolved, unaffected by linker-relaxable fragments (e.g. instructions or
674 // offset-affected FT_Align fragments). Complements the generic
675 // isSymbolRefDifferenceFullyResolvedImpl.
676 if (!PCRelTemp)
678 PCRelTemp->setFragment(const_cast<MCFragment *>(&F));
679 MCValue Res;
681 MCValue::get(nullptr, PCRelTemp), Res);
682 return !Res.getSubSym();
683}
684
685// Get the corresponding PC-relative HI fixup that a S_PCREL_LO points to, and
686// optionally the fragment containing it.
687//
688// \returns nullptr if this isn't a S_PCREL_LO pointing to a known PC-relative
689// HI fixup.
691 const MCFragment **DFOut) {
692 MCValue AUIPCLoc;
693 if (!Expr.getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr))
694 return nullptr;
695
696 const MCSymbol *AUIPCSymbol = AUIPCLoc.getAddSym();
697 if (!AUIPCSymbol)
698 return nullptr;
699 const auto *DF = AUIPCSymbol->getFragment();
700 if (!DF)
701 return nullptr;
702
703 uint64_t Offset = AUIPCSymbol->getOffset();
704 if (DF->getContents().size() == Offset) {
705 DF = DF->getNext();
706 if (!DF)
707 return nullptr;
708 Offset = 0;
709 }
710
711 for (const MCFixup &F : DF->getFixups()) {
712 if (F.getOffset() != Offset)
713 continue;
714 auto Kind = F.getKind();
715 if (!mc::isRelocation(F.getKind())) {
716 if (Kind == RISCV::fixup_riscv_pcrel_hi20) {
717 *DFOut = DF;
718 return &F;
719 }
720 break;
721 }
722 switch (Kind) {
723 case ELF::R_RISCV_GOT_HI20:
724 case ELF::R_RISCV_TLS_GOT_HI20:
725 case ELF::R_RISCV_TLS_GD_HI20:
726 case ELF::R_RISCV_TLSDESC_HI20:
727 *DFOut = DF;
728 return &F;
729 }
730 }
731
732 return nullptr;
733}
734
735std::optional<bool> RISCVAsmBackend::evaluateFixup(const MCFragment &,
736 MCFixup &Fixup,
738 uint64_t &Value) {
739 const MCFixup *AUIPCFixup;
740 const MCFragment *AUIPCDF;
741 MCValue AUIPCTarget;
742 switch (Fixup.getKind()) {
743 default:
744 // Use default handling for `Value` and `IsResolved`.
745 return {};
748 // Never resolved in the assembler
749 return false;
752 AUIPCFixup =
753 getPCRelHiFixup(cast<MCSpecifierExpr>(*Fixup.getValue()), &AUIPCDF);
754 if (!AUIPCFixup) {
755 getContext().reportError(Fixup.getLoc(),
756 "could not find corresponding %pcrel_hi");
757 return true;
758 }
759
760 // MCAssembler::evaluateFixup will emit an error for this case when it sees
761 // the %pcrel_hi, so don't duplicate it when also seeing the %pcrel_lo.
762 const MCExpr *AUIPCExpr = AUIPCFixup->getValue();
763 if (!AUIPCExpr->evaluateAsRelocatable(AUIPCTarget, Asm))
764 return true;
765 break;
766 }
767 }
768
769 if (!AUIPCTarget.getAddSym())
770 return false;
771
772 auto &SA = static_cast<const MCSymbolELF &>(*AUIPCTarget.getAddSym());
773 if (SA.isUndefined())
774 return false;
775
776 bool IsResolved = &SA.getSection() == AUIPCDF->getParent() &&
777 SA.getBinding() == ELF::STB_LOCAL &&
778 SA.getType() != ELF::STT_GNU_IFUNC;
779 if (!IsResolved)
780 return false;
781
782 Value = Asm->getSymbolOffset(SA) + AUIPCTarget.getConstant();
783 Value -= Asm->getFragmentOffset(*AUIPCDF) + AUIPCFixup->getOffset();
784
785 return AUIPCFixup->getKind() == RISCV::fixup_riscv_pcrel_hi20 &&
786 isPCRelFixupResolved(AUIPCTarget.getAddSym(), *AUIPCDF);
787}
788
790 const MCFixup &Fixup) {
791 StringRef VendorIdentifier;
792 switch (Fixup.getKind()) {
793 default:
794 // No Vendor Relocation Required.
795 return;
802 VendorIdentifier = "QUALCOMM";
803 break;
805 VendorIdentifier = "ANDES";
806 break;
807 }
808
809 // Create a local symbol for the vendor relocation to reference. It's fine if
810 // the symbol has the same name as an existing symbol.
811 MCContext &Ctx = Asm->getContext();
812 MCSymbol *VendorSymbol = Ctx.createLocalSymbol(VendorIdentifier);
813 auto [It, Inserted] =
814 VendorSymbols.try_emplace(VendorIdentifier, VendorSymbol);
815
816 if (Inserted) {
817 // Setup the just-created symbol
818 VendorSymbol->setVariableValue(MCConstantExpr::create(0, Ctx));
819 Asm->registerSymbol(*VendorSymbol);
820 } else {
821 // Fetch the existing symbol
822 VendorSymbol = It->getValue();
823 }
824
825 MCFixup VendorFixup =
826 MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_RISCV_VENDOR);
827 // Explicitly create MCValue rather than using an MCExpr and evaluating it so
828 // that the absolute vendor symbol is not evaluated to constant 0.
829 MCValue VendorTarget = MCValue::get(VendorSymbol);
830 uint64_t VendorValue;
831 Asm->getWriter().recordRelocation(F, VendorFixup, VendorTarget, VendorValue);
832}
833
835 // Some Fixups are marked as LinkerRelaxable by
836 // `RISCVMCCodeEmitter::getImmOpValue` only because they may be
837 // (assembly-)relaxed into a linker-relaxable instruction. This function
838 // should return `false` for those fixups so they do not get a `R_RISCV_RELAX`
839 // relocation emitted in addition to the relocation.
840 switch (Kind) {
841 default:
842 break;
848 return false;
849 }
850 return true;
851}
852
854 const MCValue &Target, uint64_t &FixedValue,
855 bool IsResolved) {
856 uint64_t FixedValueA, FixedValueB;
857 if (Target.getSubSym()) {
858 assert(Target.getSpecifier() == 0 &&
859 "relocatable SymA-SymB cannot have relocation specifier");
860 unsigned TA = 0, TB = 0;
861 switch (Fixup.getKind()) {
862 case llvm::FK_Data_1:
863 TA = ELF::R_RISCV_ADD8;
864 TB = ELF::R_RISCV_SUB8;
865 break;
866 case llvm::FK_Data_2:
867 TA = ELF::R_RISCV_ADD16;
868 TB = ELF::R_RISCV_SUB16;
869 break;
870 case llvm::FK_Data_4:
871 TA = ELF::R_RISCV_ADD32;
872 TB = ELF::R_RISCV_SUB32;
873 break;
874 case llvm::FK_Data_8:
875 TA = ELF::R_RISCV_ADD64;
876 TB = ELF::R_RISCV_SUB64;
877 break;
879 TA = ELF::R_RISCV_SET_ULEB128;
880 TB = ELF::R_RISCV_SUB_ULEB128;
881 break;
882 default:
883 llvm_unreachable("unsupported fixup size");
884 }
885 MCValue A = MCValue::get(Target.getAddSym(), nullptr, Target.getConstant());
886 MCValue B = MCValue::get(Target.getSubSym());
887 auto FA = MCFixup::create(Fixup.getOffset(), nullptr, TA);
888 auto FB = MCFixup::create(Fixup.getOffset(), nullptr, TB);
889 Asm->getWriter().recordRelocation(F, FA, A, FixedValueA);
890 Asm->getWriter().recordRelocation(F, FB, B, FixedValueB);
891 FixedValue = FixedValueA - FixedValueB;
892 return false;
893 }
894
895 // If linker relaxation is enabled and supported by the current fixup, then we
896 // always want to generate a relocation.
897 bool NeedsRelax = Fixup.isLinkerRelaxable() &&
899 if (NeedsRelax)
900 IsResolved = false;
901
902 if (IsResolved && Fixup.isPCRel())
903 IsResolved = isPCRelFixupResolved(Target.getAddSym(), F);
904
905 if (!IsResolved) {
906 // Some Fixups require a VENDOR relocation, record it (directly) before we
907 // add the relocation.
909
910 Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
911
912 if (NeedsRelax) {
913 // Some Fixups get a RELAX relocation, record it (directly) after we add
914 // the relocation.
915 MCFixup RelaxFixup =
916 MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_RISCV_RELAX);
917 MCValue RelaxTarget = MCValue::get(nullptr);
918 uint64_t RelaxValue;
919 Asm->getWriter().recordRelocation(F, RelaxFixup, RelaxTarget, RelaxValue);
920 }
921 }
922
923 return false;
924}
925
926// Data fixups should be swapped for big endian cores.
927// Instruction fixups should not be swapped as RISC-V instructions
928// are always little-endian.
929static bool isDataFixup(unsigned Kind) {
930 switch (Kind) {
931 default:
932 return false;
933
934 case FK_Data_1:
935 case FK_Data_2:
936 case FK_Data_4:
937 case FK_Data_8:
938 return true;
939 }
940}
941
943 const MCValue &Target, uint8_t *Data,
944 uint64_t Value, bool IsResolved) {
945 IsResolved = addReloc(F, Fixup, Target, Value, IsResolved);
946 MCFixupKind Kind = Fixup.getKind();
947 if (mc::isRelocation(Kind))
948 return;
949 MCContext &Ctx = getContext();
951 if (!Value)
952 return; // Doesn't change encoding.
953 // Apply any target-specific value adjustments.
955
956 // Shift the value into position.
957 Value <<= Info.TargetOffset;
958
959 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
960 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
961 "Invalid fixup offset!");
962
963 // For each byte of the fragment that the fixup touches, mask in the
964 // bits from the fixup value.
965 // For big endian cores, data fixup should be swapped.
966 bool SwapValue = Endian == llvm::endianness::big && isDataFixup(Kind);
967 for (unsigned i = 0; i != NumBytes; ++i) {
968 unsigned Idx = SwapValue ? (NumBytes - 1 - i) : i;
969 Data[Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
970 }
971}
972
973std::unique_ptr<MCObjectTargetWriter>
977
979public:
981 bool IsLittleEndian, const MCTargetOptions &Options)
982 : RISCVAsmBackend(STI, OSABI, Is64Bit, IsLittleEndian, Options) {}
983
984 std::unique_ptr<MCObjectTargetWriter>
985 createObjectTargetWriter() const override {
986 const Triple &TT = STI.getTargetTriple();
987 uint32_t CPUType = cantFail(MachO::getCPUType(TT));
988 uint32_t CPUSubType = cantFail(MachO::getCPUSubType(TT));
989 return createRISCVMachObjectWriter(CPUType, CPUSubType);
990 }
991
992 bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
993 uint64_t &FixedValue, bool IsResolved) override;
994
995 std::optional<bool> evaluateFixup(const MCFragment &F, MCFixup &Fixup,
996 MCValue &Target, uint64_t &Value) override {
997 const MCFixup *AUIPCFixup;
998 const MCFragment *AUIPCDF;
999 const MCFixupKind FKind = Fixup.getKind();
1000 if ((FKind == RISCV::fixup_riscv_pcrel_lo12_i) ||
1002 AUIPCFixup =
1003 getPCRelHiFixup(cast<MCSpecifierExpr>(*Fixup.getValue()), &AUIPCDF);
1004 if (!AUIPCFixup) {
1005 getContext().reportError(Fixup.getLoc(),
1006 "could not find corresponding %pcrel_hi");
1007 return true;
1008 }
1009
1010 return false;
1011 }
1012
1013 // Use default handling for all other cases.
1014 return {};
1015 }
1016};
1017
1019 const MCSubtargetInfo &STI,
1020 const MCRegisterInfo &MRI,
1021 const MCTargetOptions &Options) {
1022 const Triple &TT = STI.getTargetTriple();
1023 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
1024 if (TT.isOSBinFormatMachO())
1025 return new DarwinRISCVAsmBackend(STI, OSABI, TT.isArch64Bit(),
1026 TT.isLittleEndian(), Options);
1027
1028 return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit(), TT.isLittleEndian(),
1029 Options);
1030}
1031
1033 const MCValue &Target,
1034 uint64_t &FixedValue, bool IsResolved) {
1035 if (!IsResolved)
1036 Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
1037 return IsResolved;
1038}
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
static LVOptions Options
Definition LVOptions.cpp:25
static unsigned getRelaxedOpcode(unsigned Opcode)
#define F(x, y, z)
Definition MD5.cpp:54
Register Reg
#define T
PowerPC TLS Dynamic Call Fixup
static cl::opt< bool > AlignRvc("riscv-align-rvc", cl::init(true), cl::Hidden, cl::desc("When generating R_RISCV_ALIGN, insert $alignment-2 " "bytes of NOPs even in norvc code"))
static bool relaxableFixupNeedsRelocation(const MCFixupKind Kind)
static bool isDataFixup(unsigned Kind)
const MCFixup * getPCRelHiFixup(const MCSpecifierExpr &Expr, const MCFragment **DFOut)
static cl::opt< bool > ULEB128Reloc("riscv-uleb128-reloc", cl::init(true), cl::Hidden, cl::desc("Emit R_RISCV_SET_ULEB128/E_RISCV_SUB_ULEB128 if appropriate"))
std::optional< bool > evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target, uint64_t &Value) override
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &, uint64_t &FixedValue, bool IsResolved) override
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
DarwinRISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit, bool IsLittleEndian, const MCTargetOptions &Options)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Generic interface to target specific assembler backends.
const llvm::endianness Endian
MCAsmBackend(llvm::endianness Endian)
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
MCAssembler * Asm
MCContext & getContext() const
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition MCAsmInfo.h:452
Binary assembler expressions.
Definition MCExpr.h:299
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:446
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:449
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
const MCAsmInfo & getAsmInfo() const
Definition MCContext.h:409
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition MCExpr.cpp:450
static LLVM_ABI bool evaluateSymbolicAdd(const MCAssembler *, bool, const MCValue &, const MCValue &, MCValue &)
Definition MCExpr.cpp:407
LLVM_ABI bool evaluateKnownAbsolute(int64_t &Res, const MCAssembler &Asm) const
Aggressive variant of evaluateAsRelocatable when relocations are unavailable (e.g.
Definition MCExpr.cpp:250
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition MCFixup.h:110
const MCExpr * getValue() const
Definition MCFixup.h:101
uint32_t getOffset() const
Definition MCFixup.h:98
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:86
MCFixupKind getKind() const
Definition MCFixup.h:96
bool isLEBSigned() const
Definition MCSection.h:343
const MCExpr & getLEBValue() const
Definition MCSection.h:335
MCSection * getParent() const
Definition MCSection.h:181
LLVM_ABI void setVarFixups(ArrayRef< MCFixup > Fixups)
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getOpcode() const
Definition MCInst.h:202
ArrayRef< MCOperand > getOperands() const
Definition MCInst.h:214
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition MCExpr.h:495
const MCExpr * getSubExpr() const
Definition MCExpr.h:509
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void setVariableValue(const MCExpr *Value)
Definition MCSymbol.cpp:50
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition MCSymbol.h:251
MCFragment * getFragment() const
Definition MCSymbol.h:345
uint64_t getOffset() const
Definition MCSymbol.h:289
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB=nullptr, int64_t Val=0, uint32_t Specifier=0)
Definition MCValue.h:56
const MCSymbol * getAddSym() const
Definition MCValue.h:49
int64_t getConstant() const
Definition MCValue.h:44
const MCSymbol * getSubSym() const
Definition MCValue.h:51
std::optional< bool > evaluateFixup(const MCFragment &, MCFixup &, MCValue &, uint64_t &) override
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
bool isPCRelFixupResolved(const MCSymbol *SymA, const MCFragment &F)
void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const override
Relax the instruction in the given fragment to the next wider instruction.
bool relaxAlign(MCFragment &F, unsigned &Size) override
virtual bool addReloc(const MCFragment &, const MCFixup &, const MCValue &, uint64_t &FixedValue, bool IsResolved)
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit, bool IsLittleEndian, const MCTargetOptions &Options)
std::pair< bool, bool > relaxLEB128(MCFragment &LF, int64_t &Value) const override
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved) override
bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override
Write an (optimal) nop sequence of Count bytes to the given output.
const MCSubtargetInfo & STI
void maybeAddVendorReloc(const MCFragment &, const MCFixup &)
StringMap< MCSymbol * > VendorSymbols
bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const override
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
const MCTargetOptions & TargetOptions
bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &, const MCValue &, uint64_t, bool) const override
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
std::optional< MCFixupKind > getFixupKind(StringRef Name) const override
Map a relocation name used in .reloc to a fixup kind.
bool relaxDwarfCFA(MCFragment &) const override
bool relaxDwarfLineAddr(MCFragment &) const override
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
#define INT64_MAX
Definition DataTypes.h:71
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ STB_LOCAL
Definition ELF.h:1407
@ STT_GNU_IFUNC
Definition ELF.h:1426
LLVM_ABI Expected< uint32_t > getCPUSubType(const Triple &T)
Definition MachO.cpp:107
LLVM_ABI Expected< uint32_t > getCPUType(const Triple &T)
Definition MachO.cpp:87
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
initializer< Ty > init(const Ty &Val)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:130
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:96
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
@ Offset
Definition DWP.cpp:558
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
std::unique_ptr< MCObjectTargetWriter > createRISCVMachObjectWriter(uint32_t CPUType, uint32_t CPUSubtype)
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
@ Success
The lock was released successfully.
@ FirstTargetFixupKind
Definition MCFixup.h:44
@ FirstLiteralRelocationKind
Definition MCFixup.h:29
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:36
@ FK_Data_leb128
A leb128 fixup.
Definition MCFixup.h:38
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:35
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
MCAsmBackend * createRISCVAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
endianness
Definition bit.h:71
Target independent information on a fixup kind.