diff --git a/src/cpu/reg_class.cc b/src/cpu/reg_class.cc index 51b607af63..444d1fff14 100644 --- a/src/cpu/reg_class.cc +++ b/src/cpu/reg_class.cc @@ -43,6 +43,8 @@ #include #include "base/cprintf.hh" +#include "sim/bufval.hh" +#include "sim/byteswap.hh" namespace gem5 { @@ -58,42 +60,15 @@ RegClassOps::valString(const void *val, size_t size) const { // If this is just a RegVal, or could be interpreted as one, print it // that way. - if (size == sizeof(uint64_t)) - return csprintf("0x%016x", *(const uint64_t *)val); - else if (size == sizeof(uint32_t)) - return csprintf("0x%08x", *(const uint32_t *)val); - else if (size == sizeof(uint16_t)) - return csprintf("0x%04x", *(const uint16_t *)val); - else if (size == sizeof(uint8_t)) - return csprintf("0x%02x", *(const uint8_t *)val); + auto [reg_val_str, reg_val_success] = printUintX(val, size, HostByteOrder); + if (reg_val_success) + return reg_val_str; - // Otherwise, print it as a sequence of bytes, 4 in a chunk, separated by - // spaces, and all surrounded by []s. + // Otherwise, print it as a sequence of bytes. Use big endian order so + // that the most significant bytes are printed first, like digits in a + // regular number. - std::stringstream out; - ccprintf(out, "["); - - constexpr size_t chunk_size = 4; - const uint8_t *bytes = (const uint8_t *)val; - - while (size >= chunk_size) { - size -= chunk_size; - if (size) { - ccprintf(out, "%02x%02x%02x%02x ", bytes[0], bytes[1], bytes[2], - bytes[3]); - } else { - ccprintf(out, "%02x%02x%02x%02x", bytes[0], bytes[1], bytes[2], - bytes[3]); - } - bytes += chunk_size; - } - - while (size--) - ccprintf(out, "%02x", *bytes++); - - ccprintf(out, "]"); - - return out.str(); + return printByteBuf(val, size, ByteOrder::big); } const char *RegId::regClassStrings[] = { diff --git a/src/mem/packet.cc b/src/mem/packet.cc index 5b23f136d9..24c3d9cf34 100644 --- a/src/mem/packet.cc +++ b/src/mem/packet.cc @@ -57,6 +57,7 @@ #include "base/logging.hh" #include "base/trace.hh" #include "mem/packet_access.hh" +#include "sim/bufval.hh" namespace gem5 { @@ -345,40 +346,17 @@ Packet::popSenderState() uint64_t Packet::getUintX(ByteOrder endian) const { - switch(getSize()) { - case 1: - return (uint64_t)get(endian); - case 2: - return (uint64_t)get(endian); - case 4: - return (uint64_t)get(endian); - case 8: - return (uint64_t)get(endian); - default: - panic("%i isn't a supported word size.\n", getSize()); - } + auto [val, success] = + gem5::getUintX(getConstPtr(), getSize(), endian); + panic_if(!success, "%i isn't a supported word size.\n", getSize()); + return val; } void Packet::setUintX(uint64_t w, ByteOrder endian) { - switch(getSize()) { - case 1: - set((uint8_t)w, endian); - break; - case 2: - set((uint16_t)w, endian); - break; - case 4: - set((uint32_t)w, endian); - break; - case 8: - set((uint64_t)w, endian); - break; - default: - panic("%i isn't a supported word size.\n", getSize()); - } - + bool success = gem5::setUintX(w, getPtr(), getSize(), endian); + panic_if(!success, "%i isn't a supported word size.\n", getSize()); } void