[LoongArch] Fix undefined behavior: left shift of negative value
authorwanglei <wanglei@loongson.cn>
Wed, 11 Jan 2023 13:16:38 +0000 (21:16 +0800)
committerwanglei <wanglei@loongson.cn>
Wed, 11 Jan 2023 13:16:38 +0000 (21:16 +0800)
Fix undefined behavior in `decodeSImmOperand` where we were left shifting
a signed value.

llvm/lib/Target/LoongArch/Disassembler/LoongArchDisassembler.cpp

index beb757c..2335152 100644 (file)
@@ -114,9 +114,9 @@ static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
                                       int64_t Address,
                                       const MCDisassembler *Decoder) {
   assert(isUInt<N>(Imm) && "Invalid immediate");
-  // Sign-extend the number in the bottom <N> bits of Imm, then shift left <S>
+  // Shift left Imm <S> bits, then sign-extend the number in the bottom <N+S>
   // bits.
-  Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm) << S));
+  Inst.addOperand(MCOperand::createImm(SignExtend64<N + S>(Imm << S)));
   return MCDisassembler::Success;
 }