Skip to content

Commit

Permalink
wrapping u32
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Dec 2, 2023
1 parent 54fa9d8 commit 58f892e
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions arch/TriCore/TriCoreInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -288,16 +275,16 @@ 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:
res = DISP2(24);
break;
}

printUInt32Bang(O, to_u32(res));
printUInt32Bang(O, wrapping_u32(res));
fill_imm(MI, res);
} else
printOperand(MI, OpNum, O);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 58f892e

Please sign in to comment.