From 58f892e4905bc29397bbdfef584f29b7ccf11711 Mon Sep 17 00:00:00 2001 From: billow Date: Fri, 1 Dec 2023 20:47:52 -0800 Subject: [PATCH] wrapping u32 --- arch/TriCore/TriCoreInstPrinter.c | 42 +++++++++++-------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/arch/TriCore/TriCoreInstPrinter.c b/arch/TriCore/TriCoreInstPrinter.c index ea1f423cafa..410fe103b44 100644 --- a/arch/TriCore/TriCoreInstPrinter.c +++ b/arch/TriCore/TriCoreInstPrinter.c @@ -39,22 +39,9 @@ static void printOperand(MCInst *MI, int OpNum, SStream *O); #include "TriCoreGenRegisterInfo.inc" -static uint32_t to_u32(int64_t x) +static uint32_t wrapping_u32(int64_t x) { - if (x > UINT32_MAX || x < -(int64_t)(UINT32_MAX)) { - abort(); - } - return (uint32_t)x; -} - -static inline unsigned int get_msb(uint64_t value) -{ - unsigned int msb = 0; - while (value > 0) { - value >>= 1; // Shift bits to the right - msb++; // Increment the position of the MSB - } - return msb; + return (uint32_t)(x %= UINT32_MAX); } static bool fill_mem(MCInst *MI, unsigned int reg, int64_t disp); @@ -179,7 +166,7 @@ static void printOperand(MCInst *MI, int OpNum, SStream *O) fill_reg(MI, reg); } else if (MCOperand_isImm(Op)) { int64_t Imm = MCOperand_getImm(Op); - printUInt32Bang(O, to_u32(Imm)); + printUInt32Bang(O, wrapping_u32(Imm)); fill_imm(MI, Imm); } } @@ -189,7 +176,7 @@ static void print_sign_ext(MCInst *MI, int OpNum, SStream *O, unsigned n) MCOperand *MO = MCInst_getOperand(MI, OpNum); if (MCOperand_isImm(MO)) { int64_t imm = MCOperand_getImm(MO); - int32_t res = SignExtend32(to_u32(imm), n); + int32_t res = SignExtend32(wrapping_u32(imm), n); printInt32Bang(O, res); fill_imm(MI, res); } else @@ -248,7 +235,7 @@ static void print_zero_ext(MCInst *MI, int OpNum, SStream *O, unsigned n) const8_fixup(MI, &imm); } - printUInt32Bang(O, to_u32(imm)); + printUInt32Bang(O, wrapping_u32(imm)); fill_imm(MI, imm); } else printOperand(MI, OpNum, O); @@ -259,8 +246,8 @@ static void printOff18Imm(MCInst *MI, int OpNum, SStream *O) MCOperand *MO = MCInst_getOperand(MI, OpNum); if (MCOperand_isImm(MO)) { int64_t imm = MCOperand_getImm(MO); - imm = ((to_u32(imm) & 0x3C000) << 14) | (to_u32(imm) & 0x3fff); - printUInt32Bang(O, to_u32(imm)); + imm = ((wrapping_u32(imm) & 0x3C000) << 14) | (wrapping_u32(imm) & 0x3fff); + printUInt32Bang(O, wrapping_u32(imm)); fill_imm(MI, imm); } else printOperand(MI, OpNum, O); @@ -288,8 +275,8 @@ static void printDisp24Imm(MCInst *MI, int OpNum, SStream *O) case TRICORE_JA_b: case TRICORE_JLA_b: // = {disp24[23:20], 7’b0000000, disp24[19:0], 1’b0}; - res = ((to_u32(disp) & 0xf00000) << 28) | - ((to_u32(disp) & 0xfffff) << 1); + res = ((wrapping_u32(disp) & 0xf00000) << 28) | + ((wrapping_u32(disp) & 0xfffff) << 1); break; case TRICORE_J_b: case TRICORE_JL_b: @@ -297,7 +284,7 @@ static void printDisp24Imm(MCInst *MI, int OpNum, SStream *O) break; } - printUInt32Bang(O, to_u32(res)); + printUInt32Bang(O, wrapping_u32(res)); fill_imm(MI, res); } else printOperand(MI, OpNum, O); @@ -343,7 +330,7 @@ static void printDisp15Imm(MCInst *MI, int OpNum, SStream *O) break; } - printUInt32Bang(O, to_u32(res)); + printUInt32Bang(O, wrapping_u32(res)); fill_imm(MI, res); } else printOperand(MI, OpNum, O); @@ -369,7 +356,7 @@ static void printDisp8Imm(MCInst *MI, int OpNum, SStream *O) break; } - printUInt32Bang(O, to_u32(res)); + printUInt32Bang(O, wrapping_u32(res)); fill_imm(MI, res); } else printOperand(MI, OpNum, O); @@ -408,14 +395,15 @@ static void printDisp4Imm(MCInst *MI, int OpNum, SStream *O) break; case TRICORE_LOOP_sbr: // PC + {27b’111111111111111111111111111, disp4, 0}; - res = (int64_t) MI->address + OneExtend32(to_u32(disp) << 1, 5); + res = (int64_t)MI->address + + OneExtend32(wrapping_u32(disp) << 1, 5); break; default: // handle other cases, if any break; } - printUInt32Bang(O, (uint32_t) res); + printUInt32Bang(O, (uint32_t)res); fill_imm(MI, res); } else printOperand(MI, OpNum, O);