@@ -76,12 +76,13 @@ MCFixupKindInfo RISCVAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
7676 {" fixup_riscv_branch" , 0 , 32 , 0 },
7777 {" fixup_riscv_rvc_jump" , 2 , 11 , 0 },
7878 {" fixup_riscv_rvc_branch" , 0 , 16 , 0 },
79+ {" fixup_riscv_rvc_imm" , 0 , 16 , 0 },
7980 {" fixup_riscv_call" , 0 , 64 , 0 },
8081 {" fixup_riscv_call_plt" , 0 , 64 , 0 },
8182
8283 {" fixup_riscv_qc_e_branch" , 0 , 48 , 0 },
8384 {" fixup_riscv_qc_e_32" , 16 , 32 , 0 },
84- {" fixup_riscv_qc_abs20_u" , 12 , 20 , 0 },
85+ {" fixup_riscv_qc_abs20_u" , 0 , 32 , 0 },
8586 {" fixup_riscv_qc_e_call_plt" , 0 , 48 , 0 },
8687
8788 // Andes fixups
@@ -134,6 +135,10 @@ bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
134135 // For jump instructions the immediate must be in the range
135136 // [-1048576, 1048574]
136137 return Offset > 1048574 || Offset < -1048576 ;
138+ case RISCV::fixup_riscv_rvc_imm:
139+ // This fixup can never be emitted as a relocation, so always needs to be
140+ // relaxed.
141+ return true ;
137142 }
138143}
139144
@@ -152,6 +157,18 @@ static unsigned getRelaxedOpcode(unsigned Opcode, ArrayRef<MCOperand> Operands,
152157 // This only relaxes one "step" - i.e. from C.J to JAL, not from C.J to
153158 // QC.E.J, because we can always relax again if needed.
154159 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;
155172 case RISCV::JAL: {
156173 // We can only relax JAL if we have Xqcilb
157174 if (!STI.hasFeature (RISCV::FeatureVendorXqcilb))
@@ -240,6 +257,23 @@ void RISCVAsmBackend::relaxInstruction(MCInst &Inst,
240257 Res.addOperand (Inst.getOperand (1 ));
241258 break ;
242259 }
260+ case RISCV::C_LI: {
261+ // This should only be hit when trying to relax a `C.LI` into a `QC.E.LI`
262+ // because the `C.LI` has a bare symbol. We cannot use
263+ // `RISCVRVC::uncompress` because it will use decompression patterns. The
264+ // `QC.E.LI` compression pattern to `C.LI` is compression-only (because we
265+ // don't want `c.li` ever printed as `qc.e.li`, which might be done if the
266+ // pattern applied to decompression), but that doesn't help much becuase
267+ // `C.LI` with a bare symbol will decompress to an `ADDI` anyway (because
268+ // `simm12`'s MCOperandPredicate accepts a bare symbol and that pattern
269+ // comes first), and we still cannot emit an `ADDI` with a bare symbol.
270+ assert (STI.hasFeature (RISCV::FeatureVendorXqcili) &&
271+ " C.LI is only relaxable with Xqcili" );
272+ Res.setOpcode (getRelaxedOpcode (Inst.getOpcode (), Inst.getOperands (), STI));
273+ Res.addOperand (Inst.getOperand (0 ));
274+ Res.addOperand (Inst.getOperand (1 ));
275+ break ;
276+ }
243277 case RISCV::BEQ:
244278 case RISCV::BNE:
245279 case RISCV::BLT:
@@ -539,10 +573,18 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
539573 (Bit5 << 2 );
540574 return Value;
541575 }
576+ case RISCV::fixup_riscv_rvc_imm: {
577+ if (!isInt<6 >(Value))
578+ Ctx.reportError (Fixup.getLoc (), " fixup value out of range" );
579+ unsigned Bit5 = (Value >> 5 ) & 0x1 ;
580+ unsigned Bit4_0 = Value & 0x1f ;
581+ Value = (Bit5 << 12 ) | (Bit4_0 << 2 );
582+ return Value;
583+ }
542584 case RISCV::fixup_riscv_qc_e_32: {
543585 if (!isInt<32 >(Value))
544586 Ctx.reportError (Fixup.getLoc (), " fixup value out of range" );
545- return (( Value & 0xffffffff ) << 16 ) ;
587+ return Value & 0xffffffffu ;
546588 }
547589 case RISCV::fixup_riscv_qc_abs20_u: {
548590 if (!isInt<20 >(Value))
0 commit comments