Skip to content

Commit

Permalink
general: Avoid shift undefined behavior
Browse files Browse the repository at this point in the history
Shifts by 64 are not allowed, so add a conditional guard. Likewise,
left-shifting negative numbers is not allowed in C (unlike C++20, where
it is defined), so avoid such cases.
  • Loading branch information
aengelke committed Sep 30, 2024
1 parent be693cd commit 3d03d56
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ static uint64_t immlogical(unsigned sf, unsigned N, unsigned immr,
unsigned esize = 1 << len;
uint64_t welem = ((uint64_t)1 << (s + 1)) - 1;
// ROR(welem, r) as bits(esize)
welem = (welem >> r) | (welem << (esize - r));
if (r)
welem = (welem >> r) | (welem << (esize - r));
if (esize < 64)
welem &= ((uint64_t)1 << esize) - 1;
// Replicate(ROR(welem, r))
Expand Down
6 changes: 3 additions & 3 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ def add_mnem(self, grp: str, name: str, grpdesc: Desc, descmask: MaskedVal, ops:
# use XOR s.t. immadd can toggle between add and sub.
buildexprs = ["(" + "^".join([f"{descmask.val:#x}"] + grandExpansions) + ")"]
for de in grpdesc:
bitmask = ((1 << de.size) - 1) << de.idx
bitmask = (1 << de.size) - 1
if de.name in assignments:
buildexprs.append(f"(({assignments[de.name]})<<{de.idx}&{bitmask:#x})")
elif descmask.mask & bitmask != bitmask:
buildexprs.append(f"((({assignments[de.name]})&{bitmask:#x})<<{de.idx})")
elif descmask.mask & (bitmask << de.idx) != (bitmask << de.idx):
print("Encode desc missing for", grp, name, de.name, assignments)
return
expr = "|".join(buildexprs)
Expand Down

0 comments on commit 3d03d56

Please sign in to comment.