@@ -198,11 +198,7 @@ static DecodeStatus DecodePCRelLabel16(MCInst &Inst, unsigned Imm,
198198static DecodeStatus DecodePCRelLabel19 (MCInst &Inst, unsigned Imm,
199199 uint64_t Addr,
200200 const MCDisassembler *Decoder) {
201- int64_t ImmVal = Imm;
202-
203- // Sign-extend 19-bit immediate.
204- if (ImmVal & (1 << (19 - 1 )))
205- ImmVal |= ~((1LL << 19 ) - 1 );
201+ int64_t ImmVal = SignExtend64<19 >(Imm);
206202
207203 if (!Decoder->tryAddingSymbolicOperand (
208204 Inst, ImmVal * 4 , Addr, Inst.getOpcode () != AArch64::LDRXl, 0 , 0 , 4 ))
@@ -212,11 +208,7 @@ static DecodeStatus DecodePCRelLabel19(MCInst &Inst, unsigned Imm,
212208
213209static DecodeStatus DecodePCRelLabel9 (MCInst &Inst, unsigned Imm, uint64_t Addr,
214210 const MCDisassembler *Decoder) {
215- int64_t ImmVal = Imm;
216-
217- // Sign-extend 9-bit immediate.
218- if (ImmVal & (1 << (9 - 1 )))
219- ImmVal |= ~((1LL << 9 ) - 1 );
211+ int64_t ImmVal = SignExtend64<9 >(Imm);
220212
221213 if (!Decoder->tryAddingSymbolicOperand (Inst, (ImmVal * 4 ), Addr,
222214 /* IsBranch=*/ true , 0 , 0 , 4 ))
@@ -533,12 +525,7 @@ static DecodeStatus DecodeSignedLdStInstruction(MCInst &Inst, uint32_t insn,
533525 const MCDisassembler *Decoder) {
534526 unsigned Rt = fieldFromInstruction (insn, 0 , 5 );
535527 unsigned Rn = fieldFromInstruction (insn, 5 , 5 );
536- int64_t offset = fieldFromInstruction (insn, 12 , 9 );
537-
538- // offset is a 9-bit signed immediate, so sign extend it to
539- // fill the unsigned.
540- if (offset & (1 << (9 - 1 )))
541- offset |= ~((1LL << 9 ) - 1 );
528+ int64_t offset = SignExtend64<9 >(fieldFromInstruction (insn, 12 , 9 ));
542529
543530 // First operand is always the writeback to the address register, if needed.
544531 switch (Inst.getOpcode ()) {
@@ -835,14 +822,9 @@ static DecodeStatus DecodePairLdStInstruction(MCInst &Inst, uint32_t insn,
835822 unsigned Rt = fieldFromInstruction (insn, 0 , 5 );
836823 unsigned Rn = fieldFromInstruction (insn, 5 , 5 );
837824 unsigned Rt2 = fieldFromInstruction (insn, 10 , 5 );
838- int64_t offset = fieldFromInstruction (insn, 15 , 7 );
825+ int64_t offset = SignExtend64< 7 >( fieldFromInstruction (insn, 15 , 7 ) );
839826 bool IsLoad = fieldFromInstruction (insn, 22 , 1 );
840827
841- // offset is a 7-bit signed immediate, so sign extend it to
842- // fill the unsigned.
843- if (offset & (1 << (7 - 1 )))
844- offset |= ~((1LL << 7 ) - 1 );
845-
846828 unsigned Opcode = Inst.getOpcode ();
847829 bool NeedsDisjointWritebackTransfer = false ;
848830
@@ -1211,12 +1193,8 @@ static DecodeStatus DecodeAdrInstruction(MCInst &Inst, uint32_t insn,
12111193 uint64_t Addr,
12121194 const MCDisassembler *Decoder) {
12131195 unsigned Rd = fieldFromInstruction (insn, 0 , 5 );
1214- int64_t imm = fieldFromInstruction (insn, 5 , 19 ) << 2 ;
1215- imm |= fieldFromInstruction (insn, 29 , 2 );
1216-
1217- // Sign-extend the 21-bit immediate.
1218- if (imm & (1 << (21 - 1 )))
1219- imm |= ~((1LL << 21 ) - 1 );
1196+ int64_t imm = SignExtend64<21 >((fieldFromInstruction (insn, 5 , 19 ) << 2 ) |
1197+ fieldFromInstruction (insn, 29 , 2 ));
12201198
12211199 DecodeSimpleRegisterClass<AArch64::GPR64RegClassID, 0 , 32 >(Inst, Rd, Addr,
12221200 Decoder);
@@ -1270,11 +1248,7 @@ static DecodeStatus DecodeAddSubImmShift(MCInst &Inst, uint32_t insn,
12701248static DecodeStatus DecodeUnconditionalBranch (MCInst &Inst, uint32_t insn,
12711249 uint64_t Addr,
12721250 const MCDisassembler *Decoder) {
1273- int64_t imm = fieldFromInstruction (insn, 0 , 26 );
1274-
1275- // Sign-extend the 26-bit immediate.
1276- if (imm & (1 << (26 - 1 )))
1277- imm |= ~((1LL << 26 ) - 1 );
1251+ int64_t imm = SignExtend64<26 >(fieldFromInstruction (insn, 0 , 26 ));
12781252
12791253 if (!Decoder->tryAddingSymbolicOperand (Inst, imm * 4 , Addr, true , 0 , 0 , 4 ))
12801254 Inst.addOperand (MCOperand::createImm (imm));
@@ -1337,11 +1311,7 @@ static DecodeStatus DecodeTestAndBranch(MCInst &Inst, uint32_t insn,
13371311 uint64_t Rt = fieldFromInstruction (insn, 0 , 5 );
13381312 uint64_t bit = fieldFromInstruction (insn, 31 , 1 ) << 5 ;
13391313 bit |= fieldFromInstruction (insn, 19 , 5 );
1340- int64_t dst = fieldFromInstruction (insn, 5 , 14 );
1341-
1342- // Sign-extend 14-bit immediate.
1343- if (dst & (1 << (14 - 1 )))
1344- dst |= ~((1LL << 14 ) - 1 );
1314+ int64_t dst = SignExtend64<14 >(fieldFromInstruction (insn, 5 , 14 ));
13451315
13461316 if (fieldFromInstruction (insn, 31 , 1 ) == 0 )
13471317 DecodeSimpleRegisterClass<AArch64::GPR32RegClassID, 0 , 32 >(Inst, Rt, Addr,
0 commit comments