C++/C#: Refactor some integer constant code

Make `bitsToBytesAndBits` omit the leftover bits if zero.
This commit is contained in:
Dave Bartolomeo
2019-08-14 13:54:52 -07:00
committed by Dave Bartolomeo
parent 51ff262cbc
commit df21835759
4 changed files with 68 additions and 2 deletions

View File

@@ -192,3 +192,36 @@ predicate isGT(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a > b }
*/
bindingset[a, b]
predicate isGE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a >= b }
/**
* Converts the bit count in `bits` to a byte count and a bit count in the form
* "bytes:bits". If `bits` represents an integer number of bytes, the ":bits" section is omitted.
* If `bits` does not have a known value, the result is "?".
*/
bindingset[bits]
string bitsToBytesAndBits(IntValue bits) {
exists(int bytes, int leftoverBits |
hasValue(bits) and
bytes = bits / 8 and
leftoverBits = bits % 8 and
if leftoverBits = 0 then
result = bytes.toString()
else
result = bytes + ":" + leftoverBits
) or
not hasValue(bits) and result = "?"
}
/**
* Gets a printable string for a bit offset with possibly unknown value.
*/
bindingset[bitOffset]
string getBitOffsetString(IntValue bitOffset) {
if hasValue(bitOffset) then
if bitOffset >= 0 then
result = "+" + bitsToBytesAndBits(bitOffset)
else
result = "-" + bitsToBytesAndBits(neg(bitOffset))
else
result = "+?"
}

View File

@@ -30,5 +30,5 @@ Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntVal
bindingset[start, end]
string getIntervalString(IntValue start, IntValue end) {
// We represent an interval has half-open, so print it as "[start..end)".
result = "[" + intValueToString(start) + ".." + intValueToString(end) + ")"
result = "[" + bitsToBytesAndBits(start) + ".." + bitsToBytesAndBits(end) + ")"
}