LLVM 24.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"
23#include "llvm/Support/LEB128.h"
25
26using namespace llvm;
27
29 bool Is64Bit, bool IsLittleEndian,
31 : MCAsmBackend(IsLittleEndian ? llvm::endianness::little
32 : llvm::endianness::big),
33 CLOpts(RISCV_MC::getCLOpts(Options)), STI(STI), OSABI(OSABI),
35 RISCVFeatures::validate(STI.getTargetTriple(), STI.getFeatureBits());
36}
37
38std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
39 if (STI.getTargetTriple().isOSBinFormatELF()) {
40 unsigned Type;
42#define ELF_RELOC(NAME, ID) .Case(#NAME, ID)
43#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
44#undef ELF_RELOC
45#define ELF_RISCV_NONSTANDARD_RELOC(_VENDOR, NAME, ID) .Case(#NAME, ID)
46#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
47#undef ELF_RISCV_NONSTANDARD_RELOC
48 .Case("BFD_RELOC_NONE", ELF::R_RISCV_NONE)
49 .Case("BFD_RELOC_32", ELF::R_RISCV_32)
50 .Case("BFD_RELOC_64", ELF::R_RISCV_64)
51 .Default(-1u);
52 if (Type != -1u)
53 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
54 }
55 return std::nullopt;
56}
57
59 const static MCFixupKindInfo Infos[] = {
60 // This table *must* be in the order that the fixup_* kinds are defined in
61 // RISCVFixupKinds.h.
62 //
63 // name offset bits flags
64 {"fixup_riscv_hi20", 12, 20, 0},
65 {"fixup_riscv_lo12_i", 20, 12, 0},
66 {"fixup_riscv_12_i", 20, 12, 0},
67 {"fixup_riscv_lo12_s", 0, 32, 0},
68 {"fixup_riscv_pcrel_hi20", 12, 20, 0},
69 {"fixup_riscv_pcrel_lo12_i", 20, 12, 0},
70 {"fixup_riscv_pcrel_lo12_s", 0, 32, 0},
71 {"fixup_riscv_jal", 12, 20, 0},
72 {"fixup_riscv_branch", 0, 32, 0},
73 {"fixup_riscv_rvc_jump", 2, 11, 0},
74 {"fixup_riscv_rvc_branch", 0, 16, 0},
75 {"fixup_riscv_rvc_imm", 0, 16, 0},
76 {"fixup_riscv_call", 0, 64, 0},
77 {"fixup_riscv_call_plt", 0, 64, 0},
78
79 // Qualcomm fixups
80 {"fixup_riscv_qc_e_branch", 0, 48, 0},
81 {"fixup_riscv_qc_e_32", 16, 32, 0},
82 {"fixup_riscv_qc_abs20_u", 0, 32, 0},
83 {"fixup_riscv_qc_e_call_plt", 0, 48, 0},
84 {"fixup_qc_access_16", 0, 0, 0},
85 {"fixup_qc_access_32", 0, 0, 0},
86
87 // Andes fixups
88 {"fixup_riscv_nds_branch_10", 0, 32, 0},
89 };
90 static_assert((std::size(Infos)) == RISCV::NumTargetFixupKinds,
91 "Not all fixup kinds added to Infos array");
92
93 // Fixup kinds from raw relocation types and .reloc directives force
94 // relocations and do not use these fields.
95 if (mc::isRelocation(Kind))
96 return {};
97
98 if (Kind < FirstTargetFixupKind)
100
102 "Invalid kind!");
103 return Infos[Kind - FirstTargetFixupKind];
104}
105
107 const MCFixup &Fixup,
108 const MCValue &,
109 uint64_t Value,
110 bool Resolved) const {
111 int64_t Offset = int64_t(Value);
112 auto Kind = Fixup.getKind();
113
114 // Return true if the symbol is unresolved.
115 if (!Resolved)
116 return true;
117
118 switch (Kind) {
119 default:
120 return false;
122 // For compressed branch instructions the immediate must be
123 // in the range [-256, 254].
124 return Offset > 254 || Offset < -256;
126 // For compressed jump instructions the immediate must be
127 // in the range [-2048, 2046].
128 return Offset > 2046 || Offset < -2048;
131 // For conditional branch instructions the immediate must be
132 // in the range [-4096, 4094].
133 return Offset > 4094 || Offset < -4096;
135 // For jump instructions the immediate must be in the range
136 // [-1048576, 1048574]
137 return Offset > 1048574 || Offset < -1048576;
139 // This fixup can never be emitted as a relocation, so always needs to be
140 // relaxed.
141 return true;
142 }
143}
144
145// Given a compressed control flow instruction this function returns
146// the expanded instruction, or the original instruction code if no
147// expansion is available.
148static unsigned getRelaxedOpcode(unsigned Opcode, ArrayRef<MCOperand> Operands,
149 const MCSubtargetInfo &STI) {
150 switch (Opcode) {
151 case RISCV::C_BEQZ:
152 return RISCV::BEQ;
153 case RISCV::C_BNEZ:
154 return RISCV::BNE;
155 case RISCV::C_J:
156 case RISCV::C_JAL: // fall through.
157 // This only relaxes one "step" - i.e. from C.J to JAL, not from C.J to
158 // QC.E.J, because we can always relax again if needed.
159 return RISCV::JAL;
160 case RISCV::C_LI:
161 if (!STI.hasFeature(RISCV::FeatureVendorXqcili))
162 break;
163 // We only need this because `QC.E.LI` can be compressed into a `C.LI`. This
164 // happens because the `simm6` MCOperandPredicate accepts bare symbols, and
165 // `QC.E.LI` is the only instruction that accepts bare symbols at parse-time
166 // and compresses to `C.LI`. `C.LI` does not itself accept bare symbols at
167 // parse time.
168 //
169 // If we have a bare symbol, we need to turn this back to a `QC.E.LI`, as we
170 // have no way to emit a relocation on a `C.LI` instruction.
171 return RISCV::QC_E_LI;
172 case RISCV::JAL: {
173 // We can only relax JAL if we have Xqcilb
174 if (!STI.hasFeature(RISCV::FeatureVendorXqcilb))
175 break;
176
177 // And only if it is using X0 or X1 for rd.
178 MCRegister Reg = Operands[0].getReg();
179 if (Reg == RISCV::X0)
180 return RISCV::QC_E_J;
181 if (Reg == RISCV::X1)
182 return RISCV::QC_E_JAL;
183
184 break;
185 }
186 case RISCV::BEQ:
187 return RISCV::PseudoLongBEQ;
188 case RISCV::BNE:
189 return RISCV::PseudoLongBNE;
190 case RISCV::BEQI:
191 return RISCV::PseudoLongBEQI;
192 case RISCV::BNEI:
193 return RISCV::PseudoLongBNEI;
194 case RISCV::BLT:
195 return RISCV::PseudoLongBLT;
196 case RISCV::BGE:
197 return RISCV::PseudoLongBGE;
198 case RISCV::BLTU:
199 return RISCV::PseudoLongBLTU;
200 case RISCV::BGEU:
201 return RISCV::PseudoLongBGEU;
202 case RISCV::QC_BEQI:
203 return RISCV::PseudoLongQC_BEQI;
204 case RISCV::QC_BNEI:
205 return RISCV::PseudoLongQC_BNEI;
206 case RISCV::QC_BLTI:
207 return RISCV::PseudoLongQC_BLTI;
208 case RISCV::QC_BGEI:
209 return RISCV::PseudoLongQC_BGEI;
210 case RISCV::QC_BLTUI:
211 return RISCV::PseudoLongQC_BLTUI;
212 case RISCV::QC_BGEUI:
213 return RISCV::PseudoLongQC_BGEUI;
214 case RISCV::QC_E_BEQI:
215 return RISCV::PseudoLongQC_E_BEQI;
216 case RISCV::QC_E_BNEI:
217 return RISCV::PseudoLongQC_E_BNEI;
218 case RISCV::QC_E_BLTI:
219 return RISCV::PseudoLongQC_E_BLTI;
220 case RISCV::QC_E_BGEI:
221 return RISCV::PseudoLongQC_E_BGEI;
222 case RISCV::QC_E_BLTUI:
223 return RISCV::PseudoLongQC_E_BLTUI;
224 case RISCV::QC_E_BGEUI:
225 return RISCV::PseudoLongQC_E_BGEUI;
226 case RISCV::CV_BEQIMM:
227 return RISCV::PseudoLongCV_BEQIMM;
228 case RISCV::CV_BNEIMM:
229 return RISCV::PseudoLongCV_BNEIMM;
230 }
231
232 // Returning the original opcode means we cannot relax the instruction.
233 return Opcode;
234}
235
237 const MCSubtargetInfo &STI) const {
238 if (STI.hasFeature(RISCV::FeatureExactAssembly))
239 return;
240
241 MCInst Res;
242 switch (Inst.getOpcode()) {
243 default:
244 llvm_unreachable("Opcode not expected!");
245 // These are expanded by hand rather than with `RISCVRVC::uncompress`: the
246 // decompression patterns only accept a bare symbol as the target, but the
247 // assembler also accepts `symbol + offset` here.
248 case RISCV::C_BEQZ:
249 case RISCV::C_BNEZ:
250 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
251 Res.addOperand(Inst.getOperand(0));
252 Res.addOperand(MCOperand::createReg(RISCV::X0));
253 Res.addOperand(Inst.getOperand(1));
254 break;
255 case RISCV::C_J:
256 case RISCV::C_JAL:
257 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
259 Inst.getOpcode() == RISCV::C_J ? RISCV::X0 : RISCV::X1));
260 Res.addOperand(Inst.getOperand(0));
261 break;
262 case RISCV::JAL: {
263 // This has to be written manually because the QC.E.J -> JAL is
264 // compression-only, so that it is not used when printing disassembly.
265 assert(STI.hasFeature(RISCV::FeatureVendorXqcilb) &&
266 "JAL is only relaxable with Xqcilb");
267 assert((Inst.getOperand(0).getReg() == RISCV::X0 ||
268 Inst.getOperand(0).getReg() == RISCV::X1) &&
269 "JAL only relaxable with rd=x0 or rd=x1");
270 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
271 Res.addOperand(Inst.getOperand(1));
272 break;
273 }
274 case RISCV::C_LI: {
275 // This should only be hit when trying to relax a `C.LI` into a `QC.E.LI`
276 // because the `C.LI` has a bare symbol. We cannot use
277 // `RISCVRVC::uncompress` because it will use decompression patterns. The
278 // `QC.E.LI` compression pattern to `C.LI` is compression-only (because we
279 // don't want `c.li` ever printed as `qc.e.li`, which might be done if the
280 // pattern applied to decompression), but that doesn't help much becuase
281 // `C.LI` with a bare symbol will decompress to an `ADDI` anyway (because
282 // `simm12`'s MCOperandPredicate accepts a bare symbol and that pattern
283 // comes first), and we still cannot emit an `ADDI` with a bare symbol.
284 assert(STI.hasFeature(RISCV::FeatureVendorXqcili) &&
285 "C.LI is only relaxable with Xqcili");
286 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
287 Res.addOperand(Inst.getOperand(0));
288 Res.addOperand(Inst.getOperand(1));
289 break;
290 }
291 case RISCV::BEQ:
292 case RISCV::BNE:
293 case RISCV::BEQI:
294 case RISCV::BNEI:
295 case RISCV::BLT:
296 case RISCV::BGE:
297 case RISCV::BLTU:
298 case RISCV::BGEU:
299 case RISCV::QC_BEQI:
300 case RISCV::QC_BNEI:
301 case RISCV::QC_BLTI:
302 case RISCV::QC_BGEI:
303 case RISCV::QC_BLTUI:
304 case RISCV::QC_BGEUI:
305 case RISCV::QC_E_BEQI:
306 case RISCV::QC_E_BNEI:
307 case RISCV::QC_E_BLTI:
308 case RISCV::QC_E_BGEI:
309 case RISCV::QC_E_BLTUI:
310 case RISCV::QC_E_BGEUI:
311 case RISCV::CV_BEQIMM:
312 case RISCV::CV_BNEIMM:
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
323 MCInst Inst = F.getInst();
324 const MCSubtargetInfo &STI = *F.getSubtargetInfo();
325 relaxInstruction(Inst, STI);
326
327 // A long branch is an inverted branch over a JAL. Under linker relaxation,
328 // the inverted branch targets a symbol after the JAL so that the JAL can be
329 // relaxed.
330 switch (Inst.getOpcode()) {
331 case RISCV::PseudoLongBEQ:
332 case RISCV::PseudoLongBNE:
333 case RISCV::PseudoLongBEQI:
334 case RISCV::PseudoLongBNEI:
335 case RISCV::PseudoLongBLT:
336 case RISCV::PseudoLongBGE:
337 case RISCV::PseudoLongBLTU:
338 case RISCV::PseudoLongBGEU:
339 case RISCV::PseudoLongQC_BEQI:
340 case RISCV::PseudoLongQC_BNEI:
341 case RISCV::PseudoLongQC_BLTI:
342 case RISCV::PseudoLongQC_BGEI:
343 case RISCV::PseudoLongQC_BLTUI:
344 case RISCV::PseudoLongQC_BGEUI:
345 case RISCV::PseudoLongQC_E_BEQI:
346 case RISCV::PseudoLongQC_E_BNEI:
347 case RISCV::PseudoLongQC_E_BLTI:
348 case RISCV::PseudoLongQC_E_BGEI:
349 case RISCV::PseudoLongQC_E_BLTUI:
350 case RISCV::PseudoLongQC_E_BGEUI:
351 case RISCV::PseudoLongCV_BEQIMM:
352 case RISCV::PseudoLongCV_BNEIMM:
353 if (STI.hasFeature(RISCV::FeatureRelax)) {
354 // F must be linker-relaxable, or the skip would resolve to a constant.
355 assert(F.isLinkerRelaxable() && F.getNext());
357 Skip->setFragment(F.getNext());
358 Asm->registerSymbol(*Skip);
359 Inst.addOperand(
361 }
362 break;
363 }
364 F.setInst(Inst);
365}
366
367// Check if an R_RISCV_ALIGN relocation is needed for an alignment directive.
368// If conditions are met, compute the padding size and create a fixup encoding
369// the padding size in the addend.
371 // Alignments before the first linker-relaxable instruction have fixed sizes
372 // and do not require relocations. Alignments after a linker-relaxable
373 // instruction require a relocation, even if the STI specifies norelax.
374 //
375 // firstLinkerRelaxable is the layout order within the subsection, which may
376 // be smaller than the section's order. Therefore, alignments in a
377 // lower-numbered subsection may be unnecessarily treated as linker-relaxable.
378 auto *Sec = F.getParent();
379 if (F.getLayoutOrder() <= Sec->firstLinkerRelaxable())
380 return false;
381
382 // Use default handling unless the alignment is larger than the nop size.
383 const MCSubtargetInfo *STI = F.getSubtargetInfo();
384 unsigned MinNopLen =
385 CLOpts.align_rvc || STI->hasFeature(RISCV::FeatureStdExtZca) ? 2 : 4;
386 if (F.getAlignment() <= MinNopLen)
387 return false;
388
389 Size = F.getAlignment().value() - MinNopLen;
390 auto *Expr = MCConstantExpr::create(Size, getContext());
391 MCFixup Fixup =
392 MCFixup::create(0, Expr, FirstLiteralRelocationKind + ELF::R_RISCV_ALIGN);
393 F.setVarFixups({Fixup});
394 F.setLinkerRelaxable();
395 return true;
396}
397
399 int64_t LineDelta = F.getDwarfLineDelta();
400 const MCExpr &AddrDelta = F.getDwarfAddrDelta();
401 int64_t Value;
402 // If the label difference can be resolved, use the default handling, which
403 // utilizes a shorter special opcode.
404 if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
405 return false;
406 [[maybe_unused]] bool IsAbsolute =
407 AddrDelta.evaluateKnownAbsolute(Value, *Asm);
408 assert(IsAbsolute && "CFA with invalid expression");
409
412
413 // INT64_MAX is a signal that this is actually a DW_LNE_end_sequence.
414 if (LineDelta != INT64_MAX) {
415 OS << uint8_t(dwarf::DW_LNS_advance_line);
416 encodeSLEB128(LineDelta, OS);
417 }
418
419 // According to the DWARF specification, the `DW_LNS_fixed_advance_pc` opcode
420 // takes a single unsigned half (unencoded) operand. The maximum encodable
421 // value is therefore 65535. Set a conservative upper bound for relaxation.
422 unsigned PCBytes;
423 if (Value > 60000) {
425 OS << uint8_t(dwarf::DW_LNS_extended_op) << uint8_t(PCBytes + 1)
426 << uint8_t(dwarf::DW_LNE_set_address);
427 OS.write_zeros(PCBytes);
428 } else {
429 PCBytes = 2;
430 OS << uint8_t(dwarf::DW_LNS_fixed_advance_pc);
432 }
433 auto Offset = OS.tell() - PCBytes;
434
435 if (LineDelta == INT64_MAX) {
436 OS << uint8_t(dwarf::DW_LNS_extended_op);
437 OS << uint8_t(1);
438 OS << uint8_t(dwarf::DW_LNE_end_sequence);
439 } else {
440 OS << uint8_t(dwarf::DW_LNS_copy);
441 }
442
443 F.setVarContents(Data);
444 F.setVarFixups({MCFixup::create(Offset, &AddrDelta,
445 MCFixup::getDataKindForSize(PCBytes))});
446 return true;
447}
448
450 const MCExpr &AddrDelta = F.getDwarfAddrDelta();
452 int64_t Value;
453 if (AddrDelta.evaluateAsAbsolute(Value, *Asm))
454 return false;
455 [[maybe_unused]] bool IsAbsolute =
456 AddrDelta.evaluateKnownAbsolute(Value, *Asm);
457 assert(IsAbsolute && "CFA with invalid expression");
458
459 assert(getContext().getAsmInfo().getMinInstAlignment() == 1 &&
460 "expected 1-byte alignment");
461 if (Value == 0) {
462 F.clearVarContents();
463 F.clearVarFixups();
464 return true;
465 }
466
467 auto AddFixups = [&Fixups, &AddrDelta](unsigned Offset,
468 std::pair<unsigned, unsigned> Fixup) {
469 const MCBinaryExpr &MBE = cast<MCBinaryExpr>(AddrDelta);
470 Fixups.push_back(MCFixup::create(Offset, MBE.getLHS(), std::get<0>(Fixup)));
471 Fixups.push_back(MCFixup::create(Offset, MBE.getRHS(), std::get<1>(Fixup)));
472 };
473
476 if (isUIntN(6, Value)) {
477 OS << uint8_t(dwarf::DW_CFA_advance_loc);
478 AddFixups(0, {ELF::R_RISCV_SET6, ELF::R_RISCV_SUB6});
479 } else if (isUInt<8>(Value)) {
480 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
482 AddFixups(1, {ELF::R_RISCV_SET8, ELF::R_RISCV_SUB8});
483 } else if (isUInt<16>(Value)) {
484 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
486 AddFixups(1, {ELF::R_RISCV_SET16, ELF::R_RISCV_SUB16});
487 } else if (isUInt<32>(Value)) {
488 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
490 AddFixups(1, {ELF::R_RISCV_SET32, ELF::R_RISCV_SUB32});
491 } else {
492 llvm_unreachable("unsupported CFA encoding");
493 }
494 F.setVarContents(Data);
495 F.setVarFixups(Fixups);
496 return true;
497}
498
499std::pair<bool, bool> RISCVAsmBackend::relaxLEB128(MCFragment &LF,
500 int64_t &Value) const {
501 if (LF.isLEBSigned())
502 return std::make_pair(false, false);
503 const MCExpr &Expr = LF.getLEBValue();
504 if (CLOpts.uleb128_reloc) {
506 }
507 return std::make_pair(Expr.evaluateKnownAbsolute(Value, *Asm), false);
508}
509
512 const MCSubtargetInfo &STI) const {
513 // This function has access to two STIs, the member of the AsmBackend, and the
514 // one passed as an argument. The latter is more specific, so we query it for
515 // specific features.
516 if (STI.hasFeature(RISCV::FeatureExactAssembly))
517 return false;
518
519 return getRelaxedOpcode(Opcode, Operands, STI) != Opcode;
520}
521
523 const MCSubtargetInfo *STI) const {
524 // We mostly follow binutils' convention here: align to even boundary with a
525 // 0-fill padding. We emit up to 1 2-byte nop, though we use c.nop if RVC is
526 // enabled or 0-fill otherwise. The remainder is now padded with 4-byte nops.
527
528 // Instructions always are at even addresses. We must be in a data area or
529 // be unaligned due to some other reason.
530 if (Count % 2) {
531 OS.write("\0", 1);
532 Count -= 1;
533 }
534
535 // TODO: emit a mapping symbol right here
536
537 if (Count % 4 == 2) {
538 // The canonical nop with Zca is c.nop. For .balign 4, we generate a 2-byte
539 // c.nop even in a norvc region.
540 OS.write("\x01\0", 2);
541 Count -= 2;
542 }
543
544 // The canonical nop on RISC-V is addi x0, x0, 0.
545 for (; Count >= 4; Count -= 4)
546 OS.write("\x13\0\0\0", 4);
547
548 return true;
549}
550
552 MCContext &Ctx) {
553 switch (Fixup.getKind()) {
554 default:
555 llvm_unreachable("Unknown fixup kind!");
556 case FK_Data_1:
557 case FK_Data_2:
558 case FK_Data_4:
559 case FK_Data_8:
560 case FK_Data_leb128:
561 return Value;
564 return Value & 0xfff;
566 if (!isInt<12>(Value)) {
567 Ctx.reportError(Fixup.getLoc(),
568 "operand must be a constant 12-bit integer");
569 }
570 return Value & 0xfff;
573 return (((Value >> 5) & 0x7f) << 25) | ((Value & 0x1f) << 7);
576 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
577 return ((Value + 0x800) >> 12) & 0xfffff;
579 if (!isInt<21>(Value))
580 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
581 if (Value & 0x1)
582 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
583 // Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
584 unsigned Sbit = (Value >> 20) & 0x1;
585 unsigned Hi8 = (Value >> 12) & 0xff;
586 unsigned Mid1 = (Value >> 11) & 0x1;
587 unsigned Lo10 = (Value >> 1) & 0x3ff;
588 // Inst{31} = Sbit;
589 // Inst{30-21} = Lo10;
590 // Inst{20} = Mid1;
591 // Inst{19-12} = Hi8;
592 Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
593 return Value;
594 }
597 if (!isInt<13>(Value))
598 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
599 if (Value & 0x1)
600 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
601 // Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
602 // Value.
603 unsigned Sbit = (Value >> 12) & 0x1;
604 unsigned Hi1 = (Value >> 11) & 0x1;
605 unsigned Mid6 = (Value >> 5) & 0x3f;
606 unsigned Lo4 = (Value >> 1) & 0xf;
607 // Inst{31} = Sbit;
608 // Inst{30-25} = Mid6;
609 // Inst{11-8} = Lo4;
610 // Inst{7} = Hi1;
611 Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
612 return Value;
613 }
616 // Jalr will add UpperImm with the sign-extended 12-bit LowerImm,
617 // we need to add 0x800ULL before extract upper bits to reflect the
618 // effect of the sign extension.
619 uint64_t UpperImm = (Value + 0x800ULL) & 0xfffff000ULL;
620 uint64_t LowerImm = Value & 0xfffULL;
621 return UpperImm | ((LowerImm << 20) << 32);
622 }
624 if (!isInt<12>(Value))
625 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
626 // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value.
627 unsigned Bit11 = (Value >> 11) & 0x1;
628 unsigned Bit4 = (Value >> 4) & 0x1;
629 unsigned Bit9_8 = (Value >> 8) & 0x3;
630 unsigned Bit10 = (Value >> 10) & 0x1;
631 unsigned Bit6 = (Value >> 6) & 0x1;
632 unsigned Bit7 = (Value >> 7) & 0x1;
633 unsigned Bit3_1 = (Value >> 1) & 0x7;
634 unsigned Bit5 = (Value >> 5) & 0x1;
635 Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
636 (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
637 return Value;
638 }
640 if (!isInt<9>(Value))
641 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
642 // Need to produce offset[8|4:3], [reg 3 bit], offset[7:6|2:1|5]
643 unsigned Bit8 = (Value >> 8) & 0x1;
644 unsigned Bit7_6 = (Value >> 6) & 0x3;
645 unsigned Bit5 = (Value >> 5) & 0x1;
646 unsigned Bit4_3 = (Value >> 3) & 0x3;
647 unsigned Bit2_1 = (Value >> 1) & 0x3;
648 Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
649 (Bit5 << 2);
650 return Value;
651 }
653 if (!isInt<6>(Value))
654 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
655 unsigned Bit5 = (Value >> 5) & 0x1;
656 unsigned Bit4_0 = Value & 0x1f;
657 Value = (Bit5 << 12) | (Bit4_0 << 2);
658 return Value;
659 }
661 if (!isInt<32>(Value))
662 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
663 return Value & 0xffffffffu;
664 }
666 if (!isInt<20>(Value))
667 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
668 unsigned Bit19 = (Value >> 19) & 0x1;
669 unsigned Bit14_0 = Value & 0x7fff;
670 unsigned Bit18_15 = (Value >> 15) & 0xf;
671 Value = (Bit19 << 31) | (Bit14_0 << 16) | (Bit18_15 << 12);
672 return Value;
673 }
675 if (!isInt<32>(Value))
676 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
677 if (Value & 0x1)
678 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
679 uint64_t Bit31_16 = (Value >> 16) & 0xffff;
680 uint64_t Bit12 = (Value >> 12) & 0x1;
681 uint64_t Bit10_5 = (Value >> 5) & 0x3f;
682 uint64_t Bit15_13 = (Value >> 13) & 0x7;
683 uint64_t Bit4_1 = (Value >> 1) & 0xf;
684 uint64_t Bit11 = (Value >> 11) & 0x1;
685 Value = (Bit31_16 << 32ull) | (Bit12 << 31) | (Bit10_5 << 25) |
686 (Bit15_13 << 17) | (Bit4_1 << 8) | (Bit11 << 7);
687 return Value;
688 }
691 return 0;
693 if (!isInt<11>(Value))
694 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
695 if (Value & 0x1)
696 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
697 // Need to extract imm[10], imm[9:5], imm[4:1] from the 11-bit Value.
698 unsigned Sbit = (Value >> 10) & 0x1;
699 unsigned Hi5 = (Value >> 5) & 0x1f;
700 unsigned Lo4 = (Value >> 1) & 0xf;
701 // Inst{31} = Sbit;
702 // Inst{29-25} = Hi5;
703 // Inst{11-8} = Lo4;
704 Value = (Sbit << 31) | (Hi5 << 25) | (Lo4 << 8);
705 return Value;
706 }
707 }
708}
709
711 const MCFragment &F) {
712 // If the section does not contain linker-relaxable fragments, PC-relative
713 // fixups can be resolved.
714 if (!F.getParent()->isLinkerRelaxable())
715 return true;
716
717 // Otherwise, check if the offset between the symbol and fragment is fully
718 // resolved, unaffected by linker-relaxable fragments (e.g. instructions or
719 // offset-affected FT_Align fragments). Complements the generic
720 // isSymbolRefDifferenceFullyResolvedImpl.
721 if (!PCRelTemp)
723 PCRelTemp->setFragment(const_cast<MCFragment *>(&F));
724 MCValue Res;
726 MCValue::get(nullptr, PCRelTemp), Res);
727 return !Res.getSubSym();
728}
729
730// Get the corresponding PC-relative HI fixup that a S_PCREL_LO points to, and
731// optionally the fragment containing it.
732//
733// \returns nullptr if this isn't a S_PCREL_LO pointing to a known PC-relative
734// HI fixup.
736 const MCFragment **DFOut) {
737 MCValue AUIPCLoc;
738 if (!Expr.getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr))
739 return nullptr;
740
741 const MCSymbol *AUIPCSymbol = AUIPCLoc.getAddSym();
742 if (!AUIPCSymbol)
743 return nullptr;
744 const auto *DF = AUIPCSymbol->getFragment();
745 if (!DF)
746 return nullptr;
747
748 uint64_t Offset = AUIPCSymbol->getOffset();
749 if (DF->getContents().size() == Offset) {
750 DF = DF->getNext();
751 if (!DF)
752 return nullptr;
753 Offset = 0;
754 }
755
756 for (const MCFixup &F : DF->getFixups()) {
757 if (F.getOffset() != Offset)
758 continue;
759 auto Kind = F.getKind();
760 if (!mc::isRelocation(F.getKind())) {
761 if (Kind == RISCV::fixup_riscv_pcrel_hi20) {
762 *DFOut = DF;
763 return &F;
764 }
765 break;
766 }
767 switch (Kind) {
768 case ELF::R_RISCV_GOT_HI20:
769 case ELF::R_RISCV_TLS_GOT_HI20:
770 case ELF::R_RISCV_TLS_GD_HI20:
771 case ELF::R_RISCV_TLSDESC_HI20:
772 *DFOut = DF;
773 return &F;
774 }
775 }
776
777 return nullptr;
778}
779
780// For a PC-relative fixup whose expression is `Sym - . + C`, where `.` is the
781// address of the instruction itself, return `Sym + C`. The target operand of a
782// branch or jump is the offset from the instruction, so this is a reference to
783// `Sym + C`, which a relocation can express even when the distance to `Sym` is
784// only known at link time.
785static std::optional<MCValue>
787 const MCFixup &Fixup) {
788 MCValue Raw;
789 if (!Fixup.isPCRel() ||
790 !Fixup.getValue()->evaluateAsRelocatable(Raw, nullptr) ||
791 !Raw.getAddSym() || !Raw.getSubSym() || Raw.getSpecifier())
792 return std::nullopt;
793
794 const MCSymbol &Sub = *Raw.getSubSym();
795 if (!Sub.isInSection() || &Sub.getSection() != F.getParent())
796 return std::nullopt;
797
798 // Fixups in the variable part belong to the (relaxable) instruction that
799 // starts it; when that instruction is relaxed into a sequence, its fixups may
800 // be past the start. Otherwise the fixup is at the start of its instruction.
801 uint64_t InsnStart = Fixup.getOffset() >= F.getFixedSize()
802 ? F.getFixedSize()
803 : Fixup.getOffset();
804 if (Asm.getSymbolOffset(Sub) != Asm.getFragmentOffset(F) + InsnStart)
805 return std::nullopt;
806
807 return MCValue::get(Raw.getAddSym(), nullptr, Raw.getConstant());
808}
809
810std::optional<bool> RISCVAsmBackend::evaluateFixup(const MCFragment &F,
811 MCFixup &Fixup,
813 uint64_t &Value) {
814 const MCFixup *AUIPCFixup;
815 const MCFragment *AUIPCDF;
816 MCValue AUIPCTarget;
817 switch (Fixup.getKind()) {
818 default:
819 if (auto PCRelTarget = getPCRelTargetOfDotDifference(*Asm, F, Fixup))
820 Target = *PCRelTarget;
821 // Use default handling for `Value` and `IsResolved`.
822 return {};
825 // Never resolved in the assembler
826 return false;
829 AUIPCFixup =
830 getPCRelHiFixup(cast<MCSpecifierExpr>(*Fixup.getValue()), &AUIPCDF);
831 if (!AUIPCFixup) {
832 getContext().reportError(Fixup.getLoc(),
833 "could not find corresponding %pcrel_hi");
834 return true;
835 }
836
837 // MCAssembler::evaluateFixup will emit an error for this case when it sees
838 // the %pcrel_hi, so don't duplicate it when also seeing the %pcrel_lo.
839 const MCExpr *AUIPCExpr = AUIPCFixup->getValue();
840 if (!AUIPCExpr->evaluateAsRelocatable(AUIPCTarget, Asm))
841 return true;
842 break;
843 }
844 }
845
846 if (!AUIPCTarget.getAddSym())
847 return false;
848
849 auto &SA = static_cast<const MCSymbolELF &>(*AUIPCTarget.getAddSym());
850 if (SA.isUndefined())
851 return false;
852
853 bool IsResolved = &SA.getSection() == AUIPCDF->getParent() &&
854 SA.getBinding() == ELF::STB_LOCAL &&
855 SA.getType() != ELF::STT_GNU_IFUNC;
856 if (!IsResolved)
857 return false;
858
859 Value = Asm->getSymbolOffset(SA) + AUIPCTarget.getConstant();
860 Value -= Asm->getFragmentOffset(*AUIPCDF) + AUIPCFixup->getOffset();
861
862 return AUIPCFixup->getKind() == RISCV::fixup_riscv_pcrel_hi20 &&
863 isPCRelFixupResolved(AUIPCTarget.getAddSym(), *AUIPCDF);
864}
865
867 const MCFixup &Fixup) {
868 StringRef VendorIdentifier;
869 switch (Fixup.getKind()) {
870 default:
871 // No Vendor Relocation Required.
872 return;
879 VendorIdentifier = "QUALCOMM";
880 break;
882 VendorIdentifier = "ANDES";
883 break;
884 }
885
886 // Create a local symbol for the vendor relocation to reference. It's fine if
887 // the symbol has the same name as an existing symbol.
888 MCContext &Ctx = Asm->getContext();
889 MCSymbol *VendorSymbol = Ctx.createLocalSymbol(VendorIdentifier);
890 auto [It, Inserted] =
891 VendorSymbols.try_emplace(VendorIdentifier, VendorSymbol);
892
893 if (Inserted) {
894 // Setup the just-created symbol
895 VendorSymbol->setVariableValue(MCConstantExpr::create(0, Ctx));
896 Asm->registerSymbol(*VendorSymbol);
897 } else {
898 // Fetch the existing symbol
899 VendorSymbol = It->getValue();
900 }
901
902 MCFixup VendorFixup =
903 MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_RISCV_VENDOR);
904 // Explicitly create MCValue rather than using an MCExpr and evaluating it so
905 // that the absolute vendor symbol is not evaluated to constant 0.
906 MCValue VendorTarget = MCValue::get(VendorSymbol);
907 uint64_t VendorValue;
908 Asm->getWriter().recordRelocation(F, VendorFixup, VendorTarget, VendorValue);
909}
910
912 // Some Fixups are marked as LinkerRelaxable by
913 // `RISCVMCCodeEmitter::getImmOpValue` only because they may be
914 // (assembly-)relaxed into a linker-relaxable instruction. This function
915 // should return `false` for those fixups so they do not get a `R_RISCV_RELAX`
916 // relocation emitted in addition to the relocation.
917 switch (Kind) {
918 default:
919 break;
925 return false;
926 }
927 return true;
928}
929
931 const MCValue &Target, uint64_t &FixedValue,
932 bool IsResolved) {
933 uint64_t FixedValueA, FixedValueB;
934 if (Target.getSubSym()) {
935 assert(Target.getSpecifier() == 0 &&
936 "relocatable SymA-SymB cannot have relocation specifier");
937 unsigned TA = 0, TB = 0;
938 switch (Fixup.getKind()) {
939 case llvm::FK_Data_1:
940 TA = ELF::R_RISCV_ADD8;
941 TB = ELF::R_RISCV_SUB8;
942 break;
943 case llvm::FK_Data_2:
944 TA = ELF::R_RISCV_ADD16;
945 TB = ELF::R_RISCV_SUB16;
946 break;
947 case llvm::FK_Data_4:
948 TA = ELF::R_RISCV_ADD32;
949 TB = ELF::R_RISCV_SUB32;
950 break;
951 case llvm::FK_Data_8:
952 TA = ELF::R_RISCV_ADD64;
953 TB = ELF::R_RISCV_SUB64;
954 break;
956 TA = ELF::R_RISCV_SET_ULEB128;
957 TB = ELF::R_RISCV_SUB_ULEB128;
958 break;
959 default:
960 // There are no ADD/SUB relocations for instruction fields, so a symbol
961 // difference that is only known at link time (e.g. because of linker
962 // relaxation) cannot be encoded here.
963 getContext().reportError(Fixup.getLoc(),
964 "unsupported symbol difference in instruction");
965 FixedValue = 0;
966 return false;
967 }
968 MCValue A = MCValue::get(Target.getAddSym(), nullptr, Target.getConstant());
969 MCValue B = MCValue::get(Target.getSubSym());
970 auto FA = MCFixup::create(Fixup.getOffset(), nullptr, TA);
971 auto FB = MCFixup::create(Fixup.getOffset(), nullptr, TB);
972 Asm->getWriter().recordRelocation(F, FA, A, FixedValueA);
973 Asm->getWriter().recordRelocation(F, FB, B, FixedValueB);
974 FixedValue = FixedValueA - FixedValueB;
975 return false;
976 }
977
978 // If linker relaxation is enabled and supported by the current fixup, then we
979 // always want to generate a relocation.
980 bool NeedsRelax = Fixup.isLinkerRelaxable() &&
981 relaxableFixupNeedsRelocation(Fixup.getKind());
982 if (NeedsRelax)
983 IsResolved = false;
984
985 if (IsResolved && Fixup.isPCRel())
986 IsResolved = isPCRelFixupResolved(Target.getAddSym(), F);
987
988 if (!IsResolved) {
989 // Some Fixups require a VENDOR relocation, record it (directly) before we
990 // add the relocation.
991 maybeAddVendorReloc(F, Fixup);
992
993 Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
994
995 if (NeedsRelax) {
996 // Some Fixups get a RELAX relocation, record it (directly) after we add
997 // the relocation.
998 MCFixup RelaxFixup =
999 MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_RISCV_RELAX);
1000 MCValue RelaxTarget = MCValue::get(nullptr);
1001 uint64_t RelaxValue;
1002 Asm->getWriter().recordRelocation(F, RelaxFixup, RelaxTarget, RelaxValue);
1003 }
1004 }
1005
1006 return false;
1007}
1008
1009// Data fixups should be swapped for big endian cores.
1010// Instruction fixups should not be swapped as RISC-V instructions
1011// are always little-endian.
1012static bool isDataFixup(unsigned Kind) {
1013 switch (Kind) {
1014 default:
1015 return false;
1016
1017 case FK_Data_1:
1018 case FK_Data_2:
1019 case FK_Data_4:
1020 case FK_Data_8:
1021 return true;
1022 }
1023}
1024
1026 const MCValue &Target, uint8_t *Data,
1027 uint64_t Value, bool IsResolved) {
1028 IsResolved = addReloc(F, Fixup, Target, Value, IsResolved);
1029 MCFixupKind Kind = Fixup.getKind();
1030 if (mc::isRelocation(Kind))
1031 return;
1032 MCContext &Ctx = getContext();
1033 MCFixupKindInfo Info = getFixupKindInfo(Kind);
1034 if (!Value)
1035 return; // Doesn't change encoding.
1036 // Apply any target-specific value adjustments.
1037 Value = adjustFixupValue(Fixup, Value, Ctx);
1038
1039 // Shift the value into position.
1040 Value <<= Info.TargetOffset;
1041
1042 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
1043 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
1044 "Invalid fixup offset!");
1045
1046 // For each byte of the fragment that the fixup touches, mask in the
1047 // bits from the fixup value.
1048 // For big endian cores, data fixup should be swapped.
1049 bool SwapValue = Endian == llvm::endianness::big && isDataFixup(Kind);
1050 for (unsigned i = 0; i != NumBytes; ++i) {
1051 unsigned Idx = SwapValue ? (NumBytes - 1 - i) : i;
1052 Data[Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
1053 }
1054}
1055
1056std::unique_ptr<MCObjectTargetWriter>
1060
1062public:
1064 bool IsLittleEndian, const MCTargetOptions &Options)
1065 : RISCVAsmBackend(STI, OSABI, Is64Bit, IsLittleEndian, Options) {}
1066
1067 std::unique_ptr<MCObjectTargetWriter>
1068 createObjectTargetWriter() const override {
1069 const Triple &TT = STI.getTargetTriple();
1070 uint32_t CPUType = cantFail(MachO::getCPUType(TT));
1071 uint32_t CPUSubType = cantFail(MachO::getCPUSubType(TT));
1072 return createRISCVMachObjectWriter(CPUType, CPUSubType);
1073 }
1074
1075 bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
1076 uint64_t &FixedValue, bool IsResolved) override;
1077
1078 std::optional<bool> evaluateFixup(const MCFragment &F, MCFixup &Fixup,
1079 MCValue &Target, uint64_t &Value) override {
1080 const MCFixup *AUIPCFixup;
1081 const MCFragment *AUIPCDF;
1082 const MCFixupKind FKind = Fixup.getKind();
1083 if ((FKind == RISCV::fixup_riscv_pcrel_lo12_i) ||
1085 AUIPCFixup =
1086 getPCRelHiFixup(cast<MCSpecifierExpr>(*Fixup.getValue()), &AUIPCDF);
1087 if (!AUIPCFixup) {
1088 getContext().reportError(Fixup.getLoc(),
1089 "could not find corresponding %pcrel_hi");
1090 return true;
1091 }
1092
1093 return false;
1094 }
1095
1096 // Use default handling for all other cases.
1097 return {};
1098 }
1099};
1100
1102 const MCSubtargetInfo &STI,
1103 const MCRegisterInfo &MRI,
1104 const MCTargetOptions &Options) {
1105 const Triple &TT = STI.getTargetTriple();
1106 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
1107 if (TT.isOSBinFormatMachO())
1108 return new DarwinRISCVAsmBackend(STI, OSABI, TT.isArch64Bit(),
1109 TT.isLittleEndian(), Options);
1110
1111 return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit(), TT.isLittleEndian(),
1112 Options);
1113}
1114
1116 const MCValue &Target,
1117 uint64_t &FixedValue, bool IsResolved) {
1118 if (!IsResolved)
1119 Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
1120 return IsResolved;
1121}
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!")
uint64_t uint64_t
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
static std::optional< MCValue > getPCRelTargetOfDotDifference(const MCAssembler &Asm, const MCFragment &F, const MCFixup &Fixup)
static bool relaxableFixupNeedsRelocation(const MCFixupKind Kind)
static bool isDataFixup(unsigned Kind)
const MCFixup * getPCRelHiFixup(const MCSpecifierExpr &Expr, const MCFragment **DFOut)
MachineRegisterInfo & MRI
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
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:492
Binary assembler expressions.
Definition MCExpr.h:295
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:442
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:445
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:84
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:31
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:65
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition MCFixup.h:114
const MCExpr * getValue() const
Definition MCFixup.h:105
uint32_t getOffset() const
Definition MCFixup.h:102
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:90
MCFixupKind getKind() const
Definition MCFixup.h:100
bool isLEBSigned() const
Definition MCSection.h:340
const MCExpr & getLEBValue() const
Definition MCSection.h:332
MCSection * getParent() const
Definition MCSection.h:178
LLVM_ABI void setVarFixups(ArrayRef< MCFixup > Fixups)
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
unsigned getOpcode() const
Definition MCInst.h:201
ArrayRef< MCOperand > getOperands() const
Definition MCInst.h:213
void addOperand(const MCOperand Op)
Definition MCInst.h:214
void setOpcode(unsigned Op)
Definition MCInst.h:200
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:209
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:165
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:137
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:72
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition MCExpr.h:491
const MCExpr * getSubExpr() const
Definition MCExpr.h:505
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:210
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:51
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
uint32_t getSpecifier() const
Definition MCValue.h:46
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.
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...
const RISCVMCOptions & CLOpts
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:48
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.
@ STT_GNU_IFUNC
Definition ELF.h:1279
@ STB_LOCAL
Definition ELF.h:1260
LLVM_ABI Expected< uint32_t > getCPUSubType(const Triple &T)
Definition MachO.cpp:109
LLVM_ABI Expected< uint32_t > getCPUType(const Triple &T)
Definition MachO.cpp:89
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
@ Offset
Definition DWP.cpp:577
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
constexpr MCFixupKind FirstTargetFixupKind
Definition MCFixup.h:49
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:149
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_Data_leb128
A leb128 fixup.
Definition MCFixup.h:42
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
MCAsmBackend * createRISCVAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
@ Sub
Subtraction of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32
Target independent information on a fixup kind.